|
| 1 | +// Copyright 2013 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:test/bootstrap/browser.dart'; |
| 6 | +import 'package:test/test.dart'; |
| 7 | +import 'package:ui/src/engine.dart'; |
| 8 | +import 'package:ui/ui.dart' as ui; |
| 9 | + |
| 10 | +void main() { |
| 11 | + internalBootstrapBrowserTest(() => testMain); |
| 12 | +} |
| 13 | + |
| 14 | +void testMain() { |
| 15 | + group(ViewFocusBinding, () { |
| 16 | + late EnginePlatformDispatcher platformDispatcher; |
| 17 | + |
| 18 | + setUp(() { |
| 19 | + platformDispatcher = EnginePlatformDispatcher.instance; |
| 20 | + domDocument.activeElement?.blur(); |
| 21 | + }); |
| 22 | + |
| 23 | + test('fires a focus event - a view was focused', () async { |
| 24 | + final List<ui.ViewFocusEvent> viewFocusEvents = <ui.ViewFocusEvent>[]; |
| 25 | + final DomElement div = createDomElement('div'); |
| 26 | + final EngineFlutterView view = EngineFlutterView(platformDispatcher, div); |
| 27 | + final DomElement focusableViewElement = div |
| 28 | + .querySelector(DomManager.flutterViewTagName)! |
| 29 | + ..setAttribute('tabindex', 0); |
| 30 | + |
| 31 | + platformDispatcher.onViewFocusChange = viewFocusEvents.add; |
| 32 | + domDocument.body!.append(div); |
| 33 | + focusableViewElement.focus(); |
| 34 | + |
| 35 | + expect(viewFocusEvents, hasLength(1)); |
| 36 | + |
| 37 | + expect(viewFocusEvents[0].viewId, view.viewId); |
| 38 | + expect(viewFocusEvents[0].state, ui.ViewFocusState.focused); |
| 39 | + expect(viewFocusEvents[0].direction, ui.ViewFocusDirection.forward); |
| 40 | + }); |
| 41 | + |
| 42 | + test('fires a focus event - a view was unfocused', () async { |
| 43 | + final List<ui.ViewFocusEvent> viewFocusEvents = <ui.ViewFocusEvent>[]; |
| 44 | + final DomElement div = createDomElement('div'); |
| 45 | + final EngineFlutterView view = EngineFlutterView(platformDispatcher, div); |
| 46 | + final DomElement focusableViewElement = div |
| 47 | + .querySelector(DomManager.flutterViewTagName)! |
| 48 | + ..setAttribute('tabindex', 0); |
| 49 | + |
| 50 | + platformDispatcher.onViewFocusChange = viewFocusEvents.add; |
| 51 | + domDocument.body!.append(div); |
| 52 | + focusableViewElement.focus(); |
| 53 | + focusableViewElement.blur(); |
| 54 | + |
| 55 | + expect(viewFocusEvents, hasLength(2)); |
| 56 | + |
| 57 | + expect(viewFocusEvents[0].viewId, view.viewId); |
| 58 | + expect(viewFocusEvents[0].state, ui.ViewFocusState.focused); |
| 59 | + expect(viewFocusEvents[0].direction, ui.ViewFocusDirection.forward); |
| 60 | + |
| 61 | + expect(viewFocusEvents[1].viewId, view.viewId); |
| 62 | + expect(viewFocusEvents[1].state, ui.ViewFocusState.unfocused); |
| 63 | + expect(viewFocusEvents[1].direction, ui.ViewFocusDirection.undefined); |
| 64 | + }); |
| 65 | + |
| 66 | + test('fires a focus event - focus transitions between views', () async { |
| 67 | + final List<ui.ViewFocusEvent> viewFocusEvents = <ui.ViewFocusEvent>[]; |
| 68 | + final DomElement div1 = createDomElement('div'); |
| 69 | + final DomElement div2 = createDomElement('div'); |
| 70 | + final EngineFlutterView view1 = |
| 71 | + EngineFlutterView(platformDispatcher, div1); |
| 72 | + final EngineFlutterView view2 = |
| 73 | + EngineFlutterView(platformDispatcher, div2); |
| 74 | + final DomElement focusableViewElement1 = div1 |
| 75 | + .querySelector(DomManager.flutterViewTagName)! |
| 76 | + ..setAttribute('tabindex', 0); |
| 77 | + final DomElement focusableViewElement2 = div2 |
| 78 | + .querySelector(DomManager.flutterViewTagName)! |
| 79 | + ..setAttribute('tabindex', 0); |
| 80 | + |
| 81 | + domDocument.body!.append(div1); |
| 82 | + domDocument.body!.append(div2); |
| 83 | + |
| 84 | + platformDispatcher.onViewFocusChange = viewFocusEvents.add; |
| 85 | + |
| 86 | + focusableViewElement1.focus(); |
| 87 | + focusableViewElement2.focus(); |
| 88 | + |
| 89 | + expect(viewFocusEvents, hasLength(3)); |
| 90 | + |
| 91 | + expect(viewFocusEvents[0].viewId, view1.viewId); |
| 92 | + expect(viewFocusEvents[0].state, ui.ViewFocusState.focused); |
| 93 | + expect(viewFocusEvents[0].direction, ui.ViewFocusDirection.forward); |
| 94 | + |
| 95 | + expect(viewFocusEvents[1].viewId, view1.viewId); |
| 96 | + expect(viewFocusEvents[1].state, ui.ViewFocusState.unfocused); |
| 97 | + expect(viewFocusEvents[1].direction, ui.ViewFocusDirection.undefined); |
| 98 | + |
| 99 | + expect(viewFocusEvents[2].viewId, view2.viewId); |
| 100 | + expect(viewFocusEvents[2].state, ui.ViewFocusState.focused); |
| 101 | + expect(viewFocusEvents[2].direction, ui.ViewFocusDirection.forward); |
| 102 | + }); |
| 103 | + |
| 104 | + test('fires a focus event - focus transitions on and off views', () async { |
| 105 | + final List<ui.ViewFocusEvent> viewFocusEvents = <ui.ViewFocusEvent>[]; |
| 106 | + final DomElement div1 = createDomElement('div'); |
| 107 | + final DomElement div2 = createDomElement('div'); |
| 108 | + final EngineFlutterView view1 = |
| 109 | + EngineFlutterView(platformDispatcher, div1); |
| 110 | + final EngineFlutterView view2 = |
| 111 | + EngineFlutterView(platformDispatcher, div2); |
| 112 | + final DomElement focusableViewElement1 = div1 |
| 113 | + .querySelector(DomManager.flutterViewTagName)! |
| 114 | + ..setAttribute('tabindex', 0); |
| 115 | + final DomElement focusableViewElement2 = div2 |
| 116 | + .querySelector(DomManager.flutterViewTagName)! |
| 117 | + ..setAttribute('tabindex', 0); |
| 118 | + |
| 119 | + domDocument.body!.append(div1); |
| 120 | + domDocument.body!.append(div2); |
| 121 | + |
| 122 | + platformDispatcher.onViewFocusChange = viewFocusEvents.add; |
| 123 | + |
| 124 | + focusableViewElement1.focus(); |
| 125 | + focusableViewElement2.focus(); |
| 126 | + focusableViewElement2.blur(); |
| 127 | + |
| 128 | + expect(viewFocusEvents, hasLength(4)); |
| 129 | + |
| 130 | + expect(viewFocusEvents[0].viewId, view1.viewId); |
| 131 | + expect(viewFocusEvents[0].state, ui.ViewFocusState.focused); |
| 132 | + expect(viewFocusEvents[0].direction, ui.ViewFocusDirection.forward); |
| 133 | + |
| 134 | + expect(viewFocusEvents[1].viewId, view1.viewId); |
| 135 | + expect(viewFocusEvents[1].state, ui.ViewFocusState.unfocused); |
| 136 | + expect(viewFocusEvents[1].direction, ui.ViewFocusDirection.undefined); |
| 137 | + |
| 138 | + expect(viewFocusEvents[2].viewId, view2.viewId); |
| 139 | + expect(viewFocusEvents[2].state, ui.ViewFocusState.focused); |
| 140 | + expect(viewFocusEvents[2].direction, ui.ViewFocusDirection.forward); |
| 141 | + |
| 142 | + expect(viewFocusEvents[3].viewId, view2.viewId); |
| 143 | + expect(viewFocusEvents[3].state, ui.ViewFocusState.unfocused); |
| 144 | + expect(viewFocusEvents[3].direction, ui.ViewFocusDirection.undefined); |
| 145 | + }); |
| 146 | + }); |
| 147 | +} |
0 commit comments