File: ax_tree_manager_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (205 lines) | stat: -rw-r--r-- 7,036 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/accessibility/ax_tree_manager.h"
#include "ui/accessibility/ax_node.h"
#include "ui/accessibility/ax_serializable_tree.h"
#include "ui/accessibility/ax_tree_id.h"
#include "ui/accessibility/ax_tree_update.h"
#include "ui/accessibility/test_single_ax_tree_manager.h"

#include "testing/gtest/include/gtest/gtest.h"

namespace ui {

TEST(AXTreeManagerTest, ConstructFromInitialState) {
  AXNodeData root;
  root.id = 1;
  root.role = ax::mojom::Role::kRootWebArea;
  std::string name = "Hello";
  root.AddStringAttribute(ax::mojom::StringAttribute::kName, name);

  AXTreeUpdate initial_state;
  initial_state.root_id = 1;
  initial_state.nodes.push_back(root);
  initial_state.has_tree_data = true;

  TestSingleAXTreeManager manager(std::make_unique<AXSerializableTree>());

  manager.Initialize(initial_state);

  AXNode* returned_root = manager.GetRoot();
  ASSERT_EQ(
      returned_root->GetStringAttribute(ax::mojom::StringAttribute::kName),
      name);
}

TEST(AXTreeManagerTest, GetRootManagerUnserializedParent) {
  // The test covers the scenario where a manager's parent tree is not
  // yet serialized.
  // ++1 kRootWebArea
  // ++++2 kGenericContainer
  // ++++3 kGenericContainer
  // ++++++4 kRootWebArea
  // ++++++++5 kGenericContainer
  // ++++++++++6 kRootWebArea
  // ++++++++++++7 kGenericContainer

  AXNodeData root_1;
  AXNodeData generic_container_2;
  AXNodeData generic_container_3;
  AXNodeData root_4;
  AXNodeData generic_container_5;
  AXNodeData root_6;
  AXNodeData generic_container_7;

  root_1.id = 1;
  generic_container_2.id = 2;
  generic_container_3.id = 3;
  root_4.id = 4;
  generic_container_5.id = 5;
  root_6.id = 6;
  generic_container_7.id = 7;

  root_1.role = ax::mojom::Role::kRootWebArea;
  root_1.child_ids = {generic_container_2.id, generic_container_3.id};

  generic_container_2.role = ax::mojom::Role::kGenericContainer;

  generic_container_3.role = ax::mojom::Role::kGenericContainer;

  root_4.role = ax::mojom::Role::kRootWebArea;
  root_4.child_ids = {generic_container_5.id};

  generic_container_5.role = ax::mojom::Role::kGenericContainer;

  root_6.role = ax::mojom::Role::kRootWebArea;
  root_6.child_ids = {generic_container_7.id};

  generic_container_7.role = ax::mojom::Role::kGenericContainer;

  AXTreeUpdate first_state;
  first_state.root_id = root_1.id;
  first_state.nodes = {root_1, generic_container_2, generic_container_3};
  first_state.has_tree_data = true;
  first_state.tree_data.tree_id = AXTreeID::CreateNewAXTreeID();

  AXTreeUpdate middle_state;
  middle_state.root_id = root_4.id;
  middle_state.nodes = {root_4, generic_container_5};
  middle_state.has_tree_data = true;
  middle_state.tree_data.tree_id = AXTreeID::CreateNewAXTreeID();

  AXTreeUpdate last_state;
  last_state.root_id = root_6.id;
  last_state.nodes = {root_6, generic_container_7};
  last_state.has_tree_data = true;
  last_state.tree_data.tree_id = AXTreeID::CreateNewAXTreeID();

  // We link the first two child trees to their parent trees.
  middle_state.tree_data.parent_tree_id = first_state.tree_data.tree_id;
  generic_container_3.AddChildTreeId(middle_state.tree_data.tree_id);

  // We link the last child to the middle child.
  last_state.tree_data.parent_tree_id = middle_state.tree_data.tree_id;
  generic_container_5.AddChildTreeId(last_state.tree_data.tree_id);

  // Create the managers. We don't `Initialize` the middle manager to test the
  // scenario where the parent manager is not serialized yet.
  TestSingleAXTreeManager first_manager(std::make_unique<AXSerializableTree>());
  first_manager.Initialize(first_state);
  TestSingleAXTreeManager last_manager(std::make_unique<AXSerializableTree>());
  last_manager.Initialize(last_state);

  ASSERT_EQ(first_manager.GetRootManager(), &first_manager);

  ASSERT_EQ(last_manager.GetRootManager(), nullptr);

}

TEST(AXTreeManagerTest, GetRootManagerAndIsRoot) {
  // The test covers two cases: First, when we call GetRootManager on the root
  // manager itself. Second, when we call GetRootManager on a child of the root.
  // ++1 kRootWebArea
  // ++++2 kGenericContainer
  // ++++3 kGenericContainer
  // ++++++4 kRootWebArea
  // ++++++++5 kGenericContainer
  // ++++++++++6 kRootWebArea
  // ++++++++++++7 kGenericContainer

  AXNodeData root_1;
  AXNodeData generic_container_2;
  AXNodeData generic_container_3;
  AXNodeData root_4;
  AXNodeData generic_container_5;
  AXNodeData root_6;
  AXNodeData generic_container_7;

  root_1.id = 1;
  generic_container_2.id = 2;
  generic_container_3.id = 3;
  root_4.id = 4;
  generic_container_5.id = 5;
  root_6.id = 6;
  generic_container_7.id = 7;

  root_1.role = ax::mojom::Role::kRootWebArea;
  root_1.child_ids = {generic_container_2.id, generic_container_3.id};

  generic_container_2.role = ax::mojom::Role::kGenericContainer;

  generic_container_3.role = ax::mojom::Role::kGenericContainer;

  root_4.role = ax::mojom::Role::kRootWebArea;
  root_4.child_ids = {generic_container_5.id};

  generic_container_5.role = ax::mojom::Role::kGenericContainer;

  root_6.role = ax::mojom::Role::kRootWebArea;
  root_6.child_ids = {generic_container_7.id};

  generic_container_7.role = ax::mojom::Role::kGenericContainer;

  AXTreeUpdate first_state;
  first_state.root_id = root_1.id;
  first_state.nodes = {root_1, generic_container_2, generic_container_3};
  first_state.has_tree_data = true;
  first_state.tree_data.tree_id = AXTreeID::CreateNewAXTreeID();

  AXTreeUpdate middle_state;
  middle_state.root_id = root_4.id;
  middle_state.nodes = {root_4, generic_container_5};
  middle_state.has_tree_data = true;
  middle_state.tree_data.tree_id = AXTreeID::CreateNewAXTreeID();

  AXTreeUpdate last_state;
  last_state.root_id = root_6.id;
  last_state.nodes = {root_6, generic_container_7};
  last_state.has_tree_data = true;
  last_state.tree_data.tree_id = AXTreeID::CreateNewAXTreeID();

  // We link the first two child trees to their parent trees.
  middle_state.tree_data.parent_tree_id = first_state.tree_data.tree_id;
  generic_container_3.AddChildTreeId(middle_state.tree_data.tree_id);

  // We link the last child to the middle child.
  last_state.tree_data.parent_tree_id = middle_state.tree_data.tree_id;
  generic_container_5.AddChildTreeId(last_state.tree_data.tree_id);

  // Create the managers.
  TestSingleAXTreeManager first_manager(std::make_unique<AXSerializableTree>());
  first_manager.Initialize(first_state);
  TestSingleAXTreeManager middle_manager(
      std::make_unique<AXSerializableTree>());
  middle_manager.Initialize(middle_state);
  TestSingleAXTreeManager last_manager(std::make_unique<AXSerializableTree>());
  last_manager.Initialize(last_state);

  ASSERT_EQ(first_manager.GetRootManager(), &first_manager);
  ASSERT_EQ(middle_manager.GetRootManager(), &first_manager);
  ASSERT_EQ(last_manager.GetRootManager(), &first_manager);
}

}  // namespace ui