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 "chrome/browser/chrome_notification_types.h" |
| 6 #include "chrome/browser/search/search.h" |
| 7 #include "chrome/browser/ui/browser.h" |
| 8 #include "chrome/browser/ui/search/instant_test_utils.h" |
| 9 #include "chrome/browser/ui/search/instant_uitest_base.h" |
| 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 11 #include "chrome/test/base/in_process_browser_test.h" |
| 12 #include "chrome/test/base/interactive_test_utils.h" |
| 13 #include "chrome/test/base/ui_test_utils.h" |
| 14 #include "components/omnibox/browser/omnibox_edit_model.h" |
| 15 #include "components/omnibox/browser/omnibox_view.h" |
| 16 #include "components/omnibox/common/omnibox_focus_state.h" |
| 17 #include "content/public/browser/notification_service.h" |
| 18 #include "content/public/browser/web_contents.h" |
| 19 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 20 #include "ui/gfx/geometry/point.h" |
| 21 #include "ui/gfx/geometry/rect.h" |
| 22 #include "ui/gfx/geometry/vector2d.h" |
| 23 |
| 24 // A test class that sets up local_ntp_browsertest.html (which is mostly a copy |
| 25 // of the real local_ntp.html) as the NTP URL. |
| 26 class LocalNTPUITest : public InProcessBrowserTest, public InstantUITestBase { |
| 27 public: |
| 28 LocalNTPUITest() {} |
| 29 |
| 30 protected: |
| 31 void SetUpInProcessBrowserTestFixture() override { |
| 32 ASSERT_TRUE(https_test_server().Start()); |
| 33 GURL instant_url = |
| 34 https_test_server().GetURL("/instant_extended.html?strk=1&"); |
| 35 GURL ntp_url = |
| 36 https_test_server().GetURL("/local_ntp_browsertest.html?strk=1&"); |
| 37 InstantTestBase::Init(instant_url, ntp_url, false); |
| 38 } |
| 39 }; |
| 40 |
| 41 IN_PROC_BROWSER_TEST_F(LocalNTPUITest, FakeboxRedirectsToOmnibox) { |
| 42 ASSERT_NO_FATAL_FAILURE(SetupInstant(browser())); |
| 43 FocusOmnibox(); |
| 44 |
| 45 ui_test_utils::NavigateToURLWithDisposition( |
| 46 browser(), ntp_url(), WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 47 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB | |
| 48 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 49 content::WebContents* active_tab = |
| 50 browser()->tab_strip_model()->GetActiveWebContents(); |
| 51 ASSERT_TRUE(search::IsInstantNTP(active_tab)); |
| 52 |
| 53 // This is required to make the mouse events we send below arrive at the right |
| 54 // place. It *should* be the default for all interactive_ui_tests anyway, but |
| 55 // on Mac it isn't; see crbug.com/641969. |
| 56 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 57 |
| 58 bool result = false; |
| 59 ASSERT_TRUE(instant_test_utils::GetBoolFromJS( |
| 60 active_tab, "!!setupAdvancedTest()", &result)); |
| 61 ASSERT_TRUE(result); |
| 62 |
| 63 // Get the position of the fakebox on the page. |
| 64 double fakebox_x = 0; |
| 65 ASSERT_TRUE(instant_test_utils::GetDoubleFromJS( |
| 66 active_tab, "getFakeboxPositionX()", &fakebox_x)); |
| 67 double fakebox_y = 0; |
| 68 ASSERT_TRUE(instant_test_utils::GetDoubleFromJS( |
| 69 active_tab, "getFakeboxPositionY()", &fakebox_y)); |
| 70 |
| 71 // Move the mouse over the fakebox. |
| 72 gfx::Vector2d fakebox_pos(static_cast<int>(std::ceil(fakebox_x)), |
| 73 static_cast<int>(std::ceil(fakebox_y))); |
| 74 gfx::Point origin = active_tab->GetContainerBounds().origin(); |
| 75 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(origin + fakebox_pos + |
| 76 gfx::Vector2d(1, 1))); |
| 77 |
| 78 // Click on the fakebox, and wait for the omnibox to receive invisible focus. |
| 79 // Note that simply waiting for the first OMNIBOX_FOCUS_CHANGED notification |
| 80 // is not sufficient: If the omnibox had focus before, it will first lose the |
| 81 // focus before gaining invisible focus. |
| 82 ASSERT_NE(OMNIBOX_FOCUS_INVISIBLE, omnibox()->model()->focus_state()); |
| 83 content::WindowedNotificationObserver focus_observer( |
| 84 chrome::NOTIFICATION_OMNIBOX_FOCUS_CHANGED, |
| 85 base::Bind( |
| 86 [](const OmniboxEditModel* omnibox_model) { |
| 87 return omnibox_model->focus_state() == OMNIBOX_FOCUS_INVISIBLE; |
| 88 }, |
| 89 omnibox()->model())); |
| 90 |
| 91 ASSERT_TRUE( |
| 92 ui_test_utils::SendMouseEventsSync(ui_controls::LEFT, ui_controls::DOWN)); |
| 93 ASSERT_TRUE( |
| 94 ui_test_utils::SendMouseEventsSync(ui_controls::LEFT, ui_controls::UP)); |
| 95 |
| 96 focus_observer.Wait(); |
| 97 EXPECT_EQ(OMNIBOX_FOCUS_INVISIBLE, omnibox()->model()->focus_state()); |
| 98 |
| 99 // The fakebox should now also have focus. |
| 100 ASSERT_TRUE(instant_test_utils::GetBoolFromJS( |
| 101 active_tab, "!!fakeboxIsFocused()", &result)); |
| 102 EXPECT_TRUE(result); |
| 103 |
| 104 // Type "a". |
| 105 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 106 browser(), ui::KeyboardCode::VKEY_A, |
| 107 /*control=*/false, /*shift=*/false, /*alt=*/false, /*command=*/false)); |
| 108 |
| 109 // The omnibox should have received visible focus. |
| 110 EXPECT_EQ(OMNIBOX_FOCUS_VISIBLE, omnibox()->model()->focus_state()); |
| 111 // ...and the typed text should have arrived there. |
| 112 EXPECT_EQ("a", GetOmniboxText()); |
| 113 |
| 114 // On the JS side, the fakebox should have been hidden. |
| 115 ASSERT_TRUE(instant_test_utils::GetBoolFromJS( |
| 116 active_tab, "!!fakeboxIsVisible()", &result)); |
| 117 EXPECT_FALSE(result); |
| 118 } |
OLD | NEW |