265 lines
9.3 KiB
Dart
265 lines
9.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pipelines_app/application/app_route.dart';
|
|
import 'package:pipelines_app/application/app_router.dart';
|
|
import 'package:pipelines_app/shell/adaptive_shell.dart';
|
|
import 'package:pipelines_app/shell/destination.dart';
|
|
import 'package:pipelines_app/shell/detail_pane_navigator.dart';
|
|
import 'package:pipelines_app/shell/tab_history.dart';
|
|
|
|
/// Records the platform's own Back (closing the app) instead of running it.
|
|
List<String> _recordExits(WidgetTester tester) {
|
|
final exits = <String>[];
|
|
final messenger = tester.binding.defaultBinaryMessenger;
|
|
messenger.setMockMethodCallHandler(SystemChannels.platform, (call) async {
|
|
if (call.method == 'SystemNavigator.pop') exits.add(call.method);
|
|
return null;
|
|
});
|
|
addTearDown(
|
|
() => messenger.setMockMethodCallHandler(SystemChannels.platform, null),
|
|
);
|
|
return exits;
|
|
}
|
|
|
|
Future<void> _back(WidgetTester tester) async {
|
|
await tester.binding.handlePopRoute();
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
Future<void> _tapTab(WidgetTester tester, AppDestination tab) async {
|
|
await tester.tap(find.byKey(ValueKey('shell.destination.${tab.name}')));
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
Map<AppDestination, WidgetBuilder> _bodies({
|
|
Map<AppDestination, WidgetBuilder> overrides = const {},
|
|
}) => {
|
|
for (final destination in AppDestination.values)
|
|
destination: (_) => Center(child: Text('body ${destination.name}')),
|
|
...overrides,
|
|
};
|
|
|
|
void _expectShown(AppDestination destination) =>
|
|
expect(find.text('body ${destination.name}'), findsOneWidget);
|
|
|
|
void main() {
|
|
group('TabHistory', () {
|
|
test('keeps left tabs most recent first, without duplicates', () {
|
|
final history = TabHistory();
|
|
history.record(AppDestination.pipelines, AppDestination.runs);
|
|
history.record(AppDestination.runs, AppDestination.chat);
|
|
history.record(AppDestination.chat, AppDestination.pipelines);
|
|
expect(history.tabs, [AppDestination.chat, AppDestination.runs]);
|
|
history.record(AppDestination.pipelines, AppDestination.runs);
|
|
expect(history.tabs, [AppDestination.pipelines, AppDestination.chat]);
|
|
});
|
|
|
|
test('a page counts as its tab, and Back builds no forward trail', () {
|
|
final history = TabHistory();
|
|
history.record(AppDestination.runs, AppDestination.pipelines);
|
|
history.record(AppDestination.pipelines, AppDestination.settings);
|
|
expect(history.tabs, [AppDestination.runs]);
|
|
history.record(AppDestination.settings, AppDestination.chat);
|
|
expect(history.tabs, [AppDestination.pipelines, AppDestination.runs]);
|
|
|
|
expect(history.takePrevious(), AppDestination.pipelines);
|
|
history.record(AppDestination.chat, AppDestination.pipelines);
|
|
expect(history.tabs, [AppDestination.runs]);
|
|
expect(history.takePrevious(), AppDestination.runs);
|
|
history.record(AppDestination.pipelines, AppDestination.runs);
|
|
expect(history.isEmpty, isTrue);
|
|
expect(history.takePrevious(), isNull);
|
|
});
|
|
});
|
|
|
|
testWidgets('Back returns through the visited tabs, then to the platform', (
|
|
tester,
|
|
) async {
|
|
final exits = _recordExits(tester);
|
|
await tester.pumpWidget(
|
|
MaterialApp(home: AdaptiveShell(destinationBodies: _bodies())),
|
|
);
|
|
await _tapTab(tester, AppDestination.runs);
|
|
await _tapTab(tester, AppDestination.chat);
|
|
// Revisiting Runs moves it to the top instead of adding it twice.
|
|
await _tapTab(tester, AppDestination.runs);
|
|
await _tapTab(tester, AppDestination.chat);
|
|
_expectShown(AppDestination.chat);
|
|
|
|
await _back(tester);
|
|
_expectShown(AppDestination.runs);
|
|
await _back(tester);
|
|
_expectShown(AppDestination.pipelines);
|
|
expect(exits, isEmpty);
|
|
|
|
// The initial tab with nothing open: the platform's Back applies.
|
|
await _back(tester);
|
|
_expectShown(AppDestination.pipelines);
|
|
expect(exits, ['SystemNavigator.pop']);
|
|
});
|
|
|
|
testWidgets('Ctrl+Tab no longer switches destinations or changes history', (
|
|
tester,
|
|
) async {
|
|
final exits = _recordExits(tester);
|
|
await tester.pumpWidget(
|
|
MaterialApp(home: AdaptiveShell(destinationBodies: _bodies())),
|
|
);
|
|
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
|
|
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
|
|
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
|
|
await tester.pumpAndSettle();
|
|
_expectShown(AppDestination.pipelines);
|
|
|
|
await _back(tester);
|
|
_expectShown(AppDestination.pipelines);
|
|
expect(exits, ['SystemNavigator.pop']);
|
|
});
|
|
|
|
testWidgets('a page open in the tab closes before Back changes tabs', (
|
|
tester,
|
|
) async {
|
|
final exits = _recordExits(tester);
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
home: AdaptiveShell(
|
|
destinationBodies: _bodies(
|
|
overrides: {
|
|
AppDestination.chat: (context) => Center(
|
|
child: TextButton(
|
|
onPressed: () => Navigator.of(context).push(
|
|
MaterialPageRoute<void>(
|
|
builder: (_) => const Scaffold(body: Text('open item')),
|
|
),
|
|
),
|
|
child: const Text('body chat'),
|
|
),
|
|
),
|
|
},
|
|
),
|
|
),
|
|
),
|
|
);
|
|
await _tapTab(tester, AppDestination.runs);
|
|
await _tapTab(tester, AppDestination.chat);
|
|
await tester.tap(find.text('body chat'));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('open item'), findsOneWidget);
|
|
|
|
await _back(tester);
|
|
expect(find.text('open item'), findsNothing);
|
|
_expectShown(AppDestination.chat);
|
|
|
|
await _back(tester);
|
|
_expectShown(AppDestination.runs);
|
|
expect(exits, isEmpty);
|
|
});
|
|
|
|
testWidgets('a page in the open item\'s pane closes before Back changes '
|
|
'tabs, and only in the shown tab', (tester) async {
|
|
final exits = _recordExits(tester);
|
|
Widget pane(String name) => DetailPaneNavigator(
|
|
child: Builder(
|
|
builder: (context) => Center(
|
|
child: TextButton(
|
|
onPressed: () => Navigator.of(
|
|
context,
|
|
).push(MaterialPageRoute<void>(builder: (_) => Text('$name page'))),
|
|
child: Text('body $name'),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
home: AdaptiveShell(
|
|
destinationBodies: _bodies(
|
|
overrides: {
|
|
AppDestination.runs: (_) => pane('runs'),
|
|
AppDestination.chat: (_) => pane('chat'),
|
|
},
|
|
),
|
|
),
|
|
),
|
|
);
|
|
await _tapTab(tester, AppDestination.runs);
|
|
await tester.tap(find.text('body runs'));
|
|
await tester.pumpAndSettle();
|
|
await _tapTab(tester, AppDestination.chat);
|
|
await tester.tap(find.text('body chat'));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('chat page'), findsOneWidget);
|
|
|
|
await _back(tester);
|
|
expect(find.text('chat page'), findsNothing);
|
|
_expectShown(AppDestination.chat);
|
|
// Runs' own pane page was left alone.
|
|
expect(find.text('runs page', skipOffstage: false), findsOneWidget);
|
|
|
|
await _back(tester);
|
|
expect(find.text('runs page'), findsOneWidget);
|
|
expect(exits, isEmpty);
|
|
});
|
|
|
|
testWidgets('routed tab changes push the history; a page still closes to '
|
|
'its tab first', (tester) async {
|
|
final exits = _recordExits(tester);
|
|
late final AppRouterDelegate router;
|
|
AppDestination destinationOf(AppRoute route) => switch (route.kind) {
|
|
AppRouteKind.runs => AppDestination.runs,
|
|
AppRouteKind.chat => AppDestination.chat,
|
|
AppRouteKind.settings => AppDestination.settings,
|
|
_ => AppDestination.pipelines,
|
|
};
|
|
AppRoute routeOf(AppDestination destination) => switch (destination) {
|
|
AppDestination.runs => AppRoute.runs,
|
|
AppDestination.chat => AppRoute.chat,
|
|
AppDestination.settings => AppRoute.settings,
|
|
_ => AppRoute.pipelines,
|
|
};
|
|
router = AppRouterDelegate(
|
|
initial: AppRoute.runs,
|
|
cupertino: false,
|
|
builder: (_, route) => AdaptiveShell(
|
|
destination: destinationOf(route),
|
|
onDestinationSelected: (value) => router.goTo(routeOf(value)),
|
|
destinationBodies: _bodies(),
|
|
),
|
|
);
|
|
addTearDown(router.dispose);
|
|
final provider = PlatformRouteInformationProvider(
|
|
initialRouteInformation: RouteInformation(uri: Uri.parse('/runs')),
|
|
);
|
|
addTearDown(provider.dispose);
|
|
await tester.pumpWidget(
|
|
MaterialApp.router(
|
|
routerDelegate: router,
|
|
routeInformationParser: const AppRouteParser(),
|
|
routeInformationProvider: provider,
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
// A programmatic open (a search result or notification) changes tabs.
|
|
router.goTo(AppRoute.chatSession('s'));
|
|
await tester.pumpAndSettle();
|
|
_expectShown(AppDestination.chat);
|
|
router.goTo(AppRoute.settings);
|
|
await tester.pumpAndSettle();
|
|
_expectShown(AppDestination.settings);
|
|
|
|
// Settings closes to Pipelines first, as it always has.
|
|
await _back(tester);
|
|
expect(router.route, AppRoute.pipelines);
|
|
await _back(tester);
|
|
expect(router.route.kind, AppRouteKind.chat);
|
|
await _back(tester);
|
|
expect(router.route, AppRoute.runs);
|
|
expect(exits, isEmpty);
|
|
await _back(tester);
|
|
expect(exits, ['SystemNavigator.pop']);
|
|
await tester.pumpWidget(const SizedBox.shrink());
|
|
});
|
|
}
|