7ff012880b
Provide composable shown/current visibility for retained surfaces and migrate visibility readers away from TickerMode. This keeps ticker muting separate from painted and current-surface state.
114 lines
3.7 KiB
Dart
114 lines
3.7 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/ui/motion/surface_visibility.dart';
|
|
import 'package:pipelines_app/ui/text/elapsed_clock.dart';
|
|
import 'package:pipelines_app/ui/text/live_elapsed_builder.dart';
|
|
|
|
/// A fake wall clock the test advances together with the fake timers.
|
|
class _Time {
|
|
_Time(this.value);
|
|
DateTime value;
|
|
Future<void> advance(WidgetTester tester, Duration by) async {
|
|
value = value.add(by);
|
|
await tester.pump(by);
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
test('wakes when the displayed precision next changes', () {
|
|
expect(elapsedTextChangesIn(Duration.zero), const Duration(seconds: 1));
|
|
expect(
|
|
elapsedTextChangesIn(const Duration(seconds: 59, milliseconds: 400)),
|
|
const Duration(milliseconds: 600),
|
|
);
|
|
// `1h 2m` shows minutes, `2d 3h` hours.
|
|
expect(
|
|
elapsedTextChangesIn(const Duration(hours: 1, minutes: 2, seconds: 15)),
|
|
const Duration(seconds: 45),
|
|
);
|
|
expect(
|
|
elapsedTextChangesIn(const Duration(days: 2, minutes: 30)),
|
|
const Duration(minutes: 30),
|
|
);
|
|
// Clock skew reads 0s until a second has passed.
|
|
expect(
|
|
elapsedTextChangesIn(const Duration(seconds: -2)),
|
|
const Duration(seconds: 3),
|
|
);
|
|
});
|
|
|
|
testWidgets('a live label ticks, stops when no longer running and on '
|
|
'dispose', (tester) async {
|
|
final start = DateTime.utc(2026, 11, 4, 12);
|
|
final time = _Time(start.add(const Duration(seconds: 5)));
|
|
final clock = ElapsedClock(now: () => time.value);
|
|
var builds = 0;
|
|
Widget app(DateTime? since) => Directionality(
|
|
textDirection: TextDirection.ltr,
|
|
child: ElapsedClockScope(
|
|
clock: clock,
|
|
child: LiveElapsedBuilder(
|
|
since: since,
|
|
builder: (context, now) {
|
|
builds += 1;
|
|
return Text('${now.difference(start).inSeconds}s');
|
|
},
|
|
),
|
|
),
|
|
);
|
|
|
|
await tester.pumpWidget(app(start));
|
|
expect(find.text('5s'), findsOneWidget);
|
|
expect(clock.ticking, isTrue);
|
|
await time.advance(tester, const Duration(seconds: 1));
|
|
expect(find.text('6s'), findsOneWidget);
|
|
await time.advance(tester, const Duration(seconds: 1));
|
|
expect(find.text('7s'), findsOneWidget);
|
|
|
|
// Finished: no longer watched, so the timer stops.
|
|
await tester.pumpWidget(app(null));
|
|
expect(clock.ticking, isFalse);
|
|
final settled = builds;
|
|
await time.advance(tester, const Duration(seconds: 5));
|
|
expect(builds, settled);
|
|
|
|
await tester.pumpWidget(app(start));
|
|
expect(clock.ticking, isTrue);
|
|
await tester.pumpWidget(const SizedBox());
|
|
expect(clock.ticking, isFalse);
|
|
});
|
|
|
|
testWidgets('a muted label stops ticking and catches up when shown', (
|
|
tester,
|
|
) async {
|
|
final start = DateTime.utc(2026, 11, 4, 12);
|
|
final time = _Time(start);
|
|
final clock = ElapsedClock(now: () => time.value);
|
|
Widget app({required bool shown}) => Directionality(
|
|
textDirection: TextDirection.ltr,
|
|
child: ElapsedClockScope(
|
|
clock: clock,
|
|
child: SurfaceVisibility(
|
|
shown: shown,
|
|
current: shown,
|
|
child: LiveElapsedBuilder(
|
|
since: start,
|
|
builder: (context, now) =>
|
|
Text('${now.difference(start).inSeconds}s'),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
await tester.pumpWidget(app(shown: true));
|
|
expect(clock.ticking, isTrue);
|
|
await tester.pumpWidget(app(shown: false));
|
|
expect(clock.ticking, isFalse);
|
|
await time.advance(tester, const Duration(seconds: 30));
|
|
await tester.pumpWidget(app(shown: true));
|
|
expect(find.text('30s'), findsOneWidget);
|
|
expect(clock.ticking, isTrue);
|
|
await tester.pumpWidget(const SizedBox());
|
|
});
|
|
}
|