| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "base/win/scoped_bstr.h" | 7 #include "base/win/scoped_bstr.h" |
| 8 #include "base/win/scoped_comptr.h" | 8 #include "base/win/scoped_comptr.h" |
| 9 #include "base/win/scoped_variant.h" | 9 #include "base/win/scoped_variant.h" |
| 10 #include "content/browser/accessibility/browser_accessibility_manager.h" | 10 #include "content/browser/accessibility/browser_accessibility_manager.h" |
| 11 #include "content/browser/accessibility/browser_accessibility_manager_win.h" | 11 #include "content/browser/accessibility/browser_accessibility_manager_win.h" |
| 12 #include "content/browser/accessibility/browser_accessibility_state_impl.h" | 12 #include "content/browser/accessibility/browser_accessibility_state_impl.h" |
| 13 #include "content/browser/accessibility/browser_accessibility_win.h" | 13 #include "content/browser/accessibility/browser_accessibility_win.h" |
| 14 #include "content/browser/renderer_host/legacy_render_widget_host_win.h" | |
| 15 #include "content/common/accessibility_messages.h" | 14 #include "content/common/accessibility_messages.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "ui/base/win/atl_module.h" | 16 #include "ui/base/win/atl_module.h" |
| 18 | 17 |
| 19 namespace content { | 18 namespace content { |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 | 21 |
| 23 // CountedBrowserAccessibility ------------------------------------------------ | 22 // CountedBrowserAccessibility ------------------------------------------------ |
| 24 | 23 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 | 71 |
| 73 BrowserAccessibility* CountedBrowserAccessibilityFactory::Create() { | 72 BrowserAccessibility* CountedBrowserAccessibilityFactory::Create() { |
| 74 CComObject<CountedBrowserAccessibility>* instance; | 73 CComObject<CountedBrowserAccessibility>* instance; |
| 75 HRESULT hr = CComObject<CountedBrowserAccessibility>::CreateInstance( | 74 HRESULT hr = CComObject<CountedBrowserAccessibility>::CreateInstance( |
| 76 &instance); | 75 &instance); |
| 77 DCHECK(SUCCEEDED(hr)); | 76 DCHECK(SUCCEEDED(hr)); |
| 78 instance->AddRef(); | 77 instance->AddRef(); |
| 79 return instance; | 78 return instance; |
| 80 } | 79 } |
| 81 | 80 |
| 82 // Provides functionality for creating the accessible hwnd and ensures that | |
| 83 // the hwnd along with the LegacyRenderWidgetHostHWND instance is destroyed | |
| 84 // when this instance is destroyed. | |
| 85 class TestLegacyRenderWidgetHostHWND { | |
| 86 public: | |
| 87 TestLegacyRenderWidgetHostHWND() | |
| 88 : accessible_hwnd_(NULL) {} | |
| 89 | |
| 90 ~TestLegacyRenderWidgetHostHWND() { | |
| 91 if (accessible_hwnd()) | |
| 92 ::DestroyWindow(accessible_hwnd()->hwnd()); | |
| 93 } | |
| 94 | |
| 95 bool Initialize(HWND parent) { | |
| 96 accessible_hwnd_ = LegacyRenderWidgetHostHWND::Create(parent); | |
| 97 EXPECT_NE(accessible_hwnd_, | |
| 98 static_cast<LegacyRenderWidgetHostHWND*>(NULL)); | |
| 99 return accessible_hwnd_ != NULL; | |
| 100 } | |
| 101 | |
| 102 LegacyRenderWidgetHostHWND* accessible_hwnd() { | |
| 103 return accessible_hwnd_; | |
| 104 } | |
| 105 | |
| 106 private: | |
| 107 LegacyRenderWidgetHostHWND* accessible_hwnd_; | |
| 108 }; | |
| 109 | |
| 110 } // namespace | 81 } // namespace |
| 111 | 82 |
| 112 | 83 |
| 113 // BrowserAccessibilityTest --------------------------------------------------- | 84 // BrowserAccessibilityTest --------------------------------------------------- |
| 114 | 85 |
| 115 class BrowserAccessibilityTest : public testing::Test { | 86 class BrowserAccessibilityTest : public testing::Test { |
| 116 public: | 87 public: |
| 117 BrowserAccessibilityTest(); | 88 BrowserAccessibilityTest(); |
| 118 virtual ~BrowserAccessibilityTest(); | 89 virtual ~BrowserAccessibilityTest(); |
| 119 | 90 |
| (...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 626 ASSERT_EQ(0, CountedBrowserAccessibility::num_instances()); | 597 ASSERT_EQ(0, CountedBrowserAccessibility::num_instances()); |
| 627 } | 598 } |
| 628 | 599 |
| 629 TEST_F(BrowserAccessibilityTest, TestCreateEmptyDocument) { | 600 TEST_F(BrowserAccessibilityTest, TestCreateEmptyDocument) { |
| 630 // Try creating an empty document with busy state. Readonly is | 601 // Try creating an empty document with busy state. Readonly is |
| 631 // set automatically. | 602 // set automatically. |
| 632 CountedBrowserAccessibility::reset(); | 603 CountedBrowserAccessibility::reset(); |
| 633 const int32 busy_state = 1 << ui::AX_STATE_BUSY; | 604 const int32 busy_state = 1 << ui::AX_STATE_BUSY; |
| 634 const int32 readonly_state = 1 << ui::AX_STATE_READ_ONLY; | 605 const int32 readonly_state = 1 << ui::AX_STATE_READ_ONLY; |
| 635 const int32 enabled_state = 1 << ui::AX_STATE_ENABLED; | 606 const int32 enabled_state = 1 << ui::AX_STATE_ENABLED; |
| 636 scoped_ptr<TestLegacyRenderWidgetHostHWND> accessibility_test | |
| 637 (new TestLegacyRenderWidgetHostHWND); | |
| 638 EXPECT_EQ(accessibility_test->Initialize(GetDesktopWindow()), true); | |
| 639 scoped_ptr<BrowserAccessibilityManager> manager( | 607 scoped_ptr<BrowserAccessibilityManager> manager( |
| 640 new BrowserAccessibilityManagerWin( | 608 BrowserAccessibilityManager::Create( |
| 641 accessibility_test->accessible_hwnd(), | |
| 642 NULL, | |
| 643 BrowserAccessibilityManagerWin::GetEmptyDocument(), | 609 BrowserAccessibilityManagerWin::GetEmptyDocument(), |
| 644 NULL, | 610 NULL, |
| 645 new CountedBrowserAccessibilityFactory())); | 611 new CountedBrowserAccessibilityFactory())); |
| 646 | 612 |
| 647 // Verify the root is as we expect by default. | 613 // Verify the root is as we expect by default. |
| 648 BrowserAccessibility* root = manager->GetRoot(); | 614 BrowserAccessibility* root = manager->GetRoot(); |
| 649 EXPECT_EQ(0, root->GetId()); | 615 EXPECT_EQ(0, root->GetId()); |
| 650 EXPECT_EQ(ui::AX_ROLE_ROOT_WEB_AREA, root->GetRole()); | 616 EXPECT_EQ(ui::AX_ROLE_ROOT_WEB_AREA, root->GetRole()); |
| 651 EXPECT_EQ(busy_state | readonly_state | enabled_state, root->GetState()); | 617 EXPECT_EQ(busy_state | readonly_state | enabled_state, root->GetState()); |
| 652 | 618 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 713 } | 679 } |
| 714 | 680 |
| 715 TEST(BrowserAccessibilityManagerWinTest, TestAccessibleHWND) { | 681 TEST(BrowserAccessibilityManagerWinTest, TestAccessibleHWND) { |
| 716 HWND desktop_hwnd = GetDesktopWindow(); | 682 HWND desktop_hwnd = GetDesktopWindow(); |
| 717 base::win::ScopedComPtr<IAccessible> desktop_hwnd_iaccessible; | 683 base::win::ScopedComPtr<IAccessible> desktop_hwnd_iaccessible; |
| 718 ASSERT_EQ(S_OK, AccessibleObjectFromWindow( | 684 ASSERT_EQ(S_OK, AccessibleObjectFromWindow( |
| 719 desktop_hwnd, OBJID_CLIENT, | 685 desktop_hwnd, OBJID_CLIENT, |
| 720 IID_IAccessible, | 686 IID_IAccessible, |
| 721 reinterpret_cast<void**>(desktop_hwnd_iaccessible.Receive()))); | 687 reinterpret_cast<void**>(desktop_hwnd_iaccessible.Receive()))); |
| 722 | 688 |
| 723 scoped_ptr<TestLegacyRenderWidgetHostHWND> accessibility_test | |
| 724 (new TestLegacyRenderWidgetHostHWND); | |
| 725 EXPECT_EQ(accessibility_test->Initialize(GetDesktopWindow()), true); | |
| 726 | |
| 727 scoped_ptr<BrowserAccessibilityManagerWin> manager( | 689 scoped_ptr<BrowserAccessibilityManagerWin> manager( |
| 728 new BrowserAccessibilityManagerWin( | 690 static_cast<BrowserAccessibilityManagerWin*>( |
| 729 accessibility_test->accessible_hwnd(), | 691 BrowserAccessibilityManager::Create( |
| 730 desktop_hwnd_iaccessible, | 692 BrowserAccessibilityManagerWin::GetEmptyDocument(), NULL))); |
| 731 BrowserAccessibilityManagerWin::GetEmptyDocument(), | 693 ASSERT_EQ(NULL, manager->parent_iaccessible()); |
| 732 NULL)); | |
| 733 ASSERT_EQ(desktop_hwnd, manager->parent_hwnd()); | |
| 734 | 694 |
| 735 // Enabling screen reader support and calling MaybeCallNotifyWinEvent | 695 // Enabling screen reader support and calling MaybeCallNotifyWinEvent |
| 736 // should trigger creating the AccessibleHWND, and we should now get a | 696 // should trigger using the AccessibleHWND, and we should now get a |
| 737 // new parent_hwnd with the right window class to fool older screen | 697 // new parent_hwnd with the right window class to fool older screen |
| 738 // readers. | 698 // readers. |
| 739 BrowserAccessibilityStateImpl::GetInstance()->OnScreenReaderDetected(); | 699 BrowserAccessibilityStateImpl::GetInstance()->OnScreenReaderDetected(); |
| 740 manager->MaybeCallNotifyWinEvent(0, 0); | 700 manager->MaybeCallNotifyWinEvent(0, 0); |
| 741 HWND new_parent_hwnd = manager->parent_hwnd(); | 701 HWND new_parent_hwnd = manager->parent_hwnd(); |
| 742 ASSERT_NE(desktop_hwnd, new_parent_hwnd); | 702 ASSERT_NE(desktop_hwnd, new_parent_hwnd); |
| 703 ASSERT_TRUE(manager->parent_iaccessible()); |
| 743 WCHAR hwnd_class_name[256]; | 704 WCHAR hwnd_class_name[256]; |
| 744 ASSERT_NE(0, GetClassName(new_parent_hwnd, hwnd_class_name, 256)); | 705 ASSERT_NE(0, GetClassName(new_parent_hwnd, hwnd_class_name, 256)); |
| 745 ASSERT_STREQ(L"Chrome_RenderWidgetHostHWND", hwnd_class_name); | 706 ASSERT_STREQ(L"Chrome_RenderWidgetHostHWND", hwnd_class_name); |
| 746 | |
| 747 // Destroy the TestLegacyRenderWidgetHostHWND instance. That should in turn | |
| 748 // destroy the hwnd, which should clear the parent_hwnd(). | |
| 749 accessibility_test.reset(NULL); | |
| 750 ASSERT_EQ(FALSE, ::IsWindow(new_parent_hwnd)); | |
| 751 ASSERT_EQ(NULL, manager->parent_hwnd()); | |
| 752 | |
| 753 // Now create it again. | |
| 754 accessibility_test.reset(new TestLegacyRenderWidgetHostHWND); | |
| 755 EXPECT_EQ(accessibility_test->Initialize(::GetDesktopWindow()), true); | |
| 756 manager.reset( | |
| 757 new BrowserAccessibilityManagerWin( | |
| 758 accessibility_test->accessible_hwnd(), | |
| 759 desktop_hwnd_iaccessible, | |
| 760 BrowserAccessibilityManagerWin::GetEmptyDocument(), | |
| 761 NULL)); | |
| 762 manager->MaybeCallNotifyWinEvent(0, 0); | |
| 763 new_parent_hwnd = manager->parent_hwnd(); | |
| 764 ASSERT_FALSE(NULL == new_parent_hwnd); | |
| 765 | |
| 766 // This time, destroy the manager first, make sure the AccessibleHWND doesn't | |
| 767 // crash on destruction (to be caught by SyzyASAN or other tools). | |
| 768 manager.reset(NULL); | |
| 769 } | 707 } |
| 770 | 708 |
| 771 } // namespace content | 709 } // namespace content |
| OLD | NEW |