|
4 | 4 |
|
5 | 5 | // ignore_for_file: avoid_print
|
6 | 6 |
|
| 7 | +import 'dart:html' as html; |
| 8 | + |
| 9 | +// Imports the Flutter Driver API. |
| 10 | +import 'package:flutter/src/widgets/framework.dart'; |
7 | 11 | import 'package:flutter_test/flutter_test.dart';
|
8 | 12 | import 'package:integration_test/integration_test.dart';
|
9 | 13 |
|
| 14 | +import 'package:pointer_interceptor_example/main.dart' as app; |
| 15 | + |
| 16 | +final Finder nonClickableButtonFinder = |
| 17 | + find.byKey(const Key('transparent-button')); |
| 18 | +final Finder clickableWrappedButtonFinder = |
| 19 | + find.byKey(const Key('wrapped-transparent-button')); |
| 20 | +final Finder clickableButtonFinder = find.byKey(const Key('clickable-button')); |
| 21 | +final Finder backgroundFinder = |
| 22 | + find.byKey(const ValueKey<String>('background-widget')); |
| 23 | + |
10 | 24 | void main() {
|
11 | 25 | IntegrationTestWidgetsFlutterBinding.ensureInitialized();
|
12 |
| - // TODO(louisehsu): given the difficulty of making the same integration tests |
13 |
| - // work for both web and ios implementations, please find tests in their respective |
14 |
| - // platform implementation packages. |
15 |
| - testWidgets('placeholder test', (WidgetTester tester) async {}); |
| 26 | + |
| 27 | + group('Without semantics', () { |
| 28 | + testWidgets( |
| 29 | + 'on wrapped elements, the browser does not hit the background-html-view', |
| 30 | + (WidgetTester tester) async { |
| 31 | + app.main(); |
| 32 | + await tester.pumpAndSettle(); |
| 33 | + |
| 34 | + final html.Element element = |
| 35 | + _getHtmlElementAtCenter(clickableButtonFinder, tester); |
| 36 | + |
| 37 | + expect(element.id, isNot('background-html-view')); |
| 38 | + }, semanticsEnabled: false); |
| 39 | + |
| 40 | + testWidgets( |
| 41 | + 'on wrapped elements with intercepting set to false, the browser hits the background-html-view', |
| 42 | + (WidgetTester tester) async { |
| 43 | + app.main(); |
| 44 | + await tester.pumpAndSettle(); |
| 45 | + |
| 46 | + final html.Element element = |
| 47 | + _getHtmlElementAtCenter(clickableWrappedButtonFinder, tester); |
| 48 | + |
| 49 | + expect(element.id, 'background-html-view'); |
| 50 | + }, semanticsEnabled: false); |
| 51 | + |
| 52 | + testWidgets( |
| 53 | + 'on unwrapped elements, the browser hits the background-html-view', |
| 54 | + (WidgetTester tester) async { |
| 55 | + app.main(); |
| 56 | + await tester.pumpAndSettle(); |
| 57 | + |
| 58 | + final html.Element element = |
| 59 | + _getHtmlElementAtCenter(nonClickableButtonFinder, tester); |
| 60 | + |
| 61 | + expect(element.id, 'background-html-view'); |
| 62 | + }, semanticsEnabled: false); |
| 63 | + |
| 64 | + testWidgets('on background directly', (WidgetTester tester) async { |
| 65 | + app.main(); |
| 66 | + await tester.pumpAndSettle(); |
| 67 | + |
| 68 | + final html.Element element = |
| 69 | + _getHtmlElementAt(tester.getTopLeft(backgroundFinder)); |
| 70 | + |
| 71 | + expect(element.id, 'background-html-view'); |
| 72 | + }, semanticsEnabled: false); |
| 73 | + }); |
| 74 | + |
| 75 | + group('With semantics', () { |
| 76 | + testWidgets('finds semantics of wrapped widgets', |
| 77 | + (WidgetTester tester) async { |
| 78 | + app.main(); |
| 79 | + await tester.pumpAndSettle(); |
| 80 | + |
| 81 | + if (!_newSemanticsAvailable()) { |
| 82 | + print('Skipping test: Needs flutter > 2.10'); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + final html.Element element = |
| 87 | + _getHtmlElementAtCenter(clickableButtonFinder, tester); |
| 88 | + |
| 89 | + expect(element.tagName.toLowerCase(), 'flt-semantics'); |
| 90 | + expect(element.getAttribute('aria-label'), 'Works As Expected'); |
| 91 | + }); |
| 92 | + |
| 93 | + testWidgets( |
| 94 | + 'finds semantics of wrapped widgets with intercepting set to false', |
| 95 | + (WidgetTester tester) async { |
| 96 | + app.main(); |
| 97 | + await tester.pumpAndSettle(); |
| 98 | + |
| 99 | + if (!_newSemanticsAvailable()) { |
| 100 | + print('Skipping test: Needs flutter > 2.10'); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + final html.Element element = |
| 105 | + _getHtmlElementAtCenter(clickableWrappedButtonFinder, tester); |
| 106 | + |
| 107 | + expect(element.tagName.toLowerCase(), 'flt-semantics'); |
| 108 | + expect(element.getAttribute('aria-label'), |
| 109 | + 'Never calls onPressed transparent'); |
| 110 | + }); |
| 111 | + |
| 112 | + testWidgets('finds semantics of unwrapped elements', |
| 113 | + (WidgetTester tester) async { |
| 114 | + app.main(); |
| 115 | + await tester.pumpAndSettle(); |
| 116 | + |
| 117 | + if (!_newSemanticsAvailable()) { |
| 118 | + print('Skipping test: Needs flutter > 2.10'); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + final html.Element element = |
| 123 | + _getHtmlElementAtCenter(nonClickableButtonFinder, tester); |
| 124 | + |
| 125 | + expect(element.tagName.toLowerCase(), 'flt-semantics'); |
| 126 | + expect(element.getAttribute('aria-label'), 'Never calls onPressed'); |
| 127 | + }); |
| 128 | + |
| 129 | + // Notice that, when hit-testing the background platform view, instead of |
| 130 | + // finding a semantics node, the platform view itself is found. This is |
| 131 | + // because the platform view does not add interactive semantics nodes into |
| 132 | + // the framework's semantics tree. Instead, its semantics is determined by |
| 133 | + // the HTML content of the platform view itself. Flutter's semantics tree |
| 134 | + // simply allows the hit test to land on the platform view by making itself |
| 135 | + // hit test transparent. |
| 136 | + testWidgets('on background directly', (WidgetTester tester) async { |
| 137 | + app.main(); |
| 138 | + await tester.pumpAndSettle(); |
| 139 | + |
| 140 | + final html.Element element = |
| 141 | + _getHtmlElementAt(tester.getTopLeft(backgroundFinder)); |
| 142 | + |
| 143 | + expect(element.id, 'background-html-view'); |
| 144 | + }); |
| 145 | + }); |
| 146 | +} |
| 147 | + |
| 148 | +// Calls [_getHtmlElementAt] passing it the center of the widget identified by |
| 149 | +// the `finder`. |
| 150 | +html.Element _getHtmlElementAtCenter(Finder finder, WidgetTester tester) { |
| 151 | + final Offset point = tester.getCenter(finder); |
| 152 | + return _getHtmlElementAt(point); |
| 153 | +} |
| 154 | + |
| 155 | +// Locates the DOM element at the given `point` using `elementFromPoint`. |
| 156 | +// |
| 157 | +// `elementFromPoint` is an approximate proxy for a hit test, although it's |
| 158 | +// sensitive to the presence of shadow roots and browser quirks (not all |
| 159 | +// browsers agree on what it should return in all situations). Since this test |
| 160 | +// runs only in Chromium, it relies on Chromium's behavior. |
| 161 | +html.Element _getHtmlElementAt(Offset point) { |
| 162 | + // Probe at the shadow so the browser reports semantics nodes in addition to |
| 163 | + // platform view elements. If probed from `html.document` the browser hides |
| 164 | + // the contents of <flt-glass-name> as an implementation detail. |
| 165 | + final html.ShadowRoot glassPaneShadow = |
| 166 | + html.document.querySelector('flt-glass-pane')!.shadowRoot!; |
| 167 | + return glassPaneShadow.elementFromPoint(point.dx.toInt(), point.dy.toInt())!; |
| 168 | +} |
| 169 | + |
| 170 | +// TODO(dit): Remove this after flutter master (2.13) lands into stable. |
| 171 | +// This detects that we can do new semantics assertions by looking at the 'id' |
| 172 | +// attribute on flt-semantics elements (it is now set in 2.13 and up). |
| 173 | +bool _newSemanticsAvailable() { |
| 174 | + final html.ShadowRoot glassPaneShadow = |
| 175 | + html.document.querySelector('flt-glass-pane')!.shadowRoot!; |
| 176 | + final List<html.Element> elements = |
| 177 | + glassPaneShadow.querySelectorAll('flt-semantics[id]'); |
| 178 | + return elements.isNotEmpty; |
16 | 179 | }
|
0 commit comments