From d147e52d6ae09c73947f1e98e5e522e02a865297 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Wed, 28 Aug 2024 21:40:05 +0300 Subject: [PATCH] Add missing tests for `requestFocus` routes parameter in Material and Cupertino libraries (#154005) Fixes [Add missing tests for `requestFocus` routes parameter](https://github.com/flutter/flutter/issues/153999) --- .../flutter/test/cupertino/route_test.dart | 196 ++++++++++++++++++ .../test/material/bottom_sheet_test.dart | 71 +++++++ .../flutter/test/material/dialog_test.dart | 73 +++++++ packages/flutter/test/material/page_test.dart | 58 ++++++ .../flutter/test/widgets/routes_test.dart | 45 ++++ 5 files changed, 443 insertions(+) diff --git a/packages/flutter/test/cupertino/route_test.dart b/packages/flutter/test/cupertino/route_test.dart index 07c9e13f7e..182bf0b70d 100644 --- a/packages/flutter/test/cupertino/route_test.dart +++ b/packages/flutter/test/cupertino/route_test.dart @@ -2690,6 +2690,202 @@ void main() { await tester.tap(find.text('tap')); await tester.pumpAndSettle(); }); + + testWidgets('Setting CupertinoDialogRoute.requestFocus to false does not request focus on the dialog', (WidgetTester tester) async { + late BuildContext savedContext; + final FocusNode focusNode = FocusNode(); + addTearDown(focusNode.dispose); + const String dialogText = 'Dialog Text'; + await tester.pumpWidget( + CupertinoApp( + home: Builder( + builder: (BuildContext context) { + savedContext = context; + return CupertinoTextField(focusNode: focusNode); + }, + ), + ), + ); + await tester.pump(); + + FocusNode? getCupertinoTextFieldFocusNode() { + return tester.widget(find.descendant( + of: find.byType(CupertinoTextField), + matching: find.byType(Focus), + )).focusNode; + } + + // Initially, there is no dialog and the text field has no focus. + expect(find.text(dialogText), findsNothing); + expect(getCupertinoTextFieldFocusNode()?.hasFocus, false); + + // Request focus on the text field. + focusNode.requestFocus(); + await tester.pump(); + expect(getCupertinoTextFieldFocusNode()?.hasFocus, true); + + // Bring up dialog. + final NavigatorState navigator = Navigator.of(savedContext); + navigator.push( + CupertinoDialogRoute( + context: savedContext, + builder: (BuildContext context) => const Text(dialogText), + ), + ); + await tester.pump(); + + // The dialog is showing and the text field has lost focus. + expect(find.text(dialogText), findsOneWidget); + expect(getCupertinoTextFieldFocusNode()?.hasFocus, false); + + // Dismiss the dialog. + navigator.pop(); + await tester.pump(); + + // The dialog is dismissed and the focus is shifted back to the text field. + expect(find.text(dialogText), findsNothing); + expect(getCupertinoTextFieldFocusNode()?.hasFocus, true); + + // Bring up dialog again with requestFocus to false. + navigator.push( + CupertinoDialogRoute( + context: savedContext, + requestFocus: false, + builder: (BuildContext context) => const Text(dialogText), + ), + ); + await tester.pump(); + + // The dialog is showing and the text field still has focus. + expect(find.text(dialogText), findsOneWidget); + expect(getCupertinoTextFieldFocusNode()?.hasFocus, true); + }); + + testWidgets('Setting CupertinoModalPopupRoute.requestFocus to false does not request focus on the popup', (WidgetTester tester) async { + late BuildContext savedContext; + final FocusNode focusNode = FocusNode(); + addTearDown(focusNode.dispose); + const String dialogText = 'Popup Text'; + await tester.pumpWidget( + CupertinoApp( + home: Builder( + builder: (BuildContext context) { + savedContext = context; + return CupertinoTextField(focusNode: focusNode); + }, + ), + ), + ); + await tester.pump(); + + FocusNode? getCupertinoTextFieldFocusNode() { + return tester.widget(find.descendant( + of: find.byType(CupertinoTextField), + matching: find.byType(Focus), + )).focusNode; + } + + // Initially, there is no popup and the text field has no focus. + expect(find.text(dialogText), findsNothing); + expect(getCupertinoTextFieldFocusNode()?.hasFocus, false); + + // Request focus on the text field. + focusNode.requestFocus(); + await tester.pump(); + expect(getCupertinoTextFieldFocusNode()?.hasFocus, true); + + // Bring up popup. + final NavigatorState navigator = Navigator.of(savedContext); + navigator.push( + CupertinoModalPopupRoute( + builder: (BuildContext context) => const Text(dialogText), + ), + ); + await tester.pump(); + + // The popup is showing and the text field has lost focus. + expect(find.text(dialogText), findsOneWidget); + expect(getCupertinoTextFieldFocusNode()?.hasFocus, false); + + // Dismiss the popup. + navigator.pop(); + await tester.pump(); + + // The popup is dismissed and the focus is shifted back to the text field. + expect(find.text(dialogText), findsNothing); + expect(getCupertinoTextFieldFocusNode()?.hasFocus, true); + + // Bring up popup again with requestFocus to false. + navigator.push( + CupertinoModalPopupRoute( + requestFocus: false, + builder: (BuildContext context) => const Text(dialogText), + ), + ); + await tester.pump(); + + // The popup is showing and the text field still has focus. + expect(find.text(dialogText), findsOneWidget); + expect(getCupertinoTextFieldFocusNode()?.hasFocus, true); + }); + + testWidgets('Setting CupertinoPageRoute.requestFocus to false does not request focus on the page', (WidgetTester tester) async { + late BuildContext savedContext; + const String pageTwoText = 'Page Two'; + await tester.pumpWidget( + CupertinoApp( + home: Builder( + builder: (BuildContext context) { + savedContext = context; + return Container(); + }, + ), + ), + ); + await tester.pump(); + + // Check page two is not on the screen. + expect(find.text(pageTwoText), findsNothing); + + // Navigate to page two with text. + final NavigatorState navigator = Navigator.of(savedContext); + navigator.push( + CupertinoPageRoute( + builder: (BuildContext context) { + return const Text(pageTwoText); + } + ), + ); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 100)); // Advance route transition animation. + + // The page two is showing and the text widget has focus. + Element textOnPageTwo = tester.element(find.text(pageTwoText)); + FocusScopeNode focusScopeNode = FocusScope.of(textOnPageTwo); + expect(focusScopeNode.hasFocus, isTrue); + + // Navigate back to page one. + navigator.pop(); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 100)); // Advance route transition animation. + + // Navigate to page two again with requestFocus set to false. + navigator.push( + CupertinoPageRoute( + requestFocus: false, + builder: (BuildContext context) { + return const Text(pageTwoText); + } + ), + ); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 100)); // Advance route transition animation. + + // The page two is showing and the text widget is not focused. + textOnPageTwo = tester.element(find.text(pageTwoText)); + focusScopeNode = FocusScope.of(textOnPageTwo); + expect(focusScopeNode.hasFocus, isFalse); + }); } class MockNavigatorObserver extends NavigatorObserver { diff --git a/packages/flutter/test/material/bottom_sheet_test.dart b/packages/flutter/test/material/bottom_sheet_test.dart index 08384206d3..9f6c2deedb 100644 --- a/packages/flutter/test/material/bottom_sheet_test.dart +++ b/packages/flutter/test/material/bottom_sheet_test.dart @@ -2609,6 +2609,77 @@ void main() { // The bottom sheet is dismissed. expect(find.byKey(sheetKey), findsNothing); }); + + testWidgets('Setting ModalBottomSheetRoute.requestFocus to false does not request focus on the bottom sheet', (WidgetTester tester) async { + late BuildContext savedContext; + final FocusNode focusNode = FocusNode(); + addTearDown(focusNode.dispose); + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Builder( + builder: (BuildContext context) { + savedContext = context; + return TextField(focusNode: focusNode); + }, + ), + ), + ), + ); + await tester.pump(); + + FocusNode? getTextFieldFocusNode() { + return tester.widget(find.descendant( + of: find.byType(TextField), + matching: find.byType(Focus), + )).focusNode; + } + + // Initially, there is no bottom sheet and the text field has no focus. + expect(find.byType(BottomSheet), findsNothing); + expect(getTextFieldFocusNode()?.hasFocus, false); + + // Request focus on the text field. + focusNode.requestFocus(); + await tester.pump(); + expect(getTextFieldFocusNode()?.hasFocus, true); + + // Bring up bottom sheet. + final NavigatorState navigator = Navigator.of(savedContext); + navigator.push( + ModalBottomSheetRoute( + isScrollControlled: false, + builder: (BuildContext context) => Container(), + ), + ); + await tester.pump(); + + // The bottom sheet is showing and the text field has lost focus. + expect(find.byType(BottomSheet), findsOneWidget); + expect(getTextFieldFocusNode()?.hasFocus, false); + + // Dismiss the bottom sheet. + navigator.pop(); + await tester.pump(); + + // The bottom sheet is dismissed and the focus is shifted back to the text field. + expect(find.byType(BottomSheet), findsNothing); + expect(getTextFieldFocusNode()?.hasFocus, true); + + // Bring up bottom sheet again with requestFocus to false. + navigator.push( + ModalBottomSheetRoute( + requestFocus: false, + isScrollControlled: false, + builder: (BuildContext context) => Container(), + ), + ); + await tester.pump(); + + // The bottom sheet is showing and the text field still has focus. + expect(find.byType(BottomSheet), findsOneWidget); + expect(getTextFieldFocusNode()?.hasFocus, true); + }); } class _TestPage extends StatelessWidget { diff --git a/packages/flutter/test/material/dialog_test.dart b/packages/flutter/test/material/dialog_test.dart index 11f94251a5..9c0db0b2b8 100644 --- a/packages/flutter/test/material/dialog_test.dart +++ b/packages/flutter/test/material/dialog_test.dart @@ -2895,11 +2895,84 @@ void main() { expect(simpleDialog.insetPadding, isNull); }); + // This is a regression test for https://github.com/flutter/flutter/issues/153983. testWidgets('Can pass a null value to AlertDialog.adaptive clip behavior', (WidgetTester tester) async { for (final Clip? clipBehavior in [null, ...Clip.values]) { AlertDialog.adaptive(clipBehavior: clipBehavior); } }); + + testWidgets('Setting DialogRoute.requestFocus to false does not request focus on the dialog', (WidgetTester tester) async { + late BuildContext savedContext; + final FocusNode focusNode = FocusNode(); + addTearDown(focusNode.dispose); + const String dialogText = 'Dialog Text'; + await tester.pumpWidget( + MaterialApp( + home: Material( + child: Builder( + builder: (BuildContext context) { + savedContext = context; + return TextField(focusNode: focusNode); + }, + ), + ), + ), + ); + await tester.pump(); + + FocusNode? getTextFieldFocusNode() { + return tester.widget(find.descendant( + of: find.byType(TextField), + matching: find.byType(Focus), + )).focusNode; + } + + // Initially, there is no dialog and the text field has no focus. + expect(find.text(dialogText), findsNothing); + expect(getTextFieldFocusNode()?.hasFocus, false); + + // Request focus on the text field. + focusNode.requestFocus(); + await tester.pump(); + expect(getTextFieldFocusNode()?.hasFocus, true); + + // Bring up dialog. + final NavigatorState navigator = Navigator.of(savedContext); + navigator.push( + DialogRoute( + context: savedContext, + builder: (BuildContext context) => const Text(dialogText), + ), + ); + await tester.pump(); + + // The dialog is showing and the text field has lost focus. + expect(find.text(dialogText), findsOneWidget); + expect(getTextFieldFocusNode()?.hasFocus, false); + + // Dismiss the dialog. + navigator.pop(); + await tester.pump(); + + // The dialog is dismissed and the focus is shifted back to the text field. + expect(find.text(dialogText), findsNothing); + expect(getTextFieldFocusNode()?.hasFocus, true); + + // Bring up dialog again with requestFocus to false. + navigator.push( + ModalBottomSheetRoute( + requestFocus: false, + isScrollControlled: false, + builder: (BuildContext context) => const Text(dialogText), + ), + ); + await tester.pump(); + + // The dialog is showing and the text field still has focus. + expect(find.text(dialogText), findsOneWidget); + expect(getTextFieldFocusNode()?.hasFocus, true); + }); } class _RestorableDialogTestWidget extends StatelessWidget { diff --git a/packages/flutter/test/material/page_test.dart b/packages/flutter/test/material/page_test.dart index e347392016..7ae339e8e2 100644 --- a/packages/flutter/test/material/page_test.dart +++ b/packages/flutter/test/material/page_test.dart @@ -1271,6 +1271,64 @@ void main() { expect(find.text('route'), findsNothing); }); + + testWidgets('Setting MaterialPageRoute.requestFocus to false does not request focus on the page', (WidgetTester tester) async { + late BuildContext savedContext; + const String pageTwoText = 'Page Two'; + await tester.pumpWidget( + MaterialApp( + home: Builder( + builder: (BuildContext context) { + savedContext = context; + return Container(); + }, + ), + ), + ); + await tester.pump(); + + // Check page two is not on the screen. + expect(find.text(pageTwoText), findsNothing); + + // Navigate to page two with text. + final NavigatorState navigator = Navigator.of(savedContext); + navigator.push( + MaterialPageRoute( + builder: (BuildContext context) { + return const Text(pageTwoText); + } + ), + ); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 100)); + + // The page two is showing and the text widget has focus. + Element textOnPageTwo = tester.element(find.text(pageTwoText)); + FocusScopeNode focusScopeNode = FocusScope.of(textOnPageTwo); + expect(focusScopeNode.hasFocus, isTrue); + + // Navigate back to page one. + navigator.pop(); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 100)); + + // Navigate to page two again with requestFocus set to false. + navigator.push( + MaterialPageRoute( + requestFocus: false, + builder: (BuildContext context) { + return const Text(pageTwoText); + } + ), + ); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 100)); + + // The page two is showing and the text widget is not focused. + textOnPageTwo = tester.element(find.text(pageTwoText)); + focusScopeNode = FocusScope.of(textOnPageTwo); + expect(focusScopeNode.hasFocus, isFalse); + }); } class TransitionDetector extends DefaultTransitionDelegate { diff --git a/packages/flutter/test/widgets/routes_test.dart b/packages/flutter/test/widgets/routes_test.dart index 000ecbfb62..a058ed1bd3 100644 --- a/packages/flutter/test/widgets/routes_test.dart +++ b/packages/flutter/test/widgets/routes_test.dart @@ -1873,6 +1873,51 @@ void main() { await tester.pump(); expect(tester.takeException(), null); }); + + testWidgets('requestFocus can be updated', (WidgetTester tester) async { + final GlobalKey navigatorKey = GlobalKey(); + await tester.pumpWidget(MaterialApp( + navigatorKey: navigatorKey, + home: const Text('home'), + )); + expect(find.text('page2'), findsNothing); + + // Navigate to page 2. + navigatorKey.currentState!.push(MaterialPageRoute( + builder: (BuildContext context) { + return const Text('page2'); + }, + )); + + await tester.pumpAndSettle(); + expect(find.text('page2'), findsOneWidget); + + // Check that the modal route is requesting focus. + ModalRoute? modalRoute = ModalRoute.of(tester.element(find.text('page2'))); + expect(modalRoute, isNotNull); + expect(modalRoute!.requestFocus, isTrue); + + // Navigate back to the home page. + navigatorKey.currentState!.pop(); + await tester.pumpAndSettle(); + expect(find.text('page2'), findsNothing); + + // Navigate to page 2 again with requestFocus set to false. + navigatorKey.currentState!.push(MaterialPageRoute( + requestFocus: false, + builder: (BuildContext context) { + return const Text('page2'); + }, + )); + + await tester.pumpAndSettle(); + expect(find.text('page2'), findsOneWidget); + + // Check that the modal route is not requesting focus. + modalRoute = ModalRoute.of(tester.element(find.text('page2'))); + expect(modalRoute, isNotNull); + expect(modalRoute!.requestFocus, isFalse); + }); }); testWidgets('can be dismissed with escape keyboard shortcut', (WidgetTester tester) async {