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 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/accessibility/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_for_test.h"
#include "ui/accessibility/platform/ax_platform_node_unittest.h"
#include "ui/accessibility/platform/test_ax_node_wrapper.h"
#include "ui/accessibility/test_ax_tree_update.h"
using ax::mojom::Role;
using ax::mojom::State;
namespace ui {
namespace {
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) {
// RootWebArea #1
// ++++StaticText "text1" #2
// ++++StaticText "text2" #3
// ++++StaticText "text3" #4
AXNodeData root_data;
root_data.id = 1;
root_data.role = ax::mojom::Role::kRootWebArea;
AXNodeData item1, item2, item3;
item1.id = 2;
item1.role = ax::mojom::Role::kStaticText;
item1.SetName("text1");
item2.id = 3;
item2.role = ax::mojom::Role::kStaticText;
item2.SetName("text2");
item3.id = 4;
item3.role = ax::mojom::Role::kStaticText;
item3.SetName("text3");
root_data.child_ids = {item1.id, item2.id, item3.id};
AXTreeUpdate update;
update.root_id = 1;
update.nodes = {root_data, item1, item2, item3};
AXTree* tree = Init(update);
// Set an AXMode on the AXPlatformNode as some platforms (auralinux) use it to
// determine if it should enable accessibility.
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) {
// RootWebArea #1
// ++genericContainer IGNORED #2
// ++++StaticText "text1" #3
// ++genericContainer IGNORED #4
// ++++StaticText "text2" #5
// ++genericContainer IGNORED #6
// ++++StaticText "text3" #7
AXNodeData root_data;
root_data.id = 1;
root_data.role = ax::mojom::Role::kRootWebArea;
root_data.child_ids = {2, 4, 6};
AXNodeData container1, container2, container3;
container1.id = 2;
container1.role = ax::mojom::Role::kGenericContainer;
container1.AddState(ax::mojom::State::kIgnored);
container1.child_ids = {3};
container2.id = 4;
container2.role = ax::mojom::Role::kGenericContainer;
container2.AddState(ax::mojom::State::kIgnored);
container2.child_ids = {5};
container3.id = 6;
container3.role = ax::mojom::Role::kGenericContainer;
container3.AddState(ax::mojom::State::kIgnored);
container3.child_ids = {7};
AXNodeData item1, item2, item3;
item1.id = 3;
item1.role = ax::mojom::Role::kStaticText;
item1.SetName("text1");
item2.id = 5;
item2.role = ax::mojom::Role::kStaticText;
item2.SetName("text2");
item3.id = 7;
item3.role = ax::mojom::Role::kStaticText;
item3.SetName("text3");
AXTreeUpdate update;
update.root_id = 1;
update.nodes = {root_data, container1, container2, container3,
item1, item2, item3};
AXTree* tree = Init(update);
// Set an AXMode on the AXPlatformNode as some platforms (auralinux) use it to
// determine if it should enable accessibility.
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) {
// kGroup
// ++kStaticText "a"
// ++kStaticText "b"
// ++kGroup
// ++++kStaticText "d"
// ++++kStaticText "e"
AXNodeData root_data;
root_data.id = 1;
root_data.role = ax::mojom::Role::kGroup;
root_data.child_ids = {2, 3, 4};
AXNodeData item1, item2, group1, item3, item4;
item1.id = 2;
item1.role = ax::mojom::Role::kStaticText;
item1.SetName("a");
item2.id = 3;
item2.role = ax::mojom::Role::kStaticText;
item2.SetName("b");
group1.id = 4;
group1.role = ax::mojom::Role::kGroup;
group1.child_ids = {5, 6};
item3.id = 5;
item3.role = ax::mojom::Role::kStaticText;
item3.SetName("d");
item4.id = 6;
item4.role = ax::mojom::Role::kStaticText;
item4.SetName("e");
AXTreeUpdate update;
update.root_id = 1;
update.nodes = {root_data, item1, item2, group1, item3, item4};
AXTree* tree = Init(update);
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.
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) {
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) {
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) {
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) {
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
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.
AXPlatformNodeDelegate detached_delegate;
AXPlatformNode::Pointer detached_node =
AXPlatformNode::Create(detached_delegate);
EXPECT_EQ(std::nullopt,
n1->CompareTo(static_cast<AXPlatformNodeBase&>(*detached_node)));
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(std::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);
}
}
}
TEST_F(AXPlatformNodeTest, HypertextOffsetFromEndpoint) {
// <p>
// <a href="google.com">link</a>
// </p>
//
// kRootWebArea
// ++kParagraph
// ++++kLink
// ++++++kStaticText "link"
// ++++++kStaticText "link#2"
AXNodeData root_data;
root_data.id = 1;
root_data.role = ax::mojom::Role::kRootWebArea;
root_data.child_ids = {2};
AXNodeData container1, link1, item1, item2;
container1.id = 2;
container1.role = ax::mojom::Role::kParagraph;
container1.child_ids = {3};
link1.id = 3;
link1.role = ax::mojom::Role::kLink;
link1.child_ids = {4, 5};
item1.id = 4;
item1.role = ax::mojom::Role::kStaticText;
item1.SetName("link");
item2.id = 5;
item2.role = ax::mojom::Role::kStaticText;
item2.SetName("link#2");
AXTreeUpdate update;
update.root_id = 1;
update.nodes = {root_data, container1, link1, item1, item2};
AXTree* tree = Init(update);
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.
ScopedAXModeSetter ax_mode_setter(kAXModeComplete);
auto* paragraph = static_cast<AXPlatformNodeBase*>(
AXPlatformNode::FromNativeViewAccessible(root->ChildAtIndex(0)));
auto* link = static_cast<AXPlatformNodeBase*>(
AXPlatformNode::FromNativeViewAccessible(paragraph->ChildAtIndex(0)));
auto* static_text = static_cast<AXPlatformNodeBase*>(
AXPlatformNode::FromNativeViewAccessible(link->ChildAtIndex(0)));
auto* static_text2 = static_cast<AXPlatformNodeBase*>(
AXPlatformNode::FromNativeViewAccessible(link->ChildAtIndex(1)));
// End point is a parent, points before/after the link.
{
EXPECT_EQ(link->GetHypertextOffsetFromEndpoint(paragraph, 0), 0);
EXPECT_EQ(link->GetHypertextOffsetFromEndpoint(paragraph, 1), 10);
}
// End point is a parent, points before/after the static texts.
{
EXPECT_EQ(static_text->GetHypertextOffsetFromEndpoint(link, 0), 0);
EXPECT_EQ(static_text->GetHypertextOffsetFromEndpoint(link, 1), 4);
EXPECT_EQ(static_text->GetHypertextOffsetFromEndpoint(link, 2), 4);
EXPECT_EQ(static_text2->GetHypertextOffsetFromEndpoint(link, 0), 0);
EXPECT_EQ(static_text2->GetHypertextOffsetFromEndpoint(link, 1), 0);
EXPECT_EQ(static_text2->GetHypertextOffsetFromEndpoint(link, 2), 6);
}
// End point is a grand parent, points before/after the static text.
{
EXPECT_EQ(static_text->GetHypertextOffsetFromEndpoint(paragraph, 0), 0);
EXPECT_EQ(static_text->GetHypertextOffsetFromEndpoint(paragraph, 1), 4);
}
// End point is |this|, points into |this| text leaf object.
{
EXPECT_EQ(static_text->GetHypertextOffsetFromEndpoint(static_text, 0), 0);
EXPECT_EQ(static_text->GetHypertextOffsetFromEndpoint(static_text, 4), 4);
}
// End point is |this|, points into |this| hypertext object.
{
EXPECT_EQ(link->GetHypertextOffsetFromEndpoint(link, 0), 0);
EXPECT_EQ(link->GetHypertextOffsetFromEndpoint(link, 1), 4);
}
}
} // namespace ui
|