| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
| 6 #include "chrome/browser/history/history_service_factory.h" | |
| 7 #include "chrome/browser/instant/instant_loader.h" | 5 #include "chrome/browser/instant/instant_loader.h" |
| 8 #include "chrome/browser/instant/instant_model_observer.h" | 6 #include "chrome/browser/instant/instant_test_utils.h" |
| 9 #include "chrome/browser/prefs/pref_service.h" | |
| 10 #include "chrome/browser/search_engines/template_url_service.h" | |
| 11 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
| 12 #include "chrome/browser/task_manager/task_manager.h" | |
| 13 #include "chrome/browser/task_manager/task_manager_browsertest_util.h" | |
| 14 #include "chrome/browser/ui/browser_commands.h" | |
| 15 #include "chrome/browser/ui/browser_instant_controller.h" | |
| 16 #include "chrome/browser/ui/browser_tabstrip.h" | |
| 17 #include "chrome/browser/ui/browser_window.h" | |
| 18 #include "chrome/browser/ui/omnibox/location_bar.h" | |
| 19 #include "chrome/browser/ui/omnibox/omnibox_view.h" | |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 21 #include "chrome/common/chrome_notification_types.h" | |
| 22 #include "chrome/common/pref_names.h" | |
| 23 #include "chrome/test/base/interactive_test_utils.h" | |
| 24 #include "chrome/test/base/in_process_browser_test.h" | |
| 25 #include "chrome/test/base/ui_test_utils.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/common/result_codes.h" | |
| 30 #include "content/public/test/browser_test_utils.h" | |
| 31 #include "grit/generated_resources.h" | 7 #include "grit/generated_resources.h" |
| 32 #include "ui/base/l10n/l10n_util.h" | 8 #include "ui/base/l10n/l10n_util.h" |
| 33 | 9 |
| 34 class InstantTestModelObserver : public InstantModelObserver { | 10 class InstantTest : public InstantTestBase { |
| 35 public: | |
| 36 InstantTestModelObserver(const InstantModel* model, | |
| 37 chrome::search::Mode::Type desired_mode_type) | |
| 38 : model_(model), | |
| 39 desired_mode_type_(desired_mode_type) { | |
| 40 model_->AddObserver(this); | |
| 41 } | |
| 42 | |
| 43 ~InstantTestModelObserver() { | |
| 44 model_->RemoveObserver(this); | |
| 45 } | |
| 46 | |
| 47 void WaitUntilDesiredPreviewState() { | |
| 48 run_loop_.Run(); | |
| 49 } | |
| 50 | |
| 51 // Overridden from InstantModelObserver: | |
| 52 virtual void PreviewStateChanged(const InstantModel& model) OVERRIDE { | |
| 53 if (model.mode().mode == desired_mode_type_) | |
| 54 run_loop_.Quit(); | |
| 55 } | |
| 56 | |
| 57 private: | |
| 58 const InstantModel* const model_; | |
| 59 const chrome::search::Mode::Type desired_mode_type_; | |
| 60 base::RunLoop run_loop_; | |
| 61 | |
| 62 DISALLOW_COPY_AND_ASSIGN(InstantTestModelObserver); | |
| 63 }; | |
| 64 | |
| 65 class InstantTest : public InProcessBrowserTest { | |
| 66 protected: | 11 protected: |
| 67 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | 12 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
| 68 ASSERT_TRUE(test_server()->Start()); | 13 ASSERT_TRUE(test_server()->Start()); |
| 69 instant_url_ = test_server()->GetURL("files/instant.html"); | 14 instant_url_ = test_server()->GetURL("files/instant.html"); |
| 70 } | 15 } |
| 71 | 16 |
| 72 void SetupInstant() { | |
| 73 TemplateURLService* service = | |
| 74 TemplateURLServiceFactory::GetForProfile(browser()->profile()); | |
| 75 ui_test_utils::WaitForTemplateURLServiceToLoad(service); | |
| 76 | |
| 77 TemplateURLData data; | |
| 78 data.SetURL("http://does/not/exist?q={searchTerms}"); | |
| 79 data.instant_url = instant_url_.spec(); | |
| 80 | |
| 81 TemplateURL* template_url = new TemplateURL(browser()->profile(), data); | |
| 82 service->Add(template_url); // Takes ownership of |template_url|. | |
| 83 service->SetDefaultSearchProvider(template_url); | |
| 84 | |
| 85 browser()->profile()->GetPrefs()->SetBoolean(prefs::kInstantEnabled, true); | |
| 86 } | |
| 87 | |
| 88 InstantController* instant() { | |
| 89 return browser()->instant_controller()->instant(); | |
| 90 } | |
| 91 | |
| 92 OmniboxView* omnibox() { | |
| 93 return browser()->window()->GetLocationBar()->GetLocationEntry(); | |
| 94 } | |
| 95 | |
| 96 void KillInstantRenderView() { | |
| 97 base::KillProcess( | |
| 98 instant()->GetPreviewContents()->GetRenderProcessHost()->GetHandle(), | |
| 99 content::RESULT_CODE_KILLED, | |
| 100 false); | |
| 101 } | |
| 102 | |
| 103 void FocusOmnibox() { | |
| 104 // If the omnibox already has focus, just notify Instant. | |
| 105 if (omnibox()->model()->has_focus()) { | |
| 106 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, | |
| 107 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); | |
| 108 } else { | |
| 109 browser()->window()->GetLocationBar()->FocusLocation(false); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 void FocusOmniboxAndWaitForInstantSupport() { | |
| 114 content::WindowedNotificationObserver observer( | |
| 115 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED, | |
| 116 content::NotificationService::AllSources()); | |
| 117 FocusOmnibox(); | |
| 118 observer.Wait(); | |
| 119 } | |
| 120 | |
| 121 void SetOmniboxText(const std::string& text) { | |
| 122 FocusOmnibox(); | |
| 123 omnibox()->SetUserText(UTF8ToUTF16(text)); | |
| 124 } | |
| 125 | |
| 126 void SetOmniboxTextAndWaitForInstantToShow(const std::string& text) { | |
| 127 InstantTestModelObserver observer( | |
| 128 instant()->model(), chrome::search::Mode::MODE_SEARCH_SUGGESTIONS); | |
| 129 SetOmniboxText(text); | |
| 130 observer.WaitUntilDesiredPreviewState(); | |
| 131 } | |
| 132 | |
| 133 std::string WrapScript(const std::string& script) const { | |
| 134 return "domAutomationController.send(" + script + ")"; | |
| 135 } | |
| 136 | |
| 137 bool GetBoolFromJS(content::RenderViewHost* rvh, | |
| 138 const std::string& script, | |
| 139 bool* result) WARN_UNUSED_RESULT { | |
| 140 return content::ExecuteScriptAndExtractBool(rvh, WrapScript(script), | |
| 141 result); | |
| 142 } | |
| 143 | |
| 144 bool GetIntFromJS(content::RenderViewHost* rvh, | |
| 145 const std::string& script, | |
| 146 int* result) WARN_UNUSED_RESULT { | |
| 147 return content::ExecuteScriptAndExtractInt(rvh, WrapScript(script), result); | |
| 148 } | |
| 149 | |
| 150 bool GetStringFromJS(content::RenderViewHost* rvh, | |
| 151 const std::string& script, | |
| 152 std::string* result) WARN_UNUSED_RESULT { | |
| 153 return content::ExecuteScriptAndExtractString(rvh, WrapScript(script), | |
| 154 result); | |
| 155 } | |
| 156 | |
| 157 bool UpdateSearchState(content::WebContents* contents) WARN_UNUSED_RESULT { | 17 bool UpdateSearchState(content::WebContents* contents) WARN_UNUSED_RESULT { |
| 158 content::RenderViewHost* rvh = contents->GetRenderViewHost(); | 18 content::RenderViewHost* rvh = contents->GetRenderViewHost(); |
| 159 return GetIntFromJS(rvh, "onvisibilitycalls", &onvisibilitycalls_) && | 19 return GetIntFromJS(rvh, "onvisibilitycalls", &onvisibilitycalls_) && |
| 160 GetIntFromJS(rvh, "onchangecalls", &onchangecalls_) && | 20 GetIntFromJS(rvh, "onchangecalls", &onchangecalls_) && |
| 161 GetIntFromJS(rvh, "onsubmitcalls", &onsubmitcalls_) && | 21 GetIntFromJS(rvh, "onsubmitcalls", &onsubmitcalls_) && |
| 162 GetIntFromJS(rvh, "oncancelcalls", &oncancelcalls_) && | 22 GetIntFromJS(rvh, "oncancelcalls", &oncancelcalls_) && |
| 163 GetIntFromJS(rvh, "onresizecalls", &onresizecalls_) && | 23 GetIntFromJS(rvh, "onresizecalls", &onresizecalls_) && |
| 164 GetStringFromJS(rvh, "value", &value_) && | 24 GetStringFromJS(rvh, "value", &value_) && |
| 165 GetBoolFromJS(rvh, "verbatim", &verbatim_) && | 25 GetBoolFromJS(rvh, "verbatim", &verbatim_) && |
| 166 GetIntFromJS(rvh, "height", &height_); | 26 GetIntFromJS(rvh, "height", &height_); |
| 167 } | 27 } |
| 168 | 28 |
| 169 bool ExecuteScript(const std::string& script) WARN_UNUSED_RESULT { | |
| 170 return content::ExecuteScript( | |
| 171 instant()->GetPreviewContents()->GetRenderViewHost(), script); | |
| 172 } | |
| 173 | |
| 174 bool CheckVisibilityIs(content::WebContents* contents, | |
| 175 bool expected) WARN_UNUSED_RESULT { | |
| 176 bool actual = !expected; // Purposely start with a mis-match. | |
| 177 // We can only use ASSERT_*() in a method that returns void, hence this | |
| 178 // convoluted check. | |
| 179 return GetBoolFromJS(contents->GetRenderViewHost(), | |
| 180 "!document.webkitHidden", &actual) && | |
| 181 actual == expected; | |
| 182 } | |
| 183 | |
| 184 GURL instant_url_; | |
| 185 | |
| 186 int onvisibilitycalls_; | 29 int onvisibilitycalls_; |
| 187 int onchangecalls_; | 30 int onchangecalls_; |
| 188 int onsubmitcalls_; | 31 int onsubmitcalls_; |
| 189 int oncancelcalls_; | 32 int oncancelcalls_; |
| 190 int onresizecalls_; | 33 int onresizecalls_; |
| 191 | 34 |
| 192 std::string value_; | 35 std::string value_; |
| 193 bool verbatim_; | 36 bool verbatim_; |
| 194 int height_; | 37 int height_; |
| 195 }; | 38 }; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); | 79 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); |
| 237 | 80 |
| 238 EXPECT_EQ(preview_tab, instant()->GetPreviewContents()); | 81 EXPECT_EQ(preview_tab, instant()->GetPreviewContents()); |
| 239 | 82 |
| 240 // Doing a search should also use the same preloaded page. | 83 // Doing a search should also use the same preloaded page. |
| 241 SetOmniboxTextAndWaitForInstantToShow("query"); | 84 SetOmniboxTextAndWaitForInstantToShow("query"); |
| 242 EXPECT_TRUE(instant()->model()->mode().is_search_suggestions()); | 85 EXPECT_TRUE(instant()->model()->mode().is_search_suggestions()); |
| 243 EXPECT_EQ(preview_tab, instant()->GetPreviewContents()); | 86 EXPECT_EQ(preview_tab, instant()->GetPreviewContents()); |
| 244 } | 87 } |
| 245 | 88 |
| 89 // Test that Instant works when the URL is set via a TemplateURL (as opposed to |
| 90 // --instant-url). |
| 91 IN_PROC_BROWSER_TEST_F(InstantTest, SetWithTemplateURL) { |
| 92 ASSERT_NO_FATAL_FAILURE(SetupInstantUsingTemplateURL()); |
| 93 |
| 94 // Explicitly unfocus the omnibox. |
| 95 EXPECT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 96 ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER); |
| 97 |
| 98 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); |
| 99 EXPECT_FALSE(omnibox()->model()->has_focus()); |
| 100 |
| 101 // Delete any existing preview. |
| 102 instant()->loader_.reset(); |
| 103 EXPECT_FALSE(instant()->GetPreviewContents()); |
| 104 |
| 105 // Refocus the omnibox. The InstantController should've preloaded Instant. |
| 106 FocusOmniboxAndWaitForInstantSupport(); |
| 107 |
| 108 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_TAB_CONTAINER)); |
| 109 EXPECT_TRUE(omnibox()->model()->has_focus()); |
| 110 |
| 111 content::WebContents* preview_tab = instant()->GetPreviewContents(); |
| 112 EXPECT_TRUE(preview_tab); |
| 113 |
| 114 // Check that the page supports Instant, but it isn't showing. |
| 115 EXPECT_TRUE(instant()->loader_->supports_instant()); |
| 116 EXPECT_FALSE(instant()->IsPreviewingSearchResults()); |
| 117 EXPECT_TRUE(instant()->model()->mode().is_default()); |
| 118 } |
| 119 |
| 246 // Test that the onchange event is dispatched upon typing in the omnibox. | 120 // Test that the onchange event is dispatched upon typing in the omnibox. |
| 247 IN_PROC_BROWSER_TEST_F(InstantTest, OnChangeEvent) { | 121 IN_PROC_BROWSER_TEST_F(InstantTest, OnChangeEvent) { |
| 248 ASSERT_NO_FATAL_FAILURE(SetupInstant()); | 122 ASSERT_NO_FATAL_FAILURE(SetupInstant()); |
| 249 FocusOmniboxAndWaitForInstantSupport(); | 123 FocusOmniboxAndWaitForInstantSupport(); |
| 250 | 124 |
| 251 // Use the Instant page as the active tab, so we can exploit its visibility | 125 // Use the Instant page as the active tab, so we can exploit its visibility |
| 252 // handler to check visibility transitions. | 126 // handler to check visibility transitions. |
| 253 ui_test_utils::NavigateToURL(browser(), instant_url_); | 127 ui_test_utils::NavigateToURL(browser(), instant_url_); |
| 254 content::RenderViewHost* active_rvh = | 128 content::RenderViewHost* active_rvh = |
| 255 chrome::GetActiveWebContents(browser())->GetRenderViewHost(); | 129 chrome::GetActiveWebContents(browser())->GetRenderViewHost(); |
| (...skipping 825 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1081 SetOmniboxTextAndWaitForInstantToShow("q"); | 955 SetOmniboxTextAndWaitForInstantToShow("q"); |
| 1082 EXPECT_EQ(ASCIIToUTF16("query suggestion"), omnibox()->GetText()); | 956 EXPECT_EQ(ASCIIToUTF16("query suggestion"), omnibox()->GetText()); |
| 1083 | 957 |
| 1084 // Kill the instant renderer and wait for instant support again. | 958 // Kill the instant renderer and wait for instant support again. |
| 1085 KillInstantRenderView(); | 959 KillInstantRenderView(); |
| 1086 FocusOmniboxAndWaitForInstantSupport(); | 960 FocusOmniboxAndWaitForInstantSupport(); |
| 1087 | 961 |
| 1088 SetOmniboxTextAndWaitForInstantToShow("qu"); | 962 SetOmniboxTextAndWaitForInstantToShow("qu"); |
| 1089 EXPECT_EQ(ASCIIToUTF16("query suggestion"), omnibox()->GetText()); | 963 EXPECT_EQ(ASCIIToUTF16("query suggestion"), omnibox()->GetText()); |
| 1090 } | 964 } |
| OLD | NEW |