diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 37391a4f53..c296ebe416 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -415,6 +415,7 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/label_and_value.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/live_region.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/scrollable.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart +FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/semantics_helper.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/tappable.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/text_field.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/services/buffers.dart diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine.dart b/engine/src/flutter/lib/web_ui/lib/src/engine.dart index 4aa372e1c9..da577462e9 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine.dart @@ -73,6 +73,7 @@ part 'engine/semantics/label_and_value.dart'; part 'engine/semantics/live_region.dart'; part 'engine/semantics/scrollable.dart'; part 'engine/semantics/semantics.dart'; +part 'engine/semantics/semantics_helper.dart'; part 'engine/semantics/tappable.dart'; part 'engine/semantics/text_field.dart'; part 'engine/services/buffers.dart'; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/browser_detection.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/browser_detection.dart index fffcadeef6..5d0f55d4a2 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/browser_detection.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/browser_detection.dart @@ -121,3 +121,19 @@ OperatingSystem _detectOperatingSystem() { return OperatingSystem.unknown; } } + +/// List of Operating Systems we know to be working on laptops/desktops. +/// +/// These devices tend to behave differently on many core issues such as events, +/// screen readers, input devices. +const Set _desktopOperatingSystems = { + OperatingSystem.macOs, + OperatingSystem.linux, + OperatingSystem.windows, +}; + +/// A flag to check if the current operating system is a laptop/desktop +/// operating system. +/// +/// See [_desktopOperatingSystems]. +bool get isDesktop => _desktopOperatingSystems.contains(operatingSystem); diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/dom_renderer.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/dom_renderer.dart index 4152e5e575..90c10b40d5 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/dom_renderer.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/dom_renderer.dart @@ -389,7 +389,18 @@ flt-glass-pane * { _glassPaneElement.append(_sceneHostElement); - EngineSemanticsOwner.instance.autoEnableOnTap(this); + final html.Element _accesibilityPlaceholder = EngineSemanticsOwner + .instance.enableSemantics + .prepareAccesibilityPlaceholder(); + + // Insert the semantics placeholder after the scene host. For all widgets + // in the scene, except for platform widgets, the scene host will pass the + // pointer events through to the semantics tree. However, for platform + // views, the pointer events will not pass through, and will be handled + // by the platform view. + glassPaneElement + .insertBefore(_accesibilityPlaceholder, _sceneHostElement); + PointerBinding(this); // Hide the DOM nodes used to render the scene from accessibility, because diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart index 6005d288ea..17f3026293 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart @@ -1198,195 +1198,7 @@ class EngineSemanticsOwner { _now = () => DateTime.now(); } - /// A temporary placeholder used to capture a request to activate semantics. - html.Element _semanticsPlaceholder; - - /// We do not immediately enable semantics when the user requests it, but - /// instead wait for a short period of time before doing it. This is because - /// the request comes as a tap on the [_semanticsPlaceholder]. The tap, - /// depending on the browser, comes as a burst of events. For example, Safari - /// sends "touchstart", "touchend", and "click". So during a short time period - /// we consume all events and prevent forwarding to the framework. Otherwise, - /// the events will be interpreted twice, once as a request to activate - /// semantics, and a second time by Flutter's gesture recognizers. - Timer _semanticsActivationTimer; - - /// The number of events we processed that could potentially activate - /// semantics. - int _semanticsActivationAttempts = 0; - - /// The maximum [_semanticsActivationAttempts] before we give up waiting for - /// the user to enable semantics. - /// - /// This number is arbitrary and can be adjusted if it doesn't work well. - static const int _kMaxSemanticsActivationAttempts = 20; - - /// Whether we are waiting for the user to enable semantics. - bool get _isWaitingToEnableSemantics => _semanticsPlaceholder != null; - - /// Instructs [_tryEnableSemantics] to remove [_semanticsPlaceholder]. - /// - /// On Chrome the placeholder is removed upon any next event. - /// - /// On Safari the placeholder is removed upon the next "touchend" event. This - /// is to prevent Safari from swallowing the event that happens on an element - /// that's being removed. Chrome doesn't have this issue. - bool _schedulePlaceholderRemoval = false; - - /// Attempts to activate semantics. - /// - /// Returns true if the `event` is not related to semantics activation and - /// should be forwarded to the framework. - bool _tryEnableSemantics(html.Event event) { - if (_schedulePlaceholderRemoval) { - final bool removeNow = - browserEngine != BrowserEngine.webkit || event.type == 'touchend'; - if (removeNow) { - _semanticsPlaceholder.remove(); - _semanticsPlaceholder = null; - _semanticsActivationTimer = null; - } - return true; - } - - if (semanticsEnabled) { - // Semantics already enabled, forward to framework as normal. - return true; - } - - _semanticsActivationAttempts += 1; - if (_semanticsActivationAttempts >= _kMaxSemanticsActivationAttempts) { - // We have received multiple user events, none of which resulted in - // semantics activation. This is a signal that the user is not interested - // in semantics, and so we will stop waiting for it. - _schedulePlaceholderRemoval = true; - return true; - } - - const List kInterestingEventTypes = [ - 'click', - 'touchstart', - 'touchend', - ]; - - if (!kInterestingEventTypes.contains(event.type)) { - // The event is not relevant, forward to framework as normal. - return true; - } - - if (_semanticsActivationTimer != null) { - // We are in a waiting period to activate a timer. While the timer is - // active we should consume events pertaining to semantics activation. - // Otherwise the event will also be interpreted by the framework and - // potentially result in activating a gesture in the app. - return false; - } - - // In Chrome the debouncing works well enough to detect accessibility - // request. - final bool blinkEnableConditionPassed = - browserEngine == BrowserEngine.blink && - _gestureMode == GestureMode.browserGestures; - - // In Safari debouncing doesn't work. Instead we look at where exactly - // (within 1 pixel) the event landed. If it landed exactly in the middle of - // the placeholder we interpret it as a signal to enable accessibility. This - // is because when VoiceOver generates a tap it lands it in the middle of - // the focused element. This method is a bit flawed in that a user's finger - // could theoretically land in the middle of the element too. However, the - // chance of that happening is very small. Even low-end phones typically - // have >2 million pixels (e.g. Moto G4). It is very unlikely that a user - // will land their finger exactly in the middle. In the worst case an - // unlucky user would accidentally enable accessibility and the app will be - // slightly slower than normal, but the app will continue functioning as - // normal. Our semantics tree is designed to not interfere with Flutter's - // gesture detection. - bool safariEnableConditionPassed = false; - if (browserEngine == BrowserEngine.webkit) { - html.Point activationPoint; - - switch (event.type) { - case 'click': - final html.MouseEvent click = event; - activationPoint = click.offset; - break; - case 'touchstart': - case 'touchend': - final html.TouchEvent touch = event; - activationPoint = touch.changedTouches.first.client; - break; - default: - // The event is not relevant, forward to framework as normal. - return true; - } - - assert(activationPoint != null); - - final html.Rectangle activatingElementRect = - domRenderer.glassPaneElement.getBoundingClientRect(); - final double midX = activatingElementRect.left + - (activatingElementRect.right - activatingElementRect.left) / 2; - final double midY = activatingElementRect.top + - (activatingElementRect.bottom - activatingElementRect.top) / 2; - final double deltaX = activationPoint.x - midX; - final double deltaY = activationPoint.y - midY; - final double deltaSquared = deltaX * deltaX + deltaY * deltaY; - if (deltaSquared < 1.0) { - safariEnableConditionPassed = true; - } - } - - if (blinkEnableConditionPassed || safariEnableConditionPassed) { - assert(_semanticsActivationTimer == null); - _semanticsActivationTimer = Timer(const Duration(milliseconds: 300), () { - semanticsEnabled = true; - _schedulePlaceholderRemoval = true; - }); - return false; - } - - // This was not a semantics activating event; forward as normal. - return true; - } - - /// The message in the label for the placeholder element used to enable - /// accessibility. - /// - /// This uses US English as the default message. Set this value prior to - /// calling `runApp` to translate to another language. - static String placeholderMessage = 'Enable accessibility'; - - /// Enables accessibility when the user taps on the glasspane via an - /// accessibility focus. - /// - /// This creates a placeholder inside the glasspane, which, when focused, - /// announces that accessibility can be enabled by tapping. - void autoEnableOnTap(DomRenderer domRenderer) { - _semanticsPlaceholder = html.Element.tag('flt-semantics-placeholder'); - - // Only listen to "click" because other kinds of events are reported via - // PointerBinding. - _semanticsPlaceholder.addEventListener('click', (html.Event event) { - _tryEnableSemantics(event); - }, true); - - _semanticsPlaceholder - ..setAttribute('role', 'button') - ..setAttribute('aria-label', placeholderMessage); - _semanticsPlaceholder.style - ..position = 'absolute' - ..left = '0' - ..top = '0' - ..right = '0' - ..bottom = '0'; - // Insert the semantics placeholder after the scene host. For all widgets - // in the scene, except for platform widgets, the scene host will pass the - // pointer events through to the semantics tree. However, for platform - // views, the pointer events will not pass through, and will be handled - // by the platform view. - domRenderer.glassPaneElement - .insertBefore(_semanticsPlaceholder, domRenderer.sceneHostElement); - } + final SemanticsHelper enableSemantics = SemanticsHelper(); /// Whether the user has requested that [updateSemantics] be called when /// the semantic contents of window changes. @@ -1526,18 +1338,15 @@ class EngineSemanticsOwner { 'mousedown', 'mousemove', 'mouseup', + 'keyup', + 'keydown', ]; if (_pointerEventTypes.contains(event.type)) { _temporarilyDisableBrowserGestureMode(); } - if (!_isWaitingToEnableSemantics) { - // Forward to framework as normal. - return true; - } else { - return _tryEnableSemantics(event); - } + return enableSemantics.shouldEnableSemantics(event); } /// Callbacks called when the [GestureMode] changes. diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics_helper.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics_helper.dart new file mode 100644 index 0000000000..d353b21f34 --- /dev/null +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics_helper.dart @@ -0,0 +1,385 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +part of engine; + +/// The maximum [semanticsActivationAttempts] before we give up waiting for +/// the user to enable semantics. +/// +/// This number is arbitrary and can be adjusted if it doesn't work well. +const int kMaxSemanticsActivationAttempts = 20; + +/// After an event related to semantics activation has been received, we consume +/// the consecutive events on the engine. Do not send them to the framework. +/// For example when a 'mousedown' targeting a placeholder received following +/// 'mouseup' is also not sent to the framework. +/// Otherwise these events can cause unintended gestures on the framework side. +const Duration _periodToConsumeEvents = const Duration(milliseconds: 300); + +/// The message in the label for the placeholder element used to enable +/// accessibility. +/// +/// This uses US English as the default message. Set this value prior to +/// calling `runApp` to translate to another language. +String placeholderMessage = 'Enable accessibility'; + +/// A helper for [EngineSemanticsOwner]. +/// +/// [SemanticsHelper] prepares and placeholder to enable semantics. +/// +/// It decides if an event is purely semantics enabling related or a regular +/// event which should be forwarded to the framework. +/// +/// It does this by using a [SemanticsEnabler]. The [SemanticsEnabler] +/// implementation is choosen using form factor type. +/// +/// See [DesktopSemanticsEnabler], [MobileSemanticsEnabler]. +class SemanticsHelper { + final _SemanticsEnabler _enableSemantics = + isDesktop ? DesktopSemanticsEnabler() : MobileSemanticsEnabler(); + + bool shouldEnableSemantics(html.Event event) { + return _enableSemantics.tryEnableSemantics(event); + } + + html.Element prepareAccesibilityPlaceholder() { + return _enableSemantics.prepareAccesibilityPlaceholder(); + } +} + +abstract class _SemanticsEnabler { + /// Whether to enable semantics. + /// + /// Semantics should be enabled if the web engine is no longer waiting for + /// extra signals from the user events. See [isWaitingToEnableSemantics]. + /// + /// Or if the received [html.Event] is suitable/enough for enabling the + /// semantics. See [tryEnableSemantics]. + bool shouldEnableSemantics(html.Event event) { + if (!isWaitingToEnableSemantics) { + // Forward to framework as normal. + return true; + } else { + return tryEnableSemantics(event); + } + } + + /// Attempts to activate semantics. + /// + /// Returns true if the `event` is not related to semantics activation and + /// should be forwarded to the framework. + bool tryEnableSemantics(html.Event event); + + /// Creates the placeholder for accesibility. + /// + /// Puts it inside the glasspane. + /// + /// On focus the element announces that accessibility can be enabled by + /// tapping/clicking. (Announcement depends on the assistive technology) + html.Element prepareAccesibilityPlaceholder(); + + /// Whether platform is still consisering enabling semantics. + /// + /// At this stage a relevant set of events are always assessed to see if + /// they activate the semantics. + /// + /// If not they are sent to framework as normal events. + bool get isWaitingToEnableSemantics; +} + +@visibleForTesting +class DesktopSemanticsEnabler extends _SemanticsEnabler { + /// We do not immediately enable semantics when the user requests it, but + /// instead wait for a short period of time before doing it. This is because + /// the request comes as an event targeted on the [_semanticsPlaceholder]. + /// This event, depending on the browser, comes as a burst of events. + /// For example, Safari on MacOS sends "pointerup", "pointerdown". So during a + /// short time period we consume all events and prevent forwarding to the + /// framework. Otherwise, the events will be interpreted twice, once as a + /// request to activate semantics, and a second time by Flutter's gesture + /// recognizers. + @visibleForTesting + Timer semanticsActivationTimer; + + /// A temporary placeholder used to capture a request to activate semantics. + html.Element _semanticsPlaceholder; + + /// The number of events we processed that could potentially activate + /// semantics. + int semanticsActivationAttempts = 0; + + /// Instructs [_tryEnableSemantics] to remove [_semanticsPlaceholder]. + /// + /// The placeholder is removed upon any next event. + bool _schedulePlaceholderRemoval = false; + + /// Whether we are waiting for the user to enable semantics. + @override + bool get isWaitingToEnableSemantics => _semanticsPlaceholder != null; + + @override + bool tryEnableSemantics(html.Event event) { + if (_schedulePlaceholderRemoval) { + _semanticsPlaceholder.remove(); + _semanticsPlaceholder = null; + semanticsActivationTimer = null; + return true; + } + + if (EngineSemanticsOwner.instance.semanticsEnabled) { + // Semantics already enabled, forward to framework as normal. + return true; + } + + // In touch screen laptops, the touch is received as a mouse click + const Set kInterestingEventTypes = { + 'click', + 'keyup', + 'keydown', + 'mouseup', + 'mousedown', + 'pointerdown', + 'pointerup', + }; + + if (!kInterestingEventTypes.contains(event.type)) { + // The event is not relevant, forward to framework as normal. + return true; + } + + semanticsActivationAttempts += 1; + if (semanticsActivationAttempts >= kMaxSemanticsActivationAttempts) { + // We have received multiple user events, none of which resulted in + // semantics activation. This is a signal that the user is not interested + // in semantics, and so we will stop waiting for it. + _schedulePlaceholderRemoval = true; + return true; + } + + if (semanticsActivationTimer != null) { + // We are in a waiting period to activate a timer. While the timer is + // active we should consume events pertaining to semantics activation. + // Otherwise the event will also be interpreted by the framework and + // potentially result in activating a gesture in the app. + return false; + } + + // Check for the event target. + final bool enableConditionPassed = (event.target == _semanticsPlaceholder); + + if (enableConditionPassed) { + assert(semanticsActivationTimer == null); + semanticsActivationTimer = Timer(_periodToConsumeEvents, () { + EngineSemanticsOwner.instance.semanticsEnabled = true; + _schedulePlaceholderRemoval = true; + }); + return false; + } + + // This was not a semantics activating event; forward as normal. + return true; + } + + @override + html.Element prepareAccesibilityPlaceholder() { + _semanticsPlaceholder = html.Element.tag('flt-semantics-placeholder'); + + // Only listen to "click" because other kinds of events are reported via + // PointerBinding. + _semanticsPlaceholder.addEventListener('click', (html.Event event) { + tryEnableSemantics(event); + }, true); + + // Adding roles to semantics placeholder. 'aria-live' will make sure that + // the content is announced to the assistive technology user as soon as the + // page receives focus. 'tab-index' makes sure the button is the first + // target of tab. 'aria-label' is used to define the placeholder message + // to the assistive technology user. + _semanticsPlaceholder + ..setAttribute('role', 'button') + ..setAttribute('aria-live', 'true') + ..setAttribute('tabindex', '0') + ..setAttribute('aria-label', placeholderMessage); + _semanticsPlaceholder.style + ..position = 'absolute' + ..left = '-1px' + ..top = '-1px' + ..width = '1px' + ..height = '1px'; + return _semanticsPlaceholder; + } +} + +@visibleForTesting +class MobileSemanticsEnabler extends _SemanticsEnabler { + /// We do not immediately enable semantics when the user requests it, but + /// instead wait for a short period of time before doing it. This is because + /// the request comes as an event targeted on the [_semanticsPlaceholder]. + /// This event, depending on the browser, comes as a burst of events. + /// For example, Safari on IOS sends "touchstart", "touchend", and "click". + /// So during a short time period we consume all events and prevent forwarding + /// to the framework. Otherwise, the events will be interpreted twice, once as + /// a request to activate semantics, and a second time by Flutter's gesture + /// recognizers. + @visibleForTesting + Timer semanticsActivationTimer; + + /// A temporary placeholder used to capture a request to activate semantics. + html.Element _semanticsPlaceholder; + + /// The number of events we processed that could potentially activate + /// semantics. + int semanticsActivationAttempts = 0; + + /// Instructs [_tryEnableSemantics] to remove [_semanticsPlaceholder]. + /// + /// For Blink browser engine the placeholder is removed upon any next event. + /// + /// For Webkit browser engine the placeholder is removed upon the next + /// "touchend" event. This is to prevent Safari from swallowing the event + /// that happens on an element that's being removed. Blink doesn't have + /// this issue. + bool _schedulePlaceholderRemoval = false; + + /// Whether we are waiting for the user to enable semantics. + @override + bool get isWaitingToEnableSemantics => _semanticsPlaceholder != null; + + @override + bool tryEnableSemantics(html.Event event) { + if (_schedulePlaceholderRemoval) { + final bool removeNow = + (browserEngine != BrowserEngine.webkit || event.type == 'touchend'); + if (removeNow) { + _semanticsPlaceholder.remove(); + _semanticsPlaceholder = null; + semanticsActivationTimer = null; + } + return true; + } + + if (EngineSemanticsOwner.instance.semanticsEnabled) { + // Semantics already enabled, forward to framework as normal. + return true; + } + + semanticsActivationAttempts += 1; + if (semanticsActivationAttempts >= kMaxSemanticsActivationAttempts) { + // We have received multiple user events, none of which resulted in + // semantics activation. This is a signal that the user is not interested + // in semantics, and so we will stop waiting for it. + _schedulePlaceholderRemoval = true; + return true; + } + + const Set kInterestingEventTypes = { + 'click', + 'touchstart', + 'touchend', + }; + + if (!kInterestingEventTypes.contains(event.type)) { + // The event is not relevant, forward to framework as normal. + return true; + } + + if (semanticsActivationTimer != null) { + // We are in a waiting period to activate a timer. While the timer is + // active we should consume events pertaining to semantics activation. + // Otherwise the event will also be interpreted by the framework and + // potentially result in activating a gesture in the app. + return false; + } + + // In Chrome the debouncing works well enough to detect accessibility + // request. + final bool blinkEnableConditionPassed = + browserEngine == BrowserEngine.blink && + EngineSemanticsOwner.instance.gestureMode == + GestureMode.browserGestures; + + // In Safari debouncing doesn't work. Instead we look at where exactly + // (within 1 pixel) the event landed. If it landed exactly in the middle of + // the placeholder we interpret it as a signal to enable accessibility. This + // is because when VoiceOver generates a tap it lands it in the middle of + // the focused element. This method is a bit flawed in that a user's finger + // could theoretically land in the middle of the element too. However, the + // chance of that happening is very small. Even low-end phones typically + // have >2 million pixels (e.g. Moto G4). It is very unlikely that a user + // will land their finger exactly in the middle. In the worst case an + // unlucky user would accidentally enable accessibility and the app will be + // slightly slower than normal, but the app will continue functioning as + // normal. Our semantics tree is designed to not interfere with Flutter's + // gesture detection. + bool safariEnableConditionPassed = false; + if (browserEngine == BrowserEngine.webkit) { + html.Point activationPoint; + + switch (event.type) { + case 'click': + final html.MouseEvent click = event; + activationPoint = click.offset; + break; + case 'touchstart': + case 'touchend': + final html.TouchEvent touch = event; + activationPoint = touch.changedTouches.first.client; + break; + default: + // The event is not relevant, forward to framework as normal. + return true; + } + + assert(activationPoint != null); + + final html.Rectangle activatingElementRect = + domRenderer.glassPaneElement.getBoundingClientRect(); + final double midX = activatingElementRect.left + + (activatingElementRect.right - activatingElementRect.left) / 2; + final double midY = activatingElementRect.top + + (activatingElementRect.bottom - activatingElementRect.top) / 2; + final double deltaX = activationPoint.x - midX; + final double deltaY = activationPoint.y - midY; + final double deltaSquared = deltaX * deltaX + deltaY * deltaY; + if (deltaSquared < 1.0) { + safariEnableConditionPassed = true; + } + } + + if (blinkEnableConditionPassed || safariEnableConditionPassed) { + assert(semanticsActivationTimer == null); + semanticsActivationTimer = Timer(_periodToConsumeEvents, () { + EngineSemanticsOwner.instance.semanticsEnabled = true; + _schedulePlaceholderRemoval = true; + }); + return false; + } + + // This was not a semantics activating event; forward as normal. + return true; + } + + @override + html.Element prepareAccesibilityPlaceholder() { + _semanticsPlaceholder = html.Element.tag('flt-semantics-placeholder'); + + // Only listen to "click" because other kinds of events are reported via + // PointerBinding. + _semanticsPlaceholder.addEventListener('click', (html.Event event) { + tryEnableSemantics(event); + }, true); + + _semanticsPlaceholder + ..setAttribute('role', 'button') + ..setAttribute('aria-label', placeholderMessage); + _semanticsPlaceholder.style + ..position = 'absolute' + ..left = '0' + ..top = '0' + ..right = '0' + ..bottom = '0'; + + return _semanticsPlaceholder; + } +} diff --git a/engine/src/flutter/lib/web_ui/test/dom_renderer_test.dart b/engine/src/flutter/lib/web_ui/test/dom_renderer_test.dart index 724cc318e6..7ff6fc078c 100644 --- a/engine/src/flutter/lib/web_ui/test/dom_renderer_test.dart +++ b/engine/src/flutter/lib/web_ui/test/dom_renderer_test.dart @@ -116,4 +116,11 @@ void main() { final DomRenderer renderer = DomRenderer(); renderer.reset(); }); + + test('accesibility placeholder is attached after creation', () { + DomRenderer(); + + expect(html.document.getElementsByTagName('flt-semantics-placeholder'), + isNotEmpty); + }); } diff --git a/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_helper_test.dart b/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_helper_test.dart new file mode 100644 index 0000000000..c7fd8bc204 --- /dev/null +++ b/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_helper_test.dart @@ -0,0 +1,147 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:html' as html; + +import 'package:ui/src/engine.dart'; + +import 'package:test/test.dart'; + +void main() { + group('$DesktopSemanticsEnabler', () { + DesktopSemanticsEnabler desktopSemanticsEnabler; + html.Element _placeholder; + + setUp(() { + desktopSemanticsEnabler = DesktopSemanticsEnabler(); + }); + + tearDown(() { + if (_placeholder != null) { + _placeholder.remove(); + } + if(desktopSemanticsEnabler?.semanticsActivationTimer != null) { + desktopSemanticsEnabler.semanticsActivationTimer.cancel(); + desktopSemanticsEnabler.semanticsActivationTimer = null; + } + }); + + test('prepare accesibility placeholder', () async { + _placeholder = desktopSemanticsEnabler.prepareAccesibilityPlaceholder(); + + expect(_placeholder.getAttribute('role'), 'button'); + expect(_placeholder.getAttribute('aria-live'), 'true'); + expect(_placeholder.getAttribute('tabindex'), '0'); + + html.document.body.append(_placeholder); + + expect(html.document.getElementsByTagName('flt-semantics-placeholder'), + isNotEmpty); + + expect(_placeholder.getBoundingClientRect().height, 1); + expect(_placeholder.getBoundingClientRect().width, 1); + expect(_placeholder.getBoundingClientRect().top, -1); + expect(_placeholder.getBoundingClientRect().left, -1); + }); + + test('Not relevant events should be forwarded to the framework', () async { + // Prework. Attach the placeholder to dom. + _placeholder = desktopSemanticsEnabler.prepareAccesibilityPlaceholder(); + html.document.body.append(_placeholder); + + html.Event event = html.MouseEvent('mousemove'); + bool shouldForwardToFramework = + desktopSemanticsEnabler.tryEnableSemantics(event); + + expect(shouldForwardToFramework, true); + + event = html.PointerEvent('pointermove'); + shouldForwardToFramework = + desktopSemanticsEnabler.tryEnableSemantics(event); + + expect(shouldForwardToFramework, true); + }); + + test( + 'Relevants events targeting placeholder should not be forwarded to the framework', + () async { + // Prework. Attach the placeholder to dom. + _placeholder = desktopSemanticsEnabler.prepareAccesibilityPlaceholder(); + html.document.body.append(_placeholder); + + html.Event event = html.MouseEvent('mousedown'); + _placeholder.dispatchEvent(event); + + bool shouldForwardToFramework = + desktopSemanticsEnabler.tryEnableSemantics(event); + + expect(shouldForwardToFramework, false); + }); + + test( + 'After max number of relevant events, events should be forwarded to the framework', + () async { + // Prework. Attach the placeholder to dom. + _placeholder = desktopSemanticsEnabler.prepareAccesibilityPlaceholder(); + html.document.body.append(_placeholder); + + html.Event event = html.MouseEvent('mousedown'); + _placeholder.dispatchEvent(event); + + bool shouldForwardToFramework = + desktopSemanticsEnabler.tryEnableSemantics(event); + + expect(shouldForwardToFramework, false); + + // Send max number of events; + for (int i = 1; i <= kMaxSemanticsActivationAttempts; i++) { + event = html.MouseEvent('mousedown'); + _placeholder.dispatchEvent(event); + + shouldForwardToFramework = + desktopSemanticsEnabler.tryEnableSemantics(event); + } + + expect(shouldForwardToFramework, true); + }); + }); + + group('$MobileSemanticsEnabler', () { + MobileSemanticsEnabler mobileSemanticsEnabler; + html.Element _placeholder; + + setUp(() { + mobileSemanticsEnabler = MobileSemanticsEnabler(); + }); + + tearDown(() { + if (_placeholder != null) { + _placeholder.remove(); + } + }); + + test('prepare accesibility placeholder', () async { + _placeholder = mobileSemanticsEnabler.prepareAccesibilityPlaceholder(); + + expect(_placeholder.getAttribute('role'), 'button'); + + html.document.body.append(_placeholder); + + // Placeholder should cover all the screen on a mobile device. + final num bodyHeight = html.window.innerHeight; + final num bodyWidht = html.window.innerWidth; + + expect(_placeholder.getBoundingClientRect().height, bodyHeight); + expect(_placeholder.getBoundingClientRect().width, bodyWidht); + }); + + test('Not relevant events should be forwarded to the framework', () async { + final html.Event event = html.TouchEvent('touchcancel'); + bool shouldForwardToFramework = + mobileSemanticsEnabler.tryEnableSemantics(event); + + expect(shouldForwardToFramework, true); + }); + }); +}