ax_tree_combiner_unittest.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // 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. #include "ui/accessibility/ax_tree_combiner.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. #include "ui/accessibility/ax_enums.mojom.h"
  7. namespace ui {
  8. TEST(CombineAXTreesTest, RenumberOneTree) {
  9. AXTreeID tree_id_1 = AXTreeID::CreateNewAXTreeID();
  10. AXTreeUpdate tree;
  11. tree.has_tree_data = true;
  12. tree.tree_data.tree_id = tree_id_1;
  13. tree.root_id = 2;
  14. tree.nodes.resize(3);
  15. tree.nodes[0].id = 2;
  16. tree.nodes[0].child_ids.push_back(4);
  17. tree.nodes[0].child_ids.push_back(6);
  18. tree.nodes[1].id = 4;
  19. tree.nodes[2].id = 6;
  20. AXTreeCombiner combiner;
  21. combiner.AddTree(tree, true);
  22. combiner.Combine();
  23. const AXTreeUpdate& combined = combiner.combined();
  24. EXPECT_EQ(1, combined.root_id);
  25. ASSERT_EQ(3U, combined.nodes.size());
  26. EXPECT_EQ(1, combined.nodes[0].id);
  27. ASSERT_EQ(2U, combined.nodes[0].child_ids.size());
  28. EXPECT_EQ(2, combined.nodes[0].child_ids[0]);
  29. EXPECT_EQ(3, combined.nodes[0].child_ids[1]);
  30. EXPECT_EQ(2, combined.nodes[1].id);
  31. EXPECT_EQ(3, combined.nodes[2].id);
  32. }
  33. TEST(CombineAXTreesTest, EmbedChildTree) {
  34. AXTreeID tree_id_1 = AXTreeID::CreateNewAXTreeID();
  35. AXTreeID tree_id_2 = AXTreeID::CreateNewAXTreeID();
  36. AXTreeUpdate parent_tree;
  37. parent_tree.root_id = 1;
  38. parent_tree.has_tree_data = true;
  39. parent_tree.tree_data.tree_id = tree_id_1;
  40. parent_tree.nodes.resize(3);
  41. parent_tree.nodes[0].id = 1;
  42. parent_tree.nodes[0].child_ids.push_back(2);
  43. parent_tree.nodes[0].child_ids.push_back(3);
  44. parent_tree.nodes[1].id = 2;
  45. parent_tree.nodes[1].role = ax::mojom::Role::kButton;
  46. parent_tree.nodes[2].id = 3;
  47. parent_tree.nodes[2].role = ax::mojom::Role::kIframe;
  48. parent_tree.nodes[2].AddChildTreeId(tree_id_2);
  49. AXTreeUpdate child_tree;
  50. child_tree.root_id = 1;
  51. child_tree.has_tree_data = true;
  52. child_tree.tree_data.parent_tree_id = tree_id_1;
  53. child_tree.tree_data.tree_id = tree_id_2;
  54. child_tree.nodes.resize(3);
  55. child_tree.nodes[0].id = 1;
  56. child_tree.nodes[0].child_ids.push_back(2);
  57. child_tree.nodes[0].child_ids.push_back(3);
  58. child_tree.nodes[1].id = 2;
  59. child_tree.nodes[1].role = ax::mojom::Role::kCheckBox;
  60. child_tree.nodes[2].id = 3;
  61. child_tree.nodes[2].role = ax::mojom::Role::kRadioButton;
  62. AXTreeCombiner combiner;
  63. combiner.AddTree(parent_tree, true);
  64. combiner.AddTree(child_tree, false);
  65. combiner.Combine();
  66. const AXTreeUpdate& combined = combiner.combined();
  67. EXPECT_EQ(1, combined.root_id);
  68. ASSERT_EQ(6U, combined.nodes.size());
  69. EXPECT_EQ(1, combined.nodes[0].id);
  70. ASSERT_EQ(2U, combined.nodes[0].child_ids.size());
  71. EXPECT_EQ(2, combined.nodes[0].child_ids[0]);
  72. EXPECT_EQ(3, combined.nodes[0].child_ids[1]);
  73. EXPECT_EQ(2, combined.nodes[1].id);
  74. EXPECT_EQ(ax::mojom::Role::kButton, combined.nodes[1].role);
  75. EXPECT_EQ(3, combined.nodes[2].id);
  76. EXPECT_EQ(ax::mojom::Role::kIframe, combined.nodes[2].role);
  77. EXPECT_EQ(1U, combined.nodes[2].child_ids.size());
  78. EXPECT_EQ(4, combined.nodes[2].child_ids[0]);
  79. EXPECT_EQ(4, combined.nodes[3].id);
  80. EXPECT_EQ(5, combined.nodes[4].id);
  81. EXPECT_EQ(ax::mojom::Role::kCheckBox, combined.nodes[4].role);
  82. EXPECT_EQ(6, combined.nodes[5].id);
  83. EXPECT_EQ(ax::mojom::Role::kRadioButton, combined.nodes[5].role);
  84. }
  85. TEST(CombineAXTreesTest, MapAllIdAttributes) {
  86. AXTreeID tree_id_1 = AXTreeID::CreateNewAXTreeID();
  87. // This is a nonsensical accessibility tree, the goal is to make sure
  88. // that all attributes that reference IDs of other nodes are remapped.
  89. AXTreeUpdate tree;
  90. tree.has_tree_data = true;
  91. tree.tree_data.tree_id = tree_id_1;
  92. tree.root_id = 11;
  93. tree.nodes.resize(2);
  94. tree.nodes[0].id = 11;
  95. tree.nodes[0].child_ids.push_back(22);
  96. tree.nodes[0].AddIntAttribute(ax::mojom::IntAttribute::kTableHeaderId, 22);
  97. tree.nodes[0].AddIntAttribute(ax::mojom::IntAttribute::kTableRowHeaderId, 22);
  98. tree.nodes[0].AddIntAttribute(ax::mojom::IntAttribute::kTableColumnHeaderId,
  99. 22);
  100. tree.nodes[0].AddIntAttribute(ax::mojom::IntAttribute::kActivedescendantId,
  101. 22);
  102. std::vector<AXNodeID> ids{22};
  103. tree.nodes[0].AddIntListAttribute(
  104. ax::mojom::IntListAttribute::kIndirectChildIds, ids);
  105. tree.nodes[0].AddIntListAttribute(ax::mojom::IntListAttribute::kControlsIds,
  106. ids);
  107. tree.nodes[0].AddIntListAttribute(
  108. ax::mojom::IntListAttribute::kDescribedbyIds, ids);
  109. tree.nodes[0].AddIntListAttribute(ax::mojom::IntListAttribute::kFlowtoIds,
  110. ids);
  111. tree.nodes[0].AddIntListAttribute(ax::mojom::IntListAttribute::kLabelledbyIds,
  112. ids);
  113. tree.nodes[1].id = 22;
  114. AXTreeCombiner combiner;
  115. combiner.AddTree(tree, true);
  116. combiner.Combine();
  117. const AXTreeUpdate& combined = combiner.combined();
  118. EXPECT_EQ(1, combined.root_id);
  119. ASSERT_EQ(2U, combined.nodes.size());
  120. EXPECT_EQ(1, combined.nodes[0].id);
  121. ASSERT_EQ(1U, combined.nodes[0].child_ids.size());
  122. EXPECT_EQ(2, combined.nodes[0].child_ids[0]);
  123. EXPECT_EQ(2, combined.nodes[1].id);
  124. EXPECT_EQ(2, combined.nodes[0].GetIntAttribute(
  125. ax::mojom::IntAttribute::kTableHeaderId));
  126. EXPECT_EQ(2, combined.nodes[0].GetIntAttribute(
  127. ax::mojom::IntAttribute::kTableRowHeaderId));
  128. EXPECT_EQ(2, combined.nodes[0].GetIntAttribute(
  129. ax::mojom::IntAttribute::kTableColumnHeaderId));
  130. EXPECT_EQ(2, combined.nodes[0].GetIntAttribute(
  131. ax::mojom::IntAttribute::kActivedescendantId));
  132. EXPECT_EQ(2, combined.nodes[0].GetIntListAttribute(
  133. ax::mojom::IntListAttribute::kIndirectChildIds)[0]);
  134. EXPECT_EQ(2, combined.nodes[0].GetIntListAttribute(
  135. ax::mojom::IntListAttribute::kControlsIds)[0]);
  136. EXPECT_EQ(2, combined.nodes[0].GetIntListAttribute(
  137. ax::mojom::IntListAttribute::kDescribedbyIds)[0]);
  138. EXPECT_EQ(2, combined.nodes[0].GetIntListAttribute(
  139. ax::mojom::IntListAttribute::kFlowtoIds)[0]);
  140. EXPECT_EQ(2, combined.nodes[0].GetIntListAttribute(
  141. ax::mojom::IntListAttribute::kLabelledbyIds)[0]);
  142. }
  143. TEST(CombineAXTreesTest, FocusedTree) {
  144. AXTreeID tree_id_1 = AXTreeID::CreateNewAXTreeID();
  145. AXTreeID tree_id_2 = AXTreeID::CreateNewAXTreeID();
  146. AXTreeUpdate parent_tree;
  147. parent_tree.has_tree_data = true;
  148. parent_tree.tree_data.tree_id = tree_id_1;
  149. parent_tree.tree_data.focused_tree_id = tree_id_2;
  150. parent_tree.tree_data.focus_id = 2;
  151. parent_tree.root_id = 1;
  152. parent_tree.nodes.resize(3);
  153. parent_tree.nodes[0].id = 1;
  154. parent_tree.nodes[0].child_ids.push_back(2);
  155. parent_tree.nodes[0].child_ids.push_back(3);
  156. parent_tree.nodes[1].id = 2;
  157. parent_tree.nodes[1].role = ax::mojom::Role::kButton;
  158. parent_tree.nodes[2].id = 3;
  159. parent_tree.nodes[2].role = ax::mojom::Role::kIframe;
  160. parent_tree.nodes[2].AddChildTreeId(tree_id_2);
  161. AXTreeUpdate child_tree;
  162. child_tree.has_tree_data = true;
  163. child_tree.tree_data.parent_tree_id = tree_id_1;
  164. child_tree.tree_data.tree_id = tree_id_2;
  165. child_tree.tree_data.focus_id = 3;
  166. child_tree.root_id = 1;
  167. child_tree.nodes.resize(3);
  168. child_tree.nodes[0].id = 1;
  169. child_tree.nodes[0].child_ids.push_back(2);
  170. child_tree.nodes[0].child_ids.push_back(3);
  171. child_tree.nodes[1].id = 2;
  172. child_tree.nodes[1].role = ax::mojom::Role::kCheckBox;
  173. child_tree.nodes[2].id = 3;
  174. child_tree.nodes[2].role = ax::mojom::Role::kRadioButton;
  175. AXTreeCombiner combiner;
  176. combiner.AddTree(parent_tree, true);
  177. combiner.AddTree(child_tree, false);
  178. combiner.Combine();
  179. const AXTreeUpdate& combined = combiner.combined();
  180. ASSERT_EQ(6U, combined.nodes.size());
  181. EXPECT_EQ(6, combined.tree_data.focus_id);
  182. }
  183. TEST(CombineAXTreesTest, EmptyTree) {
  184. AXTreeUpdate tree;
  185. AXTreeCombiner combiner;
  186. combiner.AddTree(tree, true);
  187. combiner.Combine();
  188. const AXTreeUpdate& combined = combiner.combined();
  189. ASSERT_EQ(0U, combined.nodes.size());
  190. }
  191. } // namespace ui