|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:inline_tab_view/inline_tab_view.dart'; |
| 4 | +import 'package:inline_tab_view/src/offscreen_focus_exclusion_builder.dart'; |
| 5 | + |
| 6 | +void main() { |
| 7 | + testWidgets('hidden children are not focusable', (tester) async { |
| 8 | + final controller = TabController(length: 2, vsync: const TestVSync()); |
| 9 | + addTearDown(controller.dispose); |
| 10 | + |
| 11 | + final leadingFocus = FocusNode(); |
| 12 | + addTearDown(leadingFocus.dispose); |
| 13 | + final trailingFocus = FocusNode(); |
| 14 | + addTearDown(trailingFocus.dispose); |
| 15 | + final tab1Wid1Focus = FocusNode(); |
| 16 | + addTearDown(tab1Wid1Focus.dispose); |
| 17 | + final tab1Wid2Focus = FocusNode(); |
| 18 | + addTearDown(tab1Wid2Focus.dispose); |
| 19 | + final tab1Wid3Focus = FocusNode(); |
| 20 | + addTearDown(tab1Wid3Focus.dispose); |
| 21 | + final tab2Wid1Focus = FocusNode(); |
| 22 | + addTearDown(tab2Wid1Focus.dispose); |
| 23 | + final tab2Wid2Focus = FocusNode(); |
| 24 | + addTearDown(tab2Wid2Focus.dispose); |
| 25 | + final tab2Wid3Focus = FocusNode(); |
| 26 | + addTearDown(tab2Wid3Focus.dispose); |
| 27 | + |
| 28 | + await tester.pumpWidget(MaterialApp( |
| 29 | + home: Column( |
| 30 | + children: [ |
| 31 | + Focus( |
| 32 | + key: Key('leading'), |
| 33 | + focusNode: leadingFocus, |
| 34 | + child: SizedBox.square(dimension: 10)), |
| 35 | + InlineTabView( |
| 36 | + controller: controller, |
| 37 | + children: [ |
| 38 | + Column( |
| 39 | + children: [ |
| 40 | + Focus( |
| 41 | + key: Key('Tab 1 - 1'), |
| 42 | + focusNode: tab1Wid1Focus, |
| 43 | + child: SizedBox.square(dimension: 10)), |
| 44 | + Focus( |
| 45 | + key: Key('Tab 1 - 2'), |
| 46 | + focusNode: tab1Wid2Focus, |
| 47 | + child: SizedBox.square(dimension: 10)), |
| 48 | + Focus( |
| 49 | + key: Key('Tab 1 - 3'), |
| 50 | + focusNode: tab1Wid3Focus, |
| 51 | + child: SizedBox.square(dimension: 10)), |
| 52 | + ], |
| 53 | + ), |
| 54 | + Column( |
| 55 | + children: [ |
| 56 | + Focus( |
| 57 | + key: Key('Tab 2 - 1'), |
| 58 | + focusNode: tab2Wid1Focus, |
| 59 | + child: SizedBox.square(dimension: 10)), |
| 60 | + Focus( |
| 61 | + key: Key('Tab 2 - 2'), |
| 62 | + focusNode: tab2Wid2Focus, |
| 63 | + child: SizedBox.square(dimension: 10)), |
| 64 | + Focus( |
| 65 | + key: Key('Tab 2 - 3'), |
| 66 | + focusNode: tab2Wid3Focus, |
| 67 | + child: SizedBox.square(dimension: 10)), |
| 68 | + ], |
| 69 | + ) |
| 70 | + ], |
| 71 | + ), |
| 72 | + Focus( |
| 73 | + key: Key('trailing'), |
| 74 | + focusNode: trailingFocus, |
| 75 | + child: SizedBox.square(dimension: 10)), |
| 76 | + ], |
| 77 | + ), |
| 78 | + )); |
| 79 | + expect(find.byType(OffscreenFocusExclusionBuilder), findsOneWidget); |
| 80 | + |
| 81 | + tab1Wid1Focus.requestFocus(); |
| 82 | + await tester.pumpAndSettle(); |
| 83 | + expect(leadingFocus.hasFocus, false); |
| 84 | + expect(tab1Wid1Focus.hasFocus, true); |
| 85 | + expect(tab1Wid2Focus.hasFocus, false); |
| 86 | + expect(tab1Wid3Focus.hasFocus, false); |
| 87 | + expect(tab2Wid1Focus.hasFocus, false); |
| 88 | + expect(tab2Wid2Focus.hasFocus, false); |
| 89 | + expect(tab2Wid3Focus.hasFocus, false); |
| 90 | + expect(trailingFocus.hasFocus, false); |
| 91 | + |
| 92 | + // it doesn't (shouldn't) matter which node is used to request the next focus. |
| 93 | + leadingFocus.nextFocus(); |
| 94 | + await tester.pumpAndSettle(); |
| 95 | + expect(leadingFocus.hasFocus, false); |
| 96 | + expect(tab1Wid1Focus.hasFocus, false); |
| 97 | + expect(tab1Wid2Focus.hasFocus, true); |
| 98 | + expect(tab1Wid3Focus.hasFocus, false); |
| 99 | + expect(tab2Wid1Focus.hasFocus, false); |
| 100 | + expect(tab2Wid2Focus.hasFocus, false); |
| 101 | + expect(tab2Wid3Focus.hasFocus, false); |
| 102 | + expect(trailingFocus.hasFocus, false); |
| 103 | + |
| 104 | + leadingFocus.nextFocus(); |
| 105 | + await tester.pumpAndSettle(); |
| 106 | + expect(leadingFocus.hasFocus, false); |
| 107 | + expect(tab1Wid1Focus.hasFocus, false); |
| 108 | + expect(tab1Wid2Focus.hasFocus, false); |
| 109 | + expect(tab1Wid3Focus.hasFocus, true); |
| 110 | + expect(tab2Wid1Focus.hasFocus, false); |
| 111 | + expect(tab2Wid2Focus.hasFocus, false); |
| 112 | + expect(tab2Wid3Focus.hasFocus, false); |
| 113 | + expect(trailingFocus.hasFocus, false); |
| 114 | + |
| 115 | + leadingFocus.nextFocus(); |
| 116 | + await tester.pumpAndSettle(); |
| 117 | + expect(leadingFocus.hasFocus, false); |
| 118 | + expect(tab1Wid1Focus.hasFocus, false); |
| 119 | + expect(tab1Wid2Focus.hasFocus, false); |
| 120 | + expect(tab1Wid3Focus.hasFocus, false); |
| 121 | + expect(tab2Wid1Focus.hasFocus, false); |
| 122 | + expect(tab2Wid2Focus.hasFocus, false); |
| 123 | + expect(tab2Wid3Focus.hasFocus, false); |
| 124 | + expect(trailingFocus.hasFocus, true); |
| 125 | + |
| 126 | + leadingFocus.previousFocus(); |
| 127 | + leadingFocus.previousFocus(); |
| 128 | + leadingFocus.previousFocus(); |
| 129 | + leadingFocus.previousFocus(); |
| 130 | + await tester.pumpAndSettle(); |
| 131 | + expect(leadingFocus.hasFocus, true); |
| 132 | + expect(tab1Wid1Focus.hasFocus, false); |
| 133 | + expect(tab1Wid2Focus.hasFocus, false); |
| 134 | + expect(tab1Wid3Focus.hasFocus, false); |
| 135 | + expect(tab2Wid1Focus.hasFocus, false); |
| 136 | + expect(tab2Wid2Focus.hasFocus, false); |
| 137 | + expect(tab2Wid3Focus.hasFocus, false); |
| 138 | + expect(trailingFocus.hasFocus, false); |
| 139 | + }); |
| 140 | +} |
0 commit comments