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

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: Respond to comments 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"
11 #include "chrome/browser/chrome_notification_types.h" 11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/ui/views/accessibility/accessibility_event_router_views .h" 12 #include "chrome/browser/ui/views/accessibility/accessibility_event_router_views .h"
13 #include "chrome/test/base/testing_profile.h" 13 #include "chrome/test/base/testing_profile.h"
14 #include "content/public/browser/notification_registrar.h" 14 #include "content/public/browser/notification_registrar.h"
dmazzoni 2013/08/14 21:00:17 Can you delete includes of notification_registrar.
Cait (Slow) 2013/08/15 20:01:16 Some of them, yes, but not all, as some of the cla
15 #include "content/public/browser/notification_service.h" 15 #include "content/public/browser/notification_service.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/base/accessibility/accessible_view_state.h" 17 #include "ui/base/accessibility/accessible_view_state.h"
18 #include "ui/views/controls/button/label_button.h" 18 #include "ui/views/controls/button/label_button.h"
19 #include "ui/views/controls/label.h" 19 #include "ui/views/controls/label.h"
20 #include "ui/views/layout/grid_layout.h" 20 #include "ui/views/layout/grid_layout.h"
21 #include "ui/views/test/test_views_delegate.h" 21 #include "ui/views/test/test_views_delegate.h"
22 #include "ui/views/widget/native_widget.h" 22 #include "ui/views/widget/native_widget.h"
23 #include "ui/views/widget/root_view.h" 23 #include "ui/views/widget/root_view.h"
24 #include "ui/views/widget/widget.h" 24 #include "ui/views/widget/widget.h"
(...skipping 72 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)
119 aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_)); 118 aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_));
120 aura_test_helper_->SetUp(); 119 aura_test_helper_->SetUp();
121 #endif // USE_AURA 120 #endif // USE_AURA
121 EnableAccessibilityAndListenToFocusNotifications();
122 } 122 }
123 123
124 virtual void TearDown() { 124 virtual void TearDown() {
125 ClearCallback();
125 #if defined(USE_AURA) 126 #if defined(USE_AURA)
126 aura_test_helper_->TearDown(); 127 aura_test_helper_->TearDown();
127 #endif 128 #endif
128 delete views::ViewsDelegate::views_delegate; 129 delete views::ViewsDelegate::views_delegate;
129 views::ViewsDelegate::views_delegate = NULL; 130 views::ViewsDelegate::views_delegate = NULL;
130 131
131 // The Widget's FocusManager is deleted using DeleteSoon - this 132 // The Widget's FocusManager is deleted using DeleteSoon - this
132 // forces it to be deleted now, so we don't have any memory leaks 133 // forces it to be deleted now, so we don't have any memory leaks
133 // when this method exits. 134 // when this method exits.
134 base::MessageLoop::current()->RunUntilIdle(); 135 base::MessageLoop::current()->RunUntilIdle();
(...skipping 13 matching lines...) Expand all
148 context, 149 context,
149 gfx::Rect(0, 0, 500, 500)); 150 gfx::Rect(0, 0, 500, 500));
150 151
151 // Create a profile and associate it with this window. 152 // Create a profile and associate it with this window.
152 widget->SetNativeWindowProperty(Profile::kProfileKey, &profile_); 153 widget->SetNativeWindowProperty(Profile::kProfileKey, &profile_);
153 154
154 return widget; 155 return widget;
155 } 156 }
156 157
157 void EnableAccessibilityAndListenToFocusNotifications() { 158 void EnableAccessibilityAndListenToFocusNotifications() {
158 registrar_.Add(this,
159 chrome::NOTIFICATION_ACCESSIBILITY_CONTROL_FOCUSED,
160 content::NotificationService::AllSources());
161
162 // Switch on accessibility event notifications. 159 // Switch on accessibility event notifications.
163 ExtensionAccessibilityEventRouter* accessibility_event_router = 160 ExtensionAccessibilityEventRouter* accessibility_event_router =
164 ExtensionAccessibilityEventRouter::GetInstance(); 161 ExtensionAccessibilityEventRouter::GetInstance();
165 accessibility_event_router->SetAccessibilityEnabled(true); 162 accessibility_event_router->SetAccessibilityEnabled(true);
163 accessibility_event_router->SetControlEventCallbackForTesting(base::Bind(
164 &AccessibilityEventRouterViewsTest::OnFocusEvent,
165 base::Unretained(this)));
166 }
167
168 void ClearCallback() {
169 ExtensionAccessibilityEventRouter* accessibility_event_router =
170 ExtensionAccessibilityEventRouter::GetInstance();
171 accessibility_event_router->ClearControlEventCallback();
166 } 172 }
167 173
168 protected: 174 protected:
169 // Implement NotificationObserver::Observe and store information about a 175 // Handle Focus event.
170 // ACCESSIBILITY_CONTROL_FOCUSED event. 176 virtual void OnFocusEvent(ui::AccessibilityTypes::Event event,
171 virtual void Observe(int type, 177 const AccessibilityControlInfo* info) {
172 const content::NotificationSource& source,
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 15 matching lines...) Expand all
212 views::LabelButton* button3 = new views::LabelButton( 212 views::LabelButton* button3 = new views::LabelButton(
213 NULL, ASCIIToUTF16(kButton3ASCII)); 213 NULL, ASCIIToUTF16(kButton3ASCII));
214 button3->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); 214 button3->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON);
215 contents->AddChildView(button3); 215 contents->AddChildView(button3);
216 216
217 // Put the view in a window. 217 // Put the view in a window.
218 views::Widget* window = CreateWindowWithContents(contents); 218 views::Widget* window = CreateWindowWithContents(contents);
219 window->Show(); 219 window->Show();
220 window->Activate(); 220 window->Activate();
221 221
222 // Set focus to the first button initially. 222 // Set focus to the first button initially and run message loop to execute
223 // callback.
223 button1->RequestFocus(); 224 button1->RequestFocus();
224 225 base::MessageLoop::current()->RunUntilIdle();
225 EnableAccessibilityAndListenToFocusNotifications();
226 226
227 // Change the accessible name of button3. 227 // Change the accessible name of button3.
228 button3->SetAccessibleName(ASCIIToUTF16(kButton3NewASCII)); 228 button3->SetAccessibleName(ASCIIToUTF16(kButton3NewASCII));
229 229
230 // 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
231 // expected notification with the name of button 2. 231 // expected notification with the name of button 2.
232 views::FocusManager* focus_manager = contents->GetWidget()->GetFocusManager(); 232 views::FocusManager* focus_manager = contents->GetWidget()->GetFocusManager();
233 focus_event_count_ = 0; 233 focus_event_count_ = 0;
234 focus_manager->AdvanceFocus(false); 234 focus_manager->AdvanceFocus(false);
235 base::MessageLoop::current()->RunUntilIdle(); 235 base::MessageLoop::current()->RunUntilIdle();
(...skipping 24 matching lines...) Expand all
260 ASCIIToUTF16(kToolbarNameASCII), 260 ASCIIToUTF16(kToolbarNameASCII),
261 ui::AccessibilityTypes::ROLE_TOOLBAR); 261 ui::AccessibilityTypes::ROLE_TOOLBAR);
262 views::LabelButton* button = new views::LabelButton( 262 views::LabelButton* button = new views::LabelButton(
263 NULL, ASCIIToUTF16(kButtonNameASCII)); 263 NULL, ASCIIToUTF16(kButtonNameASCII));
264 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); 264 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON);
265 contents->AddChildView(button); 265 contents->AddChildView(button);
266 266
267 // Put the view in a window. 267 // Put the view in a window.
268 views::Widget* window = CreateWindowWithContents(contents); 268 views::Widget* window = CreateWindowWithContents(contents);
269 269
270 EnableAccessibilityAndListenToFocusNotifications();
271
272 // Set focus to the button. 270 // Set focus to the button.
273 focus_event_count_ = 0; 271 focus_event_count_ = 0;
274 button->RequestFocus(); 272 button->RequestFocus();
275 273
276 base::MessageLoop::current()->RunUntilIdle(); 274 base::MessageLoop::current()->RunUntilIdle();
277 275
278 // 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.
279 EXPECT_EQ(kInitialFocusCount, focus_event_count_); 277 EXPECT_EQ(kInitialFocusCount, focus_event_count_);
280 EXPECT_EQ(kButtonNameASCII, last_control_name_); 278 EXPECT_EQ(kButtonNameASCII, last_control_name_);
281 EXPECT_EQ(kToolbarNameASCII, last_control_context_); 279 EXPECT_EQ(kToolbarNameASCII, last_control_context_);
(...skipping 12 matching lines...) Expand all
294 views::Label* label = new views::Label(ASCIIToUTF16(kAlertTextASCII)); 292 views::Label* label = new views::Label(ASCIIToUTF16(kAlertTextASCII));
295 contents->AddChildView(label); 293 contents->AddChildView(label);
296 views::LabelButton* button = new views::LabelButton( 294 views::LabelButton* button = new views::LabelButton(
297 NULL, ASCIIToUTF16(kButtonNameASCII)); 295 NULL, ASCIIToUTF16(kButtonNameASCII));
298 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); 296 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON);
299 contents->AddChildView(button); 297 contents->AddChildView(button);
300 298
301 // Put the view in a window. 299 // Put the view in a window.
302 views::Widget* window = CreateWindowWithContents(contents); 300 views::Widget* window = CreateWindowWithContents(contents);
303 301
304 EnableAccessibilityAndListenToFocusNotifications();
305
306 // Set focus to the button. 302 // Set focus to the button.
307 focus_event_count_ = 0; 303 focus_event_count_ = 0;
308 button->RequestFocus(); 304 button->RequestFocus();
309 305
310 base::MessageLoop::current()->RunUntilIdle(); 306 base::MessageLoop::current()->RunUntilIdle();
311 307
312 // 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.
313 EXPECT_EQ(kInitialFocusCount, focus_event_count_); 309 EXPECT_EQ(kInitialFocusCount, focus_event_count_);
314 EXPECT_EQ(kButtonNameASCII, last_control_name_); 310 EXPECT_EQ(kButtonNameASCII, last_control_name_);
315 EXPECT_EQ(kAlertTextASCII, last_control_context_); 311 EXPECT_EQ(kAlertTextASCII, last_control_context_);
(...skipping 12 matching lines...) Expand all
328 ui::AccessibilityTypes::ROLE_CLIENT); 324 ui::AccessibilityTypes::ROLE_CLIENT);
329 ViewWithNameAndRole* child = new ViewWithNameAndRole( 325 ViewWithNameAndRole* child = new ViewWithNameAndRole(
330 ASCIIToUTF16(kOldNameASCII), 326 ASCIIToUTF16(kOldNameASCII),
331 ui::AccessibilityTypes::ROLE_PUSHBUTTON); 327 ui::AccessibilityTypes::ROLE_PUSHBUTTON);
332 child->set_focusable(true); 328 child->set_focusable(true);
333 contents->AddChildView(child); 329 contents->AddChildView(child);
334 330
335 // Put the view in a window. 331 // Put the view in a window.
336 views::Widget* window = CreateWindowWithContents(contents); 332 views::Widget* window = CreateWindowWithContents(contents);
337 333
338 EnableAccessibilityAndListenToFocusNotifications();
339
340 // Set focus to the child view. 334 // Set focus to the child view.
341 focus_event_count_ = 0; 335 focus_event_count_ = 0;
342 child->RequestFocus(); 336 child->RequestFocus();
343 337
344 // Change the child's name after the focus notification. 338 // Change the child's name after the focus notification.
345 child->set_name(ASCIIToUTF16(kNewNameASCII)); 339 child->set_name(ASCIIToUTF16(kNewNameASCII));
346 340
347 // We shouldn't get the notification right away. 341 // We shouldn't get the notification right away.
348 EXPECT_EQ(0, focus_event_count_); 342 EXPECT_EQ(0, focus_event_count_);
349 343
(...skipping 16 matching lines...) Expand all
366 ui::AccessibilityTypes::ROLE_CLIENT); 360 ui::AccessibilityTypes::ROLE_CLIENT);
367 ViewWithNameAndRole* child = new ViewWithNameAndRole( 361 ViewWithNameAndRole* child = new ViewWithNameAndRole(
368 ASCIIToUTF16(kNameASCII), 362 ASCIIToUTF16(kNameASCII),
369 ui::AccessibilityTypes::ROLE_PUSHBUTTON); 363 ui::AccessibilityTypes::ROLE_PUSHBUTTON);
370 child->set_focusable(true); 364 child->set_focusable(true);
371 contents->AddChildView(child); 365 contents->AddChildView(child);
372 366
373 // Put the view in a window. 367 // Put the view in a window.
374 views::Widget* window = CreateWindowWithContents(contents); 368 views::Widget* window = CreateWindowWithContents(contents);
375 369
376 EnableAccessibilityAndListenToFocusNotifications();
377
378 // Set focus to the child view. 370 // Set focus to the child view.
379 focus_event_count_ = 0; 371 focus_event_count_ = 0;
380 child->RequestFocus(); 372 child->RequestFocus();
381 373
382 // Delete the child! 374 // Delete the child!
383 delete child; 375 delete child;
384 376
385 // We shouldn't get the notification right away. 377 // We shouldn't get the notification right away.
386 EXPECT_EQ(0, focus_event_count_); 378 EXPECT_EQ(0, focus_event_count_);
387 379
388 // 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
389 // 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.
390 base::MessageLoop::current()->RunUntilIdle(); 382 base::MessageLoop::current()->RunUntilIdle();
391 EXPECT_EQ(0, focus_event_count_); 383 EXPECT_EQ(0, focus_event_count_);
392 384
393 window->CloseNow(); 385 window->CloseNow();
394 } 386 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698