Home | History | Annotate | Download | only in search
      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_test_utils.h"
     17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     18 #include "chrome/common/omnibox_focus_state.h"
     19 #include "chrome/common/search_types.h"
     20 #include "chrome/common/url_constants.h"
     21 #include "chrome/test/base/in_process_browser_test.h"
     22 #include "chrome/test/base/interactive_test_utils.h"
     23 #include "chrome/test/base/ui_test_utils.h"
     24 #include "content/public/browser/navigation_controller.h"
     25 #include "content/public/browser/navigation_entry.h"
     26 #include "content/public/browser/notification_service.h"
     27 #include "content/public/browser/render_process_host.h"
     28 #include "content/public/browser/web_contents.h"
     29 #include "content/public/test/browser_test_utils.h"
     30 #include "net/base/network_change_notifier.h"
     31 #include "net/dns/mock_host_resolver.h"
     32 #include "url/gurl.h"
     33 
     34 // !!! IMPORTANT !!!
     35 // These tests are run against a mock GWS using the web-page-replay system.
     36 // If you change a test, you MUST re-record the mock GWS session.
     37 // See: src/chrome/test/data/search/tools/instant_extended_manual_tests.py
     38 // for details.
     39 
     40 // Instant extended tests that need to be run manually because they need to
     41 // talk to the external network. All tests in this file should be marked as
     42 // "MANUAL_" unless they are disabled.
     43 class InstantExtendedManualTest : public InProcessBrowserTest,
     44                                   public InstantTestBase {
     45  public:
     46   InstantExtendedManualTest() {
     47     host_resolver_proc_ = new net::RuleBasedHostResolverProc(NULL);
     48     host_resolver_proc_->AllowDirectLookup("*");
     49     scoped_host_resolver_proc_.reset(
     50         new net::ScopedDefaultHostResolverProc(host_resolver_proc_.get()));
     51   }
     52 
     53   virtual ~InstantExtendedManualTest() {
     54     scoped_host_resolver_proc_.reset();
     55     host_resolver_proc_ = NULL;
     56   }
     57 
     58   virtual void SetUpOnMainThread() OVERRIDE {
     59     const testing::TestInfo* const test_info =
     60         testing::UnitTest::GetInstance()->current_test_info();
     61     ASSERT_TRUE(StartsWithASCII(test_info->name(), "MANUAL_", true) ||
     62                 StartsWithASCII(test_info->name(), "DISABLED_", true));
     63     // Make IsOffline() return false so we don't try to use the local NTP.
     64     disable_network_change_notifier_.reset(
     65         new net::NetworkChangeNotifier::DisableForTest());
     66   }
     67 
     68   virtual void TearDownOnMainThread() OVERRIDE {
     69     disable_network_change_notifier_.reset();
     70   }
     71 
     72  protected:
     73   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
     74     chrome::EnableQueryExtractionForTesting();
     75   }
     76 
     77   content::WebContents* active_tab() {
     78     return browser()->tab_strip_model()->GetActiveWebContents();
     79   }
     80 
     81   bool IsGooglePage(content::WebContents* contents) {
     82     bool is_google = false;
     83     if (!GetBoolFromJS(contents, "!!window.google", &is_google))
     84       return false;
     85     return is_google;
     86   }
     87 
     88  private:
     89   scoped_refptr<net::RuleBasedHostResolverProc> host_resolver_proc_;
     90   scoped_ptr<net::ScopedDefaultHostResolverProc> scoped_host_resolver_proc_;
     91   scoped_ptr<net::NetworkChangeNotifier::DisableForTest>
     92       disable_network_change_notifier_;
     93 };
     94 
     95 IN_PROC_BROWSER_TEST_F(InstantExtendedManualTest, MANUAL_SearchesFromFakebox) {
     96   set_browser(browser());
     97 
     98   FocusOmnibox();
     99   // Open a new tab page.
    100   content::WindowedNotificationObserver observer(
    101       content::NOTIFICATION_NAV_ENTRY_COMMITTED,
    102       content::NotificationService::AllSources());
    103   ui_test_utils::NavigateToURLWithDisposition(
    104       browser(),
    105       GURL(chrome::kChromeUINewTabURL),
    106       CURRENT_TAB,
    107       ui_test_utils::BROWSER_TEST_NONE);
    108   observer.Wait();
    109   content::WebContents* active_tab =
    110       browser()->tab_strip_model()->GetActiveWebContents();
    111   ASSERT_TRUE(IsGooglePage(active_tab));
    112 
    113   // Click in the fakebox and expect invisible focus.
    114   ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER);
    115   bool fakebox_is_present = false;
    116   content::WindowedNotificationObserver focus_observer(
    117       chrome::NOTIFICATION_OMNIBOX_FOCUS_CHANGED,
    118       content::NotificationService::AllSources());
    119   ASSERT_TRUE(GetBoolFromJS(active_tab, "!!document.querySelector('#fkbx')",
    120                             &fakebox_is_present));
    121   ASSERT_TRUE(fakebox_is_present);
    122   ASSERT_TRUE(content::ExecuteScript(
    123       active_tab, "document.querySelector('#fkbx').click()"));
    124   focus_observer.Wait();
    125   EXPECT_EQ(OMNIBOX_FOCUS_INVISIBLE, omnibox()->model()->focus_state());
    126 
    127   // Type "test".
    128   const ui::KeyboardCode query[] = {
    129     ui::VKEY_T, ui::VKEY_E, ui::VKEY_S, ui::VKEY_T,
    130     ui::VKEY_UNKNOWN
    131   };
    132   for (size_t i = 0; query[i] != ui::VKEY_UNKNOWN; i++) {
    133     ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), query[i],
    134                                                 false, false, false, false));
    135   }
    136 
    137   // The omnibox should say "test" and have visible focus.
    138   EXPECT_EQ("test", GetOmniboxText());
    139   EXPECT_EQ(OMNIBOX_FOCUS_VISIBLE, omnibox()->model()->focus_state());
    140 
    141   // Pressing enter should search for [test].
    142   const base::string16& search_title =
    143       base::ASCIIToUTF16("test - Google Search");
    144   content::TitleWatcher title_watcher(active_tab, search_title);
    145   PressEnterAndWaitForNavigation();
    146   EXPECT_EQ(search_title, title_watcher.WaitAndGetTitle());
    147 }
    148