1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/frame/app_menu_button_observer.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/toolbar/app_menu.h"
#include "chrome/browser/ui/views/toolbar/browser_app_menu_button.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_navigation_observer.h"
#include "ui/base/test/ui_controls.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/style/platform_style.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
namespace {
// An async version of SendKeyPressSync since we don't get notified when a
// menu is showing.
void SendKeyPress(Browser* browser, ui::KeyboardCode key) {
ASSERT_TRUE(ui_controls::SendKeyPress(browser->window()->GetNativeWindow(),
key, false, false, false, false));
}
// Helper class that waits until the focus has changed to a view other
// than the one with the provided view id.
class ViewFocusChangeWaiter : public views::FocusChangeListener {
public:
ViewFocusChangeWaiter(views::FocusManager* focus_manager,
int previous_view_id)
: focus_manager_(focus_manager), previous_view_id_(previous_view_id) {
focus_manager_->AddFocusChangeListener(this);
// Call the focus change notification once in case the focus has
// already changed.
OnWillChangeFocus(nullptr, focus_manager_->GetFocusedView());
}
ViewFocusChangeWaiter(const ViewFocusChangeWaiter&) = delete;
ViewFocusChangeWaiter& operator=(const ViewFocusChangeWaiter&) = delete;
~ViewFocusChangeWaiter() override {
focus_manager_->RemoveFocusChangeListener(this);
}
void Wait() { loop_.Run(); }
private:
// views::FocusChangeListener:
void OnDidChangeFocus(views::View* focused_before,
views::View* focused_now) override {
if (focused_now && focused_now->GetID() != previous_view_id_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, loop_.QuitWhenIdleClosure());
}
}
raw_ptr<views::FocusManager> focus_manager_;
base::RunLoop loop_;
int previous_view_id_;
base::WeakPtrFactory<ViewFocusChangeWaiter> weak_factory_{this};
};
class SendKeysMenuListener : public AppMenuButtonObserver {
public:
SendKeysMenuListener(AppMenuButton* app_menu_button,
Browser* browser,
bool test_dismiss_menu)
: browser_(browser),
menu_open_count_(0),
test_dismiss_menu_(test_dismiss_menu) {
observation_.Observe(app_menu_button);
}
SendKeysMenuListener(const SendKeysMenuListener&) = delete;
SendKeysMenuListener& operator=(const SendKeysMenuListener&) = delete;
~SendKeysMenuListener() override = default;
void Wait() { loop_.Run(); }
// AppMenuButtonObserver:
void AppMenuShown() override {
menu_open_count_++;
if (test_dismiss_menu_) {
SendKeyPress(browser_, ui::VKEY_ESCAPE);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, loop_.QuitWhenIdleClosure(), base::Milliseconds(200));
} else {
DCHECK(observation_.IsObserving());
observation_.Reset();
if (!views::PlatformStyle::kAutoSelectFirstMenuItemFromKeyboard) {
// Press DOWN to select the first item, then RETURN to select it. Only
// needed on platforms where we don't automatically select the first
// item on menu open.
SendKeyPress(browser_, ui::VKEY_DOWN);
}
SendKeyPress(browser_, ui::VKEY_RETURN);
}
}
int menu_open_count() const { return menu_open_count_; }
private:
raw_ptr<Browser> browser_;
// Keeps track of the number of times the menu was opened.
int menu_open_count_;
// If this is set then on receiving a notification that the menu was opened
// we dismiss it by sending the ESC key.
bool test_dismiss_menu_;
// This used to use content::RunMessageLoop() which used a nestable loop. We
// tried removing kNestableTasksAllowed but that failed on trybots.
base::RunLoop loop_{base::RunLoop::Type::kNestableTasksAllowed};
base::ScopedObservation<AppMenuButton, AppMenuButtonObserver> observation_{
this};
};
class KeyboardAccessTest : public InProcessBrowserTest {
public:
KeyboardAccessTest() = default;
KeyboardAccessTest(const KeyboardAccessTest&) = delete;
KeyboardAccessTest& operator=(const KeyboardAccessTest&) = delete;
// Use the keyboard to select "New Tab" from the app menu.
// This test depends on the fact that there is one menu and that
// New Tab is the first item in the menu. If the menus change,
// this test will need to be changed to reflect that.
//
// If alternate_key_sequence is true, use "Alt" instead of "F10" to
// open the menu bar, and "Down" instead of "Enter" to open a menu.
// If focus_omnibox is true then the test on startup sets focus to the
// omnibox.
void TestMenuKeyboardAccess(bool alternate_key_sequence,
bool shift,
bool focus_omnibox);
int GetFocusedViewID() {
gfx::NativeWindow window = browser()->window()->GetNativeWindow();
views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
const views::FocusManager* focus_manager = widget->GetFocusManager();
const views::View* focused_view = focus_manager->GetFocusedView();
return focused_view ? focused_view->GetID() : -1;
}
void WaitForFocusedViewIDToChange(int original_view_id) {
if (GetFocusedViewID() != original_view_id) {
return;
}
gfx::NativeWindow window = browser()->window()->GetNativeWindow();
views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
views::FocusManager* focus_manager = widget->GetFocusManager();
ViewFocusChangeWaiter waiter(focus_manager, original_view_id);
waiter.Wait();
}
#if BUILDFLAG(IS_WIN)
// Opens the system menu on Windows with the Alt Space combination and selects
// the New Tab option from the menu.
void TestSystemMenuWithKeyboard();
void TestSystemMenuReopenClosedTabWithKeyboard();
#endif
// Uses the keyboard to select the app menu i.e. with the F10 key.
// It verifies that the menu when dismissed by sending the ESC key it does
// not display twice.
void TestMenuKeyboardAccessAndDismiss();
};
void KeyboardAccessTest::TestMenuKeyboardAccess(bool alternate_key_sequence,
bool shift,
bool focus_omnibox) {
// Navigate to a page in the first tab, which makes sure that focus is
// set to the browser window.
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL("chrome://version/")));
// The initial tab index should be 0.
ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
// Get the focused view ID, then press a key to activate the
// page menu, then wait until the focused view changes.
int original_view_id = GetFocusedViewID();
ui_test_utils::TabAddedWaiter tab_add(browser());
BrowserView* browser_view = static_cast<BrowserView*>(browser()->window());
SendKeysMenuListener menu_listener(
browser_view->toolbar_button_provider()->GetAppMenuButton(), browser(),
false);
if (focus_omnibox) {
browser()->window()->GetLocationBar()->FocusLocation(false);
}
#if BUILDFLAG(IS_CHROMEOS)
// Chrome OS doesn't have a way to just focus the app menu, so we use Alt+F to
// bring up the menu.
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_F, false,
shift, true, false));
#else
ui::KeyboardCode menu_key =
alternate_key_sequence ? ui::VKEY_MENU : ui::VKEY_F10;
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), menu_key, false, shift,
false, false));
#endif
if (shift) {
// Verify Chrome does not move the view focus. We should not move the view
// focus when typing a menu key with modifier keys, such as shift keys or
// control keys.
int new_view_id = GetFocusedViewID();
ASSERT_EQ(original_view_id, new_view_id);
return;
}
WaitForFocusedViewIDToChange(original_view_id);
// See above comment. Since we already brought up the menu, no need to do this
// on ChromeOS.
#if !BUILDFLAG(IS_CHROMEOS)
if (alternate_key_sequence) {
SendKeyPress(browser(), ui::VKEY_DOWN);
} else {
SendKeyPress(browser(), ui::VKEY_RETURN);
}
#endif
// Wait for the new tab to appear.
tab_add.Wait();
// Make sure that the new tab index is 1.
ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
}
#if BUILDFLAG(IS_WIN)
// This CBT hook is set for the duration of the TestSystemMenuWithKeyboard test
LRESULT CALLBACK SystemMenuTestCBTHook(int n_code,
WPARAM w_param,
LPARAM l_param) {
// Look for the system menu window getting created or becoming visible and
// then select the New Tab option from the menu.
if (n_code == HCBT_ACTIVATE || n_code == HCBT_CREATEWND) {
wchar_t class_name[MAX_PATH] = {};
GetClassName(reinterpret_cast<HWND>(w_param), class_name,
std::size(class_name));
if (base::EqualsCaseInsensitiveASCII(class_name, "#32768")) {
// Select the New Tab option and then send the enter key to execute it.
::PostMessage(reinterpret_cast<HWND>(w_param), WM_CHAR, 'T', 0);
::PostMessage(reinterpret_cast<HWND>(w_param), WM_KEYDOWN, VK_RETURN, 0);
::PostMessage(reinterpret_cast<HWND>(w_param), WM_KEYUP, VK_RETURN, 0);
}
}
return ::CallNextHookEx(0, n_code, w_param, l_param);
}
void KeyboardAccessTest::TestSystemMenuWithKeyboard() {
// Navigate to a page in the first tab, which makes sure that focus is
// set to the browser window.
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL("chrome://version/")));
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
ui_test_utils::TabAddedWaiter tab_add(browser());
// Sending the Alt space keys to the browser will bring up the system menu
// which runs a model loop. We set a CBT hook to look for the menu and send
// keystrokes to it.
HHOOK cbt_hook = ::SetWindowsHookEx(WH_CBT, SystemMenuTestCBTHook, NULL,
::GetCurrentThreadId());
ASSERT_TRUE(cbt_hook);
bool ret = ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_SPACE, false,
false, true, false);
EXPECT_TRUE(ret);
if (ret) {
// Wait for the new tab to appear.
tab_add.Wait();
// Make sure that the new tab index is 1.
EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
}
::UnhookWindowsHookEx(cbt_hook);
}
// This CBT hook is set for the duration of the
// TestSystemMenuReopenClosedTabWithKeyboard test
LRESULT CALLBACK SystemMenuReopenClosedTabTestCBTHook(int n_code,
WPARAM w_param,
LPARAM l_param) {
// Look for the system menu window getting created or becoming visible and
// then select the New Tab option from the menu.
if (n_code == HCBT_ACTIVATE || n_code == HCBT_CREATEWND) {
wchar_t class_name[MAX_PATH] = {};
GetClassName(reinterpret_cast<HWND>(w_param), class_name,
std::size(class_name));
if (base::EqualsCaseInsensitiveASCII(class_name, "#32768")) {
// Send 'E' for the Reopen closed tab option.
::PostMessage(reinterpret_cast<HWND>(w_param), WM_CHAR, 'E', 0);
}
}
return ::CallNextHookEx(0, n_code, w_param, l_param);
}
void KeyboardAccessTest::TestSystemMenuReopenClosedTabWithKeyboard() {
// Navigate to a page in the first tab, which makes sure that focus is
// set to the browser window.
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL("chrome://version/")));
ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL("chrome://version/"),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
ASSERT_EQ(1, browser()->tab_strip_model()->active_index());
content::WebContents* tab_to_close =
browser()->tab_strip_model()->GetActiveWebContents();
content::WebContentsDestroyedWatcher destroyed_watcher(tab_to_close);
browser()->tab_strip_model()->CloseSelectedTabs();
destroyed_watcher.Wait();
ASSERT_EQ(1, browser()->tab_strip_model()->count());
ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
ui_test_utils::TabAddedWaiter tab_add(browser());
// Sending the Alt space keys to the browser will bring up the system menu
// which runs a model loop. We set a CBT hook to look for the menu and send
// keystrokes to it.
HHOOK cbt_hook =
::SetWindowsHookEx(WH_CBT, SystemMenuReopenClosedTabTestCBTHook, NULL,
::GetCurrentThreadId());
ASSERT_TRUE(cbt_hook);
bool ret = ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_SPACE, false,
false, true, false);
EXPECT_TRUE(ret);
if (ret) {
// Wait for the new tab to appear.
tab_add.Wait();
// Make sure that the new tab index is 1.
EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
}
::UnhookWindowsHookEx(cbt_hook);
}
#endif
void KeyboardAccessTest::TestMenuKeyboardAccessAndDismiss() {
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL("chrome://version/")));
ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
int original_view_id = GetFocusedViewID();
BrowserView* browser_view = static_cast<BrowserView*>(browser()->window());
SendKeysMenuListener menu_listener(
browser_view->toolbar_button_provider()->GetAppMenuButton(), browser(),
true);
browser()->window()->GetLocationBar()->FocusLocation(false);
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_F10, false,
false, false, false));
WaitForFocusedViewIDToChange(original_view_id);
SendKeyPress(browser(), ui::VKEY_DOWN);
menu_listener.Wait();
ASSERT_EQ(1, menu_listener.menu_open_count());
}
// http://crbug.com/62310.
#if BUILDFLAG(IS_CHROMEOS)
#define MAYBE_TestMenuKeyboardAccess DISABLED_TestMenuKeyboardAccess
#elif BUILDFLAG(IS_MAC)
// No keyboard shortcut for the Chrome menu on Mac: http://crbug.com/823952
#define MAYBE_TestMenuKeyboardAccess DISABLED_TestMenuKeyboardAccess
#else
#define MAYBE_TestMenuKeyboardAccess TestMenuKeyboardAccess
#endif
IN_PROC_BROWSER_TEST_F(KeyboardAccessTest, MAYBE_TestMenuKeyboardAccess) {
TestMenuKeyboardAccess(false, false, false);
}
// http://crbug.com/62310.
#if BUILDFLAG(IS_CHROMEOS)
#define MAYBE_TestAltMenuKeyboardAccess DISABLED_TestAltMenuKeyboardAccess
#elif BUILDFLAG(IS_MAC)
// No keyboard shortcut for the Chrome menu on Mac: http://crbug.com/823952
#define MAYBE_TestAltMenuKeyboardAccess DISABLED_TestAltMenuKeyboardAccess
#else
#define MAYBE_TestAltMenuKeyboardAccess TestAltMenuKeyboardAccess
#endif
IN_PROC_BROWSER_TEST_F(KeyboardAccessTest, MAYBE_TestAltMenuKeyboardAccess) {
TestMenuKeyboardAccess(true, false, false);
}
// If this flakes, use http://crbug.com/62311.
#if BUILDFLAG(IS_WIN)
#define MAYBE_TestShiftAltMenuKeyboardAccess \
DISABLED_TestShiftAltMenuKeyboardAccess
#else
#define MAYBE_TestShiftAltMenuKeyboardAccess TestShiftAltMenuKeyboardAccess
#endif
IN_PROC_BROWSER_TEST_F(KeyboardAccessTest,
MAYBE_TestShiftAltMenuKeyboardAccess) {
TestMenuKeyboardAccess(true, true, false);
}
#if BUILDFLAG(IS_WIN)
IN_PROC_BROWSER_TEST_F(KeyboardAccessTest,
DISABLED_TestAltMenuKeyboardAccessFocusOmnibox) {
TestMenuKeyboardAccess(true, false, true);
}
IN_PROC_BROWSER_TEST_F(KeyboardAccessTest, TestSystemMenuWithKeyboard) {
TestSystemMenuWithKeyboard();
}
IN_PROC_BROWSER_TEST_F(KeyboardAccessTest,
TestSystemMenuReopenClosedTabWithKeyboard) {
TestSystemMenuReopenClosedTabWithKeyboard();
}
#endif
#if !BUILDFLAG(IS_WIN) && defined(USE_AURA)
IN_PROC_BROWSER_TEST_F(KeyboardAccessTest, TestMenuKeyboardOpenDismiss) {
TestMenuKeyboardAccessAndDismiss();
}
#endif
// Test that JavaScript cannot intercept reserved keyboard accelerators like
// ctrl-t to open a new tab or ctrl-f4 to close a tab.
// TODO(isherman): This test times out on ChromeOS. We should merge it with
// BrowserKeyEventsTest.ReservedAccelerators, but just disable for now.
// If this flakes, use http://crbug.com/62311.
IN_PROC_BROWSER_TEST_F(KeyboardAccessTest, ReserveKeyboardAccelerators) {
const std::string kBadPage =
"<html><script>"
"document.onkeydown = function() {"
" event.preventDefault();"
" return false;"
"}"
"</script></html>";
GURL url("data:text/html," + kBadPage);
ui_test_utils::NavigateToURLWithDisposition(
browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_TAB, true,
false, false, false));
ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
ui_test_utils::NavigateToURLWithDisposition(
browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
ASSERT_EQ(2, browser()->tab_strip_model()->active_index());
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
#if BUILDFLAG(IS_MAC)
browser(), ui::VKEY_W, false, false, false, true));
#else
browser(), ui::VKEY_W, true, false, false, false));
#endif
ASSERT_EQ(0, browser()->tab_strip_model()->active_index());
}
#if BUILDFLAG(IS_WIN) // These keys are Windows-only.
IN_PROC_BROWSER_TEST_F(KeyboardAccessTest, BackForwardKeys) {
// Navigate to create some history.
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL("chrome://version/")));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GURL("chrome://about/")));
std::u16string before_back;
ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &before_back));
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
// Navigate back.
{
content::TestNavigationObserver navigation_observer(web_contents, 1);
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
browser(), ui::VKEY_BROWSER_BACK, false, false, false, false));
navigation_observer.Wait();
std::u16string after_back;
ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &after_back));
EXPECT_NE(before_back, after_back);
}
// And then forward.
{
content::TestNavigationObserver navigation_observer(web_contents, 1);
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
browser(), ui::VKEY_BROWSER_FORWARD, false, false, false, false));
navigation_observer.Wait();
std::u16string after_forward;
ASSERT_TRUE(ui_test_utils::GetCurrentTabTitle(browser(), &after_forward));
EXPECT_EQ(before_back, after_forward);
}
}
#endif
} // namespace
|