fix mobile a11y placeholder (flutter/engine#26238)

This commit is contained in:
Yegor
2021-05-19 14:44:02 -07:00
committed by GitHub
parent dcb08fa9ae
commit 119d24d270
3 changed files with 148 additions and 146 deletions

View File

@@ -299,72 +299,63 @@ class MobileSemanticsEnabler extends SemanticsEnabler {
return false;
}
// In Chrome the debouncing works well enough to detect accessibility
// request.
final bool blinkEnableConditionPassed =
(browserEngine == BrowserEngine.blink ||
browserEngine == BrowserEngine.samsung) &&
EngineSemanticsOwner.instance.gestureMode ==
GestureMode.browserGestures;
// 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 and TalkBack
// generate 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 enableConditionPassed = false;
html.Point<num> activationPoint;
// 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<num> activationPoint;
switch (event.type) {
case 'click':
final html.MouseEvent click = event as html.MouseEvent;
activationPoint = click.offset;
break;
case 'touchstart':
case 'touchend':
final html.TouchEvent touch = event as html.TouchEvent;
activationPoint = touch.changedTouches!.first.client;
break;
case 'pointerdown':
case 'pointerup':
final html.PointerEvent touch = event as html.PointerEvent;
activationPoint = new html.Point(touch.client.x, touch.client.y);
break;
default:
// The event is not relevant, forward to framework as normal.
return true;
}
final html.Rectangle<num> activatingElementRect =
domRenderer.glassPaneElement!.getBoundingClientRect();
final double midX = (activatingElementRect.left +
(activatingElementRect.right - activatingElementRect.left) / 2)
.toDouble();
final double midY = (activatingElementRect.top +
(activatingElementRect.bottom - activatingElementRect.top) / 2)
.toDouble();
final double deltaX = activationPoint.x.toDouble() - midX;
final double deltaY = activationPoint.y.toDouble() - midY;
final double deltaSquared = deltaX * deltaX + deltaY * deltaY;
if (deltaSquared < 1.0) {
safariEnableConditionPassed = true;
}
switch (event.type) {
case 'click':
final html.MouseEvent click = event as html.MouseEvent;
activationPoint = click.offset;
break;
case 'touchstart':
case 'touchend':
final html.TouchEvent touch = event as html.TouchEvent;
activationPoint = touch.changedTouches!.first.client;
break;
case 'pointerdown':
case 'pointerup':
final html.PointerEvent touch = event as html.PointerEvent;
activationPoint = new html.Point(touch.client.x, touch.client.y);
break;
default:
// The event is not relevant, forward to framework as normal.
return true;
}
if (blinkEnableConditionPassed || safariEnableConditionPassed) {
final html.Rectangle<num> activatingElementRect =
_semanticsPlaceholder!.getBoundingClientRect();
final double midX = (activatingElementRect.left +
(activatingElementRect.right - activatingElementRect.left) / 2)
.toDouble();
final double midY = (activatingElementRect.top +
(activatingElementRect.bottom - activatingElementRect.top) / 2)
.toDouble();
final double deltaX = activationPoint.x.toDouble() - midX;
final double deltaY = activationPoint.y.toDouble() - midY;
final double deltaSquared = deltaX * deltaX + deltaY * deltaY;
if (deltaSquared < 1.0) {
enableConditionPassed = true;
}
if (enableConditionPassed) {
assert(semanticsActivationTimer == null);
_schedulePlaceholderRemoval = true;
semanticsActivationTimer = Timer(_periodToConsumeEvents, () {
dispose();
EngineSemanticsOwner.instance.semanticsEnabled = true;
_schedulePlaceholderRemoval = true;
});
return false;
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.6
// @dart = 2.12
import 'dart:html' as html;
import 'package:test/bootstrap/browser.dart';
@@ -17,43 +17,42 @@ void main() {
void testMain() {
group('$DesktopSemanticsEnabler', () {
DesktopSemanticsEnabler desktopSemanticsEnabler;
html.Element _placeholder;
late DesktopSemanticsEnabler desktopSemanticsEnabler;
late html.Element? _placeholder;
setUp(() {
EngineSemanticsOwner.instance.semanticsEnabled = false;
desktopSemanticsEnabler = DesktopSemanticsEnabler();
_placeholder = desktopSemanticsEnabler.prepareAccessibilityPlaceholder();
html.document.body!.append(_placeholder!);
});
tearDown(() {
if (_placeholder != null) {
_placeholder.remove();
}
expect(_placeholder, isNotNull,
reason: 'Expected the test to create a placeholder');
_placeholder!.remove();
EngineSemanticsOwner.instance.semanticsEnabled = false;
});
test('prepare accesibility placeholder', () async {
_placeholder = desktopSemanticsEnabler.prepareAccessibilityPlaceholder();
expect(_placeholder!.getAttribute('role'), 'button');
expect(_placeholder!.getAttribute('aria-live'), 'true');
expect(_placeholder!.getAttribute('tabindex'), '0');
expect(_placeholder.getAttribute('role'), 'button');
expect(_placeholder.getAttribute('aria-live'), 'true');
expect(_placeholder.getAttribute('tabindex'), '0');
html.document.body.append(_placeholder);
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);
},
// TODO(nurhan): https://github.com/flutter/flutter/issues/50590
skip: browserEngine == BrowserEngine.webkit);
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.prepareAccessibilityPlaceholder();
html.document.body.append(_placeholder);
// Attach the placeholder to dom.
html.document.body!.append(_placeholder!);
html.Event event = html.MouseEvent('mousemove');
bool shouldForwardToFramework =
@@ -74,12 +73,8 @@ void testMain() {
test(
'Relevants events targeting placeholder should not be forwarded to the framework',
() async {
// Prework. Attach the placeholder to dom.
_placeholder = desktopSemanticsEnabler.prepareAccessibilityPlaceholder();
html.document.body.append(_placeholder);
html.Event event = html.MouseEvent('mousedown');
_placeholder.dispatchEvent(event);
_placeholder!.dispatchEvent(event);
bool shouldForwardToFramework =
desktopSemanticsEnabler.tryEnableSemantics(event);
@@ -88,61 +83,89 @@ void testMain() {
});
test('disposes of the placeholder', () {
_placeholder = desktopSemanticsEnabler.prepareAccessibilityPlaceholder();
html.document.body.append(_placeholder);
html.document.body!.append(_placeholder!);
expect(_placeholder.isConnected, isTrue);
expect(_placeholder!.isConnected, isTrue);
desktopSemanticsEnabler.dispose();
expect(_placeholder.isConnected, isFalse);
expect(_placeholder!.isConnected, isFalse);
});
});
}, skip: isMobile);
group('$MobileSemanticsEnabler', () {
MobileSemanticsEnabler mobileSemanticsEnabler;
html.Element _placeholder;
group(
'$MobileSemanticsEnabler',
() {
late MobileSemanticsEnabler mobileSemanticsEnabler;
html.Element? _placeholder;
setUp(() {
mobileSemanticsEnabler = MobileSemanticsEnabler();
});
setUp(() {
EngineSemanticsOwner.instance.semanticsEnabled = false;
mobileSemanticsEnabler = MobileSemanticsEnabler();
_placeholder = mobileSemanticsEnabler.prepareAccessibilityPlaceholder();
html.document.body!.append(_placeholder!);
});
tearDown(() {
if (_placeholder != null) {
_placeholder.remove();
}
});
tearDown(() {
_placeholder!.remove();
EngineSemanticsOwner.instance.semanticsEnabled = false;
});
test('prepare accesibility placeholder', () async {
_placeholder = mobileSemanticsEnabler.prepareAccessibilityPlaceholder();
test('prepare accesibility placeholder', () async {
expect(_placeholder!.getAttribute('role'), 'button');
expect(_placeholder.getAttribute('role'), 'button');
// Placeholder should cover all the screen on a mobile device.
final num bodyHeight = html.window.innerHeight!;
final num bodyWidht = html.window.innerWidth!;
html.document.body.append(_placeholder);
expect(_placeholder!.getBoundingClientRect().height, bodyHeight);
expect(_placeholder!.getBoundingClientRect().width, bodyWidht);
});
// Placeholder should cover all the screen on a mobile device.
final num bodyHeight = html.window.innerHeight;
final num bodyWidht = html.window.innerWidth;
test('Non-relevant events should be forwarded to the framework',
() async {
html.Event event;
if (_defaultSupportDetector.hasPointerEvents) {
event = html.PointerEvent('pointermove');
} else if (_defaultSupportDetector.hasTouchEvents) {
event = html.TouchEvent('touchcancel');
} else {
event = html.MouseEvent('mousemove');
}
expect(_placeholder.getBoundingClientRect().height, bodyHeight);
expect(_placeholder.getBoundingClientRect().width, bodyWidht);
bool shouldForwardToFramework =
mobileSemanticsEnabler.tryEnableSemantics(event);
expect(shouldForwardToFramework, true);
});
test('Enables semantics when receiving a relevant event', () {
expect(mobileSemanticsEnabler.semanticsActivationTimer, isNull);
// Send a click off center
_placeholder!.dispatchEvent(html.MouseEvent(
'click',
clientX: 0,
clientY: 0,
));
expect(mobileSemanticsEnabler.semanticsActivationTimer, isNull);
// Send a click at center
final html.Rectangle<num> activatingElementRect =
_placeholder!.getBoundingClientRect();
final int midX = (activatingElementRect.left +
(activatingElementRect.right - activatingElementRect.left) / 2)
.toInt();
final int midY = (activatingElementRect.top +
(activatingElementRect.bottom - activatingElementRect.top) / 2)
.toInt();
_placeholder!.dispatchEvent(html.MouseEvent(
'click',
clientX: midX,
clientY: midY,
));
expect(mobileSemanticsEnabler.semanticsActivationTimer, isNotNull);
});
},
// TODO(nurhan): https://github.com/flutter/flutter/issues/50590
skip: browserEngine == BrowserEngine.webkit);
test('Not relevant events should be forwarded to the framework', () async {
html.Event event;
if (_defaultSupportDetector.hasPointerEvents) {
event = html.PointerEvent('pointermove');
} else if (_defaultSupportDetector.hasTouchEvents) {
event = html.TouchEvent('touchcancel');
} else {
event = html.MouseEvent('mousemove');
}
bool shouldForwardToFramework =
mobileSemanticsEnabler.tryEnableSemantics(event);
expect(shouldForwardToFramework, true);
});
}, // Run the `MobileSemanticsEnabler` only on mobile browsers.
skip: isDesktop);
// We can run `MobileSemanticsEnabler` tests in mobile browsers and in desktop Chrome.
skip: isDesktop && browserEngine != BrowserEngine.blink,
);
}

View File

@@ -98,25 +98,13 @@ void _testEngineSemanticsOwner() {
clientY: (rect.top + (rect.bottom - rect.top) / 2).floor(),
));
// On mobile semantics is not enabled synchronously. This is because the
// placeholder receives pointer events in non-accessibility mode too, and
// therefore we wait to see if any subsequent pointer events are issued
// indicating that this is not a request to enable accessibility.
// On mobile semantics is enabled asynchronously.
if (isMobile) {
while (!semantics().semanticsEnabled) {
while (placeholder.isConnected) {
await Future<void>.delayed(const Duration(milliseconds: 50));
}
}
expect(semantics().semanticsEnabled, true);
// The placeholder should be removed
if (isMobile) {
// On mobile the placeholder is not removed synchronously. Instead it is
// removed upon the next DOM event. Otherwise Safari swallows pointerup.
expect(placeholder.isConnected, true);
placeholder.click();
await Future<void>.delayed(Duration.zero);
}
expect(placeholder.isConnected, false);
});