|
| 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/actions/focusable_action_detector.0.dart' as example; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + final Finder redContainerFinder = find.byWidgetPredicate( |
| 12 | + (Widget widget) => widget is Container && widget.color == Colors.red, |
| 13 | + ); |
| 14 | + |
| 15 | + testWidgets('Taps on the "And me" button toggle the red box', (WidgetTester tester) async { |
| 16 | + await tester.pumpWidget( |
| 17 | + const example.FocusableActionDetectorExampleApp(), |
| 18 | + ); |
| 19 | + |
| 20 | + expect(find.widgetWithText(AppBar, 'FocusableActionDetector Example'), findsOne); |
| 21 | + |
| 22 | + expect(redContainerFinder, findsNothing); |
| 23 | + |
| 24 | + await tester.tap(find.text('And Me')); |
| 25 | + await tester.pump(); |
| 26 | + |
| 27 | + expect(redContainerFinder, findsOne); |
| 28 | + |
| 29 | + await tester.tap(find.text('And Me')); |
| 30 | + await tester.pump(); |
| 31 | + |
| 32 | + expect(redContainerFinder, findsNothing); |
| 33 | + }); |
| 34 | + |
| 35 | + testWidgets('Hits on the X key when "And me" is focused toggle the red box', (WidgetTester tester) async { |
| 36 | + await tester.pumpWidget( |
| 37 | + const example.FocusableActionDetectorExampleApp(), |
| 38 | + ); |
| 39 | + |
| 40 | + expect(find.widgetWithText(AppBar, 'FocusableActionDetector Example'), findsOne); |
| 41 | + |
| 42 | + expect(redContainerFinder, findsNothing); |
| 43 | + |
| 44 | + await tester.sendKeyEvent(LogicalKeyboardKey.tab); // Focuses the "Press Me" button. |
| 45 | + await tester.pump(); |
| 46 | + |
| 47 | + expect(redContainerFinder, findsNothing); |
| 48 | + |
| 49 | + await tester.sendKeyEvent(LogicalKeyboardKey.keyX); |
| 50 | + await tester.pump(); |
| 51 | + |
| 52 | + expect(redContainerFinder, findsNothing); |
| 53 | + |
| 54 | + await tester.sendKeyEvent(LogicalKeyboardKey.tab); // Focuses the "And Me" button. |
| 55 | + await tester.pump(); |
| 56 | + |
| 57 | + expect(redContainerFinder, findsNothing); |
| 58 | + |
| 59 | + await tester.sendKeyEvent(LogicalKeyboardKey.keyX); |
| 60 | + await tester.pump(); |
| 61 | + |
| 62 | + expect(redContainerFinder, findsOne); |
| 63 | + |
| 64 | + await tester.sendKeyEvent(LogicalKeyboardKey.keyX); |
| 65 | + await tester.pump(); |
| 66 | + |
| 67 | + expect(redContainerFinder, findsNothing); |
| 68 | + }); |
| 69 | +} |
0 commit comments