Share one minute clock across time labels and narrow shell and runs rebuilds
Relative-time labels follow one wall-minute RelativeTimeClock instead of a timer per row, skip ticks while their tab is offstage and rebuild only when their text changes. The shell rebuilds its top bar only for the focused destination's published bar and its tab bar only for badge changes. Run events that repeat a row's state no longer notify, and the runs list filters and sorts once per build. Adds a rebuild/timer benchmark.
This commit is contained in:
@@ -1,45 +1,96 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/ui/text/relative_time.dart';
|
||||
import 'package:pipelines_app/ui/text/relative_time_clock.dart';
|
||||
import 'package:pipelines_app/ui/text/relative_time_label.dart';
|
||||
|
||||
/// The start of the current wall-clock minute, where the shared clock ticks.
|
||||
DateTime _minute() {
|
||||
final now = DateTime.now();
|
||||
return DateTime(now.year, now.month, now.day, now.hour, now.minute);
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('updates at the next relative-time boundary', (tester) async {
|
||||
final instant = DateTime.now().subtract(const Duration(minutes: 4));
|
||||
final clock = RelativeTimeClock.shared;
|
||||
|
||||
testWidgets('updates on the next minute tick', (tester) async {
|
||||
final instant = _minute().subtract(const Duration(minutes: 4));
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(home: RelativeTimeLabel(instant: instant)),
|
||||
);
|
||||
expect(find.text('4m'), findsOneWidget);
|
||||
|
||||
await tester.pump(const Duration(minutes: 1, seconds: 1));
|
||||
await tester.pump(const Duration(minutes: 1));
|
||||
expect(find.text('5m'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('future labels schedule their next boundary', (tester) async {
|
||||
final instant = DateTime.now().add(const Duration(minutes: 4));
|
||||
testWidgets('future labels count down on the ticks', (tester) async {
|
||||
final instant = _minute().add(
|
||||
const Duration(minutes: 4, milliseconds: 500),
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(home: RelativeTimeLabel(instant: instant)),
|
||||
);
|
||||
expect(find.text('in 3m'), findsOneWidget);
|
||||
expect(find.text(formatRelativeTime(instant)!), findsOneWidget);
|
||||
|
||||
await tester.pump(const Duration(minutes: 1, seconds: 1));
|
||||
await tester.pump(const Duration(minutes: 2));
|
||||
expect(find.text('in 2m'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('cancels its timer when disposed', (tester) async {
|
||||
testWidgets('many labels share one timer, released when disposed', (
|
||||
tester,
|
||||
) async {
|
||||
final instant = DateTime.now().subtract(const Duration(minutes: 4));
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: RelativeTimeLabel(
|
||||
instant: DateTime.now().subtract(const Duration(minutes: 4)),
|
||||
home: Wrap(
|
||||
children: [
|
||||
for (var i = 0; i < 50; i++) RelativeTimeLabel(instant: instant),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(clock.ticking, isTrue);
|
||||
await tester.pumpWidget(const MaterialApp(home: SizedBox()));
|
||||
expect(clock.ticking, isFalse);
|
||||
await tester.pump(const Duration(hours: 1));
|
||||
});
|
||||
|
||||
testWidgets('a muted label skips ticks and catches up when shown', (
|
||||
tester,
|
||||
) async {
|
||||
final instant = _minute().subtract(const Duration(minutes: 4));
|
||||
var builds = 0;
|
||||
Widget app(bool enabled) => MaterialApp(
|
||||
home: TickerMode(
|
||||
enabled: enabled,
|
||||
child: Builder(
|
||||
builder: (_) {
|
||||
builds++;
|
||||
return RelativeTimeLabel(instant: instant);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpWidget(app(false));
|
||||
final label = find.byType(RelativeTimeLabel);
|
||||
var labelBuilds = 0;
|
||||
debugOnRebuildDirtyWidget = (element, _) {
|
||||
if (element.widget is RelativeTimeLabel) labelBuilds++;
|
||||
};
|
||||
addTearDown(() => debugOnRebuildDirtyWidget = null);
|
||||
await tester.pump(const Duration(minutes: 1));
|
||||
expect(labelBuilds, 0);
|
||||
expect(find.descendant(of: label, matching: find.text('4m')), findsOneWidget);
|
||||
|
||||
await tester.pumpWidget(app(true));
|
||||
expect(find.text('5m'), findsOneWidget);
|
||||
expect(builds, 2);
|
||||
});
|
||||
|
||||
testWidgets('keeps ticking while inactive, pauses while hidden and '
|
||||
'refreshes on resume', (tester) async {
|
||||
final instant = DateTime.now().subtract(const Duration(minutes: 4));
|
||||
final instant = _minute().subtract(const Duration(minutes: 4));
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(home: RelativeTimeLabel(instant: instant)),
|
||||
);
|
||||
@@ -49,12 +100,13 @@ void main() {
|
||||
WidgetsBinding.instance.handleAppLifecycleStateChanged(
|
||||
AppLifecycleState.inactive,
|
||||
);
|
||||
await tester.pump(const Duration(minutes: 1, seconds: 1));
|
||||
await tester.pump(const Duration(minutes: 1));
|
||||
expect(find.text('5m'), findsOneWidget);
|
||||
|
||||
WidgetsBinding.instance.handleAppLifecycleStateChanged(
|
||||
AppLifecycleState.hidden,
|
||||
);
|
||||
expect(clock.ticking, isFalse);
|
||||
await tester.pump(const Duration(minutes: 1, seconds: 1));
|
||||
expect(find.text('5m'), findsOneWidget);
|
||||
|
||||
@@ -64,8 +116,8 @@ void main() {
|
||||
WidgetsBinding.instance.handleAppLifecycleStateChanged(
|
||||
AppLifecycleState.resumed,
|
||||
);
|
||||
expect(clock.ticking, isTrue);
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(minutes: 1, seconds: 1));
|
||||
expect(find.text('5m'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user