#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include <stddef.h>
#include "base/check.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_paths.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/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/blink/public/common/switches.h"
#include "ui/events/keycodes/keyboard_codes.h"
NavigationController;
namespace {
constexpr char kTestingPage[] = …;
constexpr char kSuppressEventJS[] = …;
constexpr char kGetResultJS[] = …;
constexpr char kGetResultLengthJS[] = …;
constexpr char kGetFocusedElementJS[] = …;
constexpr char kSetFocusedElementJS[] = …;
constexpr char kGetTextBoxValueJS[] = …;
constexpr char kSetTextBoxValueJS[] = …;
constexpr char kStartTestJS[] = …;
constexpr size_t kMaxResultLength = …;
struct KeyEventTestData { … };
class TestFinishObserver : public content::WebContentsObserver { … };
class BrowserKeyEventsTest : public InProcessBrowserTest { … };
IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, DISABLED_NormalKeyEvents) { … }
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, CtrlKeyEvents) { … }
#elif BUILDFLAG(IS_MAC)
IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, CommandKeyEvents) {
static const KeyEventTestData kTestCmdF = {
ui::VKEY_F, false, false, false, true,
false, false, false, false, 2,
{ "D 91 0 false false false true",
"D 70 0 false false false true" }
};
static const KeyEventTestData kTestCmdFSuppressKeyDown = {
ui::VKEY_F, false, false, false, true,
true, false, false, false, 3,
{ "D 91 0 false false false true",
"D 70 0 false false false true",
"U 91 0 false false false true" }
};
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
GURL url = embedded_test_server()->GetURL(kTestingPage);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_NO_FATAL_FAILURE(ClickOnView(VIEW_ID_TAB_CONTAINER));
ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
int tab_index = browser()->tab_strip_model()->active_index();
EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCmdF));
EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
browser(), ui::VKEY_ESCAPE, false, false, false, false));
ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCmdFSuppressKeyDown));
ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
}
#endif
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_AccessKeys …
#else
#define MAYBE_AccessKeys …
#endif
IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, MAYBE_AccessKeys) { … }
IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, ReservedAccelerators) { … }
#if BUILDFLAG(IS_MAC)
IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, EditorKeyBindings) {
static const KeyEventTestData kTestCtrlA = {
ui::VKEY_A, true, false, false, false,
false, false, false, false, 4,
{ "D 17 0 true false false false",
"D 65 0 true false false false",
"U 65 0 true false false false",
"U 17 0 true false false false" }
};
static const KeyEventTestData kTestCtrlF = {
ui::VKEY_F, true, false, false, false,
false, false, false, false, 4,
{ "D 17 0 true false false false",
"D 70 0 true false false false",
"U 70 0 true false false false",
"U 17 0 true false false false" }
};
static const KeyEventTestData kTestCtrlK = {
ui::VKEY_K, true, false, false, false,
false, false, false, false, 4,
{ "D 17 0 true false false false",
"D 75 0 true false false false",
"U 75 0 true false false false",
"U 17 0 true false false false" }
};
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
GURL url = embedded_test_server()->GetURL(kTestingPage);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_NO_FATAL_FAILURE(ClickOnView(VIEW_ID_TAB_CONTAINER));
ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
int tab_index = browser()->tab_strip_model()->active_index();
ASSERT_NO_FATAL_FAILURE(SetFocusedElement(tab_index, "A"));
ASSERT_NO_FATAL_FAILURE(SetTextBoxValue(tab_index, "A", "Hello"));
EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlA));
EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlF));
EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlK));
EXPECT_NO_FATAL_FAILURE(CheckTextBoxValue(tab_index, "A", "H"));
}
#endif
IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, PageUpDownKeys) { … }
#if BUILDFLAG(IS_WIN)
IN_PROC_BROWSER_TEST_F(BrowserKeyEventsTest, FocusMenuBarByAltKey) {
static const KeyEventTestData kTestAltKey = {
ui::VKEY_MENU, false, false, false, false,
false, false, false, false, 2,
{ "D 18 0 false false true false",
"U 18 0 false false true false" }
};
static const KeyEventTestData kTestAltKeySuppress = {
ui::VKEY_MENU, false, false, false, false,
true, false, false, false, 2,
{ "D 18 0 false false true false",
"U 18 0 false false true false" }
};
static const KeyEventTestData kTestCtrlAltKey = {
ui::VKEY_MENU, true, false, false, false,
false, false, false, false, 4,
{ "D 17 0 true false false false",
"D 18 0 true false true false",
"U 18 0 true false true false",
"U 17 0 true false false false" }
};
ASSERT_TRUE(embedded_test_server()->Start());
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
GURL url = embedded_test_server()->GetURL(kTestingPage);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_NO_FATAL_FAILURE(ClickOnView(VIEW_ID_TAB_CONTAINER));
ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
int tab_index = browser()->tab_strip_model()->active_index();
EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestAltKey));
EXPECT_TRUE(IsViewFocused(VIEW_ID_APP_MENU));
ASSERT_NO_FATAL_FAILURE(ClickOnView(VIEW_ID_TAB_CONTAINER));
ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestAltKeySuppress));
ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlAltKey));
ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
}
#endif
}