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 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
|
// Copyright 2022 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/automation/automation_tree_manager_owner.h"
#include <memory>
#include "base/memory/raw_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "gin/array_buffer.h"
#include "gin/public/context_holder.h"
#include "gin/public/isolate_holder.h"
#include "gin/v8_initializer.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/accessibility/ax_enum_util.h"
#include "ui/accessibility/ax_tree_id.h"
#include "ui/accessibility/platform/automation/automation_v8_bindings.h"
#include "ui/accessibility/platform/automation/automation_v8_router.h"
#include "ui/gfx/geometry/rect.h"
#include "v8/include/v8-context.h"
#include "v8/include/v8-template.h"
namespace ui {
// Tests will be run against this class which overrides
// AutomationTreeManagerOwner.
class FakeAutomationTreeManagerOwner : public AutomationTreeManagerOwner {
public:
FakeAutomationTreeManagerOwner() = default;
FakeAutomationTreeManagerOwner(const FakeAutomationTreeManagerOwner&) =
delete;
FakeAutomationTreeManagerOwner& operator=(
const FakeAutomationTreeManagerOwner&) = delete;
~FakeAutomationTreeManagerOwner() override = default;
// AutomationTreeManagerOwner:
AutomationV8Bindings* GetAutomationV8Bindings() const override {
return automation_v8_bindings_;
}
void NotifyTreeEventListenersChanged() override {}
// For testing:
void SetAutomationV8Bindings(AutomationV8Bindings* bindings) {
automation_v8_bindings_ = bindings;
}
private:
raw_ptr<AutomationV8Bindings> automation_v8_bindings_ = nullptr;
};
// A skeleton AutomationV8Router implementation for use by a test.
// Starts V8 when constructed. Does not construct a context and will
// fail the test if GetContext() is called.
class FakeAutomationV8Router : public AutomationV8Router {
public:
FakeAutomationV8Router() {
if (!gin::IsolateHolder::Initialized()) {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
gin::V8Initializer::LoadV8Snapshot();
#endif
gin::IsolateHolder::Initialize(
gin::IsolateHolder::kNonStrictMode,
gin::ArrayBufferAllocator::SharedInstance());
}
isolate_holder_ = std::make_unique<gin::IsolateHolder>(
base::SingleThreadTaskRunner::GetCurrentDefault(),
gin::IsolateHolder::kSingleThread,
gin::IsolateHolder::IsolateType::kUtility);
}
FakeAutomationV8Router(const FakeAutomationV8Router&) = delete;
FakeAutomationV8Router& operator=(const FakeAutomationV8Router&) = delete;
~FakeAutomationV8Router() override = default;
// AutomationV8Router:
void ThrowInvalidArgumentsException(bool is_fatal = true) const override {}
v8::Isolate* GetIsolate() const override {
return isolate_holder_->isolate();
}
v8::Local<v8::Context> GetContext() const override {
DCHECK(context_holder_) << "V8 context was not initialized for this test.";
return context_holder_->context();
}
void StartCachingAccessibilityTrees() override {}
void StopCachingAccessibilityTrees() override {}
TreeChangeObserverFilter ParseTreeChangeObserverFilter(
const std::string& filter) const override {
return TreeChangeObserverFilter::kAllTreeChanges;
}
std::string GetMarkerTypeString(ax::mojom::MarkerType type) const override {
return ToString(type);
}
std::string GetFocusedStateString() const override { return "focused"; }
std::string GetOffscreenStateString() const override { return "offscreen"; }
std::string GetLocalizedStringForImageAnnotationStatus(
ax::mojom::ImageAnnotationStatus status) const override {
return ToString(status);
}
std::string GetTreeChangeTypeString(
ax::mojom::Mutation change_type) const override {
return ToString(change_type);
}
std::string GetEventTypeString(
const std::tuple<ax::mojom::Event, AXEventGenerator::Event>& event_type)
const override {
std::string first = ToString(std::get<0>(event_type));
std::string second = ToString(std::get<1>(event_type));
return first + " " + second;
}
void RouteHandlerFunction(const std::string& name,
scoped_refptr<V8HandlerFunctionWrapper>
handler_function_wrapper) override {}
void DispatchEvent(const std::string& event_name,
const base::Value::List& event_args) const override {
if (!notify_event_ && !notify_tree_destroyed_ &&
!notify_get_text_location_) {
return;
}
if (notify_event_ &&
event_name == "automationInternal.onAccessibilityEvent") {
const base::Value::Dict* dict = event_args[0].GetIfDict();
ASSERT_TRUE(dict);
const std::string* event_type_string = dict->FindString("eventType");
ASSERT_TRUE(event_type_string);
notify_event_.Run(*event_type_string);
}
if (notify_tree_destroyed_ &&
event_name == "automationInternal.onAccessibilityTreeDestroyed") {
const std::string* tree_id_str = event_args[0].GetIfString();
ASSERT_TRUE(tree_id_str);
AXTreeID tree_id = AXTreeID::FromString(*tree_id_str);
notify_tree_destroyed_.Run(tree_id);
}
if (notify_get_text_location_ &&
event_name == "automationInternal.onGetTextLocationResult") {
const base::Value::Dict* params = event_args[0].GetIfDict();
ASSERT_TRUE(params);
AXActionData data;
const std::string* tree_id = params->FindString("treeID");
ASSERT_TRUE(tree_id);
data.target_tree_id = AXTreeID::FromString(*tree_id);
std::optional<int> node_id = params->FindInt("nodeID");
ASSERT_TRUE(node_id);
data.target_node_id = *node_id;
std::optional<int> request_id = params->FindInt("requestID");
ASSERT_TRUE(request_id);
data.request_id = *request_id;
std::optional<int> x = params->FindInt("left");
ASSERT_TRUE(x);
std::optional<int> y = params->FindInt("top");
ASSERT_TRUE(y);
std::optional<int> width = params->FindInt("width");
ASSERT_TRUE(width);
std::optional<int> height = params->FindInt("height");
ASSERT_TRUE(height);
std::optional<gfx::Rect> rect = gfx::Rect();
rect->SetRect(*x, *y, *width, *height);
notify_get_text_location_.Run(data, rect);
}
}
// For tests.
void AddEventCallback(
base::RepeatingCallback<void(const std::string&)> callback) {
notify_event_ = std::move(callback);
}
// For tests.
void AddTreeDestroyedCallback(
base::RepeatingCallback<void(const AXTreeID&)> callback) {
notify_tree_destroyed_ = std::move(callback);
}
// For tests.
void AddGetTextLocationResultCallback(
base::RepeatingCallback<void(const AXActionData&,
const std::optional<gfx::Rect>&)> callback) {
notify_get_text_location_ = std::move(callback);
}
private:
std::unique_ptr<gin::IsolateHolder> isolate_holder_;
std::unique_ptr<gin::ContextHolder> context_holder_;
base::RepeatingCallback<void(const std::string&)> notify_event_;
base::RepeatingCallback<void(const AXTreeID&)> notify_tree_destroyed_;
base::RepeatingCallback<void(const AXActionData&,
const std::optional<gfx::Rect>&)>
notify_get_text_location_;
};
// Tests for AutomationTreeManagerOwner.
class AutomationTreeManagerOwnerTest : public testing::Test {
public:
AutomationTreeManagerOwnerTest() = default;
AutomationTreeManagerOwnerTest(const AutomationTreeManagerOwnerTest&) =
delete;
AutomationTreeManagerOwnerTest& operator=(
const AutomationTreeManagerOwnerTest&) = delete;
~AutomationTreeManagerOwnerTest() override = default;
void SetUp() override {
tree_manager_owner_ = std::make_unique<FakeAutomationTreeManagerOwner>();
router_ = std::make_unique<FakeAutomationV8Router>();
bindings_ = std::make_unique<AutomationV8Bindings>(
tree_manager_owner_.get(), router_.get());
tree_manager_owner_->SetAutomationV8Bindings(bindings_.get());
}
void TearDown() override {
tree_manager_owner_->SetAutomationV8Bindings(nullptr);
}
protected:
std::map<AXTreeID, std::unique_ptr<AutomationAXTreeWrapper>>&
GetTreeIDToTreeMap() {
return tree_manager_owner_->tree_id_to_tree_wrapper_map_;
}
void SendAccessibilityEvents(const AXTreeID& tree_id,
const std::vector<AXTreeUpdate>& updates,
const gfx::Point& mouse_location,
const std::vector<AXEvent>& events) {
tree_manager_owner_->DispatchAccessibilityEvents(tree_id, updates,
mouse_location, events);
}
void SendOnTreeDestroyedEvent(const AXTreeID& tree_id) {
tree_manager_owner_->DispatchTreeDestroyedEvent(tree_id);
}
void SendGetTextLocationResult(const AXActionData& data,
const std::optional<gfx::Rect>& rect) {
#if BUILDFLAG(IS_CHROMEOS)
tree_manager_owner_->DispatchGetTextLocationResult(data, rect);
#else
GTEST_FAIL();
#endif // BUILDFLAG(IS_CHROMEOS)
}
bool CallGetFocusInternal(AutomationAXTreeWrapper* top_wrapper,
AutomationAXTreeWrapper** focused_wrapper,
AXNode** focused_node) {
return tree_manager_owner_->GetFocusInternal(top_wrapper, focused_wrapper,
focused_node);
}
gfx::Rect CallComputeGlobalNodeBounds(AutomationAXTreeWrapper* wrapper,
AXNode* node) {
return tree_manager_owner_->ComputeGlobalNodeBounds(wrapper, node);
}
std::vector<AXNode*> CallGetRootsOfChildTree(AXNode* node) {
return tree_manager_owner_->GetRootsOfChildTree(node);
}
void AddEventCallback(
base::RepeatingCallback<void(const std::string&)> callback) {
router_->AddEventCallback(std::move(callback));
}
void AddTreeDestroyedCallback(
base::RepeatingCallback<void(const AXTreeID&)> callback) {
router_->AddTreeDestroyedCallback(std::move(callback));
}
void AddGetTextLocationResultCallback(
base::RepeatingCallback<void(const AXActionData&,
const std::optional<gfx::Rect>&)> callback) {
router_->AddGetTextLocationResultCallback(std::move(callback));
}
private:
base::test::TaskEnvironment task_environment_;
std::unique_ptr<FakeAutomationTreeManagerOwner> tree_manager_owner_;
std::unique_ptr<FakeAutomationV8Router> router_;
std::unique_ptr<AutomationV8Bindings> bindings_;
};
TEST_F(AutomationTreeManagerOwnerTest, GetDesktop) {
EXPECT_TRUE(GetTreeIDToTreeMap().empty());
std::vector<AXTreeUpdate> updates;
updates.emplace_back();
auto& tree_update = updates.back();
auto& tree_data = tree_update.tree_data;
tree_data.tree_id = AXTreeID::CreateNewAXTreeID();
tree_update.root_id = 1;
tree_update.nodes.emplace_back();
auto& node_data = tree_update.nodes.back();
node_data.role = ax::mojom::Role::kDesktop;
node_data.id = 1;
std::vector<AXEvent> events;
SendAccessibilityEvents(tree_data.tree_id, updates, gfx::Point(), events);
ASSERT_EQ(1U, GetTreeIDToTreeMap().size());
AutomationAXTreeWrapper* desktop = GetTreeIDToTreeMap().begin()->second.get();
ASSERT_TRUE(desktop);
EXPECT_TRUE(desktop->IsDesktopTree());
}
TEST_F(AutomationTreeManagerOwnerTest, GetFocusOneTree) {
// A desktop tree with focus on a button.
std::vector<AXTreeUpdate> updates;
updates.emplace_back();
auto& tree_update = updates.back();
tree_update.has_tree_data = true;
tree_update.root_id = 1;
auto& tree_data = tree_update.tree_data;
tree_data.tree_id = AXTreeID::CreateNewAXTreeID();
tree_data.focus_id = 2;
tree_update.nodes.emplace_back();
auto& node_data1 = tree_update.nodes.back();
node_data1.id = 1;
node_data1.role = ax::mojom::Role::kDesktop;
node_data1.child_ids.push_back(2);
tree_update.nodes.emplace_back();
auto& node_data2 = tree_update.nodes.back();
node_data2.id = 2;
node_data2.role = ax::mojom::Role::kButton;
std::vector<AXEvent> events;
SendAccessibilityEvents(tree_data.tree_id, updates, gfx::Point(), events);
ASSERT_EQ(1U, GetTreeIDToTreeMap().size());
AutomationAXTreeWrapper* desktop = GetTreeIDToTreeMap().begin()->second.get();
ASSERT_TRUE(desktop);
AutomationAXTreeWrapper* focused_wrapper = nullptr;
AXNode* focused_node = nullptr;
CallGetFocusInternal(desktop, &focused_wrapper, &focused_node);
ASSERT_TRUE(focused_wrapper);
ASSERT_TRUE(focused_node);
EXPECT_EQ(desktop, focused_wrapper);
EXPECT_EQ(ax::mojom::Role::kButton, focused_node->GetRole());
// Push an update where we change the focus.
focused_wrapper = nullptr;
focused_node = nullptr;
tree_data.focus_id = 1;
SendAccessibilityEvents(tree_data.tree_id, updates, gfx::Point(), events);
CallGetFocusInternal(desktop, &focused_wrapper, &focused_node);
ASSERT_TRUE(focused_wrapper);
ASSERT_TRUE(focused_node);
EXPECT_EQ(desktop, focused_wrapper);
EXPECT_EQ(ax::mojom::Role::kDesktop, focused_node->GetRole());
// Push an update where we change the focus to nothing.
focused_wrapper = nullptr;
focused_node = nullptr;
tree_data.focus_id = 100;
SendAccessibilityEvents(tree_data.tree_id, updates, gfx::Point(), events);
CallGetFocusInternal(desktop, &focused_wrapper, &focused_node);
ASSERT_FALSE(focused_wrapper);
ASSERT_FALSE(focused_node);
}
TEST_F(AutomationTreeManagerOwnerTest,
GetFocusMultipleTreesChildTreeConstruction) {
// Three trees each with a button and link.
std::vector<std::vector<AXTreeUpdate>> updates_list;
for (int i = 0; i < 3; i++) {
std::vector<AXTreeUpdate>& updates = updates_list.emplace_back();
updates.emplace_back();
auto& tree_update = updates.back();
tree_update.has_tree_data = true;
tree_update.root_id = 1;
auto& tree_data = tree_update.tree_data;
// This is a point of inconsistency as the mojo representation allows
// updates from multiple trees.
tree_data.tree_id = AXTreeID::CreateNewAXTreeID();
tree_data.focus_id = 2;
tree_update.nodes.emplace_back();
auto& node_data1 = tree_update.nodes.back();
node_data1.id = 1;
node_data1.role = ax::mojom::Role::kRootWebArea;
node_data1.child_ids.push_back(2);
node_data1.child_ids.push_back(3);
tree_update.nodes.emplace_back();
auto& node_data2 = tree_update.nodes.back();
node_data2.id = 2;
node_data2.role = ax::mojom::Role::kButton;
tree_update.nodes.emplace_back();
auto& node_data3 = tree_update.nodes.back();
node_data3.id = 3;
node_data3.role = ax::mojom::Role::kLink;
}
// Link up the trees so that the first is a parent of the other two using
// child tree id.
AXTreeID tree_0_id = updates_list[0][0].tree_data.tree_id;
AXTreeID tree_1_id = updates_list[1][0].tree_data.tree_id;
AXTreeID tree_2_id = updates_list[2][0].tree_data.tree_id;
updates_list[0][0].nodes[1].AddChildTreeId(tree_1_id);
updates_list[0][0].nodes[2].AddChildTreeId(tree_2_id);
std::vector<AXEvent> empty_events;
for (auto& updates : updates_list) {
SendAccessibilityEvents(updates[0].tree_data.tree_id, updates, gfx::Point(),
empty_events);
}
ASSERT_EQ(3U, GetTreeIDToTreeMap().size());
AutomationAXTreeWrapper* wrapper_0 = GetTreeIDToTreeMap()[tree_0_id].get();
ASSERT_TRUE(wrapper_0);
AutomationAXTreeWrapper* wrapper_1 = GetTreeIDToTreeMap()[tree_1_id].get();
ASSERT_TRUE(wrapper_1);
AutomationAXTreeWrapper* wrapper_2 = GetTreeIDToTreeMap()[tree_2_id].get();
ASSERT_TRUE(wrapper_2);
AutomationAXTreeWrapper* focused_wrapper = nullptr;
AXNode* focused_node = nullptr;
CallGetFocusInternal(wrapper_0, &focused_wrapper, &focused_node);
ASSERT_TRUE(focused_wrapper);
ASSERT_TRUE(focused_node);
EXPECT_EQ(wrapper_1, focused_wrapper);
EXPECT_EQ(tree_1_id, focused_node->tree()->GetAXTreeID());
EXPECT_EQ(ax::mojom::Role::kButton, focused_node->GetRole());
// Push an update where we change the focus.
focused_wrapper = nullptr;
focused_node = nullptr;
// The link in wrapper 0 which has a child tree id pointing to wrapper 2.
updates_list[0][0].tree_data.focus_id = 3;
SendAccessibilityEvents(updates_list[0][0].tree_data.tree_id, updates_list[0],
gfx::Point(), empty_events);
CallGetFocusInternal(wrapper_0, &focused_wrapper, &focused_node);
ASSERT_TRUE(focused_wrapper);
ASSERT_TRUE(focused_node);
EXPECT_EQ(wrapper_2, focused_wrapper);
EXPECT_EQ(tree_2_id, focused_node->tree()->GetAXTreeID());
EXPECT_EQ(ax::mojom::Role::kButton, focused_node->GetRole());
}
TEST_F(AutomationTreeManagerOwnerTest, GetFocusMultipleTreesAppIdConstruction) {
// Three trees each with a button and link.
std::vector<std::vector<AXTreeUpdate>> updates_list;
for (int i = 0; i < 3; i++) {
std::vector<AXTreeUpdate>& updates = updates_list.emplace_back();
updates.emplace_back();
auto& tree_update = updates.back();
tree_update.has_tree_data = true;
tree_update.root_id = 1;
auto& tree_data = tree_update.tree_data;
// This is a point of inconsistency as the mojo representation allows
// updates from ultiple trees.
tree_data.tree_id = AXTreeID::CreateNewAXTreeID();
tree_data.focus_id = 2;
tree_update.nodes.emplace_back();
auto& node_data1 = tree_update.nodes.back();
node_data1.id = 1;
node_data1.role = ax::mojom::Role::kRootWebArea;
node_data1.child_ids.push_back(2);
node_data1.child_ids.push_back(3);
tree_update.nodes.emplace_back();
auto& node_data2 = tree_update.nodes.back();
node_data2.id = 2;
node_data2.role = ax::mojom::Role::kButton;
tree_update.nodes.emplace_back();
auto& node_data3 = tree_update.nodes.back();
node_data3.id = 3;
node_data3.role = ax::mojom::Role::kLink;
}
// Link up the trees so that the first is a parent of the other two using app
// ids.
AXTreeID tree_0_id = updates_list[0][0].tree_data.tree_id;
AXTreeID tree_1_id = updates_list[1][0].tree_data.tree_id;
AXTreeID tree_2_id = updates_list[2][0].tree_data.tree_id;
auto& wrapper0_button_data = updates_list[0][0].nodes[1];
auto& wrapper0_link_data = updates_list[0][0].nodes[2];
auto& wrapper1_link_data = updates_list[1][0].nodes[2];
auto& wrapper2_button_data = updates_list[2][0].nodes[1];
// This construction requires the hosting and client nodes annotate with the
// same app id.
wrapper0_button_data.AddStringAttribute(
ax::mojom::StringAttribute::kChildTreeNodeAppId, "app1");
wrapper1_link_data.AddStringAttribute(ax::mojom::StringAttribute::kAppId,
"app1");
wrapper0_link_data.AddStringAttribute(
ax::mojom::StringAttribute::kChildTreeNodeAppId, "app2");
wrapper2_button_data.AddStringAttribute(ax::mojom::StringAttribute::kAppId,
"app2");
std::vector<AXEvent> empty_events;
for (auto& updates : updates_list) {
SendAccessibilityEvents(updates[0].tree_data.tree_id, updates, gfx::Point(),
empty_events);
}
ASSERT_EQ(3U, GetTreeIDToTreeMap().size());
AutomationAXTreeWrapper* wrapper_0 = GetTreeIDToTreeMap()[tree_0_id].get();
ASSERT_TRUE(wrapper_0);
AutomationAXTreeWrapper* wrapper_1 = GetTreeIDToTreeMap()[tree_1_id].get();
ASSERT_TRUE(wrapper_1);
AutomationAXTreeWrapper* wrapper_2 = GetTreeIDToTreeMap()[tree_2_id].get();
ASSERT_TRUE(wrapper_2);
AutomationAXTreeWrapper* focused_wrapper = nullptr;
AXNode* focused_node = nullptr;
CallGetFocusInternal(wrapper_0, &focused_wrapper, &focused_node);
ASSERT_TRUE(focused_wrapper);
ASSERT_TRUE(focused_node);
EXPECT_EQ(wrapper_1, focused_wrapper);
EXPECT_EQ(tree_1_id, focused_node->tree()->GetAXTreeID());
// This is an interesting inconsistency as this node is technically not in the
// app (which starts at the link in wrapper 1).
EXPECT_EQ(ax::mojom::Role::kButton, focused_node->GetRole());
// Push an update where we change the focus.
focused_wrapper = nullptr;
focused_node = nullptr;
// The link in wrapper 0 which has a child tree id pointing to wrapper 2.
updates_list[0][0].tree_data.focus_id = 3;
SendAccessibilityEvents(updates_list[0][0].tree_data.tree_id, updates_list[0],
gfx::Point(), empty_events);
CallGetFocusInternal(wrapper_0, &focused_wrapper, &focused_node);
ASSERT_TRUE(focused_wrapper);
ASSERT_TRUE(focused_node);
EXPECT_EQ(wrapper_2, focused_wrapper);
EXPECT_EQ(tree_2_id, focused_node->tree()->GetAXTreeID());
EXPECT_EQ(ax::mojom::Role::kButton, focused_node->GetRole());
}
TEST_F(AutomationTreeManagerOwnerTest, GetBoundsAppIdConstruction) {
// two trees each with a button.
std::vector<std::vector<AXTreeUpdate>> updates_list;
for (int i = 0; i < 2; i++) {
std::vector<AXTreeUpdate>& updates = updates_list.emplace_back();
updates.emplace_back();
auto& tree_update = updates.back();
tree_update.has_tree_data = true;
tree_update.root_id = 1;
auto& tree_data = tree_update.tree_data;
tree_data.tree_id = AXTreeID::CreateNewAXTreeID();
tree_update.nodes.emplace_back();
auto& node_data1 = tree_update.nodes.back();
node_data1.id = 1;
node_data1.role =
i == 0 ? ax::mojom::Role::kDesktop : ax::mojom::Role::kRootWebArea;
node_data1.child_ids.push_back(2);
node_data1.relative_bounds.bounds = gfx::RectF(100, 100, 100, 100);
tree_update.nodes.emplace_back();
auto& node_data2 = tree_update.nodes.back();
node_data2.id = 2;
node_data2.role = ax::mojom::Role::kButton;
node_data2.relative_bounds.bounds = gfx::RectF(0, 0, 200, 200);
}
// Link up the trees by app id.
AXTreeID tree_0_id = updates_list[0][0].tree_data.tree_id;
AXTreeID tree_1_id = updates_list[1][0].tree_data.tree_id;
auto& wrapper0_button_data = updates_list[0][0].nodes[1];
auto& wrapper1_button_data = updates_list[1][0].nodes[1];
// This construction requires the hosting and client nodes annotate with the
// same app id.
wrapper0_button_data.AddStringAttribute(
ax::mojom::StringAttribute::kChildTreeNodeAppId, "app1");
wrapper1_button_data.AddStringAttribute(ax::mojom::StringAttribute::kAppId,
"app1");
wrapper0_button_data.AddFloatAttribute(
ax::mojom::FloatAttribute::kChildTreeScale, 2.0);
std::vector<AXEvent> empty_events;
for (auto& updates : updates_list) {
SendAccessibilityEvents(updates[0].tree_data.tree_id, updates, gfx::Point(),
empty_events);
}
ASSERT_EQ(2U, GetTreeIDToTreeMap().size());
AutomationAXTreeWrapper* wrapper_0 = GetTreeIDToTreeMap()[tree_0_id].get();
ASSERT_TRUE(wrapper_0);
AutomationAXTreeWrapper* wrapper_1 = GetTreeIDToTreeMap()[tree_1_id].get();
ASSERT_TRUE(wrapper_1);
AXNode* wrapper1_button = wrapper_1->ax_tree()->GetFromId(2);
ASSERT_TRUE(wrapper1_button);
// The button in wrapper 1 is scaled by .5 (200 * .5). It's root is also
// scaled (100 * .5). In wrapper 0, it is *not* offset by the tree's root
// bounds.
EXPECT_EQ(gfx::Rect(50, 50, 100, 100),
CallComputeGlobalNodeBounds(wrapper_1, wrapper1_button));
}
TEST_F(AutomationTreeManagerOwnerTest, GetBoundsNestedAppIdConstruction) {
// two trees each with a button and a client node.
std::vector<std::vector<AXTreeUpdate>> updates_list;
for (int i = 0; i < 2; i++) {
std::vector<AXTreeUpdate>& updates = updates_list.emplace_back();
updates.emplace_back();
auto& tree_update = updates.back();
tree_update.has_tree_data = true;
tree_update.root_id = 1;
auto& tree_data = tree_update.tree_data;
tree_data.tree_id = AXTreeID::CreateNewAXTreeID();
tree_update.nodes.emplace_back();
auto& node_data1 = tree_update.nodes.back();
node_data1.id = 1;
node_data1.role =
i == 0 ? ax::mojom::Role::kDesktop : ax::mojom::Role::kRootWebArea;
node_data1.child_ids.push_back(2);
node_data1.child_ids.push_back(3);
node_data1.relative_bounds.bounds = gfx::RectF(100, 100, 100, 100);
tree_update.nodes.emplace_back();
auto& node_data2 = tree_update.nodes.back();
node_data2.id = 2;
node_data2.role = ax::mojom::Role::kButton;
node_data2.relative_bounds.bounds = gfx::RectF(0, 0, 200, 200);
tree_update.nodes.emplace_back();
auto& node_data3 = tree_update.nodes.back();
node_data3.id = 3;
node_data3.role = ax::mojom::Role::kClient;
node_data3.relative_bounds.bounds = gfx::RectF(0, 0, 200, 200);
}
// Link up the trees by app id. One button -> child button; client -> child
// root.
AXTreeID tree_0_id = updates_list[0][0].tree_data.tree_id;
AXTreeID tree_1_id = updates_list[1][0].tree_data.tree_id;
auto& wrapper0_button_data = updates_list[0][0].nodes[1];
auto& wrapper0_client_data = updates_list[0][0].nodes[2];
auto& wrapper1_root_data = updates_list[1][0].nodes[0];
auto& wrapper1_button_data = updates_list[1][0].nodes[1];
// This construction requires the hosting and client nodes annotate with the
// same app id.
wrapper0_button_data.AddStringAttribute(
ax::mojom::StringAttribute::kChildTreeNodeAppId, "app1");
wrapper1_button_data.AddStringAttribute(ax::mojom::StringAttribute::kAppId,
"app1");
wrapper0_button_data.AddFloatAttribute(
ax::mojom::FloatAttribute::kChildTreeScale, 2.0);
// Adding this app id should not impact the above bounds computation.
wrapper0_client_data.AddStringAttribute(
ax::mojom::StringAttribute::kChildTreeNodeAppId, "app2");
wrapper1_root_data.AddStringAttribute(ax::mojom::StringAttribute::kAppId,
"app2");
std::vector<AXEvent> empty_events;
for (auto& updates : updates_list) {
SendAccessibilityEvents(updates[0].tree_data.tree_id, updates, gfx::Point(),
empty_events);
}
ASSERT_EQ(2U, GetTreeIDToTreeMap().size());
AutomationAXTreeWrapper* wrapper_0 = GetTreeIDToTreeMap()[tree_0_id].get();
ASSERT_TRUE(wrapper_0);
AutomationAXTreeWrapper* wrapper_1 = GetTreeIDToTreeMap()[tree_1_id].get();
ASSERT_TRUE(wrapper_1);
AXNode* wrapper1_button = wrapper_1->ax_tree()->GetFromId(2);
ASSERT_TRUE(wrapper1_button);
// The button in wrapper 1 is scaled by .5 (200 * .5). It's root is also
// scaled (100 * .5). In wrapper 0, it is *not* offset by the tree's root
// bounds.
EXPECT_EQ(gfx::Rect(50, 50, 100, 100),
CallComputeGlobalNodeBounds(wrapper_1, wrapper1_button));
AXNode* wrapper1_root = wrapper_1->ax_tree()->GetFromId(1);
ASSERT_TRUE(wrapper1_root);
// Similar to the button, but not scaled. This does not cross an app id
// boundary, so is also offset by the parent tree's root (100 + 100).
EXPECT_EQ(gfx::Rect(200, 200, 100, 100),
CallComputeGlobalNodeBounds(wrapper_1, wrapper1_root));
}
TEST_F(AutomationTreeManagerOwnerTest, IgnoredAncestorTrees) {
// Three trees each with a button and link.
std::vector<std::vector<AXTreeUpdate>> updates_list;
for (int i = 0; i < 3; i++) {
std::vector<AXTreeUpdate>& updates = updates_list.emplace_back();
updates.emplace_back();
auto& tree_update = updates.back();
tree_update.has_tree_data = true;
tree_update.root_id = 1;
auto& tree_data = tree_update.tree_data;
// This is a point of inconsistency as the mojo representation allows
// updates from multiple trees.
tree_data.tree_id = AXTreeID::CreateNewAXTreeID();
tree_data.focus_id = 2;
tree_update.nodes.emplace_back();
auto& node_data1 = tree_update.nodes.back();
node_data1.id = 1;
node_data1.role = ax::mojom::Role::kRootWebArea;
node_data1.child_ids.push_back(2);
node_data1.child_ids.push_back(3);
tree_update.nodes.emplace_back();
auto& node_data2 = tree_update.nodes.back();
node_data2.id = 2;
node_data2.role = ax::mojom::Role::kButton;
tree_update.nodes.emplace_back();
auto& node_data3 = tree_update.nodes.back();
node_data3.id = 3;
node_data3.role = ax::mojom::Role::kLink;
}
// Link up the trees so that the first is a parent of the second and the
// second a parent of the third.
AXTreeID tree_0_id = updates_list[0][0].tree_data.tree_id;
AXTreeID tree_1_id = updates_list[1][0].tree_data.tree_id;
AXTreeID tree_2_id = updates_list[2][0].tree_data.tree_id;
updates_list[0][0].nodes[1].AddChildTreeId(tree_1_id);
// Make the hosting node ignored.
updates_list[0][0].nodes[1].AddState(ax::mojom::State::kInvisible);
updates_list[1][0].nodes[1].AddChildTreeId(tree_2_id);
std::vector<AXEvent> empty_events;
for (auto& updates : updates_list) {
SendAccessibilityEvents(updates[0].tree_data.tree_id, updates, gfx::Point(),
empty_events);
}
ASSERT_EQ(3U, GetTreeIDToTreeMap().size());
AutomationAXTreeWrapper* wrapper_0 = GetTreeIDToTreeMap()[tree_0_id].get();
ASSERT_TRUE(wrapper_0);
AutomationAXTreeWrapper* wrapper_1 = GetTreeIDToTreeMap()[tree_1_id].get();
ASSERT_TRUE(wrapper_1);
AutomationAXTreeWrapper* wrapper_2 = GetTreeIDToTreeMap()[tree_2_id].get();
ASSERT_TRUE(wrapper_2);
// The root tree isn't ignored.
EXPECT_FALSE(wrapper_0->IsTreeIgnored());
// However, since the hosting node in |wrapper_0| is ignored, both of the
// descendant trees should be ignored.
EXPECT_TRUE(wrapper_1->IsTreeIgnored());
EXPECT_TRUE(wrapper_2->IsTreeIgnored());
// No longer invisible.
AXNode* button = wrapper_0->ax_tree()->GetFromId(2);
AXNodeData data = button->TakeData();
data.RemoveState(ax::mojom::State::kInvisible);
button->SetData(data);
EXPECT_FALSE(wrapper_0->IsTreeIgnored());
EXPECT_FALSE(wrapper_1->IsTreeIgnored());
EXPECT_FALSE(wrapper_2->IsTreeIgnored());
}
TEST_F(AutomationTreeManagerOwnerTest, GetMultipleChildRootsAppIdConstruction) {
// Two trees each with a button and a client node.
std::vector<std::vector<AXTreeUpdate>> updates_list;
for (int i = 0; i < 2; i++) {
std::vector<AXTreeUpdate>& updates = updates_list.emplace_back();
updates.emplace_back();
auto& tree_update = updates.back();
tree_update.has_tree_data = true;
tree_update.root_id = 1;
auto& tree_data = tree_update.tree_data;
tree_data.tree_id = AXTreeID::CreateNewAXTreeID();
tree_update.nodes.emplace_back();
auto& node_data1 = tree_update.nodes.back();
node_data1.id = 1;
node_data1.role =
i == 0 ? ax::mojom::Role::kDesktop : ax::mojom::Role::kRootWebArea;
node_data1.child_ids.push_back(2);
node_data1.child_ids.push_back(3);
node_data1.relative_bounds.bounds = gfx::RectF(100, 100, 100, 100);
tree_update.nodes.emplace_back();
auto& node_data2 = tree_update.nodes.back();
node_data2.id = 2;
node_data2.role = ax::mojom::Role::kButton;
node_data2.relative_bounds.bounds = gfx::RectF(0, 0, 200, 200);
tree_update.nodes.emplace_back();
auto& node_data3 = tree_update.nodes.back();
node_data3.id = 3;
node_data3.role = ax::mojom::Role::kClient;
node_data3.relative_bounds.bounds = gfx::RectF(0, 0, 200, 200);
}
// Link up the trees by using one app id. Tree 0's client has two children
// from tree 1.
AXTreeID tree_0_id = updates_list[0][0].tree_data.tree_id;
AXTreeID tree_1_id = updates_list[1][0].tree_data.tree_id;
auto& wrapper0_client_data = updates_list[0][0].nodes[2];
auto& wrapper1_button_data = updates_list[1][0].nodes[1];
auto& wrapper1_client_data = updates_list[1][0].nodes[2];
// This construction requires the hosting and client nodes annotate with the
// same app id.
wrapper0_client_data.AddStringAttribute(
ax::mojom::StringAttribute::kChildTreeNodeAppId, "app1");
wrapper1_button_data.AddStringAttribute(ax::mojom::StringAttribute::kAppId,
"app1");
wrapper1_client_data.AddStringAttribute(ax::mojom::StringAttribute::kAppId,
"app1");
std::vector<AXEvent> empty_events;
for (auto& updates : updates_list) {
SendAccessibilityEvents(updates[0].tree_data.tree_id, updates, gfx::Point(),
empty_events);
}
ASSERT_EQ(2U, GetTreeIDToTreeMap().size());
AutomationAXTreeWrapper* wrapper_0 = GetTreeIDToTreeMap()[tree_0_id].get();
ASSERT_TRUE(wrapper_0);
AXNode* wrapper0_client = wrapper_0->ax_tree()->GetFromId(3);
ASSERT_TRUE(wrapper0_client);
std::vector<AXNode*> child_roots = CallGetRootsOfChildTree(wrapper0_client);
EXPECT_EQ(2U, child_roots.size());
EXPECT_EQ(tree_1_id, child_roots[0]->tree()->GetAXTreeID());
EXPECT_EQ(tree_1_id, child_roots[1]->tree()->GetAXTreeID());
EXPECT_EQ(2, child_roots[0]->id());
EXPECT_EQ(3, child_roots[1]->id());
}
TEST_F(AutomationTreeManagerOwnerTest, FireEventsWithListeners) {
// A simple tree.
std::vector<AXTreeUpdate> updates;
updates.emplace_back();
auto& tree_update = updates.back();
tree_update.has_tree_data = true;
tree_update.root_id = 1;
auto& tree_data = tree_update.tree_data;
tree_data.tree_id = AXTreeID::CreateNewAXTreeID();
tree_update.nodes.emplace_back();
auto& node_data1 = tree_update.nodes.back();
node_data1.id = 1;
node_data1.role = ax::mojom::Role::kRootWebArea;
node_data1.child_ids.push_back(2);
node_data1.relative_bounds.bounds = gfx::RectF(100, 100, 100, 100);
tree_update.nodes.emplace_back();
auto& node_data2 = tree_update.nodes.back();
node_data2.id = 2;
node_data2.role = ax::mojom::Role::kButton;
node_data2.relative_bounds.bounds = gfx::RectF(0, 0, 200, 200);
// Add a hook for events from automation.
// Event names are the concatenation of the ax::mojom::Event string and the
// generated event string.
std::vector<std::string> events;
AddEventCallback(base::BindLambdaForTesting(
[&](const std::string& event) { events.push_back(event); }));
std::vector<AXEvent> ax_events;
SendAccessibilityEvents(updates[0].tree_data.tree_id, updates, gfx::Point(),
ax_events);
// We aren't listening for any events yet, but we should still get one that
// gets fired on initial tree creation.
ASSERT_EQ(1U, events.size());
EXPECT_EQ("none none", events[0]);
events.clear();
// Remove the root node data and don't update tree data.
tree_update.nodes.erase(tree_update.nodes.begin());
tree_update.has_tree_data = false;
// Trigger a role change.
tree_update.nodes[0].role = ax::mojom::Role::kSwitch;
SendAccessibilityEvents(updates[0].tree_data.tree_id, updates, gfx::Point(),
ax_events);
// There should be no events since there are no listeners and this isn't the
// initial tree.
ASSERT_TRUE(events.empty());
// Add a role change listener and do trigger the role change again.
auto* wrapper = GetTreeIDToTreeMap()[tree_data.tree_id].get();
auto* tree = wrapper->ax_tree();
// The button is id 2.
std::tuple<ax::mojom::Event, AXEventGenerator::Event> event_type(
ax::mojom::Event::kNone, AXEventGenerator::Event::ROLE_CHANGED);
wrapper->EventListenerAdded(event_type, tree->GetFromId(2));
EXPECT_EQ(1U, wrapper->EventListenerCount());
EXPECT_TRUE(wrapper->HasEventListener(event_type, tree->GetFromId(2)));
tree_update.nodes[0].role = ax::mojom::Role::kButton;
SendAccessibilityEvents(updates[0].tree_data.tree_id, updates, gfx::Point(),
ax_events);
// We should now have exactly one event.
ASSERT_EQ(1U, events.size());
EXPECT_EQ("none roleChanged", events[0]);
events.clear();
// Now, remove the listener and do the same as above.
wrapper->EventListenerRemoved(event_type, tree->GetFromId(2));
// We have to add another listener to ensure we don't shut down (no event
// listeners means this renderer closes).
wrapper->EventListenerAdded(
std::tuple<ax::mojom::Event, AXEventGenerator::Event>(
ax::mojom::Event::kLoadComplete, AXEventGenerator::Event::NONE),
tree->GetFromId(1));
tree_update.nodes[0].role = ax::mojom::Role::kSwitch;
SendAccessibilityEvents(updates[0].tree_data.tree_id, updates, gfx::Point(),
ax_events);
// We should have no events.
ASSERT_TRUE(events.empty());
// Finally, let's fire a non-generated event on the button, but add the
// listener on the root. This will test both non-generated events and
// respecting event listeners on ancestors of the target.
// First, fire the event without the click listener.
tree_update.nodes.clear();
ax_events.emplace_back();
auto& event = ax_events.back();
event.event_type = ax::mojom::Event::kClicked;
event.id = 2;
SendAccessibilityEvents(updates[0].tree_data.tree_id, updates, gfx::Point(),
ax_events);
// No event.
ASSERT_TRUE(events.empty());
// Now, add the click listener to the root, and fire the click event on the
// button.
wrapper->EventListenerAdded(
std::tuple<ax::mojom::Event, AXEventGenerator::Event>(
ax::mojom::Event::kClicked, AXEventGenerator::Event::NONE),
tree->GetFromId(1));
SendAccessibilityEvents(updates[0].tree_data.tree_id, updates, gfx::Point(),
ax_events);
ASSERT_EQ(1U, events.size());
EXPECT_EQ("clicked none", events[0]);
#if BUILDFLAG(IS_CHROMEOS)
// Verify that the manager forwards the text location.
bool text_location_sent = false;
AddGetTextLocationResultCallback(base::BindLambdaForTesting(
[&](const AXActionData& data, const std::optional<gfx::Rect>& rect) {
text_location_sent = true;
}));
AXActionData action_data;
action_data.target_tree_id = updates[0].tree_data.tree_id;
action_data.target_node_id = 1;
action_data.request_id = 1;
std::optional<gfx::Rect> rect = gfx::Rect();
rect->SetRect(2, 2, 4, 4);
SendGetTextLocationResult(action_data, rect);
EXPECT_TRUE(text_location_sent);
#endif // BUILDFLAG(IS_CHROMEOS)
// Finally, check if sending an event to delete the tree correctly notify
// listeners.
bool tree_destroyed = false;
AddTreeDestroyedCallback(base::BindLambdaForTesting(
[&](const AXTreeID& tree_id) { tree_destroyed = true; }));
SendOnTreeDestroyedEvent(updates[0].tree_data.tree_id);
EXPECT_TRUE(tree_destroyed);
}
} // namespace ui
|