summaryrefslogtreecommitdiff
path: root/chromium/ui/accessibility/platform/ax_platform_node_base_unittest.cc
blob: 4072a27b660b70f0290862c84b4295848178c17c (plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/accessibility/platform/ax_platform_node_base.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/accessibility/platform/ax_platform_node_unittest.h"
#include "ui/accessibility/platform/test_ax_node_wrapper.h"

namespace ui {
namespace {

void MakeStaticText(AXNodeData* node, int id, const std::string& text) {
  node->id = id;
  node->role = ax::mojom::Role::kStaticText;
  node->SetName(text);
}

void MakeGroup(AXNodeData* node, int id, std::vector<int> child_ids) {
  node->id = id;
  node->role = ax::mojom::Role::kGroup;
  node->child_ids = child_ids;
}

void SetIsInvisible(AXTree* tree, int id, bool invisible) {
  AXTreeUpdate update;
  update.nodes.resize(1);
  update.nodes[0] = tree->GetFromId(id)->data();
  if (invisible)
    update.nodes[0].AddState(ax::mojom::State::kInvisible);
  else
    update.nodes[0].RemoveState(ax::mojom::State::kInvisible);
  tree->Unserialize(update);
}

void SetRole(AXTree* tree, int id, ax::mojom::Role role) {
  AXTreeUpdate update;
  update.nodes.resize(1);
  update.nodes[0] = tree->GetFromId(id)->data();
  update.nodes[0].role = role;
  tree->Unserialize(update);
}

}  // namespace

TEST_F(AXPlatformNodeTest, GetHypertext) {
  AXTreeUpdate update;

  // RootWebArea #1
  // ++++StaticText "text1" #2
  // ++++StaticText "text2" #3
  // ++++StaticText "text3" #4

  update.root_id = 1;
  update.nodes.resize(4);

  update.nodes[0].id = 1;
  update.nodes[0].role = ax::mojom::Role::kRootWebArea;
  update.nodes[0].child_ids = {2, 3, 4};

  MakeStaticText(&update.nodes[1], 2, "text1");
  MakeStaticText(&update.nodes[2], 3, "text2");
  MakeStaticText(&update.nodes[3], 4, "text3");

  Init(update);
  AXTree& tree = *GetTree();

  // Set an AXMode on the AXPlatformNode as some platforms (auralinux) use it to
  // determine if it should enable accessibility.
  testing::ScopedAxModeSetter ax_mode_setter(kAXModeComplete);

  AXPlatformNodeBase* root = static_cast<AXPlatformNodeBase*>(
      TestAXNodeWrapper::GetOrCreate(&tree, tree.root())->ax_platform_node());

  EXPECT_EQ(root->GetHypertext(), u"text1text2text3");

  AXPlatformNodeBase* text1 = static_cast<AXPlatformNodeBase*>(
      AXPlatformNode::FromNativeViewAccessible(root->ChildAtIndex(0)));
  EXPECT_EQ(text1->GetHypertext(), u"text1");

  AXPlatformNodeBase* text2 = static_cast<AXPlatformNodeBase*>(
      AXPlatformNode::FromNativeViewAccessible(root->ChildAtIndex(1)));
  EXPECT_EQ(text2->GetHypertext(), u"text2");

  AXPlatformNodeBase* text3 = static_cast<AXPlatformNodeBase*>(
      AXPlatformNode::FromNativeViewAccessible(root->ChildAtIndex(2)));
  EXPECT_EQ(text3->GetHypertext(), u"text3");
}

TEST_F(AXPlatformNodeTest, GetHypertextIgnoredContainerSiblings) {
  AXTreeUpdate update;

  // RootWebArea #1
  // ++genericContainer IGNORED #2
  // ++++StaticText "text1" #3
  // ++genericContainer IGNORED #4
  // ++++StaticText "text2" #5
  // ++genericContainer IGNORED #6
  // ++++StaticText "text3" #7

  update.root_id = 1;
  update.nodes.resize(7);

  update.nodes[0].id = 1;
  update.nodes[0].role = ax::mojom::Role::kRootWebArea;
  update.nodes[0].child_ids = {2, 4, 6};

  update.nodes[1].id = 2;
  update.nodes[1].child_ids = {3};
  update.nodes[1].role = ax::mojom::Role::kGenericContainer;
  update.nodes[1].AddState(ax::mojom::State::kIgnored);
  MakeStaticText(&update.nodes[2], 3, "text1");

  update.nodes[3].id = 4;
  update.nodes[3].child_ids = {5};
  update.nodes[3].role = ax::mojom::Role::kGenericContainer;
  update.nodes[3].AddState(ax::mojom::State::kIgnored);
  MakeStaticText(&update.nodes[4], 5, "text2");

  update.nodes[5].id = 6;
  update.nodes[5].child_ids = {7};
  update.nodes[5].role = ax::mojom::Role::kGenericContainer;
  update.nodes[5].AddState(ax::mojom::State::kIgnored);
  MakeStaticText(&update.nodes[6], 7, "text3");

  Init(update);

  AXTree& tree = *GetTree();
  // Set an AXMode on the AXPlatformNode as some platforms (auralinux) use it to
  // determine if it should enable accessibility.
  ui::testing::ScopedAxModeSetter ax_mode_setter(kAXModeComplete);

  AXPlatformNodeBase* root = static_cast<AXPlatformNodeBase*>(
      TestAXNodeWrapper::GetOrCreate(&tree, tree.root())->ax_platform_node());

  EXPECT_EQ(root->GetHypertext(), u"text1text2text3");

  AXPlatformNodeBase* text1_ignored_container =
      static_cast<AXPlatformNodeBase*>(
          AXPlatformNode::FromNativeViewAccessible(root->ChildAtIndex(0)));
  EXPECT_EQ(text1_ignored_container->GetHypertext(), u"text1");

  AXPlatformNodeBase* text2_ignored_container =
      static_cast<AXPlatformNodeBase*>(
          AXPlatformNode::FromNativeViewAccessible(root->ChildAtIndex(1)));
  EXPECT_EQ(text2_ignored_container->GetHypertext(), u"text2");

  AXPlatformNodeBase* text3_ignored_container =
      static_cast<AXPlatformNodeBase*>(
          AXPlatformNode::FromNativeViewAccessible(root->ChildAtIndex(2)));
  EXPECT_EQ(text3_ignored_container->GetHypertext(), u"text3");
}

TEST_F(AXPlatformNodeTest, GetTextContentIgnoresInvisibleAndIgnored) {
  AXTreeUpdate update;

  update.root_id = 1;
  update.nodes.resize(6);

  MakeStaticText(&update.nodes[1], 2, "a");
  MakeStaticText(&update.nodes[2], 3, "b");

  MakeStaticText(&update.nodes[4], 5, "d");
  MakeStaticText(&update.nodes[5], 6, "e");

  MakeGroup(&update.nodes[3], 4, {5, 6});
  MakeGroup(&update.nodes[0], 1, {2, 3, 4});

  Init(update);

  AXTree& tree = *GetTree();
  auto* root = static_cast<AXPlatformNodeBase*>(
      TestAXNodeWrapper::GetOrCreate(&tree, tree.root())->ax_platform_node());

  // Set an AXMode on the AXPlatformNode as some platforms (auralinux) use it to
  // determine if it should enable accessibility.
  ui::testing::ScopedAxModeSetter ax_mode_setter(kAXModeComplete);

  EXPECT_EQ(root->GetTextContentUTF16(), u"abde");

  // Setting invisible or ignored on a static text node causes it to be included
  // or excluded from the root node's text content:
  {
    SetIsInvisible(&tree, 2, true);
    EXPECT_EQ(root->GetTextContentUTF16(), u"bde");

    SetIsInvisible(&tree, 2, false);
    EXPECT_EQ(root->GetTextContentUTF16(), u"abde");

    SetRole(&tree, 2, ax::mojom::Role::kNone);
    EXPECT_EQ(root->GetTextContentUTF16(), u"bde");

    SetRole(&tree, 2, ax::mojom::Role::kStaticText);
    EXPECT_EQ(root->GetTextContentUTF16(), u"abde");
  }

  // Setting invisible or ignored on a group node has no effect on the
  // text content:
  {
    SetIsInvisible(&tree, 4, true);
    EXPECT_EQ(root->GetTextContentUTF16(), u"abde");

    SetRole(&tree, 4, ax::mojom::Role::kNone);
    EXPECT_EQ(root->GetTextContentUTF16(), u"abde");
  }
}

TEST_F(AXPlatformNodeTest, TestMenuSelectedItems) {
  ui::testing::ScopedAxModeSetter ax_mode_setter(kAXModeComplete);

  AXNodeData root_data;
  root_data.id = 1;
  root_data.role = ax::mojom::Role::kMenu;

  AXNodeData item_1_data;
  item_1_data.id = 2;
  item_1_data.role = ax::mojom::Role::kMenuItem;
  item_1_data.AddBoolAttribute(ax::mojom::BoolAttribute::kSelected, true);

  AXNodeData item_2_data;
  item_2_data.id = 3;
  item_2_data.role = ax::mojom::Role::kMenuItem;

  root_data.child_ids = {item_1_data.id, item_2_data.id};

  AXTreeUpdate update;
  update.root_id = 1;
  update.nodes = {root_data, item_1_data, item_2_data};
  Init(update);

  AXTree& tree = *GetTree();
  auto* root = static_cast<AXPlatformNodeBase*>(
      TestAXNodeWrapper::GetOrCreate(&tree, tree.root())->ax_platform_node());

  int num = root->GetSelectionCount();
  EXPECT_EQ(num, 1);

  gfx::NativeViewAccessible first_child = root->ChildAtIndex(0);
  AXPlatformNodeBase* first_selected_node = root->GetSelectedItem(0);
  EXPECT_EQ(first_child, first_selected_node->GetNativeViewAccessible());
  EXPECT_EQ(nullptr, root->GetSelectedItem(1));
}

TEST_F(AXPlatformNodeTest, TestSelectedChildren) {
  ui::testing::ScopedAxModeSetter ax_mode_setter(kAXModeComplete);

  AXNodeData root_data;
  root_data.id = 1;
  root_data.role = ax::mojom::Role::kListBox;
  root_data.AddState(ax::mojom::State::kFocusable);
  root_data.child_ids = {2, 3};

  AXNodeData item_1_data;
  item_1_data.id = 2;
  item_1_data.role = ax::mojom::Role::kListBoxOption;
  item_1_data.AddBoolAttribute(ax::mojom::BoolAttribute::kSelected, true);

  AXNodeData item_2_data;
  item_2_data.id = 3;
  item_2_data.role = ax::mojom::Role::kListBoxOption;

  AXTreeUpdate update;
  update.root_id = 1;
  update.nodes = {root_data, item_1_data, item_2_data};
  Init(update);

  AXTree& tree = *GetTree();
  auto* root = static_cast<AXPlatformNodeBase*>(
      TestAXNodeWrapper::GetOrCreate(&tree, tree.root())->ax_platform_node());

  int num = root->GetSelectionCount();
  EXPECT_EQ(num, 1);

  gfx::NativeViewAccessible first_child = root->ChildAtIndex(0);
  AXPlatformNodeBase* first_selected_node = root->GetSelectedItem(0);
  EXPECT_EQ(first_child, first_selected_node->GetNativeViewAccessible());
  EXPECT_EQ(nullptr, root->GetSelectedItem(1));
}

TEST_F(AXPlatformNodeTest, TestSelectedChildrenWithGroup) {
  ui::testing::ScopedAxModeSetter ax_mode_setter(kAXModeComplete);

  AXNodeData root_data;
  root_data.id = 1;
  root_data.role = ax::mojom::Role::kListBox;
  root_data.AddState(ax::mojom::State::kFocusable);
  root_data.AddState(ax::mojom::State::kMultiselectable);
  root_data.child_ids = {2, 3};

  AXNodeData group_1_data;
  group_1_data.id = 2;
  group_1_data.role = ax::mojom::Role::kGroup;
  group_1_data.child_ids = {4, 5};

  AXNodeData group_2_data;
  group_2_data.id = 3;
  group_2_data.role = ax::mojom::Role::kGroup;
  group_2_data.child_ids = {6, 7};

  AXNodeData item_1_data;
  item_1_data.id = 4;
  item_1_data.role = ax::mojom::Role::kListBoxOption;
  item_1_data.AddBoolAttribute(ax::mojom::BoolAttribute::kSelected, true);

  AXNodeData item_2_data;
  item_2_data.id = 5;
  item_2_data.role = ax::mojom::Role::kListBoxOption;

  AXNodeData item_3_data;
  item_3_data.id = 6;
  item_3_data.role = ax::mojom::Role::kListBoxOption;

  AXNodeData item_4_data;
  item_4_data.id = 7;
  item_4_data.role = ax::mojom::Role::kListBoxOption;
  item_4_data.AddBoolAttribute(ax::mojom::BoolAttribute::kSelected, true);

  AXTreeUpdate update;
  update.root_id = 1;
  update.nodes = {root_data,   group_1_data, group_2_data, item_1_data,
                  item_2_data, item_3_data,  item_4_data};
  Init(update);

  AXTree& tree = *GetTree();
  auto* root = static_cast<AXPlatformNodeBase*>(
      TestAXNodeWrapper::GetOrCreate(&tree, tree.root())->ax_platform_node());

  int num = root->GetSelectionCount();
  EXPECT_EQ(num, 2);

  gfx::NativeViewAccessible first_group_child =
      static_cast<AXPlatformNodeBase*>(
          AXPlatformNode::FromNativeViewAccessible(root->ChildAtIndex(0)))
          ->ChildAtIndex(0);
  AXPlatformNodeBase* first_selected_node = root->GetSelectedItem(0);
  EXPECT_EQ(first_group_child, first_selected_node->GetNativeViewAccessible());

  gfx::NativeViewAccessible second_group_child =
      static_cast<AXPlatformNodeBase*>(
          AXPlatformNode::FromNativeViewAccessible(root->ChildAtIndex(1)))
          ->ChildAtIndex(1);
  AXPlatformNodeBase* second_selected_node = root->GetSelectedItem(1);
  EXPECT_EQ(second_group_child,
            second_selected_node->GetNativeViewAccessible());
}

TEST_F(AXPlatformNodeTest, TestSelectedChildrenMixed) {
  ui::testing::ScopedAxModeSetter ax_mode_setter(kAXModeComplete);

  // Build the below tree which is mixed with listBoxOption and group.
  // id=1 listBox FOCUSABLE MULTISELECTABLE (0, 0)-(0, 0) child_ids=2,3,4,9
  // ++id=2 listBoxOption (0, 0)-(0, 0) selected=true
  // ++id=3 group (0, 0)-(0, 0) child_ids=5,6
  // ++++id=5 listBoxOption (0, 0)-(0, 0) selected=true
  // ++++id=6 listBoxOption (0, 0)-(0, 0)
  // ++id=4 group (0, 0)-(0, 0) child_ids=7,8
  // ++++id=7 listBoxOption (0, 0)-(0, 0)
  // ++++id=8 listBoxOption (0, 0)-(0, 0) selected=true
  // ++id=9 listBoxOption (0, 0)-(0, 0) selected=true

  AXNodeData root_data;
  root_data.id = 1;
  root_data.role = ax::mojom::Role::kListBox;
  root_data.AddState(ax::mojom::State::kFocusable);
  root_data.AddState(ax::mojom::State::kMultiselectable);
  root_data.child_ids = {2, 3, 4, 9};

  AXNodeData item_1_data;
  item_1_data.id = 2;
  item_1_data.role = ax::mojom::Role::kListBoxOption;
  item_1_data.AddBoolAttribute(ax::mojom::BoolAttribute::kSelected, true);

  AXNodeData group_1_data;
  group_1_data.id = 3;
  group_1_data.role = ax::mojom::Role::kGroup;
  group_1_data.child_ids = {5, 6};

  AXNodeData item_2_data;
  item_2_data.id = 5;
  item_2_data.role = ax::mojom::Role::kListBoxOption;
  item_2_data.AddBoolAttribute(ax::mojom::BoolAttribute::kSelected, true);

  AXNodeData item_3_data;
  item_3_data.id = 6;
  item_3_data.role = ax::mojom::Role::kListBoxOption;

  AXNodeData group_2_data;
  group_2_data.id = 4;
  group_2_data.role = ax::mojom::Role::kGroup;
  group_2_data.child_ids = {7, 8};

  AXNodeData item_4_data;
  item_4_data.id = 7;
  item_4_data.role = ax::mojom::Role::kListBoxOption;

  AXNodeData item_5_data;
  item_5_data.id = 8;
  item_5_data.role = ax::mojom::Role::kListBoxOption;
  item_5_data.AddBoolAttribute(ax::mojom::BoolAttribute::kSelected, true);

  AXNodeData item_6_data;
  item_6_data.id = 9;
  item_6_data.role = ax::mojom::Role::kListBoxOption;
  item_6_data.AddBoolAttribute(ax::mojom::BoolAttribute::kSelected, true);

  AXTreeUpdate update;
  update.root_id = 1;
  update.nodes = {root_data,   item_1_data, group_1_data,
                  item_2_data, item_3_data, group_2_data,
                  item_4_data, item_5_data, item_6_data};
  Init(update);

  AXTree& tree = *GetTree();
  auto* root = static_cast<AXPlatformNodeBase*>(
      TestAXNodeWrapper::GetOrCreate(&tree, tree.root())->ax_platform_node());

  int num = root->GetSelectionCount();
  EXPECT_EQ(num, 4);

  gfx::NativeViewAccessible first_child = root->ChildAtIndex(0);
  AXPlatformNodeBase* first_selected_node = root->GetSelectedItem(0);
  EXPECT_EQ(first_child, first_selected_node->GetNativeViewAccessible());

  gfx::NativeViewAccessible first_group_child =
      static_cast<AXPlatformNodeBase*>(
          AXPlatformNode::FromNativeViewAccessible(root->ChildAtIndex(1)))
          ->ChildAtIndex(0);
  AXPlatformNodeBase* second_selected_node = root->GetSelectedItem(1);
  EXPECT_EQ(first_group_child, second_selected_node->GetNativeViewAccessible());

  gfx::NativeViewAccessible second_group_child =
      static_cast<AXPlatformNodeBase*>(
          AXPlatformNode::FromNativeViewAccessible(root->ChildAtIndex(2)))
          ->ChildAtIndex(1);
  AXPlatformNodeBase* third_selected_node = root->GetSelectedItem(2);
  EXPECT_EQ(second_group_child, third_selected_node->GetNativeViewAccessible());

  gfx::NativeViewAccessible fourth_child = root->ChildAtIndex(3);
  AXPlatformNodeBase* fourth_selected_node = root->GetSelectedItem(3);
  EXPECT_EQ(fourth_child, fourth_selected_node->GetNativeViewAccessible());
}

TEST_F(AXPlatformNodeTest, CompareTo) {
  // Compare the nodes' logical orders for the following tree. Node name is
  // denoted according to its id (i.e. "n#" is id#). Nodes that have smaller ids
  // are always logically less than nodes with bigger ids.
  //
  //        n1
  //        |
  //      __ n2 ___
  //    /      \    \
  //   n3 _     n8   n9
  //  / \   \         \
  // n4  n5  n6       n10
  //         /
  //        n7
  ui::testing::ScopedAxModeSetter ax_mode_setter(kAXModeComplete);
  AXNodeData node1;
  node1.id = 1;
  node1.role = ax::mojom::Role::kRootWebArea;
  node1.child_ids = {2};

  AXNodeData node2;
  node2.id = 2;
  node2.role = ax::mojom::Role::kStaticText;
  node2.child_ids = {3, 8, 9};

  AXNodeData node3;
  node3.id = 3;
  node3.role = ax::mojom::Role::kStaticText;
  node3.child_ids = {4, 5, 6};

  AXNodeData node4;
  node4.id = 4;
  node4.role = ax::mojom::Role::kStaticText;

  AXNodeData node5;
  node5.id = 5;
  node5.role = ax::mojom::Role::kStaticText;

  AXNodeData node6;
  node6.id = 6;
  node6.role = ax::mojom::Role::kStaticText;
  node6.child_ids = {7};

  AXNodeData node7;
  node7.id = 7;
  node7.role = ax::mojom::Role::kStaticText;

  AXNodeData node8;
  node8.id = 8;
  node8.role = ax::mojom::Role::kStaticText;

  AXNodeData node9;
  node9.id = 9;
  node9.role = ax::mojom::Role::kStaticText;
  node9.child_ids = {10};

  AXNodeData node10;
  node10.id = 10;
  node10.role = ax::mojom::Role::kStaticText;

  AXTreeUpdate update;
  update.root_id = 1;
  update.nodes = {node1, node2, node3, node4, node5,
                  node6, node7, node8, node9, node10};

  Init(update);

  AXTree& tree = *GetTree();
  // Retrieve the nodes in a level-order traversal way.
  auto* n1 = static_cast<AXPlatformNodeBase*>(
      TestAXNodeWrapper::GetOrCreate(&tree, tree.root())->ax_platform_node());
  auto* n2 = static_cast<AXPlatformNodeBase*>(
      AXPlatformNode::FromNativeViewAccessible(n1->ChildAtIndex(0)));
  auto* n3 = static_cast<AXPlatformNodeBase*>(
      AXPlatformNode::FromNativeViewAccessible(n2->ChildAtIndex(0)));
  auto* n8 = static_cast<AXPlatformNodeBase*>(
      AXPlatformNode::FromNativeViewAccessible(n2->ChildAtIndex(1)));
  auto* n9 = static_cast<AXPlatformNodeBase*>(
      AXPlatformNode::FromNativeViewAccessible(n2->ChildAtIndex(2)));
  auto* n4 = static_cast<AXPlatformNodeBase*>(
      AXPlatformNode::FromNativeViewAccessible(n3->ChildAtIndex(0)));
  auto* n5 = static_cast<AXPlatformNodeBase*>(
      AXPlatformNode::FromNativeViewAccessible(n3->ChildAtIndex(1)));
  auto* n6 = static_cast<AXPlatformNodeBase*>(
      AXPlatformNode::FromNativeViewAccessible(n3->ChildAtIndex(2)));
  auto* n10 = static_cast<AXPlatformNodeBase*>(
      AXPlatformNode::FromNativeViewAccessible(n9->ChildAtIndex(0)));
  auto* n7 = static_cast<AXPlatformNodeBase*>(
      AXPlatformNode::FromNativeViewAccessible(n6->ChildAtIndex(0)));

  // Test for two nodes that do not share the same root. They should not be
  // comparable.
  AXPlatformNodeDelegateBase detached_delegate;
  AXPlatformNodeBase* detached_node = static_cast<AXPlatformNodeBase*>(
      AXPlatformNode::Create(&detached_delegate));
  EXPECT_EQ(absl::nullopt, n1->CompareTo(*detached_node));
  detached_node->Destroy();
  detached_node = nullptr;

  // Create a test vector of all the tree nodes arranged in a pre-order
  // traversal way. The node that has a smaller index in the vector should also
  // be logically less (comes before) the nodes with bigger index.
  std::vector<AXPlatformNodeBase*> preorder_tree_nodes = {n1, n2, n3, n4, n5,
                                                          n6, n7, n8, n9, n10};
  // Test through all permutations of lhs/rhs comparisons of nodes from
  // |preorder_tree_nodes|.
  for (auto* lhs : preorder_tree_nodes) {
    for (auto* rhs : preorder_tree_nodes) {
      int expected_result = 0;
      if (lhs->GetData().id < rhs->GetData().id)
        expected_result = -1;
      else if (lhs->GetData().id > rhs->GetData().id)
        expected_result = 1;

      EXPECT_NE(absl::nullopt, lhs->CompareTo(*rhs));
      int actual_result = 0;
      if (lhs->CompareTo(*rhs) < 0)
        actual_result = -1;
      else if (lhs->CompareTo(*rhs) > 0)
        actual_result = 1;

      SCOPED_TRACE(::testing::Message()
                   << "lhs.id=" << base::NumberToString(lhs->GetData().id)
                   << ", rhs.id=" << base::NumberToString(rhs->GetData().id)
                   << ", lhs->CompareTo(*rhs)={actual:"
                   << base::NumberToString(actual_result) << ", expected:"
                   << base::NumberToString(expected_result) << "}");

      EXPECT_EQ(expected_result, actual_result);
    }
  }
}
}  // namespace ui