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

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

Issue 6312160: Map Views to Profiles directly from their window, eliminating the need... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 10 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) 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/extensions/extension_accessibility_api.h" 10 #include "chrome/browser/extensions/extension_accessibility_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/browser/ui/views/accessible_view_helper.h"
13 #include "chrome/common/notification_registrar.h" 12 #include "chrome/common/notification_registrar.h"
14 #include "chrome/common/notification_service.h" 13 #include "chrome/common/notification_service.h"
15 #include "chrome/test/testing_profile.h" 14 #include "chrome/test/testing_profile.h"
16 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
17 #include "views/controls/button/native_button.h" 16 #include "views/controls/button/native_button.h"
18 #include "views/layout/grid_layout.h" 17 #include "views/layout/grid_layout.h"
19 #include "views/views_delegate.h" 18 #include "views/views_delegate.h"
20 #include "views/widget/root_view.h" 19 #include "views/widget/root_view.h"
20 #include "views/widget/widget.h"
21 #include "views/window/window.h" 21 #include "views/window/window.h"
22 22 #include "views/window/window_delegate.h"
23 #if defined(OS_WIN)
24 #include "views/widget/widget_win.h"
25 #elif defined(OS_LINUX)
26 #include "views/widget/widget_gtk.h"
27 #endif
28 23
29 #if defined(TOOLKIT_VIEWS) 24 #if defined(TOOLKIT_VIEWS)
30 25
31 class AccessibilityViewsDelegate : public views::ViewsDelegate { 26 class AccessibilityViewsDelegate : public views::ViewsDelegate {
32 public: 27 public:
33 AccessibilityViewsDelegate() {} 28 AccessibilityViewsDelegate() {}
34 virtual ~AccessibilityViewsDelegate() {} 29 virtual ~AccessibilityViewsDelegate() {}
35 30
36 // Overridden from views::ViewsDelegate: 31 // Overridden from views::ViewsDelegate:
37 virtual ui::Clipboard* GetClipboard() const { return NULL; } 32 virtual ui::Clipboard* GetClipboard() const { return NULL; }
(...skipping 21 matching lines...) Expand all
59 virtual HICON GetDefaultWindowIcon() const { 54 virtual HICON GetDefaultWindowIcon() const {
60 return NULL; 55 return NULL;
61 } 56 }
62 #endif 57 #endif
63 virtual void AddRef() {} 58 virtual void AddRef() {}
64 virtual void ReleaseRef() {} 59 virtual void ReleaseRef() {}
65 60
66 DISALLOW_COPY_AND_ASSIGN(AccessibilityViewsDelegate); 61 DISALLOW_COPY_AND_ASSIGN(AccessibilityViewsDelegate);
67 }; 62 };
68 63
64 class AccessibilityWindowDelegate : public views::WindowDelegate {
65 public:
66 explicit AccessibilityWindowDelegate(views::View* contents)
67 : contents_(contents) { }
68
69 virtual void DeleteDelegate() { delete this; }
70
71 virtual views::View* GetContentsView() { return contents_; }
72
73 private:
74 views::View* contents_;
75 };
76
69 class AccessibilityEventRouterViewsTest 77 class AccessibilityEventRouterViewsTest
70 : public testing::Test, 78 : public testing::Test,
71 public NotificationObserver { 79 public NotificationObserver {
72 public: 80 public:
73 virtual void SetUp() { 81 virtual void SetUp() {
74 views::ViewsDelegate::views_delegate = new AccessibilityViewsDelegate(); 82 views::ViewsDelegate::views_delegate = new AccessibilityViewsDelegate();
83 window_delegate_ = NULL;
75 } 84 }
76 85
77 virtual void TearDown() { 86 virtual void TearDown() {
78 delete views::ViewsDelegate::views_delegate; 87 delete views::ViewsDelegate::views_delegate;
79 views::ViewsDelegate::views_delegate = NULL; 88 views::ViewsDelegate::views_delegate = NULL;
89 if (window_delegate_)
90 delete window_delegate_;
80 } 91 }
81 92
82 views::Widget* CreateWidget() { 93 views::Window* CreateWindowWithContents(views::View* contents) {
83 #if defined(OS_WIN) 94 window_delegate_ = new AccessibilityWindowDelegate(contents);
84 return new views::WidgetWin(); 95 return views::Window::CreateChromeWindow(
85 #elif defined(OS_LINUX) 96 NULL, gfx::Rect(0, 0, 500, 500), window_delegate_);
86 return new views::WidgetGtk(views::WidgetGtk::TYPE_WINDOW);
87 #endif
88 } 97 }
89 98
90 protected: 99 protected:
91 // Implement NotificationObserver::Observe and store information about a 100 // Implement NotificationObserver::Observe and store information about a
92 // ACCESSIBILITY_CONTROL_FOCUSED event. 101 // ACCESSIBILITY_CONTROL_FOCUSED event.
93 virtual void Observe(NotificationType type, 102 virtual void Observe(NotificationType type,
94 const NotificationSource& source, 103 const NotificationSource& source,
95 const NotificationDetails& details) { 104 const NotificationDetails& details) {
96 ASSERT_EQ(type.value, NotificationType::ACCESSIBILITY_CONTROL_FOCUSED); 105 ASSERT_EQ(type.value, NotificationType::ACCESSIBILITY_CONTROL_FOCUSED);
97 const AccessibilityControlInfo* info = 106 const AccessibilityControlInfo* info =
98 Details<const AccessibilityControlInfo>(details).ptr(); 107 Details<const AccessibilityControlInfo>(details).ptr();
99 focus_event_count_++; 108 focus_event_count_++;
100 last_control_name_ = info->name(); 109 last_control_name_ = info->name();
101 } 110 }
102 111
103 MessageLoopForUI message_loop_; 112 MessageLoopForUI message_loop_;
104 int focus_event_count_; 113 int focus_event_count_;
105 std::string last_control_name_; 114 std::string last_control_name_;
115 AccessibilityWindowDelegate* window_delegate_;
106 }; 116 };
107 117
108 TEST_F(AccessibilityEventRouterViewsTest, TestFocusNotification) { 118 TEST_F(AccessibilityEventRouterViewsTest, TestFocusNotification) {
109 const char kButton1ASCII[] = "Button1"; 119 const char kButton1ASCII[] = "Button1";
110 const char kButton2ASCII[] = "Button2"; 120 const char kButton2ASCII[] = "Button2";
111 const char kButton3ASCII[] = "Button3"; 121 const char kButton3ASCII[] = "Button3";
112 const char kButton3NewASCII[] = "Button3"; 122 const char kButton3NewASCII[] = "Button3New";
113 123
114 // Create a window and layout. 124 // Create a contents view with 3 buttons.
115 views::Widget* window = CreateWidget(); 125 views::View* contents = new views::View();
116 window->Init(NULL, gfx::Rect(0, 0, 100, 100));
117 views::RootView* root_view = window->GetRootView();
118 views::GridLayout* layout = new views::GridLayout(root_view);
119 root_view->SetLayoutManager(layout);
120 views::ColumnSet* column_set = layout->AddColumnSet(0);
121 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
122 views::GridLayout::USE_PREF, 0, 0);
123
124 // Add 3 buttons.
125 views::NativeButton* button1 = new views::NativeButton( 126 views::NativeButton* button1 = new views::NativeButton(
126 NULL, ASCIIToWide(kButton1ASCII)); 127 NULL, ASCIIToWide(kButton1ASCII));
127 layout->StartRow(0, 0); 128 contents->AddChildView(button1);
128 layout->AddView(button1);
129 views::NativeButton* button2 = new views::NativeButton( 129 views::NativeButton* button2 = new views::NativeButton(
130 NULL, ASCIIToWide(kButton2ASCII)); 130 NULL, ASCIIToWide(kButton2ASCII));
131 layout->StartRow(0, 0); 131 contents->AddChildView(button2);
132 layout->AddView(button2);
133 views::NativeButton* button3 = new views::NativeButton( 132 views::NativeButton* button3 = new views::NativeButton(
134 NULL, ASCIIToWide(kButton3ASCII)); 133 NULL, ASCIIToWide(kButton3ASCII));
135 layout->StartRow(0, 0); 134 contents->AddChildView(button3);
136 layout->AddView(button3); 135
136 // Put the view in a window.
137 views::Window* window = CreateWindowWithContents(contents);
137 138
138 // Set focus to the first button initially. 139 // Set focus to the first button initially.
139 button1->RequestFocus(); 140 button1->RequestFocus();
140 141
141 // Start listening to ACCESSIBILITY_CONTROL_FOCUSED notifications. 142 // Start listening to ACCESSIBILITY_CONTROL_FOCUSED notifications.
142 NotificationRegistrar registrar; 143 NotificationRegistrar registrar;
143 registrar.Add(this, 144 registrar.Add(this,
144 NotificationType::ACCESSIBILITY_CONTROL_FOCUSED, 145 NotificationType::ACCESSIBILITY_CONTROL_FOCUSED,
145 NotificationService::AllSources()); 146 NotificationService::AllSources());
146 147
147 // Switch on accessibility event notifications. 148 // Switch on accessibility event notifications.
148 TestingProfile profile;
149 ExtensionAccessibilityEventRouter* accessibility_event_router = 149 ExtensionAccessibilityEventRouter* accessibility_event_router =
150 ExtensionAccessibilityEventRouter::GetInstance(); 150 ExtensionAccessibilityEventRouter::GetInstance();
151 accessibility_event_router->SetAccessibilityEnabled(true); 151 accessibility_event_router->SetAccessibilityEnabled(true);
152 152
153 // Create an AccessibleViewHelper for this window, which will send 153 // Create a profile and associate it with this window.
154 // accessibility notifications for all events that happen in child views. 154 TestingProfile profile;
155 AccessibleViewHelper accessible_view_helper(root_view, &profile); 155 window->SetNativeWindowProperty(
156 accessible_view_helper.SetViewName(button3, std::string(kButton3NewASCII)); 156 Profile::kProfileKey, &profile);
157
158 // Change the accessible name of button3.
159 button3->SetAccessibleName(ASCIIToUTF16(kButton3NewASCII));
157 160
158 // Advance focus to the next button and test that we got the 161 // Advance focus to the next button and test that we got the
159 // expected notification with the name of button 2. 162 // expected notification with the name of button 2.
160 views::FocusManager* focus_manager = window->GetFocusManager(); 163 views::FocusManager* focus_manager = contents->GetWidget()->GetFocusManager();
161 focus_event_count_ = 0; 164 focus_event_count_ = 0;
162 focus_manager->AdvanceFocus(false); 165 focus_manager->AdvanceFocus(false);
163 EXPECT_EQ(1, focus_event_count_); 166 EXPECT_EQ(1, focus_event_count_);
164 EXPECT_EQ(kButton2ASCII, last_control_name_); 167 EXPECT_EQ(kButton2ASCII, last_control_name_);
165 168
166 // Advance to button 3. Expect the new accessible name we assigned. 169 // Advance to button 3. Expect the new accessible name we assigned.
167 focus_manager->AdvanceFocus(false); 170 focus_manager->AdvanceFocus(false);
168 EXPECT_EQ(2, focus_event_count_); 171 EXPECT_EQ(2, focus_event_count_);
169 EXPECT_EQ(kButton3NewASCII, last_control_name_); 172 EXPECT_EQ(kButton3NewASCII, last_control_name_);
170 173
171 // Advance to button 1 and check the notification. 174 // Advance to button 1 and check the notification.
172 focus_manager->AdvanceFocus(false); 175 focus_manager->AdvanceFocus(false);
173 EXPECT_EQ(3, focus_event_count_); 176 EXPECT_EQ(3, focus_event_count_);
174 EXPECT_EQ(kButton1ASCII, last_control_name_); 177 EXPECT_EQ(kButton1ASCII, last_control_name_);
175 } 178 }
176 179
177 #endif // defined(TOOLKIT_VIEWS) 180 #endif // defined(TOOLKIT_VIEWS)
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/accessibility_event_router_views.cc ('k') | chrome/browser/ui/views/accessible_view_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698