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

Side by Side Diff: chrome/browser/ui/views/accessibility/accessibility_event_router_views_unittest.cc

Issue 74083003: Allow controls other than windows to send alert accessibility events. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: rebase Created 7 years, 1 month 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <string> 5 #include <string>
6 6
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/accessibility/accessibility_extension_api.h" 10 #include "chrome/browser/accessibility/accessibility_extension_api.h"
11 #include "chrome/browser/accessibility/accessibility_extension_api_constants.h"
11 #include "chrome/browser/ui/views/accessibility/accessibility_event_router_views .h" 12 #include "chrome/browser/ui/views/accessibility/accessibility_event_router_views .h"
12 #include "chrome/test/base/testing_profile.h" 13 #include "chrome/test/base/testing_profile.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/base/accessibility/accessibility_types.h"
14 #include "ui/base/accessibility/accessible_view_state.h" 16 #include "ui/base/accessibility/accessible_view_state.h"
15 #include "ui/views/controls/button/label_button.h" 17 #include "ui/views/controls/button/label_button.h"
16 #include "ui/views/controls/label.h" 18 #include "ui/views/controls/label.h"
17 #include "ui/views/layout/grid_layout.h" 19 #include "ui/views/layout/grid_layout.h"
18 #include "ui/views/test/test_views_delegate.h" 20 #include "ui/views/test/test_views_delegate.h"
19 #include "ui/views/widget/native_widget.h" 21 #include "ui/views/widget/native_widget.h"
20 #include "ui/views/widget/root_view.h" 22 #include "ui/views/widget/root_view.h"
21 #include "ui/views/widget/widget.h" 23 #include "ui/views/widget/widget.h"
22 #include "ui/views/widget/widget_delegate.h" 24 #include "ui/views/widget/widget_delegate.h"
23 25
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 98
97 private: 99 private:
98 string16 name_; 100 string16 name_;
99 ui::AccessibilityTypes::Role role_; 101 ui::AccessibilityTypes::Role role_;
100 DISALLOW_COPY_AND_ASSIGN(ViewWithNameAndRole); 102 DISALLOW_COPY_AND_ASSIGN(ViewWithNameAndRole);
101 }; 103 };
102 104
103 class AccessibilityEventRouterViewsTest 105 class AccessibilityEventRouterViewsTest
104 : public testing::Test { 106 : public testing::Test {
105 public: 107 public:
106 AccessibilityEventRouterViewsTest() : focus_event_count_(0) { 108 AccessibilityEventRouterViewsTest() : control_event_count_(0) {
107 } 109 }
108 110
109 virtual void SetUp() { 111 virtual void SetUp() {
110 #if defined(OS_WIN) 112 #if defined(OS_WIN)
111 ole_initializer_.reset(new ui::ScopedOleInitializer()); 113 ole_initializer_.reset(new ui::ScopedOleInitializer());
112 #endif 114 #endif
113 views::ViewsDelegate::views_delegate = new AccessibilityViewsDelegate(); 115 views::ViewsDelegate::views_delegate = new AccessibilityViewsDelegate();
114 #if defined(USE_AURA) 116 #if defined(USE_AURA)
115 aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_)); 117 aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_));
116 aura_test_helper_->SetUp(); 118 aura_test_helper_->SetUp();
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 153
152 return widget; 154 return widget;
153 } 155 }
154 156
155 void EnableAccessibilityAndListenToFocusNotifications() { 157 void EnableAccessibilityAndListenToFocusNotifications() {
156 // Switch on accessibility event notifications. 158 // Switch on accessibility event notifications.
157 ExtensionAccessibilityEventRouter* accessibility_event_router = 159 ExtensionAccessibilityEventRouter* accessibility_event_router =
158 ExtensionAccessibilityEventRouter::GetInstance(); 160 ExtensionAccessibilityEventRouter::GetInstance();
159 accessibility_event_router->SetAccessibilityEnabled(true); 161 accessibility_event_router->SetAccessibilityEnabled(true);
160 accessibility_event_router->SetControlEventCallbackForTesting(base::Bind( 162 accessibility_event_router->SetControlEventCallbackForTesting(base::Bind(
161 &AccessibilityEventRouterViewsTest::OnFocusEvent, 163 &AccessibilityEventRouterViewsTest::OnControlEvent,
162 base::Unretained(this))); 164 base::Unretained(this)));
163 } 165 }
164 166
165 void ClearCallback() { 167 void ClearCallback() {
166 ExtensionAccessibilityEventRouter* accessibility_event_router = 168 ExtensionAccessibilityEventRouter* accessibility_event_router =
167 ExtensionAccessibilityEventRouter::GetInstance(); 169 ExtensionAccessibilityEventRouter::GetInstance();
168 accessibility_event_router->ClearControlEventCallback(); 170 accessibility_event_router->ClearControlEventCallback();
169 } 171 }
170 172
171 protected: 173 protected:
172 // Handle Focus event. 174 // Handle Focus event.
173 virtual void OnFocusEvent(ui::AccessibilityTypes::Event event, 175 virtual void OnControlEvent(ui::AccessibilityTypes::Event event,
174 const AccessibilityControlInfo* info) { 176 const AccessibilityControlInfo* info) {
175 focus_event_count_++; 177 control_event_count_++;
178 last_control_type_ = info->type();
176 last_control_name_ = info->name(); 179 last_control_name_ = info->name();
177 last_control_context_ = info->context(); 180 last_control_context_ = info->context();
178 } 181 }
179 182
180 base::MessageLoopForUI message_loop_; 183 base::MessageLoopForUI message_loop_;
181 int focus_event_count_; 184 int control_event_count_;
185 std::string last_control_type_;
182 std::string last_control_name_; 186 std::string last_control_name_;
183 std::string last_control_context_; 187 std::string last_control_context_;
184 TestingProfile profile_; 188 TestingProfile profile_;
185 #if defined(OS_WIN) 189 #if defined(OS_WIN)
186 scoped_ptr<ui::ScopedOleInitializer> ole_initializer_; 190 scoped_ptr<ui::ScopedOleInitializer> ole_initializer_;
187 #endif 191 #endif
188 #if defined(USE_AURA) 192 #if defined(USE_AURA)
189 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_; 193 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
190 #endif 194 #endif
191 }; 195 };
(...skipping 15 matching lines...) Expand all
207 button2->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); 211 button2->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON);
208 contents->AddChildView(button2); 212 contents->AddChildView(button2);
209 views::LabelButton* button3 = new views::LabelButton( 213 views::LabelButton* button3 = new views::LabelButton(
210 NULL, ASCIIToUTF16(kButton3ASCII)); 214 NULL, ASCIIToUTF16(kButton3ASCII));
211 button3->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); 215 button3->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON);
212 contents->AddChildView(button3); 216 contents->AddChildView(button3);
213 217
214 // Put the view in a window. 218 // Put the view in a window.
215 views::Widget* window = CreateWindowWithContents(contents); 219 views::Widget* window = CreateWindowWithContents(contents);
216 window->Show(); 220 window->Show();
217 window->Activate();
218 221
219 // Set focus to the first button initially and run message loop to execute 222 // Set focus to the first button initially and run message loop to execute
220 // callback. 223 // callback.
221 button1->RequestFocus(); 224 button1->RequestFocus();
222 base::MessageLoop::current()->RunUntilIdle(); 225 base::MessageLoop::current()->RunUntilIdle();
223 226
224 // Change the accessible name of button3. 227 // Change the accessible name of button3.
225 button3->SetAccessibleName(ASCIIToUTF16(kButton3NewASCII)); 228 button3->SetAccessibleName(ASCIIToUTF16(kButton3NewASCII));
226 229
227 // Advance focus to the next button and test that we got the 230 // Advance focus to the next button and test that we got the
228 // expected notification with the name of button 2. 231 // expected notification with the name of button 2.
229 views::FocusManager* focus_manager = contents->GetWidget()->GetFocusManager(); 232 views::FocusManager* focus_manager = contents->GetWidget()->GetFocusManager();
230 focus_event_count_ = 0; 233 control_event_count_ = 0;
231 focus_manager->AdvanceFocus(false); 234 focus_manager->AdvanceFocus(false);
232 base::MessageLoop::current()->RunUntilIdle(); 235 base::MessageLoop::current()->RunUntilIdle();
233 EXPECT_EQ(1, focus_event_count_); 236 EXPECT_EQ(1, control_event_count_);
234 EXPECT_EQ(kButton2ASCII, last_control_name_); 237 EXPECT_EQ(kButton2ASCII, last_control_name_);
235 238
236 // Advance to button 3. Expect the new accessible name we assigned. 239 // Advance to button 3. Expect the new accessible name we assigned.
237 focus_manager->AdvanceFocus(false); 240 focus_manager->AdvanceFocus(false);
238 base::MessageLoop::current()->RunUntilIdle(); 241 base::MessageLoop::current()->RunUntilIdle();
239 EXPECT_EQ(2, focus_event_count_); 242 EXPECT_EQ(2, control_event_count_);
240 EXPECT_EQ(kButton3NewASCII, last_control_name_); 243 EXPECT_EQ(kButton3NewASCII, last_control_name_);
241 244
242 // Advance to button 1 and check the notification. 245 // Advance to button 1 and check the notification.
243 focus_manager->AdvanceFocus(false); 246 focus_manager->AdvanceFocus(false);
244 base::MessageLoop::current()->RunUntilIdle(); 247 base::MessageLoop::current()->RunUntilIdle();
245 EXPECT_EQ(3, focus_event_count_); 248 EXPECT_EQ(3, control_event_count_);
246 EXPECT_EQ(kButton1ASCII, last_control_name_); 249 EXPECT_EQ(kButton1ASCII, last_control_name_);
247 250
248 window->CloseNow(); 251 window->CloseNow();
249 } 252 }
250 253
251 TEST_F(AccessibilityEventRouterViewsTest, TestToolbarContext) { 254 TEST_F(AccessibilityEventRouterViewsTest, TestToolbarContext) {
252 const char kToolbarNameASCII[] = "MyToolbar"; 255 const char kToolbarNameASCII[] = "MyToolbar";
253 const char kButtonNameASCII[] = "MyButton"; 256 const char kButtonNameASCII[] = "MyButton";
254 257
255 // Create a toolbar with a button. 258 // Create a toolbar with a button.
256 views::View* contents = new ViewWithNameAndRole( 259 views::View* contents = new ViewWithNameAndRole(
257 ASCIIToUTF16(kToolbarNameASCII), 260 ASCIIToUTF16(kToolbarNameASCII),
258 ui::AccessibilityTypes::ROLE_TOOLBAR); 261 ui::AccessibilityTypes::ROLE_TOOLBAR);
259 views::LabelButton* button = new views::LabelButton( 262 views::LabelButton* button = new views::LabelButton(
260 NULL, ASCIIToUTF16(kButtonNameASCII)); 263 NULL, ASCIIToUTF16(kButtonNameASCII));
261 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); 264 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON);
262 contents->AddChildView(button); 265 contents->AddChildView(button);
263 266
264 // Put the view in a window. 267 // Put the view in a window.
265 views::Widget* window = CreateWindowWithContents(contents); 268 views::Widget* window = CreateWindowWithContents(contents);
266 269
267 // Set focus to the button. 270 // Set focus to the button.
268 focus_event_count_ = 0; 271 control_event_count_ = 0;
269 button->RequestFocus(); 272 button->RequestFocus();
270 273
271 base::MessageLoop::current()->RunUntilIdle(); 274 base::MessageLoop::current()->RunUntilIdle();
272 275
273 // Test that we got the event with the expected name and context. 276 // Test that we got the event with the expected name and context.
274 EXPECT_EQ(kInitialFocusCount, focus_event_count_); 277 EXPECT_EQ(kInitialFocusCount, control_event_count_);
275 EXPECT_EQ(kButtonNameASCII, last_control_name_); 278 EXPECT_EQ(kButtonNameASCII, last_control_name_);
276 EXPECT_EQ(kToolbarNameASCII, last_control_context_); 279 EXPECT_EQ(kToolbarNameASCII, last_control_context_);
277 280
278 window->CloseNow(); 281 window->CloseNow();
279 } 282 }
280 283
281 TEST_F(AccessibilityEventRouterViewsTest, TestAlertContext) { 284 TEST_F(AccessibilityEventRouterViewsTest, TestAlertContext) {
282 const char kAlertTextASCII[] = "MyAlertText"; 285 const char kAlertTextASCII[] = "MyAlertText";
283 const char kButtonNameASCII[] = "MyButton"; 286 const char kButtonNameASCII[] = "MyButton";
284 287
285 // Create an alert with static text and a button, similar to an infobar. 288 // Create an alert with static text and a button, similar to an infobar.
286 views::View* contents = new ViewWithNameAndRole( 289 views::View* contents = new ViewWithNameAndRole(
287 string16(), 290 string16(),
288 ui::AccessibilityTypes::ROLE_ALERT); 291 ui::AccessibilityTypes::ROLE_ALERT);
289 views::Label* label = new views::Label(ASCIIToUTF16(kAlertTextASCII)); 292 views::Label* label = new views::Label(ASCIIToUTF16(kAlertTextASCII));
290 contents->AddChildView(label); 293 contents->AddChildView(label);
291 views::LabelButton* button = new views::LabelButton( 294 views::LabelButton* button = new views::LabelButton(
292 NULL, ASCIIToUTF16(kButtonNameASCII)); 295 NULL, ASCIIToUTF16(kButtonNameASCII));
293 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); 296 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON);
294 contents->AddChildView(button); 297 contents->AddChildView(button);
295 298
296 // Put the view in a window. 299 // Put the view in a window.
297 views::Widget* window = CreateWindowWithContents(contents); 300 views::Widget* window = CreateWindowWithContents(contents);
298 301
299 // Set focus to the button. 302 // Set focus to the button.
300 focus_event_count_ = 0; 303 control_event_count_ = 0;
301 button->RequestFocus(); 304 button->RequestFocus();
302 305
303 base::MessageLoop::current()->RunUntilIdle(); 306 base::MessageLoop::current()->RunUntilIdle();
304 307
305 // Test that we got the event with the expected name and context. 308 // Test that we got the event with the expected name and context.
306 EXPECT_EQ(kInitialFocusCount, focus_event_count_); 309 EXPECT_EQ(kInitialFocusCount, control_event_count_);
307 EXPECT_EQ(kButtonNameASCII, last_control_name_); 310 EXPECT_EQ(kButtonNameASCII, last_control_name_);
308 EXPECT_EQ(kAlertTextASCII, last_control_context_); 311 EXPECT_EQ(kAlertTextASCII, last_control_context_);
309 312
310 window->CloseNow(); 313 window->CloseNow();
311 } 314 }
312 315
313 TEST_F(AccessibilityEventRouterViewsTest, StateChangeAfterNotification) { 316 TEST_F(AccessibilityEventRouterViewsTest, StateChangeAfterNotification) {
314 const char kContentsNameASCII[] = "Contents"; 317 const char kContentsNameASCII[] = "Contents";
315 const char kOldNameASCII[] = "OldName"; 318 const char kOldNameASCII[] = "OldName";
316 const char kNewNameASCII[] = "NewName"; 319 const char kNewNameASCII[] = "NewName";
317 320
318 // Create a toolbar with a button. 321 // Create a toolbar with a button.
319 views::View* contents = new ViewWithNameAndRole( 322 views::View* contents = new ViewWithNameAndRole(
320 ASCIIToUTF16(kContentsNameASCII), 323 ASCIIToUTF16(kContentsNameASCII),
321 ui::AccessibilityTypes::ROLE_CLIENT); 324 ui::AccessibilityTypes::ROLE_CLIENT);
322 ViewWithNameAndRole* child = new ViewWithNameAndRole( 325 ViewWithNameAndRole* child = new ViewWithNameAndRole(
323 ASCIIToUTF16(kOldNameASCII), 326 ASCIIToUTF16(kOldNameASCII),
324 ui::AccessibilityTypes::ROLE_PUSHBUTTON); 327 ui::AccessibilityTypes::ROLE_PUSHBUTTON);
325 child->set_focusable(true); 328 child->set_focusable(true);
326 contents->AddChildView(child); 329 contents->AddChildView(child);
327 330
328 // Put the view in a window. 331 // Put the view in a window.
329 views::Widget* window = CreateWindowWithContents(contents); 332 views::Widget* window = CreateWindowWithContents(contents);
330 333
331 // Set focus to the child view. 334 // Set focus to the child view.
332 focus_event_count_ = 0; 335 control_event_count_ = 0;
333 child->RequestFocus(); 336 child->RequestFocus();
334 337
335 // Change the child's name after the focus notification. 338 // Change the child's name after the focus notification.
336 child->set_name(ASCIIToUTF16(kNewNameASCII)); 339 child->set_name(ASCIIToUTF16(kNewNameASCII));
337 340
338 // We shouldn't get the notification right away. 341 // We shouldn't get the notification right away.
339 EXPECT_EQ(0, focus_event_count_); 342 EXPECT_EQ(0, control_event_count_);
340 343
341 // Process anything in the event loop. Now we should get the notification, 344 // Process anything in the event loop. Now we should get the notification,
342 // and it should give us the new control name, not the old one. 345 // and it should give us the new control name, not the old one.
343 base::MessageLoop::current()->RunUntilIdle(); 346 base::MessageLoop::current()->RunUntilIdle();
344 EXPECT_EQ(kInitialFocusCount, focus_event_count_); 347 EXPECT_EQ(kInitialFocusCount, control_event_count_);
345 EXPECT_EQ(kNewNameASCII, last_control_name_); 348 EXPECT_EQ(kNewNameASCII, last_control_name_);
346 349
347 window->CloseNow(); 350 window->CloseNow();
348 } 351 }
349 352
350 TEST_F(AccessibilityEventRouterViewsTest, NotificationOnDeletedObject) { 353 TEST_F(AccessibilityEventRouterViewsTest, NotificationOnDeletedObject) {
351 const char kContentsNameASCII[] = "Contents"; 354 const char kContentsNameASCII[] = "Contents";
352 const char kNameASCII[] = "OldName"; 355 const char kNameASCII[] = "OldName";
353 356
354 // Create a toolbar with a button. 357 // Create a toolbar with a button.
355 views::View* contents = new ViewWithNameAndRole( 358 views::View* contents = new ViewWithNameAndRole(
356 ASCIIToUTF16(kContentsNameASCII), 359 ASCIIToUTF16(kContentsNameASCII),
357 ui::AccessibilityTypes::ROLE_CLIENT); 360 ui::AccessibilityTypes::ROLE_CLIENT);
358 ViewWithNameAndRole* child = new ViewWithNameAndRole( 361 ViewWithNameAndRole* child = new ViewWithNameAndRole(
359 ASCIIToUTF16(kNameASCII), 362 ASCIIToUTF16(kNameASCII),
360 ui::AccessibilityTypes::ROLE_PUSHBUTTON); 363 ui::AccessibilityTypes::ROLE_PUSHBUTTON);
361 child->set_focusable(true); 364 child->set_focusable(true);
362 contents->AddChildView(child); 365 contents->AddChildView(child);
363 366
364 // Put the view in a window. 367 // Put the view in a window.
365 views::Widget* window = CreateWindowWithContents(contents); 368 views::Widget* window = CreateWindowWithContents(contents);
366 369
367 // Set focus to the child view. 370 // Set focus to the child view.
368 focus_event_count_ = 0; 371 control_event_count_ = 0;
369 child->RequestFocus(); 372 child->RequestFocus();
370 373
371 // Delete the child! 374 // Delete the child!
372 delete child; 375 delete child;
373 376
374 // We shouldn't get the notification right away. 377 // We shouldn't get the notification right away.
375 EXPECT_EQ(0, focus_event_count_); 378 EXPECT_EQ(0, control_event_count_);
376 379
377 // Process anything in the event loop. We shouldn't get a notification 380 // Process anything in the event loop. We shouldn't get a notification
378 // because the view is no longer valid, and this shouldn't crash. 381 // because the view is no longer valid, and this shouldn't crash.
379 base::MessageLoop::current()->RunUntilIdle(); 382 base::MessageLoop::current()->RunUntilIdle();
380 EXPECT_EQ(0, focus_event_count_); 383 EXPECT_EQ(0, control_event_count_);
381 384
382 window->CloseNow(); 385 window->CloseNow();
383 } 386 }
387
388 TEST_F(AccessibilityEventRouterViewsTest, AlertsFromWindowAndControl) {
389 const char kButtonASCII[] = "Button";
390 const char* kTypeAlert = extension_accessibility_api_constants::kTypeAlert;
391 const char* kTypeWindow = extension_accessibility_api_constants::kTypeWindow;
392
393 // Create a contents view with a button.
394 views::View* contents = new views::View();
395 views::LabelButton* button = new views::LabelButton(
396 NULL, ASCIIToUTF16(kButtonASCII));
397 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON);
398 contents->AddChildView(button);
399
400 // Put the view in a window.
401 views::Widget* window = CreateWindowWithContents(contents);
402 window->Show();
403
404 // Send an alert event from the button and let the event loop run.
405 control_event_count_ = 0;
406 button->NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_ALERT, true);
407 base::MessageLoop::current()->RunUntilIdle();
408
409 EXPECT_EQ(kTypeAlert, last_control_type_);
410 EXPECT_EQ(1, control_event_count_);
411 EXPECT_EQ(kButtonASCII, last_control_name_);
412
413 // Send an alert event from the window and let the event loop run.
414 control_event_count_ = 0;
415 window->GetRootView()->NotifyAccessibilityEvent(
416 ui::AccessibilityTypes::EVENT_ALERT, true);
417 base::MessageLoop::current()->RunUntilIdle();
418
419 EXPECT_EQ(1, control_event_count_);
420 EXPECT_EQ(kTypeWindow, last_control_type_);
421
422 window->CloseNow();
423 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698