| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 | |
| 7 #include "base/message_loop.h" | |
| 8 #include "base/string_util.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/browser/accessibility/accessibility_extension_api.h" | |
| 11 #include "chrome/browser/ui/views/accessibility_event_router_views.h" | |
| 12 #include "chrome/common/chrome_notification_types.h" | |
| 13 #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" | |
| 17 #include "ui/base/accessibility/accessible_view_state.h" | |
| 18 #include "ui/views/controls/button/text_button.h" | |
| 19 #include "ui/views/controls/label.h" | |
| 20 #include "ui/views/layout/grid_layout.h" | |
| 21 #include "ui/views/views_delegate.h" | |
| 22 #include "ui/views/widget/native_widget.h" | |
| 23 #include "ui/views/widget/root_view.h" | |
| 24 #include "ui/views/widget/widget.h" | |
| 25 #include "ui/views/widget/widget_delegate.h" | |
| 26 | |
| 27 #if defined(USE_AURA) | |
| 28 #include "ui/aura/test/aura_test_helper.h" | |
| 29 #endif | |
| 30 | |
| 31 #if defined(TOOLKIT_VIEWS) | |
| 32 | |
| 33 class AccessibilityViewsDelegate : public views::ViewsDelegate { | |
| 34 public: | |
| 35 AccessibilityViewsDelegate() {} | |
| 36 virtual ~AccessibilityViewsDelegate() {} | |
| 37 | |
| 38 // Overridden from views::ViewsDelegate: | |
| 39 virtual ui::Clipboard* GetClipboard() const OVERRIDE { return NULL; } | |
| 40 virtual void SaveWindowPlacement(const views::Widget* window, | |
| 41 const std::string& window_name, | |
| 42 const gfx::Rect& bounds, | |
| 43 ui::WindowShowState show_state) OVERRIDE { | |
| 44 } | |
| 45 virtual bool GetSavedWindowPlacement( | |
| 46 const std::string& window_name, | |
| 47 gfx::Rect* bounds, | |
| 48 ui::WindowShowState* show_state) const OVERRIDE { | |
| 49 return false; | |
| 50 } | |
| 51 virtual void NotifyAccessibilityEvent( | |
| 52 views::View* view, ui::AccessibilityTypes::Event event_type) OVERRIDE { | |
| 53 AccessibilityEventRouterViews::GetInstance()->HandleAccessibilityEvent( | |
| 54 view, event_type); | |
| 55 } | |
| 56 virtual void NotifyMenuItemFocused(const string16& menu_name, | |
| 57 const string16& menu_item_name, | |
| 58 int item_index, | |
| 59 int item_count, | |
| 60 bool has_submenu) OVERRIDE {} | |
| 61 #if defined(OS_WIN) | |
| 62 virtual HICON GetDefaultWindowIcon() const OVERRIDE { | |
| 63 return NULL; | |
| 64 } | |
| 65 #endif | |
| 66 virtual views::NonClientFrameView* CreateDefaultNonClientFrameView( | |
| 67 views::Widget* widget) OVERRIDE { | |
| 68 return NULL; | |
| 69 } | |
| 70 virtual bool UseTransparentWindows() const OVERRIDE { | |
| 71 return false; | |
| 72 } | |
| 73 virtual void AddRef() OVERRIDE {} | |
| 74 virtual void ReleaseRef() OVERRIDE {} | |
| 75 | |
| 76 virtual int GetDispositionForEvent(int event_flags) OVERRIDE { | |
| 77 return 0; | |
| 78 } | |
| 79 | |
| 80 #if defined(USE_AURA) | |
| 81 virtual views::NativeWidgetHelperAura* CreateNativeWidgetHelper( | |
| 82 views::NativeWidgetAura* native_widget) OVERRIDE { | |
| 83 return NULL; | |
| 84 } | |
| 85 #endif | |
| 86 | |
| 87 virtual content::WebContents* CreateWebContents( | |
| 88 content::BrowserContext* browser_context, | |
| 89 content::SiteInstance* site_instance) OVERRIDE { | |
| 90 return NULL; | |
| 91 } | |
| 92 | |
| 93 DISALLOW_COPY_AND_ASSIGN(AccessibilityViewsDelegate); | |
| 94 }; | |
| 95 | |
| 96 class AccessibilityWindowDelegate : public views::WidgetDelegate { | |
| 97 public: | |
| 98 explicit AccessibilityWindowDelegate(views::View* contents) | |
| 99 : contents_(contents) { } | |
| 100 | |
| 101 // Overridden from views::WidgetDelegate: | |
| 102 virtual void DeleteDelegate() OVERRIDE { delete this; } | |
| 103 virtual views::View* GetContentsView() OVERRIDE { return contents_; } | |
| 104 virtual const views::Widget* GetWidget() const OVERRIDE { | |
| 105 return contents_->GetWidget(); | |
| 106 } | |
| 107 virtual views::Widget* GetWidget() OVERRIDE { return contents_->GetWidget(); } | |
| 108 | |
| 109 private: | |
| 110 views::View* contents_; | |
| 111 }; | |
| 112 | |
| 113 class ViewWithNameAndRole : public views::View { | |
| 114 public: | |
| 115 explicit ViewWithNameAndRole(const string16& name, | |
| 116 ui::AccessibilityTypes::Role role) | |
| 117 : name_(name), | |
| 118 role_(role) { | |
| 119 } | |
| 120 | |
| 121 void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE { | |
| 122 views::View::GetAccessibleState(state); | |
| 123 state->name = name_; | |
| 124 state->role = role_; | |
| 125 } | |
| 126 | |
| 127 private: | |
| 128 string16 name_; | |
| 129 ui::AccessibilityTypes::Role role_; | |
| 130 }; | |
| 131 | |
| 132 class AccessibilityEventRouterViewsTest | |
| 133 : public testing::Test, | |
| 134 public content::NotificationObserver { | |
| 135 public: | |
| 136 AccessibilityEventRouterViewsTest() { | |
| 137 } | |
| 138 | |
| 139 virtual void SetUp() { | |
| 140 views::ViewsDelegate::views_delegate = new AccessibilityViewsDelegate(); | |
| 141 #if defined(USE_AURA) | |
| 142 aura_test_helper_.reset(new aura::test::AuraTestHelper(&message_loop_)); | |
| 143 aura_test_helper_->SetUp(); | |
| 144 #endif // USE_AURA | |
| 145 } | |
| 146 | |
| 147 virtual void TearDown() { | |
| 148 #if defined(USE_AURA) | |
| 149 aura_test_helper_->TearDown(); | |
| 150 #endif | |
| 151 delete views::ViewsDelegate::views_delegate; | |
| 152 views::ViewsDelegate::views_delegate = NULL; | |
| 153 | |
| 154 // The Widget's FocusManager is deleted using DeleteSoon - this | |
| 155 // forces it to be deleted now, so we don't have any memory leaks | |
| 156 // when this method exits. | |
| 157 MessageLoop::current()->RunAllPending(); | |
| 158 } | |
| 159 | |
| 160 views::Widget* CreateWindowWithContents(views::View* contents) { | |
| 161 return views::Widget::CreateWindowWithBounds( | |
| 162 new AccessibilityWindowDelegate(contents), | |
| 163 gfx::Rect(0, 0, 500, 500)); | |
| 164 } | |
| 165 | |
| 166 protected: | |
| 167 // Implement NotificationObserver::Observe and store information about a | |
| 168 // ACCESSIBILITY_CONTROL_FOCUSED event. | |
| 169 virtual void Observe(int type, | |
| 170 const content::NotificationSource& source, | |
| 171 const content::NotificationDetails& details) { | |
| 172 ASSERT_EQ(type, chrome::NOTIFICATION_ACCESSIBILITY_CONTROL_FOCUSED); | |
| 173 const AccessibilityControlInfo* info = | |
| 174 content::Details<const AccessibilityControlInfo>(details).ptr(); | |
| 175 focus_event_count_++; | |
| 176 last_control_name_ = info->name(); | |
| 177 last_control_context_ = info->context(); | |
| 178 } | |
| 179 | |
| 180 MessageLoopForUI message_loop_; | |
| 181 int focus_event_count_; | |
| 182 std::string last_control_name_; | |
| 183 std::string last_control_context_; | |
| 184 #if defined(USE_AURA) | |
| 185 scoped_ptr<aura::test::AuraTestHelper> aura_test_helper_; | |
| 186 #endif | |
| 187 }; | |
| 188 | |
| 189 TEST_F(AccessibilityEventRouterViewsTest, TestFocusNotification) { | |
| 190 const char kButton1ASCII[] = "Button1"; | |
| 191 const char kButton2ASCII[] = "Button2"; | |
| 192 const char kButton3ASCII[] = "Button3"; | |
| 193 const char kButton3NewASCII[] = "Button3New"; | |
| 194 | |
| 195 // Create a contents view with 3 buttons. | |
| 196 views::View* contents = new views::View(); | |
| 197 views::NativeTextButton* button1 = new views::NativeTextButton( | |
| 198 NULL, ASCIIToUTF16(kButton1ASCII)); | |
| 199 contents->AddChildView(button1); | |
| 200 views::NativeTextButton* button2 = new views::NativeTextButton( | |
| 201 NULL, ASCIIToUTF16(kButton2ASCII)); | |
| 202 contents->AddChildView(button2); | |
| 203 views::NativeTextButton* button3 = new views::NativeTextButton( | |
| 204 NULL, ASCIIToUTF16(kButton3ASCII)); | |
| 205 contents->AddChildView(button3); | |
| 206 | |
| 207 // Put the view in a window. | |
| 208 views::Widget* window = CreateWindowWithContents(contents); | |
| 209 window->Show(); | |
| 210 window->Activate(); | |
| 211 | |
| 212 // Set focus to the first button initially. | |
| 213 button1->RequestFocus(); | |
| 214 | |
| 215 // Start listening to ACCESSIBILITY_CONTROL_FOCUSED notifications. | |
| 216 content::NotificationRegistrar registrar; | |
| 217 registrar.Add(this, | |
| 218 chrome::NOTIFICATION_ACCESSIBILITY_CONTROL_FOCUSED, | |
| 219 content::NotificationService::AllSources()); | |
| 220 | |
| 221 // Switch on accessibility event notifications. | |
| 222 ExtensionAccessibilityEventRouter* accessibility_event_router = | |
| 223 ExtensionAccessibilityEventRouter::GetInstance(); | |
| 224 accessibility_event_router->SetAccessibilityEnabled(true); | |
| 225 | |
| 226 // Create a profile and associate it with this window. | |
| 227 TestingProfile profile; | |
| 228 window->SetNativeWindowProperty(Profile::kProfileKey, &profile); | |
| 229 | |
| 230 // Change the accessible name of button3. | |
| 231 button3->SetAccessibleName(ASCIIToUTF16(kButton3NewASCII)); | |
| 232 | |
| 233 // Advance focus to the next button and test that we got the | |
| 234 // expected notification with the name of button 2. | |
| 235 views::FocusManager* focus_manager = contents->GetWidget()->GetFocusManager(); | |
| 236 focus_event_count_ = 0; | |
| 237 focus_manager->AdvanceFocus(false); | |
| 238 EXPECT_EQ(1, focus_event_count_); | |
| 239 EXPECT_EQ(kButton2ASCII, last_control_name_); | |
| 240 | |
| 241 // Advance to button 3. Expect the new accessible name we assigned. | |
| 242 focus_manager->AdvanceFocus(false); | |
| 243 EXPECT_EQ(2, focus_event_count_); | |
| 244 EXPECT_EQ(kButton3NewASCII, last_control_name_); | |
| 245 | |
| 246 // Advance to button 1 and check the notification. | |
| 247 focus_manager->AdvanceFocus(false); | |
| 248 EXPECT_EQ(3, focus_event_count_); | |
| 249 EXPECT_EQ(kButton1ASCII, last_control_name_); | |
| 250 | |
| 251 window->CloseNow(); | |
| 252 } | |
| 253 | |
| 254 TEST_F(AccessibilityEventRouterViewsTest, TestToolbarContext) { | |
| 255 const char kToolbarNameASCII[] = "MyToolbar"; | |
| 256 const char kButtonNameASCII[] = "MyButton"; | |
| 257 | |
| 258 // Create a toolbar with a button. | |
| 259 views::View* contents = new ViewWithNameAndRole( | |
| 260 ASCIIToUTF16(kToolbarNameASCII), | |
| 261 ui::AccessibilityTypes::ROLE_TOOLBAR); | |
| 262 views::NativeTextButton* button = new views::NativeTextButton( | |
| 263 NULL, ASCIIToUTF16(kButtonNameASCII)); | |
| 264 contents->AddChildView(button); | |
| 265 | |
| 266 // Put the view in a window. | |
| 267 views::Widget* window = CreateWindowWithContents(contents); | |
| 268 | |
| 269 // Start listening to ACCESSIBILITY_CONTROL_FOCUSED notifications. | |
| 270 content::NotificationRegistrar registrar; | |
| 271 registrar.Add(this, | |
| 272 chrome::NOTIFICATION_ACCESSIBILITY_CONTROL_FOCUSED, | |
| 273 content::NotificationService::AllSources()); | |
| 274 | |
| 275 // Switch on accessibility event notifications. | |
| 276 ExtensionAccessibilityEventRouter* accessibility_event_router = | |
| 277 ExtensionAccessibilityEventRouter::GetInstance(); | |
| 278 accessibility_event_router->SetAccessibilityEnabled(true); | |
| 279 | |
| 280 // Create a profile and associate it with this window. | |
| 281 TestingProfile profile; | |
| 282 window->SetNativeWindowProperty(Profile::kProfileKey, &profile); | |
| 283 | |
| 284 // Set focus to the button. | |
| 285 focus_event_count_ = 0; | |
| 286 button->RequestFocus(); | |
| 287 | |
| 288 // Test that we got the event with the expected name and context. | |
| 289 EXPECT_EQ(1, focus_event_count_); | |
| 290 EXPECT_EQ(kButtonNameASCII, last_control_name_); | |
| 291 EXPECT_EQ(kToolbarNameASCII, last_control_context_); | |
| 292 | |
| 293 window->CloseNow(); | |
| 294 } | |
| 295 | |
| 296 TEST_F(AccessibilityEventRouterViewsTest, TestAlertContext) { | |
| 297 const char kAlertTextASCII[] = "MyAlertText"; | |
| 298 const char kButtonNameASCII[] = "MyButton"; | |
| 299 | |
| 300 // Create an alert with static text and a button, similar to an infobar. | |
| 301 views::View* contents = new ViewWithNameAndRole( | |
| 302 string16(), | |
| 303 ui::AccessibilityTypes::ROLE_ALERT); | |
| 304 views::Label* label = new views::Label(ASCIIToUTF16(kAlertTextASCII)); | |
| 305 contents->AddChildView(label); | |
| 306 views::NativeTextButton* button = new views::NativeTextButton( | |
| 307 NULL, ASCIIToUTF16(kButtonNameASCII)); | |
| 308 contents->AddChildView(button); | |
| 309 | |
| 310 // Put the view in a window. | |
| 311 views::Widget* window = CreateWindowWithContents(contents); | |
| 312 | |
| 313 // Start listening to ACCESSIBILITY_CONTROL_FOCUSED notifications. | |
| 314 content::NotificationRegistrar registrar; | |
| 315 registrar.Add(this, | |
| 316 chrome::NOTIFICATION_ACCESSIBILITY_CONTROL_FOCUSED, | |
| 317 content::NotificationService::AllSources()); | |
| 318 | |
| 319 // Switch on accessibility event notifications. | |
| 320 ExtensionAccessibilityEventRouter* accessibility_event_router = | |
| 321 ExtensionAccessibilityEventRouter::GetInstance(); | |
| 322 accessibility_event_router->SetAccessibilityEnabled(true); | |
| 323 | |
| 324 // Create a profile and associate it with this window. | |
| 325 TestingProfile profile; | |
| 326 window->SetNativeWindowProperty(Profile::kProfileKey, &profile); | |
| 327 | |
| 328 // Set focus to the button. | |
| 329 focus_event_count_ = 0; | |
| 330 button->RequestFocus(); | |
| 331 | |
| 332 // Test that we got the event with the expected name and context. | |
| 333 EXPECT_EQ(1, focus_event_count_); | |
| 334 EXPECT_EQ(kButtonNameASCII, last_control_name_); | |
| 335 EXPECT_EQ(kAlertTextASCII, last_control_context_); | |
| 336 | |
| 337 window->CloseNow(); | |
| 338 } | |
| 339 | |
| 340 #endif // defined(TOOLKIT_VIEWS) | |
| OLD | NEW |