|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/material.dart'; |
| 6 | +import 'package:flutter/services.dart'; |
| 7 | +import 'package:flutter_api_samples/widgets/focus_manager/focus_node.0.dart' |
| 8 | + as example; |
| 9 | +import 'package:flutter_test/flutter_test.dart'; |
| 10 | + |
| 11 | +void main() { |
| 12 | + testWidgets( |
| 13 | + 'FocusNode gets focused and unfocused on ColorfulButton tap', |
| 14 | + (WidgetTester tester) async { |
| 15 | + await tester.pumpWidget( |
| 16 | + const example.FocusNodeExampleApp(), |
| 17 | + ); |
| 18 | + |
| 19 | + final Element button = tester.element( |
| 20 | + find.byType(example.ColorfulButton), |
| 21 | + ); |
| 22 | + |
| 23 | + expect( |
| 24 | + tester.binding.focusManager.primaryFocus?.context, |
| 25 | + isNot(equals(button)), |
| 26 | + ); |
| 27 | + |
| 28 | + // Tapping on ColorfulButton to focus on FocusNode. |
| 29 | + await tester.tap(find.byType(example.ColorfulButton)); |
| 30 | + await tester.pump(); |
| 31 | + |
| 32 | + expect( |
| 33 | + tester.binding.focusManager.primaryFocus?.context, |
| 34 | + equals(button), |
| 35 | + ); |
| 36 | + |
| 37 | + // Tapping on ColorfulButton to unfocus from FocusNode. |
| 38 | + await tester.tap(find.byType(example.ColorfulButton)); |
| 39 | + await tester.pump(); |
| 40 | + |
| 41 | + expect( |
| 42 | + tester.binding.focusManager.primaryFocus?.context, |
| 43 | + isNot(equals(button)), |
| 44 | + ); |
| 45 | + }, |
| 46 | + ); |
| 47 | + |
| 48 | + testWidgets( |
| 49 | + 'FocusNode updates the text label when focused or unfocused', |
| 50 | + (WidgetTester tester) async { |
| 51 | + await tester.pumpWidget( |
| 52 | + const example.FocusNodeExampleApp(), |
| 53 | + ); |
| 54 | + |
| 55 | + expect(find.text('Press to focus'), findsOneWidget); |
| 56 | + expect(find.text("I'm in color! Press R,G,B!"), findsNothing); |
| 57 | + |
| 58 | + // Tapping on ColorfulButton to focus on FocusNode. |
| 59 | + await tester.tap(find.byType(example.ColorfulButton)); |
| 60 | + await tester.pump(); |
| 61 | + |
| 62 | + expect(find.text('Press to focus'), findsNothing); |
| 63 | + expect(find.text("I'm in color! Press R,G,B!"), findsOneWidget); |
| 64 | + |
| 65 | + // Tapping on ColorfulButton to unfocus from FocusNode. |
| 66 | + await tester.tap(find.byType(example.ColorfulButton)); |
| 67 | + await tester.pump(); |
| 68 | + |
| 69 | + expect(find.text('Press to focus'), findsOneWidget); |
| 70 | + expect(find.text("I'm in color! Press R,G,B!"), findsNothing); |
| 71 | + }, |
| 72 | + ); |
| 73 | + |
| 74 | + testWidgets( |
| 75 | + 'FocusNode updates color of the Container according to the key events when focused', |
| 76 | + (WidgetTester tester) async { |
| 77 | + await tester.pumpWidget( |
| 78 | + const example.FocusNodeExampleApp(), |
| 79 | + ); |
| 80 | + |
| 81 | + // Tapping on ColorfulButton to focus on FocusNode. |
| 82 | + await tester.tap(find.byType(example.ColorfulButton)); |
| 83 | + await tester.pump(); |
| 84 | + |
| 85 | + final Finder containerFinder = find.descendant( |
| 86 | + of: find.byType(example.ColorfulButton), |
| 87 | + matching: find.byType(Container), |
| 88 | + ); |
| 89 | + |
| 90 | + Container container = tester.widget<Container>(containerFinder); |
| 91 | + expect(container.color, equals(Colors.white)); |
| 92 | + |
| 93 | + await tester.sendKeyEvent(LogicalKeyboardKey.keyR); |
| 94 | + await tester.pump(); |
| 95 | + |
| 96 | + container = tester.widget<Container>(containerFinder); |
| 97 | + expect(container.color, equals(Colors.red)); |
| 98 | + |
| 99 | + await tester.sendKeyEvent(LogicalKeyboardKey.keyG); |
| 100 | + await tester.pump(); |
| 101 | + |
| 102 | + container = tester.widget<Container>(containerFinder); |
| 103 | + expect(container.color, equals(Colors.green)); |
| 104 | + |
| 105 | + await tester.sendKeyEvent(LogicalKeyboardKey.keyB); |
| 106 | + await tester.pump(); |
| 107 | + |
| 108 | + container = tester.widget<Container>(containerFinder); |
| 109 | + expect(container.color, equals(Colors.blue)); |
| 110 | + }, |
| 111 | + ); |
| 112 | + |
| 113 | + testWidgets( |
| 114 | + 'FocusNode does not listen to the key events when unfocused', |
| 115 | + (WidgetTester tester) async { |
| 116 | + await tester.pumpWidget( |
| 117 | + const example.FocusNodeExampleApp(), |
| 118 | + ); |
| 119 | + |
| 120 | + final Finder containerFinder = find.descendant( |
| 121 | + of: find.byType(example.ColorfulButton), |
| 122 | + matching: find.byType(Container), |
| 123 | + ); |
| 124 | + |
| 125 | + Container container = tester.widget<Container>(containerFinder); |
| 126 | + expect(container.color, equals(Colors.white)); |
| 127 | + |
| 128 | + await tester.sendKeyEvent(LogicalKeyboardKey.keyR); |
| 129 | + await tester.pump(); |
| 130 | + |
| 131 | + container = tester.widget<Container>(containerFinder); |
| 132 | + expect(container.color, equals(Colors.white)); |
| 133 | + |
| 134 | + await tester.sendKeyEvent(LogicalKeyboardKey.keyG); |
| 135 | + await tester.pump(); |
| 136 | + |
| 137 | + container = tester.widget<Container>(containerFinder); |
| 138 | + expect(container.color, equals(Colors.white)); |
| 139 | + |
| 140 | + await tester.sendKeyEvent(LogicalKeyboardKey.keyB); |
| 141 | + await tester.pump(); |
| 142 | + |
| 143 | + container = tester.widget<Container>(containerFinder); |
| 144 | + expect(container.color, equals(Colors.white)); |
| 145 | + }, |
| 146 | + ); |
| 147 | + |
| 148 | + testWidgets( |
| 149 | + 'FocusNode sets color to the white when unfocused and sets it back to the selected one when focused', |
| 150 | + (WidgetTester tester) async { |
| 151 | + await tester.pumpWidget( |
| 152 | + const example.FocusNodeExampleApp(), |
| 153 | + ); |
| 154 | + |
| 155 | + final Finder containerFinder = find.descendant( |
| 156 | + of: find.byType(example.ColorfulButton), |
| 157 | + matching: find.byType(Container), |
| 158 | + ); |
| 159 | + |
| 160 | + Container container = tester.widget<Container>(containerFinder); |
| 161 | + expect(container.color, equals(Colors.white)); |
| 162 | + |
| 163 | + // Tapping on ColorfulButton to focus on FocusNode. |
| 164 | + await tester.tap(find.byType(example.ColorfulButton)); |
| 165 | + await tester.pump(); |
| 166 | + |
| 167 | + container = tester.widget<Container>(containerFinder); |
| 168 | + expect(container.color, equals(Colors.white)); |
| 169 | + |
| 170 | + await tester.sendKeyEvent(LogicalKeyboardKey.keyR); |
| 171 | + await tester.pump(); |
| 172 | + |
| 173 | + container = tester.widget<Container>(containerFinder); |
| 174 | + expect(container.color, equals(Colors.red)); |
| 175 | + |
| 176 | + // Tapping on ColorfulButton to unfocus from FocusNode. |
| 177 | + await tester.tap(find.byType(example.ColorfulButton)); |
| 178 | + await tester.pump(); |
| 179 | + |
| 180 | + container = tester.widget<Container>(containerFinder); |
| 181 | + expect(container.color, equals(Colors.white)); |
| 182 | + |
| 183 | + // Tapping on ColorfulButton to focus on FocusNode. |
| 184 | + await tester.tap(find.byType(example.ColorfulButton)); |
| 185 | + await tester.pump(); |
| 186 | + |
| 187 | + container = tester.widget<Container>(containerFinder); |
| 188 | + expect(container.color, equals(Colors.red)); |
| 189 | + }, |
| 190 | + ); |
| 191 | +} |
0 commit comments