19f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher// Copyright 2013 The Chromium Authors. All rights reserved.
29f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher// Use of this source code is governed by a BSD-style license that can be
39f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher// found in the LICENSE file.
49f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher
59f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "base/strings/string_util.h"
69f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/browser/chrome_notification_types.h"
79f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/browser/search/instant_service.h"
89f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/browser/search/instant_service_factory.h"
99f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/browser/search/search.h"
109f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/browser/task_manager/task_manager.h"
119f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/browser/task_manager/task_manager_browsertest_util.h"
129f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/browser/ui/browser.h"
139f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/browser/ui/browser_commands.h"
149f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/browser/ui/host_desktop.h"
159f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/browser/ui/omnibox/omnibox_view.h"
169f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/browser/ui/search/instant_test_utils.h"
179f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/browser/ui/tabs/tab_strip_model.h"
189f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/common/omnibox_focus_state.h"
199f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/common/search_types.h"
209f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/common/url_constants.h"
219f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/test/base/in_process_browser_test.h"
229f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/test/base/interactive_test_utils.h"
239f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "chrome/test/base/ui_test_utils.h"
249f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "content/public/browser/navigation_controller.h"
259f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "content/public/browser/navigation_entry.h"
269f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "content/public/browser/notification_service.h"
279f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "content/public/browser/render_process_host.h"
289f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "content/public/browser/web_contents.h"
299f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "content/public/test/browser_test_utils.h"
309f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher#include "net/base/network_change_notifier.h"
3136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "net/dns/mock_host_resolver.h"
32f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher#include "url/gurl.h"
339f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher
349f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher// !!! IMPORTANT !!!
359f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher// These tests are run against a mock GWS using the web-page-replay system.
369f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher// If you change a test, you MUST re-record the mock GWS session.
379f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher// See: src/chrome/test/data/search/tools/instant_extended_manual_tests.py
389f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher// for details.
399f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher
409f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher// Instant extended tests that need to be run manually because they need to
419f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher// talk to the external network. All tests in this file should be marked as
429f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher// "MANUAL_" unless they are disabled.
43800e6ee52fd32e0b129cbccd811166d7e215e6adEric Christopherclass InstantExtendedManualTest : public InProcessBrowserTest,
449f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher                                  public InstantTestBase {
459f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher public:
46f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher  InstantExtendedManualTest() {
47800e6ee52fd32e0b129cbccd811166d7e215e6adEric Christopher    host_resolver_proc_ = new net::RuleBasedHostResolverProc(NULL);
48f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher    host_resolver_proc_->AllowDirectLookup("*");
499f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher    scoped_host_resolver_proc_.reset(
509f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher        new net::ScopedDefaultHostResolverProc(host_resolver_proc_.get()));
51769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher  }
52800e6ee52fd32e0b129cbccd811166d7e215e6adEric Christopher
539f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher  virtual ~InstantExtendedManualTest() {
54769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher    scoped_host_resolver_proc_.reset();
55769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher    host_resolver_proc_ = NULL;
56769d24a60d798ffa3a47a01c9e7a0dcfaefbb882Eric Christopher  }
579f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher
58f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher  virtual void SetUpOnMainThread() OVERRIDE {
59f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher    const testing::TestInfo* const test_info =
6060057270d9ea9cb9527e0ac8b77ba2c5a08a8af9Eric Christopher        testing::UnitTest::GetInstance()->current_test_info();
6160057270d9ea9cb9527e0ac8b77ba2c5a08a8af9Eric Christopher    ASSERT_TRUE(StartsWithASCII(test_info->name(), "MANUAL_", true) ||
62f7306f224e7f85c2690256636613422c4a7b8230Eric Christopher                StartsWithASCII(test_info->name(), "DISABLED_", true));
639f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher    // Make IsOffline() return false so we don't try to use the local NTP.
649f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher    disable_network_change_notifier_.reset(
65800e6ee52fd32e0b129cbccd811166d7e215e6adEric Christopher        new net::NetworkChangeNotifier::DisableForTest());
669f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher  }
679f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher
689f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher  virtual void TearDownOnMainThread() OVERRIDE {
699f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher    disable_network_change_notifier_.reset();
709f31f7c3055a3c028c54888832ed61c6912aac41Eric Christopher  }
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
95IN_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