Index: chrome/browser/keyboardevent_code_interactive_browsertest.cc |
diff --git a/chrome/browser/keyboardevent_code_interactive_browsertest.cc b/chrome/browser/keyboardevent_code_interactive_browsertest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..32275a2f33537324d226197e902e851bf98163e2 |
--- /dev/null |
+++ b/chrome/browser/keyboardevent_code_interactive_browsertest.cc |
@@ -0,0 +1,89 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <string> |
+ |
+#include "base/strings/string_util.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/ui/tabs/tab_strip_model.h" |
+#include "chrome/test/base/in_process_browser_test.h" |
+#include "chrome/test/base/interactive_test_utils.h" |
+#include "content/public/test/browser_test_utils.h" |
+#include "content/public/test/test_utils.h" |
+#include "ui/events/keycodes/keyboard_codes.h" |
+ |
+namespace { |
+// The html file to receive key events and exports all the events with |
+// "report()" function. It has one magic key: pressing "X" to indicate the end |
+// of all the keys. |
+constexpr char kKeyboardEventCodeTestHtml[] = "/keyboardevent_code_test.html"; |
+} // namespace |
+ |
+class KeyboardEventCodeInteractiveTest : public InProcessBrowserTest { |
+ public: |
+ KeyboardEventCodeInteractiveTest() = default; |
+ ~KeyboardEventCodeInteractiveTest() override = default; |
+ |
+ protected: |
+ // Starts the test page and waits for it to be loaded. |
+ void StartTestPage(); |
+ |
+ // Sends a single key to the focused window. |
+ void SendKey(ui::KeyboardCode code); |
+ |
+ // Sends a magic KeyX to the focused window to stop the test case and receives |
+ // the result. |
+ std::string FinishTestAndGetResult(); |
+}; |
+ |
+void KeyboardEventCodeInteractiveTest::StartTestPage() { |
+ ASSERT_TRUE(embedded_test_server()->Start()); |
+ ui_test_utils::NavigateToURLWithDisposition( |
+ browser(), |
+ embedded_test_server()->GetURL(kKeyboardEventCodeTestHtml), |
+ WindowOpenDisposition::NEW_FOREGROUND_TAB, |
+ ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
+ ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
+} |
+ |
+void KeyboardEventCodeInteractiveTest::SendKey(ui::KeyboardCode code) { |
+ ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
+ browser(), code, false, false, false, false)); |
+} |
+ |
+std::string KeyboardEventCodeInteractiveTest::FinishTestAndGetResult() { |
+ // Magic KeyX to stop the test. |
+ EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), |
+ ui::VKEY_X, false, false, false, false)); |
+ std::string result; |
+ EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
+ browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost(), |
+ "report()", |
+ &result)); |
+#if defined(OS_MACOSX) |
+ // On MacOSX command key is used for most of the shortcuts, so replace it with |
+ // control to reduce the complexity of comparison of the results. |
+ base::ReplaceSubstringsAfterOffset(&result, 0, "MetaLeft", "ControlLeft"); |
+#endif |
+ base::TrimWhitespaceASCII(result, base::TRIM_ALL, &result); |
+ return result; |
+} |
+ |
+IN_PROC_BROWSER_TEST_F(KeyboardEventCodeInteractiveTest, SendKeys) { |
+ ASSERT_EQ(browser()->tab_strip_model()->count(), 1); |
+ StartTestPage(); |
+ |
+ SendKey(ui::VKEY_A); |
+ SendKey(ui::VKEY_S); |
+ SendKey(ui::VKEY_D); |
+ SendKey(ui::VKEY_F); |
+ |
+ std::string result = FinishTestAndGetResult(); |
+ ASSERT_EQ(result, |
+ "KeyA\n" |
+ "KeyS\n" |
+ "KeyD\n" |
+ "KeyF\n" |
+ "KeyX"); |
+} |