From 8cf829016cd1219dea551624fe13fa9ca1c3c544 Mon Sep 17 00:00:00 2001 From: Thang Nguyen Date: Fri, 13 Nov 2026 15:48:35 +0700 Subject: [PATCH] fix(mobile): app lock covers every route, not just home --- app/lib/mobile/mobile_app.dart | 9 ++- app/lib/mobile/security/app_lock_gate.dart | 18 ++++- app/test/mobile/app_lock_gate_test.dart | 91 +++++++++++++++++++++- 3 files changed, 113 insertions(+), 5 deletions(-) diff --git a/app/lib/mobile/mobile_app.dart b/app/lib/mobile/mobile_app.dart index e7dd3280..440ddccd 100644 --- a/app/lib/mobile/mobile_app.dart +++ b/app/lib/mobile/mobile_app.dart @@ -36,9 +36,14 @@ class _YourSSHMobileAppState extends State { debugShowCheckedModeBanner: false, themeMode: ThemeMode.dark, theme: buildMobileTheme(), - builder: ysRootBuilderTouch, + // The lock wraps the whole navigator, so pushed routes and dialogs are + // covered too. It sits outside ysRootBuilderTouch's FToaster so toasts + // stay under the lock as well. + builder: (c, child) => AppLockGate( + child: ysRootBuilderTouch(c, child), + ), navigatorObservers: [mobileRouteObserver], - home: const AppLockGate(child: MobileHomeShell()), + home: const MobileHomeShell(), ), ); } diff --git a/app/lib/mobile/security/app_lock_gate.dart b/app/lib/mobile/security/app_lock_gate.dart index 37b96420..2039cf90 100644 --- a/app/lib/mobile/security/app_lock_gate.dart +++ b/app/lib/mobile/security/app_lock_gate.dart @@ -115,7 +115,23 @@ class _AppLockGateState extends State with WidgetsBindingObserver { @override Widget build(BuildContext context) { if (!_locked) return widget.child; - // Nothing of the app is built while locked: only this screen. + // The child (navigator, pushed routes, dialogs, toasts) stays mounted so + // sessions and routes survive the unlock, but offstage with tickers off: + // nothing of it is painted, so neither the screen nor the app-switcher + // snapshot can show it. The lock screen is built outside the Navigator, so + // it brings its own FTheme (the gate sits above the app's FTheme/FToaster). + return Stack( + fit: StackFit.expand, + children: [ + Offstage( + child: TickerMode(enabled: false, child: widget.child), + ), + FTheme(data: ysThemeDataTouch(), child: _lockScreen()), + ], + ); + } + + Widget _lockScreen() { return Scaffold( backgroundColor: YsColors.background, body: Center( diff --git a/app/test/mobile/app_lock_gate_test.dart b/app/test/mobile/app_lock_gate_test.dart index 598e8bb7..0291ec7a 100644 --- a/app/test/mobile/app_lock_gate_test.dart +++ b/app/test/mobile/app_lock_gate_test.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:yourssh/mobile/security/app_lock_gate.dart'; +import 'package:yourssh/theme/app_theme.dart'; import 'package:yourssh_ui/yourssh_ui.dart'; import '../helpers/ys_test_app.dart'; @@ -37,7 +38,7 @@ void main() { expect(find.text('Unlock'), findsOneWidget); }); - testWidgets('lock screen: wording, FButton >= 44 px, nothing behind it', + testWidgets('lock screen: wording, FButton >= 44 px, child offstage', (tester) async { await _pump(tester, enabled: true, auth: () async => false); expect(find.text('YourSSH is locked'), findsOneWidget); @@ -45,8 +46,9 @@ void main() { final size = tester.getSize(find.byType(FButton)); expect(size.height, greaterThanOrEqualTo(44)); expect(size.width, greaterThanOrEqualTo(44)); + // The child stays mounted (sessions/routes survive) but offstage. expect(find.byKey(_childKey), findsNothing); - expect(find.byKey(_childKey, skipOffstage: false), findsNothing); + expect(find.byKey(_childKey, skipOffstage: false), findsOneWidget); }); testWidgets('Unlock retries auth and unlocks on success', (tester) async { @@ -58,4 +60,89 @@ void main() { expect(calls, 2); expect(find.byKey(_childKey), findsOneWidget); }); + + group('gate in MaterialApp.builder covers every route', () { + final nav = GlobalKey(); + var calls = 0; + var allow = false; + + Future pumpApp(WidgetTester t) async { + calls = 0; + await t.pumpWidget(MaterialApp( + navigatorKey: nav, + theme: ysMaterialTheme(), + // Mirrors mobile_app.dart: gate outside the toaster so toasts stay + // under the lock too. + builder: (c, child) => AppLockGate( + enabledOverride: true, + authenticator: () async { + calls++; + return allow; + }, + child: ysRootBuilderTouch(c, child), + ), + home: const Text('home'), + )); + await t.pumpAndSettle(); + } + + testWidgets('pushed route is covered while locked and survives unlock', + (t) async { + allow = false; + await pumpApp(t); + nav.currentState! + .push(MaterialPageRoute(builder: (_) => const Text('terminal'))); + await t.pumpAndSettle(); + expect(find.text('YourSSH is locked'), findsOneWidget); + expect(find.text('terminal'), findsNothing); + expect(find.text('terminal', skipOffstage: false), findsOneWidget); + allow = true; + await t.tap(find.text('Unlock')); + await t.pumpAndSettle(); + expect(find.text('terminal'), findsOneWidget); + }); + + testWidgets('backgrounding re-locks over the current route, no prompt loop', + (t) async { + allow = true; + await pumpApp(t); + nav.currentState! + .push(MaterialPageRoute(builder: (_) => const Text('terminal'))); + await t.pumpAndSettle(); + expect(find.text('terminal'), findsOneWidget); + expect(calls, 1); + t.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive); + await t.pump(); + expect(find.text('terminal'), findsNothing); + expect(find.text('YourSSH is locked'), findsOneWidget); + t.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed); + await t.pumpAndSettle(); + expect(find.text('terminal'), findsOneWidget); + expect(calls, 2); + }); + + testWidgets('a dialog on the root navigator is covered while locked', + (t) async { + allow = false; + await pumpApp(t); + showDialog( + context: nav.currentContext!, + builder: (_) => const AlertDialog(content: Text('secret dialog'))); + await t.pumpAndSettle(); + expect(find.text('YourSSH is locked'), findsOneWidget); + expect(find.text('secret dialog'), findsNothing); + expect(find.text('secret dialog', skipOffstage: false), findsOneWidget); + }); + + testWidgets('a toast shown while locked does not paint above the lock', + (t) async { + allow = false; + await pumpApp(t); + showFToast( + context: nav.currentContext!, title: const Text('secret toast')); + await t.pumpAndSettle(); + expect(find.text('YourSSH is locked'), findsOneWidget); + expect(find.text('secret toast'), findsNothing); + }); + }); }