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

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: VS compile workaround 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 28 matching lines...) Expand all
220 // callback. 224 // callback.
221 button1->RequestFocus(); 225 button1->RequestFocus();
222 base::MessageLoop::current()->RunUntilIdle(); 226 base::MessageLoop::current()->RunUntilIdle();
223 227
224 // Change the accessible name of button3. 228 // Change the accessible name of button3.
225 button3->SetAccessibleName(ASCIIToUTF16(kButton3NewASCII)); 229 button3->SetAccessibleName(ASCIIToUTF16(kButton3NewASCII));
226 230
227 // Advance focus to the next button and test that we got the 231 // Advance focus to the next button and test that we got the
228 // expected notification with the name of button 2. 232 // expected notification with the name of button 2.
229 views::FocusManager* focus_manager = contents->GetWidget()->GetFocusManager(); 233 views::FocusManager* focus_manager = contents->GetWidget()->GetFocusManager();
230 focus_event_count_ = 0; 234 control_event_count_ = 0;
231 focus_manager->AdvanceFocus(false); 235 focus_manager->AdvanceFocus(false);
232 base::MessageLoop::current()->RunUntilIdle(); 236 base::MessageLoop::current()->RunUntilIdle();
233 EXPECT_EQ(1, focus_event_count_); 237 EXPECT_EQ(1, control_event_count_);
234 EXPECT_EQ(kButton2ASCII, last_control_name_); 238 EXPECT_EQ(kButton2ASCII, last_control_name_);
235 239
236 // Advance to button 3. Expect the new accessible name we assigned. 240 // Advance to button 3. Expect the new accessible name we assigned.
237 focus_manager->AdvanceFocus(false); 241 focus_manager->AdvanceFocus(false);
238 base::MessageLoop::current()->RunUntilIdle(); 242 base::MessageLoop::current()->RunUntilIdle();
239 EXPECT_EQ(2, focus_event_count_); 243 EXPECT_EQ(2, control_event_count_);
240 EXPECT_EQ(kButton3NewASCII, last_control_name_); 244 EXPECT_EQ(kButton3NewASCII, last_control_name_);
241 245
242 // Advance to button 1 and check the notification. 246 // Advance to button 1 and check the notification.
243 focus_manager->AdvanceFocus(false); 247 focus_manager->AdvanceFocus(false);
244 base::MessageLoop::current()->RunUntilIdle(); 248 base::MessageLoop::current()->RunUntilIdle();
245 EXPECT_EQ(3, focus_event_count_); 249 EXPECT_EQ(3, control_event_count_);
246 EXPECT_EQ(kButton1ASCII, last_control_name_); 250 EXPECT_EQ(kButton1ASCII, last_control_name_);
247 251
248 window->CloseNow(); 252 window->CloseNow();
249 } 253 }
250 254
251 TEST_F(AccessibilityEventRouterViewsTest, TestToolbarContext) { 255 TEST_F(AccessibilityEventRouterViewsTest, TestToolbarContext) {
252 const char kToolbarNameASCII[] = "MyToolbar"; 256 const char kToolbarNameASCII[] = "MyToolbar";
253 const char kButtonNameASCII[] = "MyButton"; 257 const char kButtonNameASCII[] = "MyButton";
254 258
255 // Create a toolbar with a button. 259 // Create a toolbar with a button.
256 views::View* contents = new ViewWithNameAndRole( 260 views::View* contents = new ViewWithNameAndRole(
257 ASCIIToUTF16(kToolbarNameASCII), 261 ASCIIToUTF16(kToolbarNameASCII),
258 ui::AccessibilityTypes::ROLE_TOOLBAR); 262 ui::AccessibilityTypes::ROLE_TOOLBAR);
259 views::LabelButton* button = new views::LabelButton( 263 views::LabelButton* button = new views::LabelButton(
260 NULL, ASCIIToUTF16(kButtonNameASCII)); 264 NULL, ASCIIToUTF16(kButtonNameASCII));
261 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); 265 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON);
262 contents->AddChildView(button); 266 contents->AddChildView(button);
263 267
264 // Put the view in a window. 268 // Put the view in a window.
265 views::Widget* window = CreateWindowWithContents(contents); 269 views::Widget* window = CreateWindowWithContents(contents);
266 270
267 // Set focus to the button. 271 // Set focus to the button.
268 focus_event_count_ = 0; 272 control_event_count_ = 0;
269 button->RequestFocus(); 273 button->RequestFocus();
270 274
271 base::MessageLoop::current()->RunUntilIdle(); 275 base::MessageLoop::current()->RunUntilIdle();
272 276
273 // Test that we got the event with the expected name and context. 277 // Test that we got the event with the expected name and context.
274 EXPECT_EQ(kInitialFocusCount, focus_event_count_); 278 EXPECT_EQ(kInitialFocusCount, control_event_count_);
275 EXPECT_EQ(kButtonNameASCII, last_control_name_); 279 EXPECT_EQ(kButtonNameASCII, last_control_name_);
276 EXPECT_EQ(kToolbarNameASCII, last_control_context_); 280 EXPECT_EQ(kToolbarNameASCII, last_control_context_);
277 281
278 window->CloseNow(); 282 window->CloseNow();
279 } 283 }
280 284
281 TEST_F(AccessibilityEventRouterViewsTest, TestAlertContext) { 285 TEST_F(AccessibilityEventRouterViewsTest, TestAlertContext) {
282 const char kAlertTextASCII[] = "MyAlertText"; 286 const char kAlertTextASCII[] = "MyAlertText";
283 const char kButtonNameASCII[] = "MyButton"; 287 const char kButtonNameASCII[] = "MyButton";
284 288
285 // Create an alert with static text and a button, similar to an infobar. 289 // Create an alert with static text and a button, similar to an infobar.
286 views::View* contents = new ViewWithNameAndRole( 290 views::View* contents = new ViewWithNameAndRole(
287 string16(), 291 string16(),
288 ui::AccessibilityTypes::ROLE_ALERT); 292 ui::AccessibilityTypes::ROLE_ALERT);
289 views::Label* label = new views::Label(ASCIIToUTF16(kAlertTextASCII)); 293 views::Label* label = new views::Label(ASCIIToUTF16(kAlertTextASCII));
290 contents->AddChildView(label); 294 contents->AddChildView(label);
291 views::LabelButton* button = new views::LabelButton( 295 views::LabelButton* button = new views::LabelButton(
292 NULL, ASCIIToUTF16(kButtonNameASCII)); 296 NULL, ASCIIToUTF16(kButtonNameASCII));
293 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); 297 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON);
294 contents->AddChildView(button); 298 contents->AddChildView(button);
295 299
296 // Put the view in a window. 300 // Put the view in a window.
297 views::Widget* window = CreateWindowWithContents(contents); 301 views::Widget* window = CreateWindowWithContents(contents);
298 302
299 // Set focus to the button. 303 // Set focus to the button.
300 focus_event_count_ = 0; 304 control_event_count_ = 0;
301 button->RequestFocus(); 305 button->RequestFocus();
302 306
303 base::MessageLoop::current()->RunUntilIdle(); 307 base::MessageLoop::current()->RunUntilIdle();
304 308
305 // Test that we got the event with the expected name and context. 309 // Test that we got the event with the expected name and context.
306 EXPECT_EQ(kInitialFocusCount, focus_event_count_); 310 EXPECT_EQ(kInitialFocusCount, control_event_count_);
307 EXPECT_EQ(kButtonNameASCII, last_control_name_); 311 EXPECT_EQ(kButtonNameASCII, last_control_name_);
308 EXPECT_EQ(kAlertTextASCII, last_control_context_); 312 EXPECT_EQ(kAlertTextASCII, last_control_context_);
309 313
310 window->CloseNow(); 314 window->CloseNow();
311 } 315 }
312 316
313 TEST_F(AccessibilityEventRouterViewsTest, StateChangeAfterNotification) { 317 TEST_F(AccessibilityEventRouterViewsTest, StateChangeAfterNotification) {
314 const char kContentsNameASCII[] = "Contents"; 318 const char kContentsNameASCII[] = "Contents";
315 const char kOldNameASCII[] = "OldName"; 319 const char kOldNameASCII[] = "OldName";
316 const char kNewNameASCII[] = "NewName"; 320 const char kNewNameASCII[] = "NewName";
317 321
318 // Create a toolbar with a button. 322 // Create a toolbar with a button.
319 views::View* contents = new ViewWithNameAndRole( 323 views::View* contents = new ViewWithNameAndRole(
320 ASCIIToUTF16(kContentsNameASCII), 324 ASCIIToUTF16(kContentsNameASCII),
321 ui::AccessibilityTypes::ROLE_CLIENT); 325 ui::AccessibilityTypes::ROLE_CLIENT);
322 ViewWithNameAndRole* child = new ViewWithNameAndRole( 326 ViewWithNameAndRole* child = new ViewWithNameAndRole(
323 ASCIIToUTF16(kOldNameASCII), 327 ASCIIToUTF16(kOldNameASCII),
324 ui::AccessibilityTypes::ROLE_PUSHBUTTON); 328 ui::AccessibilityTypes::ROLE_PUSHBUTTON);
325 child->set_focusable(true); 329 child->set_focusable(true);
326 contents->AddChildView(child); 330 contents->AddChildView(child);
327 331
328 // Put the view in a window. 332 // Put the view in a window.
329 views::Widget* window = CreateWindowWithContents(contents); 333 views::Widget* window = CreateWindowWithContents(contents);
330 334
331 // Set focus to the child view. 335 // Set focus to the child view.
332 focus_event_count_ = 0; 336 control_event_count_ = 0;
333 child->RequestFocus(); 337 child->RequestFocus();
334 338
335 // Change the child's name after the focus notification. 339 // Change the child's name after the focus notification.
336 child->set_name(ASCIIToUTF16(kNewNameASCII)); 340 child->set_name(ASCIIToUTF16(kNewNameASCII));
337 341
338 // We shouldn't get the notification right away. 342 // We shouldn't get the notification right away.
339 EXPECT_EQ(0, focus_event_count_); 343 EXPECT_EQ(0, control_event_count_);
340 344
341 // Process anything in the event loop. Now we should get the notification, 345 // 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. 346 // and it should give us the new control name, not the old one.
343 base::MessageLoop::current()->RunUntilIdle(); 347 base::MessageLoop::current()->RunUntilIdle();
344 EXPECT_EQ(kInitialFocusCount, focus_event_count_); 348 EXPECT_EQ(kInitialFocusCount, control_event_count_);
345 EXPECT_EQ(kNewNameASCII, last_control_name_); 349 EXPECT_EQ(kNewNameASCII, last_control_name_);
346 350
347 window->CloseNow(); 351 window->CloseNow();
348 } 352 }
349 353
350 TEST_F(AccessibilityEventRouterViewsTest, NotificationOnDeletedObject) { 354 TEST_F(AccessibilityEventRouterViewsTest, NotificationOnDeletedObject) {
351 const char kContentsNameASCII[] = "Contents"; 355 const char kContentsNameASCII[] = "Contents";
352 const char kNameASCII[] = "OldName"; 356 const char kNameASCII[] = "OldName";
353 357
354 // Create a toolbar with a button. 358 // Create a toolbar with a button.
355 views::View* contents = new ViewWithNameAndRole( 359 views::View* contents = new ViewWithNameAndRole(
356 ASCIIToUTF16(kContentsNameASCII), 360 ASCIIToUTF16(kContentsNameASCII),
357 ui::AccessibilityTypes::ROLE_CLIENT); 361 ui::AccessibilityTypes::ROLE_CLIENT);
358 ViewWithNameAndRole* child = new ViewWithNameAndRole( 362 ViewWithNameAndRole* child = new ViewWithNameAndRole(
359 ASCIIToUTF16(kNameASCII), 363 ASCIIToUTF16(kNameASCII),
360 ui::AccessibilityTypes::ROLE_PUSHBUTTON); 364 ui::AccessibilityTypes::ROLE_PUSHBUTTON);
361 child->set_focusable(true); 365 child->set_focusable(true);
362 contents->AddChildView(child); 366 contents->AddChildView(child);
363 367
364 // Put the view in a window. 368 // Put the view in a window.
365 views::Widget* window = CreateWindowWithContents(contents); 369 views::Widget* window = CreateWindowWithContents(contents);
366 370
367 // Set focus to the child view. 371 // Set focus to the child view.
368 focus_event_count_ = 0; 372 control_event_count_ = 0;
369 child->RequestFocus(); 373 child->RequestFocus();
370 374
371 // Delete the child! 375 // Delete the child!
372 delete child; 376 delete child;
373 377
374 // We shouldn't get the notification right away. 378 // We shouldn't get the notification right away.
375 EXPECT_EQ(0, focus_event_count_); 379 EXPECT_EQ(0, control_event_count_);
376 380
377 // Process anything in the event loop. We shouldn't get a notification 381 // 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. 382 // because the view is no longer valid, and this shouldn't crash.
379 base::MessageLoop::current()->RunUntilIdle(); 383 base::MessageLoop::current()->RunUntilIdle();
380 EXPECT_EQ(0, focus_event_count_); 384 EXPECT_EQ(0, control_event_count_);
381 385
382 window->CloseNow(); 386 window->CloseNow();
383 } 387 }
388
389 TEST_F(AccessibilityEventRouterViewsTest, AlertsFromWindowAndControl) {
390 const char kButtonASCII[] = "Button";
391 const char* kTypeAlert = extension_accessibility_api_constants::kTypeAlert;
392 const char* kTypeWindow = extension_accessibility_api_constants::kTypeWindow;
393
394 // Create a contents view with a button.
395 views::View* contents = new views::View();
396 views::LabelButton* button = new views::LabelButton(
397 NULL, ASCIIToUTF16(kButtonASCII));
398 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON);
399 contents->AddChildView(button);
400
401 // Put the view in a window.
402 views::Widget* window = CreateWindowWithContents(contents);
403 window->Show();
404 window->Activate();
sky 2013/11/16 00:28:46 If you're relying on activation then this needs to
dmazzoni 2013/11/16 00:35:14 I think this is okay. This test is really a unit t
405
406 // Send an alert event from the button and let the event loop run.
407 control_event_count_ = 0;
408 button->NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_ALERT, true);
409 base::MessageLoop::current()->RunUntilIdle();
410
411 EXPECT_EQ(kTypeAlert, last_control_type_);
412 EXPECT_EQ(1, control_event_count_);
413 EXPECT_EQ(kButtonASCII, last_control_name_);
414
415 // Send an alert event from the window and let the event loop run.
416 control_event_count_ = 0;
417 window->GetRootView()->NotifyAccessibilityEvent(
418 ui::AccessibilityTypes::EVENT_ALERT, true);
419 base::MessageLoop::current()->RunUntilIdle();
420
421 EXPECT_EQ(1, control_event_count_);
422 EXPECT_EQ(kTypeWindow, last_control_type_);
423
424 window->CloseNow();
425 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698