OLD | NEW |
(Empty) | |
| 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 |
| 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/omnibox/omnibox_view.h" |
| 17 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 18 #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h" |
| 19 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
| 20 #include "chrome/test/base/in_process_browser_test.h" |
| 21 #include "chrome/test/base/interactive_test_utils.h" |
| 22 #include "chrome/test/base/ui_test_utils.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" |
| 28 |
| 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(); |
| 35 |
| 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); |
| 45 |
| 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); |
| 54 |
| 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); |
| 63 |
| 64 struct EventInfo { |
| 65 DWORD event; |
| 66 HWND hwnd; |
| 67 LONG obj_id; |
| 68 LONG child_id; |
| 69 }; |
| 70 |
| 71 std::deque<EventInfo> event_queue_; |
| 72 scoped_refptr<content::MessageLoopRunner> loop_runner_; |
| 73 HWINEVENTHOOK win_event_hook_handle_; |
| 74 static WinAccessibilityEventMonitor* instance_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(WinAccessibilityEventMonitor); |
| 77 }; |
| 78 |
| 79 // static |
| 80 WinAccessibilityEventMonitor* WinAccessibilityEventMonitor::instance_ = NULL; |
| 81 |
| 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); |
| 95 } |
| 96 |
| 97 WinAccessibilityEventMonitor::~WinAccessibilityEventMonitor() { |
| 98 UnhookWinEvent(win_event_hook_handle_); |
| 99 instance_ = NULL; |
| 100 } |
| 101 |
| 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; |
| 112 } |
| 113 EventInfo event_info = event_queue_.front(); |
| 114 event_queue_.pop_front(); |
| 115 |
| 116 *out_event = event_info.event; |
| 117 *out_hwnd = event_info.hwnd; |
| 118 |
| 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())); |
| 124 |
| 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); |
| 128 else |
| 129 *out_role = 0; |
| 130 |
| 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); |
| 134 else |
| 135 *out_state = 0; |
| 136 |
| 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 = ""; |
| 143 } |
| 144 |
| 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(); |
| 161 } |
| 162 |
| 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); |
| 175 } |
| 176 } |
| 177 |
| 178 class NavigationAccessibilityTest : public InProcessBrowserTest { |
| 179 protected: |
| 180 NavigationAccessibilityTest() {} |
| 181 virtual ~NavigationAccessibilityTest() {} |
| 182 |
| 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))); |
| 189 } |
| 190 |
| 191 private: |
| 192 base::win::ScopedCOMInitializer com_initializer_; |
| 193 |
| 194 DISALLOW_COPY_AND_ASSIGN(NavigationAccessibilityTest); |
| 195 }; |
| 196 |
| 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 IN_PROC_BROWSER_TEST_F(NavigationAccessibilityTest, TestNavigateToNewUrl) { |
| 200 content::BrowserAccessibilityState::GetInstance()->EnableAccessibility(); |
| 201 |
| 202 ui_test_utils::NavigateToURL(browser(), |
| 203 GURL("data:text/html;charset=utf-8," |
| 204 "<head><title>First Page</title></head>")); |
| 205 |
| 206 chrome::ExecuteCommand(browser(), IDC_FOCUS_LOCATION); |
| 207 |
| 208 host_resolver()->AddRule("*", "127.0.0.1"); |
| 209 ASSERT_TRUE(test_server()->Start()); |
| 210 GURL main_url(test_server()->GetURL("files/english_page.html")); |
| 211 |
| 212 OmniboxViewViews* omnibox_view = |
| 213 BrowserView::GetBrowserViewForBrowser(browser())-> |
| 214 toolbar()->location_bar()->omnibox_view(); |
| 215 omnibox_view->SetUserText(base::UTF8ToUTF16(main_url.spec()), |
| 216 base::UTF8ToUTF16(main_url.spec()), |
| 217 false); |
| 218 |
| 219 WinAccessibilityEventMonitor monitor(EVENT_OBJECT_FOCUS, EVENT_OBJECT_FOCUS); |
| 220 SendKeyPress(ui::VKEY_RETURN); |
| 221 |
| 222 for (;;) { |
| 223 DWORD event; |
| 224 HWND hwnd; |
| 225 UINT role; |
| 226 UINT state; |
| 227 std::string name; |
| 228 monitor.WaitForNextEvent(&event, &hwnd, &role, &state, &name); |
| 229 |
| 230 LOG(INFO) << "Got event: " |
| 231 << " event=" << event |
| 232 << " hwnd=" << hwnd |
| 233 << " role=" << role |
| 234 << " state=" << state |
| 235 << " name=" << name; |
| 236 |
| 237 // We should get only focus events. |
| 238 EXPECT_EQ(EVENT_OBJECT_FOCUS, event); |
| 239 |
| 240 // We should get only focus events on document objects. (On a page with |
| 241 // JavaScript or autofocus, additional focus events would be expected.) |
| 242 EXPECT_EQ(ROLE_SYSTEM_DOCUMENT, role); |
| 243 |
| 244 // We shouldn't get any events on the first page because from the time |
| 245 // we start monitoring, the user has already initiated a load to the |
| 246 // second page. |
| 247 EXPECT_NE("First Page", name); |
| 248 |
| 249 // Finish when we get an event on the second page. |
| 250 if (name == "This page is in English") { |
| 251 LOG(INFO) << "Got event on second page, finishing test."; |
| 252 break; |
| 253 } |
| 254 } |
| 255 } |
OLD | NEW |