Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / views / accessibility / navigation_accessibility_uitest_win.cc
blobb9e839906c80c3c70de0df12711c07a4001413e9
1 // Copyright (c) 2015 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.
5 #include <oleacc.h>
7 #include "base/strings/string_util.h"
8 #include "base/win/scoped_bstr.h"
9 #include "base/win/scoped_com_initializer.h"
10 #include "base/win/scoped_comptr.h"
11 #include "base/win/scoped_variant.h"
12 #include "chrome/app/chrome_command_ids.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_commands.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #include "chrome/browser/ui/views/frame/browser_view.h"
17 #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h"
18 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "chrome/test/base/interactive_test_utils.h"
21 #include "chrome/test/base/ui_test_utils.h"
22 #include "components/omnibox/browser/omnibox_view.h"
23 #include "content/public/browser/browser_accessibility_state.h"
24 #include "net/dns/mock_host_resolver.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "ui/base/test/ui_controls.h"
27 #include "url/gurl.h"
29 // We could move this into a utility file in the future if it ends up
30 // being useful to other tests.
31 class WinAccessibilityEventMonitor {
32 public:
33 WinAccessibilityEventMonitor(UINT event_min, UINT event_max);
34 ~WinAccessibilityEventMonitor();
36 // Blocks until the next event is received. When it's received, it
37 // queries accessibility information about the object that fired the
38 // event and populates the event, hwnd, role, state, and name in the
39 // passed arguments.
40 void WaitForNextEvent(DWORD* out_event,
41 HWND* out_hwnd,
42 UINT* out_role,
43 UINT* out_state,
44 std::string* out_name);
46 private:
47 void OnWinEventHook(HWINEVENTHOOK handle,
48 DWORD event,
49 HWND hwnd,
50 LONG obj_id,
51 LONG child_id,
52 DWORD event_thread,
53 DWORD event_time);
55 static void CALLBACK WinEventHookThunk(
56 HWINEVENTHOOK handle,
57 DWORD event,
58 HWND hwnd,
59 LONG obj_id,
60 LONG child_id,
61 DWORD event_thread,
62 DWORD event_time);
64 struct EventInfo {
65 DWORD event;
66 HWND hwnd;
67 LONG obj_id;
68 LONG child_id;
71 std::deque<EventInfo> event_queue_;
72 scoped_refptr<content::MessageLoopRunner> loop_runner_;
73 HWINEVENTHOOK win_event_hook_handle_;
74 static WinAccessibilityEventMonitor* instance_;
76 DISALLOW_COPY_AND_ASSIGN(WinAccessibilityEventMonitor);
79 // static
80 WinAccessibilityEventMonitor* WinAccessibilityEventMonitor::instance_ = NULL;
82 WinAccessibilityEventMonitor::WinAccessibilityEventMonitor(
83 UINT event_min, UINT event_max) {
84 CHECK(!instance_) << "There can be only one instance of"
85 << " WinAccessibilityEventMonitor at a time.";
86 instance_ = this;
87 win_event_hook_handle_ = SetWinEventHook(
88 event_min,
89 event_max,
90 NULL,
91 &WinAccessibilityEventMonitor::WinEventHookThunk,
92 GetCurrentProcessId(),
93 0, // Hook all threads
94 WINEVENT_OUTOFCONTEXT);
97 WinAccessibilityEventMonitor::~WinAccessibilityEventMonitor() {
98 UnhookWinEvent(win_event_hook_handle_);
99 instance_ = NULL;
102 void WinAccessibilityEventMonitor::WaitForNextEvent(
103 DWORD* out_event,
104 HWND* out_hwnd,
105 UINT* out_role,
106 UINT* out_state,
107 std::string* out_name) {
108 if (event_queue_.empty()) {
109 loop_runner_ = new content::MessageLoopRunner();
110 loop_runner_->Run();
111 loop_runner_ = NULL;
113 EventInfo event_info = event_queue_.front();
114 event_queue_.pop_front();
116 *out_event = event_info.event;
117 *out_hwnd = event_info.hwnd;
119 base::win::ScopedComPtr<IAccessible> acc_obj;
120 base::win::ScopedVariant child_variant;
121 CHECK(S_OK == AccessibleObjectFromEvent(
122 event_info.hwnd, event_info.obj_id, event_info.child_id,
123 acc_obj.Receive(), child_variant.Receive()));
125 base::win::ScopedVariant role_variant;
126 if (S_OK == acc_obj->get_accRole(child_variant, role_variant.Receive()))
127 *out_role = V_I4(role_variant.ptr());
128 else
129 *out_role = 0;
131 base::win::ScopedVariant state_variant;
132 if (S_OK == acc_obj->get_accState(child_variant, state_variant.Receive()))
133 *out_state = V_I4(state_variant.ptr());
134 else
135 *out_state = 0;
137 base::win::ScopedBstr name_bstr;
138 HRESULT hr = acc_obj->get_accName(child_variant, name_bstr.Receive());
139 if (S_OK == hr)
140 *out_name = base::UTF16ToUTF8(base::string16(name_bstr));
141 else
142 *out_name = "";
145 void WinAccessibilityEventMonitor::OnWinEventHook(
146 HWINEVENTHOOK handle,
147 DWORD event,
148 HWND hwnd,
149 LONG obj_id,
150 LONG child_id,
151 DWORD event_thread,
152 DWORD event_time) {
153 EventInfo event_info;
154 event_info.event = event;
155 event_info.hwnd = hwnd;
156 event_info.obj_id = obj_id;
157 event_info.child_id = child_id;
158 event_queue_.push_back(event_info);
159 if (loop_runner_.get())
160 loop_runner_->Quit();
163 // static
164 void CALLBACK WinAccessibilityEventMonitor::WinEventHookThunk(
165 HWINEVENTHOOK handle,
166 DWORD event,
167 HWND hwnd,
168 LONG obj_id,
169 LONG child_id,
170 DWORD event_thread,
171 DWORD event_time) {
172 if (instance_) {
173 instance_->OnWinEventHook(handle, event, hwnd, obj_id, child_id,
174 event_thread, event_time);
178 class NavigationAccessibilityTest : public InProcessBrowserTest {
179 protected:
180 NavigationAccessibilityTest() {}
181 ~NavigationAccessibilityTest() override {}
183 void SendKeyPress(ui::KeyboardCode key) {
184 gfx::NativeWindow native_window = browser()->window()->GetNativeWindow();
185 ASSERT_NO_FATAL_FAILURE(
186 ASSERT_TRUE(
187 ui_test_utils::SendKeyPressToWindowSync(
188 native_window, key, false, false, false, false)));
191 private:
192 base::win::ScopedCOMInitializer com_initializer_;
194 DISALLOW_COPY_AND_ASSIGN(NavigationAccessibilityTest);
197 // Tests that when focus is in the omnibox and the user types a url and
198 // presses enter, no focus events are sent on the old document.
199 // TODO(dmazzoni): enable this test. http://crbug.com/421116
200 IN_PROC_BROWSER_TEST_F(NavigationAccessibilityTest,
201 DISABLED_TestNavigateToNewUrl) {
202 content::BrowserAccessibilityState::GetInstance()->EnableAccessibility();
204 ui_test_utils::NavigateToURL(browser(),
205 GURL("data:text/html;charset=utf-8,"
206 "<head><title>First Page</title></head>"));
208 chrome::ExecuteCommand(browser(), IDC_FOCUS_LOCATION);
210 host_resolver()->AddRule("*", "127.0.0.1");
211 ASSERT_TRUE(test_server()->Start());
212 GURL main_url(test_server()->GetURL("files/english_page.html"));
214 OmniboxViewViews* omnibox_view =
215 BrowserView::GetBrowserViewForBrowser(browser())->
216 toolbar()->location_bar()->omnibox_view();
217 omnibox_view->SetUserText(base::UTF8ToUTF16(main_url.spec()),
218 base::UTF8ToUTF16(main_url.spec()),
219 false);
221 WinAccessibilityEventMonitor monitor(EVENT_OBJECT_FOCUS, EVENT_OBJECT_FOCUS);
222 SendKeyPress(ui::VKEY_RETURN);
224 for (;;) {
225 DWORD event;
226 HWND hwnd;
227 UINT role;
228 UINT state;
229 std::string name;
230 monitor.WaitForNextEvent(&event, &hwnd, &role, &state, &name);
232 LOG(INFO) << "Got event: "
233 << " event=" << event
234 << " hwnd=" << hwnd
235 << " role=" << role
236 << " state=" << state
237 << " name=" << name;
239 // We should get only focus events.
240 EXPECT_EQ(EVENT_OBJECT_FOCUS, event);
242 // We should get only focus events on document objects. (On a page with
243 // JavaScript or autofocus, additional focus events would be expected.)
244 EXPECT_EQ(ROLE_SYSTEM_DOCUMENT, role);
246 // We shouldn't get any events on the first page because from the time
247 // we start monitoring, the user has already initiated a load to the
248 // second page.
249 EXPECT_NE("First Page", name);
251 // Finish when we get an event on the second page.
252 if (name == "This page is in English") {
253 LOG(INFO) << "Got event on second page, finishing test.";
254 break;