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

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

Issue 8850004: Add a context field to the accessibility extension API. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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.h" 7 #include "base/message_loop.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/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/ui/views/accessibility_event_router_views.h" 11 #include "chrome/browser/ui/views/accessibility_event_router_views.h"
12 #include "chrome/common/chrome_notification_types.h" 12 #include "chrome/common/chrome_notification_types.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"
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/views/controls/button/text_button.h" 18 #include "ui/views/controls/button/text_button.h"
19 #include "ui/views/controls/label.h"
18 #include "ui/views/layout/grid_layout.h" 20 #include "ui/views/layout/grid_layout.h"
19 #include "ui/views/views_delegate.h" 21 #include "ui/views/views_delegate.h"
20 #include "ui/views/widget/native_widget.h" 22 #include "ui/views/widget/native_widget.h"
21 #include "ui/views/widget/root_view.h" 23 #include "ui/views/widget/root_view.h"
22 #include "ui/views/widget/widget.h" 24 #include "ui/views/widget/widget.h"
23 #include "ui/views/widget/widget_delegate.h" 25 #include "ui/views/widget/widget_delegate.h"
24 26
25 #if defined(TOOLKIT_VIEWS) 27 #if defined(TOOLKIT_VIEWS)
26 28
27 class AccessibilityViewsDelegate : public views::ViewsDelegate { 29 class AccessibilityViewsDelegate : public views::ViewsDelegate {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 virtual views::View* GetContentsView() OVERRIDE { return contents_; } 79 virtual views::View* GetContentsView() OVERRIDE { return contents_; }
78 virtual const views::Widget* GetWidget() const OVERRIDE { 80 virtual const views::Widget* GetWidget() const OVERRIDE {
79 return contents_->GetWidget(); 81 return contents_->GetWidget();
80 } 82 }
81 virtual views::Widget* GetWidget() OVERRIDE { return contents_->GetWidget(); } 83 virtual views::Widget* GetWidget() OVERRIDE { return contents_->GetWidget(); }
82 84
83 private: 85 private:
84 views::View* contents_; 86 views::View* contents_;
85 }; 87 };
86 88
89 class ViewWithNameAndRole : public views::View {
90 public:
91 explicit ViewWithNameAndRole(const string16& name,
92 ui::AccessibilityTypes::Role role)
93 : name_(name),
94 role_(role) {
95 }
96
97 void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE {
98 views::View::GetAccessibleState(state);
99 state->name = name_;
100 state->role = role_;
101 }
102
103 private:
104 string16 name_;
105 ui::AccessibilityTypes::Role role_;
106 };
107
87 class AccessibilityEventRouterViewsTest 108 class AccessibilityEventRouterViewsTest
88 : public testing::Test, 109 : public testing::Test,
89 public content::NotificationObserver { 110 public content::NotificationObserver {
90 public: 111 public:
91 virtual void SetUp() { 112 virtual void SetUp() {
92 views::ViewsDelegate::views_delegate = new AccessibilityViewsDelegate(); 113 views::ViewsDelegate::views_delegate = new AccessibilityViewsDelegate();
93 } 114 }
94 115
95 virtual void TearDown() { 116 virtual void TearDown() {
96 delete views::ViewsDelegate::views_delegate; 117 delete views::ViewsDelegate::views_delegate;
(...skipping 10 matching lines...) Expand all
107 // Implement NotificationObserver::Observe and store information about a 128 // Implement NotificationObserver::Observe and store information about a
108 // ACCESSIBILITY_CONTROL_FOCUSED event. 129 // ACCESSIBILITY_CONTROL_FOCUSED event.
109 virtual void Observe(int type, 130 virtual void Observe(int type,
110 const content::NotificationSource& source, 131 const content::NotificationSource& source,
111 const content::NotificationDetails& details) { 132 const content::NotificationDetails& details) {
112 ASSERT_EQ(type, chrome::NOTIFICATION_ACCESSIBILITY_CONTROL_FOCUSED); 133 ASSERT_EQ(type, chrome::NOTIFICATION_ACCESSIBILITY_CONTROL_FOCUSED);
113 const AccessibilityControlInfo* info = 134 const AccessibilityControlInfo* info =
114 content::Details<const AccessibilityControlInfo>(details).ptr(); 135 content::Details<const AccessibilityControlInfo>(details).ptr();
115 focus_event_count_++; 136 focus_event_count_++;
116 last_control_name_ = info->name(); 137 last_control_name_ = info->name();
138 last_control_context_ = info->context();
117 } 139 }
118 140
119 MessageLoopForUI message_loop_; 141 MessageLoopForUI message_loop_;
120 int focus_event_count_; 142 int focus_event_count_;
121 std::string last_control_name_; 143 std::string last_control_name_;
144 std::string last_control_context_;
122 }; 145 };
123 146
124 TEST_F(AccessibilityEventRouterViewsTest, TestFocusNotification) { 147 TEST_F(AccessibilityEventRouterViewsTest, TestFocusNotification) {
125 const char kButton1ASCII[] = "Button1"; 148 const char kButton1ASCII[] = "Button1";
126 const char kButton2ASCII[] = "Button2"; 149 const char kButton2ASCII[] = "Button2";
127 const char kButton3ASCII[] = "Button3"; 150 const char kButton3ASCII[] = "Button3";
128 const char kButton3NewASCII[] = "Button3New"; 151 const char kButton3NewASCII[] = "Button3New";
129 152
130 // Create a contents view with 3 buttons. 153 // Create a contents view with 3 buttons.
131 views::View* contents = new views::View(); 154 views::View* contents = new views::View();
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 EXPECT_EQ(kButton3NewASCII, last_control_name_); 200 EXPECT_EQ(kButton3NewASCII, last_control_name_);
178 201
179 // Advance to button 1 and check the notification. 202 // Advance to button 1 and check the notification.
180 focus_manager->AdvanceFocus(false); 203 focus_manager->AdvanceFocus(false);
181 EXPECT_EQ(3, focus_event_count_); 204 EXPECT_EQ(3, focus_event_count_);
182 EXPECT_EQ(kButton1ASCII, last_control_name_); 205 EXPECT_EQ(kButton1ASCII, last_control_name_);
183 206
184 window->CloseNow(); 207 window->CloseNow();
185 } 208 }
186 209
210 TEST_F(AccessibilityEventRouterViewsTest, TestToolbarContext) {
211 const char kToolbarNameASCII[] = "MyToolbar";
212 const char kButtonNameASCII[] = "MyButton";
213
214 // Create a toolbar with a button.
215 views::View* contents = new ViewWithNameAndRole(
216 ASCIIToUTF16(kToolbarNameASCII),
217 ui::AccessibilityTypes::ROLE_TOOLBAR);
218 views::NativeTextButton* button = new views::NativeTextButton(
219 NULL, ASCIIToUTF16(kButtonNameASCII));
220 contents->AddChildView(button);
221
222 // Put the view in a window.
223 views::Widget* window = CreateWindowWithContents(contents);
224
225 // Start listening to ACCESSIBILITY_CONTROL_FOCUSED notifications.
226 content::NotificationRegistrar registrar;
227 registrar.Add(this,
228 chrome::NOTIFICATION_ACCESSIBILITY_CONTROL_FOCUSED,
229 content::NotificationService::AllSources());
230
231 // Switch on accessibility event notifications.
232 ExtensionAccessibilityEventRouter* accessibility_event_router =
233 ExtensionAccessibilityEventRouter::GetInstance();
234 accessibility_event_router->SetAccessibilityEnabled(true);
235
236 // Create a profile and associate it with this window.
237 TestingProfile profile;
238 window->SetNativeWindowProperty(Profile::kProfileKey, &profile);
239
240 // Set focus to the button.
241 focus_event_count_ = 0;
242 button->RequestFocus();
243
244 // Test that we got the event with the expected name and context.
245 EXPECT_EQ(1, focus_event_count_);
246 EXPECT_EQ(kButtonNameASCII, last_control_name_);
247 EXPECT_EQ(kToolbarNameASCII, last_control_context_);
248
249 window->CloseNow();
250 }
251
252 TEST_F(AccessibilityEventRouterViewsTest, TestAlertContext) {
253 const char kAlertTextASCII[] = "MyAlertText";
254 const char kButtonNameASCII[] = "MyButton";
255
256 // Create an alert with static text and a button, similar to an infobar.
257 views::View* contents = new ViewWithNameAndRole(
258 string16(),
259 ui::AccessibilityTypes::ROLE_ALERT);
260 views::Label* label = new views::Label(ASCIIToUTF16(kAlertTextASCII));
261 contents->AddChildView(label);
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(kAlertTextASCII, last_control_context_);
292
293 window->CloseNow();
294 }
295
187 #endif // defined(TOOLKIT_VIEWS) 296 #endif // defined(TOOLKIT_VIEWS)
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/accessibility_event_router_views.cc ('k') | chrome/common/extensions/api/extension_api.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698