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

Side by Side Diff: ui/views/accessibility/native_view_accessibility_win_unittest.cc

Issue 266963002: Expose an accessible relation between the main window and active alert. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ia2_1-3
Patch Set: Address feedback Created 6 years, 7 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
« no previous file with comments | « ui/views/accessibility/native_view_accessibility_win.cc ('k') | ui/views/views.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 <oleacc.h> 5 #include <oleacc.h>
6 6
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 "third_party/iaccessible2/ia2_api_all.h"
10 #include "ui/views/accessibility/native_view_accessibility.h" 11 #include "ui/views/accessibility/native_view_accessibility.h"
11 #include "ui/views/controls/textfield/textfield.h" 12 #include "ui/views/controls/textfield/textfield.h"
12 #include "ui/views/test/views_test_base.h" 13 #include "ui/views/test/views_test_base.h"
13 14
15 using base::win::ScopedBstr;
16 using base::win::ScopedComPtr;
17 using base::win::ScopedVariant;
18
14 namespace views { 19 namespace views {
15 namespace test { 20 namespace test {
16 21
17 typedef ViewsTestBase NativeViewAcccessibilityWinTest; 22 class NativeViewAcccessibilityWinTest : public ViewsTestBase {
23 public:
24 NativeViewAcccessibilityWinTest() {}
25 virtual ~NativeViewAcccessibilityWinTest() {}
26
27 protected:
28 void GetIAccessible2InterfaceForView(View* view, IAccessible2_2** result) {
29 ScopedComPtr<IAccessible> view_accessible(
30 view->GetNativeViewAccessible());
31 ScopedComPtr<IServiceProvider> service_provider;
32 ASSERT_EQ(S_OK, view_accessible.QueryInterface(service_provider.Receive()));
33 ASSERT_EQ(S_OK,
34 service_provider->QueryService(IID_IAccessible2_2, result));
35 }
36 };
18 37
19 TEST_F(NativeViewAcccessibilityWinTest, TextfieldAccessibility) { 38 TEST_F(NativeViewAcccessibilityWinTest, TextfieldAccessibility) {
20 Widget widget; 39 Widget widget;
21 Widget::InitParams init_params = 40 Widget::InitParams init_params =
22 CreateParams(Widget::InitParams::TYPE_POPUP); 41 CreateParams(Widget::InitParams::TYPE_POPUP);
23 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 42 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
24 widget.Init(init_params); 43 widget.Init(init_params);
25 44
26 View* content = new View; 45 View* content = new View;
27 widget.SetContentsView(content); 46 widget.SetContentsView(content);
28 47
29 Textfield* textfield = new Textfield; 48 Textfield* textfield = new Textfield;
30 textfield->SetAccessibleName(L"Name"); 49 textfield->SetAccessibleName(L"Name");
31 textfield->SetText(L"Value"); 50 textfield->SetText(L"Value");
32 content->AddChildView(textfield); 51 content->AddChildView(textfield);
33 52
34 base::win::ScopedComPtr<IAccessible> content_accessible( 53 ScopedComPtr<IAccessible> content_accessible(
35 content->GetNativeViewAccessible()); 54 content->GetNativeViewAccessible());
36 LONG child_count = 0; 55 LONG child_count = 0;
37 ASSERT_EQ(S_OK, content_accessible->get_accChildCount(&child_count)); 56 ASSERT_EQ(S_OK, content_accessible->get_accChildCount(&child_count));
38 ASSERT_EQ(1L, child_count); 57 ASSERT_EQ(1L, child_count);
39 58
40 base::win::ScopedComPtr<IDispatch> textfield_dispatch; 59 ScopedComPtr<IDispatch> textfield_dispatch;
41 base::win::ScopedComPtr<IAccessible> textfield_accessible; 60 ScopedComPtr<IAccessible> textfield_accessible;
42 base::win::ScopedVariant child_index(1); 61 ScopedVariant child_index(1);
43 ASSERT_EQ(S_OK, content_accessible->get_accChild( 62 ASSERT_EQ(S_OK, content_accessible->get_accChild(
44 child_index, textfield_dispatch.Receive())); 63 child_index, textfield_dispatch.Receive()));
45 ASSERT_EQ(S_OK, textfield_dispatch.QueryInterface( 64 ASSERT_EQ(S_OK, textfield_dispatch.QueryInterface(
46 textfield_accessible.Receive())); 65 textfield_accessible.Receive()));
47 66
48 base::win::ScopedBstr name; 67 ScopedBstr name;
49 base::win::ScopedVariant childid_self(CHILDID_SELF); 68 ScopedVariant childid_self(CHILDID_SELF);
50 ASSERT_EQ(S_OK, textfield_accessible->get_accName( 69 ASSERT_EQ(S_OK, textfield_accessible->get_accName(
51 childid_self, name.Receive())); 70 childid_self, name.Receive()));
52 ASSERT_STREQ(L"Name", name); 71 ASSERT_STREQ(L"Name", name);
53 72
54 base::win::ScopedBstr value; 73 ScopedBstr value;
55 ASSERT_EQ(S_OK, textfield_accessible->get_accValue( 74 ASSERT_EQ(S_OK, textfield_accessible->get_accValue(
56 childid_self, value.Receive())); 75 childid_self, value.Receive()));
57 ASSERT_STREQ(L"Value", value); 76 ASSERT_STREQ(L"Value", value);
58 77
59 base::win::ScopedBstr new_value(L"New value"); 78 ScopedBstr new_value(L"New value");
60 ASSERT_EQ(S_OK, textfield_accessible->put_accValue(childid_self, new_value)); 79 ASSERT_EQ(S_OK, textfield_accessible->put_accValue(childid_self, new_value));
61 80
62 ASSERT_STREQ(L"New value", textfield->text().c_str()); 81 ASSERT_STREQ(L"New value", textfield->text().c_str());
63 } 82 }
64 83
65 TEST_F(NativeViewAcccessibilityWinTest, UnattachedWebView) { 84 TEST_F(NativeViewAcccessibilityWinTest, UnattachedWebView) {
66 // This is a regression test. Calling get_accChild on the native accessible 85 // This is a regression test. Calling get_accChild on the native accessible
67 // object for a WebView with no attached WebContents was causing an 86 // object for a WebView with no attached WebContents was causing an
68 // infinite loop and crash. This test simulates that with an ordinary 87 // infinite loop and crash. This test simulates that with an ordinary
69 // View that registers itself as a web view with NativeViewAcccessibility. 88 // View that registers itself as a web view with NativeViewAcccessibility.
70 89
71 Widget widget; 90 Widget widget;
72 Widget::InitParams init_params = 91 Widget::InitParams init_params =
73 CreateParams(Widget::InitParams::TYPE_POPUP); 92 CreateParams(Widget::InitParams::TYPE_POPUP);
74 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 93 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
75 widget.Init(init_params); 94 widget.Init(init_params);
76 95
77 View* content = new View; 96 View* content = new View;
78 widget.SetContentsView(content); 97 widget.SetContentsView(content);
79 98
80 View* web_view = new View; 99 View* web_view = new View;
81 content->AddChildView(web_view); 100 content->AddChildView(web_view);
82 NativeViewAccessibility::RegisterWebView(web_view); 101 NativeViewAccessibility::RegisterWebView(web_view);
83 102
84 base::win::ScopedComPtr<IAccessible> web_view_accessible( 103 ScopedComPtr<IAccessible> web_view_accessible(
85 web_view->GetNativeViewAccessible()); 104 web_view->GetNativeViewAccessible());
86 base::win::ScopedComPtr<IDispatch> result_dispatch; 105 ScopedComPtr<IDispatch> result_dispatch;
87 base::win::ScopedVariant child_index(-999); 106 ScopedVariant child_index(-999);
88 ASSERT_EQ(E_FAIL, web_view_accessible->get_accChild( 107 ASSERT_EQ(E_FAIL, web_view_accessible->get_accChild(
89 child_index, result_dispatch.Receive())); 108 child_index, result_dispatch.Receive()));
90 109
91 NativeViewAccessibility::UnregisterWebView(web_view); 110 NativeViewAccessibility::UnregisterWebView(web_view);
92 } 111 }
93 112
94 TEST_F(NativeViewAcccessibilityWinTest, AuraOwnedWidgets) { 113 TEST_F(NativeViewAcccessibilityWinTest, AuraOwnedWidgets) {
95 Widget widget; 114 Widget widget;
96 Widget::InitParams init_params = 115 Widget::InitParams init_params =
97 CreateParams(Widget::InitParams::TYPE_WINDOW); 116 CreateParams(Widget::InitParams::TYPE_WINDOW);
98 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 117 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
99 widget.Init(init_params); 118 widget.Init(init_params);
100 119
101 base::win::ScopedComPtr<IAccessible> root_view_accessible( 120 ScopedComPtr<IAccessible> root_view_accessible(
102 widget.GetRootView()->GetNativeViewAccessible()); 121 widget.GetRootView()->GetNativeViewAccessible());
103 122
104 LONG child_count = 0; 123 LONG child_count = 0;
105 ASSERT_EQ(S_OK, root_view_accessible->get_accChildCount(&child_count)); 124 ASSERT_EQ(S_OK, root_view_accessible->get_accChildCount(&child_count));
106 ASSERT_EQ(1L, child_count); 125 ASSERT_EQ(1L, child_count);
107 126
108 Widget owned_widget; 127 Widget owned_widget;
109 Widget::InitParams owned_init_params = 128 Widget::InitParams owned_init_params =
110 CreateParams(Widget::InitParams::TYPE_POPUP); 129 CreateParams(Widget::InitParams::TYPE_POPUP);
111 owned_init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 130 owned_init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
112 owned_init_params.child = false; 131 owned_init_params.child = false;
113 owned_init_params.parent = widget.GetNativeView(); 132 owned_init_params.parent = widget.GetNativeView();
114 owned_widget.Init(owned_init_params); 133 owned_widget.Init(owned_init_params);
115 owned_widget.Show(); 134 owned_widget.Show();
116 135
117 ASSERT_EQ(S_OK, root_view_accessible->get_accChildCount(&child_count)); 136 ASSERT_EQ(S_OK, root_view_accessible->get_accChildCount(&child_count));
118 ASSERT_EQ(2L, child_count); 137 ASSERT_EQ(2L, child_count);
119 } 138 }
120 139
140 TEST_F(NativeViewAcccessibilityWinTest, RetrieveAllAlerts) {
141 Widget widget;
142 Widget::InitParams init_params =
143 CreateParams(Widget::InitParams::TYPE_POPUP);
144 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
145 widget.Init(init_params);
146
147 View* content = new View;
148 widget.SetContentsView(content);
149
150 View* infobar = new View;
151 content->AddChildView(infobar);
152
153 View* infobar2 = new View;
154 content->AddChildView(infobar2);
155
156 View* root_view = content->parent();
157 ASSERT_EQ(NULL, root_view->parent());
158
159 ScopedComPtr<IAccessible2_2> root_view_accessible;
160 GetIAccessible2InterfaceForView(root_view, root_view_accessible.Receive());
161
162 ScopedComPtr<IAccessible2_2> infobar_accessible;
163 GetIAccessible2InterfaceForView(infobar, infobar_accessible.Receive());
164
165 ScopedComPtr<IAccessible2_2> infobar2_accessible;
166 GetIAccessible2InterfaceForView(infobar2, infobar2_accessible.Receive());
167
168 // Initially, there are no alerts
169 ScopedBstr alerts_bstr(L"alerts");
170 IUnknown** targets;
171 long n_targets;
172 ASSERT_EQ(S_FALSE, root_view_accessible->get_relationTargetsOfType(
173 alerts_bstr, 0, &targets, &n_targets));
174 ASSERT_EQ(0, n_targets);
175
176 // Fire alert events on the infobars.
177 infobar->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true);
178 infobar2->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true);
179
180 // Now calling get_relationTargetsOfType should retrieve the alerts.
181 ASSERT_EQ(S_OK, root_view_accessible->get_relationTargetsOfType(
182 alerts_bstr, 0, &targets, &n_targets));
183 ASSERT_EQ(2, n_targets);
184 ASSERT_TRUE(infobar_accessible.IsSameObject(targets[0]));
185 ASSERT_TRUE(infobar2_accessible.IsSameObject(targets[1]));
186 CoTaskMemFree(targets);
187
188 // If we set max_targets to 1, we should only get the first one.
189 ASSERT_EQ(S_OK, root_view_accessible->get_relationTargetsOfType(
190 alerts_bstr, 1, &targets, &n_targets));
191 ASSERT_EQ(1, n_targets);
192 ASSERT_TRUE(infobar_accessible.IsSameObject(targets[0]));
193 CoTaskMemFree(targets);
194
195 // If we delete the first view, we should only get the second one now.
196 delete infobar;
197 ASSERT_EQ(S_OK, root_view_accessible->get_relationTargetsOfType(
198 alerts_bstr, 0, &targets, &n_targets));
199 ASSERT_EQ(1, n_targets);
200 ASSERT_TRUE(infobar2_accessible.IsSameObject(targets[0]));
201 CoTaskMemFree(targets);
202 }
203
121 } // namespace test 204 } // namespace test
122 } // namespace views 205 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/accessibility/native_view_accessibility_win.cc ('k') | ui/views/views.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698