Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: chrome/browser/browser_accessibility_unittest.cc

Issue 2830005: Rename browser_accessibility to browser_accessibility_win, and same for... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_BUTTON;
51 button.state = 0;
52
53 WebAccessibility checkbox;
54 checkbox.id = 3;
55 checkbox.name = L"Checkbox";
56 checkbox.role = WebAccessibility::ROLE_CHECKBOX;
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, NULL,
75 new CountedBrowserAccessibilityFactory());
76 ASSERT_EQ(3, CountedBrowserAccessibility::global_obj_count_);
77
78 // Delete the manager and test that all 3 instances are deleted.
79 delete manager;
80 ASSERT_EQ(0, CountedBrowserAccessibility::global_obj_count_);
81
82 // Construct a manager again, and this time use the IAccessible interface
83 // to get new references to two of the three nodes in the tree.
84 manager = new BrowserAccessibilityManager(
85 GetDesktopWindow(), root, NULL,
86 new CountedBrowserAccessibilityFactory());
87 ASSERT_EQ(3, CountedBrowserAccessibility::global_obj_count_);
88 BrowserAccessibility* root_accessible = manager->GetRoot();
89 IDispatch* root_iaccessible = NULL;
90 IDispatch* child1_iaccessible = NULL;
91 VARIANT var_child;
92 var_child.vt = VT_I4;
93 var_child.lVal = CHILDID_SELF;
94 HRESULT hr = root_accessible->get_accChild(var_child, &root_iaccessible);
95 ASSERT_EQ(S_OK, hr);
96 var_child.lVal = 1;
97 hr = root_accessible->get_accChild(var_child, &child1_iaccessible);
98 ASSERT_EQ(S_OK, hr);
99
100 // Now delete the manager, and only one of the three nodes in the tree
101 // should be released.
102 delete manager;
103 ASSERT_EQ(2, CountedBrowserAccessibility::global_obj_count_);
104
105 // Release each of our references and make sure that each one results in
106 // the instance being deleted as its reference count hits zero.
107 root_iaccessible->Release();
108 ASSERT_EQ(1, CountedBrowserAccessibility::global_obj_count_);
109 child1_iaccessible->Release();
110 ASSERT_EQ(0, CountedBrowserAccessibility::global_obj_count_);
111 }
OLDNEW
« no previous file with comments | « chrome/browser/browser_accessibility_manager_win.cc ('k') | chrome/browser/browser_accessibility_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698