Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-11-26 01:10:39 +00:00

215 lines
5.8 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/foundation.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
void main() {
Future<void> setAppLifeCycleState(AppLifecycleState state) async {
final ByteData? message = const StringCodec().encodeMessage(state.toString());
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
'flutter/lifecycle',
message,
(_) {},
);
}
testWidgets('Ticker mute control test', (WidgetTester tester) async {
var tickCount = 0;
void handleTick(Duration duration) {
tickCount += 1;
}
final ticker = Ticker(handleTick);
addTearDown(ticker.dispose);
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isFalse);
ticker.start();
expect(ticker.isTicking, isTrue);
expect(ticker.isActive, isTrue);
expect(tickCount, equals(0));
FlutterError? error;
try {
ticker.start();
} on FlutterError catch (e) {
error = e;
}
expect(error, isNotNull);
expect(error!.diagnostics.length, 3);
expect(error.diagnostics.last, isA<DiagnosticsProperty<Ticker>>());
expect(
error.toStringDeep(),
startsWith(
'FlutterError\n'
' A ticker was started twice.\n'
' A ticker that is already active cannot be started again without\n'
' first stopping it.\n'
' The affected ticker was:\n'
' Ticker()\n',
),
);
await tester.pump(const Duration(milliseconds: 10));
expect(tickCount, equals(1));
ticker.muted = true;
await tester.pump(const Duration(milliseconds: 10));
expect(tickCount, equals(1));
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isTrue);
ticker.muted = false;
await tester.pump(const Duration(milliseconds: 10));
expect(tickCount, equals(2));
expect(ticker.isTicking, isTrue);
expect(ticker.isActive, isTrue);
ticker.muted = true;
await tester.pump(const Duration(milliseconds: 10));
expect(tickCount, equals(2));
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isTrue);
ticker.stop();
expect(tickCount, equals(2));
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isFalse);
ticker.muted = false;
expect(tickCount, equals(2));
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isFalse);
await tester.pump(const Duration(milliseconds: 10));
expect(tickCount, equals(2));
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isFalse);
});
testWidgets('Ticker control test', (WidgetTester tester) async {
late Ticker ticker;
addTearDown(() => ticker.dispose());
void testFunction() {
ticker = Ticker((Duration _) {});
}
testFunction();
expect(ticker, hasOneLineDescription);
expect(ticker.toString(debugIncludeStack: true), contains('testFunction'));
});
testWidgets('Ticker can be sped up with time dilation', (WidgetTester tester) async {
timeDilation = 0.5; // Move twice as fast.
late Duration lastDuration;
void handleTick(Duration duration) {
lastDuration = duration;
}
final ticker = Ticker(handleTick);
ticker.start();
await tester.pump(const Duration(milliseconds: 10));
await tester.pump(const Duration(milliseconds: 10));
expect(lastDuration, const Duration(milliseconds: 20));
ticker.dispose();
timeDilation = 1.0; // restore time dilation, or it will affect other tests
});
testWidgets('Ticker can be slowed down with time dilation', (WidgetTester tester) async {
timeDilation = 2.0; // Move half as fast.
late Duration lastDuration;
void handleTick(Duration duration) {
lastDuration = duration;
}
final ticker = Ticker(handleTick);
ticker.start();
await tester.pump(const Duration(milliseconds: 10));
await tester.pump(const Duration(milliseconds: 10));
expect(lastDuration, const Duration(milliseconds: 5));
ticker.dispose();
timeDilation = 1.0; // restore time dilation, or it will affect other tests
});
testWidgets('Ticker stops ticking when application is paused', (WidgetTester tester) async {
var tickCount = 0;
void handleTick(Duration duration) {
tickCount += 1;
}
final ticker = Ticker(handleTick);
addTearDown(ticker.dispose);
ticker.start();
expect(ticker.isTicking, isTrue);
expect(ticker.isActive, isTrue);
expect(tickCount, equals(0));
setAppLifeCycleState(AppLifecycleState.paused);
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isTrue);
ticker.stop();
setAppLifeCycleState(AppLifecycleState.resumed);
});
testWidgets('Ticker can be created before application unpauses', (WidgetTester tester) async {
setAppLifeCycleState(AppLifecycleState.paused);
var tickCount = 0;
void handleTick(Duration duration) {
tickCount += 1;
}
final ticker = Ticker(handleTick);
addTearDown(ticker.dispose);
ticker.start();
expect(tickCount, equals(0));
expect(ticker.isTicking, isFalse);
await tester.pump(const Duration(milliseconds: 10));
expect(tickCount, equals(0));
expect(ticker.isTicking, isFalse);
setAppLifeCycleState(AppLifecycleState.resumed);
await tester.pump(const Duration(milliseconds: 10));
expect(tickCount, equals(1));
expect(ticker.isTicking, isTrue);
ticker.stop();
});
test('Ticker dispatches memory events', () async {
await expectLater(
await memoryEvents(() => Ticker((_) {}).dispose(), Ticker),
areCreateAndDispose,
);
});
}