Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2612)

Unified Diff: chrome/browser/keyboardevent_code_interactive_browsertest.cc

Issue 2946103002: Add KeyboardEventCodeInteractiveTest
Patch Set: This iteration is expected to fail on Windows as https://codereview.chromium.org/2939283002/ is req… Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/test/BUILD.gn » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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");
+}
« no previous file with comments | « no previous file | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698