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

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

Issue 22922003: Remove notifications from Accessibility API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Clean up Created 7 years, 4 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
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"
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 97
98 void set_name(const string16& name) { name_ = name; } 98 void set_name(const string16& name) { name_ = name; }
99 99
100 private: 100 private:
101 string16 name_; 101 string16 name_;
102 ui::AccessibilityTypes::Role role_; 102 ui::AccessibilityTypes::Role role_;
103 DISALLOW_COPY_AND_ASSIGN(ViewWithNameAndRole); 103 DISALLOW_COPY_AND_ASSIGN(ViewWithNameAndRole);
104 }; 104 };
105 105
106 class AccessibilityEventRouterViewsTest 106 class AccessibilityEventRouterViewsTest
107 : public testing::Test, 107 : public testing::Test {
108 public content::NotificationObserver {
109 public: 108 public:
110 AccessibilityEventRouterViewsTest() { 109 AccessibilityEventRouterViewsTest() {
111 } 110 }
112 111
113 virtual void SetUp() { 112 virtual void SetUp() {
114 #if defined(OS_WIN) 113 #if defined(OS_WIN)
115 ole_initializer_.reset(new ui::ScopedOleInitializer()); 114 ole_initializer_.reset(new ui::ScopedOleInitializer());
116 #endif 115 #endif
117 views::ViewsDelegate::views_delegate = new AccessibilityViewsDelegate(); 116 views::ViewsDelegate::views_delegate = new AccessibilityViewsDelegate();
118 #if defined(USE_AURA) 117 #if defined(USE_AURA)
(...skipping 29 matching lines...) Expand all
148 context, 147 context,
149 gfx::Rect(0, 0, 500, 500)); 148 gfx::Rect(0, 0, 500, 500));
150 149
151 // Create a profile and associate it with this window. 150 // Create a profile and associate it with this window.
152 widget->SetNativeWindowProperty(Profile::kProfileKey, &profile_); 151 widget->SetNativeWindowProperty(Profile::kProfileKey, &profile_);
153 152
154 return widget; 153 return widget;
155 } 154 }
156 155
157 void EnableAccessibilityAndListenToFocusNotifications() { 156 void EnableAccessibilityAndListenToFocusNotifications() {
158 registrar_.Add(this,
159 chrome::NOTIFICATION_ACCESSIBILITY_CONTROL_FOCUSED,
160 content::NotificationService::AllSources());
161
162 // Switch on accessibility event notifications. 157 // Switch on accessibility event notifications.
163 ExtensionAccessibilityEventRouter* accessibility_event_router = 158 ExtensionAccessibilityEventRouter* accessibility_event_router =
164 ExtensionAccessibilityEventRouter::GetInstance(); 159 ExtensionAccessibilityEventRouter::GetInstance();
165 accessibility_event_router->SetAccessibilityEnabled(true); 160 accessibility_event_router->SetAccessibilityEnabled(true);
161 accessibility_event_router->SetControlEventCallbackForTesting(base::Bind(
162 &AccessibilityEventRouterViewsTest::OnFocusEvent,
163 base::Unretained(this)));
164 }
165
166 void ClearCallback() {
dmazzoni 2013/08/14 19:36:37 Can you do this in TearDown rather than having to
Cait (Slow) 2013/08/14 20:53:36 Done.
167 // Switch on accessibility event notifications.
168 ExtensionAccessibilityEventRouter* accessibility_event_router =
169 ExtensionAccessibilityEventRouter::GetInstance();
170 accessibility_event_router->ClearControlEventCallback();
166 } 171 }
167 172
168 protected: 173 protected:
169 // Implement NotificationObserver::Observe and store information about a 174 // Implement NotificationObserver::Observe and store information about a
170 // ACCESSIBILITY_CONTROL_FOCUSED event. 175 // ACCESSIBILITY_CONTROL_FOCUSED event.
171 virtual void Observe(int type, 176 virtual void OnFocusEvent(ui::AccessibilityTypes::Event event,
172 const content::NotificationSource& source, 177 const AccessibilityControlInfo* info) {
173 const content::NotificationDetails& details) OVERRIDE {
174 ASSERT_EQ(type, chrome::NOTIFICATION_ACCESSIBILITY_CONTROL_FOCUSED);
175 const AccessibilityControlInfo* info =
176 content::Details<const AccessibilityControlInfo>(details).ptr();
177 focus_event_count_++; 178 focus_event_count_++;
178 last_control_name_ = info->name(); 179 last_control_name_ = info->name();
179 last_control_context_ = info->context(); 180 last_control_context_ = info->context();
180 } 181 }
181 182
182 base::MessageLoopForUI message_loop_; 183 base::MessageLoopForUI message_loop_;
183 int focus_event_count_; 184 int focus_event_count_;
184 std::string last_control_name_; 185 std::string last_control_name_;
185 std::string last_control_context_; 186 std::string last_control_context_;
186 content::NotificationRegistrar registrar_;
187 TestingProfile profile_; 187 TestingProfile profile_;
188 #if defined(OS_WIN) 188 #if defined(OS_WIN)
189 scoped_ptr<ui::ScopedOleInitializer> ole_initializer_; 189 scoped_ptr<ui::ScopedOleInitializer> ole_initializer_;
190 #endif 190 #endif
191 #if defined(USE_AURA) 191 #if defined(USE_AURA)
192 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_; 192 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_;
193 #endif 193 #endif
194 }; 194 };
195 195
196 TEST_F(AccessibilityEventRouterViewsTest, TestFocusNotification) { 196 TEST_F(AccessibilityEventRouterViewsTest, TestFocusNotification) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 base::MessageLoop::current()->RunUntilIdle(); 241 base::MessageLoop::current()->RunUntilIdle();
242 EXPECT_EQ(2, focus_event_count_); 242 EXPECT_EQ(2, focus_event_count_);
243 EXPECT_EQ(kButton3NewASCII, last_control_name_); 243 EXPECT_EQ(kButton3NewASCII, last_control_name_);
244 244
245 // Advance to button 1 and check the notification. 245 // Advance to button 1 and check the notification.
246 focus_manager->AdvanceFocus(false); 246 focus_manager->AdvanceFocus(false);
247 base::MessageLoop::current()->RunUntilIdle(); 247 base::MessageLoop::current()->RunUntilIdle();
248 EXPECT_EQ(3, focus_event_count_); 248 EXPECT_EQ(3, focus_event_count_);
249 EXPECT_EQ(kButton1ASCII, last_control_name_); 249 EXPECT_EQ(kButton1ASCII, last_control_name_);
250 250
251 ClearCallback();
251 window->CloseNow(); 252 window->CloseNow();
252 } 253 }
253 254
254 TEST_F(AccessibilityEventRouterViewsTest, TestToolbarContext) { 255 TEST_F(AccessibilityEventRouterViewsTest, TestToolbarContext) {
255 const char kToolbarNameASCII[] = "MyToolbar"; 256 const char kToolbarNameASCII[] = "MyToolbar";
256 const char kButtonNameASCII[] = "MyButton"; 257 const char kButtonNameASCII[] = "MyButton";
257 258
258 // Create a toolbar with a button. 259 // Create a toolbar with a button.
259 views::View* contents = new ViewWithNameAndRole( 260 views::View* contents = new ViewWithNameAndRole(
260 ASCIIToUTF16(kToolbarNameASCII), 261 ASCIIToUTF16(kToolbarNameASCII),
(...skipping 12 matching lines...) Expand all
273 focus_event_count_ = 0; 274 focus_event_count_ = 0;
274 button->RequestFocus(); 275 button->RequestFocus();
275 276
276 base::MessageLoop::current()->RunUntilIdle(); 277 base::MessageLoop::current()->RunUntilIdle();
277 278
278 // Test that we got the event with the expected name and context. 279 // Test that we got the event with the expected name and context.
279 EXPECT_EQ(kInitialFocusCount, focus_event_count_); 280 EXPECT_EQ(kInitialFocusCount, focus_event_count_);
280 EXPECT_EQ(kButtonNameASCII, last_control_name_); 281 EXPECT_EQ(kButtonNameASCII, last_control_name_);
281 EXPECT_EQ(kToolbarNameASCII, last_control_context_); 282 EXPECT_EQ(kToolbarNameASCII, last_control_context_);
282 283
284 ClearCallback();
283 window->CloseNow(); 285 window->CloseNow();
284 } 286 }
285 287
286 TEST_F(AccessibilityEventRouterViewsTest, TestAlertContext) { 288 TEST_F(AccessibilityEventRouterViewsTest, TestAlertContext) {
287 const char kAlertTextASCII[] = "MyAlertText"; 289 const char kAlertTextASCII[] = "MyAlertText";
288 const char kButtonNameASCII[] = "MyButton"; 290 const char kButtonNameASCII[] = "MyButton";
289 291
290 // Create an alert with static text and a button, similar to an infobar. 292 // Create an alert with static text and a button, similar to an infobar.
291 views::View* contents = new ViewWithNameAndRole( 293 views::View* contents = new ViewWithNameAndRole(
292 string16(), 294 string16(),
(...skipping 14 matching lines...) Expand all
307 focus_event_count_ = 0; 309 focus_event_count_ = 0;
308 button->RequestFocus(); 310 button->RequestFocus();
309 311
310 base::MessageLoop::current()->RunUntilIdle(); 312 base::MessageLoop::current()->RunUntilIdle();
311 313
312 // Test that we got the event with the expected name and context. 314 // Test that we got the event with the expected name and context.
313 EXPECT_EQ(kInitialFocusCount, focus_event_count_); 315 EXPECT_EQ(kInitialFocusCount, focus_event_count_);
314 EXPECT_EQ(kButtonNameASCII, last_control_name_); 316 EXPECT_EQ(kButtonNameASCII, last_control_name_);
315 EXPECT_EQ(kAlertTextASCII, last_control_context_); 317 EXPECT_EQ(kAlertTextASCII, last_control_context_);
316 318
319 ClearCallback();
317 window->CloseNow(); 320 window->CloseNow();
318 } 321 }
319 322
320 TEST_F(AccessibilityEventRouterViewsTest, StateChangeAfterNotification) { 323 TEST_F(AccessibilityEventRouterViewsTest, StateChangeAfterNotification) {
321 const char kContentsNameASCII[] = "Contents"; 324 const char kContentsNameASCII[] = "Contents";
322 const char kOldNameASCII[] = "OldName"; 325 const char kOldNameASCII[] = "OldName";
323 const char kNewNameASCII[] = "NewName"; 326 const char kNewNameASCII[] = "NewName";
324 327
325 // Create a toolbar with a button. 328 // Create a toolbar with a button.
326 views::View* contents = new ViewWithNameAndRole( 329 views::View* contents = new ViewWithNameAndRole(
(...skipping 19 matching lines...) Expand all
346 349
347 // We shouldn't get the notification right away. 350 // We shouldn't get the notification right away.
348 EXPECT_EQ(0, focus_event_count_); 351 EXPECT_EQ(0, focus_event_count_);
349 352
350 // Process anything in the event loop. Now we should get the notification, 353 // Process anything in the event loop. Now we should get the notification,
351 // and it should give us the new control name, not the old one. 354 // and it should give us the new control name, not the old one.
352 base::MessageLoop::current()->RunUntilIdle(); 355 base::MessageLoop::current()->RunUntilIdle();
353 EXPECT_EQ(kInitialFocusCount, focus_event_count_); 356 EXPECT_EQ(kInitialFocusCount, focus_event_count_);
354 EXPECT_EQ(kNewNameASCII, last_control_name_); 357 EXPECT_EQ(kNewNameASCII, last_control_name_);
355 358
359 ClearCallback();
356 window->CloseNow(); 360 window->CloseNow();
357 } 361 }
358 362
359 TEST_F(AccessibilityEventRouterViewsTest, NotificationOnDeletedObject) { 363 TEST_F(AccessibilityEventRouterViewsTest, NotificationOnDeletedObject) {
360 const char kContentsNameASCII[] = "Contents"; 364 const char kContentsNameASCII[] = "Contents";
361 const char kNameASCII[] = "OldName"; 365 const char kNameASCII[] = "OldName";
362 366
363 // Create a toolbar with a button. 367 // Create a toolbar with a button.
364 views::View* contents = new ViewWithNameAndRole( 368 views::View* contents = new ViewWithNameAndRole(
365 ASCIIToUTF16(kContentsNameASCII), 369 ASCIIToUTF16(kContentsNameASCII),
(...skipping 17 matching lines...) Expand all
383 delete child; 387 delete child;
384 388
385 // We shouldn't get the notification right away. 389 // We shouldn't get the notification right away.
386 EXPECT_EQ(0, focus_event_count_); 390 EXPECT_EQ(0, focus_event_count_);
387 391
388 // Process anything in the event loop. We shouldn't get a notification 392 // Process anything in the event loop. We shouldn't get a notification
389 // because the view is no longer valid, and this shouldn't crash. 393 // because the view is no longer valid, and this shouldn't crash.
390 base::MessageLoop::current()->RunUntilIdle(); 394 base::MessageLoop::current()->RunUntilIdle();
391 EXPECT_EQ(0, focus_event_count_); 395 EXPECT_EQ(0, focus_event_count_);
392 396
397 ClearCallback();
393 window->CloseNow(); 398 window->CloseNow();
394 } 399 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698