fix(app): restore standard Android back navigation

This commit is contained in:
2026-10-24 01:44:50 +01:00
parent ed22ef6e44
commit 7f88f14a52
10 changed files with 387 additions and 118 deletions
@@ -0,0 +1,151 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/application/app_router.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
import '../core/transport/pipelines_client_test.dart' show FakeChannel;
import '../features/pipelines/fixtures.dart';
import 'chat_registration_test.dart' as chat;
import 'pipelines_app_test.dart' as app;
import 'pipelines_replies.dart' as pipelines;
Future<void> backGesture(WidgetTester tester, String method) async {
await tester.binding.defaultBinaryMessenger.handlePlatformMessage(
'flutter/backgesture',
const StandardMethodCodec().encodeMethodCall(
MethodCall(
method,
method == 'startBackGesture'
? {
'touchOffset': <double>[5, 300],
'progress': 0.0,
'swipeEdge': 0,
}
: null,
),
),
(_) {},
);
await tester.pump();
}
void main() {
for (final predictive in [false, true]) {
for (final destination in ['pipelines', 'runs', 'chat']) {
testWidgets(
'$destination: ${predictive ? 'gesture' : 'button'} Back pops dialog, detail, then app',
(tester) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final exits = <String>[];
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
SystemChannels.platform,
(call) async {
if (call.method == 'SystemNavigator.pop') exits.add(call.method);
return null;
},
);
addTearDown(
() => tester.binding.defaultBinaryMessenger
.setMockMethodCallHandler(SystemChannels.platform, null),
);
final channel = FakeChannel()..readyCompleter.complete();
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(
uri: Uri.parse('/$destination'),
),
);
addTearDown(provider.dispose);
await app.pumpApp(
tester,
channel: channel,
client: PipelinesClient(channelFactory: (_) => channel),
provider: provider,
);
final fixture = PipelineFixture();
final String title;
switch (destination) {
case 'pipelines':
title = 'Alpha review';
await pipelines.replyPipelines(tester, channel, fixture);
case 'runs':
title = 'Navigation run';
await app.replyList(
tester,
channel,
runs: [
{'id': 1, 'title': title, 'status': 'succeeded'},
],
);
default:
title = 'Wire chat';
await chat.replySessions(tester, channel);
}
await tester.pumpAndSettle();
await tester.tap(find.text(title).first);
await tester.pump();
switch (destination) {
case 'pipelines':
await pipelines.replyPipelineDetail(tester, channel, fixture);
case 'runs':
await app.replyDetail(tester, channel, 1);
default:
await chat.replySubscribe(tester, channel);
await chat.replyHistory(tester, channel);
}
await tester.pumpAndSettle();
final detailPath = provider.value.uri.path;
expect(detailPath, isNot('/$destination'));
final navigator = tester.state<NavigatorState>(
find.byType(Navigator),
);
expect(navigator.canPop(), isTrue);
if (predictive) {
await backGesture(tester, 'startBackGesture');
expect(navigator.userGestureInProgress, isTrue);
await backGesture(tester, 'cancelBackGesture');
await tester.pumpAndSettle();
expect(provider.value.uri.path, detailPath);
expect(navigator.canPop(), isTrue);
}
// A real route on top of the detail must consume Back first.
showDialog<void>(
context: navigator.overlay!.context,
builder: (_) => const AlertDialog(title: Text('Navigation dialog')),
);
await tester.pumpAndSettle();
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(find.text('Navigation dialog'), findsNothing);
expect(provider.value.uri.path, detailPath);
expect(find.byKey(const ValueKey('shell.back')), findsOneWidget);
if (predictive) {
await backGesture(tester, 'startBackGesture');
await backGesture(tester, 'commitBackGesture');
} else {
await tester.binding.handlePopRoute();
}
await tester.pumpAndSettle();
expect(provider.value.uri.path, '/$destination');
expect(find.text(title), findsWidgets);
expect(navigator.canPop(), isFalse);
expect(exits, isEmpty);
// No confirmation, double-back requirement, or hidden detail route.
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(exits, ['SystemNavigator.pop']);
expect(find.text('Close Pipelines?'), findsNothing);
final router =
Router.of(navigator.context).routerDelegate as AppRouterDelegate;
expect(router.route.path, '/$destination');
},
);
}
}
}
+3 -11
View File
@@ -213,20 +213,12 @@ void main() {
expect(provider.value.uri.path, '/runs');
expect(exits, isEmpty);
// From the list, Back asks before leaving the app.
// From the list, Back is left to Android. No custom exit dialog.
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(find.text('Close Pipelines?'), findsOneWidget);
await tester.tap(find.byKey(const Key('app-exit-stay')));
await tester.pumpAndSettle();
expect(exits, isEmpty);
expect(provider.value.uri.path, '/runs');
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('app-exit-close')));
await tester.pumpAndSettle();
expect(find.text('Close Pipelines?'), findsNothing);
expect(exits, ['SystemNavigator.pop']);
expect(provider.value.uri.path, '/runs');
});
testWidgets('tabs and run selection update the route; browser Back pops it', (
+83 -1
View File
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/shell/destination.dart';
import 'package:pipelines_app/shell/detail_page.dart';
@@ -82,7 +83,12 @@ void main() {
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);
expect(
tester
.state<_OwnerState>(find.byType(_Owner, skipOffstage: false))
.closed,
0,
);
});
testWidgets('the system Back closes the page first', (tester) async {
@@ -95,4 +101,80 @@ void main() {
expect(find.text('the list'), findsOneWidget);
expect(selection.value, isNull);
});
testWidgets('an owner update during Back does not push the detail again', (
tester,
) async {
final selection = ValueNotifier<String?>('5');
await tester.pumpWidget(MaterialApp(home: _Owner(selection: selection)));
await tester.pumpAndSettle();
await tester.binding.handlePopRoute();
await tester.pump(const Duration(milliseconds: 30));
// A live reply rebuilds the owner while the page is sliding away.
selection.value = '6';
await tester.pumpAndSettle();
expect(find.text('the list'), findsOneWidget);
expect(find.text('detail 6'), findsNothing);
expect(selection.value, isNull);
expect(tester.state<_OwnerState>(find.byType(_Owner)).closed, 1);
expect(
tester.state<NavigatorState>(find.byType(Navigator)).canPop(),
isFalse,
);
});
testWidgets('Android predictive Back can cancel, then commit one pop', (
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));
Future<void> gesture(String method) async {
await tester.binding.defaultBinaryMessenger.handlePlatformMessage(
'flutter/backgesture',
const StandardMethodCodec().encodeMethodCall(
MethodCall(
method,
method == 'startBackGesture'
? {
'touchOffset': <double>[5, 300],
'progress': 0.0,
'swipeEdge': 0,
}
: null,
),
),
(_) {},
);
await tester.pump();
}
await gesture('startBackGesture');
expect(navigator.userGestureInProgress, isTrue);
await gesture('cancelBackGesture');
await tester.pumpAndSettle();
expect(selection.value, '5');
expect(find.text('detail 5'), findsOneWidget);
expect(
tester
.state<_OwnerState>(find.byType(_Owner, skipOffstage: false))
.closed,
0,
);
await gesture('startBackGesture');
expect(navigator.userGestureInProgress, isTrue);
await gesture('commitBackGesture');
// Rebuild during the committed gesture's closing animation as well.
selection.value = '6';
await tester.pumpAndSettle();
expect(find.text('the list'), findsOneWidget);
expect(selection.value, isNull);
expect(tester.state<_OwnerState>(find.byType(_Owner)).closed, 1);
expect(navigator.canPop(), isFalse);
});
}
+5 -8
View File
@@ -1,4 +1,5 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/ui/theme/app_theme.dart';
@@ -26,18 +27,14 @@ void main() {
expect(AppColors.groupedBackground.darkColor, isNot(CupertinoColors.black));
});
test('Android pages slide quickly, without a fade', () {
final builder =
appDarkTheme().pageTransitionsTheme.builders[TargetPlatform.android]!;
expect(builder, isA<FastSlidePageTransitionsBuilder>());
expect(builder.transitionDuration.inMilliseconds, lessThanOrEqualTo(200));
test('Android pages retain native predictive Back in both themes', () {
expect(
builder.reverseTransitionDuration.inMilliseconds,
lessThanOrEqualTo(200),
appDarkTheme().pageTransitionsTheme.builders[TargetPlatform.android],
isA<PredictiveBackPageTransitionsBuilder>(),
);
expect(
appLightTheme().pageTransitionsTheme.builders[TargetPlatform.android],
isA<FastSlidePageTransitionsBuilder>(),
isA<PredictiveBackPageTransitionsBuilder>(),
);
});
}