| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/scoped_ptr.h" | |
| 6 #include "chrome/browser/browser_accessibility_manager.h" | |
| 7 #include "chrome/browser/browser_accessibility.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 using webkit_glue::WebAccessibility; | |
| 11 | |
| 12 // Subclass of BrowserAccessibility that counts the number of instances. | |
| 13 class CountedBrowserAccessibility : public BrowserAccessibility { | |
| 14 public: | |
| 15 CountedBrowserAccessibility() { global_obj_count_++; } | |
| 16 virtual ~CountedBrowserAccessibility() { global_obj_count_--; } | |
| 17 static int global_obj_count_; | |
| 18 }; | |
| 19 | |
| 20 int CountedBrowserAccessibility::global_obj_count_ = 0; | |
| 21 | |
| 22 // Factory that creates a CountedBrowserAccessibility. | |
| 23 class CountedBrowserAccessibilityFactory : public BrowserAccessibilityFactory { | |
| 24 public: | |
| 25 virtual ~CountedBrowserAccessibilityFactory() {} | |
| 26 virtual BrowserAccessibility* Create() { | |
| 27 CComObject<CountedBrowserAccessibility>* instance; | |
| 28 HRESULT hr = CComObject<CountedBrowserAccessibility>::CreateInstance( | |
| 29 &instance); | |
| 30 DCHECK(SUCCEEDED(hr)); | |
| 31 return instance->NewReference(); | |
| 32 } | |
| 33 }; | |
| 34 | |
| 35 // Test that BrowserAccessibilityManager correctly releases the tree of | |
| 36 // BrowserAccessibility instances upon delete. | |
| 37 TEST(BrowserAccessibilityTest, TestNoLeaks) { | |
| 38 // ATL needs a pointer to a COM module. | |
| 39 CComModule module; | |
| 40 _pAtlModule = &module; | |
| 41 // Make sure COM is initialized for this thread; it's safe to call twice. | |
| 42 ::CoInitialize(NULL); | |
| 43 | |
| 44 // Create WebAccessibility objects for a simple document tree, | |
| 45 // representing the accessibility information used to initialize | |
| 46 // BrowserAccessibilityManager. | |
| 47 WebAccessibility button; | |
| 48 button.id = 2; | |
| 49 button.name = L"Button"; | |
| 50 button.role = WebAccessibility::ROLE_PUSHBUTTON; | |
| 51 button.state = 0; | |
| 52 | |
| 53 WebAccessibility checkbox; | |
| 54 checkbox.id = 3; | |
| 55 checkbox.name = L"Checkbox"; | |
| 56 checkbox.role = WebAccessibility::ROLE_CHECKBUTTON; | |
| 57 checkbox.state = 0; | |
| 58 | |
| 59 WebAccessibility root; | |
| 60 root.id = 1; | |
| 61 root.name = L"Document"; | |
| 62 root.role = WebAccessibility::ROLE_DOCUMENT; | |
| 63 root.state = 0; | |
| 64 root.children.push_back(button); | |
| 65 root.children.push_back(checkbox); | |
| 66 | |
| 67 // Construct a BrowserAccessibilityManager with this WebAccessibility tree | |
| 68 // and a factory for an instance-counting BrowserAccessibility, and ensure | |
| 69 // that exactly 3 instances were created. Note that the manager takes | |
| 70 // ownership of the factory. | |
| 71 CountedBrowserAccessibility::global_obj_count_ = 0; | |
| 72 BrowserAccessibilityManager* manager = | |
| 73 new BrowserAccessibilityManager( | |
| 74 GetDesktopWindow(), root, new CountedBrowserAccessibilityFactory()); | |
| 75 ASSERT_EQ(3, CountedBrowserAccessibility::global_obj_count_); | |
| 76 | |
| 77 // Delete the manager and test that all 3 instances are deleted. | |
| 78 delete manager; | |
| 79 ASSERT_EQ(0, CountedBrowserAccessibility::global_obj_count_); | |
| 80 | |
| 81 // Construct a manager again, and this time use the IAccessible interface | |
| 82 // to get new references to two of the three nodes in the tree. | |
| 83 manager = new BrowserAccessibilityManager( | |
| 84 GetDesktopWindow(), root, new CountedBrowserAccessibilityFactory()); | |
| 85 ASSERT_EQ(3, CountedBrowserAccessibility::global_obj_count_); | |
| 86 BrowserAccessibility* root_accessible = manager->GetRoot(); | |
| 87 IDispatch* root_iaccessible = NULL; | |
| 88 IDispatch* child1_iaccessible = NULL; | |
| 89 VARIANT var_child; | |
| 90 var_child.vt = VT_I4; | |
| 91 var_child.lVal = CHILDID_SELF; | |
| 92 HRESULT hr = root_accessible->get_accChild(var_child, &root_iaccessible); | |
| 93 ASSERT_EQ(S_OK, hr); | |
| 94 var_child.lVal = 1; | |
| 95 hr = root_accessible->get_accChild(var_child, &child1_iaccessible); | |
| 96 ASSERT_EQ(S_OK, hr); | |
| 97 | |
| 98 // Now delete the manager, and only one of the three nodes in the tree | |
| 99 // should be released. | |
| 100 delete manager; | |
| 101 ASSERT_EQ(2, CountedBrowserAccessibility::global_obj_count_); | |
| 102 | |
| 103 // Release each of our references and make sure that each one results in | |
| 104 // the instance being deleted as its reference count hits zero. | |
| 105 root_iaccessible->Release(); | |
| 106 ASSERT_EQ(1, CountedBrowserAccessibility::global_obj_count_); | |
| 107 child1_iaccessible->Release(); | |
| 108 ASSERT_EQ(0, CountedBrowserAccessibility::global_obj_count_); | |
| 109 } | |
| OLD | NEW |