1 // Copyright 2013 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.
5 #include "base/strings/string_util.h"
6 #include "chrome/browser/chrome_notification_types.h"
7 #include "chrome/browser/search/instant_service.h"
8 #include "chrome/browser/search/instant_service_factory.h"
9 #include "chrome/browser/search/search.h"
10 #include "chrome/browser/task_manager/task_manager.h"
11 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_commands.h"
14 #include "chrome/browser/ui/host_desktop.h"
15 #include "chrome/browser/ui/search/instant_test_utils.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/search_types.h"
18 #include "chrome/common/url_constants.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "chrome/test/base/interactive_test_utils.h"
21 #include "chrome/test/base/ui_test_utils.h"
22 #include "components/omnibox/browser/omnibox_view.h"
23 #include "components/omnibox/common/omnibox_focus_state.h"
24 #include "components/search/search.h"
25 #include "content/public/browser/navigation_controller.h"
26 #include "content/public/browser/navigation_entry.h"
27 #include "content/public/browser/notification_service.h"
28 #include "content/public/browser/render_process_host.h"
29 #include "content/public/browser/web_contents.h"
30 #include "content/public/test/browser_test_utils.h"
31 #include "net/base/network_change_notifier.h"
32 #include "net/dns/mock_host_resolver.h"
36 // These tests are run against a mock GWS using the web-page-replay system.
37 // If you change a test, you MUST re-record the mock GWS session.
38 // See: src/chrome/test/data/search/tools/instant_extended_manual_tests.py
41 // Instant extended tests that need to be run manually because they need to
42 // talk to the external network. All tests in this file should be marked as
43 // "MANUAL_" unless they are disabled.
44 class InstantExtendedManualTest
: public InProcessBrowserTest
,
45 public InstantTestBase
{
47 InstantExtendedManualTest() {
48 host_resolver_proc_
= new net::RuleBasedHostResolverProc(NULL
);
49 host_resolver_proc_
->AllowDirectLookup("*");
50 scoped_host_resolver_proc_
.reset(
51 new net::ScopedDefaultHostResolverProc(host_resolver_proc_
.get()));
54 ~InstantExtendedManualTest() override
{
55 scoped_host_resolver_proc_
.reset();
56 host_resolver_proc_
= NULL
;
59 void SetUpOnMainThread() override
{
60 const testing::TestInfo
* const test_info
=
61 testing::UnitTest::GetInstance()->current_test_info();
62 ASSERT_TRUE(base::StartsWith(test_info
->name(), "MANUAL_",
63 base::CompareCase::SENSITIVE
) ||
64 base::StartsWith(test_info
->name(), "DISABLED_",
65 base::CompareCase::SENSITIVE
));
66 // Make IsOffline() return false so we don't try to use the local NTP.
67 disable_network_change_notifier_
.reset(
68 new net::NetworkChangeNotifier::DisableForTest());
71 void TearDownOnMainThread() override
{
72 disable_network_change_notifier_
.reset();
76 void SetUpInProcessBrowserTestFixture() override
{
77 search::EnableQueryExtractionForTesting();
80 content::WebContents
* active_tab() {
81 return browser()->tab_strip_model()->GetActiveWebContents();
84 bool IsGooglePage(content::WebContents
* contents
) {
85 bool is_google
= false;
86 if (!GetBoolFromJS(contents
, "!!window.google", &is_google
))
92 scoped_refptr
<net::RuleBasedHostResolverProc
> host_resolver_proc_
;
93 scoped_ptr
<net::ScopedDefaultHostResolverProc
> scoped_host_resolver_proc_
;
94 scoped_ptr
<net::NetworkChangeNotifier::DisableForTest
>
95 disable_network_change_notifier_
;
98 IN_PROC_BROWSER_TEST_F(InstantExtendedManualTest
, MANUAL_SearchesFromFakebox
) {
99 set_browser(browser());
102 // Open a new tab page.
103 content::WindowedNotificationObserver
observer(
104 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
105 content::NotificationService::AllSources());
106 ui_test_utils::NavigateToURLWithDisposition(
108 GURL(chrome::kChromeUINewTabURL
),
110 ui_test_utils::BROWSER_TEST_NONE
);
112 content::WebContents
* active_tab
=
113 browser()->tab_strip_model()->GetActiveWebContents();
114 ASSERT_TRUE(IsGooglePage(active_tab
));
116 // Click in the fakebox and expect invisible focus.
117 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER
);
118 bool fakebox_is_present
= false;
119 content::WindowedNotificationObserver
focus_observer(
120 chrome::NOTIFICATION_OMNIBOX_FOCUS_CHANGED
,
121 content::NotificationService::AllSources());
122 ASSERT_TRUE(GetBoolFromJS(active_tab
, "!!document.querySelector('#fkbx')",
123 &fakebox_is_present
));
124 ASSERT_TRUE(fakebox_is_present
);
125 ASSERT_TRUE(content::ExecuteScript(
126 active_tab
, "document.querySelector('#fkbx').click()"));
127 focus_observer
.Wait();
128 EXPECT_EQ(OMNIBOX_FOCUS_INVISIBLE
, omnibox()->model()->focus_state());
131 const ui::KeyboardCode query
[] = {
132 ui::VKEY_T
, ui::VKEY_E
, ui::VKEY_S
, ui::VKEY_T
,
135 for (size_t i
= 0; query
[i
] != ui::VKEY_UNKNOWN
; i
++) {
136 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), query
[i
],
137 false, false, false, false));
140 // The omnibox should say "test" and have visible focus.
141 EXPECT_EQ("test", GetOmniboxText());
142 EXPECT_EQ(OMNIBOX_FOCUS_VISIBLE
, omnibox()->model()->focus_state());
144 // Pressing enter should search for [test].
145 const base::string16
& search_title
=
146 base::ASCIIToUTF16("test - Google Search");
147 content::TitleWatcher
title_watcher(active_tab
, search_title
);
148 PressEnterAndWaitForNavigation();
149 EXPECT_EQ(search_title
, title_watcher
.WaitAndGetTitle());