Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/scoped_ptr.h" | 5 #include "base/scoped_ptr.h" |
| 6 #include "base/scoped_comptr_win.h" | 6 #include "base/scoped_comptr_win.h" |
| 7 #include "chrome/browser/browser_accessibility_manager_win.h" | 7 #include "chrome/browser/accessibility/browser_accessibility_manager_win.h" |
| 8 #include "chrome/browser/browser_accessibility_win.h" | 8 #include "chrome/browser/accessibility/browser_accessibility_win.h" |
| 9 #include "chrome/common/render_messages_params.h" | 9 #include "chrome/common/render_messages_params.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 using webkit_glue::WebAccessibility; | 12 using webkit_glue::WebAccessibility; |
| 13 | 13 |
| 14 // Subclass of BrowserAccessibility that counts the number of instances. | 14 // Subclass of BrowserAccessibility that counts the number of instances. |
| 15 class CountedBrowserAccessibility : public BrowserAccessibility { | 15 class CountedBrowserAccessibility : public BrowserAccessibilityWin { |
| 16 public: | 16 public: |
| 17 CountedBrowserAccessibility() { global_obj_count_++; } | 17 CountedBrowserAccessibility() { global_obj_count_++; } |
| 18 virtual ~CountedBrowserAccessibility() { global_obj_count_--; } | 18 virtual ~CountedBrowserAccessibility() { global_obj_count_--; } |
| 19 static int global_obj_count_; | 19 static int global_obj_count_; |
| 20 }; | 20 }; |
| 21 | 21 |
| 22 int CountedBrowserAccessibility::global_obj_count_ = 0; | 22 int CountedBrowserAccessibility::global_obj_count_ = 0; |
| 23 | 23 |
| 24 // Factory that creates a CountedBrowserAccessibility. | 24 // Factory that creates a CountedBrowserAccessibility. |
| 25 class CountedBrowserAccessibilityFactory : public BrowserAccessibilityFactory { | 25 class CountedBrowserAccessibilityFactory : public BrowserAccessibilityFactory { |
| 26 public: | 26 public: |
| 27 virtual ~CountedBrowserAccessibilityFactory() {} | 27 virtual ~CountedBrowserAccessibilityFactory() {} |
| 28 virtual BrowserAccessibility* Create() { | 28 virtual BrowserAccessibilityWin* Create() { |
| 29 CComObject<CountedBrowserAccessibility>* instance; | 29 CComObject<CountedBrowserAccessibility>* instance; |
| 30 HRESULT hr = CComObject<CountedBrowserAccessibility>::CreateInstance( | 30 HRESULT hr = CComObject<CountedBrowserAccessibility>::CreateInstance( |
| 31 &instance); | 31 &instance); |
| 32 DCHECK(SUCCEEDED(hr)); | 32 DCHECK(SUCCEEDED(hr)); |
| 33 return instance->NewReference(); | 33 return instance->NewReference(); |
| 34 } | 34 } |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 VARIANT CreateI4Variant(LONG value) { | 37 VARIANT CreateI4Variant(LONG value) { |
| 38 VARIANT variant = {0}; | 38 VARIANT variant = {0}; |
| 39 | 39 |
| 40 V_VT(&variant) = VT_I4; | 40 V_VT(&variant) = VT_I4; |
| 41 V_I4(&variant) = value; | 41 V_I4(&variant) = value; |
| 42 | 42 |
| 43 return variant; | 43 return variant; |
| 44 } | 44 } |
| 45 | 45 |
| 46 class BrowserAccessibilityTest : public testing::Test { | 46 class BrowserAccessibilityTest : public testing::Test { |
| 47 protected: | 47 protected: |
| 48 virtual void SetUp() { | 48 virtual void SetUp() { |
| 49 // ATL needs a pointer to a COM module. | 49 // ATL needs a pointer to a COM module. |
| 50 static CComModule module; | 50 static CComModule module; |
| 51 _pAtlModule = &module; | 51 _pAtlModule = &module; |
| 52 | 52 |
| 53 // Make sure COM is initialized for this thread; it's safe to call twice. | 53 // Make sure COM is initialized for this thread; it's safe to call twice. |
| 54 ::CoInitialize(NULL); | 54 ::CoInitialize(NULL); |
| 55 } | 55 } |
| 56 | 56 |
| 57 virtual void TearDown() | 57 virtual void TearDown() { |
| 58 { | |
| 59 ::CoUninitialize(); | 58 ::CoUninitialize(); |
| 60 } | 59 } |
| 61 }; | 60 }; |
| 62 | 61 |
| 63 // Test that BrowserAccessibilityManager correctly releases the tree of | 62 // Test that BrowserAccessibilityManager correctly releases the tree of |
| 64 // BrowserAccessibility instances upon delete. | 63 // BrowserAccessibility instances upon delete. |
| 65 TEST_F(BrowserAccessibilityTest, TestNoLeaks) { | 64 TEST_F(BrowserAccessibilityTest, TestNoLeaks) { |
| 66 // Create WebAccessibility objects for a simple document tree, | 65 // Create WebAccessibility objects for a simple document tree, |
| 67 // representing the accessibility information used to initialize | 66 // representing the accessibility information used to initialize |
| 68 // BrowserAccessibilityManager. | 67 // BrowserAccessibilityManager. |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 85 root.state = 0; | 84 root.state = 0; |
| 86 root.children.push_back(button); | 85 root.children.push_back(button); |
| 87 root.children.push_back(checkbox); | 86 root.children.push_back(checkbox); |
| 88 | 87 |
| 89 // Construct a BrowserAccessibilityManager with this WebAccessibility tree | 88 // Construct a BrowserAccessibilityManager with this WebAccessibility tree |
| 90 // and a factory for an instance-counting BrowserAccessibility, and ensure | 89 // and a factory for an instance-counting BrowserAccessibility, and ensure |
| 91 // that exactly 3 instances were created. Note that the manager takes | 90 // that exactly 3 instances were created. Note that the manager takes |
| 92 // ownership of the factory. | 91 // ownership of the factory. |
| 93 CountedBrowserAccessibility::global_obj_count_ = 0; | 92 CountedBrowserAccessibility::global_obj_count_ = 0; |
| 94 BrowserAccessibilityManager* manager = | 93 BrowserAccessibilityManager* manager = |
| 95 new BrowserAccessibilityManager( | 94 new BrowserAccessibilityManagerWin( |
| 96 GetDesktopWindow(), root, NULL, | 95 GetDesktopWindow(), root, NULL, |
| 97 new CountedBrowserAccessibilityFactory()); | 96 new CountedBrowserAccessibilityFactory()); |
| 98 ASSERT_EQ(3, CountedBrowserAccessibility::global_obj_count_); | 97 ASSERT_EQ(3, CountedBrowserAccessibility::global_obj_count_); |
| 99 | 98 |
| 100 // Delete the manager and test that all 3 instances are deleted. | 99 // Delete the manager and test that all 3 instances are deleted. |
| 101 delete manager; | 100 delete manager; |
| 102 ASSERT_EQ(0, CountedBrowserAccessibility::global_obj_count_); | 101 ASSERT_EQ(0, CountedBrowserAccessibility::global_obj_count_); |
| 103 | 102 |
| 104 // Construct a manager again, and this time use the IAccessible interface | 103 // Construct a manager again, and this time use the IAccessible interface |
| 105 // to get new references to two of the three nodes in the tree. | 104 // to get new references to two of the three nodes in the tree. |
| 106 manager = new BrowserAccessibilityManager( | 105 manager = new BrowserAccessibilityManagerWin( |
| 107 GetDesktopWindow(), root, NULL, | 106 GetDesktopWindow(), root, NULL, |
| 108 new CountedBrowserAccessibilityFactory()); | 107 new CountedBrowserAccessibilityFactory()); |
| 109 ASSERT_EQ(3, CountedBrowserAccessibility::global_obj_count_); | 108 ASSERT_EQ(3, CountedBrowserAccessibility::global_obj_count_); |
| 110 BrowserAccessibility* root_accessible = manager->GetRoot(); | 109 IAccessible* root_accessible = manager->GetRootAccessible(); |
| 111 IDispatch* root_iaccessible = NULL; | 110 IDispatch* root_iaccessible = NULL; |
| 112 IDispatch* child1_iaccessible = NULL; | 111 IDispatch* child1_iaccessible = NULL; |
| 113 VARIANT var_child; | 112 VARIANT var_child; |
| 114 var_child.vt = VT_I4; | 113 var_child.vt = VT_I4; |
| 115 var_child.lVal = CHILDID_SELF; | 114 var_child.lVal = CHILDID_SELF; |
| 116 HRESULT hr = root_accessible->get_accChild(var_child, &root_iaccessible); | 115 HRESULT hr = root_accessible->get_accChild(var_child, &root_iaccessible); |
| 117 ASSERT_EQ(S_OK, hr); | 116 ASSERT_EQ(S_OK, hr); |
| 118 var_child.lVal = 1; | 117 var_child.lVal = 1; |
| 119 hr = root_accessible->get_accChild(var_child, &child1_iaccessible); | 118 hr = root_accessible->get_accChild(var_child, &child1_iaccessible); |
| 120 ASSERT_EQ(S_OK, hr); | 119 ASSERT_EQ(S_OK, hr); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 146 root.id = 1; | 145 root.id = 1; |
| 147 root.name = L"Document"; | 146 root.name = L"Document"; |
| 148 root.role = WebAccessibility::ROLE_DOCUMENT; | 147 root.role = WebAccessibility::ROLE_DOCUMENT; |
| 149 root.state = 0; | 148 root.state = 0; |
| 150 root.children.push_back(text); | 149 root.children.push_back(text); |
| 151 | 150 |
| 152 // Construct a BrowserAccessibilityManager with this WebAccessibility tree | 151 // Construct a BrowserAccessibilityManager with this WebAccessibility tree |
| 153 // and a factory for an instance-counting BrowserAccessibility. | 152 // and a factory for an instance-counting BrowserAccessibility. |
| 154 CountedBrowserAccessibility::global_obj_count_ = 0; | 153 CountedBrowserAccessibility::global_obj_count_ = 0; |
| 155 BrowserAccessibilityManager* manager = | 154 BrowserAccessibilityManager* manager = |
| 156 new BrowserAccessibilityManager( | 155 new BrowserAccessibilityManagerWin( |
| 157 GetDesktopWindow(), root, NULL, | 156 GetDesktopWindow(), root, NULL, |
| 158 new CountedBrowserAccessibilityFactory()); | 157 new CountedBrowserAccessibilityFactory()); |
| 159 | 158 |
| 160 // Query for the text IAccessible and verify that it returns "old text" as its | 159 // Query for the text IAccessible and verify that it returns "old text" as its |
| 161 // value. | 160 // value. |
| 162 ScopedComPtr<IDispatch> text_dispatch; | 161 ScopedComPtr<IDispatch> text_dispatch; |
| 163 HRESULT hr = manager->GetRoot()->get_accChild(CreateI4Variant(1), | 162 HRESULT hr = manager->GetRootAccessible()->get_accChild( |
| 164 text_dispatch.Receive()); | 163 CreateI4Variant(1), text_dispatch.Receive()); |
| 165 ASSERT_EQ(S_OK, hr); | 164 ASSERT_EQ(S_OK, hr); |
| 166 | 165 |
| 167 ScopedComPtr<IAccessible> text_accessible; | 166 ScopedComPtr<IAccessible> text_accessible; |
| 168 hr = text_dispatch.QueryInterface(text_accessible.Receive()); | 167 hr = text_dispatch.QueryInterface(text_accessible.Receive()); |
| 169 ASSERT_EQ(S_OK, hr); | 168 ASSERT_EQ(S_OK, hr); |
| 170 | 169 |
| 171 CComBSTR value; | 170 CComBSTR value; |
| 172 hr = text_accessible->get_accValue(CreateI4Variant(CHILDID_SELF), &value); | 171 hr = text_accessible->get_accValue(CreateI4Variant(CHILDID_SELF), &value); |
| 173 ASSERT_EQ(S_OK, hr); | 172 ASSERT_EQ(S_OK, hr); |
| 174 EXPECT_STREQ(L"old text", value.m_str); | 173 EXPECT_STREQ(L"old text", value.m_str); |
| 175 | 174 |
| 176 text_dispatch.Release(); | 175 text_dispatch.Release(); |
| 177 text_accessible.Release(); | 176 text_accessible.Release(); |
| 178 | 177 |
| 179 // Notify the BrowserAccessibilityManager that the text child has changed. | 178 // Notify the BrowserAccessibilityManager that the text child has changed. |
| 180 text.value = L"new text"; | 179 text.value = L"new text"; |
| 181 ViewHostMsg_AccessibilityNotification_Params param; | 180 ViewHostMsg_AccessibilityNotification_Params param; |
| 182 param.notification_type = | 181 param.notification_type = |
| 183 ViewHostMsg_AccessibilityNotification_Params:: | 182 ViewHostMsg_AccessibilityNotification_Params:: |
| 184 NOTIFICATION_TYPE_CHILDREN_CHANGED; | 183 NOTIFICATION_TYPE_CHILDREN_CHANGED; |
| 185 param.acc_obj = text; | 184 param.acc_obj = text; |
| 186 std::vector<ViewHostMsg_AccessibilityNotification_Params> notifications; | 185 std::vector<ViewHostMsg_AccessibilityNotification_Params> notifications; |
| 187 notifications.push_back(param); | 186 notifications.push_back(param); |
| 188 manager->OnAccessibilityNotifications(notifications); | 187 manager->OnAccessibilityNotifications(notifications); |
| 189 | 188 |
| 190 // Query for the text IAccessible and verify that it now returns "new text" | 189 // Query for the text IAccessible and verify that it now returns "new text" |
| 191 // as its value. | 190 // as its value. |
| 192 hr = manager->GetRoot()->get_accChild( | 191 hr = manager->GetRootAccessible()->get_accChild( |
| 193 CreateI4Variant(1), | 192 CreateI4Variant(1), |
| 194 text_dispatch.Receive()); | 193 text_dispatch.Receive()); |
| 195 ASSERT_EQ(S_OK, hr); | 194 ASSERT_EQ(S_OK, hr); |
| 196 | 195 |
| 197 hr = text_dispatch.QueryInterface(text_accessible.Receive()); | 196 hr = text_dispatch.QueryInterface(text_accessible.Receive()); |
| 198 ASSERT_EQ(S_OK, hr); | 197 ASSERT_EQ(S_OK, hr); |
| 199 | 198 |
| 200 hr = text_accessible->get_accValue(CreateI4Variant(CHILDID_SELF), &value); | 199 hr = text_accessible->get_accValue(CreateI4Variant(CHILDID_SELF), &value); |
| 201 ASSERT_EQ(S_OK, hr); | 200 ASSERT_EQ(S_OK, hr); |
| 202 EXPECT_STREQ(L"new text", value.m_str); | 201 EXPECT_STREQ(L"new text", value.m_str); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 233 root.role = WebAccessibility::ROLE_DOCUMENT; | 232 root.role = WebAccessibility::ROLE_DOCUMENT; |
| 234 root.state = 0; | 233 root.state = 0; |
| 235 root.children.push_back(div); | 234 root.children.push_back(div); |
| 236 | 235 |
| 237 // Construct a BrowserAccessibilityManager with this WebAccessibility tree | 236 // Construct a BrowserAccessibilityManager with this WebAccessibility tree |
| 238 // and a factory for an instance-counting BrowserAccessibility and ensure | 237 // and a factory for an instance-counting BrowserAccessibility and ensure |
| 239 // that exactly 4 instances were created. Note that the manager takes | 238 // that exactly 4 instances were created. Note that the manager takes |
| 240 // ownership of the factory. | 239 // ownership of the factory. |
| 241 CountedBrowserAccessibility::global_obj_count_ = 0; | 240 CountedBrowserAccessibility::global_obj_count_ = 0; |
| 242 BrowserAccessibilityManager* manager = | 241 BrowserAccessibilityManager* manager = |
| 243 new BrowserAccessibilityManager( | 242 new BrowserAccessibilityManagerWin( |
|
dmazzoni
2010/10/01 17:14:37
Can you use BrowserAccessibilityManager::Create in
Chris Guillory
2010/10/01 17:42:23
I think factory is one of the things that should m
| |
| 244 GetDesktopWindow(), root, NULL, | 243 GetDesktopWindow(), root, NULL, |
| 245 new CountedBrowserAccessibilityFactory()); | 244 new CountedBrowserAccessibilityFactory()); |
| 246 ASSERT_EQ(4, CountedBrowserAccessibility::global_obj_count_); | 245 ASSERT_EQ(4, CountedBrowserAccessibility::global_obj_count_); |
| 247 | 246 |
| 248 // Notify the BrowserAccessibilityManager that the div node and its children | 247 // Notify the BrowserAccessibilityManager that the div node and its children |
| 249 // were removed and ensure that only one BrowserAccessibility instance exists. | 248 // were removed and ensure that only one BrowserAccessibility instance exists. |
| 250 root.children.clear(); | 249 root.children.clear(); |
| 251 ViewHostMsg_AccessibilityNotification_Params param; | 250 ViewHostMsg_AccessibilityNotification_Params param; |
| 252 param.notification_type = | 251 param.notification_type = |
| 253 ViewHostMsg_AccessibilityNotification_Params:: | 252 ViewHostMsg_AccessibilityNotification_Params:: |
| 254 NOTIFICATION_TYPE_CHILDREN_CHANGED; | 253 NOTIFICATION_TYPE_CHILDREN_CHANGED; |
| 255 param.acc_obj = root; | 254 param.acc_obj = root; |
| 256 std::vector<ViewHostMsg_AccessibilityNotification_Params> notifications; | 255 std::vector<ViewHostMsg_AccessibilityNotification_Params> notifications; |
| 257 notifications.push_back(param); | 256 notifications.push_back(param); |
| 258 manager->OnAccessibilityNotifications(notifications); | 257 manager->OnAccessibilityNotifications(notifications); |
| 259 ASSERT_EQ(1, CountedBrowserAccessibility::global_obj_count_); | 258 ASSERT_EQ(1, CountedBrowserAccessibility::global_obj_count_); |
| 260 | 259 |
| 261 // Delete the manager and test that all BrowserAccessibility instances are | 260 // Delete the manager and test that all BrowserAccessibility instances are |
| 262 // deleted. | 261 // deleted. |
| 263 delete manager; | 262 delete manager; |
| 264 ASSERT_EQ(0, CountedBrowserAccessibility::global_obj_count_); | 263 ASSERT_EQ(0, CountedBrowserAccessibility::global_obj_count_); |
| 265 } | 264 } |
| OLD | NEW |