| OLD | NEW |
| 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" | |
| 12 #include "chrome/browser/ui/views/accessibility/accessibility_event_router_views
.h" | 11 #include "chrome/browser/ui/views/accessibility/accessibility_event_router_views
.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/browser/notification_registrar.h" | |
| 15 #include "content/public/browser/notification_service.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "ui/base/accessibility/accessible_view_state.h" | 14 #include "ui/base/accessibility/accessible_view_state.h" |
| 18 #include "ui/views/controls/button/label_button.h" | 15 #include "ui/views/controls/button/label_button.h" |
| 19 #include "ui/views/controls/label.h" | 16 #include "ui/views/controls/label.h" |
| 20 #include "ui/views/layout/grid_layout.h" | 17 #include "ui/views/layout/grid_layout.h" |
| 21 #include "ui/views/test/test_views_delegate.h" | 18 #include "ui/views/test/test_views_delegate.h" |
| 22 #include "ui/views/widget/native_widget.h" | 19 #include "ui/views/widget/native_widget.h" |
| 23 #include "ui/views/widget/root_view.h" | 20 #include "ui/views/widget/root_view.h" |
| 24 #include "ui/views/widget/widget.h" | 21 #include "ui/views/widget/widget.h" |
| 25 #include "ui/views/widget/widget_delegate.h" | 22 #include "ui/views/widget/widget_delegate.h" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 94 |
| 98 void set_name(const string16& name) { name_ = name; } | 95 void set_name(const string16& name) { name_ = name; } |
| 99 | 96 |
| 100 private: | 97 private: |
| 101 string16 name_; | 98 string16 name_; |
| 102 ui::AccessibilityTypes::Role role_; | 99 ui::AccessibilityTypes::Role role_; |
| 103 DISALLOW_COPY_AND_ASSIGN(ViewWithNameAndRole); | 100 DISALLOW_COPY_AND_ASSIGN(ViewWithNameAndRole); |
| 104 }; | 101 }; |
| 105 | 102 |
| 106 class AccessibilityEventRouterViewsTest | 103 class AccessibilityEventRouterViewsTest |
| 107 : public testing::Test, | 104 : public testing::Test { |
| 108 public content::NotificationObserver { | |
| 109 public: | 105 public: |
| 110 AccessibilityEventRouterViewsTest() { | 106 AccessibilityEventRouterViewsTest() { |
| 111 } | 107 } |
| 112 | 108 |
| 113 virtual void SetUp() { | 109 virtual void SetUp() { |
| 114 #if defined(OS_WIN) | 110 #if defined(OS_WIN) |
| 115 ole_initializer_.reset(new ui::ScopedOleInitializer()); | 111 ole_initializer_.reset(new ui::ScopedOleInitializer()); |
| 116 #endif | 112 #endif |
| 117 views::ViewsDelegate::views_delegate = new AccessibilityViewsDelegate(); | 113 views::ViewsDelegate::views_delegate = new AccessibilityViewsDelegate(); |
| 118 #if defined(USE_AURA) | 114 #if defined(USE_AURA) |
| 119 aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_)); | 115 aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_)); |
| 120 aura_test_helper_->SetUp(); | 116 aura_test_helper_->SetUp(); |
| 121 #endif // USE_AURA | 117 #endif // USE_AURA |
| 118 EnableAccessibilityAndListenToFocusNotifications(); |
| 122 } | 119 } |
| 123 | 120 |
| 124 virtual void TearDown() { | 121 virtual void TearDown() { |
| 122 ClearCallback(); |
| 125 #if defined(USE_AURA) | 123 #if defined(USE_AURA) |
| 126 aura_test_helper_->TearDown(); | 124 aura_test_helper_->TearDown(); |
| 127 #endif | 125 #endif |
| 128 delete views::ViewsDelegate::views_delegate; | 126 delete views::ViewsDelegate::views_delegate; |
| 129 views::ViewsDelegate::views_delegate = NULL; | 127 views::ViewsDelegate::views_delegate = NULL; |
| 130 | 128 |
| 131 // The Widget's FocusManager is deleted using DeleteSoon - this | 129 // The Widget's FocusManager is deleted using DeleteSoon - this |
| 132 // forces it to be deleted now, so we don't have any memory leaks | 130 // forces it to be deleted now, so we don't have any memory leaks |
| 133 // when this method exits. | 131 // when this method exits. |
| 134 base::MessageLoop::current()->RunUntilIdle(); | 132 base::MessageLoop::current()->RunUntilIdle(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 148 context, | 146 context, |
| 149 gfx::Rect(0, 0, 500, 500)); | 147 gfx::Rect(0, 0, 500, 500)); |
| 150 | 148 |
| 151 // Create a profile and associate it with this window. | 149 // Create a profile and associate it with this window. |
| 152 widget->SetNativeWindowProperty(Profile::kProfileKey, &profile_); | 150 widget->SetNativeWindowProperty(Profile::kProfileKey, &profile_); |
| 153 | 151 |
| 154 return widget; | 152 return widget; |
| 155 } | 153 } |
| 156 | 154 |
| 157 void EnableAccessibilityAndListenToFocusNotifications() { | 155 void EnableAccessibilityAndListenToFocusNotifications() { |
| 158 registrar_.Add(this, | |
| 159 chrome::NOTIFICATION_ACCESSIBILITY_CONTROL_FOCUSED, | |
| 160 content::NotificationService::AllSources()); | |
| 161 | |
| 162 // Switch on accessibility event notifications. | 156 // Switch on accessibility event notifications. |
| 163 ExtensionAccessibilityEventRouter* accessibility_event_router = | 157 ExtensionAccessibilityEventRouter* accessibility_event_router = |
| 164 ExtensionAccessibilityEventRouter::GetInstance(); | 158 ExtensionAccessibilityEventRouter::GetInstance(); |
| 165 accessibility_event_router->SetAccessibilityEnabled(true); | 159 accessibility_event_router->SetAccessibilityEnabled(true); |
| 160 accessibility_event_router->SetControlEventCallbackForTesting(base::Bind( |
| 161 &AccessibilityEventRouterViewsTest::OnFocusEvent, |
| 162 base::Unretained(this))); |
| 163 } |
| 164 |
| 165 void ClearCallback() { |
| 166 ExtensionAccessibilityEventRouter* accessibility_event_router = |
| 167 ExtensionAccessibilityEventRouter::GetInstance(); |
| 168 accessibility_event_router->ClearControlEventCallback(); |
| 166 } | 169 } |
| 167 | 170 |
| 168 protected: | 171 protected: |
| 169 // Implement NotificationObserver::Observe and store information about a | 172 // Handle Focus event. |
| 170 // ACCESSIBILITY_CONTROL_FOCUSED event. | 173 virtual void OnFocusEvent(ui::AccessibilityTypes::Event event, |
| 171 virtual void Observe(int type, | 174 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_++; | 175 focus_event_count_++; |
| 178 last_control_name_ = info->name(); | 176 last_control_name_ = info->name(); |
| 179 last_control_context_ = info->context(); | 177 last_control_context_ = info->context(); |
| 180 } | 178 } |
| 181 | 179 |
| 182 base::MessageLoopForUI message_loop_; | 180 base::MessageLoopForUI message_loop_; |
| 183 int focus_event_count_; | 181 int focus_event_count_; |
| 184 std::string last_control_name_; | 182 std::string last_control_name_; |
| 185 std::string last_control_context_; | 183 std::string last_control_context_; |
| 186 content::NotificationRegistrar registrar_; | |
| 187 TestingProfile profile_; | 184 TestingProfile profile_; |
| 188 #if defined(OS_WIN) | 185 #if defined(OS_WIN) |
| 189 scoped_ptr<ui::ScopedOleInitializer> ole_initializer_; | 186 scoped_ptr<ui::ScopedOleInitializer> ole_initializer_; |
| 190 #endif | 187 #endif |
| 191 #if defined(USE_AURA) | 188 #if defined(USE_AURA) |
| 192 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_; | 189 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_; |
| 193 #endif | 190 #endif |
| 194 }; | 191 }; |
| 195 | 192 |
| 196 TEST_F(AccessibilityEventRouterViewsTest, TestFocusNotification) { | 193 TEST_F(AccessibilityEventRouterViewsTest, TestFocusNotification) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 212 views::LabelButton* button3 = new views::LabelButton( | 209 views::LabelButton* button3 = new views::LabelButton( |
| 213 NULL, ASCIIToUTF16(kButton3ASCII)); | 210 NULL, ASCIIToUTF16(kButton3ASCII)); |
| 214 button3->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); | 211 button3->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); |
| 215 contents->AddChildView(button3); | 212 contents->AddChildView(button3); |
| 216 | 213 |
| 217 // Put the view in a window. | 214 // Put the view in a window. |
| 218 views::Widget* window = CreateWindowWithContents(contents); | 215 views::Widget* window = CreateWindowWithContents(contents); |
| 219 window->Show(); | 216 window->Show(); |
| 220 window->Activate(); | 217 window->Activate(); |
| 221 | 218 |
| 222 // Set focus to the first button initially. | 219 // Set focus to the first button initially and run message loop to execute |
| 220 // callback. |
| 223 button1->RequestFocus(); | 221 button1->RequestFocus(); |
| 224 | 222 base::MessageLoop::current()->RunUntilIdle(); |
| 225 EnableAccessibilityAndListenToFocusNotifications(); | |
| 226 | 223 |
| 227 // Change the accessible name of button3. | 224 // Change the accessible name of button3. |
| 228 button3->SetAccessibleName(ASCIIToUTF16(kButton3NewASCII)); | 225 button3->SetAccessibleName(ASCIIToUTF16(kButton3NewASCII)); |
| 229 | 226 |
| 230 // Advance focus to the next button and test that we got the | 227 // Advance focus to the next button and test that we got the |
| 231 // expected notification with the name of button 2. | 228 // expected notification with the name of button 2. |
| 232 views::FocusManager* focus_manager = contents->GetWidget()->GetFocusManager(); | 229 views::FocusManager* focus_manager = contents->GetWidget()->GetFocusManager(); |
| 233 focus_event_count_ = 0; | 230 focus_event_count_ = 0; |
| 234 focus_manager->AdvanceFocus(false); | 231 focus_manager->AdvanceFocus(false); |
| 235 base::MessageLoop::current()->RunUntilIdle(); | 232 base::MessageLoop::current()->RunUntilIdle(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 260 ASCIIToUTF16(kToolbarNameASCII), | 257 ASCIIToUTF16(kToolbarNameASCII), |
| 261 ui::AccessibilityTypes::ROLE_TOOLBAR); | 258 ui::AccessibilityTypes::ROLE_TOOLBAR); |
| 262 views::LabelButton* button = new views::LabelButton( | 259 views::LabelButton* button = new views::LabelButton( |
| 263 NULL, ASCIIToUTF16(kButtonNameASCII)); | 260 NULL, ASCIIToUTF16(kButtonNameASCII)); |
| 264 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); | 261 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); |
| 265 contents->AddChildView(button); | 262 contents->AddChildView(button); |
| 266 | 263 |
| 267 // Put the view in a window. | 264 // Put the view in a window. |
| 268 views::Widget* window = CreateWindowWithContents(contents); | 265 views::Widget* window = CreateWindowWithContents(contents); |
| 269 | 266 |
| 270 EnableAccessibilityAndListenToFocusNotifications(); | |
| 271 | |
| 272 // Set focus to the button. | 267 // Set focus to the button. |
| 273 focus_event_count_ = 0; | 268 focus_event_count_ = 0; |
| 274 button->RequestFocus(); | 269 button->RequestFocus(); |
| 275 | 270 |
| 276 base::MessageLoop::current()->RunUntilIdle(); | 271 base::MessageLoop::current()->RunUntilIdle(); |
| 277 | 272 |
| 278 // Test that we got the event with the expected name and context. | 273 // Test that we got the event with the expected name and context. |
| 279 EXPECT_EQ(kInitialFocusCount, focus_event_count_); | 274 EXPECT_EQ(kInitialFocusCount, focus_event_count_); |
| 280 EXPECT_EQ(kButtonNameASCII, last_control_name_); | 275 EXPECT_EQ(kButtonNameASCII, last_control_name_); |
| 281 EXPECT_EQ(kToolbarNameASCII, last_control_context_); | 276 EXPECT_EQ(kToolbarNameASCII, last_control_context_); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 294 views::Label* label = new views::Label(ASCIIToUTF16(kAlertTextASCII)); | 289 views::Label* label = new views::Label(ASCIIToUTF16(kAlertTextASCII)); |
| 295 contents->AddChildView(label); | 290 contents->AddChildView(label); |
| 296 views::LabelButton* button = new views::LabelButton( | 291 views::LabelButton* button = new views::LabelButton( |
| 297 NULL, ASCIIToUTF16(kButtonNameASCII)); | 292 NULL, ASCIIToUTF16(kButtonNameASCII)); |
| 298 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); | 293 button->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); |
| 299 contents->AddChildView(button); | 294 contents->AddChildView(button); |
| 300 | 295 |
| 301 // Put the view in a window. | 296 // Put the view in a window. |
| 302 views::Widget* window = CreateWindowWithContents(contents); | 297 views::Widget* window = CreateWindowWithContents(contents); |
| 303 | 298 |
| 304 EnableAccessibilityAndListenToFocusNotifications(); | |
| 305 | |
| 306 // Set focus to the button. | 299 // Set focus to the button. |
| 307 focus_event_count_ = 0; | 300 focus_event_count_ = 0; |
| 308 button->RequestFocus(); | 301 button->RequestFocus(); |
| 309 | 302 |
| 310 base::MessageLoop::current()->RunUntilIdle(); | 303 base::MessageLoop::current()->RunUntilIdle(); |
| 311 | 304 |
| 312 // Test that we got the event with the expected name and context. | 305 // Test that we got the event with the expected name and context. |
| 313 EXPECT_EQ(kInitialFocusCount, focus_event_count_); | 306 EXPECT_EQ(kInitialFocusCount, focus_event_count_); |
| 314 EXPECT_EQ(kButtonNameASCII, last_control_name_); | 307 EXPECT_EQ(kButtonNameASCII, last_control_name_); |
| 315 EXPECT_EQ(kAlertTextASCII, last_control_context_); | 308 EXPECT_EQ(kAlertTextASCII, last_control_context_); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 328 ui::AccessibilityTypes::ROLE_CLIENT); | 321 ui::AccessibilityTypes::ROLE_CLIENT); |
| 329 ViewWithNameAndRole* child = new ViewWithNameAndRole( | 322 ViewWithNameAndRole* child = new ViewWithNameAndRole( |
| 330 ASCIIToUTF16(kOldNameASCII), | 323 ASCIIToUTF16(kOldNameASCII), |
| 331 ui::AccessibilityTypes::ROLE_PUSHBUTTON); | 324 ui::AccessibilityTypes::ROLE_PUSHBUTTON); |
| 332 child->set_focusable(true); | 325 child->set_focusable(true); |
| 333 contents->AddChildView(child); | 326 contents->AddChildView(child); |
| 334 | 327 |
| 335 // Put the view in a window. | 328 // Put the view in a window. |
| 336 views::Widget* window = CreateWindowWithContents(contents); | 329 views::Widget* window = CreateWindowWithContents(contents); |
| 337 | 330 |
| 338 EnableAccessibilityAndListenToFocusNotifications(); | |
| 339 | |
| 340 // Set focus to the child view. | 331 // Set focus to the child view. |
| 341 focus_event_count_ = 0; | 332 focus_event_count_ = 0; |
| 342 child->RequestFocus(); | 333 child->RequestFocus(); |
| 343 | 334 |
| 344 // Change the child's name after the focus notification. | 335 // Change the child's name after the focus notification. |
| 345 child->set_name(ASCIIToUTF16(kNewNameASCII)); | 336 child->set_name(ASCIIToUTF16(kNewNameASCII)); |
| 346 | 337 |
| 347 // We shouldn't get the notification right away. | 338 // We shouldn't get the notification right away. |
| 348 EXPECT_EQ(0, focus_event_count_); | 339 EXPECT_EQ(0, focus_event_count_); |
| 349 | 340 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 366 ui::AccessibilityTypes::ROLE_CLIENT); | 357 ui::AccessibilityTypes::ROLE_CLIENT); |
| 367 ViewWithNameAndRole* child = new ViewWithNameAndRole( | 358 ViewWithNameAndRole* child = new ViewWithNameAndRole( |
| 368 ASCIIToUTF16(kNameASCII), | 359 ASCIIToUTF16(kNameASCII), |
| 369 ui::AccessibilityTypes::ROLE_PUSHBUTTON); | 360 ui::AccessibilityTypes::ROLE_PUSHBUTTON); |
| 370 child->set_focusable(true); | 361 child->set_focusable(true); |
| 371 contents->AddChildView(child); | 362 contents->AddChildView(child); |
| 372 | 363 |
| 373 // Put the view in a window. | 364 // Put the view in a window. |
| 374 views::Widget* window = CreateWindowWithContents(contents); | 365 views::Widget* window = CreateWindowWithContents(contents); |
| 375 | 366 |
| 376 EnableAccessibilityAndListenToFocusNotifications(); | |
| 377 | |
| 378 // Set focus to the child view. | 367 // Set focus to the child view. |
| 379 focus_event_count_ = 0; | 368 focus_event_count_ = 0; |
| 380 child->RequestFocus(); | 369 child->RequestFocus(); |
| 381 | 370 |
| 382 // Delete the child! | 371 // Delete the child! |
| 383 delete child; | 372 delete child; |
| 384 | 373 |
| 385 // We shouldn't get the notification right away. | 374 // We shouldn't get the notification right away. |
| 386 EXPECT_EQ(0, focus_event_count_); | 375 EXPECT_EQ(0, focus_event_count_); |
| 387 | 376 |
| 388 // Process anything in the event loop. We shouldn't get a notification | 377 // 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. | 378 // because the view is no longer valid, and this shouldn't crash. |
| 390 base::MessageLoop::current()->RunUntilIdle(); | 379 base::MessageLoop::current()->RunUntilIdle(); |
| 391 EXPECT_EQ(0, focus_event_count_); | 380 EXPECT_EQ(0, focus_event_count_); |
| 392 | 381 |
| 393 window->CloseNow(); | 382 window->CloseNow(); |
| 394 } | 383 } |
| OLD | NEW |