OLD | NEW |
---|---|
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 view_accessible.QueryInterface(service_provider.Receive()); | |
David Tseng
2014/05/13 01:34:54
Dup with below?
dmazzoni
2014/05/13 07:38:46
Done.
| |
33 ASSERT_EQ(view_accessible.QueryInterface(service_provider.Receive()), S_OK); | |
David Tseng
2014/05/13 01:34:54
(S_OK, ...); i.e. ASSERT_EQ(expected, actual)
dmazzoni
2014/05/13 07:38:46
Done.
| |
34 ASSERT_EQ(S_OK, | |
35 service_provider->QueryService(IID_IAccessible2_2, result)); | |
36 } | |
37 }; | |
18 | 38 |
19 TEST_F(NativeViewAcccessibilityWinTest, TextfieldAccessibility) { | 39 TEST_F(NativeViewAcccessibilityWinTest, TextfieldAccessibility) { |
20 Widget widget; | 40 Widget widget; |
21 Widget::InitParams init_params = | 41 Widget::InitParams init_params = |
22 CreateParams(Widget::InitParams::TYPE_POPUP); | 42 CreateParams(Widget::InitParams::TYPE_POPUP); |
23 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 43 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
24 widget.Init(init_params); | 44 widget.Init(init_params); |
25 | 45 |
26 View* content = new View; | 46 View* content = new View; |
27 widget.SetContentsView(content); | 47 widget.SetContentsView(content); |
28 | 48 |
29 Textfield* textfield = new Textfield; | 49 Textfield* textfield = new Textfield; |
30 textfield->SetAccessibleName(L"Name"); | 50 textfield->SetAccessibleName(L"Name"); |
31 textfield->SetText(L"Value"); | 51 textfield->SetText(L"Value"); |
32 content->AddChildView(textfield); | 52 content->AddChildView(textfield); |
33 | 53 |
34 base::win::ScopedComPtr<IAccessible> content_accessible( | 54 ScopedComPtr<IAccessible> content_accessible( |
35 content->GetNativeViewAccessible()); | 55 content->GetNativeViewAccessible()); |
36 LONG child_count = 0; | 56 LONG child_count = 0; |
37 ASSERT_EQ(S_OK, content_accessible->get_accChildCount(&child_count)); | 57 ASSERT_EQ(S_OK, content_accessible->get_accChildCount(&child_count)); |
38 ASSERT_EQ(1L, child_count); | 58 ASSERT_EQ(1L, child_count); |
39 | 59 |
40 base::win::ScopedComPtr<IDispatch> textfield_dispatch; | 60 ScopedComPtr<IDispatch> textfield_dispatch; |
41 base::win::ScopedComPtr<IAccessible> textfield_accessible; | 61 ScopedComPtr<IAccessible> textfield_accessible; |
42 base::win::ScopedVariant child_index(1); | 62 ScopedVariant child_index(1); |
43 ASSERT_EQ(S_OK, content_accessible->get_accChild( | 63 ASSERT_EQ(S_OK, content_accessible->get_accChild( |
44 child_index, textfield_dispatch.Receive())); | 64 child_index, textfield_dispatch.Receive())); |
45 ASSERT_EQ(S_OK, textfield_dispatch.QueryInterface( | 65 ASSERT_EQ(S_OK, textfield_dispatch.QueryInterface( |
46 textfield_accessible.Receive())); | 66 textfield_accessible.Receive())); |
47 | 67 |
48 base::win::ScopedBstr name; | 68 ScopedBstr name; |
49 base::win::ScopedVariant childid_self(CHILDID_SELF); | 69 ScopedVariant childid_self(CHILDID_SELF); |
50 ASSERT_EQ(S_OK, textfield_accessible->get_accName( | 70 ASSERT_EQ(S_OK, textfield_accessible->get_accName( |
51 childid_self, name.Receive())); | 71 childid_self, name.Receive())); |
52 ASSERT_STREQ(L"Name", name); | 72 ASSERT_STREQ(L"Name", name); |
53 | 73 |
54 base::win::ScopedBstr value; | 74 ScopedBstr value; |
55 ASSERT_EQ(S_OK, textfield_accessible->get_accValue( | 75 ASSERT_EQ(S_OK, textfield_accessible->get_accValue( |
56 childid_self, value.Receive())); | 76 childid_self, value.Receive())); |
57 ASSERT_STREQ(L"Value", value); | 77 ASSERT_STREQ(L"Value", value); |
58 | 78 |
59 base::win::ScopedBstr new_value(L"New value"); | 79 ScopedBstr new_value(L"New value"); |
60 ASSERT_EQ(S_OK, textfield_accessible->put_accValue(childid_self, new_value)); | 80 ASSERT_EQ(S_OK, textfield_accessible->put_accValue(childid_self, new_value)); |
61 | 81 |
62 ASSERT_STREQ(L"New value", textfield->text().c_str()); | 82 ASSERT_STREQ(L"New value", textfield->text().c_str()); |
63 } | 83 } |
64 | 84 |
65 TEST_F(NativeViewAcccessibilityWinTest, UnattachedWebView) { | 85 TEST_F(NativeViewAcccessibilityWinTest, UnattachedWebView) { |
66 // This is a regression test. Calling get_accChild on the native accessible | 86 // This is a regression test. Calling get_accChild on the native accessible |
67 // object for a WebView with no attached WebContents was causing an | 87 // object for a WebView with no attached WebContents was causing an |
68 // infinite loop and crash. This test simulates that with an ordinary | 88 // infinite loop and crash. This test simulates that with an ordinary |
69 // View that registers itself as a web view with NativeViewAcccessibility. | 89 // View that registers itself as a web view with NativeViewAcccessibility. |
70 | 90 |
71 Widget widget; | 91 Widget widget; |
72 Widget::InitParams init_params = | 92 Widget::InitParams init_params = |
73 CreateParams(Widget::InitParams::TYPE_POPUP); | 93 CreateParams(Widget::InitParams::TYPE_POPUP); |
74 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 94 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
75 widget.Init(init_params); | 95 widget.Init(init_params); |
76 | 96 |
77 View* content = new View; | 97 View* content = new View; |
78 widget.SetContentsView(content); | 98 widget.SetContentsView(content); |
79 | 99 |
80 View* web_view = new View; | 100 View* web_view = new View; |
81 content->AddChildView(web_view); | 101 content->AddChildView(web_view); |
82 NativeViewAccessibility::RegisterWebView(web_view); | 102 NativeViewAccessibility::RegisterWebView(web_view); |
83 | 103 |
84 base::win::ScopedComPtr<IAccessible> web_view_accessible( | 104 ScopedComPtr<IAccessible> web_view_accessible( |
85 web_view->GetNativeViewAccessible()); | 105 web_view->GetNativeViewAccessible()); |
86 base::win::ScopedComPtr<IDispatch> result_dispatch; | 106 ScopedComPtr<IDispatch> result_dispatch; |
87 base::win::ScopedVariant child_index(-999); | 107 ScopedVariant child_index(-999); |
88 ASSERT_EQ(E_FAIL, web_view_accessible->get_accChild( | 108 ASSERT_EQ(E_FAIL, web_view_accessible->get_accChild( |
89 child_index, result_dispatch.Receive())); | 109 child_index, result_dispatch.Receive())); |
90 | 110 |
91 NativeViewAccessibility::UnregisterWebView(web_view); | 111 NativeViewAccessibility::UnregisterWebView(web_view); |
92 } | 112 } |
93 | 113 |
94 TEST_F(NativeViewAcccessibilityWinTest, AuraOwnedWidgets) { | 114 TEST_F(NativeViewAcccessibilityWinTest, AuraOwnedWidgets) { |
95 Widget widget; | 115 Widget widget; |
96 Widget::InitParams init_params = | 116 Widget::InitParams init_params = |
97 CreateParams(Widget::InitParams::TYPE_WINDOW); | 117 CreateParams(Widget::InitParams::TYPE_WINDOW); |
98 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 118 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
99 widget.Init(init_params); | 119 widget.Init(init_params); |
100 | 120 |
101 base::win::ScopedComPtr<IAccessible> root_view_accessible( | 121 ScopedComPtr<IAccessible> root_view_accessible( |
102 widget.GetRootView()->GetNativeViewAccessible()); | 122 widget.GetRootView()->GetNativeViewAccessible()); |
103 | 123 |
104 LONG child_count = 0; | 124 LONG child_count = 0; |
105 ASSERT_EQ(S_OK, root_view_accessible->get_accChildCount(&child_count)); | 125 ASSERT_EQ(S_OK, root_view_accessible->get_accChildCount(&child_count)); |
106 ASSERT_EQ(1L, child_count); | 126 ASSERT_EQ(1L, child_count); |
107 | 127 |
108 Widget owned_widget; | 128 Widget owned_widget; |
109 Widget::InitParams owned_init_params = | 129 Widget::InitParams owned_init_params = |
110 CreateParams(Widget::InitParams::TYPE_POPUP); | 130 CreateParams(Widget::InitParams::TYPE_POPUP); |
111 owned_init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 131 owned_init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
112 owned_init_params.child = false; | 132 owned_init_params.child = false; |
113 owned_init_params.parent = widget.GetNativeView(); | 133 owned_init_params.parent = widget.GetNativeView(); |
114 owned_widget.Init(owned_init_params); | 134 owned_widget.Init(owned_init_params); |
115 owned_widget.Show(); | 135 owned_widget.Show(); |
116 | 136 |
117 ASSERT_EQ(S_OK, root_view_accessible->get_accChildCount(&child_count)); | 137 ASSERT_EQ(S_OK, root_view_accessible->get_accChildCount(&child_count)); |
118 ASSERT_EQ(2L, child_count); | 138 ASSERT_EQ(2L, child_count); |
119 } | 139 } |
120 | 140 |
141 TEST_F(NativeViewAcccessibilityWinTest, RetrieveAllAlerts) { | |
142 Widget widget; | |
143 Widget::InitParams init_params = | |
144 CreateParams(Widget::InitParams::TYPE_POPUP); | |
145 init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
146 widget.Init(init_params); | |
147 | |
148 View* content = new View; | |
149 widget.SetContentsView(content); | |
150 | |
151 View* infobar = new View; | |
152 content->AddChildView(infobar); | |
153 | |
154 View* infobar2 = new View; | |
155 content->AddChildView(infobar2); | |
156 | |
157 View* root_view = content->parent(); | |
158 ASSERT_EQ(NULL, root_view->parent()); | |
159 | |
160 ScopedComPtr<IAccessible2_2> root_view_accessible; | |
161 GetIAccessible2InterfaceForView(root_view, root_view_accessible.Receive()); | |
David Tseng
2014/05/13 01:34:54
ASSERT_EQ?
dmazzoni
2014/05/13 07:38:46
It asserts inside this helper function.
| |
162 | |
163 ScopedComPtr<IAccessible2_2> infobar_accessible; | |
164 GetIAccessible2InterfaceForView(infobar, infobar_accessible.Receive()); | |
165 | |
166 ScopedComPtr<IAccessible2_2> infobar2_accessible; | |
167 GetIAccessible2InterfaceForView(infobar2, infobar2_accessible.Receive()); | |
168 | |
169 // Initially, there are no alerts | |
170 ScopedBstr alerts_bstr(L"alerts"); | |
171 IUnknown** targets; | |
172 long n_targets; | |
173 ASSERT_EQ(S_FALSE, root_view_accessible->get_relationTargetsOfType( | |
174 alerts_bstr, 0, &targets, &n_targets)); | |
175 ASSERT_EQ(0, n_targets); | |
176 | |
177 // Fire alert events on the infobars. | |
178 infobar->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true); | |
179 infobar2->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, true); | |
180 | |
181 // Now calling get_relationTargetsOfType should retrieve the alerts. | |
182 ASSERT_EQ(S_OK, root_view_accessible->get_relationTargetsOfType( | |
183 alerts_bstr, 0, &targets, &n_targets)); | |
184 ASSERT_EQ(2, n_targets); | |
185 ASSERT_TRUE(infobar_accessible.IsSameObject(targets[0])); | |
186 ASSERT_TRUE(infobar2_accessible.IsSameObject(targets[1])); | |
187 CoTaskMemFree(targets); | |
188 | |
189 // If we set max_targets to 1, we should only get the first one. | |
190 ASSERT_EQ(S_OK, root_view_accessible->get_relationTargetsOfType( | |
191 alerts_bstr, 1, &targets, &n_targets)); | |
192 ASSERT_EQ(1, n_targets); | |
193 ASSERT_TRUE(infobar_accessible.IsSameObject(targets[0])); | |
194 CoTaskMemFree(targets); | |
David Tseng
2014/05/13 01:34:54
Odd to see this freed after each call; is this cor
dmazzoni
2014/05/13 07:38:46
Yep, that's what the spec says. The server allocat
| |
195 | |
196 // If we delete the first view, we should only get the second one now. | |
197 delete infobar; | |
198 ASSERT_EQ(S_OK, root_view_accessible->get_relationTargetsOfType( | |
199 alerts_bstr, 0, &targets, &n_targets)); | |
200 ASSERT_EQ(1, n_targets); | |
201 ASSERT_TRUE(infobar2_accessible.IsSameObject(targets[0])); | |
202 CoTaskMemFree(targets); | |
203 } | |
204 | |
121 } // namespace test | 205 } // namespace test |
122 } // namespace views | 206 } // namespace views |
OLD | NEW |