diff --git a/engine/src/flutter/shell/platform/common/accessibility_bridge.cc b/engine/src/flutter/shell/platform/common/accessibility_bridge.cc index 3a050a5a41..3ec39b9f5e 100644 --- a/engine/src/flutter/shell/platform/common/accessibility_bridge.cc +++ b/engine/src/flutter/shell/platform/common/accessibility_bridge.cc @@ -43,7 +43,30 @@ void AccessibilityBridge::AddFlutterSemanticsCustomActionUpdate( } void AccessibilityBridge::CommitUpdates() { + // AXTree cannot move a node in a single update. + // This must be split across two updates: + // + // * Update 1: remove nodes from their old parents. + // * Update 2: re-add nodes (including their children) to their new parents. + // + // First, start by removing nodes if necessary. + std::optional remove_reparented = + CreateRemoveReparentedNodesUpdate(); + if (remove_reparented.has_value()) { + tree_.Unserialize(remove_reparented.value()); + + std::string error = tree_.error(); + if (!error.empty()) { + FML_LOG(ERROR) << "Failed to update ui::AXTree, error: " << error; + assert(false); + return; + } + } + + // Second, apply the pending node updates. This also moves reparented nodes to + // their new parents if needed. ui::AXTreeUpdate update{.tree_data = tree_.data()}; + // Figure out update order, ui::AXTree only accepts update in tree order, // where parent node must come before the child node in // ui::AXTreeUpdate.nodes. We start with picking a random node and turn the @@ -73,7 +96,7 @@ void AccessibilityBridge::CommitUpdates() { std::string error = tree_.error(); if (!error.empty()) { - BASE_LOG() << "Failed to update ui::AXTree, error: " << error; + FML_LOG(ERROR) << "Failed to update ui::AXTree, error: " << error; return; } // Handles accessibility events as the result of the semantics update. @@ -177,6 +200,62 @@ void AccessibilityBridge::OnAtomicUpdateFinished( } } +std::optional +AccessibilityBridge::CreateRemoveReparentedNodesUpdate() { + std::unordered_map updates; + + for (auto node_update : pending_semantics_node_updates_) { + for (int32_t child_id : node_update.second.children_in_traversal_order) { + // Skip nodes that don't exist or have a parent in the current tree. + ui::AXNode* child = tree_.GetFromId(child_id); + if (!child) { + continue; + } + + // Flutter's root node should never be reparented. + assert(child->parent()); + + // Skip nodes whose parents are unchanged. + if (child->parent()->id() == node_update.second.id) { + continue; + } + + // This pending update moves the current child node. + // That new child must have a corresponding pending update. + assert(pending_semantics_node_updates_.find(child_id) != + pending_semantics_node_updates_.end()); + + // Create an update to remove the child from its previous parent. + int32_t parent_id = child->parent()->id(); + if (updates.find(parent_id) == updates.end()) { + updates[parent_id] = tree_.GetFromId(parent_id)->data(); + } + + ui::AXNodeData* parent = &updates[parent_id]; + auto iter = std::find(parent->child_ids.begin(), parent->child_ids.end(), + child_id); + + assert(iter != parent->child_ids.end()); + parent->child_ids.erase(iter); + } + } + + if (updates.empty()) { + return std::nullopt; + } + + ui::AXTreeUpdate update{ + .tree_data = tree_.data(), + .nodes = std::vector(), + }; + + for (std::pair data : updates) { + update.nodes.push_back(std::move(data.second)); + } + + return update; +} + // Private method. void AccessibilityBridge::GetSubTreeList(SemanticsNode target, std::vector& result) { diff --git a/engine/src/flutter/shell/platform/common/accessibility_bridge.h b/engine/src/flutter/shell/platform/common/accessibility_bridge.h index ad573beeb0..9c4cc9919b 100644 --- a/engine/src/flutter/shell/platform/common/accessibility_bridge.h +++ b/engine/src/flutter/shell/platform/common/accessibility_bridge.h @@ -222,6 +222,11 @@ class AccessibilityBridge std::unique_ptr delegate_; void InitAXTree(const ui::AXTreeUpdate& initial_state); + + // Create an update that removes any nodes that will be reparented by + // pending_semantics_updates_. Returns std::nullopt if none are reparented. + std::optional CreateRemoveReparentedNodesUpdate(); + void GetSubTreeList(SemanticsNode target, std::vector& result); void ConvertFlutterUpdate(const SemanticsNode& node, ui::AXTreeUpdate& tree_update); diff --git a/engine/src/flutter/shell/platform/common/accessibility_bridge_unittests.cc b/engine/src/flutter/shell/platform/common/accessibility_bridge_unittests.cc index 41834b107a..c320b12b33 100644 --- a/engine/src/flutter/shell/platform/common/accessibility_bridge_unittests.cc +++ b/engine/src/flutter/shell/platform/common/accessibility_bridge_unittests.cc @@ -14,45 +14,40 @@ namespace testing { using ::testing::Contains; +FlutterSemanticsNode CreateSemanticsNode( + int32_t id, + const char* label, + const std::vector* children = nullptr) { + return { + .id = id, + .flags = static_cast(0), + .actions = static_cast(0), + .text_selection_base = -1, + .text_selection_extent = -1, + .label = label, + .hint = "", + .value = "", + .increased_value = "", + .decreased_value = "", + .child_count = children ? children->size() : 0, + .children_in_traversal_order = children ? children->data() : nullptr, + .custom_accessibility_actions_count = 0, + }; +} + TEST(AccessibilityBridgeTest, basicTest) { std::shared_ptr bridge = std::make_shared( std::make_unique()); - FlutterSemanticsNode root; - root.id = 0; - root.label = "root"; - root.hint = ""; - root.value = ""; - root.increased_value = ""; - root.decreased_value = ""; - root.child_count = 2; - int32_t children[] = {1, 2}; - root.children_in_traversal_order = children; - root.custom_accessibility_actions_count = 0; + + std::vector children{1, 2}; + FlutterSemanticsNode root = CreateSemanticsNode(0, "root", &children); + FlutterSemanticsNode child1 = CreateSemanticsNode(1, "child 1"); + FlutterSemanticsNode child2 = CreateSemanticsNode(2, "child 2"); + bridge->AddFlutterSemanticsNodeUpdate(&root); - - FlutterSemanticsNode child1; - child1.id = 1; - child1.label = "child 1"; - child1.hint = ""; - child1.value = ""; - child1.increased_value = ""; - child1.decreased_value = ""; - child1.child_count = 0; - child1.custom_accessibility_actions_count = 0; bridge->AddFlutterSemanticsNodeUpdate(&child1); - - FlutterSemanticsNode child2; - child2.id = 2; - child2.label = "child 2"; - child2.hint = ""; - child2.value = ""; - child2.increased_value = ""; - child2.decreased_value = ""; - child2.child_count = 0; - child2.custom_accessibility_actions_count = 0; bridge->AddFlutterSemanticsNodeUpdate(&child2); - bridge->CommitUpdates(); auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock(); @@ -76,36 +71,12 @@ TEST(AccessibilityBridgeTest, canFireChildrenChangedCorrectly) { std::unique_ptr ptr(delegate); std::shared_ptr bridge = std::make_shared(std::move(ptr)); - FlutterSemanticsNode root; - root.id = 0; - root.flags = static_cast(0); - root.actions = static_cast(0); - root.text_selection_base = -1; - root.text_selection_extent = -1; - root.label = "root"; - root.hint = ""; - root.value = ""; - root.increased_value = ""; - root.decreased_value = ""; - root.child_count = 1; - int32_t children[] = {1}; - root.children_in_traversal_order = children; - root.custom_accessibility_actions_count = 0; - bridge->AddFlutterSemanticsNodeUpdate(&root); - FlutterSemanticsNode child1; - child1.id = 1; - child1.flags = static_cast(0); - child1.actions = static_cast(0); - child1.text_selection_base = -1; - child1.text_selection_extent = -1; - child1.label = "child 1"; - child1.hint = ""; - child1.value = ""; - child1.increased_value = ""; - child1.decreased_value = ""; - child1.child_count = 0; - child1.custom_accessibility_actions_count = 0; + std::vector children{1}; + FlutterSemanticsNode root = CreateSemanticsNode(0, "root", &children); + FlutterSemanticsNode child1 = CreateSemanticsNode(1, "child 1"); + + bridge->AddFlutterSemanticsNodeUpdate(&root); bridge->AddFlutterSemanticsNodeUpdate(&child1); bridge->CommitUpdates(); @@ -124,23 +95,11 @@ TEST(AccessibilityBridgeTest, canFireChildrenChangedCorrectly) { root.child_count = 2; int32_t new_children[] = {1, 2}; root.children_in_traversal_order = new_children; + + FlutterSemanticsNode child2 = CreateSemanticsNode(2, "child 2"); + bridge->AddFlutterSemanticsNodeUpdate(&root); - - FlutterSemanticsNode child2; - child2.id = 2; - child2.flags = static_cast(0); - child2.actions = static_cast(0); - child2.text_selection_base = -1; - child2.text_selection_extent = -1; - child2.label = "child 2"; - child2.hint = ""; - child2.value = ""; - child2.increased_value = ""; - child2.decreased_value = ""; - child2.child_count = 0; - child2.custom_accessibility_actions_count = 0; bridge->AddFlutterSemanticsNodeUpdate(&child2); - bridge->CommitUpdates(); root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock(); @@ -162,38 +121,13 @@ TEST(AccessibilityBridgeTest, canUpdateDelegate) { std::shared_ptr bridge = std::make_shared( std::make_unique()); - FlutterSemanticsNode root; - root.id = 0; - root.flags = static_cast(0); - root.actions = static_cast(0); - root.text_selection_base = -1; - root.text_selection_extent = -1; - root.label = "root"; - root.hint = ""; - root.value = ""; - root.increased_value = ""; - root.decreased_value = ""; - root.child_count = 1; - int32_t children[] = {1}; - root.children_in_traversal_order = children; - root.custom_accessibility_actions_count = 0; + + std::vector children{1}; + FlutterSemanticsNode root = CreateSemanticsNode(0, "root", &children); + FlutterSemanticsNode child1 = CreateSemanticsNode(1, "child 1"); + bridge->AddFlutterSemanticsNodeUpdate(&root); - - FlutterSemanticsNode child1; - child1.id = 1; - child1.flags = static_cast(0); - child1.actions = static_cast(0); - child1.text_selection_base = -1; - child1.text_selection_extent = -1; - child1.label = "child 1"; - child1.hint = ""; - child1.value = ""; - child1.increased_value = ""; - child1.decreased_value = ""; - child1.child_count = 0; - child1.custom_accessibility_actions_count = 0; bridge->AddFlutterSemanticsNodeUpdate(&child1); - bridge->CommitUpdates(); auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0); @@ -224,21 +158,9 @@ TEST(AccessibilityBridgeTest, canHandleSelectionChangeCorrectly) { std::unique_ptr ptr(delegate); std::shared_ptr bridge = std::make_shared(std::move(ptr)); - FlutterSemanticsNode root; - root.id = 0; + FlutterSemanticsNode root = CreateSemanticsNode(0, "root"); root.flags = FlutterSemanticsFlag::kFlutterSemanticsFlagIsTextField; - root.actions = static_cast(0); - root.text_selection_base = -1; - root.text_selection_extent = -1; - root.label = "root"; - root.hint = ""; - root.value = ""; - root.increased_value = ""; - root.decreased_value = ""; - root.child_count = 0; - root.custom_accessibility_actions_count = 0; bridge->AddFlutterSemanticsNodeUpdate(&root); - bridge->CommitUpdates(); const ui::AXTreeData& tree = bridge->GetAXTreeData(); @@ -267,23 +189,11 @@ TEST(AccessibilityBridgeTest, doesNotAssignEditableRootToSelectableText) { std::shared_ptr bridge = std::make_shared( std::make_unique()); - FlutterSemanticsNode root; - root.id = 0; + FlutterSemanticsNode root = CreateSemanticsNode(0, "root"); root.flags = static_cast( FlutterSemanticsFlag::kFlutterSemanticsFlagIsTextField | FlutterSemanticsFlag::kFlutterSemanticsFlagIsReadOnly); - root.actions = static_cast(0); - root.text_selection_base = -1; - root.text_selection_extent = -1; - root.label = "root"; - root.hint = ""; - root.value = ""; - root.increased_value = ""; - root.decreased_value = ""; - root.child_count = 0; - root.custom_accessibility_actions_count = 0; bridge->AddFlutterSemanticsNodeUpdate(&root); - bridge->CommitUpdates(); auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock(); @@ -296,17 +206,11 @@ TEST(AccessibilityBridgeTest, ToggleHasToggleButtonRole) { std::shared_ptr bridge = std::make_shared( std::make_unique()); - FlutterSemanticsNode root{.id = 0}; + FlutterSemanticsNode root = CreateSemanticsNode(0, "root"); root.flags = static_cast( FlutterSemanticsFlag::kFlutterSemanticsFlagHasToggledState | FlutterSemanticsFlag::kFlutterSemanticsFlagHasEnabledState | FlutterSemanticsFlag::kFlutterSemanticsFlagIsEnabled); - root.actions = static_cast(0); - root.text_selection_base = -1; - root.text_selection_extent = -1; - root.label = "root"; - root.child_count = 0; - root.custom_accessibility_actions_count = 0; bridge->AddFlutterSemanticsNodeUpdate(&root); bridge->CommitUpdates(); @@ -318,18 +222,12 @@ TEST(AccessibilityBridgeTest, SliderHasSliderRole) { std::shared_ptr bridge = std::make_shared( std::make_unique()); - FlutterSemanticsNode root{.id = 0}; + FlutterSemanticsNode root = CreateSemanticsNode(0, "root"); root.flags = static_cast( FlutterSemanticsFlag::kFlutterSemanticsFlagIsSlider | FlutterSemanticsFlag::kFlutterSemanticsFlagHasEnabledState | FlutterSemanticsFlag::kFlutterSemanticsFlagIsEnabled | FlutterSemanticsFlag::kFlutterSemanticsFlagIsFocusable); - root.actions = static_cast(0); - root.text_selection_base = -1; - root.text_selection_extent = -1; - root.label = "root"; - root.child_count = 0; - root.custom_accessibility_actions_count = 0; bridge->AddFlutterSemanticsNodeUpdate(&root); bridge->CommitUpdates(); @@ -346,20 +244,11 @@ TEST(AccessibilityBridgeTest, CanSetCheckboxChecked) { std::shared_ptr bridge = std::make_shared( std::make_unique()); - FlutterSemanticsNode root; - root.id = 0; - root.label = "root"; - root.hint = ""; - root.value = ""; - root.increased_value = ""; - root.decreased_value = ""; - root.child_count = 0; - root.custom_accessibility_actions_count = 0; + FlutterSemanticsNode root = CreateSemanticsNode(0, "root"); root.flags = static_cast( FlutterSemanticsFlag::kFlutterSemanticsFlagHasCheckedState | FlutterSemanticsFlag::kFlutterSemanticsFlagIsChecked); bridge->AddFlutterSemanticsNodeUpdate(&root); - bridge->CommitUpdates(); auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock(); @@ -368,5 +257,252 @@ TEST(AccessibilityBridgeTest, CanSetCheckboxChecked) { ax::mojom::CheckedState::kTrue); } +// Verify that a node can be moved from one parent to another. +TEST(AccessibilityBridgeTest, CanReparentNode) { + TestAccessibilityBridgeDelegate* delegate = + new TestAccessibilityBridgeDelegate(); + std::unique_ptr ptr(delegate); + std::shared_ptr bridge = + std::make_shared(std::move(ptr)); + + std::vector root_children{1}; + std::vector child1_children{2}; + FlutterSemanticsNode root = CreateSemanticsNode(0, "root", &root_children); + FlutterSemanticsNode child1 = + CreateSemanticsNode(1, "child 1", &child1_children); + FlutterSemanticsNode child2 = CreateSemanticsNode(2, "child 2"); + + bridge->AddFlutterSemanticsNodeUpdate(&root); + bridge->AddFlutterSemanticsNodeUpdate(&child1); + bridge->AddFlutterSemanticsNodeUpdate(&child2); + bridge->CommitUpdates(); + delegate->accessibility_events.clear(); + + // Reparent child2 from child1 to the root. + child1.child_count = 0; + child1.children_in_traversal_order = nullptr; + + int32_t new_root_children[] = {1, 2}; + root.child_count = 2; + root.children_in_traversal_order = new_root_children; + + bridge->AddFlutterSemanticsNodeUpdate(&root); + bridge->AddFlutterSemanticsNodeUpdate(&child1); + bridge->AddFlutterSemanticsNodeUpdate(&child2); + bridge->CommitUpdates(); + + auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(0).lock(); + auto child1_node = bridge->GetFlutterPlatformNodeDelegateFromID(1).lock(); + auto child2_node = bridge->GetFlutterPlatformNodeDelegateFromID(2).lock(); + + EXPECT_EQ(root_node->GetChildCount(), 2); + EXPECT_EQ(root_node->GetData().child_ids[0], 1); + EXPECT_EQ(root_node->GetData().child_ids[1], 2); + EXPECT_EQ(root_node->GetName(), "root"); + + EXPECT_EQ(child1_node->GetChildCount(), 0); + EXPECT_EQ(child1_node->GetName(), "child 1"); + + EXPECT_EQ(child2_node->GetChildCount(), 0); + EXPECT_EQ(child2_node->GetName(), "child 2"); + + ASSERT_EQ(delegate->accessibility_events.size(), size_t{5}); + + // Child2 is moved from child1 to root. + EXPECT_THAT(delegate->accessibility_events, + Contains(ui::AXEventGenerator::Event::CHILDREN_CHANGED).Times(2)); + EXPECT_THAT(delegate->accessibility_events, + Contains(ui::AXEventGenerator::Event::SUBTREE_CREATED).Times(1)); + + // Child1 is no longer a parent. It loses its group role and disables its + // 'clip children' attribute. + EXPECT_THAT( + delegate->accessibility_events, + Contains(ui::AXEventGenerator::Event::OTHER_ATTRIBUTE_CHANGED).Times(1)); + EXPECT_THAT(delegate->accessibility_events, + Contains(ui::AXEventGenerator::Event::ROLE_CHANGED).Times(1)); +} + +// Verify that multiple nodes can be moved to new parents. +TEST(AccessibilityBridgeTest, CanReparentMultipleNodes) { + TestAccessibilityBridgeDelegate* delegate = + new TestAccessibilityBridgeDelegate(); + std::unique_ptr ptr(delegate); + std::shared_ptr bridge = + std::make_shared(std::move(ptr)); + + int32_t root_id = 0; + int32_t intermediary1_id = 1; + int32_t intermediary2_id = 2; + int32_t leaf1_id = 3; + int32_t leaf2_id = 4; + int32_t leaf3_id = 5; + + std::vector root_children{intermediary1_id, intermediary2_id}; + std::vector intermediary1_children{leaf1_id}; + std::vector intermediary2_children{leaf2_id, leaf3_id}; + FlutterSemanticsNode root = + CreateSemanticsNode(root_id, "root", &root_children); + FlutterSemanticsNode intermediary1 = CreateSemanticsNode( + intermediary1_id, "intermediary 1", &intermediary1_children); + FlutterSemanticsNode intermediary2 = CreateSemanticsNode( + intermediary2_id, "intermediary 2", &intermediary2_children); + FlutterSemanticsNode leaf1 = CreateSemanticsNode(leaf1_id, "leaf 1"); + FlutterSemanticsNode leaf2 = CreateSemanticsNode(leaf2_id, "leaf 2"); + FlutterSemanticsNode leaf3 = CreateSemanticsNode(leaf3_id, "leaf 3"); + + bridge->AddFlutterSemanticsNodeUpdate(&root); + bridge->AddFlutterSemanticsNodeUpdate(&intermediary1); + bridge->AddFlutterSemanticsNodeUpdate(&intermediary2); + bridge->AddFlutterSemanticsNodeUpdate(&leaf1); + bridge->AddFlutterSemanticsNodeUpdate(&leaf2); + bridge->AddFlutterSemanticsNodeUpdate(&leaf3); + bridge->CommitUpdates(); + delegate->accessibility_events.clear(); + + // Swap intermediary 1's and intermediary2's children. + int32_t new_intermediary1_children[] = {leaf2_id, leaf3_id}; + intermediary1.child_count = 2; + intermediary1.children_in_traversal_order = new_intermediary1_children; + + int32_t new_intermediary2_children[] = {leaf1_id}; + intermediary2.child_count = 1; + intermediary2.children_in_traversal_order = new_intermediary2_children; + + bridge->AddFlutterSemanticsNodeUpdate(&intermediary1); + bridge->AddFlutterSemanticsNodeUpdate(&intermediary2); + bridge->AddFlutterSemanticsNodeUpdate(&leaf1); + bridge->AddFlutterSemanticsNodeUpdate(&leaf2); + bridge->AddFlutterSemanticsNodeUpdate(&leaf3); + bridge->CommitUpdates(); + + auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(root_id).lock(); + auto intermediary1_node = + bridge->GetFlutterPlatformNodeDelegateFromID(intermediary1_id).lock(); + auto intermediary2_node = + bridge->GetFlutterPlatformNodeDelegateFromID(intermediary2_id).lock(); + auto leaf1_node = + bridge->GetFlutterPlatformNodeDelegateFromID(leaf1_id).lock(); + auto leaf2_node = + bridge->GetFlutterPlatformNodeDelegateFromID(leaf2_id).lock(); + auto leaf3_node = + bridge->GetFlutterPlatformNodeDelegateFromID(leaf3_id).lock(); + + EXPECT_EQ(root_node->GetChildCount(), 2); + EXPECT_EQ(root_node->GetData().child_ids[0], intermediary1_id); + EXPECT_EQ(root_node->GetData().child_ids[1], intermediary2_id); + EXPECT_EQ(root_node->GetName(), "root"); + + EXPECT_EQ(intermediary1_node->GetChildCount(), 2); + EXPECT_EQ(intermediary1_node->GetData().child_ids[0], leaf2_id); + EXPECT_EQ(intermediary1_node->GetData().child_ids[1], leaf3_id); + EXPECT_EQ(intermediary1_node->GetName(), "intermediary 1"); + + EXPECT_EQ(intermediary2_node->GetChildCount(), 1); + EXPECT_EQ(intermediary2_node->GetData().child_ids[0], leaf1_id); + EXPECT_EQ(intermediary2_node->GetName(), "intermediary 2"); + + EXPECT_EQ(leaf1_node->GetChildCount(), 0); + EXPECT_EQ(leaf1_node->GetName(), "leaf 1"); + + EXPECT_EQ(leaf2_node->GetChildCount(), 0); + EXPECT_EQ(leaf2_node->GetName(), "leaf 2"); + + EXPECT_EQ(leaf3_node->GetChildCount(), 0); + EXPECT_EQ(leaf3_node->GetName(), "leaf 3"); + + // Intermediary 1 and intermediary 2 have new children. + // Leaf 1, 2, and 3 are all moved. + ASSERT_EQ(delegate->accessibility_events.size(), size_t{5}); + EXPECT_THAT(delegate->accessibility_events, + Contains(ui::AXEventGenerator::Event::CHILDREN_CHANGED).Times(2)); + EXPECT_THAT(delegate->accessibility_events, + Contains(ui::AXEventGenerator::Event::SUBTREE_CREATED).Times(3)); +} + +// Verify that a node with a child can be moved from one parent to another. +TEST(AccessibilityBridgeTest, CanReparentNodeWithChild) { + TestAccessibilityBridgeDelegate* delegate = + new TestAccessibilityBridgeDelegate(); + std::unique_ptr ptr(delegate); + std::shared_ptr bridge = + std::make_shared(std::move(ptr)); + + int32_t root_id = 0; + int32_t intermediary1_id = 1; + int32_t intermediary2_id = 2; + int32_t leaf1_id = 3; + + std::vector root_children{intermediary1_id, intermediary2_id}; + std::vector intermediary1_children{leaf1_id}; + FlutterSemanticsNode root = + CreateSemanticsNode(root_id, "root", &root_children); + FlutterSemanticsNode intermediary1 = CreateSemanticsNode( + intermediary1_id, "intermediary 1", &intermediary1_children); + FlutterSemanticsNode intermediary2 = + CreateSemanticsNode(intermediary2_id, "intermediary 2"); + FlutterSemanticsNode leaf1 = CreateSemanticsNode(leaf1_id, "leaf 1"); + + bridge->AddFlutterSemanticsNodeUpdate(&root); + bridge->AddFlutterSemanticsNodeUpdate(&intermediary1); + bridge->AddFlutterSemanticsNodeUpdate(&intermediary2); + bridge->AddFlutterSemanticsNodeUpdate(&leaf1); + bridge->CommitUpdates(); + delegate->accessibility_events.clear(); + + // Move intermediary1 from root to intermediary 2. + int32_t new_root_children[] = {intermediary2_id}; + root.child_count = 1; + root.children_in_traversal_order = new_root_children; + + int32_t new_intermediary2_children[] = {intermediary1_id}; + intermediary2.child_count = 1; + intermediary2.children_in_traversal_order = new_intermediary2_children; + + bridge->AddFlutterSemanticsNodeUpdate(&root); + bridge->AddFlutterSemanticsNodeUpdate(&intermediary1); + bridge->AddFlutterSemanticsNodeUpdate(&intermediary2); + bridge->AddFlutterSemanticsNodeUpdate(&leaf1); + bridge->CommitUpdates(); + + auto root_node = bridge->GetFlutterPlatformNodeDelegateFromID(root_id).lock(); + auto intermediary1_node = + bridge->GetFlutterPlatformNodeDelegateFromID(intermediary1_id).lock(); + auto intermediary2_node = + bridge->GetFlutterPlatformNodeDelegateFromID(intermediary2_id).lock(); + auto leaf1_node = + bridge->GetFlutterPlatformNodeDelegateFromID(leaf1_id).lock(); + + EXPECT_EQ(root_node->GetChildCount(), 1); + EXPECT_EQ(root_node->GetData().child_ids[0], intermediary2_id); + EXPECT_EQ(root_node->GetName(), "root"); + + EXPECT_EQ(intermediary2_node->GetChildCount(), 1); + EXPECT_EQ(intermediary2_node->GetData().child_ids[0], intermediary1_id); + EXPECT_EQ(intermediary2_node->GetName(), "intermediary 2"); + + EXPECT_EQ(intermediary1_node->GetChildCount(), 1); + EXPECT_EQ(intermediary1_node->GetData().child_ids[0], leaf1_id); + EXPECT_EQ(intermediary1_node->GetName(), "intermediary 1"); + + EXPECT_EQ(leaf1_node->GetChildCount(), 0); + EXPECT_EQ(leaf1_node->GetName(), "leaf 1"); + + ASSERT_EQ(delegate->accessibility_events.size(), size_t{5}); + + EXPECT_THAT(delegate->accessibility_events, + Contains(ui::AXEventGenerator::Event::CHILDREN_CHANGED).Times(2)); + EXPECT_THAT(delegate->accessibility_events, + Contains(ui::AXEventGenerator::Event::SUBTREE_CREATED).Times(1)); + + // Intermediary 2 becomes a parent node. It updates to group role and enables + // its 'clip children' attribute. + EXPECT_THAT( + delegate->accessibility_events, + Contains(ui::AXEventGenerator::Event::OTHER_ATTRIBUTE_CHANGED).Times(1)); + EXPECT_THAT(delegate->accessibility_events, + Contains(ui::AXEventGenerator::Event::ROLE_CHANGED).Times(1)); +} + } // namespace testing } // namespace flutter