blob: 70f861c0966c7fb74c475387ce7b54f116e600ff [file] [log] [blame]
dmazzonid95ae842016-04-12 21:17:391// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "testing/gtest/include/gtest/gtest.h"
6#include "ui/accessibility/ax_tree_combiner.h"
7
8namespace ui {
9
10TEST(CombineAXTreesTest, RenumberOneTree) {
11 AXTreeUpdate tree;
12 tree.has_tree_data = true;
13 tree.tree_data.tree_id = 1;
dmazzoni67b4db22016-04-23 00:40:0414 tree.root_id = 2;
dmazzonid95ae842016-04-12 21:17:3915 tree.nodes.resize(3);
16 tree.nodes[0].id = 2;
dmazzonid95ae842016-04-12 21:17:3917 tree.nodes[0].child_ids.push_back(4);
18 tree.nodes[0].child_ids.push_back(6);
19 tree.nodes[1].id = 4;
20 tree.nodes[2].id = 6;
21
22 AXTreeCombiner combiner;
23 combiner.AddTree(tree, true);
24 combiner.Combine();
25
26 const AXTreeUpdate& combined = combiner.combined();
27
dmazzoni67b4db22016-04-23 00:40:0428 EXPECT_EQ(1, combined.root_id);
dmazzonid95ae842016-04-12 21:17:3929 ASSERT_EQ(3U, combined.nodes.size());
30 EXPECT_EQ(1, combined.nodes[0].id);
31 ASSERT_EQ(2U, combined.nodes[0].child_ids.size());
32 EXPECT_EQ(2, combined.nodes[0].child_ids[0]);
33 EXPECT_EQ(3, combined.nodes[0].child_ids[1]);
34 EXPECT_EQ(2, combined.nodes[1].id);
35 EXPECT_EQ(3, combined.nodes[2].id);
36}
37
38TEST(CombineAXTreesTest, EmbedChildTree) {
39 AXTreeUpdate parent_tree;
dmazzoni67b4db22016-04-23 00:40:0440 parent_tree.root_id = 1;
dmazzonid95ae842016-04-12 21:17:3941 parent_tree.has_tree_data = true;
42 parent_tree.tree_data.tree_id = 1;
43 parent_tree.nodes.resize(3);
44 parent_tree.nodes[0].id = 1;
dmazzonid95ae842016-04-12 21:17:3945 parent_tree.nodes[0].child_ids.push_back(2);
46 parent_tree.nodes[0].child_ids.push_back(3);
47 parent_tree.nodes[1].id = 2;
48 parent_tree.nodes[1].role = AX_ROLE_BUTTON;
49 parent_tree.nodes[2].id = 3;
50 parent_tree.nodes[2].role = AX_ROLE_IFRAME;
51 parent_tree.nodes[2].AddIntAttribute(AX_ATTR_CHILD_TREE_ID, 2);
52
53 AXTreeUpdate child_tree;
dmazzoni67b4db22016-04-23 00:40:0454 child_tree.root_id = 1;
dmazzonid95ae842016-04-12 21:17:3955 child_tree.has_tree_data = true;
56 child_tree.tree_data.parent_tree_id = 1;
57 child_tree.tree_data.tree_id = 2;
58 child_tree.nodes.resize(3);
59 child_tree.nodes[0].id = 1;
dmazzonid95ae842016-04-12 21:17:3960 child_tree.nodes[0].child_ids.push_back(2);
61 child_tree.nodes[0].child_ids.push_back(3);
62 child_tree.nodes[1].id = 2;
63 child_tree.nodes[1].role = AX_ROLE_CHECK_BOX;
64 child_tree.nodes[2].id = 3;
65 child_tree.nodes[2].role = AX_ROLE_RADIO_BUTTON;
66
67 AXTreeCombiner combiner;
68 combiner.AddTree(parent_tree, true);
69 combiner.AddTree(child_tree, false);
70 combiner.Combine();
71
72 const AXTreeUpdate& combined = combiner.combined();
73
dmazzoni67b4db22016-04-23 00:40:0474 EXPECT_EQ(1, combined.root_id);
dmazzonid95ae842016-04-12 21:17:3975 ASSERT_EQ(6U, combined.nodes.size());
76 EXPECT_EQ(1, combined.nodes[0].id);
77 ASSERT_EQ(2U, combined.nodes[0].child_ids.size());
78 EXPECT_EQ(2, combined.nodes[0].child_ids[0]);
79 EXPECT_EQ(3, combined.nodes[0].child_ids[1]);
80 EXPECT_EQ(2, combined.nodes[1].id);
81 EXPECT_EQ(AX_ROLE_BUTTON, combined.nodes[1].role);
82 EXPECT_EQ(3, combined.nodes[2].id);
83 EXPECT_EQ(AX_ROLE_IFRAME, combined.nodes[2].role);
84 EXPECT_EQ(1U, combined.nodes[2].child_ids.size());
85 EXPECT_EQ(4, combined.nodes[2].child_ids[0]);
86 EXPECT_EQ(4, combined.nodes[3].id);
dmazzonid95ae842016-04-12 21:17:3987 EXPECT_EQ(5, combined.nodes[4].id);
88 EXPECT_EQ(AX_ROLE_CHECK_BOX, combined.nodes[4].role);
89 EXPECT_EQ(6, combined.nodes[5].id);
90 EXPECT_EQ(AX_ROLE_RADIO_BUTTON, combined.nodes[5].role);
91}
92
93TEST(CombineAXTreesTest, MapAllIdAttributes) {
94 // This is a nonsensical accessibility tree, the goal is to make sure
95 // that all attributes that reference IDs of other nodes are remapped.
96
97 AXTreeUpdate tree;
98 tree.has_tree_data = true;
99 tree.tree_data.tree_id = 1;
dmazzoni67b4db22016-04-23 00:40:04100 tree.root_id = 11;
dmazzonid95ae842016-04-12 21:17:39101 tree.nodes.resize(2);
102 tree.nodes[0].id = 11;
dmazzonid95ae842016-04-12 21:17:39103 tree.nodes[0].child_ids.push_back(22);
104 tree.nodes[0].AddIntAttribute(AX_ATTR_TABLE_HEADER_ID, 22);
105 tree.nodes[0].AddIntAttribute(AX_ATTR_TABLE_ROW_HEADER_ID, 22);
106 tree.nodes[0].AddIntAttribute(AX_ATTR_TABLE_COLUMN_HEADER_ID, 22);
107 tree.nodes[0].AddIntAttribute(AX_ATTR_ACTIVEDESCENDANT_ID, 22);
108 std::vector<int32_t> ids { 22 };
109 tree.nodes[0].AddIntListAttribute(AX_ATTR_INDIRECT_CHILD_IDS, ids);
110 tree.nodes[0].AddIntListAttribute(AX_ATTR_CONTROLS_IDS, ids);
111 tree.nodes[0].AddIntListAttribute(AX_ATTR_DESCRIBEDBY_IDS, ids);
112 tree.nodes[0].AddIntListAttribute(AX_ATTR_FLOWTO_IDS, ids);
113 tree.nodes[0].AddIntListAttribute(AX_ATTR_LABELLEDBY_IDS, ids);
114 tree.nodes[0].AddIntListAttribute(AX_ATTR_CELL_IDS, ids);
115 tree.nodes[0].AddIntListAttribute(AX_ATTR_UNIQUE_CELL_IDS, ids);
116 tree.nodes[1].id = 22;
117
118 AXTreeCombiner combiner;
119 combiner.AddTree(tree, true);
120 combiner.Combine();
121
122 const AXTreeUpdate& combined = combiner.combined();
123
dmazzoni67b4db22016-04-23 00:40:04124 EXPECT_EQ(1, combined.root_id);
dmazzonid95ae842016-04-12 21:17:39125 ASSERT_EQ(2U, combined.nodes.size());
126 EXPECT_EQ(1, combined.nodes[0].id);
127 ASSERT_EQ(1U, combined.nodes[0].child_ids.size());
128 EXPECT_EQ(2, combined.nodes[0].child_ids[0]);
129 EXPECT_EQ(2, combined.nodes[1].id);
130
131 EXPECT_EQ(2, combined.nodes[0].GetIntAttribute(AX_ATTR_TABLE_HEADER_ID));
132 EXPECT_EQ(2, combined.nodes[0].GetIntAttribute(AX_ATTR_TABLE_ROW_HEADER_ID));
133 EXPECT_EQ(2, combined.nodes[0].GetIntAttribute(
134 AX_ATTR_TABLE_COLUMN_HEADER_ID));
135 EXPECT_EQ(2, combined.nodes[0].GetIntAttribute(AX_ATTR_ACTIVEDESCENDANT_ID));
136 EXPECT_EQ(2, combined.nodes[0].GetIntListAttribute(
137 AX_ATTR_INDIRECT_CHILD_IDS)[0]);
138 EXPECT_EQ(2, combined.nodes[0].GetIntListAttribute(
139 AX_ATTR_CONTROLS_IDS)[0]);
140 EXPECT_EQ(2, combined.nodes[0].GetIntListAttribute(
141 AX_ATTR_DESCRIBEDBY_IDS)[0]);
142 EXPECT_EQ(2, combined.nodes[0].GetIntListAttribute(
143 AX_ATTR_FLOWTO_IDS)[0]);
144 EXPECT_EQ(2, combined.nodes[0].GetIntListAttribute(
145 AX_ATTR_LABELLEDBY_IDS)[0]);
146 EXPECT_EQ(2, combined.nodes[0].GetIntListAttribute(
147 AX_ATTR_CELL_IDS)[0]);
148 EXPECT_EQ(2, combined.nodes[0].GetIntListAttribute(
149 AX_ATTR_UNIQUE_CELL_IDS)[0]);
150}
151
dmazzonid95ae842016-04-12 21:17:39152TEST(CombineAXTreesTest, FocusedTree) {
153 AXTreeUpdate parent_tree;
154 parent_tree.has_tree_data = true;
155 parent_tree.tree_data.tree_id = 1;
156 parent_tree.tree_data.focused_tree_id = 2;
157 parent_tree.tree_data.focus_id = 2;
dmazzoni67b4db22016-04-23 00:40:04158 parent_tree.root_id = 1;
dmazzonid95ae842016-04-12 21:17:39159 parent_tree.nodes.resize(3);
160 parent_tree.nodes[0].id = 1;
dmazzonid95ae842016-04-12 21:17:39161 parent_tree.nodes[0].child_ids.push_back(2);
162 parent_tree.nodes[0].child_ids.push_back(3);
163 parent_tree.nodes[1].id = 2;
164 parent_tree.nodes[1].role = AX_ROLE_BUTTON;
165 parent_tree.nodes[2].id = 3;
166 parent_tree.nodes[2].role = AX_ROLE_IFRAME;
167 parent_tree.nodes[2].AddIntAttribute(AX_ATTR_CHILD_TREE_ID, 2);
168
169 AXTreeUpdate child_tree;
170 child_tree.has_tree_data = true;
171 child_tree.tree_data.parent_tree_id = 1;
172 child_tree.tree_data.tree_id = 2;
173 child_tree.tree_data.focus_id = 3;
dmazzoni67b4db22016-04-23 00:40:04174 child_tree.root_id = 1;
dmazzonid95ae842016-04-12 21:17:39175 child_tree.nodes.resize(3);
176 child_tree.nodes[0].id = 1;
dmazzonid95ae842016-04-12 21:17:39177 child_tree.nodes[0].child_ids.push_back(2);
178 child_tree.nodes[0].child_ids.push_back(3);
179 child_tree.nodes[1].id = 2;
180 child_tree.nodes[1].role = AX_ROLE_CHECK_BOX;
181 child_tree.nodes[2].id = 3;
182 child_tree.nodes[2].role = AX_ROLE_RADIO_BUTTON;
183
184 AXTreeCombiner combiner;
185 combiner.AddTree(parent_tree, true);
186 combiner.AddTree(child_tree, false);
187 combiner.Combine();
188
189 const AXTreeUpdate& combined = combiner.combined();
190
191 ASSERT_EQ(6U, combined.nodes.size());
192 EXPECT_EQ(6, combined.tree_data.focus_id);
193}
194
Dominic Mazzoni72482e62017-06-16 21:29:49195TEST(CombineAXTreesTest, EmptyTree) {
196 AXTreeUpdate tree;
197
198 AXTreeCombiner combiner;
199 combiner.AddTree(tree, true);
200 combiner.Combine();
201
202 const AXTreeUpdate& combined = combiner.combined();
203 ASSERT_EQ(0U, combined.nodes.size());
204}
205
dmazzonid95ae842016-04-12 21:17:39206} // namespace ui