|
| 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_scope/focus.1.dart' |
| 8 | + as example; |
| 9 | +import 'package:flutter_test/flutter_test.dart'; |
| 10 | + |
| 11 | +void main() { |
| 12 | + testWidgets('FocusableText shows content and color depending on focus', |
| 13 | + (WidgetTester tester) async { |
| 14 | + await tester.pumpWidget(const MaterialApp( |
| 15 | + home: Scaffold( |
| 16 | + body: example.FocusableText( |
| 17 | + 'Item 0', |
| 18 | + autofocus: false, |
| 19 | + ), |
| 20 | + ), |
| 21 | + )); |
| 22 | + // Autofocus needs to check that no other node in the [FocusScope] is |
| 23 | + // focused and can only request focus for the second frame. |
| 24 | + await tester.pumpAndSettle(); |
| 25 | + expect(find.descendant( |
| 26 | + of: find.byType(example.FocusableText), |
| 27 | + matching: find.byType(Focus), |
| 28 | + ), findsOneWidget); |
| 29 | + expect(find.text('Item 0'), findsOneWidget); |
| 30 | + |
| 31 | + expect(find.byType(Container), findsOneWidget); |
| 32 | + final Container container1 = tester.widget<Container>( |
| 33 | + find.byType(Container) |
| 34 | + ); |
| 35 | + expect(container1.color, Colors.white); |
| 36 | + |
| 37 | + await tester.pumpWidget(const MaterialApp( |
| 38 | + home: Scaffold( |
| 39 | + body: example.FocusableText( |
| 40 | + 'Item 1', |
| 41 | + autofocus: true, |
| 42 | + ), |
| 43 | + ), |
| 44 | + )); |
| 45 | + await tester.pumpAndSettle(); |
| 46 | + |
| 47 | + final Finder focusableTextFinder2 = find.ancestor( |
| 48 | + of: find.text('Item 1'), |
| 49 | + matching: find.byType(example.FocusableText), |
| 50 | + ); |
| 51 | + expect(tester.widget<Focus>(find.descendant( |
| 52 | + of: focusableTextFinder2, |
| 53 | + matching: find.byType(Focus), |
| 54 | + )).autofocus, isTrue); |
| 55 | + final Container container2 = tester.widget<Container>(find.descendant( |
| 56 | + of: focusableTextFinder2, |
| 57 | + matching: find.byType(Container), |
| 58 | + )); |
| 59 | + expect(container2.color, Colors.red); |
| 60 | + }); |
| 61 | + |
| 62 | + testWidgets('builds list showcasing focus traversal', |
| 63 | + (WidgetTester tester) async { |
| 64 | + await tester.pumpWidget(const example.FocusExampleApp()); |
| 65 | + await tester.pumpAndSettle(); |
| 66 | + |
| 67 | + expect(find.byType(ListView), findsOneWidget); |
| 68 | + |
| 69 | + final Finder childFinder = find.descendant( |
| 70 | + of: find.byType(ListView), |
| 71 | + matching: find.byType(example.FocusableText), |
| 72 | + ); |
| 73 | + expect(childFinder, findsAtLeastNWidgets(2)); |
| 74 | + |
| 75 | + Container container0 = tester.widget<Container>(find.descendant( |
| 76 | + of: childFinder.first, |
| 77 | + matching: find.byType(Container), |
| 78 | + )); |
| 79 | + Container container1 = tester.widget<Container>(find.descendant( |
| 80 | + of: childFinder.at(1), |
| 81 | + matching: find.byType(Container), |
| 82 | + )); |
| 83 | + expect(container0.color, Colors.red); |
| 84 | + expect(container1.color, Colors.white); |
| 85 | + |
| 86 | + await tester.sendKeyDownEvent(LogicalKeyboardKey.arrowDown); |
| 87 | + await tester.pumpAndSettle(); |
| 88 | + |
| 89 | + container0 = tester.widget<Container>(find.descendant( |
| 90 | + of: childFinder.first, |
| 91 | + matching: find.byType(Container), |
| 92 | + )); |
| 93 | + container1 = tester.widget<Container>(find.descendant( |
| 94 | + of: childFinder.at(1), |
| 95 | + matching: find.byType(Container), |
| 96 | + )); |
| 97 | + expect(container0.color, Colors.white); |
| 98 | + expect(container1.color, Colors.red); |
| 99 | + }); |
| 100 | +} |
0 commit comments