|
| 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/material/text_form_field/text_form_field.1.dart' as example; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + testWidgets('Pressing space should focus the next field', (WidgetTester tester) async { |
| 12 | + await tester.pumpWidget(const example.TextFormFieldExampleApp()); |
| 13 | + final Finder textFormField = find.byType(TextFormField); |
| 14 | + |
| 15 | + expect(textFormField, findsExactly(5)); |
| 16 | + |
| 17 | + final Finder editableText = find.byType(EditableText); |
| 18 | + expect(editableText, findsExactly(5)); |
| 19 | + |
| 20 | + List<bool> getFocuses() { |
| 21 | + return editableText.evaluate() |
| 22 | + .map((Element finderResult) => (finderResult.widget as EditableText).focusNode.hasFocus) |
| 23 | + .toList(); |
| 24 | + } |
| 25 | + |
| 26 | + expect(getFocuses(), const <bool>[false, false, false, false, false]); |
| 27 | + |
| 28 | + await tester.tap(textFormField.first); |
| 29 | + await tester.pump(); |
| 30 | + |
| 31 | + expect(getFocuses(), const <bool>[true, false, false, false, false]); |
| 32 | + |
| 33 | + await tester.sendKeyEvent(LogicalKeyboardKey.space); |
| 34 | + await tester.pump(); |
| 35 | + |
| 36 | + expect(getFocuses(), const <bool>[false, true, false, false, false]); |
| 37 | + |
| 38 | + await tester.sendKeyEvent(LogicalKeyboardKey.space); |
| 39 | + await tester.pump(); |
| 40 | + |
| 41 | + expect(getFocuses(), const <bool>[false, false, true, false, false]); |
| 42 | + |
| 43 | + await tester.sendKeyEvent(LogicalKeyboardKey.space); |
| 44 | + await tester.pump(); |
| 45 | + |
| 46 | + expect(getFocuses(), const <bool>[false, false, false, true, false]); |
| 47 | + |
| 48 | + await tester.sendKeyEvent(LogicalKeyboardKey.space); |
| 49 | + await tester.pump(); |
| 50 | + |
| 51 | + expect(getFocuses(), const <bool>[false, false, false, false, true]); |
| 52 | + |
| 53 | + await tester.sendKeyEvent(LogicalKeyboardKey.space); |
| 54 | + await tester.pump(); |
| 55 | + |
| 56 | + expect(getFocuses(), const <bool>[true, false, false, false, false]); |
| 57 | + }); |
| 58 | +} |
0 commit comments