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)
This commit is contained in:
@@ -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<Focus>(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<void>(
|
||||
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<void>(
|
||||
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<Focus>(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<void>(
|
||||
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<void>(
|
||||
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<void>(
|
||||
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<void>(
|
||||
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 {
|
||||
|
||||
@@ -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<Focus>(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<void>(
|
||||
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<void>(
|
||||
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 {
|
||||
|
||||
@@ -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 <Clip?>[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<Focus>(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<void>(
|
||||
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<void>(
|
||||
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 {
|
||||
|
||||
@@ -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<void>(
|
||||
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<void>(
|
||||
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<void> {
|
||||
|
||||
@@ -1873,6 +1873,51 @@ void main() {
|
||||
await tester.pump();
|
||||
expect(tester.takeException(), null);
|
||||
});
|
||||
|
||||
testWidgets('requestFocus can be updated', (WidgetTester tester) async {
|
||||
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
navigatorKey: navigatorKey,
|
||||
home: const Text('home'),
|
||||
));
|
||||
expect(find.text('page2'), findsNothing);
|
||||
|
||||
// Navigate to page 2.
|
||||
navigatorKey.currentState!.push<void>(MaterialPageRoute<void>(
|
||||
builder: (BuildContext context) {
|
||||
return const Text('page2');
|
||||
},
|
||||
));
|
||||
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('page2'), findsOneWidget);
|
||||
|
||||
// Check that the modal route is requesting focus.
|
||||
ModalRoute<void>? modalRoute = ModalRoute.of<void>(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<void>(MaterialPageRoute<void>(
|
||||
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<void>(tester.element(find.text('page2')));
|
||||
expect(modalRoute, isNotNull);
|
||||
expect(modalRoute!.requestFocus, isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('can be dismissed with escape keyboard shortcut', (WidgetTester tester) async {
|
||||
|
||||
Reference in New Issue
Block a user