mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Add test for focus_node.0.dart API example. (#146943)
This PR contributes to https://github.com/flutter/flutter/issues/130459 ### Description - Adds `examples/api/test/widgets/focus_manager/focus_node.0_test.dart` test
This commit is contained in:
parent
16cace56a0
commit
f6dc1ac00d
@ -392,7 +392,6 @@ final Set<String> _knownMissingTests = <String>{
|
||||
'examples/api/test/widgets/navigator/restorable_route_future.0_test.dart',
|
||||
'examples/api/test/widgets/navigator/navigator_state.restorable_push.0_test.dart',
|
||||
'examples/api/test/widgets/focus_manager/focus_node.unfocus.0_test.dart',
|
||||
'examples/api/test/widgets/focus_manager/focus_node.0_test.dart',
|
||||
'examples/api/test/widgets/framework/build_owner.0_test.dart',
|
||||
'examples/api/test/widgets/framework/error_widget.0_test.dart',
|
||||
'examples/api/test/widgets/inherited_theme/inherited_theme.0_test.dart',
|
||||
|
191
examples/api/test/widgets/focus_manager/focus_node.0_test.dart
Normal file
191
examples/api/test/widgets/focus_manager/focus_node.0_test.dart
Normal file
@ -0,0 +1,191 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_api_samples/widgets/focus_manager/focus_node.0.dart'
|
||||
as example;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'FocusNode gets focused and unfocused on ColorfulButton tap',
|
||||
(WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.FocusNodeExampleApp(),
|
||||
);
|
||||
|
||||
final Element button = tester.element(
|
||||
find.byType(example.ColorfulButton),
|
||||
);
|
||||
|
||||
expect(
|
||||
tester.binding.focusManager.primaryFocus?.context,
|
||||
isNot(equals(button)),
|
||||
);
|
||||
|
||||
// Tapping on ColorfulButton to focus on FocusNode.
|
||||
await tester.tap(find.byType(example.ColorfulButton));
|
||||
await tester.pump();
|
||||
|
||||
expect(
|
||||
tester.binding.focusManager.primaryFocus?.context,
|
||||
equals(button),
|
||||
);
|
||||
|
||||
// Tapping on ColorfulButton to unfocus from FocusNode.
|
||||
await tester.tap(find.byType(example.ColorfulButton));
|
||||
await tester.pump();
|
||||
|
||||
expect(
|
||||
tester.binding.focusManager.primaryFocus?.context,
|
||||
isNot(equals(button)),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'FocusNode updates the text label when focused or unfocused',
|
||||
(WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.FocusNodeExampleApp(),
|
||||
);
|
||||
|
||||
expect(find.text('Press to focus'), findsOneWidget);
|
||||
expect(find.text("I'm in color! Press R,G,B!"), findsNothing);
|
||||
|
||||
// Tapping on ColorfulButton to focus on FocusNode.
|
||||
await tester.tap(find.byType(example.ColorfulButton));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Press to focus'), findsNothing);
|
||||
expect(find.text("I'm in color! Press R,G,B!"), findsOneWidget);
|
||||
|
||||
// Tapping on ColorfulButton to unfocus from FocusNode.
|
||||
await tester.tap(find.byType(example.ColorfulButton));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text('Press to focus'), findsOneWidget);
|
||||
expect(find.text("I'm in color! Press R,G,B!"), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'FocusNode updates color of the Container according to the key events when focused',
|
||||
(WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.FocusNodeExampleApp(),
|
||||
);
|
||||
|
||||
// Tapping on ColorfulButton to focus on FocusNode.
|
||||
await tester.tap(find.byType(example.ColorfulButton));
|
||||
await tester.pump();
|
||||
|
||||
final Finder containerFinder = find.descendant(
|
||||
of: find.byType(example.ColorfulButton),
|
||||
matching: find.byType(Container),
|
||||
);
|
||||
|
||||
Container container = tester.widget<Container>(containerFinder);
|
||||
expect(container.color, equals(Colors.white));
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyR);
|
||||
await tester.pump();
|
||||
|
||||
container = tester.widget<Container>(containerFinder);
|
||||
expect(container.color, equals(Colors.red));
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyG);
|
||||
await tester.pump();
|
||||
|
||||
container = tester.widget<Container>(containerFinder);
|
||||
expect(container.color, equals(Colors.green));
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyB);
|
||||
await tester.pump();
|
||||
|
||||
container = tester.widget<Container>(containerFinder);
|
||||
expect(container.color, equals(Colors.blue));
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'FocusNode does not listen to the key events when unfocused',
|
||||
(WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.FocusNodeExampleApp(),
|
||||
);
|
||||
|
||||
final Finder containerFinder = find.descendant(
|
||||
of: find.byType(example.ColorfulButton),
|
||||
matching: find.byType(Container),
|
||||
);
|
||||
|
||||
Container container = tester.widget<Container>(containerFinder);
|
||||
expect(container.color, equals(Colors.white));
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyR);
|
||||
await tester.pump();
|
||||
|
||||
container = tester.widget<Container>(containerFinder);
|
||||
expect(container.color, equals(Colors.white));
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyG);
|
||||
await tester.pump();
|
||||
|
||||
container = tester.widget<Container>(containerFinder);
|
||||
expect(container.color, equals(Colors.white));
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyB);
|
||||
await tester.pump();
|
||||
|
||||
container = tester.widget<Container>(containerFinder);
|
||||
expect(container.color, equals(Colors.white));
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'FocusNode sets color to the white when unfocused and sets it back to the selected one when focused',
|
||||
(WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.FocusNodeExampleApp(),
|
||||
);
|
||||
|
||||
final Finder containerFinder = find.descendant(
|
||||
of: find.byType(example.ColorfulButton),
|
||||
matching: find.byType(Container),
|
||||
);
|
||||
|
||||
Container container = tester.widget<Container>(containerFinder);
|
||||
expect(container.color, equals(Colors.white));
|
||||
|
||||
// Tapping on ColorfulButton to focus on FocusNode.
|
||||
await tester.tap(find.byType(example.ColorfulButton));
|
||||
await tester.pump();
|
||||
|
||||
container = tester.widget<Container>(containerFinder);
|
||||
expect(container.color, equals(Colors.white));
|
||||
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyR);
|
||||
await tester.pump();
|
||||
|
||||
container = tester.widget<Container>(containerFinder);
|
||||
expect(container.color, equals(Colors.red));
|
||||
|
||||
// Tapping on ColorfulButton to unfocus from FocusNode.
|
||||
await tester.tap(find.byType(example.ColorfulButton));
|
||||
await tester.pump();
|
||||
|
||||
container = tester.widget<Container>(containerFinder);
|
||||
expect(container.color, equals(Colors.white));
|
||||
|
||||
// Tapping on ColorfulButton to focus on FocusNode.
|
||||
await tester.tap(find.byType(example.ColorfulButton));
|
||||
await tester.pump();
|
||||
|
||||
container = tester.widget<Container>(containerFinder);
|
||||
expect(container.color, equals(Colors.red));
|
||||
},
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue
Block a user