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
|
// Copyright (c) 2012 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 "base/memory/scoped_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/scoped_bstr.h"
#include "base/win/scoped_comptr.h"
#include "base/win/scoped_variant.h"
#include "content/browser/accessibility/browser_accessibility_manager.h"
#include "content/browser/accessibility/browser_accessibility_manager_win.h"
#include "content/browser/accessibility/browser_accessibility_state_impl.h"
#include "content/browser/accessibility/browser_accessibility_win.h"
#include "content/browser/renderer_host/legacy_render_widget_host_win.h"
#include "content/common/accessibility_messages.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/win/atl_module.h"
namespace content {
namespace {
// CountedBrowserAccessibility ------------------------------------------------
// Subclass of BrowserAccessibilityWin that counts the number of instances.
class CountedBrowserAccessibility : public BrowserAccessibilityWin {
public:
CountedBrowserAccessibility();
virtual ~CountedBrowserAccessibility();
static void reset() { num_instances_ = 0; }
static int num_instances() { return num_instances_; }
private:
static int num_instances_;
DISALLOW_COPY_AND_ASSIGN(CountedBrowserAccessibility);
};
// static
int CountedBrowserAccessibility::num_instances_ = 0;
CountedBrowserAccessibility::CountedBrowserAccessibility() {
++num_instances_;
}
CountedBrowserAccessibility::~CountedBrowserAccessibility() {
--num_instances_;
}
// CountedBrowserAccessibilityFactory -----------------------------------------
// Factory that creates a CountedBrowserAccessibility.
class CountedBrowserAccessibilityFactory : public BrowserAccessibilityFactory {
public:
CountedBrowserAccessibilityFactory();
private:
virtual ~CountedBrowserAccessibilityFactory();
virtual BrowserAccessibility* Create() override;
DISALLOW_COPY_AND_ASSIGN(CountedBrowserAccessibilityFactory);
};
CountedBrowserAccessibilityFactory::CountedBrowserAccessibilityFactory() {
}
CountedBrowserAccessibilityFactory::~CountedBrowserAccessibilityFactory() {
}
BrowserAccessibility* CountedBrowserAccessibilityFactory::Create() {
CComObject<CountedBrowserAccessibility>* instance;
HRESULT hr = CComObject<CountedBrowserAccessibility>::CreateInstance(
&instance);
DCHECK(SUCCEEDED(hr));
instance->AddRef();
return instance;
}
} // namespace
// BrowserAccessibilityTest ---------------------------------------------------
class BrowserAccessibilityTest : public testing::Test {
public:
BrowserAccessibilityTest();
virtual ~BrowserAccessibilityTest();
private:
virtual void SetUp() override;
DISALLOW_COPY_AND_ASSIGN(BrowserAccessibilityTest);
};
BrowserAccessibilityTest::BrowserAccessibilityTest() {
}
BrowserAccessibilityTest::~BrowserAccessibilityTest() {
}
void BrowserAccessibilityTest::SetUp() {
ui::win::CreateATLModuleIfNeeded();
}
// Actual tests ---------------------------------------------------------------
// Test that BrowserAccessibilityManager correctly releases the tree of
// BrowserAccessibility instances upon delete.
TEST_F(BrowserAccessibilityTest, TestNoLeaks) {
// Create ui::AXNodeData objects for a simple document tree,
// representing the accessibility information used to initialize
// BrowserAccessibilityManager.
ui::AXNodeData button;
button.id = 2;
button.SetName("Button");
button.role = ui::AX_ROLE_BUTTON;
button.state = 0;
ui::AXNodeData checkbox;
checkbox.id = 3;
checkbox.SetName("Checkbox");
checkbox.role = ui::AX_ROLE_CHECK_BOX;
checkbox.state = 0;
ui::AXNodeData root;
root.id = 1;
root.SetName("Document");
root.role = ui::AX_ROLE_ROOT_WEB_AREA;
root.state = 0;
root.child_ids.push_back(2);
root.child_ids.push_back(3);
// Construct a BrowserAccessibilityManager with this
// ui::AXNodeData tree and a factory for an instance-counting
// BrowserAccessibility, and ensure that exactly 3 instances were
// created. Note that the manager takes ownership of the factory.
CountedBrowserAccessibility::reset();
scoped_ptr<BrowserAccessibilityManager> manager(
BrowserAccessibilityManager::Create(
MakeAXTreeUpdate(root, button, checkbox),
NULL, new CountedBrowserAccessibilityFactory()));
ASSERT_EQ(3, CountedBrowserAccessibility::num_instances());
// Delete the manager and test that all 3 instances are deleted.
manager.reset();
ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
// Construct a manager again, and this time use the IAccessible interface
// to get new references to two of the three nodes in the tree.
manager.reset(BrowserAccessibilityManager::Create(
MakeAXTreeUpdate(root, button, checkbox),
NULL, new CountedBrowserAccessibilityFactory()));
ASSERT_EQ(3, CountedBrowserAccessibility::num_instances());
IAccessible* root_accessible =
manager->GetRoot()->ToBrowserAccessibilityWin();
IDispatch* root_iaccessible = NULL;
IDispatch* child1_iaccessible = NULL;
base::win::ScopedVariant childid_self(CHILDID_SELF);
HRESULT hr = root_accessible->get_accChild(childid_self, &root_iaccessible);
ASSERT_EQ(S_OK, hr);
base::win::ScopedVariant one(1);
hr = root_accessible->get_accChild(one, &child1_iaccessible);
ASSERT_EQ(S_OK, hr);
// Now delete the manager, and only one of the three nodes in the tree
// should be released.
manager.reset();
ASSERT_EQ(2, CountedBrowserAccessibility::num_instances());
// Release each of our references and make sure that each one results in
// the instance being deleted as its reference count hits zero.
root_iaccessible->Release();
ASSERT_EQ(1, CountedBrowserAccessibility::num_instances());
child1_iaccessible->Release();
ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
}
TEST_F(BrowserAccessibilityTest, TestChildrenChange) {
// Create ui::AXNodeData objects for a simple document tree,
// representing the accessibility information used to initialize
// BrowserAccessibilityManager.
ui::AXNodeData text;
text.id = 2;
text.role = ui::AX_ROLE_STATIC_TEXT;
text.SetName("old text");
text.state = 0;
ui::AXNodeData root;
root.id = 1;
root.SetName("Document");
root.role = ui::AX_ROLE_ROOT_WEB_AREA;
root.state = 0;
root.child_ids.push_back(2);
// Construct a BrowserAccessibilityManager with this
// ui::AXNodeData tree and a factory for an instance-counting
// BrowserAccessibility.
CountedBrowserAccessibility::reset();
scoped_ptr<BrowserAccessibilityManager> manager(
BrowserAccessibilityManager::Create(
MakeAXTreeUpdate(root, text),
NULL, new CountedBrowserAccessibilityFactory()));
// Query for the text IAccessible and verify that it returns "old text" as its
// value.
base::win::ScopedVariant one(1);
base::win::ScopedComPtr<IDispatch> text_dispatch;
HRESULT hr = manager->GetRoot()->ToBrowserAccessibilityWin()->get_accChild(
one, text_dispatch.Receive());
ASSERT_EQ(S_OK, hr);
base::win::ScopedComPtr<IAccessible> text_accessible;
hr = text_dispatch.QueryInterface(text_accessible.Receive());
ASSERT_EQ(S_OK, hr);
base::win::ScopedVariant childid_self(CHILDID_SELF);
base::win::ScopedBstr name;
hr = text_accessible->get_accName(childid_self, name.Receive());
ASSERT_EQ(S_OK, hr);
EXPECT_EQ(L"old text", base::string16(name));
name.Reset();
text_dispatch.Release();
text_accessible.Release();
// Notify the BrowserAccessibilityManager that the text child has changed.
ui::AXNodeData text2;
text2.id = 2;
text2.role = ui::AX_ROLE_STATIC_TEXT;
text2.SetName("new text");
text2.SetName("old text");
AccessibilityHostMsg_EventParams param;
param.event_type = ui::AX_EVENT_CHILDREN_CHANGED;
param.update.nodes.push_back(text2);
param.id = text2.id;
std::vector<AccessibilityHostMsg_EventParams> events;
events.push_back(param);
manager->OnAccessibilityEvents(events);
// Query for the text IAccessible and verify that it now returns "new text"
// as its value.
hr = manager->GetRoot()->ToBrowserAccessibilityWin()->get_accChild(
one, text_dispatch.Receive());
ASSERT_EQ(S_OK, hr);
hr = text_dispatch.QueryInterface(text_accessible.Receive());
ASSERT_EQ(S_OK, hr);
hr = text_accessible->get_accName(childid_self, name.Receive());
ASSERT_EQ(S_OK, hr);
EXPECT_EQ(L"new text", base::string16(name));
text_dispatch.Release();
text_accessible.Release();
// Delete the manager and test that all BrowserAccessibility instances are
// deleted.
manager.reset();
ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
}
TEST_F(BrowserAccessibilityTest, TestChildrenChangeNoLeaks) {
// Create ui::AXNodeData objects for a simple document tree,
// representing the accessibility information used to initialize
// BrowserAccessibilityManager.
ui::AXNodeData div;
div.id = 2;
div.role = ui::AX_ROLE_GROUP;
div.state = 0;
ui::AXNodeData text3;
text3.id = 3;
text3.role = ui::AX_ROLE_STATIC_TEXT;
text3.state = 0;
ui::AXNodeData text4;
text4.id = 4;
text4.role = ui::AX_ROLE_STATIC_TEXT;
text4.state = 0;
div.child_ids.push_back(3);
div.child_ids.push_back(4);
ui::AXNodeData root;
root.id = 1;
root.role = ui::AX_ROLE_ROOT_WEB_AREA;
root.state = 0;
root.child_ids.push_back(2);
// Construct a BrowserAccessibilityManager with this
// ui::AXNodeData tree and a factory for an instance-counting
// BrowserAccessibility and ensure that exactly 4 instances were
// created. Note that the manager takes ownership of the factory.
CountedBrowserAccessibility::reset();
scoped_ptr<BrowserAccessibilityManager> manager(
BrowserAccessibilityManager::Create(
MakeAXTreeUpdate(root, div, text3, text4),
NULL, new CountedBrowserAccessibilityFactory()));
ASSERT_EQ(4, CountedBrowserAccessibility::num_instances());
// Notify the BrowserAccessibilityManager that the div node and its children
// were removed and ensure that only one BrowserAccessibility instance exists.
root.child_ids.clear();
AccessibilityHostMsg_EventParams param;
param.event_type = ui::AX_EVENT_CHILDREN_CHANGED;
param.update.nodes.push_back(root);
param.id = root.id;
std::vector<AccessibilityHostMsg_EventParams> events;
events.push_back(param);
manager->OnAccessibilityEvents(events);
ASSERT_EQ(1, CountedBrowserAccessibility::num_instances());
// Delete the manager and test that all BrowserAccessibility instances are
// deleted.
manager.reset();
ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
}
TEST_F(BrowserAccessibilityTest, TestTextBoundaries) {
std::string text1_value = "One two three.\nFour five six.";
ui::AXNodeData text1;
text1.id = 11;
text1.role = ui::AX_ROLE_TEXT_FIELD;
text1.state = 0;
text1.AddStringAttribute(ui::AX_ATTR_VALUE, text1_value);
std::vector<int32> line_breaks;
line_breaks.push_back(15);
text1.AddIntListAttribute(
ui::AX_ATTR_LINE_BREAKS, line_breaks);
ui::AXNodeData root;
root.id = 1;
root.role = ui::AX_ROLE_ROOT_WEB_AREA;
root.state = 0;
root.child_ids.push_back(11);
CountedBrowserAccessibility::reset();
scoped_ptr<BrowserAccessibilityManager> manager(
BrowserAccessibilityManager::Create(
MakeAXTreeUpdate(root, text1),
NULL, new CountedBrowserAccessibilityFactory()));
ASSERT_EQ(2, CountedBrowserAccessibility::num_instances());
BrowserAccessibilityWin* root_obj =
manager->GetRoot()->ToBrowserAccessibilityWin();
BrowserAccessibilityWin* text1_obj =
root_obj->PlatformGetChild(0)->ToBrowserAccessibilityWin();
long text1_len;
ASSERT_EQ(S_OK, text1_obj->get_nCharacters(&text1_len));
base::win::ScopedBstr text;
ASSERT_EQ(S_OK, text1_obj->get_text(0, text1_len, text.Receive()));
ASSERT_EQ(text1_value, base::UTF16ToUTF8(base::string16(text)));
text.Reset();
ASSERT_EQ(S_OK, text1_obj->get_text(0, 4, text.Receive()));
ASSERT_STREQ(L"One ", text);
text.Reset();
long start;
long end;
ASSERT_EQ(S_OK, text1_obj->get_textAtOffset(
1, IA2_TEXT_BOUNDARY_CHAR, &start, &end, text.Receive()));
ASSERT_EQ(1, start);
ASSERT_EQ(2, end);
ASSERT_STREQ(L"n", text);
text.Reset();
ASSERT_EQ(S_FALSE, text1_obj->get_textAtOffset(
text1_len, IA2_TEXT_BOUNDARY_CHAR, &start, &end, text.Receive()));
ASSERT_EQ(text1_len, start);
ASSERT_EQ(text1_len, end);
text.Reset();
ASSERT_EQ(S_OK, text1_obj->get_textAtOffset(
1, IA2_TEXT_BOUNDARY_WORD, &start, &end, text.Receive()));
ASSERT_EQ(0, start);
ASSERT_EQ(3, end);
ASSERT_STREQ(L"One", text);
text.Reset();
ASSERT_EQ(S_OK, text1_obj->get_textAtOffset(
6, IA2_TEXT_BOUNDARY_WORD, &start, &end, text.Receive()));
ASSERT_EQ(4, start);
ASSERT_EQ(7, end);
ASSERT_STREQ(L"two", text);
text.Reset();
ASSERT_EQ(S_OK, text1_obj->get_textAtOffset(
text1_len, IA2_TEXT_BOUNDARY_WORD, &start, &end, text.Receive()));
ASSERT_EQ(25, start);
ASSERT_EQ(29, end);
ASSERT_STREQ(L"six.", text);
text.Reset();
ASSERT_EQ(S_OK, text1_obj->get_textAtOffset(
1, IA2_TEXT_BOUNDARY_LINE, &start, &end, text.Receive()));
ASSERT_EQ(0, start);
ASSERT_EQ(15, end);
ASSERT_STREQ(L"One two three.\n", text);
text.Reset();
ASSERT_EQ(S_OK,
text1_obj->get_text(0, IA2_TEXT_OFFSET_LENGTH, text.Receive()));
ASSERT_STREQ(L"One two three.\nFour five six.", text);
// Delete the manager and test that all BrowserAccessibility instances are
// deleted.
manager.reset();
ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
}
TEST_F(BrowserAccessibilityTest, TestSimpleHypertext) {
const std::string text1_name = "One two three.";
const std::string text2_name = " Four five six.";
ui::AXNodeData text1;
text1.id = 11;
text1.role = ui::AX_ROLE_STATIC_TEXT;
text1.state = 1 << ui::AX_STATE_READ_ONLY;
text1.SetName(text1_name);
ui::AXNodeData text2;
text2.id = 12;
text2.role = ui::AX_ROLE_STATIC_TEXT;
text2.state = 1 << ui::AX_STATE_READ_ONLY;
text2.SetName(text2_name);
ui::AXNodeData root;
root.id = 1;
root.role = ui::AX_ROLE_ROOT_WEB_AREA;
root.state = 1 << ui::AX_STATE_READ_ONLY;
root.child_ids.push_back(11);
root.child_ids.push_back(12);
CountedBrowserAccessibility::reset();
scoped_ptr<BrowserAccessibilityManager> manager(
BrowserAccessibilityManager::Create(
MakeAXTreeUpdate(root, root, text1, text2),
NULL, new CountedBrowserAccessibilityFactory()));
ASSERT_EQ(3, CountedBrowserAccessibility::num_instances());
BrowserAccessibilityWin* root_obj =
manager->GetRoot()->ToBrowserAccessibilityWin();
long text_len;
ASSERT_EQ(S_OK, root_obj->get_nCharacters(&text_len));
base::win::ScopedBstr text;
ASSERT_EQ(S_OK, root_obj->get_text(0, text_len, text.Receive()));
EXPECT_EQ(text1_name + text2_name, base::UTF16ToUTF8(base::string16(text)));
long hyperlink_count;
ASSERT_EQ(S_OK, root_obj->get_nHyperlinks(&hyperlink_count));
EXPECT_EQ(0, hyperlink_count);
base::win::ScopedComPtr<IAccessibleHyperlink> hyperlink;
EXPECT_EQ(E_INVALIDARG, root_obj->get_hyperlink(-1, hyperlink.Receive()));
EXPECT_EQ(E_INVALIDARG, root_obj->get_hyperlink(0, hyperlink.Receive()));
EXPECT_EQ(E_INVALIDARG, root_obj->get_hyperlink(28, hyperlink.Receive()));
EXPECT_EQ(E_INVALIDARG, root_obj->get_hyperlink(29, hyperlink.Receive()));
long hyperlink_index;
EXPECT_EQ(E_FAIL, root_obj->get_hyperlinkIndex(0, &hyperlink_index));
EXPECT_EQ(-1, hyperlink_index);
EXPECT_EQ(E_FAIL, root_obj->get_hyperlinkIndex(28, &hyperlink_index));
EXPECT_EQ(-1, hyperlink_index);
EXPECT_EQ(E_INVALIDARG, root_obj->get_hyperlinkIndex(-1, &hyperlink_index));
EXPECT_EQ(-1, hyperlink_index);
EXPECT_EQ(E_INVALIDARG, root_obj->get_hyperlinkIndex(29, &hyperlink_index));
EXPECT_EQ(-1, hyperlink_index);
// Delete the manager and test that all BrowserAccessibility instances are
// deleted.
manager.reset();
ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
}
TEST_F(BrowserAccessibilityTest, TestComplexHypertext) {
const std::string text1_name = "One two three.";
const std::string text2_name = " Four five six.";
const std::string button1_text_name = "red";
const std::string link1_text_name = "blue";
ui::AXNodeData text1;
text1.id = 11;
text1.role = ui::AX_ROLE_STATIC_TEXT;
text1.state = 1 << ui::AX_STATE_READ_ONLY;
text1.SetName(text1_name);
ui::AXNodeData text2;
text2.id = 12;
text2.role = ui::AX_ROLE_STATIC_TEXT;
text2.state = 1 << ui::AX_STATE_READ_ONLY;
text2.SetName(text2_name);
ui::AXNodeData button1, button1_text;
button1.id = 13;
button1_text.id = 15;
button1_text.SetName(button1_text_name);
button1.role = ui::AX_ROLE_BUTTON;
button1_text.role = ui::AX_ROLE_STATIC_TEXT;
button1.state = 1 << ui::AX_STATE_READ_ONLY;
button1_text.state = 1 << ui::AX_STATE_READ_ONLY;
button1.child_ids.push_back(15);
ui::AXNodeData link1, link1_text;
link1.id = 14;
link1_text.id = 16;
link1_text.SetName(link1_text_name);
link1.role = ui::AX_ROLE_LINK;
link1_text.role = ui::AX_ROLE_STATIC_TEXT;
link1.state = 1 << ui::AX_STATE_READ_ONLY;
link1_text.state = 1 << ui::AX_STATE_READ_ONLY;
link1.child_ids.push_back(16);
ui::AXNodeData root;
root.id = 1;
root.role = ui::AX_ROLE_ROOT_WEB_AREA;
root.state = 1 << ui::AX_STATE_READ_ONLY;
root.child_ids.push_back(11);
root.child_ids.push_back(13);
root.child_ids.push_back(12);
root.child_ids.push_back(14);
CountedBrowserAccessibility::reset();
scoped_ptr<BrowserAccessibilityManager> manager(
BrowserAccessibilityManager::Create(
MakeAXTreeUpdate(root,
text1, button1, button1_text,
text2, link1, link1_text),
NULL, new CountedBrowserAccessibilityFactory()));
ASSERT_EQ(7, CountedBrowserAccessibility::num_instances());
BrowserAccessibilityWin* root_obj =
manager->GetRoot()->ToBrowserAccessibilityWin();
long text_len;
ASSERT_EQ(S_OK, root_obj->get_nCharacters(&text_len));
base::win::ScopedBstr text;
ASSERT_EQ(S_OK, root_obj->get_text(0, text_len, text.Receive()));
const std::string embed = base::UTF16ToUTF8(
BrowserAccessibilityWin::kEmbeddedCharacter);
EXPECT_EQ(text1_name + embed + text2_name + embed,
base::UTF16ToUTF8(base::string16(text)));
text.Reset();
long hyperlink_count;
ASSERT_EQ(S_OK, root_obj->get_nHyperlinks(&hyperlink_count));
EXPECT_EQ(2, hyperlink_count);
base::win::ScopedComPtr<IAccessibleHyperlink> hyperlink;
base::win::ScopedComPtr<IAccessibleText> hypertext;
EXPECT_EQ(E_INVALIDARG, root_obj->get_hyperlink(-1, hyperlink.Receive()));
EXPECT_EQ(E_INVALIDARG, root_obj->get_hyperlink(2, hyperlink.Receive()));
EXPECT_EQ(E_INVALIDARG, root_obj->get_hyperlink(28, hyperlink.Receive()));
EXPECT_EQ(S_OK, root_obj->get_hyperlink(0, hyperlink.Receive()));
EXPECT_EQ(S_OK,
hyperlink.QueryInterface<IAccessibleText>(hypertext.Receive()));
EXPECT_EQ(S_OK, hypertext->get_text(0, 3, text.Receive()));
EXPECT_STREQ(button1_text_name.c_str(),
base::UTF16ToUTF8(base::string16(text)).c_str());
text.Reset();
hyperlink.Release();
hypertext.Release();
EXPECT_EQ(S_OK, root_obj->get_hyperlink(1, hyperlink.Receive()));
EXPECT_EQ(S_OK,
hyperlink.QueryInterface<IAccessibleText>(hypertext.Receive()));
EXPECT_EQ(S_OK, hypertext->get_text(0, 4, text.Receive()));
EXPECT_STREQ(link1_text_name.c_str(),
base::UTF16ToUTF8(base::string16(text)).c_str());
text.Reset();
hyperlink.Release();
hypertext.Release();
long hyperlink_index;
EXPECT_EQ(E_FAIL, root_obj->get_hyperlinkIndex(0, &hyperlink_index));
EXPECT_EQ(-1, hyperlink_index);
EXPECT_EQ(E_FAIL, root_obj->get_hyperlinkIndex(28, &hyperlink_index));
EXPECT_EQ(-1, hyperlink_index);
EXPECT_EQ(S_OK, root_obj->get_hyperlinkIndex(14, &hyperlink_index));
EXPECT_EQ(0, hyperlink_index);
EXPECT_EQ(S_OK, root_obj->get_hyperlinkIndex(30, &hyperlink_index));
EXPECT_EQ(1, hyperlink_index);
// Delete the manager and test that all BrowserAccessibility instances are
// deleted.
manager.reset();
ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
}
TEST_F(BrowserAccessibilityTest, TestCreateEmptyDocument) {
// Try creating an empty document with busy state. Readonly is
// set automatically.
CountedBrowserAccessibility::reset();
const int32 busy_state = 1 << ui::AX_STATE_BUSY;
const int32 readonly_state = 1 << ui::AX_STATE_READ_ONLY;
const int32 enabled_state = 1 << ui::AX_STATE_ENABLED;
scoped_ptr<BrowserAccessibilityManager> manager(
new BrowserAccessibilityManagerWin(
BrowserAccessibilityManagerWin::GetEmptyDocument(),
NULL,
new CountedBrowserAccessibilityFactory()));
// Verify the root is as we expect by default.
BrowserAccessibility* root = manager->GetRoot();
EXPECT_EQ(0, root->GetId());
EXPECT_EQ(ui::AX_ROLE_ROOT_WEB_AREA, root->GetRole());
EXPECT_EQ(busy_state | readonly_state | enabled_state, root->GetState());
// Tree with a child textfield.
ui::AXNodeData tree1_1;
tree1_1.id = 1;
tree1_1.role = ui::AX_ROLE_ROOT_WEB_AREA;
tree1_1.child_ids.push_back(2);
ui::AXNodeData tree1_2;
tree1_2.id = 2;
tree1_2.role = ui::AX_ROLE_TEXT_FIELD;
// Process a load complete.
std::vector<AccessibilityHostMsg_EventParams> params;
params.push_back(AccessibilityHostMsg_EventParams());
AccessibilityHostMsg_EventParams* msg = ¶ms[0];
msg->event_type = ui::AX_EVENT_LOAD_COMPLETE;
msg->update.nodes.push_back(tree1_1);
msg->update.nodes.push_back(tree1_2);
msg->id = tree1_1.id;
manager->OnAccessibilityEvents(params);
// Save for later comparison.
BrowserAccessibility* acc1_2 = manager->GetFromID(2);
// Verify the root has changed.
EXPECT_NE(root, manager->GetRoot());
// And the proper child remains.
EXPECT_EQ(ui::AX_ROLE_TEXT_FIELD, acc1_2->GetRole());
EXPECT_EQ(2, acc1_2->GetId());
// Tree with a child button.
ui::AXNodeData tree2_1;
tree2_1.id = 1;
tree2_1.role = ui::AX_ROLE_ROOT_WEB_AREA;
tree2_1.child_ids.push_back(3);
ui::AXNodeData tree2_2;
tree2_2.id = 3;
tree2_2.role = ui::AX_ROLE_BUTTON;
msg->update.nodes.clear();
msg->update.nodes.push_back(tree2_1);
msg->update.nodes.push_back(tree2_2);
msg->id = tree2_1.id;
// Fire another load complete.
manager->OnAccessibilityEvents(params);
BrowserAccessibility* acc2_2 = manager->GetFromID(3);
// Verify the root has changed.
EXPECT_NE(root, manager->GetRoot());
// And the new child exists.
EXPECT_EQ(ui::AX_ROLE_BUTTON, acc2_2->GetRole());
EXPECT_EQ(3, acc2_2->GetId());
// Ensure we properly cleaned up.
manager.reset();
ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
}
// This is a regression test for a bug where the initial empty document
// loaded by a BrowserAccessibilityManagerWin couldn't be looked up by
// its UniqueIDWin, because the AX Tree was loaded in
// BrowserAccessibilityManager code before BrowserAccessibilityManagerWin
// was initialized.
TEST_F(BrowserAccessibilityTest, EmptyDocHasUniqueIdWin) {
scoped_ptr<BrowserAccessibilityManagerWin> manager(
new BrowserAccessibilityManagerWin(
BrowserAccessibilityManagerWin::GetEmptyDocument(),
NULL,
new CountedBrowserAccessibilityFactory()));
// Verify the root is as we expect by default.
BrowserAccessibility* root = manager->GetRoot();
EXPECT_EQ(0, root->GetId());
EXPECT_EQ(ui::AX_ROLE_ROOT_WEB_AREA, root->GetRole());
EXPECT_EQ(1 << ui::AX_STATE_BUSY |
1 << ui::AX_STATE_READ_ONLY |
1 << ui::AX_STATE_ENABLED,
root->GetState());
LONG unique_id_win = root->ToBrowserAccessibilityWin()->unique_id_win();
ASSERT_EQ(root, manager->GetFromUniqueIdWin(unique_id_win));
}
TEST_F(BrowserAccessibilityTest, TestIA2Attributes) {
ui::AXNodeData button;
ui::AXNodeData checkbox;
checkbox.id = 2;
checkbox.SetName("Checkbox");
checkbox.role = ui::AX_ROLE_CHECK_BOX;
checkbox.state = 1 << ui::AX_STATE_CHECKED;
ui::AXNodeData root;
root.id = 1;
root.SetName("Document");
root.role = ui::AX_ROLE_ROOT_WEB_AREA;
root.state = 0;
root.child_ids.push_back(2);
CountedBrowserAccessibility::reset();
scoped_ptr<BrowserAccessibilityManager> manager(
BrowserAccessibilityManager::Create(
MakeAXTreeUpdate(root, checkbox),
nullptr, new CountedBrowserAccessibilityFactory()));
ASSERT_EQ(2, CountedBrowserAccessibility::num_instances());
ASSERT_NE(nullptr, manager->GetRoot());
BrowserAccessibilityWin* root_accessible =
manager->GetRoot()->ToBrowserAccessibilityWin();
ASSERT_NE(nullptr, root_accessible);
ASSERT_EQ(1, root_accessible->PlatformChildCount());
BrowserAccessibilityWin* checkbox_accessible =
root_accessible->PlatformGetChild(0)->ToBrowserAccessibilityWin();
ASSERT_NE(nullptr, checkbox_accessible);
base::win::ScopedBstr attributes;
HRESULT hr = checkbox_accessible->get_attributes(attributes.Receive());
EXPECT_EQ(S_OK, hr);
EXPECT_NE(nullptr, static_cast<BSTR>(attributes));
std::wstring attributes_str(attributes, attributes.Length());
EXPECT_EQ(L"checkable:true;", attributes_str);
manager.reset();
ASSERT_EQ(0, CountedBrowserAccessibility::num_instances());
}
} // namespace content
|