99 lines
3.2 KiB
Dart
99 lines
3.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/shell/destination.dart';
|
|
import 'package:pipelines_app/shell/detail_page.dart';
|
|
import 'package:pipelines_app/shell/shell_bar.dart';
|
|
|
|
class _Owner extends StatefulWidget {
|
|
const _Owner({required this.selection});
|
|
|
|
final ValueNotifier<String?> selection;
|
|
|
|
@override
|
|
State<_Owner> createState() => _OwnerState();
|
|
}
|
|
|
|
class _OwnerState extends State<_Owner> {
|
|
final DetailPageHost page = DetailPageHost();
|
|
int closed = 0;
|
|
|
|
@override
|
|
void dispose() {
|
|
page.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => ValueListenableBuilder<String?>(
|
|
valueListenable: widget.selection,
|
|
builder: (context, selected, _) {
|
|
page.sync(
|
|
context,
|
|
selected: selected != null,
|
|
destination: AppDestination.runs,
|
|
onClosed: () {
|
|
closed++;
|
|
widget.selection.value = null;
|
|
},
|
|
builder: (context) =>
|
|
ShellBar(title: 'Run $selected', child: Text('detail $selected')),
|
|
);
|
|
return const Text('the list');
|
|
},
|
|
);
|
|
}
|
|
|
|
void main() {
|
|
testWidgets('a selection opens its page; Back closes it and clears it', (
|
|
tester,
|
|
) async {
|
|
final selection = ValueNotifier<String?>(null);
|
|
await tester.pumpWidget(MaterialApp(home: _Owner(selection: selection)));
|
|
expect(find.text('the list'), findsOneWidget);
|
|
|
|
selection.value = '7';
|
|
await tester.pumpAndSettle();
|
|
// A page of its own over the list, titled by its content.
|
|
expect(find.text('detail 7'), findsOneWidget);
|
|
expect(find.text('Run 7'), findsOneWidget);
|
|
expect(find.text('the list'), findsNothing);
|
|
|
|
// The open page follows the owner's later state.
|
|
selection.value = '8';
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('detail 8'), findsOneWidget);
|
|
|
|
await tester.tap(find.byKey(const ValueKey('shell.back')));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('the list'), findsOneWidget);
|
|
expect(selection.value, isNull);
|
|
final state = tester.state<_OwnerState>(find.byType(_Owner));
|
|
expect(state.closed, 1);
|
|
});
|
|
|
|
testWidgets('a selection cleared elsewhere removes the page', (tester) async {
|
|
final selection = ValueNotifier<String?>('3');
|
|
await tester.pumpWidget(MaterialApp(home: _Owner(selection: selection)));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('detail 3'), findsOneWidget);
|
|
|
|
selection.value = null;
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('detail 3'), findsNothing);
|
|
expect(find.text('the list'), findsOneWidget);
|
|
// Removed by the owner, so nothing is reported back.
|
|
expect(tester.state<_OwnerState>(find.byType(_Owner)).closed, 0);
|
|
});
|
|
|
|
testWidgets('the system Back closes the page first', (tester) async {
|
|
final selection = ValueNotifier<String?>('5');
|
|
await tester.pumpWidget(MaterialApp(home: _Owner(selection: selection)));
|
|
await tester.pumpAndSettle();
|
|
final navigator = tester.state<NavigatorState>(find.byType(Navigator));
|
|
expect(await navigator.maybePop(), isTrue);
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('the list'), findsOneWidget);
|
|
expect(selection.value, isNull);
|
|
});
|
|
}
|