OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 <string> |
| 6 |
| 7 #include "base/strings/string_util.h" |
| 8 #include "chrome/browser/ui/browser.h" |
| 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 10 #include "chrome/test/base/in_process_browser_test.h" |
| 11 #include "chrome/test/base/interactive_test_utils.h" |
| 12 #include "content/public/test/browser_test_utils.h" |
| 13 #include "content/public/test/test_utils.h" |
| 14 #include "ui/events/keycodes/keyboard_codes.h" |
| 15 |
| 16 namespace { |
| 17 // The html file to receive key events and exports all the events with |
| 18 // "report()" function. It has one magic key: pressing "X" to indicate the end |
| 19 // of all the keys. |
| 20 constexpr char kKeyboardEventCodeTestHtml[] = "/keyboardevent_code_test.html"; |
| 21 } // namespace |
| 22 |
| 23 class KeyboardEventCodeInteractiveTest : public InProcessBrowserTest { |
| 24 public: |
| 25 KeyboardEventCodeInteractiveTest() = default; |
| 26 ~KeyboardEventCodeInteractiveTest() override = default; |
| 27 |
| 28 protected: |
| 29 // Starts the test page and waits for it to be loaded. |
| 30 void StartTestPage(); |
| 31 |
| 32 // Sends a single key to the focused window. |
| 33 void SendKey(ui::KeyboardCode code); |
| 34 |
| 35 // Sends a magic KeyX to the focused window to stop the test case and receives |
| 36 // the result. |
| 37 std::string FinishTestAndGetResult(); |
| 38 }; |
| 39 |
| 40 void KeyboardEventCodeInteractiveTest::StartTestPage() { |
| 41 ASSERT_TRUE(embedded_test_server()->Start()); |
| 42 ui_test_utils::NavigateToURLWithDisposition( |
| 43 browser(), |
| 44 embedded_test_server()->GetURL(kKeyboardEventCodeTestHtml), |
| 45 WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 46 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 47 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 48 } |
| 49 |
| 50 void KeyboardEventCodeInteractiveTest::SendKey(ui::KeyboardCode code) { |
| 51 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 52 browser(), code, false, false, false, false)); |
| 53 } |
| 54 |
| 55 std::string KeyboardEventCodeInteractiveTest::FinishTestAndGetResult() { |
| 56 // Magic KeyX to stop the test. |
| 57 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), |
| 58 ui::VKEY_X, false, false, false, false)); |
| 59 std::string result; |
| 60 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 61 browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost(), |
| 62 "report()", |
| 63 &result)); |
| 64 #if defined(OS_MACOSX) |
| 65 // On MacOSX command key is used for most of the shortcuts, so replace it with |
| 66 // control to reduce the complexity of comparison of the results. |
| 67 base::ReplaceSubstringsAfterOffset(&result, 0, "MetaLeft", "ControlLeft"); |
| 68 #endif |
| 69 base::TrimWhitespaceASCII(result, base::TRIM_ALL, &result); |
| 70 return result; |
| 71 } |
| 72 |
| 73 IN_PROC_BROWSER_TEST_F(KeyboardEventCodeInteractiveTest, SendKeys) { |
| 74 ASSERT_EQ(browser()->tab_strip_model()->count(), 1); |
| 75 StartTestPage(); |
| 76 |
| 77 SendKey(ui::VKEY_A); |
| 78 SendKey(ui::VKEY_S); |
| 79 SendKey(ui::VKEY_D); |
| 80 SendKey(ui::VKEY_F); |
| 81 |
| 82 std::string result = FinishTestAndGetResult(); |
| 83 ASSERT_EQ(result, |
| 84 "KeyA\n" |
| 85 "KeyS\n" |
| 86 "KeyD\n" |
| 87 "KeyF\n" |
| 88 "KeyX"); |
| 89 } |
OLD | NEW |