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. 4 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/omnibox/omnibox_view.h" 16 #include "chrome/browser/ui/search/instant_ntp_prerenderer.h" 17 #include "chrome/browser/ui/search/instant_test_utils.h" 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" 19 #include "chrome/common/omnibox_focus_state.h" 20 #include "chrome/common/search_types.h" 21 #include "chrome/common/url_constants.h" 22 #include "chrome/test/base/in_process_browser_test.h" 23 #include "chrome/test/base/interactive_test_utils.h" 24 #include "chrome/test/base/ui_test_utils.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" 33 #include "url/gurl.h" 34 35 // !!! IMPORTANT !!! 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 39 // for details. 40 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 { 46 public: 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())); 52 } 53 54 virtual ~InstantExtendedManualTest() { 55 scoped_host_resolver_proc_.reset(); 56 host_resolver_proc_ = NULL; 57 } 58 59 virtual void SetUpOnMainThread() OVERRIDE { 60 const testing::TestInfo* const test_info = 61 testing::UnitTest::GetInstance()->current_test_info(); 62 ASSERT_TRUE(StartsWithASCII(test_info->name(), "MANUAL_", true) || 63 StartsWithASCII(test_info->name(), "DISABLED_", true)); 64 // Make IsOffline() return false so we don't try to use the local NTP. 65 disable_network_change_notifier_.reset( 66 new net::NetworkChangeNotifier::DisableForTest()); 67 } 68 69 virtual void CleanUpOnMainThread() OVERRIDE { 70 disable_network_change_notifier_.reset(); 71 } 72 73 protected: 74 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { 75 chrome::EnableQueryExtractionForTesting(); 76 } 77 78 content::WebContents* active_tab() { 79 return browser()->tab_strip_model()->GetActiveWebContents(); 80 } 81 82 bool IsGooglePage(content::WebContents* contents) { 83 bool is_google = false; 84 if (!GetBoolFromJS(contents, "!!window.google", &is_google)) 85 return false; 86 return is_google; 87 } 88 89 private: 90 scoped_refptr<net::RuleBasedHostResolverProc> host_resolver_proc_; 91 scoped_ptr<net::ScopedDefaultHostResolverProc> scoped_host_resolver_proc_; 92 scoped_ptr<net::NetworkChangeNotifier::DisableForTest> 93 disable_network_change_notifier_; 94 }; 95 96 IN_PROC_BROWSER_TEST_F(InstantExtendedManualTest, MANUAL_ShowsGoogleNTP) { 97 set_browser(browser()); 98 InstantService* instant_service = 99 InstantServiceFactory::GetForProfile(browser()->profile()); 100 ASSERT_NE(static_cast<InstantService*>(NULL), instant_service); 101 instant_service->ntp_prerenderer()->ReloadInstantNTP(); 102 103 FocusOmniboxAndWaitForInstantNTPSupport(); 104 content::WindowedNotificationObserver observer( 105 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 106 content::NotificationService::AllSources()); 107 ui_test_utils::NavigateToURLWithDisposition( 108 browser(), 109 GURL(chrome::kChromeUINewTabURL), 110 CURRENT_TAB, 111 ui_test_utils::BROWSER_TEST_NONE); 112 observer.Wait(); 113 content::WebContents* active_tab = 114 browser()->tab_strip_model()->GetActiveWebContents(); 115 EXPECT_TRUE(IsGooglePage(active_tab)); 116 } 117 118 IN_PROC_BROWSER_TEST_F(InstantExtendedManualTest, MANUAL_SearchesFromFakebox) { 119 set_browser(browser()); 120 InstantService* instant_service = 121 InstantServiceFactory::GetForProfile(browser()->profile()); 122 ASSERT_NE(static_cast<InstantService*>(NULL), instant_service); 123 instant_service->ntp_prerenderer()->ReloadInstantNTP(); 124 125 FocusOmniboxAndWaitForInstantNTPSupport(); 126 // Open a new tab page. 127 content::WindowedNotificationObserver observer( 128 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 129 content::NotificationService::AllSources()); 130 ui_test_utils::NavigateToURLWithDisposition( 131 browser(), 132 GURL(chrome::kChromeUINewTabURL), 133 CURRENT_TAB, 134 ui_test_utils::BROWSER_TEST_NONE); 135 observer.Wait(); 136 content::WebContents* active_tab = 137 browser()->tab_strip_model()->GetActiveWebContents(); 138 ASSERT_TRUE(IsGooglePage(active_tab)); 139 140 // Click in the fakebox and expect invisible focus. 141 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER); 142 bool fakebox_is_present = false; 143 content::WindowedNotificationObserver focus_observer( 144 chrome::NOTIFICATION_OMNIBOX_FOCUS_CHANGED, 145 content::NotificationService::AllSources()); 146 ASSERT_TRUE(GetBoolFromJS(active_tab, "!!document.querySelector('#fkbx')", 147 &fakebox_is_present)); 148 ASSERT_TRUE(fakebox_is_present); 149 ASSERT_TRUE(content::ExecuteScript( 150 active_tab, "document.querySelector('#fkbx').click()")); 151 focus_observer.Wait(); 152 EXPECT_EQ(OMNIBOX_FOCUS_INVISIBLE, omnibox()->model()->focus_state()); 153 154 // Type "test". 155 const ui::KeyboardCode query[] = { 156 ui::VKEY_T, ui::VKEY_E, ui::VKEY_S, ui::VKEY_T, 157 ui::VKEY_UNKNOWN 158 }; 159 for (size_t i = 0; query[i] != ui::VKEY_UNKNOWN; i++) { 160 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), query[i], 161 false, false, false, false)); 162 } 163 164 // The omnibox should say "test" and have visible focus. 165 EXPECT_EQ("test", GetOmniboxText()); 166 EXPECT_EQ(OMNIBOX_FOCUS_VISIBLE, omnibox()->model()->focus_state()); 167 168 // Pressing enter should search for [test]. 169 const base::string16& search_title = ASCIIToUTF16("test - Google Search"); 170 content::TitleWatcher title_watcher(active_tab, search_title); 171 PressEnterAndWaitForNavigation(); 172 EXPECT_EQ(search_title, title_watcher.WaitAndGetTitle()); 173 } 174