ax_table_info_unittest.cc 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  1. // Copyright 2018 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_table_info.h"
  5. #include "build/build_config.h"
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. #include "ui/accessibility/ax_enums.mojom.h"
  8. #include "ui/accessibility/ax_node.h"
  9. #include "ui/accessibility/ax_tree.h"
  10. namespace ui {
  11. namespace {
  12. void MakeTable(AXNodeData* table, int id, int row_count, int col_count) {
  13. table->id = id;
  14. table->role = ax::mojom::Role::kTable;
  15. table->AddIntAttribute(ax::mojom::IntAttribute::kTableRowCount, row_count);
  16. table->AddIntAttribute(ax::mojom::IntAttribute::kTableColumnCount, col_count);
  17. }
  18. void MakeRowGroup(AXNodeData* row_group, int id) {
  19. row_group->id = id;
  20. row_group->role = ax::mojom::Role::kRowGroup;
  21. }
  22. void MakeRow(AXNodeData* row, int id, int row_index) {
  23. row->id = id;
  24. row->role = ax::mojom::Role::kRow;
  25. row->AddIntAttribute(ax::mojom::IntAttribute::kTableRowIndex, row_index);
  26. }
  27. void MakeCell(AXNodeData* cell,
  28. int id,
  29. int row_index,
  30. int col_index,
  31. int row_span = 1,
  32. int col_span = 1) {
  33. cell->id = id;
  34. cell->role = ax::mojom::Role::kCell;
  35. cell->AddIntAttribute(ax::mojom::IntAttribute::kTableCellRowIndex, row_index);
  36. cell->AddIntAttribute(ax::mojom::IntAttribute::kTableCellColumnIndex,
  37. col_index);
  38. if (row_span > 1)
  39. cell->AddIntAttribute(ax::mojom::IntAttribute::kTableCellRowSpan, row_span);
  40. if (col_span > 1)
  41. cell->AddIntAttribute(ax::mojom::IntAttribute::kTableCellColumnSpan,
  42. col_span);
  43. }
  44. void MakeColumnHeader(AXNodeData* cell,
  45. int id,
  46. int row_index,
  47. int col_index,
  48. int row_span = 1,
  49. int col_span = 1) {
  50. MakeCell(cell, id, row_index, col_index, row_span, col_span);
  51. cell->role = ax::mojom::Role::kColumnHeader;
  52. }
  53. void MakeRowHeader(AXNodeData* cell,
  54. int id,
  55. int row_index,
  56. int col_index,
  57. int row_span = 1,
  58. int col_span = 1) {
  59. MakeCell(cell, id, row_index, col_index, row_span, col_span);
  60. cell->role = ax::mojom::Role::kRowHeader;
  61. }
  62. } // namespace
  63. // A macro for testing that a absl::optional has both a value and that its value
  64. // is set to a particular expectation.
  65. #define EXPECT_OPTIONAL_EQ(expected, actual) \
  66. EXPECT_TRUE(actual.has_value()); \
  67. if (actual) { \
  68. EXPECT_EQ(expected, actual.value()); \
  69. }
  70. class AXTableInfoTest : public testing::Test {
  71. public:
  72. AXTableInfoTest() {}
  73. AXTableInfoTest(const AXTableInfoTest&) = delete;
  74. AXTableInfoTest& operator=(const AXTableInfoTest&) = delete;
  75. ~AXTableInfoTest() override {}
  76. protected:
  77. AXTableInfo* GetTableInfo(AXTree* tree, AXNode* node) {
  78. return tree->GetTableInfo(node);
  79. }
  80. };
  81. TEST_F(AXTableInfoTest, SimpleTable) {
  82. // Simple 2 x 2 table with 2 column headers in first row, 2 cells in second
  83. // row. The first row is parented by a rowgroup.
  84. AXTreeUpdate initial_state;
  85. initial_state.root_id = 1;
  86. initial_state.nodes.resize(8);
  87. MakeTable(&initial_state.nodes[0], 1, 0, 0);
  88. initial_state.nodes[0].child_ids = {888, 3};
  89. MakeRowGroup(&initial_state.nodes[1], 888);
  90. initial_state.nodes[1].child_ids = {2};
  91. MakeRow(&initial_state.nodes[2], 2, 0);
  92. initial_state.nodes[2].child_ids = {4, 5};
  93. MakeRow(&initial_state.nodes[3], 3, 1);
  94. initial_state.nodes[3].child_ids = {6, 7};
  95. MakeColumnHeader(&initial_state.nodes[4], 4, 0, 0);
  96. MakeColumnHeader(&initial_state.nodes[5], 5, 0, 1);
  97. MakeCell(&initial_state.nodes[6], 6, 1, 0);
  98. MakeCell(&initial_state.nodes[7], 7, 1, 1);
  99. AXTree tree(initial_state);
  100. //
  101. // Low-level: test the AXTableInfo directly.
  102. //
  103. AXTableInfo* table_info = GetTableInfo(&tree, tree.root()->children()[0]);
  104. EXPECT_FALSE(table_info);
  105. table_info = GetTableInfo(&tree, tree.root());
  106. EXPECT_TRUE(table_info);
  107. EXPECT_EQ(2u, table_info->row_count);
  108. EXPECT_EQ(2u, table_info->col_count);
  109. EXPECT_EQ(2U, table_info->row_headers.size());
  110. EXPECT_EQ(0U, table_info->row_headers[0].size());
  111. EXPECT_EQ(0U, table_info->row_headers[1].size());
  112. EXPECT_EQ(2U, table_info->col_headers.size());
  113. EXPECT_EQ(1U, table_info->col_headers[0].size());
  114. EXPECT_EQ(4, table_info->col_headers[0][0]);
  115. EXPECT_EQ(1U, table_info->col_headers[1].size());
  116. EXPECT_EQ(5, table_info->col_headers[1][0]);
  117. EXPECT_EQ(4, table_info->cell_ids[0][0]);
  118. EXPECT_EQ(5, table_info->cell_ids[0][1]);
  119. EXPECT_EQ(6, table_info->cell_ids[1][0]);
  120. EXPECT_EQ(7, table_info->cell_ids[1][1]);
  121. EXPECT_EQ(4U, table_info->unique_cell_ids.size());
  122. EXPECT_EQ(4, table_info->unique_cell_ids[0]);
  123. EXPECT_EQ(5, table_info->unique_cell_ids[1]);
  124. EXPECT_EQ(6, table_info->unique_cell_ids[2]);
  125. EXPECT_EQ(7, table_info->unique_cell_ids[3]);
  126. EXPECT_EQ(0u, table_info->cell_id_to_index[4]);
  127. EXPECT_EQ(1u, table_info->cell_id_to_index[5]);
  128. EXPECT_EQ(2u, table_info->cell_id_to_index[6]);
  129. EXPECT_EQ(3u, table_info->cell_id_to_index[7]);
  130. EXPECT_EQ(2u, table_info->row_nodes.size());
  131. EXPECT_EQ(2, table_info->row_nodes[0]->id());
  132. EXPECT_EQ(3, table_info->row_nodes[1]->id());
  133. #if BUILDFLAG(IS_MAC)
  134. EXPECT_EQ(3U, table_info->extra_mac_nodes.size());
  135. #else
  136. EXPECT_EQ(0U, table_info->extra_mac_nodes.size());
  137. #endif
  138. //
  139. // High-level: Test the helper functions on AXNode.
  140. //
  141. AXNode* table = tree.root();
  142. EXPECT_TRUE(table->IsTable());
  143. EXPECT_FALSE(table->IsTableRow());
  144. EXPECT_FALSE(table->IsTableCellOrHeader());
  145. EXPECT_OPTIONAL_EQ(2, table->GetTableColCount());
  146. EXPECT_OPTIONAL_EQ(2, table->GetTableRowCount());
  147. ASSERT_TRUE(table->GetTableCellFromCoords(0, 0));
  148. EXPECT_EQ(4, table->GetTableCellFromCoords(0, 0)->id());
  149. EXPECT_EQ(5, table->GetTableCellFromCoords(0, 1)->id());
  150. EXPECT_EQ(6, table->GetTableCellFromCoords(1, 0)->id());
  151. EXPECT_EQ(7, table->GetTableCellFromCoords(1, 1)->id());
  152. EXPECT_EQ(nullptr, table->GetTableCellFromCoords(2, 1));
  153. EXPECT_EQ(nullptr, table->GetTableCellFromCoords(1, -1));
  154. EXPECT_EQ(4, table->GetTableCellFromIndex(0)->id());
  155. EXPECT_EQ(5, table->GetTableCellFromIndex(1)->id());
  156. EXPECT_EQ(6, table->GetTableCellFromIndex(2)->id());
  157. EXPECT_EQ(7, table->GetTableCellFromIndex(3)->id());
  158. EXPECT_EQ(nullptr, table->GetTableCellFromIndex(-1));
  159. EXPECT_EQ(nullptr, table->GetTableCellFromIndex(4));
  160. AXNode* row_0 = tree.GetFromId(2);
  161. EXPECT_FALSE(row_0->IsTable());
  162. EXPECT_TRUE(row_0->IsTableRow());
  163. EXPECT_FALSE(row_0->IsTableCellOrHeader());
  164. EXPECT_OPTIONAL_EQ(0, row_0->GetTableRowRowIndex());
  165. AXNode* row_1 = tree.GetFromId(3);
  166. EXPECT_FALSE(row_1->IsTable());
  167. EXPECT_TRUE(row_1->IsTableRow());
  168. EXPECT_FALSE(row_1->IsTableCellOrHeader());
  169. EXPECT_OPTIONAL_EQ(1, row_1->GetTableRowRowIndex());
  170. AXNode* cell_0_0 = tree.GetFromId(4);
  171. EXPECT_FALSE(cell_0_0->IsTable());
  172. EXPECT_FALSE(cell_0_0->IsTableRow());
  173. EXPECT_TRUE(cell_0_0->IsTableCellOrHeader());
  174. EXPECT_OPTIONAL_EQ(0, cell_0_0->GetTableCellIndex());
  175. EXPECT_OPTIONAL_EQ(0, cell_0_0->GetTableCellColIndex());
  176. EXPECT_OPTIONAL_EQ(0, cell_0_0->GetTableCellRowIndex());
  177. EXPECT_OPTIONAL_EQ(1, cell_0_0->GetTableCellColSpan());
  178. EXPECT_OPTIONAL_EQ(1, cell_0_0->GetTableCellRowSpan());
  179. AXNode* cell_1_1 = tree.GetFromId(7);
  180. EXPECT_FALSE(cell_1_1->IsTable());
  181. EXPECT_FALSE(cell_1_1->IsTableRow());
  182. EXPECT_TRUE(cell_1_1->IsTableCellOrHeader());
  183. EXPECT_OPTIONAL_EQ(3, cell_1_1->GetTableCellIndex());
  184. EXPECT_OPTIONAL_EQ(1, cell_1_1->GetTableCellRowIndex());
  185. EXPECT_OPTIONAL_EQ(1, cell_1_1->GetTableCellColSpan());
  186. EXPECT_OPTIONAL_EQ(1, cell_1_1->GetTableCellRowSpan());
  187. std::vector<AXNode*> col_headers;
  188. cell_1_1->GetTableCellColHeaders(&col_headers);
  189. EXPECT_EQ(1U, col_headers.size());
  190. EXPECT_EQ(5, col_headers[0]->id());
  191. std::vector<AXNode*> row_headers;
  192. cell_1_1->GetTableCellRowHeaders(&row_headers);
  193. EXPECT_EQ(0U, row_headers.size());
  194. EXPECT_EQ(2u, table->GetTableRowNodeIds().size());
  195. EXPECT_EQ(2, table->GetTableRowNodeIds()[0]);
  196. EXPECT_EQ(3, table->GetTableRowNodeIds()[1]);
  197. EXPECT_EQ(2u, row_0->GetTableRowNodeIds().size());
  198. EXPECT_EQ(2, row_0->GetTableRowNodeIds()[0]);
  199. EXPECT_EQ(3, row_0->GetTableRowNodeIds()[1]);
  200. EXPECT_EQ(2u, row_1->GetTableRowNodeIds().size());
  201. EXPECT_EQ(2, row_1->GetTableRowNodeIds()[0]);
  202. EXPECT_EQ(3, row_1->GetTableRowNodeIds()[1]);
  203. EXPECT_EQ(2u, cell_0_0->GetTableRowNodeIds().size());
  204. EXPECT_EQ(2, cell_0_0->GetTableRowNodeIds()[0]);
  205. EXPECT_EQ(3, cell_0_0->GetTableRowNodeIds()[1]);
  206. EXPECT_EQ(2u, cell_1_1->GetTableRowNodeIds().size());
  207. EXPECT_EQ(2, cell_1_1->GetTableRowNodeIds()[0]);
  208. EXPECT_EQ(3, cell_1_1->GetTableRowNodeIds()[1]);
  209. }
  210. TEST_F(AXTableInfoTest, ComputedTableSizeIncludesSpans) {
  211. // Simple 2 x 2 table with 2 column headers in first row, 2 cells in second
  212. // row, but two cells have spans, affecting the computed row and column count.
  213. AXTreeUpdate initial_state;
  214. initial_state.root_id = 1;
  215. initial_state.nodes.resize(7);
  216. MakeTable(&initial_state.nodes[0], 1, 0, 0);
  217. initial_state.nodes[0].child_ids = {2, 3};
  218. MakeRow(&initial_state.nodes[1], 2, 0);
  219. initial_state.nodes[1].child_ids = {4, 5};
  220. MakeRow(&initial_state.nodes[2], 3, 1);
  221. initial_state.nodes[2].child_ids = {6, 7};
  222. MakeCell(&initial_state.nodes[3], 4, 0, 0);
  223. MakeCell(&initial_state.nodes[4], 5, 0, 1, 1, 5); // Column span of 5
  224. MakeCell(&initial_state.nodes[5], 6, 1, 0);
  225. MakeCell(&initial_state.nodes[6], 7, 1, 1, 3, 1); // Row span of 3
  226. AXTree tree(initial_state);
  227. AXTableInfo* table_info = GetTableInfo(&tree, tree.root());
  228. EXPECT_EQ(4u, table_info->row_count);
  229. EXPECT_EQ(6u, table_info->col_count);
  230. EXPECT_EQ(2u, table_info->row_nodes.size());
  231. EXPECT_EQ(2, table_info->row_nodes[0]->id());
  232. EXPECT_EQ(3, table_info->row_nodes[1]->id());
  233. }
  234. TEST_F(AXTableInfoTest, AuthorRowAndColumnCountsAreRespected) {
  235. // Simple 1 x 1 table, but the table's authored row and column
  236. // counts imply a larger table (with missing cells).
  237. AXTreeUpdate initial_state;
  238. initial_state.root_id = 1;
  239. initial_state.nodes.resize(3);
  240. MakeTable(&initial_state.nodes[0], 1, 8, 9);
  241. initial_state.nodes[0].child_ids = {2};
  242. MakeRow(&initial_state.nodes[1], 2, 0);
  243. initial_state.nodes[1].child_ids = {3};
  244. MakeCell(&initial_state.nodes[2], 3, 0, 1);
  245. AXTree tree(initial_state);
  246. AXTableInfo* table_info = GetTableInfo(&tree, tree.root());
  247. EXPECT_EQ(8u, table_info->row_count);
  248. EXPECT_EQ(9u, table_info->col_count);
  249. EXPECT_EQ(1u, table_info->row_nodes.size());
  250. EXPECT_EQ(2, table_info->row_nodes[0]->id());
  251. }
  252. TEST_F(AXTableInfoTest, TableInfoRecomputedOnlyWhenTableChanges) {
  253. // Simple 1 x 1 table.
  254. AXTreeUpdate initial_state;
  255. initial_state.root_id = 1;
  256. initial_state.nodes.resize(3);
  257. MakeTable(&initial_state.nodes[0], 1, 0, 0);
  258. initial_state.nodes[0].child_ids = {2};
  259. MakeRow(&initial_state.nodes[1], 2, 0);
  260. initial_state.nodes[1].child_ids = {3};
  261. MakeCell(&initial_state.nodes[2], 3, 0, 0);
  262. AXTree tree(initial_state);
  263. AXTableInfo* table_info = GetTableInfo(&tree, tree.root());
  264. EXPECT_EQ(1u, table_info->row_count);
  265. EXPECT_EQ(1u, table_info->col_count);
  266. // Table info is cached.
  267. AXTableInfo* table_info_2 = GetTableInfo(&tree, tree.root());
  268. EXPECT_EQ(table_info, table_info_2);
  269. // Update the table so that the cell has a span.
  270. AXTreeUpdate update = initial_state;
  271. MakeCell(&update.nodes[2], 3, 0, 0, 1, 2);
  272. EXPECT_TRUE(tree.Unserialize(update));
  273. AXTableInfo* table_info_3 = GetTableInfo(&tree, tree.root());
  274. EXPECT_EQ(1u, table_info_3->row_count);
  275. EXPECT_EQ(2u, table_info_3->col_count);
  276. EXPECT_EQ(1u, table_info->row_nodes.size());
  277. EXPECT_EQ(2, table_info->row_nodes[0]->id());
  278. }
  279. TEST_F(AXTableInfoTest, CellIdsHandlesSpansAndMissingCells) {
  280. // 3 column x 2 row table with spans and missing cells:
  281. //
  282. // +---+---+---+
  283. // | | 5 |
  284. // + 4 +---+---+
  285. // | | 6 |
  286. // +---+---+
  287. AXTreeUpdate initial_state;
  288. initial_state.root_id = 1;
  289. initial_state.nodes.resize(6);
  290. MakeTable(&initial_state.nodes[0], 1, 0, 0);
  291. initial_state.nodes[0].child_ids = {2, 3};
  292. MakeRow(&initial_state.nodes[1], 2, 0);
  293. initial_state.nodes[1].child_ids = {4, 5};
  294. MakeRow(&initial_state.nodes[2], 3, 1);
  295. initial_state.nodes[2].child_ids = {6};
  296. MakeCell(&initial_state.nodes[3], 4, 0, 0, 2, 1); // Row span of 2
  297. MakeCell(&initial_state.nodes[4], 5, 0, 1, 1, 5); // Column span of 2
  298. MakeCell(&initial_state.nodes[5], 6, 1, 1);
  299. AXTree tree(initial_state);
  300. AXTableInfo* table_info = GetTableInfo(&tree, tree.root());
  301. EXPECT_EQ(4, table_info->cell_ids[0][0]);
  302. EXPECT_EQ(5, table_info->cell_ids[0][1]);
  303. EXPECT_EQ(5, table_info->cell_ids[0][1]);
  304. EXPECT_EQ(4, table_info->cell_ids[1][0]);
  305. EXPECT_EQ(6, table_info->cell_ids[1][1]);
  306. EXPECT_EQ(0, table_info->cell_ids[1][2]);
  307. EXPECT_EQ(3U, table_info->unique_cell_ids.size());
  308. EXPECT_EQ(4, table_info->unique_cell_ids[0]);
  309. EXPECT_EQ(5, table_info->unique_cell_ids[1]);
  310. EXPECT_EQ(6, table_info->unique_cell_ids[2]);
  311. EXPECT_EQ(0u, table_info->cell_id_to_index[4]);
  312. EXPECT_EQ(1u, table_info->cell_id_to_index[5]);
  313. EXPECT_EQ(2u, table_info->cell_id_to_index[6]);
  314. EXPECT_EQ(2u, table_info->row_nodes.size());
  315. EXPECT_EQ(2, table_info->row_nodes[0]->id());
  316. EXPECT_EQ(3, table_info->row_nodes[1]->id());
  317. }
  318. TEST_F(AXTableInfoTest, SkipsGenericAndIgnoredNodes) {
  319. // Simple 2 x 2 table with 2 cells in the first row, 2 cells in the second
  320. // row, but with extra divs and ignored nodes in the tree.
  321. //
  322. // 1 Table
  323. // 2 Row
  324. // 3 Ignored
  325. // 4 Generic
  326. // 5 Cell
  327. // 6 Cell
  328. // 7 Ignored
  329. // 8 Row
  330. // 9 Cell
  331. // 10 Cell
  332. AXTreeUpdate initial_state;
  333. initial_state.root_id = 1;
  334. initial_state.nodes.resize(10);
  335. MakeTable(&initial_state.nodes[0], 1, 0, 0);
  336. initial_state.nodes[0].child_ids = {2, 7};
  337. MakeRow(&initial_state.nodes[1], 2, 0);
  338. initial_state.nodes[1].child_ids = {3};
  339. initial_state.nodes[2].id = 3;
  340. initial_state.nodes[2].AddState(ax::mojom::State::kIgnored);
  341. initial_state.nodes[2].child_ids = {4, 6};
  342. initial_state.nodes[3].id = 4;
  343. initial_state.nodes[3].role = ax::mojom::Role::kGenericContainer;
  344. initial_state.nodes[3].child_ids = {5};
  345. MakeCell(&initial_state.nodes[4], 5, 0, 0);
  346. MakeCell(&initial_state.nodes[5], 6, 0, 1);
  347. initial_state.nodes[6].id = 7;
  348. initial_state.nodes[6].AddState(ax::mojom::State::kIgnored);
  349. initial_state.nodes[6].child_ids = {8};
  350. MakeRow(&initial_state.nodes[7], 8, 1);
  351. initial_state.nodes[7].child_ids = {9, 10};
  352. MakeCell(&initial_state.nodes[8], 9, 1, 0);
  353. MakeCell(&initial_state.nodes[9], 10, 1, 1);
  354. AXTree tree(initial_state);
  355. AXTableInfo* table_info = GetTableInfo(&tree, tree.root()->children()[0]);
  356. EXPECT_FALSE(table_info);
  357. table_info = GetTableInfo(&tree, tree.root());
  358. EXPECT_TRUE(table_info);
  359. EXPECT_EQ(2u, table_info->row_count);
  360. EXPECT_EQ(2u, table_info->col_count);
  361. EXPECT_EQ(5, table_info->cell_ids[0][0]);
  362. EXPECT_EQ(6, table_info->cell_ids[0][1]);
  363. EXPECT_EQ(9, table_info->cell_ids[1][0]);
  364. EXPECT_EQ(10, table_info->cell_ids[1][1]);
  365. EXPECT_EQ(2u, table_info->row_nodes.size());
  366. EXPECT_EQ(2, table_info->row_nodes[0]->id());
  367. EXPECT_EQ(8, table_info->row_nodes[1]->id());
  368. }
  369. TEST_F(AXTableInfoTest, HeadersWithSpans) {
  370. // Row and column headers spanning multiple cells.
  371. // In the figure below, 5 and 6 are headers.
  372. //
  373. // +---+---+
  374. // | 5 |
  375. // +---+---+---+
  376. // | | 7 |
  377. // + 6 +---+---+
  378. // | | | 8 |
  379. // +---+ +---+
  380. AXTreeUpdate initial_state;
  381. initial_state.root_id = 1;
  382. initial_state.nodes.resize(8);
  383. MakeTable(&initial_state.nodes[0], 1, 0, 0);
  384. initial_state.nodes[0].child_ids = {2, 3, 4};
  385. MakeRow(&initial_state.nodes[1], 2, 0);
  386. initial_state.nodes[1].child_ids = {5};
  387. MakeRow(&initial_state.nodes[2], 3, 1);
  388. initial_state.nodes[2].child_ids = {6, 7};
  389. MakeRow(&initial_state.nodes[3], 4, 2);
  390. initial_state.nodes[3].child_ids = {8};
  391. MakeColumnHeader(&initial_state.nodes[4], 5, 0, 1, 1, 2);
  392. MakeRowHeader(&initial_state.nodes[5], 6, 1, 0, 2, 1);
  393. MakeCell(&initial_state.nodes[6], 7, 1, 1);
  394. MakeCell(&initial_state.nodes[7], 8, 2, 2);
  395. AXTree tree(initial_state);
  396. AXTableInfo* table_info = GetTableInfo(&tree, tree.root()->children()[0]);
  397. EXPECT_FALSE(table_info);
  398. table_info = GetTableInfo(&tree, tree.root());
  399. EXPECT_TRUE(table_info);
  400. EXPECT_EQ(3U, table_info->row_headers.size());
  401. EXPECT_EQ(0U, table_info->row_headers[0].size());
  402. EXPECT_EQ(1U, table_info->row_headers[1].size());
  403. EXPECT_EQ(6, table_info->row_headers[1][0]);
  404. EXPECT_EQ(1U, table_info->row_headers[1].size());
  405. EXPECT_EQ(6, table_info->row_headers[2][0]);
  406. EXPECT_EQ(3U, table_info->col_headers.size());
  407. EXPECT_EQ(0U, table_info->col_headers[0].size());
  408. EXPECT_EQ(1U, table_info->col_headers[1].size());
  409. EXPECT_EQ(5, table_info->col_headers[1][0]);
  410. EXPECT_EQ(1U, table_info->col_headers[2].size());
  411. EXPECT_EQ(5, table_info->col_headers[2][0]);
  412. EXPECT_EQ(0, table_info->cell_ids[0][0]);
  413. EXPECT_EQ(5, table_info->cell_ids[0][1]);
  414. EXPECT_EQ(5, table_info->cell_ids[0][2]);
  415. EXPECT_EQ(6, table_info->cell_ids[1][0]);
  416. EXPECT_EQ(7, table_info->cell_ids[1][1]);
  417. EXPECT_EQ(0, table_info->cell_ids[1][2]);
  418. EXPECT_EQ(6, table_info->cell_ids[2][0]);
  419. EXPECT_EQ(0, table_info->cell_ids[2][1]);
  420. EXPECT_EQ(8, table_info->cell_ids[2][2]);
  421. EXPECT_EQ(3u, table_info->row_nodes.size());
  422. EXPECT_EQ(2, table_info->row_nodes[0]->id());
  423. EXPECT_EQ(3, table_info->row_nodes[1]->id());
  424. EXPECT_EQ(4, table_info->row_nodes[2]->id());
  425. }
  426. #if BUILDFLAG(IS_MAC)
  427. TEST_F(AXTableInfoTest, ExtraMacNodes) {
  428. // Simple 2 x 2 table with 2 column headers in first row, 2 cells in second
  429. // row.
  430. AXTreeUpdate initial_state;
  431. initial_state.root_id = 1;
  432. initial_state.nodes.resize(7);
  433. MakeTable(&initial_state.nodes[0], 1, 0, 0);
  434. initial_state.nodes[0].child_ids = {2, 3};
  435. MakeRow(&initial_state.nodes[1], 2, 0);
  436. initial_state.nodes[1].child_ids = {4, 5};
  437. MakeRow(&initial_state.nodes[2], 3, 1);
  438. initial_state.nodes[2].child_ids = {6, 7};
  439. MakeColumnHeader(&initial_state.nodes[3], 4, 0, 0);
  440. MakeColumnHeader(&initial_state.nodes[4], 5, 0, 1);
  441. MakeCell(&initial_state.nodes[5], 6, 1, 0);
  442. MakeCell(&initial_state.nodes[6], 7, 1, 1);
  443. AXTree tree(initial_state);
  444. AXTableInfo* table_info = GetTableInfo(&tree, tree.root()->children()[0]);
  445. EXPECT_FALSE(table_info);
  446. table_info = GetTableInfo(&tree, tree.root());
  447. EXPECT_TRUE(table_info);
  448. // We expect 3 extra Mac nodes: two column nodes, and one header node.
  449. EXPECT_EQ(3U, table_info->extra_mac_nodes.size());
  450. // The first column.
  451. AXNode* extra_node_0 = table_info->extra_mac_nodes[0];
  452. EXPECT_EQ(-1, extra_node_0->id());
  453. EXPECT_EQ(1, extra_node_0->parent()->id());
  454. EXPECT_EQ(ax::mojom::Role::kColumn, extra_node_0->GetRole());
  455. EXPECT_EQ(2U, extra_node_0->GetIndexInParent());
  456. EXPECT_EQ(2U, extra_node_0->GetUnignoredIndexInParent());
  457. EXPECT_EQ(0, extra_node_0->GetIntAttribute(
  458. ax::mojom::IntAttribute::kTableColumnIndex));
  459. std::vector<AXNodeID> indirect_child_ids;
  460. EXPECT_EQ(true, extra_node_0->GetIntListAttribute(
  461. ax::mojom::IntListAttribute::kIndirectChildIds,
  462. &indirect_child_ids));
  463. EXPECT_EQ(2U, indirect_child_ids.size());
  464. EXPECT_EQ(4, indirect_child_ids[0]);
  465. EXPECT_EQ(6, indirect_child_ids[1]);
  466. // The second column.
  467. AXNode* extra_node_1 = table_info->extra_mac_nodes[1];
  468. EXPECT_EQ(-2, extra_node_1->id());
  469. EXPECT_EQ(1, extra_node_1->parent()->id());
  470. EXPECT_EQ(ax::mojom::Role::kColumn, extra_node_1->GetRole());
  471. EXPECT_EQ(3U, extra_node_1->GetIndexInParent());
  472. EXPECT_EQ(3U, extra_node_1->GetUnignoredIndexInParent());
  473. EXPECT_EQ(1, extra_node_1->GetIntAttribute(
  474. ax::mojom::IntAttribute::kTableColumnIndex));
  475. indirect_child_ids.clear();
  476. EXPECT_EQ(true, extra_node_1->GetIntListAttribute(
  477. ax::mojom::IntListAttribute::kIndirectChildIds,
  478. &indirect_child_ids));
  479. EXPECT_EQ(2U, indirect_child_ids.size());
  480. EXPECT_EQ(5, indirect_child_ids[0]);
  481. EXPECT_EQ(7, indirect_child_ids[1]);
  482. // The table header container.
  483. AXNode* extra_node_2 = table_info->extra_mac_nodes[2];
  484. EXPECT_EQ(-3, extra_node_2->id());
  485. EXPECT_EQ(1, extra_node_2->parent()->id());
  486. EXPECT_EQ(ax::mojom::Role::kTableHeaderContainer, extra_node_2->GetRole());
  487. EXPECT_EQ(4U, extra_node_2->GetIndexInParent());
  488. EXPECT_EQ(4U, extra_node_2->GetUnignoredIndexInParent());
  489. indirect_child_ids.clear();
  490. EXPECT_EQ(true, extra_node_2->GetIntListAttribute(
  491. ax::mojom::IntListAttribute::kIndirectChildIds,
  492. &indirect_child_ids));
  493. EXPECT_EQ(2U, indirect_child_ids.size());
  494. EXPECT_EQ(4, indirect_child_ids[0]);
  495. EXPECT_EQ(5, indirect_child_ids[1]);
  496. }
  497. #endif
  498. TEST_F(AXTableInfoTest, TableWithNoIndices) {
  499. AXTreeUpdate initial_state;
  500. initial_state.root_id = 1;
  501. initial_state.nodes.resize(7);
  502. initial_state.nodes[0].id = 1;
  503. initial_state.nodes[0].role = ax::mojom::Role::kTable;
  504. initial_state.nodes[0].child_ids = {2, 3};
  505. initial_state.nodes[1].id = 2;
  506. initial_state.nodes[1].role = ax::mojom::Role::kRow;
  507. initial_state.nodes[1].child_ids = {4, 5};
  508. initial_state.nodes[2].id = 3;
  509. initial_state.nodes[2].role = ax::mojom::Role::kRow;
  510. initial_state.nodes[2].child_ids = {6, 7};
  511. initial_state.nodes[3].id = 4;
  512. initial_state.nodes[3].role = ax::mojom::Role::kColumnHeader;
  513. initial_state.nodes[4].id = 5;
  514. initial_state.nodes[4].role = ax::mojom::Role::kColumnHeader;
  515. initial_state.nodes[5].id = 6;
  516. initial_state.nodes[5].role = ax::mojom::Role::kCell;
  517. initial_state.nodes[6].id = 7;
  518. initial_state.nodes[6].role = ax::mojom::Role::kCell;
  519. AXTree tree(initial_state);
  520. AXNode* table = tree.root();
  521. EXPECT_TRUE(table->IsTable());
  522. EXPECT_FALSE(table->IsTableRow());
  523. EXPECT_FALSE(table->IsTableCellOrHeader());
  524. EXPECT_EQ(2, table->GetTableColCount());
  525. EXPECT_EQ(2, table->GetTableRowCount());
  526. EXPECT_EQ(2u, table->GetTableRowNodeIds().size());
  527. EXPECT_EQ(2, table->GetTableRowNodeIds()[0]);
  528. EXPECT_EQ(3, table->GetTableRowNodeIds()[1]);
  529. EXPECT_EQ(4, table->GetTableCellFromCoords(0, 0)->id());
  530. EXPECT_EQ(5, table->GetTableCellFromCoords(0, 1)->id());
  531. EXPECT_EQ(6, table->GetTableCellFromCoords(1, 0)->id());
  532. EXPECT_EQ(7, table->GetTableCellFromCoords(1, 1)->id());
  533. EXPECT_EQ(nullptr, table->GetTableCellFromCoords(2, 1));
  534. EXPECT_EQ(nullptr, table->GetTableCellFromCoords(1, -1));
  535. EXPECT_EQ(4, table->GetTableCellFromIndex(0)->id());
  536. EXPECT_EQ(5, table->GetTableCellFromIndex(1)->id());
  537. EXPECT_EQ(6, table->GetTableCellFromIndex(2)->id());
  538. EXPECT_EQ(7, table->GetTableCellFromIndex(3)->id());
  539. EXPECT_EQ(nullptr, table->GetTableCellFromIndex(-1));
  540. EXPECT_EQ(nullptr, table->GetTableCellFromIndex(4));
  541. AXNode* cell_0_0 = tree.GetFromId(4);
  542. EXPECT_EQ(0, cell_0_0->GetTableCellRowIndex());
  543. EXPECT_EQ(0, cell_0_0->GetTableCellColIndex());
  544. AXNode* cell_0_1 = tree.GetFromId(5);
  545. EXPECT_EQ(0, cell_0_1->GetTableCellRowIndex());
  546. EXPECT_EQ(1, cell_0_1->GetTableCellColIndex());
  547. AXNode* cell_1_0 = tree.GetFromId(6);
  548. EXPECT_EQ(1, cell_1_0->GetTableCellRowIndex());
  549. EXPECT_EQ(0, cell_1_0->GetTableCellColIndex());
  550. AXNode* cell_1_1 = tree.GetFromId(7);
  551. EXPECT_EQ(1, cell_1_1->GetTableCellRowIndex());
  552. EXPECT_EQ(1, cell_1_1->GetTableCellColIndex());
  553. }
  554. TEST_F(AXTableInfoTest, TableWithPartialIndices) {
  555. // Start with a table with no indices.
  556. AXTreeUpdate initial_state;
  557. initial_state.root_id = 1;
  558. initial_state.nodes.resize(7);
  559. initial_state.nodes[0].id = 1;
  560. initial_state.nodes[0].role = ax::mojom::Role::kTable;
  561. initial_state.nodes[0].child_ids = {2, 3};
  562. initial_state.nodes[1].id = 2;
  563. initial_state.nodes[1].role = ax::mojom::Role::kRow;
  564. initial_state.nodes[1].child_ids = {4, 5};
  565. initial_state.nodes[2].id = 3;
  566. initial_state.nodes[2].role = ax::mojom::Role::kRow;
  567. initial_state.nodes[2].child_ids = {6, 7};
  568. initial_state.nodes[3].id = 4;
  569. initial_state.nodes[3].role = ax::mojom::Role::kColumnHeader;
  570. initial_state.nodes[4].id = 5;
  571. initial_state.nodes[4].role = ax::mojom::Role::kColumnHeader;
  572. initial_state.nodes[5].id = 6;
  573. initial_state.nodes[5].role = ax::mojom::Role::kCell;
  574. initial_state.nodes[6].id = 7;
  575. initial_state.nodes[6].role = ax::mojom::Role::kCell;
  576. AXTree tree(initial_state);
  577. AXNode* table = tree.root();
  578. EXPECT_EQ(2, table->GetTableColCount());
  579. EXPECT_EQ(2, table->GetTableRowCount());
  580. AXNode* cell_0_0 = tree.GetFromId(4);
  581. EXPECT_EQ(0, cell_0_0->GetTableCellRowIndex());
  582. EXPECT_EQ(0, cell_0_0->GetTableCellColIndex());
  583. AXNode* cell_0_1 = tree.GetFromId(5);
  584. EXPECT_EQ(0, cell_0_1->GetTableCellRowIndex());
  585. EXPECT_EQ(1, cell_0_1->GetTableCellColIndex());
  586. AXNode* cell_1_0 = tree.GetFromId(6);
  587. EXPECT_EQ(1, cell_1_0->GetTableCellRowIndex());
  588. EXPECT_EQ(0, cell_1_0->GetTableCellColIndex());
  589. AXNode* cell_1_1 = tree.GetFromId(7);
  590. EXPECT_EQ(1, cell_1_1->GetTableCellRowIndex());
  591. EXPECT_EQ(1, cell_1_1->GetTableCellColIndex());
  592. AXTreeUpdate update = initial_state;
  593. update.nodes[0].AddIntAttribute(ax::mojom::IntAttribute::kTableColumnCount,
  594. 5);
  595. update.nodes[0].AddIntAttribute(ax::mojom::IntAttribute::kTableRowCount, 2);
  596. update.nodes[5].AddIntAttribute(ax::mojom::IntAttribute::kTableCellRowIndex,
  597. 2);
  598. update.nodes[5].AddIntAttribute(
  599. ax::mojom::IntAttribute::kTableCellColumnIndex, 0);
  600. update.nodes[6].AddIntAttribute(ax::mojom::IntAttribute::kTableCellRowIndex,
  601. 2);
  602. update.nodes[6].AddIntAttribute(
  603. ax::mojom::IntAttribute::kTableCellColumnIndex, 2);
  604. EXPECT_TRUE(tree.Unserialize(update));
  605. // The largest column index in the table is 2, but the
  606. // table claims it has a column count of 5. That's allowed.
  607. EXPECT_EQ(5, table->GetTableColCount());
  608. // While the table claims it has a row count of 2, the
  609. // last row has an index of 2, so the correct row count is 3.
  610. EXPECT_EQ(3, table->GetTableRowCount());
  611. EXPECT_EQ(2u, table->GetTableRowNodeIds().size());
  612. EXPECT_EQ(2, table->GetTableRowNodeIds()[0]);
  613. EXPECT_EQ(3, table->GetTableRowNodeIds()[1]);
  614. // All of the specified row and cell indices are legal
  615. // so they're respected.
  616. EXPECT_EQ(0, cell_0_0->GetTableCellRowIndex());
  617. EXPECT_EQ(0, cell_0_0->GetTableCellColIndex());
  618. EXPECT_EQ(0, cell_0_1->GetTableCellRowIndex());
  619. EXPECT_EQ(1, cell_0_1->GetTableCellColIndex());
  620. EXPECT_EQ(2, cell_1_0->GetTableCellRowIndex());
  621. EXPECT_EQ(0, cell_1_0->GetTableCellColIndex());
  622. EXPECT_EQ(2, cell_1_1->GetTableCellRowIndex());
  623. EXPECT_EQ(2, cell_1_1->GetTableCellColIndex());
  624. // Fetching cells by coordinates works.
  625. EXPECT_EQ(4, table->GetTableCellFromCoords(0, 0)->id());
  626. EXPECT_EQ(5, table->GetTableCellFromCoords(0, 1)->id());
  627. EXPECT_EQ(6, table->GetTableCellFromCoords(2, 0)->id());
  628. EXPECT_EQ(7, table->GetTableCellFromCoords(2, 2)->id());
  629. EXPECT_EQ(nullptr, table->GetTableCellFromCoords(0, 2));
  630. EXPECT_EQ(nullptr, table->GetTableCellFromCoords(1, 0));
  631. EXPECT_EQ(nullptr, table->GetTableCellFromCoords(1, 1));
  632. EXPECT_EQ(nullptr, table->GetTableCellFromCoords(2, 1));
  633. }
  634. TEST_F(AXTableInfoTest, BadRowIndicesIgnored) {
  635. // The table claims it has two rows and two columns, but
  636. // the cell indices for both the first and second rows
  637. // are for row 2 (zero-based).
  638. //
  639. // The cell indexes for the first row should be
  640. // respected, and for the second row it will get the
  641. // next row index.
  642. AXTreeUpdate initial_state;
  643. initial_state.root_id = 1;
  644. initial_state.nodes.resize(7);
  645. MakeTable(&initial_state.nodes[0], 1, 2, 2);
  646. initial_state.nodes[0].child_ids = {2, 3};
  647. MakeRow(&initial_state.nodes[1], 2, 0);
  648. initial_state.nodes[1].child_ids = {4, 5};
  649. MakeRow(&initial_state.nodes[2], 3, 0);
  650. initial_state.nodes[2].child_ids = {6, 7};
  651. MakeCell(&initial_state.nodes[3], 4, 2, 0);
  652. MakeCell(&initial_state.nodes[4], 5, 2, 1);
  653. MakeCell(&initial_state.nodes[5], 6, 2, 0);
  654. MakeCell(&initial_state.nodes[6], 7, 2, 1);
  655. AXTree tree(initial_state);
  656. AXNode* table = tree.root();
  657. EXPECT_EQ(2, table->GetTableColCount());
  658. EXPECT_EQ(4, table->GetTableRowCount());
  659. EXPECT_EQ(2u, table->GetTableRowNodeIds().size());
  660. EXPECT_EQ(2, table->GetTableRowNodeIds()[0]);
  661. EXPECT_EQ(3, table->GetTableRowNodeIds()[1]);
  662. AXNode* cell_id_4 = tree.GetFromId(4);
  663. EXPECT_EQ(2, cell_id_4->GetTableCellRowIndex());
  664. EXPECT_EQ(0, cell_id_4->GetTableCellColIndex());
  665. AXNode* cell_id_5 = tree.GetFromId(5);
  666. EXPECT_EQ(2, cell_id_5->GetTableCellRowIndex());
  667. EXPECT_EQ(1, cell_id_5->GetTableCellColIndex());
  668. AXNode* cell_id_6 = tree.GetFromId(6);
  669. EXPECT_EQ(3, cell_id_6->GetTableCellRowIndex());
  670. EXPECT_EQ(0, cell_id_6->GetTableCellColIndex());
  671. AXNode* cell_id_7 = tree.GetFromId(7);
  672. EXPECT_EQ(3, cell_id_7->GetTableCellRowIndex());
  673. EXPECT_EQ(1, cell_id_7->GetTableCellColIndex());
  674. }
  675. TEST_F(AXTableInfoTest, BadColIndicesIgnored) {
  676. // The table claims it has two rows and two columns, but
  677. // the cell indices for the columns either repeat or
  678. // go backwards.
  679. AXTreeUpdate initial_state;
  680. initial_state.root_id = 1;
  681. initial_state.nodes.resize(7);
  682. MakeTable(&initial_state.nodes[0], 1, 2, 2);
  683. initial_state.nodes[0].child_ids = {2, 3};
  684. MakeRow(&initial_state.nodes[1], 2, 0);
  685. initial_state.nodes[1].child_ids = {4, 5};
  686. MakeRow(&initial_state.nodes[2], 3, 0);
  687. initial_state.nodes[2].child_ids = {6, 7};
  688. MakeCell(&initial_state.nodes[3], 4, 0, 1);
  689. MakeCell(&initial_state.nodes[4], 5, 0, 1);
  690. MakeCell(&initial_state.nodes[5], 6, 1, 2);
  691. MakeCell(&initial_state.nodes[6], 7, 1, 1);
  692. AXTree tree(initial_state);
  693. AXNode* table = tree.root();
  694. EXPECT_EQ(4, table->GetTableColCount());
  695. EXPECT_EQ(2, table->GetTableRowCount());
  696. EXPECT_EQ(2u, table->GetTableRowNodeIds().size());
  697. EXPECT_EQ(2, table->GetTableRowNodeIds()[0]);
  698. EXPECT_EQ(3, table->GetTableRowNodeIds()[1]);
  699. AXNode* cell_id_4 = tree.GetFromId(4);
  700. EXPECT_EQ(0, cell_id_4->GetTableCellRowIndex());
  701. EXPECT_EQ(1, cell_id_4->GetTableCellColIndex());
  702. AXNode* cell_id_5 = tree.GetFromId(5);
  703. EXPECT_EQ(0, cell_id_5->GetTableCellRowIndex());
  704. EXPECT_EQ(2, cell_id_5->GetTableCellColIndex());
  705. AXNode* cell_id_6 = tree.GetFromId(6);
  706. EXPECT_EQ(1, cell_id_6->GetTableCellRowIndex());
  707. EXPECT_EQ(2, cell_id_6->GetTableCellColIndex());
  708. AXNode* cell_id_7 = tree.GetFromId(7);
  709. EXPECT_EQ(1, cell_id_7->GetTableCellRowIndex());
  710. EXPECT_EQ(3, cell_id_7->GetTableCellColIndex());
  711. }
  712. TEST_F(AXTableInfoTest, AriaIndicesInferred) {
  713. // Note that ARIA indices are 1-based, whereas the rest of
  714. // the table indices are zero-based.
  715. AXTreeUpdate initial_state;
  716. initial_state.root_id = 1;
  717. initial_state.nodes.resize(13);
  718. MakeTable(&initial_state.nodes[0], 1, 3, 3);
  719. initial_state.nodes[0].AddIntAttribute(ax::mojom::IntAttribute::kAriaRowCount,
  720. 5);
  721. initial_state.nodes[0].AddIntAttribute(
  722. ax::mojom::IntAttribute::kAriaColumnCount, 5);
  723. initial_state.nodes[0].child_ids = {2, 3, 4};
  724. MakeRow(&initial_state.nodes[1], 2, 0);
  725. initial_state.nodes[1].child_ids = {5, 6, 7};
  726. MakeRow(&initial_state.nodes[2], 3, 1);
  727. initial_state.nodes[2].AddIntAttribute(
  728. ax::mojom::IntAttribute::kAriaCellRowIndex, 4);
  729. initial_state.nodes[2].child_ids = {8, 9, 10};
  730. MakeRow(&initial_state.nodes[3], 4, 2);
  731. initial_state.nodes[3].AddIntAttribute(
  732. ax::mojom::IntAttribute::kAriaCellRowIndex, 4);
  733. initial_state.nodes[3].child_ids = {11, 12, 13};
  734. MakeCell(&initial_state.nodes[4], 5, 0, 0);
  735. initial_state.nodes[4].AddIntAttribute(
  736. ax::mojom::IntAttribute::kAriaCellRowIndex, 2);
  737. initial_state.nodes[4].AddIntAttribute(
  738. ax::mojom::IntAttribute::kAriaCellColumnIndex, 2);
  739. MakeCell(&initial_state.nodes[5], 6, 0, 1);
  740. MakeCell(&initial_state.nodes[6], 7, 0, 2);
  741. MakeCell(&initial_state.nodes[7], 8, 1, 0);
  742. MakeCell(&initial_state.nodes[8], 9, 1, 1);
  743. MakeCell(&initial_state.nodes[9], 10, 1, 2);
  744. MakeCell(&initial_state.nodes[10], 11, 2, 0);
  745. initial_state.nodes[10].AddIntAttribute(
  746. ax::mojom::IntAttribute::kAriaCellColumnIndex, 3);
  747. MakeCell(&initial_state.nodes[11], 12, 2, 1);
  748. initial_state.nodes[11].AddIntAttribute(
  749. ax::mojom::IntAttribute::kAriaCellColumnIndex, 2);
  750. MakeCell(&initial_state.nodes[12], 13, 2, 2);
  751. initial_state.nodes[12].AddIntAttribute(
  752. ax::mojom::IntAttribute::kAriaCellColumnIndex, 1);
  753. AXTree tree(initial_state);
  754. AXNode* table = tree.root();
  755. EXPECT_EQ(5, table->GetTableAriaColCount());
  756. EXPECT_EQ(5, table->GetTableAriaRowCount());
  757. EXPECT_EQ(3u, table->GetTableRowNodeIds().size());
  758. EXPECT_EQ(2, table->GetTableRowNodeIds()[0]);
  759. EXPECT_EQ(3, table->GetTableRowNodeIds()[1]);
  760. EXPECT_EQ(4, table->GetTableRowNodeIds()[2]);
  761. // The first row has the first cell ARIA row and column index
  762. // specified as (2, 2). The rest of the row is inferred.
  763. AXNode* cell_0_0 = tree.GetFromId(5);
  764. EXPECT_EQ(2, cell_0_0->GetTableCellAriaRowIndex());
  765. EXPECT_EQ(2, cell_0_0->GetTableCellAriaColIndex());
  766. AXNode* cell_0_1 = tree.GetFromId(6);
  767. EXPECT_EQ(2, cell_0_1->GetTableCellAriaRowIndex());
  768. EXPECT_EQ(3, cell_0_1->GetTableCellAriaColIndex());
  769. AXNode* cell_0_2 = tree.GetFromId(7);
  770. EXPECT_EQ(2, cell_0_2->GetTableCellAriaRowIndex());
  771. EXPECT_EQ(4, cell_0_2->GetTableCellAriaColIndex());
  772. // The next row has the ARIA row index set to 4 on the row
  773. // element. The rest is inferred.
  774. AXNode* cell_1_0 = tree.GetFromId(8);
  775. EXPECT_EQ(4, cell_1_0->GetTableCellAriaRowIndex());
  776. EXPECT_EQ(1, cell_1_0->GetTableCellAriaColIndex());
  777. AXNode* cell_1_1 = tree.GetFromId(9);
  778. EXPECT_EQ(4, cell_1_1->GetTableCellAriaRowIndex());
  779. EXPECT_EQ(2, cell_1_1->GetTableCellAriaColIndex());
  780. AXNode* cell_1_2 = tree.GetFromId(10);
  781. EXPECT_EQ(4, cell_1_2->GetTableCellAriaRowIndex());
  782. EXPECT_EQ(3, cell_1_2->GetTableCellAriaColIndex());
  783. // The last row has the ARIA row index set to 4 again, which is
  784. // illegal so we should get 5. The cells have column indices of
  785. // 3, 2, 1 which is illegal so we ignore the latter two and should
  786. // end up with column indices of 3, 4, 5.
  787. AXNode* cell_2_0 = tree.GetFromId(11);
  788. EXPECT_EQ(5, cell_2_0->GetTableCellAriaRowIndex());
  789. EXPECT_EQ(3, cell_2_0->GetTableCellAriaColIndex());
  790. AXNode* cell_2_1 = tree.GetFromId(12);
  791. EXPECT_EQ(5, cell_2_1->GetTableCellAriaRowIndex());
  792. EXPECT_EQ(4, cell_2_1->GetTableCellAriaColIndex());
  793. AXNode* cell_2_2 = tree.GetFromId(13);
  794. EXPECT_EQ(5, cell_2_2->GetTableCellAriaRowIndex());
  795. EXPECT_EQ(5, cell_2_2->GetTableCellAriaColIndex());
  796. }
  797. TEST_F(AXTableInfoTest, TableChanges) {
  798. // Simple 2 col x 1 row table
  799. AXTreeUpdate initial_state;
  800. initial_state.root_id = 1;
  801. initial_state.nodes.resize(4);
  802. MakeTable(&initial_state.nodes[0], 1, 0, 0);
  803. initial_state.nodes[0].child_ids = {2};
  804. MakeRow(&initial_state.nodes[1], 2, 0);
  805. initial_state.nodes[1].child_ids = {3, 4};
  806. MakeCell(&initial_state.nodes[2], 3, 0, 0);
  807. MakeCell(&initial_state.nodes[3], 4, 0, 1);
  808. AXTree tree(initial_state);
  809. AXTableInfo* table_info = GetTableInfo(&tree, tree.root());
  810. EXPECT_TRUE(table_info);
  811. EXPECT_EQ(1u, table_info->row_count);
  812. EXPECT_EQ(2u, table_info->col_count);
  813. // Update the tree to remove the table role.
  814. AXTreeUpdate update = initial_state;
  815. update.nodes[0].role = ax::mojom::Role::kGroup;
  816. ASSERT_TRUE(tree.Unserialize(update));
  817. table_info = GetTableInfo(&tree, tree.root());
  818. EXPECT_FALSE(table_info);
  819. }
  820. #if BUILDFLAG(IS_MAC)
  821. TEST_F(AXTableInfoTest, ExtraMacNodesChanges) {
  822. // Simple 2 x 2 table with 2 column headers in first row, 2 cells in second
  823. // row.
  824. AXTreeUpdate initial_state;
  825. initial_state.root_id = 1;
  826. initial_state.nodes.resize(7);
  827. MakeTable(&initial_state.nodes[0], 1, 0, 0);
  828. initial_state.nodes[0].child_ids = {2, 3};
  829. MakeRow(&initial_state.nodes[1], 2, 0);
  830. initial_state.nodes[1].child_ids = {4, 5};
  831. MakeRow(&initial_state.nodes[2], 3, 1);
  832. initial_state.nodes[2].child_ids = {6, 7};
  833. MakeColumnHeader(&initial_state.nodes[3], 4, 0, 0);
  834. MakeColumnHeader(&initial_state.nodes[4], 5, 0, 1);
  835. MakeCell(&initial_state.nodes[5], 6, 1, 0);
  836. MakeCell(&initial_state.nodes[6], 7, 1, 1);
  837. AXTree tree(initial_state);
  838. AXTableInfo* table_info = GetTableInfo(&tree, tree.root());
  839. ASSERT_NE(nullptr, table_info);
  840. // We expect 3 extra Mac nodes: two column nodes, and one header node.
  841. ASSERT_EQ(3U, table_info->extra_mac_nodes.size());
  842. // Hide the first row. The number of extra Mac nodes should remain the same,
  843. // but their data should change.
  844. AXTreeUpdate update1;
  845. update1.nodes.resize(1);
  846. MakeRow(&update1.nodes[0], 2, 0);
  847. update1.nodes[0].AddState(ax::mojom::State::kIgnored);
  848. update1.nodes[0].child_ids = {4, 5};
  849. ASSERT_TRUE(tree.Unserialize(update1));
  850. table_info = GetTableInfo(&tree, tree.root());
  851. ASSERT_EQ(3U, table_info->extra_mac_nodes.size());
  852. {
  853. // The first column.
  854. AXNode* extra_node_0 = table_info->extra_mac_nodes[0];
  855. EXPECT_EQ(-4, extra_node_0->id());
  856. EXPECT_EQ(1, extra_node_0->parent()->id());
  857. EXPECT_EQ(ax::mojom::Role::kColumn, extra_node_0->GetRole());
  858. EXPECT_EQ(2U, extra_node_0->GetIndexInParent());
  859. EXPECT_EQ(3U, extra_node_0->GetUnignoredIndexInParent());
  860. EXPECT_EQ(0, extra_node_0->GetIntAttribute(
  861. ax::mojom::IntAttribute::kTableColumnIndex));
  862. std::vector<AXNodeID> indirect_child_ids;
  863. EXPECT_EQ(true, extra_node_0->GetIntListAttribute(
  864. ax::mojom::IntListAttribute::kIndirectChildIds,
  865. &indirect_child_ids));
  866. EXPECT_EQ(1U, indirect_child_ids.size());
  867. EXPECT_EQ(6, indirect_child_ids[0]);
  868. // The second column.
  869. AXNode* extra_node_1 = table_info->extra_mac_nodes[1];
  870. EXPECT_EQ(-5, extra_node_1->id());
  871. EXPECT_EQ(1, extra_node_1->parent()->id());
  872. EXPECT_EQ(ax::mojom::Role::kColumn, extra_node_1->GetRole());
  873. EXPECT_EQ(3U, extra_node_1->GetIndexInParent());
  874. EXPECT_EQ(4U, extra_node_1->GetUnignoredIndexInParent());
  875. EXPECT_EQ(1, extra_node_1->GetIntAttribute(
  876. ax::mojom::IntAttribute::kTableColumnIndex));
  877. indirect_child_ids.clear();
  878. EXPECT_EQ(true, extra_node_1->GetIntListAttribute(
  879. ax::mojom::IntListAttribute::kIndirectChildIds,
  880. &indirect_child_ids));
  881. EXPECT_EQ(1U, indirect_child_ids.size());
  882. EXPECT_EQ(7, indirect_child_ids[0]);
  883. // The table header container.
  884. AXNode* extra_node_2 = table_info->extra_mac_nodes[2];
  885. EXPECT_EQ(-6, extra_node_2->id());
  886. EXPECT_EQ(1, extra_node_2->parent()->id());
  887. EXPECT_EQ(ax::mojom::Role::kTableHeaderContainer, extra_node_2->GetRole());
  888. EXPECT_EQ(4U, extra_node_2->GetIndexInParent());
  889. EXPECT_EQ(5U, extra_node_2->GetUnignoredIndexInParent());
  890. indirect_child_ids.clear();
  891. EXPECT_EQ(true, extra_node_2->GetIntListAttribute(
  892. ax::mojom::IntListAttribute::kIndirectChildIds,
  893. &indirect_child_ids));
  894. EXPECT_EQ(0U, indirect_child_ids.size());
  895. }
  896. // Delete the first row. Again, the number of extra Mac nodes should remain
  897. // the same, but their data should change.
  898. AXTreeUpdate update2;
  899. update2.node_id_to_clear = 2;
  900. update2.nodes.resize(1);
  901. MakeTable(&update2.nodes[0], 1, 0, 0);
  902. update2.nodes[0].child_ids = {3};
  903. ASSERT_TRUE(tree.Unserialize(update2));
  904. table_info = GetTableInfo(&tree, tree.root());
  905. ASSERT_EQ(3U, table_info->extra_mac_nodes.size());
  906. {
  907. // The first column.
  908. AXNode* extra_node_0 = table_info->extra_mac_nodes[0];
  909. EXPECT_EQ(-7, extra_node_0->id());
  910. EXPECT_EQ(1, extra_node_0->parent()->id());
  911. EXPECT_EQ(ax::mojom::Role::kColumn, extra_node_0->GetRole());
  912. EXPECT_EQ(1U, extra_node_0->GetIndexInParent());
  913. EXPECT_EQ(1U, extra_node_0->GetUnignoredIndexInParent());
  914. EXPECT_EQ(0, extra_node_0->GetIntAttribute(
  915. ax::mojom::IntAttribute::kTableColumnIndex));
  916. std::vector<AXNodeID> indirect_child_ids;
  917. EXPECT_EQ(true, extra_node_0->GetIntListAttribute(
  918. ax::mojom::IntListAttribute::kIndirectChildIds,
  919. &indirect_child_ids));
  920. EXPECT_EQ(1U, indirect_child_ids.size());
  921. EXPECT_EQ(6, indirect_child_ids[0]);
  922. // The second column.
  923. AXNode* extra_node_1 = table_info->extra_mac_nodes[1];
  924. EXPECT_EQ(-8, extra_node_1->id());
  925. EXPECT_EQ(1, extra_node_1->parent()->id());
  926. EXPECT_EQ(ax::mojom::Role::kColumn, extra_node_1->GetRole());
  927. EXPECT_EQ(2U, extra_node_1->GetIndexInParent());
  928. EXPECT_EQ(2U, extra_node_1->GetUnignoredIndexInParent());
  929. EXPECT_EQ(1, extra_node_1->GetIntAttribute(
  930. ax::mojom::IntAttribute::kTableColumnIndex));
  931. indirect_child_ids.clear();
  932. EXPECT_EQ(true, extra_node_1->GetIntListAttribute(
  933. ax::mojom::IntListAttribute::kIndirectChildIds,
  934. &indirect_child_ids));
  935. EXPECT_EQ(1U, indirect_child_ids.size());
  936. EXPECT_EQ(7, indirect_child_ids[0]);
  937. // The table header container.
  938. AXNode* extra_node_2 = table_info->extra_mac_nodes[2];
  939. EXPECT_EQ(-9, extra_node_2->id());
  940. EXPECT_EQ(1, extra_node_2->parent()->id());
  941. EXPECT_EQ(ax::mojom::Role::kTableHeaderContainer, extra_node_2->GetRole());
  942. EXPECT_EQ(3U, extra_node_2->GetIndexInParent());
  943. EXPECT_EQ(3U, extra_node_2->GetUnignoredIndexInParent());
  944. indirect_child_ids.clear();
  945. EXPECT_EQ(true, extra_node_2->GetIntListAttribute(
  946. ax::mojom::IntListAttribute::kIndirectChildIds,
  947. &indirect_child_ids));
  948. EXPECT_EQ(0U, indirect_child_ids.size());
  949. }
  950. }
  951. #endif
  952. TEST_F(AXTableInfoTest, RowColumnSpanChanges) {
  953. // Simple 2 col x 1 row table
  954. AXTreeUpdate update;
  955. update.root_id = 1;
  956. update.nodes.resize(4);
  957. MakeTable(&update.nodes[0], 1, 0, 0);
  958. update.nodes[0].child_ids = {2};
  959. MakeRow(&update.nodes[1], 2, 0);
  960. update.nodes[1].child_ids = {3, 10};
  961. MakeCell(&update.nodes[2], 3, 0, 0);
  962. MakeCell(&update.nodes[3], 10, 0, 1);
  963. AXTree tree(update);
  964. AXTableInfo* table_info = GetTableInfo(&tree, tree.root());
  965. ASSERT_TRUE(table_info);
  966. EXPECT_EQ(1u, table_info->row_count);
  967. EXPECT_EQ(2u, table_info->col_count);
  968. EXPECT_EQ("|3 |10|\n", table_info->ToString());
  969. // Add a row to the table.
  970. update.nodes.resize(6);
  971. update.nodes[0].child_ids = {2, 4};
  972. MakeRow(&update.nodes[4], 4, 0);
  973. update.nodes[4].child_ids = {5};
  974. MakeCell(&update.nodes[5], 5, -1, -1);
  975. tree.Unserialize(update);
  976. table_info = GetTableInfo(&tree, tree.root());
  977. ASSERT_TRUE(table_info);
  978. EXPECT_EQ(2u, table_info->row_count);
  979. EXPECT_EQ(2u, table_info->col_count);
  980. EXPECT_EQ(
  981. "|3 |10|\n"
  982. "|5 |0 |\n",
  983. table_info->ToString());
  984. // Add a row to the middle of the table, with a span. Intentionally omit other
  985. // rows from the update.
  986. update.nodes.resize(3);
  987. update.nodes[0].child_ids = {2, 6, 4};
  988. MakeRow(&update.nodes[1], 6, 0);
  989. update.nodes[1].child_ids = {7};
  990. MakeCell(&update.nodes[2], 7, -1, -1, 1, 2);
  991. tree.Unserialize(update);
  992. table_info = GetTableInfo(&tree, tree.root());
  993. ASSERT_TRUE(table_info);
  994. EXPECT_EQ(3u, table_info->row_count);
  995. EXPECT_EQ(2u, table_info->col_count);
  996. EXPECT_EQ(
  997. "|3 |10|\n"
  998. "|7 |7 |\n"
  999. "|5 |0 |\n",
  1000. table_info->ToString());
  1001. // Add a row to the end of the table, with a span. Intentionally omit other
  1002. // rows from the update.
  1003. update.nodes.resize(3);
  1004. update.nodes[0].child_ids = {2, 6, 4, 8};
  1005. MakeRow(&update.nodes[1], 8, 0);
  1006. update.nodes[1].child_ids = {9};
  1007. MakeCell(&update.nodes[2], 9, -1, -1, 2, 3);
  1008. tree.Unserialize(update);
  1009. table_info = GetTableInfo(&tree, tree.root());
  1010. ASSERT_TRUE(table_info);
  1011. EXPECT_EQ(5u, table_info->row_count);
  1012. EXPECT_EQ(3u, table_info->col_count);
  1013. EXPECT_EQ(
  1014. "|3 |10|0 |\n"
  1015. "|7 |7 |0 |\n"
  1016. "|5 |0 |0 |\n"
  1017. "|9 |9 |9 |\n"
  1018. "|9 |9 |9 |\n",
  1019. table_info->ToString());
  1020. // Finally, delete a few rows.
  1021. update.nodes.resize(1);
  1022. update.nodes[0].child_ids = {6, 8};
  1023. tree.Unserialize(update);
  1024. table_info = GetTableInfo(&tree, tree.root());
  1025. ASSERT_TRUE(table_info);
  1026. EXPECT_EQ(3u, table_info->row_count);
  1027. EXPECT_EQ(3u, table_info->col_count);
  1028. EXPECT_EQ(
  1029. "|7|7|0|\n"
  1030. "|9|9|9|\n"
  1031. "|9|9|9|\n",
  1032. table_info->ToString());
  1033. }
  1034. } // namespace ui