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.
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"
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
{
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
40 void WaitForNextEvent(DWORD
* out_event
,
44 std::string
* out_name
);
47 void OnWinEventHook(HWINEVENTHOOK handle
,
55 static void CALLBACK
WinEventHookThunk(
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
);
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.";
87 win_event_hook_handle_
= SetWinEventHook(
91 &WinAccessibilityEventMonitor::WinEventHookThunk
,
92 GetCurrentProcessId(),
93 0, // Hook all threads
94 WINEVENT_OUTOFCONTEXT
);
97 WinAccessibilityEventMonitor::~WinAccessibilityEventMonitor() {
98 UnhookWinEvent(win_event_hook_handle_
);
102 void WinAccessibilityEventMonitor::WaitForNextEvent(
107 std::string
* out_name
) {
108 if (event_queue_
.empty()) {
109 loop_runner_
= new content::MessageLoopRunner();
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());
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());
137 base::win::ScopedBstr name_bstr
;
138 HRESULT hr
= acc_obj
->get_accName(child_variant
, name_bstr
.Receive());
140 *out_name
= base::UTF16ToUTF8(base::string16(name_bstr
));
145 void WinAccessibilityEventMonitor::OnWinEventHook(
146 HWINEVENTHOOK handle
,
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();
164 void CALLBACK
WinAccessibilityEventMonitor::WinEventHookThunk(
165 HWINEVENTHOOK handle
,
173 instance_
->OnWinEventHook(handle
, event
, hwnd
, obj_id
, child_id
,
174 event_thread
, event_time
);
178 class NavigationAccessibilityTest
: public InProcessBrowserTest
{
180 NavigationAccessibilityTest() {}
181 ~NavigationAccessibilityTest() override
{}
183 void SendKeyPress(ui::KeyboardCode key
) {
184 gfx::NativeWindow native_window
= browser()->window()->GetNativeWindow();
185 ASSERT_NO_FATAL_FAILURE(
187 ui_test_utils::SendKeyPressToWindowSync(
188 native_window
, key
, false, false, false, false)));
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()),
221 WinAccessibilityEventMonitor
monitor(EVENT_OBJECT_FOCUS
, EVENT_OBJECT_FOCUS
);
222 SendKeyPress(ui::VKEY_RETURN
);
230 monitor
.WaitForNextEvent(&event
, &hwnd
, &role
, &state
, &name
);
232 LOG(INFO
) << "Got event: "
233 << " event=" << event
236 << " state=" << state
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
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.";