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