forked from firka/flutter
[web] Implement multi-view for semantics (flutter/engine#48251)
Implement multi-view for semantics. Main changes: - `EngineSemanticsOwner` singleton is split into two classes: - `EngineSemantics` is a singleton that manages global properties of the semantics tree, such as whether semantics is currently enabled, and what gesture mode is used. - `EngineSemanticsOwner` now supports creating multiple instances of the class. `EngineFlutterView` now points a `EngineSemanticsOwner` that it owns. - Fix a number of issues with disposal logic. Now that views can come in and out disposal has to be more robust than previously. `EngineSemanticsOwner` acquired a `reset()` and `SemanticsObject` acquired a `dispose()` that clean up the tree and individual nodes respectively. - In particular, this fixes an issue in Skwasm mode where slight differences in asynchrony caused the semantics test to fail because old nodes continued firing events after they were removed from the document due to improper disposal.
This commit is contained in:
@@ -545,4 +545,7 @@ class SemanticsActionEvent {
|
||||
arguments: arguments == _noArgumentPlaceholder ? this.arguments : arguments,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() => 'SemanticsActionEvent($type, view: $viewId, node: $nodeId)';
|
||||
}
|
||||
|
||||
@@ -50,8 +50,15 @@ void registerHotRestartListener(ui.VoidCallback listener) {
|
||||
/// such as removing static DOM listeners, prior to allowing the Dart runtime
|
||||
/// to re-initialize the program.
|
||||
void debugEmulateHotRestart() {
|
||||
for (final ui.VoidCallback listener in _hotRestartListeners) {
|
||||
listener();
|
||||
// While hot restart listeners are executing, more listeners may be added. To
|
||||
// avoid concurrent modification, the listeners are copies and emptied. If new
|
||||
// listeners are added in the process, the loop will pick them up.
|
||||
while (_hotRestartListeners.isNotEmpty) {
|
||||
final List<ui.VoidCallback> copyOfListeners = _hotRestartListeners.toList();
|
||||
_hotRestartListeners.clear();
|
||||
for (final ui.VoidCallback listener in copyOfListeners) {
|
||||
listener();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -148,7 +148,7 @@ class KeyboardBinding {
|
||||
if (_debugLogKeyEvents) {
|
||||
print(event.type);
|
||||
}
|
||||
if (EngineSemanticsOwner.instance.receiveGlobalEvent(event)) {
|
||||
if (EngineSemantics.instance.receiveGlobalEvent(event)) {
|
||||
handler(event);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -806,7 +806,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
|
||||
call `updateSemantics`.
|
||||
''')
|
||||
void updateSemantics(ui.SemanticsUpdate update) {
|
||||
EngineSemanticsOwner.instance.updateSemantics(update);
|
||||
implicitView?.semantics.updateSemantics(update);
|
||||
}
|
||||
|
||||
/// This is equivalent to `locales.first`, except that it will provide an
|
||||
|
||||
@@ -523,7 +523,7 @@ abstract class _BaseAdapter {
|
||||
// Report the event to semantics. This information is used to debounce
|
||||
// browser gestures. Semantics tells us whether it is safe to forward
|
||||
// the event to the framework.
|
||||
if (EngineSemanticsOwner.instance.receiveGlobalEvent(event)) {
|
||||
if (EngineSemantics.instance.receiveGlobalEvent(event)) {
|
||||
handler(event);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import 'package:ui/ui.dart' as ui show Offset;
|
||||
|
||||
import '../dom.dart';
|
||||
import '../platform_dispatcher.dart';
|
||||
import '../semantics.dart' show EngineSemanticsOwner;
|
||||
import '../semantics.dart' show EngineSemantics;
|
||||
import '../text_editing/text_editing.dart';
|
||||
import '../vector_math.dart';
|
||||
|
||||
@@ -25,7 +25,7 @@ import '../vector_math.dart';
|
||||
/// offsetX, offsetY == 0 (TalkBack events).
|
||||
ui.Offset computeEventOffsetToTarget(DomMouseEvent event, DomElement actualTarget) {
|
||||
// On a TalkBack event
|
||||
if (EngineSemanticsOwner.instance.semanticsEnabled && event.offsetX == 0 && event.offsetY == 0) {
|
||||
if (EngineSemantics.instance.semanticsEnabled && event.offsetX == 0 && event.offsetY == 0) {
|
||||
return _computeOffsetForTalkbackEvent(event, actualTarget);
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ ui.Offset _computeOffsetForInputs(DomMouseEvent event, EditableTextGeometry inpu
|
||||
/// positions. Finally, we deduct that from clientX, clientY of the event.
|
||||
// TODO(dit): Make this understand 3D transforms, https://github.com/flutter/flutter/issues/117091
|
||||
ui.Offset _computeOffsetForTalkbackEvent(DomMouseEvent event, DomElement actualTarget) {
|
||||
assert(EngineSemanticsOwner.instance.semanticsEnabled);
|
||||
assert(EngineSemantics.instance.semanticsEnabled);
|
||||
// Use clientX/clientY as the position of the event (this is relative to
|
||||
// the top left of the page, including scroll)
|
||||
double offsetX = event.clientX;
|
||||
|
||||
@@ -55,7 +55,7 @@ class Incrementable extends PrimaryRoleManager {
|
||||
_gestureModeListener = (GestureMode mode) {
|
||||
update();
|
||||
};
|
||||
semanticsObject.owner.addGestureModeListener(_gestureModeListener);
|
||||
EngineSemantics.instance.addGestureModeListener(_gestureModeListener);
|
||||
_focusManager.manage(semanticsObject.id, _element);
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ class Incrementable extends PrimaryRoleManager {
|
||||
/// Disables the input [_element] when the gesture mode switches to
|
||||
/// [GestureMode.pointerEvents], and enables it when the mode switches back to
|
||||
/// [GestureMode.browserGestures].
|
||||
GestureModeCallback? _gestureModeListener;
|
||||
late final GestureModeCallback _gestureModeListener;
|
||||
|
||||
/// Whether we forwarded a semantics action to the framework and awaiting an
|
||||
/// update.
|
||||
@@ -89,7 +89,7 @@ class Incrementable extends PrimaryRoleManager {
|
||||
void update() {
|
||||
super.update();
|
||||
|
||||
switch (semanticsObject.owner.gestureMode) {
|
||||
switch (EngineSemantics.instance.gestureMode) {
|
||||
case GestureMode.browserGestures:
|
||||
_enableBrowserGestureHandling();
|
||||
_updateInputValues();
|
||||
@@ -100,7 +100,7 @@ class Incrementable extends PrimaryRoleManager {
|
||||
}
|
||||
|
||||
void _enableBrowserGestureHandling() {
|
||||
assert(semanticsObject.owner.gestureMode == GestureMode.browserGestures);
|
||||
assert(EngineSemantics.instance.gestureMode == GestureMode.browserGestures);
|
||||
if (!_element.disabled!) {
|
||||
return;
|
||||
}
|
||||
@@ -108,7 +108,7 @@ class Incrementable extends PrimaryRoleManager {
|
||||
}
|
||||
|
||||
void _updateInputValues() {
|
||||
assert(semanticsObject.owner.gestureMode == GestureMode.browserGestures);
|
||||
assert(EngineSemantics.instance.gestureMode == GestureMode.browserGestures);
|
||||
|
||||
final bool updateNeeded = _pendingResync ||
|
||||
semanticsObject.isValueDirty ||
|
||||
@@ -148,11 +148,9 @@ class Incrementable extends PrimaryRoleManager {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
assert(_gestureModeListener != null);
|
||||
super.dispose();
|
||||
_focusManager.stopManaging();
|
||||
semanticsObject.owner.removeGestureModeListener(_gestureModeListener);
|
||||
_gestureModeListener = null;
|
||||
EngineSemantics.instance.removeGestureModeListener(_gestureModeListener);
|
||||
_disableBrowserGestureHandling();
|
||||
_element.remove();
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ class Scrollable extends PrimaryRoleManager {
|
||||
/// Responds to browser-detected "scroll" gestures.
|
||||
void _recomputeScrollPosition() {
|
||||
if (_domScrollPosition != _effectiveNeutralScrollPosition) {
|
||||
if (!semanticsObject.owner.shouldAcceptBrowserGesture('scroll')) {
|
||||
if (!EngineSemantics.instance.shouldAcceptBrowserGesture('scroll')) {
|
||||
return;
|
||||
}
|
||||
final bool doScrollForward =
|
||||
@@ -121,7 +121,7 @@ class Scrollable extends PrimaryRoleManager {
|
||||
_gestureModeListener = (_) {
|
||||
_gestureModeDidChange();
|
||||
};
|
||||
semanticsObject.owner.addGestureModeListener(_gestureModeListener);
|
||||
EngineSemantics.instance.addGestureModeListener(_gestureModeListener!);
|
||||
|
||||
_scrollListener = createDomEventListener((_) {
|
||||
_recomputeScrollPosition();
|
||||
@@ -196,7 +196,7 @@ class Scrollable extends PrimaryRoleManager {
|
||||
}
|
||||
|
||||
void _gestureModeDidChange() {
|
||||
switch (semanticsObject.owner.gestureMode) {
|
||||
switch (EngineSemantics.instance.gestureMode) {
|
||||
case GestureMode.browserGestures:
|
||||
// overflow:scroll will cause the browser report "scroll" events when
|
||||
// the accessibility focus shifts outside the visible bounds.
|
||||
@@ -232,8 +232,11 @@ class Scrollable extends PrimaryRoleManager {
|
||||
style.removeProperty('touch-action');
|
||||
if (_scrollListener != null) {
|
||||
removeEventListener('scroll', _scrollListener);
|
||||
_scrollListener = null;
|
||||
}
|
||||
if (_gestureModeListener != null) {
|
||||
EngineSemantics.instance.removeGestureModeListener(_gestureModeListener!);
|
||||
_gestureModeListener = null;
|
||||
}
|
||||
semanticsObject.owner.removeGestureModeListener(_gestureModeListener);
|
||||
_gestureModeListener = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import '../dom.dart';
|
||||
import '../platform_dispatcher.dart';
|
||||
import '../util.dart';
|
||||
import '../vector_math.dart';
|
||||
import '../window.dart';
|
||||
import 'checkable.dart';
|
||||
import 'dialog.dart';
|
||||
import 'focusable.dart';
|
||||
@@ -1737,6 +1738,17 @@ class SemanticsObject {
|
||||
}());
|
||||
return result;
|
||||
}
|
||||
|
||||
bool _isDisposed = false;
|
||||
|
||||
void dispose() {
|
||||
assert(!_isDisposed);
|
||||
_isDisposed = true;
|
||||
element.remove();
|
||||
_parent = null;
|
||||
primaryRole?.dispose();
|
||||
primaryRole = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Controls how pointer events and browser-detected gestures are treated by
|
||||
@@ -1792,20 +1804,19 @@ enum SemanticsUpdatePhase {
|
||||
postUpdate,
|
||||
}
|
||||
|
||||
/// The top-level service that manages everything semantics-related.
|
||||
class EngineSemanticsOwner {
|
||||
EngineSemanticsOwner._() {
|
||||
registerHotRestartListener(() {
|
||||
_rootSemanticsElement?.remove();
|
||||
});
|
||||
}
|
||||
/// The semantics system of the Web Engine.
|
||||
///
|
||||
/// Maintains global properties and behaviors of semantics in the engine, such
|
||||
/// as whether semantics is currently enabled or disabled.
|
||||
class EngineSemantics {
|
||||
EngineSemantics._();
|
||||
|
||||
/// The singleton instance that manages semantics.
|
||||
static EngineSemanticsOwner get instance {
|
||||
return _instance ??= EngineSemanticsOwner._();
|
||||
static EngineSemantics get instance {
|
||||
return _instance ??= EngineSemantics._();
|
||||
}
|
||||
|
||||
static EngineSemanticsOwner? _instance;
|
||||
static EngineSemantics? _instance;
|
||||
|
||||
/// Disables semantics and uninitializes the singleton [instance].
|
||||
///
|
||||
@@ -1820,125 +1831,6 @@ class EngineSemanticsOwner {
|
||||
_instance = null;
|
||||
}
|
||||
|
||||
/// The current update phase of this semantics owner.
|
||||
SemanticsUpdatePhase get phase => _phase;
|
||||
SemanticsUpdatePhase _phase = SemanticsUpdatePhase.idle;
|
||||
|
||||
final Map<int, SemanticsObject> _semanticsTree = <int, SemanticsObject>{};
|
||||
|
||||
/// Map [SemanticsObject.id] to parent [SemanticsObject] it was attached to
|
||||
/// this frame.
|
||||
Map<int, SemanticsObject> _attachments = <int, SemanticsObject>{};
|
||||
|
||||
/// Declares that the [child] must be attached to the [parent].
|
||||
///
|
||||
/// Attachments take precedence over detachments (see [_detachObject]). This
|
||||
/// allows the same node to be detached from one parent in the tree and
|
||||
/// reattached to another parent.
|
||||
void _attachObject({required SemanticsObject parent, required SemanticsObject child}) {
|
||||
child._parent = parent;
|
||||
_attachments[child.id] = parent;
|
||||
}
|
||||
|
||||
/// List of objects that were detached this frame.
|
||||
///
|
||||
/// The objects in this list will be detached permanently unless they are
|
||||
/// reattached via the [_attachObject] method.
|
||||
List<SemanticsObject> _detachments = <SemanticsObject>[];
|
||||
|
||||
/// Declares that the [SemanticsObject] with the given [id] was detached from
|
||||
/// its current parent object.
|
||||
///
|
||||
/// The object will be detached permanently unless it is reattached via the
|
||||
/// [_attachObject] method.
|
||||
void _detachObject(int id) {
|
||||
final SemanticsObject? object = _semanticsTree[id];
|
||||
assert(object != null);
|
||||
if (object != null) {
|
||||
_detachments.add(object);
|
||||
}
|
||||
}
|
||||
|
||||
/// Callbacks called after all objects in the tree have their properties
|
||||
/// populated and their sizes and locations computed.
|
||||
///
|
||||
/// This list is reset to empty after all callbacks are called.
|
||||
List<ui.VoidCallback> _oneTimePostUpdateCallbacks = <ui.VoidCallback>[];
|
||||
|
||||
/// Schedules a one-time callback to be called after all objects in the tree
|
||||
/// have their properties populated and their sizes and locations computed.
|
||||
void addOneTimePostUpdateCallback(ui.VoidCallback callback) {
|
||||
_oneTimePostUpdateCallbacks.add(callback);
|
||||
}
|
||||
|
||||
/// Reconciles [_attachments] and [_detachments], and after that calls all
|
||||
/// the one-time callbacks scheduled via the [addOneTimePostUpdateCallback]
|
||||
/// method.
|
||||
void _finalizeTree() {
|
||||
for (final SemanticsObject detachmentRoot in _detachments) {
|
||||
// A detached node may or may not have some of its descendants reattached
|
||||
// elsewhere. Walk the descendant tree and find all descendants that were
|
||||
// reattached to a parent. Those descendants need to be removed.
|
||||
final List<SemanticsObject> removals = <SemanticsObject>[];
|
||||
detachmentRoot.visitDepthFirst((SemanticsObject node) {
|
||||
final SemanticsObject? parent = _attachments[node.id];
|
||||
if (parent == null) {
|
||||
// Was not reparented and is removed permanently from the tree.
|
||||
removals.add(node);
|
||||
} else {
|
||||
assert(node._parent == parent);
|
||||
assert(node.element.parentNode == parent._childContainerElement);
|
||||
}
|
||||
});
|
||||
|
||||
for (final SemanticsObject removal in removals) {
|
||||
_semanticsTree.remove(removal.id);
|
||||
removal._parent = null;
|
||||
removal.element.remove();
|
||||
}
|
||||
}
|
||||
_detachments = <SemanticsObject>[];
|
||||
_attachments = <int, SemanticsObject>{};
|
||||
|
||||
_phase = SemanticsUpdatePhase.postUpdate;
|
||||
try {
|
||||
if (_oneTimePostUpdateCallbacks.isNotEmpty) {
|
||||
for (final ui.VoidCallback callback in _oneTimePostUpdateCallbacks) {
|
||||
callback();
|
||||
}
|
||||
_oneTimePostUpdateCallbacks = <ui.VoidCallback>[];
|
||||
}
|
||||
} finally {
|
||||
_phase = SemanticsUpdatePhase.idle;
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the entire semantics tree for testing.
|
||||
///
|
||||
/// Works only in debug mode.
|
||||
Map<int, SemanticsObject>? get debugSemanticsTree {
|
||||
Map<int, SemanticsObject>? result;
|
||||
assert(() {
|
||||
result = _semanticsTree;
|
||||
return true;
|
||||
}());
|
||||
return result;
|
||||
}
|
||||
|
||||
/// The top-level DOM element of the semantics DOM element tree.
|
||||
DomElement? _rootSemanticsElement;
|
||||
TimestampFunction _now = () => DateTime.now();
|
||||
|
||||
void debugOverrideTimestampFunction(TimestampFunction value) {
|
||||
_now = value;
|
||||
}
|
||||
|
||||
void debugResetTimestampFunction() {
|
||||
_now = () => DateTime.now();
|
||||
}
|
||||
|
||||
final SemanticsHelper semanticsHelper = SemanticsHelper();
|
||||
|
||||
/// Whether the user has requested that [updateSemantics] be called when the
|
||||
/// semantic contents of window changes.
|
||||
///
|
||||
@@ -1972,19 +1864,54 @@ class EngineSemanticsOwner {
|
||||
_gestureMode = GestureMode.pointerEvents;
|
||||
_notifyGestureModeListeners();
|
||||
}
|
||||
final List<int> keys = _semanticsTree.keys.toList();
|
||||
final int len = keys.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
_detachObject(keys[i]);
|
||||
for (final EngineFlutterView view in EnginePlatformDispatcher.instance.views) {
|
||||
view.semantics.reset();
|
||||
}
|
||||
_finalizeTree();
|
||||
_rootSemanticsElement?.remove();
|
||||
_rootSemanticsElement = null;
|
||||
_gestureModeClock?.datetime = null;
|
||||
}
|
||||
EnginePlatformDispatcher.instance.updateSemanticsEnabled(_semanticsEnabled);
|
||||
}
|
||||
|
||||
/// Prepares the semantics system for a semantic tree update.
|
||||
///
|
||||
/// This method must be called prior to updating the semantics inside any
|
||||
/// individual view.
|
||||
///
|
||||
/// Automatically enables semantics in a production setting. In Flutter test
|
||||
/// environment keeps engine semantics turned off due to tests frequently
|
||||
/// sending inconsistent semantics updates.
|
||||
///
|
||||
/// The caller is expected to check if [semanticsEnabled] is true prior to
|
||||
/// actually updating the semantic DOM.
|
||||
void didReceiveSemanticsUpdate() {
|
||||
if (!_semanticsEnabled) {
|
||||
if (ui_web.debugEmulateFlutterTesterEnvironment) {
|
||||
// Running Flutter widget tests in a fake environment. Don't enable
|
||||
// engine semantics. Test semantics trees violate invariants in ways
|
||||
// production implementation isn't built to handle. For example, tests
|
||||
// routinely reset semantics node IDs, which is messing up the update
|
||||
// process.
|
||||
return;
|
||||
} else {
|
||||
// Running a real app. Auto-enable engine semantics.
|
||||
semanticsHelper.dispose(); // placeholder no longer needed
|
||||
semanticsEnabled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TimestampFunction _now = () => DateTime.now();
|
||||
|
||||
void debugOverrideTimestampFunction(TimestampFunction value) {
|
||||
_now = value;
|
||||
}
|
||||
|
||||
void debugResetTimestampFunction() {
|
||||
_now = () => DateTime.now();
|
||||
}
|
||||
|
||||
final SemanticsHelper semanticsHelper = SemanticsHelper();
|
||||
|
||||
/// Controls how pointer events and browser-detected gestures are treated by
|
||||
/// the Web Engine.
|
||||
///
|
||||
@@ -2095,14 +2022,14 @@ class EngineSemanticsOwner {
|
||||
/// Callbacks are called synchronously. HTML DOM updates made in a callback
|
||||
/// take effect in the current animation frame and/or the current message loop
|
||||
/// event.
|
||||
final List<GestureModeCallback?> _gestureModeListeners = <GestureModeCallback?>[];
|
||||
final List<GestureModeCallback> _gestureModeListeners = <GestureModeCallback>[];
|
||||
|
||||
/// Calls the [callback] every time the current [GestureMode] changes.
|
||||
///
|
||||
/// The callback is called synchronously. HTML DOM updates made in the
|
||||
/// callback take effect in the current animation frame and/or the current
|
||||
/// message loop event.
|
||||
void addGestureModeListener(GestureModeCallback? callback) {
|
||||
void addGestureModeListener(GestureModeCallback callback) {
|
||||
_gestureModeListeners.add(callback);
|
||||
}
|
||||
|
||||
@@ -2110,14 +2037,14 @@ class EngineSemanticsOwner {
|
||||
///
|
||||
/// The passed [callback] must be the exact same object as the one passed to
|
||||
/// [addGestureModeListener].
|
||||
void removeGestureModeListener(GestureModeCallback? callback) {
|
||||
void removeGestureModeListener(GestureModeCallback callback) {
|
||||
assert(_gestureModeListeners.contains(callback));
|
||||
_gestureModeListeners.remove(callback);
|
||||
}
|
||||
|
||||
void _notifyGestureModeListeners() {
|
||||
for (int i = 0; i < _gestureModeListeners.length; i++) {
|
||||
_gestureModeListeners[i]!(_gestureMode);
|
||||
_gestureModeListeners[i](_gestureMode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2149,6 +2076,141 @@ class EngineSemanticsOwner {
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// The top-level service that manages everything semantics-related.
|
||||
class EngineSemanticsOwner {
|
||||
EngineSemanticsOwner(this.semanticsHost) {
|
||||
registerHotRestartListener(() {
|
||||
_rootSemanticsElement?.remove();
|
||||
});
|
||||
}
|
||||
|
||||
/// The permanent element in the view's DOM structure that hosts the semantics
|
||||
/// tree.
|
||||
///
|
||||
/// The only child of this element is the [rootSemanticsElement]. Unlike the
|
||||
/// root element, this element is never replaced. It is always part of the
|
||||
/// DOM structure of the respective [FlutterView].
|
||||
// TODO(yjbanov): rename to hostElement
|
||||
final DomElement semanticsHost;
|
||||
|
||||
/// The DOM element corresponding to the root semantics node in the semantics
|
||||
/// tree.
|
||||
///
|
||||
/// This element is the direct child of the [semanticsHost] and it is
|
||||
/// replaceable.
|
||||
// TODO(yjbanov): rename to rootElement
|
||||
DomElement? get rootSemanticsElement => _rootSemanticsElement;
|
||||
DomElement? _rootSemanticsElement;
|
||||
|
||||
/// The current update phase of this semantics owner.
|
||||
SemanticsUpdatePhase get phase => _phase;
|
||||
SemanticsUpdatePhase _phase = SemanticsUpdatePhase.idle;
|
||||
|
||||
final Map<int, SemanticsObject> _semanticsTree = <int, SemanticsObject>{};
|
||||
|
||||
/// Map [SemanticsObject.id] to parent [SemanticsObject] it was attached to
|
||||
/// this frame.
|
||||
Map<int, SemanticsObject> _attachments = <int, SemanticsObject>{};
|
||||
|
||||
/// Declares that the [child] must be attached to the [parent].
|
||||
///
|
||||
/// Attachments take precedence over detachments (see [_detachObject]). This
|
||||
/// allows the same node to be detached from one parent in the tree and
|
||||
/// reattached to another parent.
|
||||
void _attachObject({required SemanticsObject parent, required SemanticsObject child}) {
|
||||
child._parent = parent;
|
||||
_attachments[child.id] = parent;
|
||||
}
|
||||
|
||||
/// List of objects that were detached this frame.
|
||||
///
|
||||
/// The objects in this list will be detached permanently unless they are
|
||||
/// reattached via the [_attachObject] method.
|
||||
List<SemanticsObject> _detachments = <SemanticsObject>[];
|
||||
|
||||
/// Declares that the [SemanticsObject] with the given [id] was detached from
|
||||
/// its current parent object.
|
||||
///
|
||||
/// The object will be detached permanently unless it is reattached via the
|
||||
/// [_attachObject] method.
|
||||
void _detachObject(int id) {
|
||||
final SemanticsObject? object = _semanticsTree[id];
|
||||
assert(object != null);
|
||||
if (object != null) {
|
||||
_detachments.add(object);
|
||||
}
|
||||
}
|
||||
|
||||
/// Callbacks called after all objects in the tree have their properties
|
||||
/// populated and their sizes and locations computed.
|
||||
///
|
||||
/// This list is reset to empty after all callbacks are called.
|
||||
List<ui.VoidCallback> _oneTimePostUpdateCallbacks = <ui.VoidCallback>[];
|
||||
|
||||
/// Schedules a one-time callback to be called after all objects in the tree
|
||||
/// have their properties populated and their sizes and locations computed.
|
||||
void addOneTimePostUpdateCallback(ui.VoidCallback callback) {
|
||||
_oneTimePostUpdateCallbacks.add(callback);
|
||||
}
|
||||
|
||||
/// Reconciles [_attachments] and [_detachments], and after that calls all
|
||||
/// the one-time callbacks scheduled via the [addOneTimePostUpdateCallback]
|
||||
/// method.
|
||||
void _finalizeTree() {
|
||||
// Collect all nodes that need to be permanently removed, i.e. nodes that
|
||||
// were detached from their parent, but not reattached to another parent.
|
||||
final Set<SemanticsObject> removals = <SemanticsObject>{};
|
||||
for (final SemanticsObject detachmentRoot in _detachments) {
|
||||
// A detached node may or may not have some of its descendants reattached
|
||||
// elsewhere. Walk the descendant tree and find all descendants that were
|
||||
// reattached to a parent. Those descendants need to be removed.
|
||||
detachmentRoot.visitDepthFirst((SemanticsObject node) {
|
||||
final SemanticsObject? parent = _attachments[node.id];
|
||||
if (parent == null) {
|
||||
// Was not reparented and is removed permanently from the tree.
|
||||
removals.add(node);
|
||||
} else {
|
||||
assert(node._parent == parent);
|
||||
assert(node.element.parentNode == parent._childContainerElement);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
for (final SemanticsObject removal in removals) {
|
||||
_semanticsTree.remove(removal.id);
|
||||
removal.dispose();
|
||||
}
|
||||
|
||||
_detachments = <SemanticsObject>[];
|
||||
_attachments = <int, SemanticsObject>{};
|
||||
|
||||
_phase = SemanticsUpdatePhase.postUpdate;
|
||||
try {
|
||||
if (_oneTimePostUpdateCallbacks.isNotEmpty) {
|
||||
for (final ui.VoidCallback callback in _oneTimePostUpdateCallbacks) {
|
||||
callback();
|
||||
}
|
||||
_oneTimePostUpdateCallbacks = <ui.VoidCallback>[];
|
||||
}
|
||||
} finally {
|
||||
_phase = SemanticsUpdatePhase.idle;
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the entire semantics tree for testing.
|
||||
///
|
||||
/// Works only in debug mode.
|
||||
Map<int, SemanticsObject>? get debugSemanticsTree {
|
||||
Map<int, SemanticsObject>? result;
|
||||
assert(() {
|
||||
result = _semanticsTree;
|
||||
return true;
|
||||
}());
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Looks up a [SemanticsObject] in the semantics tree by ID, or creates a new
|
||||
/// instance if it does not exist.
|
||||
@@ -2161,23 +2223,45 @@ class EngineSemanticsOwner {
|
||||
return object;
|
||||
}
|
||||
|
||||
// Checks the consistency of the semantics node tree against the {ID: node}
|
||||
// map. The two must be in total agreement. Every node in the map must be
|
||||
// somewhere in the tree.
|
||||
(bool, String) _computeNodeMapConsistencyMessage() {
|
||||
final Map<int, List<int>> liveIds = <int, List<int>>{};
|
||||
|
||||
final SemanticsObject? root = _semanticsTree[0];
|
||||
if (root != null) {
|
||||
root.visitDepthFirst((SemanticsObject child) {
|
||||
liveIds[child.id] = child._childrenInTraversalOrder?.toList() ?? const <int>[];
|
||||
});
|
||||
}
|
||||
|
||||
final bool isConsistent = _semanticsTree.keys.every(liveIds.keys.contains);
|
||||
final String heading = 'The semantics node map is ${isConsistent ? 'consistent' : 'inconsistent'}';
|
||||
final StringBuffer message = StringBuffer('$heading:\n');
|
||||
message.writeln(' Nodes in tree:');
|
||||
for (final MapEntry<int, List<int>> entry in liveIds.entries) {
|
||||
message.writeln(' ${entry.key}: ${entry.value}');
|
||||
}
|
||||
message.writeln(' Nodes in map: [${_semanticsTree.keys.join(', ')}]');
|
||||
|
||||
return (isConsistent, message.toString());
|
||||
}
|
||||
|
||||
/// Updates the semantics tree from data in the [uiUpdate].
|
||||
void updateSemantics(ui.SemanticsUpdate uiUpdate) {
|
||||
if (!_semanticsEnabled) {
|
||||
if (ui_web.debugEmulateFlutterTesterEnvironment) {
|
||||
// Running Flutter widget tests in a fake environment. Don't enable
|
||||
// engine semantics. Test semantics trees violate invariants in ways
|
||||
// production implementation isn't built to handle. For example, tests
|
||||
// routinely reset semantics node IDs, which is messing up the update
|
||||
// process.
|
||||
return;
|
||||
} else {
|
||||
// Running a real app. Auto-enable engine semantics.
|
||||
semanticsHelper.dispose(); // placeholder no longer needed
|
||||
semanticsEnabled = true;
|
||||
}
|
||||
EngineSemantics.instance.didReceiveSemanticsUpdate();
|
||||
|
||||
if (!EngineSemantics.instance.semanticsEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
(bool, String)? preUpdateNodeMapConsistency;
|
||||
assert(() {
|
||||
preUpdateNodeMapConsistency = _computeNodeMapConsistencyMessage();
|
||||
return true;
|
||||
}());
|
||||
|
||||
_phase = SemanticsUpdatePhase.updating;
|
||||
final SemanticsUpdate update = uiUpdate as SemanticsUpdate;
|
||||
|
||||
@@ -2198,33 +2282,31 @@ class EngineSemanticsOwner {
|
||||
object._dirtyFields = 0;
|
||||
}
|
||||
|
||||
final SemanticsObject root = _semanticsTree[0]!;
|
||||
if (_rootSemanticsElement == null) {
|
||||
final SemanticsObject root = _semanticsTree[0]!;
|
||||
_rootSemanticsElement = root.element;
|
||||
// TODO(mdebbar): There could be multiple views with multiple semantics hosts.
|
||||
// https://github.com/flutter/flutter/issues/137344
|
||||
final DomElement semanticsHost = EnginePlatformDispatcher.instance.implicitView!.dom.semanticsHost;
|
||||
semanticsHost.append(root.element);
|
||||
}
|
||||
|
||||
_finalizeTree();
|
||||
|
||||
assert(_semanticsTree.containsKey(0)); // must contain root node
|
||||
assert(() {
|
||||
// Validate that the node map only contains live elements, i.e. descendants
|
||||
// of the root node. If a node is not reachable from the root, it should
|
||||
// have been removed from the map.
|
||||
final List<int> liveIds = <int>[];
|
||||
final SemanticsObject root = _semanticsTree[0]!;
|
||||
root.visitDepthFirst((SemanticsObject child) {
|
||||
liveIds.add(child.id);
|
||||
});
|
||||
assert(
|
||||
_semanticsTree.keys.every(liveIds.contains),
|
||||
'The semantics node map is inconsistent:\n'
|
||||
' Nodes in tree: [${liveIds.join(', ')}]\n'
|
||||
' Nodes in map : [${_semanticsTree.keys.join(', ')}]'
|
||||
);
|
||||
final (bool isConsistent, String description) = _computeNodeMapConsistencyMessage();
|
||||
if (!isConsistent) {
|
||||
// Use StateError because AssertionError escapes line breaks, but this
|
||||
// error message is very detailed and it needs line breaks for
|
||||
// legibility.
|
||||
throw StateError('''
|
||||
Semantics node map was inconsistent after update:
|
||||
|
||||
BEFORE: ${preUpdateNodeMapConsistency?.$2}
|
||||
|
||||
AFTER: $description
|
||||
''');
|
||||
}
|
||||
|
||||
// Validate that each node in the final tree is self-consistent.
|
||||
_semanticsTree.forEach((int? id, SemanticsObject object) {
|
||||
@@ -2270,6 +2352,29 @@ class EngineSemanticsOwner {
|
||||
return true;
|
||||
}());
|
||||
}
|
||||
|
||||
/// Removes the semantics tree for this view from the page and collects all
|
||||
/// resources.
|
||||
///
|
||||
/// The object remains usable after this operation, but because the previous
|
||||
/// semantics tree is completely removed, partial udpates will not succeed as
|
||||
/// they rely on the prior state of the tree. There is no distinction between
|
||||
/// a full update and partial update, so the failure may be cryptic.
|
||||
void reset() {
|
||||
final List<int> keys = _semanticsTree.keys.toList();
|
||||
final int len = keys.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
_detachObject(keys[i]);
|
||||
}
|
||||
_finalizeTree();
|
||||
_rootSemanticsElement?.remove();
|
||||
_rootSemanticsElement = null;
|
||||
_semanticsTree.clear();
|
||||
_attachments.clear();
|
||||
_detachments.clear();
|
||||
_phase = SemanticsUpdatePhase.idle;
|
||||
_oneTimePostUpdateCallbacks.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// Computes the [longest increasing subsequence](http://en.wikipedia.org/wiki/Longest_increasing_subsequence).
|
||||
|
||||
@@ -138,7 +138,7 @@ class DesktopSemanticsEnabler extends SemanticsEnabler {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (EngineSemanticsOwner.instance.semanticsEnabled) {
|
||||
if (EngineSemantics.instance.semanticsEnabled) {
|
||||
// Semantics already enabled, forward to framework as normal.
|
||||
return true;
|
||||
}
|
||||
@@ -167,7 +167,7 @@ class DesktopSemanticsEnabler extends SemanticsEnabler {
|
||||
return true;
|
||||
}
|
||||
|
||||
EngineSemanticsOwner.instance.semanticsEnabled = true;
|
||||
EngineSemantics.instance.semanticsEnabled = true;
|
||||
dispose();
|
||||
return false;
|
||||
}
|
||||
@@ -267,7 +267,7 @@ class MobileSemanticsEnabler extends SemanticsEnabler {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (EngineSemanticsOwner.instance.semanticsEnabled) {
|
||||
if (EngineSemantics.instance.semanticsEnabled) {
|
||||
// Semantics already enabled, forward to framework as normal.
|
||||
return true;
|
||||
}
|
||||
@@ -357,7 +357,7 @@ class MobileSemanticsEnabler extends SemanticsEnabler {
|
||||
_schedulePlaceholderRemoval = true;
|
||||
semanticsActivationTimer = Timer(_periodToConsumeEvents, () {
|
||||
dispose();
|
||||
EngineSemanticsOwner.instance.semanticsEnabled = true;
|
||||
EngineSemantics.instance.semanticsEnabled = true;
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -296,7 +296,7 @@ class TextField extends PrimaryRoleManager {
|
||||
_initializeEditableElement();
|
||||
activeEditableElement.addEventListener('focus',
|
||||
createDomEventListener((DomEvent event) {
|
||||
if (semanticsObject.owner.gestureMode != GestureMode.browserGestures) {
|
||||
if (EngineSemantics.instance.gestureMode != GestureMode.browserGestures) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -305,7 +305,7 @@ class TextField extends PrimaryRoleManager {
|
||||
}));
|
||||
activeEditableElement.addEventListener('blur',
|
||||
createDomEventListener((DomEvent event) {
|
||||
if (semanticsObject.owner.gestureMode != GestureMode.browserGestures) {
|
||||
if (EngineSemantics.instance.gestureMode != GestureMode.browserGestures) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -2308,7 +2308,7 @@ class HybridTextEditing {
|
||||
/// Supplies the DOM element used for editing.
|
||||
late final DefaultTextEditingStrategy strategy =
|
||||
debugTextEditingStrategyOverride ??
|
||||
(EngineSemanticsOwner.instance.semanticsEnabled
|
||||
(EngineSemantics.instance.semanticsEnabled
|
||||
? SemanticsTextEditingStrategy.ensureInitialized(this)
|
||||
: createDefaultTextEditingStrategy(this));
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ import 'style_manager.dart';
|
||||
/// +- <style>
|
||||
///
|
||||
class DomManager {
|
||||
factory DomManager({required double devicePixelRatio}) {
|
||||
factory DomManager({required int viewId, required double devicePixelRatio}) {
|
||||
final DomElement rootElement = domDocument.createElement(DomManager.flutterViewTagName);
|
||||
final DomElement platformViewsHost = domDocument.createElement(DomManager.glassPaneTagName);
|
||||
final DomShadowRoot renderingHost = _attachShadowRoot(platformViewsHost);
|
||||
@@ -52,10 +52,20 @@ class DomManager {
|
||||
final DomElement semanticsHost = domDocument.createElement(DomManager.semanticsHostTagName);
|
||||
final DomElement announcementsHost = createDomElement(DomManager.announcementsHostTagName);
|
||||
|
||||
// Root element children.
|
||||
// This `flt-view-id` attribute does not serve a function in the engine's
|
||||
// operation, but it's useful for debugging, test automation, and DOM
|
||||
// interop use-cases. It allows one to use CSS selectors to find views by
|
||||
// their identifiers.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// document.querySelector('flutter-view[flt-view-id="$viewId"]')
|
||||
rootElement.setAttribute('flt-view-id', viewId);
|
||||
|
||||
// Root element children.
|
||||
rootElement.appendChild(platformViewsHost);
|
||||
rootElement.appendChild(textEditingHost);
|
||||
|
||||
// The semantic host goes last because hit-test order-wise it must be
|
||||
// first. If semantics goes under the scene host, platform views will
|
||||
// obscure semantic elements.
|
||||
@@ -70,10 +80,7 @@ class DomManager {
|
||||
|
||||
// Rendering host (shadow root) children.
|
||||
|
||||
// TODO(yjbanov): In a multi-view world, we may want to have a separate
|
||||
// semantics owner for each view.
|
||||
// https://github.com/flutter/flutter/issues/137445
|
||||
final DomElement accessibilityPlaceholder = EngineSemanticsOwner
|
||||
final DomElement accessibilityPlaceholder = EngineSemantics
|
||||
.instance.semanticsHelper
|
||||
.prepareAccessibilityPlaceholder();
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import 'mouse/cursor.dart';
|
||||
import 'navigation/history.dart';
|
||||
import 'platform_dispatcher.dart';
|
||||
import 'platform_views/message_handler.dart';
|
||||
import 'semantics/accessibility.dart';
|
||||
import 'semantics.dart';
|
||||
import 'services.dart';
|
||||
import 'util.dart';
|
||||
import 'view_embedder/dom_manager.dart';
|
||||
@@ -93,6 +93,7 @@ base class EngineFlutterView implements ui.FlutterView {
|
||||
dom.rootElement.remove();
|
||||
// TODO(harryterkelsen): What should we do about this in multi-view?
|
||||
renderer.clearFragmentProgramCache();
|
||||
semantics.reset();
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -105,7 +106,7 @@ base class EngineFlutterView implements ui.FlutterView {
|
||||
@override
|
||||
void updateSemantics(ui.SemanticsUpdate update) {
|
||||
assert(!isDisposed, 'Trying to update semantics on a disposed EngineFlutterView.');
|
||||
platformDispatcher.updateSemantics(update);
|
||||
semantics.updateSemantics(update);
|
||||
}
|
||||
|
||||
// TODO(yjbanov): How should this look like for multi-view?
|
||||
@@ -117,7 +118,7 @@ base class EngineFlutterView implements ui.FlutterView {
|
||||
|
||||
late final ContextMenu contextMenu = ContextMenu(dom.rootElement);
|
||||
|
||||
late final DomManager dom = DomManager(devicePixelRatio: devicePixelRatio);
|
||||
late final DomManager dom = DomManager(viewId: viewId, devicePixelRatio: devicePixelRatio);
|
||||
|
||||
late final PlatformViewMessageHandler platformViewMessageHandler =
|
||||
PlatformViewMessageHandler(platformViewsContainer: dom.platformViewsHost);
|
||||
@@ -126,6 +127,8 @@ base class EngineFlutterView implements ui.FlutterView {
|
||||
@override
|
||||
ViewConstraints get physicalConstraints => ViewConstraints.tight(physicalSize);
|
||||
|
||||
late final EngineSemanticsOwner semantics = EngineSemanticsOwner(dom.semanticsHost);
|
||||
|
||||
@override
|
||||
ui.Size get physicalSize {
|
||||
if (_physicalSize == null) {
|
||||
|
||||
@@ -2671,11 +2671,11 @@ void _testClickDebouncer() {
|
||||
test(
|
||||
description,
|
||||
() async {
|
||||
EngineSemanticsOwner.instance
|
||||
EngineSemantics.instance
|
||||
..debugOverrideTimestampFunction(() => testTime)
|
||||
..semanticsEnabled = true;
|
||||
await body();
|
||||
EngineSemanticsOwner.instance.semanticsEnabled = false;
|
||||
EngineSemantics.instance.semanticsEnabled = false;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
// 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:async';
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'package:ui/src/engine.dart';
|
||||
|
||||
import '../../common/test_initialization.dart';
|
||||
import 'semantics_tester.dart';
|
||||
|
||||
EngineSemantics semantics() => EngineSemantics.instance;
|
||||
EngineSemanticsOwner owner() => EnginePlatformDispatcher.instance.implicitView!.semantics;
|
||||
|
||||
void main() {
|
||||
internalBootstrapBrowserTest(() {
|
||||
return testMain;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> testMain() async {
|
||||
await bootstrapAndRunApp();
|
||||
|
||||
test('EngineSemanticsOwner auto-enables semantics on update', () async {
|
||||
expect(semantics().semanticsEnabled, isFalse);
|
||||
expect(
|
||||
EnginePlatformDispatcher
|
||||
.instance.accessibilityFeatures.accessibleNavigation,
|
||||
isFalse);
|
||||
|
||||
final DomShadowRoot renderingHost =
|
||||
EnginePlatformDispatcher.instance.implicitView!.dom.renderingHost;
|
||||
|
||||
final DomElement placeholder =
|
||||
renderingHost.querySelector('flt-semantics-placeholder')!;
|
||||
|
||||
expect(placeholder.isConnected, isTrue);
|
||||
|
||||
// Sending a semantics update should auto-enable engine semantics.
|
||||
final SemanticsTester tester = SemanticsTester(owner());
|
||||
tester.updateNode(id: 0);
|
||||
tester.apply();
|
||||
|
||||
expect(semantics().semanticsEnabled, isTrue);
|
||||
expect(
|
||||
EnginePlatformDispatcher.instance.accessibilityFeatures.accessibleNavigation,
|
||||
isTrue,
|
||||
);
|
||||
|
||||
// The placeholder should be removed
|
||||
expect(placeholder.isConnected, isFalse);
|
||||
});
|
||||
}
|
||||
@@ -18,7 +18,7 @@ void testMain() {
|
||||
late DomElement? placeholder;
|
||||
|
||||
setUp(() {
|
||||
EngineSemanticsOwner.instance.semanticsEnabled = false;
|
||||
EngineSemantics.instance.semanticsEnabled = false;
|
||||
desktopSemanticsEnabler = DesktopSemanticsEnabler();
|
||||
placeholder = desktopSemanticsEnabler.prepareAccessibilityPlaceholder();
|
||||
domDocument.body!.append(placeholder!);
|
||||
@@ -28,7 +28,7 @@ void testMain() {
|
||||
expect(placeholder, isNotNull,
|
||||
reason: 'Expected the test to create a placeholder');
|
||||
placeholder!.remove();
|
||||
EngineSemanticsOwner.instance.semanticsEnabled = false;
|
||||
EngineSemantics.instance.semanticsEnabled = false;
|
||||
});
|
||||
|
||||
test('prepare accessibility placeholder', () async {
|
||||
@@ -95,7 +95,7 @@ void testMain() {
|
||||
DomElement? placeholder;
|
||||
|
||||
setUp(() {
|
||||
EngineSemanticsOwner.instance.semanticsEnabled = false;
|
||||
EngineSemantics.instance.semanticsEnabled = false;
|
||||
mobileSemanticsEnabler = MobileSemanticsEnabler();
|
||||
placeholder = mobileSemanticsEnabler.prepareAccessibilityPlaceholder();
|
||||
domDocument.body!.append(placeholder!);
|
||||
@@ -103,7 +103,7 @@ void testMain() {
|
||||
|
||||
tearDown(() {
|
||||
placeholder!.remove();
|
||||
EngineSemanticsOwner.instance.semanticsEnabled = false;
|
||||
EngineSemantics.instance.semanticsEnabled = false;
|
||||
});
|
||||
|
||||
test('prepare accessibility placeholder', () async {
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
// 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:async';
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'package:ui/src/engine.dart';
|
||||
import 'package:ui/ui.dart' as ui;
|
||||
|
||||
import '../../common/test_initialization.dart';
|
||||
import 'semantics_tester.dart';
|
||||
|
||||
void main() {
|
||||
internalBootstrapBrowserTest(() {
|
||||
return testMain;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> testMain() async {
|
||||
// TODO(yjbanov): see https://github.com/flutter/flutter/issues/138906
|
||||
// This test tests multi-view mode only, but currently there's
|
||||
// no way to cleanly boot into pure multi-view mode. As a
|
||||
// workaroud we boot into a mode that includes the implicit
|
||||
// view, but we then remove the implicit view.
|
||||
await bootstrapAndRunApp();
|
||||
final EngineFlutterWindow implicitView = EnginePlatformDispatcher.instance.implicitView!;
|
||||
EnginePlatformDispatcher.instance.viewManager.disposeAndUnregisterView(implicitView.viewId);
|
||||
|
||||
test('Can create multiple views each with its own semantics tree', () async {
|
||||
EngineSemantics.instance.semanticsEnabled = true;
|
||||
|
||||
final DomElement host1 = createDomElement('view-host');
|
||||
domDocument.body!.append(host1);
|
||||
final EngineFlutterView view1 = EngineFlutterView(EnginePlatformDispatcher.instance, host1);
|
||||
EnginePlatformDispatcher.instance.viewManager.registerView(view1);
|
||||
final SemanticsTester tester1 = SemanticsTester(view1.semantics);
|
||||
|
||||
final DomElement host2 = createDomElement('view-host');
|
||||
domDocument.body!.append(host2);
|
||||
final EngineFlutterView view2 = EngineFlutterView(EnginePlatformDispatcher.instance, host2);
|
||||
EnginePlatformDispatcher.instance.viewManager.registerView(view2);
|
||||
final SemanticsTester tester2 = SemanticsTester(view2.semantics);
|
||||
|
||||
tester1.updateNode(id: 0);
|
||||
tester1.apply();
|
||||
|
||||
tester2.updateNode(id: 0);
|
||||
tester2.apply();
|
||||
|
||||
// Check that we have both root nodes in the DOM (root nodes have id == 0)
|
||||
expect(domDocument.querySelectorAll('flutter-view'), hasLength(2));
|
||||
expect(domDocument.querySelectorAll('flutter-view[flt-view-id="${view1.viewId}"]'), hasLength(1));
|
||||
expect(domDocument.querySelectorAll('flutter-view[flt-view-id="${view2.viewId}"]'), hasLength(1));
|
||||
expect(domDocument.querySelectorAll('flt-semantics[id=flt-semantic-node-0]'), hasLength(2));
|
||||
|
||||
// Check that each is attached to its own view
|
||||
expect(view1.semantics.semanticsHost, view1.dom.semanticsHost);
|
||||
expect(view2.semantics.semanticsHost, view2.dom.semanticsHost);
|
||||
|
||||
// Check semantics
|
||||
expectSemanticsTree(view1.semantics, '<sem style="filter: opacity(0%); color: rgba(0, 0, 0, 0)"></sem>');
|
||||
expectSemanticsTree(view2.semantics, '<sem style="filter: opacity(0%); color: rgba(0, 0, 0, 0)"></sem>');
|
||||
|
||||
// Add some children
|
||||
tester1.updateNode(
|
||||
id: 0,
|
||||
children: <SemanticsNodeUpdate>[
|
||||
tester1.updateNode(
|
||||
id: 1,
|
||||
isFocusable: true,
|
||||
hasTap: true,
|
||||
hasEnabledState: true,
|
||||
isEnabled: true,
|
||||
isButton: true,
|
||||
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
|
||||
)
|
||||
],
|
||||
);
|
||||
tester1.apply();
|
||||
|
||||
tester2.updateNode(
|
||||
id: 0,
|
||||
children: <SemanticsNodeUpdate>[
|
||||
tester2.updateNode(
|
||||
id: 2,
|
||||
hasIncrease: true,
|
||||
label: 'd',
|
||||
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
|
||||
),
|
||||
],
|
||||
);
|
||||
tester2.apply();
|
||||
|
||||
// Test that each view renders its own semantics tree.
|
||||
expectSemanticsTree(
|
||||
view1.semantics,
|
||||
'''
|
||||
<sem style="filter: opacity(0%); color: rgba(0, 0, 0, 0)">
|
||||
<sem-c>
|
||||
<sem flt-tappable="" role="button"></sem>
|
||||
</sem-c>
|
||||
</sem>''',
|
||||
);
|
||||
expectSemanticsTree(
|
||||
view2.semantics,
|
||||
'''
|
||||
<sem style="filter: opacity(0%); color: rgba(0, 0, 0, 0)">
|
||||
<sem-c>
|
||||
<sem aria-label="d"><input aria-valuemax="1" aria-valuemin="1" aria-valuenow="1" aria-valuetext="" role="slider"></sem>
|
||||
</sem-c>
|
||||
</sem>
|
||||
''');
|
||||
|
||||
// Remove the first view, but keep the second one.
|
||||
EnginePlatformDispatcher.instance.viewManager.disposeAndUnregisterView(view1.viewId);
|
||||
|
||||
expect(domDocument.querySelectorAll('flutter-view'), hasLength(1));
|
||||
expect(domDocument.querySelectorAll('flt-semantics[id=flt-semantic-node-0]'), hasLength(1));
|
||||
expect(domDocument.querySelectorAll('flt-semantics[id=flt-semantic-node-2]'), hasLength(1));
|
||||
|
||||
// Disable semantics; make sure the view is there but semantics is removed.
|
||||
EngineSemantics.instance.semanticsEnabled = false;
|
||||
expect(domDocument.querySelectorAll('flutter-view'), hasLength(1));
|
||||
expect(domDocument.querySelectorAll('flt-semantics[id=flt-semantic-node-0]'), isEmpty);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// 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:async';
|
||||
|
||||
import 'package:test/bootstrap/browser.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'package:ui/src/engine.dart';
|
||||
|
||||
import '../../common/test_initialization.dart';
|
||||
|
||||
EngineSemantics semantics() => EngineSemantics.instance;
|
||||
EngineSemanticsOwner owner() => EnginePlatformDispatcher.instance.implicitView!.semantics;
|
||||
|
||||
void main() {
|
||||
internalBootstrapBrowserTest(() {
|
||||
return testMain;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> testMain() async {
|
||||
await bootstrapAndRunApp();
|
||||
|
||||
test('EngineSemantics is enabled via a placeholder click', () async {
|
||||
expect(semantics().semanticsEnabled, isFalse);
|
||||
|
||||
// Synthesize a click on the placeholder.
|
||||
final DomShadowRoot renderingHost =
|
||||
EnginePlatformDispatcher.instance.implicitView!.dom.renderingHost;
|
||||
final DomElement placeholder =
|
||||
renderingHost.querySelector('flt-semantics-placeholder')!;
|
||||
|
||||
expect(placeholder.isConnected, isTrue);
|
||||
|
||||
final DomRect rect = placeholder.getBoundingClientRect();
|
||||
placeholder.dispatchEvent(createDomMouseEvent('click', <Object?, Object?>{
|
||||
'clientX': (rect.left + (rect.right - rect.left) / 2).floor(),
|
||||
'clientY': (rect.top + (rect.bottom - rect.top) / 2).floor(),
|
||||
}));
|
||||
|
||||
// On mobile semantics is enabled asynchronously.
|
||||
if (isMobile) {
|
||||
while (placeholder.isConnected!) {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 50));
|
||||
}
|
||||
}
|
||||
expect(semantics().semanticsEnabled, isTrue);
|
||||
expect(placeholder.isConnected, isFalse);
|
||||
});
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -7,24 +7,12 @@ import 'dart:typed_data';
|
||||
|
||||
import 'package:test/test.dart';
|
||||
import 'package:ui/src/engine/dom.dart';
|
||||
import 'package:ui/src/engine/platform_dispatcher.dart';
|
||||
import 'package:ui/src/engine/semantics.dart';
|
||||
import 'package:ui/src/engine/util.dart';
|
||||
import 'package:ui/src/engine/vector_math.dart';
|
||||
import 'package:ui/ui.dart' as ui;
|
||||
|
||||
import '../../common/matchers.dart';
|
||||
|
||||
/// Gets the DOM host where the Flutter app is being rendered.
|
||||
///
|
||||
/// This function returns the correct host for the flutter app under testing,
|
||||
/// so we don't have to hardcode domDocument across the test. The semantics
|
||||
/// tree has moved outside of the shadowDOM as a workaround for a password
|
||||
/// autofill bug on Chrome.
|
||||
/// Ref: https://github.com/flutter/flutter/issues/87735
|
||||
DomElement get rootElement =>
|
||||
EnginePlatformDispatcher.instance.implicitView!.dom.rootElement;
|
||||
|
||||
/// CSS style applied to the root of the semantics tree.
|
||||
// TODO(yjbanov): this should be handled internally by [expectSemanticsTree].
|
||||
// No need for every test to inject it.
|
||||
@@ -349,20 +337,24 @@ class SemanticsTester {
|
||||
TextField getTextField(int id) {
|
||||
return getSemanticsObject(id).primaryRole! as TextField;
|
||||
}
|
||||
|
||||
void expectSemantics(String semanticsHtml) {
|
||||
expectSemanticsTree(owner, semanticsHtml);
|
||||
}
|
||||
}
|
||||
|
||||
/// Verifies the HTML structure of the current semantics tree.
|
||||
void expectSemanticsTree(String semanticsHtml) {
|
||||
void expectSemanticsTree(EngineSemanticsOwner owner, String semanticsHtml) {
|
||||
const List<String> ignoredStyleProperties = <String>['pointer-events'];
|
||||
expect(
|
||||
canonicalizeHtml(rootElement.querySelector('flt-semantics')!.outerHTML!, ignoredStyleProperties: ignoredStyleProperties),
|
||||
canonicalizeHtml(owner.semanticsHost.querySelector('flt-semantics')!.outerHTML!, ignoredStyleProperties: ignoredStyleProperties),
|
||||
canonicalizeHtml(semanticsHtml),
|
||||
);
|
||||
}
|
||||
|
||||
/// Finds the first HTML element in the semantics tree used for scrolling.
|
||||
DomElement? findScrollable() {
|
||||
return rootElement.querySelectorAll('flt-semantics').firstWhereOrNull(
|
||||
DomElement findScrollable(EngineSemanticsOwner owner) {
|
||||
return owner.semanticsHost.querySelectorAll('flt-semantics').singleWhere(
|
||||
(DomElement? element) {
|
||||
return element!.style.overflow == 'hidden' ||
|
||||
element.style.overflowY == 'scroll' ||
|
||||
|
||||
@@ -21,7 +21,8 @@ final InputConfiguration multilineConfig = InputConfiguration(
|
||||
inputAction: 'TextInputAction.newline',
|
||||
);
|
||||
|
||||
EngineSemanticsOwner semantics() => EngineSemanticsOwner.instance;
|
||||
EngineSemantics semantics() => EngineSemantics.instance;
|
||||
EngineSemanticsOwner owner() => EnginePlatformDispatcher.instance.implicitView!.semantics;
|
||||
|
||||
const MethodCodec codec = JSONMethodCodec();
|
||||
|
||||
@@ -35,7 +36,7 @@ void testMain() {
|
||||
ensureFlutterViewEmbedderInitialized();
|
||||
|
||||
setUp(() {
|
||||
EngineSemanticsOwner.debugResetSemantics();
|
||||
EngineSemantics.debugResetSemantics();
|
||||
});
|
||||
|
||||
group('$SemanticsTextEditingStrategy pre-initialization tests', () {
|
||||
@@ -86,7 +87,7 @@ void testMain() {
|
||||
test('renders a text field', () {
|
||||
createTextFieldSemantics(value: 'hello');
|
||||
|
||||
expectSemanticsTree('''
|
||||
expectSemanticsTree(owner(), '''
|
||||
<sem style="$rootSemanticStyle">
|
||||
<input value="hello" />
|
||||
</sem>''');
|
||||
@@ -98,20 +99,20 @@ void testMain() {
|
||||
final SemanticsActionLogger logger = SemanticsActionLogger();
|
||||
createTextFieldSemantics(value: 'hello');
|
||||
|
||||
final DomElement textField = rootElement
|
||||
final DomElement textField = owner().semanticsHost
|
||||
.querySelector('input[data-semantics-role="text-field"]')!;
|
||||
|
||||
expect(rootElement.ownerDocument?.activeElement, isNot(textField));
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, isNot(textField));
|
||||
|
||||
textField.focus();
|
||||
|
||||
expect(rootElement.ownerDocument?.activeElement, textField);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, textField);
|
||||
expect(await logger.idLog.first, 0);
|
||||
expect(await logger.actionLog.first, ui.SemanticsAction.didGainAccessibilityFocus);
|
||||
|
||||
textField.blur();
|
||||
|
||||
expect(rootElement.ownerDocument?.activeElement, isNot(textField));
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, isNot(textField));
|
||||
expect(await logger.idLog.first, 0);
|
||||
expect(await logger.actionLog.first, ui.SemanticsAction.didLoseAccessibilityFocus);
|
||||
}, // TODO(yjbanov): https://github.com/flutter/flutter/issues/46638
|
||||
@@ -119,7 +120,7 @@ void testMain() {
|
||||
skip: browserEngine != BrowserEngine.blink);
|
||||
|
||||
test('Syncs semantic state from framework', () {
|
||||
expect(rootElement.ownerDocument?.activeElement, domDocument.body);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, domDocument.body);
|
||||
|
||||
int changeCount = 0;
|
||||
int actionCount = 0;
|
||||
@@ -142,7 +143,7 @@ void testMain() {
|
||||
);
|
||||
|
||||
final TextField textField = textFieldSemantics.primaryRole! as TextField;
|
||||
expect(rootElement.ownerDocument?.activeElement, strategy.domElement);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, strategy.domElement);
|
||||
expect(textField.editableElement, strategy.domElement);
|
||||
expect(textField.activeEditableElement.getAttribute('aria-label'), 'greeting');
|
||||
expect(textField.activeEditableElement.style.width, '10px');
|
||||
@@ -155,7 +156,7 @@ void testMain() {
|
||||
rect: const ui.Rect.fromLTWH(0, 0, 12, 17),
|
||||
);
|
||||
|
||||
expect(rootElement.ownerDocument?.activeElement, domDocument.body);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, domDocument.body);
|
||||
expect(strategy.domElement, null);
|
||||
expect(textField.activeEditableElement.getAttribute('aria-label'), 'farewell');
|
||||
expect(textField.activeEditableElement.style.width, '12px');
|
||||
@@ -200,7 +201,7 @@ void testMain() {
|
||||
test(
|
||||
'Updates editing state when receiving framework messages from the text input channel',
|
||||
() {
|
||||
expect(rootElement.ownerDocument?.activeElement, domDocument.body);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, domDocument.body);
|
||||
|
||||
strategy.enable(
|
||||
singlelineConfig,
|
||||
@@ -243,7 +244,7 @@ void testMain() {
|
||||
});
|
||||
|
||||
test('Gives up focus after DOM blur', () {
|
||||
expect(rootElement.ownerDocument?.activeElement, domDocument.body);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, domDocument.body);
|
||||
|
||||
strategy.enable(
|
||||
singlelineConfig,
|
||||
@@ -257,11 +258,11 @@ void testMain() {
|
||||
|
||||
final TextField textField = textFieldSemantics.primaryRole! as TextField;
|
||||
expect(textField.editableElement, strategy.domElement);
|
||||
expect(rootElement.ownerDocument?.activeElement, strategy.domElement);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, strategy.domElement);
|
||||
|
||||
// The input should not refocus after blur.
|
||||
textField.activeEditableElement.blur();
|
||||
expect(rootElement.ownerDocument?.activeElement, domDocument.body);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, domDocument.body);
|
||||
strategy.disable();
|
||||
});
|
||||
|
||||
@@ -281,17 +282,17 @@ void testMain() {
|
||||
isFocused: true,
|
||||
);
|
||||
expect(strategy.domElement, isNotNull);
|
||||
expect(rootElement.ownerDocument?.activeElement, strategy.domElement);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, strategy.domElement);
|
||||
|
||||
strategy.disable();
|
||||
expect(strategy.domElement, isNull);
|
||||
|
||||
// It doesn't remove the DOM element.
|
||||
final TextField textField = textFieldSemantics.primaryRole! as TextField;
|
||||
expect(rootElement.contains(textField.editableElement), isTrue);
|
||||
expect(owner().semanticsHost.contains(textField.editableElement), isTrue);
|
||||
// Editing element is not enabled.
|
||||
expect(strategy.isEnabled, isFalse);
|
||||
expect(rootElement.ownerDocument?.activeElement, domDocument.body);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, domDocument.body);
|
||||
});
|
||||
|
||||
test('Refocuses when setting editing state', () {
|
||||
@@ -306,11 +307,11 @@ void testMain() {
|
||||
isFocused: true,
|
||||
);
|
||||
expect(strategy.domElement, isNotNull);
|
||||
expect(rootElement.ownerDocument?.activeElement, strategy.domElement);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, strategy.domElement);
|
||||
|
||||
// Blur the element without telling the framework.
|
||||
strategy.activeDomElement.blur();
|
||||
expect(rootElement.ownerDocument?.activeElement, domDocument.body);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, domDocument.body);
|
||||
|
||||
// The input will have focus after editing state is set and semantics updated.
|
||||
strategy.setEditingState(EditingState(text: 'foo'));
|
||||
@@ -328,7 +329,7 @@ void testMain() {
|
||||
value: 'hello',
|
||||
isFocused: true,
|
||||
);
|
||||
expect(rootElement.ownerDocument?.activeElement, strategy.domElement);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, strategy.domElement);
|
||||
|
||||
strategy.disable();
|
||||
});
|
||||
@@ -348,7 +349,7 @@ void testMain() {
|
||||
final DomHTMLTextAreaElement textArea =
|
||||
strategy.domElement! as DomHTMLTextAreaElement;
|
||||
|
||||
expect(rootElement.ownerDocument?.activeElement, strategy.domElement);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, strategy.domElement);
|
||||
|
||||
strategy.enable(
|
||||
singlelineConfig,
|
||||
@@ -357,11 +358,11 @@ void testMain() {
|
||||
);
|
||||
|
||||
textArea.blur();
|
||||
expect(rootElement.ownerDocument?.activeElement, domDocument.body);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, domDocument.body);
|
||||
|
||||
strategy.disable();
|
||||
// It doesn't remove the textarea from the DOM.
|
||||
expect(rootElement.contains(textArea), isTrue);
|
||||
expect(owner().semanticsHost.contains(textArea), isTrue);
|
||||
// Editing element is not enabled.
|
||||
expect(strategy.isEnabled, isFalse);
|
||||
});
|
||||
@@ -437,17 +438,17 @@ void testMain() {
|
||||
|
||||
// Switch between the two fields a few times.
|
||||
for (int i = 0; i < 5; i++) {
|
||||
final SemanticsTester tester = SemanticsTester(semantics());
|
||||
final SemanticsTester tester = SemanticsTester(owner());
|
||||
createTwoFieldSemantics(tester, focusFieldId: 1);
|
||||
expect(tester.apply().length, 3);
|
||||
|
||||
expect(rootElement.ownerDocument?.activeElement,
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement,
|
||||
tester.getTextField(1).editableElement);
|
||||
expect(strategy.domElement, tester.getTextField(1).editableElement);
|
||||
|
||||
createTwoFieldSemantics(tester, focusFieldId: 2);
|
||||
expect(tester.apply().length, 3);
|
||||
expect(rootElement.ownerDocument?.activeElement,
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement,
|
||||
tester.getTextField(2).editableElement);
|
||||
expect(strategy.domElement, tester.getTextField(2).editableElement);
|
||||
}
|
||||
@@ -478,16 +479,16 @@ void testMain() {
|
||||
});
|
||||
|
||||
test('does not render a text field', () {
|
||||
expect(rootElement.querySelector('flt-semantics[role="textbox"]'), isNull);
|
||||
expect(owner().semanticsHost.querySelector('flt-semantics[role="textbox"]'), isNull);
|
||||
createTextFieldSemanticsForIos(value: 'hello');
|
||||
expect(rootElement.querySelector('flt-semantics[role="textbox"]'), isNotNull);
|
||||
expect(owner().semanticsHost.querySelector('flt-semantics[role="textbox"]'), isNotNull);
|
||||
});
|
||||
|
||||
test('tap detection works', () async {
|
||||
final SemanticsActionLogger logger = SemanticsActionLogger();
|
||||
createTextFieldSemanticsForIos(value: 'hello');
|
||||
|
||||
final DomElement textField = rootElement
|
||||
final DomElement textField = owner().semanticsHost
|
||||
.querySelector('flt-semantics[role="textbox"]')!;
|
||||
|
||||
simulateTap(textField);
|
||||
@@ -496,7 +497,7 @@ void testMain() {
|
||||
});
|
||||
|
||||
test('Syncs semantic state from framework', () {
|
||||
expect(rootElement.ownerDocument?.activeElement, domDocument.body);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, domDocument.body);
|
||||
|
||||
int changeCount = 0;
|
||||
int actionCount = 0;
|
||||
@@ -519,7 +520,7 @@ void testMain() {
|
||||
);
|
||||
|
||||
final TextField textField = textFieldSemantics.primaryRole! as TextField;
|
||||
expect(rootElement.ownerDocument?.activeElement, strategy.domElement);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, strategy.domElement);
|
||||
expect(textField.editableElement, strategy.domElement);
|
||||
expect(textField.activeEditableElement.getAttribute('aria-label'), 'greeting');
|
||||
expect(textField.activeEditableElement.style.width, '10px');
|
||||
@@ -532,10 +533,10 @@ void testMain() {
|
||||
rect: const ui.Rect.fromLTWH(0, 0, 12, 17),
|
||||
);
|
||||
final DomElement textBox =
|
||||
rootElement.querySelector('flt-semantics[role="textbox"]')!;
|
||||
owner().semanticsHost.querySelector('flt-semantics[role="textbox"]')!;
|
||||
|
||||
expect(strategy.domElement, null);
|
||||
expect(rootElement.ownerDocument?.activeElement, textBox);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, textBox);
|
||||
expect(textBox.getAttribute('aria-label'), 'farewell');
|
||||
|
||||
strategy.disable();
|
||||
@@ -577,7 +578,7 @@ void testMain() {
|
||||
test(
|
||||
'Updates editing state when receiving framework messages from the text input channel',
|
||||
() {
|
||||
expect(rootElement.ownerDocument?.activeElement, domDocument.body);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, domDocument.body);
|
||||
|
||||
strategy.enable(
|
||||
singlelineConfig,
|
||||
@@ -620,7 +621,7 @@ void testMain() {
|
||||
});
|
||||
|
||||
test('Gives up focus after DOM blur', () {
|
||||
expect(rootElement.ownerDocument?.activeElement, domDocument.body);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, domDocument.body);
|
||||
|
||||
strategy.enable(
|
||||
singlelineConfig,
|
||||
@@ -634,13 +635,13 @@ void testMain() {
|
||||
|
||||
final TextField textField = textFieldSemantics.primaryRole! as TextField;
|
||||
expect(textField.editableElement, strategy.domElement);
|
||||
expect(rootElement.ownerDocument?.activeElement, strategy.domElement);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, strategy.domElement);
|
||||
|
||||
// The input should not refocus after blur.
|
||||
textField.activeEditableElement.blur();
|
||||
final DomElement textBox =
|
||||
rootElement.querySelector('flt-semantics[role="textbox"]')!;
|
||||
expect(rootElement.ownerDocument?.activeElement, textBox);
|
||||
owner().semanticsHost.querySelector('flt-semantics[role="textbox"]')!;
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, textBox);
|
||||
|
||||
strategy.disable();
|
||||
});
|
||||
@@ -661,20 +662,20 @@ void testMain() {
|
||||
isFocused: true,
|
||||
);
|
||||
expect(strategy.domElement, isNotNull);
|
||||
expect(rootElement.ownerDocument?.activeElement, strategy.domElement);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, strategy.domElement);
|
||||
|
||||
strategy.disable();
|
||||
expect(strategy.domElement, isNull);
|
||||
|
||||
// It removes the DOM element.
|
||||
final TextField textField = textFieldSemantics.primaryRole! as TextField;
|
||||
expect(rootElement.contains(textField.editableElement), isFalse);
|
||||
expect(owner().semanticsHost.contains(textField.editableElement), isFalse);
|
||||
// Editing element is not enabled.
|
||||
expect(strategy.isEnabled, isFalse);
|
||||
// Focus is on the semantic object
|
||||
final DomElement textBox =
|
||||
rootElement.querySelector('flt-semantics[role="textbox"]')!;
|
||||
expect(rootElement.ownerDocument?.activeElement, textBox);
|
||||
owner().semanticsHost.querySelector('flt-semantics[role="textbox"]')!;
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, textBox);
|
||||
});
|
||||
|
||||
test('Refocuses when setting editing state', () {
|
||||
@@ -689,13 +690,13 @@ void testMain() {
|
||||
isFocused: true,
|
||||
);
|
||||
expect(strategy.domElement, isNotNull);
|
||||
expect(rootElement.ownerDocument?.activeElement, strategy.domElement);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, strategy.domElement);
|
||||
|
||||
// Blur the element without telling the framework.
|
||||
strategy.activeDomElement.blur();
|
||||
final DomElement textBox =
|
||||
rootElement.querySelector('flt-semantics[role="textbox"]')!;
|
||||
expect(rootElement.ownerDocument?.activeElement, textBox);
|
||||
owner().semanticsHost.querySelector('flt-semantics[role="textbox"]')!;
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, textBox);
|
||||
|
||||
// The input will have focus after editing state is set and semantics updated.
|
||||
strategy.setEditingState(EditingState(text: 'foo'));
|
||||
@@ -713,7 +714,7 @@ void testMain() {
|
||||
value: 'hello',
|
||||
isFocused: true,
|
||||
);
|
||||
expect(rootElement.ownerDocument?.activeElement, strategy.domElement);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, strategy.domElement);
|
||||
|
||||
strategy.disable();
|
||||
});
|
||||
@@ -732,7 +733,7 @@ void testMain() {
|
||||
|
||||
final DomHTMLTextAreaElement textArea =
|
||||
strategy.domElement! as DomHTMLTextAreaElement;
|
||||
expect(rootElement.ownerDocument?.activeElement, strategy.domElement);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, strategy.domElement);
|
||||
|
||||
strategy.enable(
|
||||
singlelineConfig,
|
||||
@@ -740,17 +741,17 @@ void testMain() {
|
||||
onAction: (_) {},
|
||||
);
|
||||
|
||||
expect(rootElement.contains(textArea), isTrue);
|
||||
expect(owner().semanticsHost.contains(textArea), isTrue);
|
||||
|
||||
textArea.blur();
|
||||
final DomElement textBox =
|
||||
rootElement.querySelector('flt-semantics[role="textbox"]')!;
|
||||
owner().semanticsHost.querySelector('flt-semantics[role="textbox"]')!;
|
||||
|
||||
expect(rootElement.ownerDocument?.activeElement, textBox);
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement, textBox);
|
||||
|
||||
strategy.disable();
|
||||
// It removes the textarea from the DOM.
|
||||
expect(rootElement.contains(textArea), isFalse);
|
||||
expect(owner().semanticsHost.contains(textArea), isFalse);
|
||||
// Editing element is not enabled.
|
||||
expect(strategy.isEnabled, isFalse);
|
||||
});
|
||||
@@ -805,17 +806,17 @@ void testMain() {
|
||||
|
||||
// Switch between the two fields a few times.
|
||||
for (int i = 0; i < 1; i++) {
|
||||
final SemanticsTester tester = SemanticsTester(semantics());
|
||||
final SemanticsTester tester = SemanticsTester(owner());
|
||||
createTwoFieldSemanticsForIos(tester, focusFieldId: 1);
|
||||
|
||||
expect(tester.apply().length, 3);
|
||||
expect(rootElement.ownerDocument?.activeElement,
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement,
|
||||
tester.getTextField(1).editableElement);
|
||||
expect(strategy.domElement, tester.getTextField(1).editableElement);
|
||||
|
||||
createTwoFieldSemanticsForIos(tester, focusFieldId: 2);
|
||||
expect(tester.apply().length, 3);
|
||||
expect(rootElement.ownerDocument?.activeElement,
|
||||
expect(owner().semanticsHost.ownerDocument?.activeElement,
|
||||
tester.getTextField(2).editableElement);
|
||||
expect(strategy.domElement, tester.getTextField(2).editableElement);
|
||||
}
|
||||
@@ -874,7 +875,7 @@ SemanticsObject createTextFieldSemantics({
|
||||
int textSelectionBase = 0,
|
||||
int textSelectionExtent = 0,
|
||||
}) {
|
||||
final SemanticsTester tester = SemanticsTester(semantics());
|
||||
final SemanticsTester tester = SemanticsTester(owner());
|
||||
tester.updateNode(
|
||||
id: 0,
|
||||
label: label,
|
||||
@@ -976,7 +977,7 @@ Map<int, SemanticsObject> createTwoFieldSemanticsForIos(SemanticsTester builder,
|
||||
builder.apply();
|
||||
final String label = focusFieldId == 1 ? 'Hello' : 'World';
|
||||
final DomElement textBox =
|
||||
rootElement.querySelector('flt-semantics[aria-label="$label"]')!;
|
||||
owner().semanticsHost.querySelector('flt-semantics[aria-label="$label"]')!;
|
||||
|
||||
simulateTap(textBox);
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ void main() {
|
||||
void doTests() {
|
||||
group('DomManager', () {
|
||||
test('DOM tree looks right', () {
|
||||
final DomManager domManager = DomManager(devicePixelRatio: 3.0);
|
||||
final DomManager domManager = DomManager(viewId: 0, devicePixelRatio: 3.0);
|
||||
|
||||
// Check tag names.
|
||||
|
||||
@@ -47,7 +47,7 @@ void doTests() {
|
||||
});
|
||||
|
||||
test('hide placeholder text for textfield', () {
|
||||
final DomManager domManager = DomManager(devicePixelRatio: 3.0);
|
||||
final DomManager domManager = DomManager(viewId: 0, devicePixelRatio: 3.0);
|
||||
domDocument.body!.append(domManager.rootElement);
|
||||
|
||||
final DomHTMLInputElement regularTextField = createDomHTMLInputElement();
|
||||
@@ -87,12 +87,12 @@ void doTests() {
|
||||
|
||||
attachShadow = null; // Break ShadowDOM
|
||||
|
||||
expect(() => DomManager(devicePixelRatio: 3.0), throwsAssertionError);
|
||||
expect(() => DomManager(viewId: 0, devicePixelRatio: 3.0), throwsAssertionError);
|
||||
attachShadow = oldAttachShadow; // Restore ShadowDOM
|
||||
});
|
||||
|
||||
test('Initializes and attaches a shadow root', () {
|
||||
final DomManager domManager = DomManager(devicePixelRatio: 3.0);
|
||||
final DomManager domManager = DomManager(viewId: 0, devicePixelRatio: 3.0);
|
||||
|
||||
expect(domInstanceOfString(domManager.renderingHost, 'ShadowRoot'), isTrue);
|
||||
expect(domManager.renderingHost.host, domManager.platformViewsHost);
|
||||
@@ -109,7 +109,7 @@ void doTests() {
|
||||
});
|
||||
|
||||
test('Attaches a stylesheet to the shadow root', () {
|
||||
final DomManager domManager = DomManager(devicePixelRatio: 3.0);
|
||||
final DomManager domManager = DomManager(viewId: 0, devicePixelRatio: 3.0);
|
||||
final DomElement? style =
|
||||
domManager.renderingHost.querySelector('#flt-internals-stylesheet');
|
||||
|
||||
@@ -119,7 +119,7 @@ void doTests() {
|
||||
});
|
||||
|
||||
test('setScene', () {
|
||||
final DomManager domManager = DomManager(devicePixelRatio: 3.0);
|
||||
final DomManager domManager = DomManager(viewId: 0, devicePixelRatio: 3.0);
|
||||
|
||||
final DomElement sceneHost =
|
||||
domManager.renderingHost.querySelector('flt-scene-host')!;
|
||||
|
||||
@@ -28,7 +28,7 @@ Future<void> testMain() async {
|
||||
|
||||
void runCanvasTests({required bool deviceClipRoundsOut}) {
|
||||
setUp(() {
|
||||
EngineSemanticsOwner.debugResetSemantics();
|
||||
EngineSemantics.debugResetSemantics();
|
||||
});
|
||||
|
||||
group('ui.Canvas transform tests', () {
|
||||
|
||||
Reference in New Issue
Block a user