| 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 } | |
| 109 else { | |
| 110 browser()->window()->GetLocationBar()->FocusLocation(false); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 void FocusOmniboxAndWaitForInstantSupport() { | |
| 115 content::WindowedNotificationObserver observer( | |
| 116 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED, | |
| 117 content::NotificationService::AllSources()); | |
| 118 FocusOmnibox(); | |
| 119 observer.Wait(); | |
| 120 } | |
| 121 | |
| 122 void SetOmniboxText(const std::string& text) { | |
| 123 FocusOmnibox(); | |
| 124 omnibox()->SetUserText(UTF8ToUTF16(text)); | |
| 125 } | |
| 126 | |
| 127 void SetOmniboxTextAndWaitForInstantToShow(const std::string& text) { | |
| 128 InstantTestModelObserver observer( | |
| 129 instant()->model(), chrome::search::Mode::MODE_SEARCH_SUGGESTIONS); | |
| 130 SetOmniboxText(text); | |
| 131 observer.WaitUntilDesiredPreviewState(); | |
| 132 } | |
| 133 | |
| 134 std::wstring WrapScript(const std::string& script) const { | |
| 135 return UTF8ToWide("domAutomationController.send(" + script + ")"); | |
| 136 } | |
| 137 | |
| 138 bool GetBoolFromJS(content::RenderViewHost* rvh, | |
| 139 const std::string& script, | |
| 140 bool* result) WARN_UNUSED_RESULT { | |
| 141 return content::ExecuteJavaScriptAndExtractBool(rvh, std::wstring(), | |
| 142 WrapScript(script), result); | |
| 143 } | |
| 144 | |
| 145 bool GetIntFromJS(content::RenderViewHost* rvh, | |
| 146 const std::string& script, | |
| 147 int* result) WARN_UNUSED_RESULT { | |
| 148 return content::ExecuteJavaScriptAndExtractInt(rvh, std::wstring(), | |
| 149 WrapScript(script), result); | |
| 150 } | |
| 151 | |
| 152 bool GetStringFromJS(content::RenderViewHost* rvh, | |
| 153 const std::string& script, | |
| 154 std::string* result) WARN_UNUSED_RESULT { | |
| 155 return content::ExecuteJavaScriptAndExtractString( | |
| 156 rvh, std::wstring(), WrapScript(script), result); | |
| 157 } | |
| 158 | |
| 159 bool UpdateSearchState(content::WebContents* contents) WARN_UNUSED_RESULT { | 17 bool UpdateSearchState(content::WebContents* contents) WARN_UNUSED_RESULT { |
| 160 content::RenderViewHost* rvh = contents->GetRenderViewHost(); | 18 content::RenderViewHost* rvh = contents->GetRenderViewHost(); |
| 161 return GetIntFromJS(rvh, "onvisibilitycalls", &onvisibilitycalls_) && | 19 return GetIntFromJS(rvh, "onvisibilitycalls", &onvisibilitycalls_) && |
| 162 GetIntFromJS(rvh, "onchangecalls", &onchangecalls_) && | 20 GetIntFromJS(rvh, "onchangecalls", &onchangecalls_) && |
| 163 GetIntFromJS(rvh, "onsubmitcalls", &onsubmitcalls_) && | 21 GetIntFromJS(rvh, "onsubmitcalls", &onsubmitcalls_) && |
| 164 GetIntFromJS(rvh, "oncancelcalls", &oncancelcalls_) && | 22 GetIntFromJS(rvh, "oncancelcalls", &oncancelcalls_) && |
| 165 GetIntFromJS(rvh, "onresizecalls", &onresizecalls_) && | 23 GetIntFromJS(rvh, "onresizecalls", &onresizecalls_) && |
| 166 GetStringFromJS(rvh, "value", &value_) && | 24 GetStringFromJS(rvh, "value", &value_) && |
| 167 GetBoolFromJS(rvh, "verbatim", &verbatim_) && | 25 GetBoolFromJS(rvh, "verbatim", &verbatim_) && |
| 168 GetIntFromJS(rvh, "height", &height_); | 26 GetIntFromJS(rvh, "height", &height_); |
| 169 } | 27 } |
| 170 | 28 |
| 171 bool ExecuteScript(const std::string& script) WARN_UNUSED_RESULT { | |
| 172 return content::ExecuteJavaScript( | |
| 173 instant()->GetPreviewContents()->GetRenderViewHost(), std::wstring(), | |
| 174 UTF8ToWide(script)); | |
| 175 } | |
| 176 | |
| 177 bool CheckVisibilityIs(content::WebContents* contents, | |
| 178 bool expected) WARN_UNUSED_RESULT { | |
| 179 bool actual = !expected; // Purposely start with a mis-match. | |
| 180 // We can only use ASSERT_*() in a method that returns void, hence this | |
| 181 // convoluted check. | |
| 182 return GetBoolFromJS(contents->GetRenderViewHost(), | |
| 183 "!document.webkitHidden", &actual) && | |
| 184 actual == expected; | |
| 185 } | |
| 186 | |
| 187 GURL instant_url_; | |
| 188 | |
| 189 int onvisibilitycalls_; | 29 int onvisibilitycalls_; |
| 190 int onchangecalls_; | 30 int onchangecalls_; |
| 191 int onsubmitcalls_; | 31 int onsubmitcalls_; |
| 192 int oncancelcalls_; | 32 int oncancelcalls_; |
| 193 int onresizecalls_; | 33 int onresizecalls_; |
| 194 | 34 |
| 195 std::string value_; | 35 std::string value_; |
| 196 bool verbatim_; | 36 bool verbatim_; |
| 197 int height_; | 37 int height_; |
| 198 }; | 38 }; |
| (...skipping 885 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1084 SetOmniboxTextAndWaitForInstantToShow("q"); | 924 SetOmniboxTextAndWaitForInstantToShow("q"); |
| 1085 EXPECT_EQ(ASCIIToUTF16("query suggestion"), omnibox()->GetText()); | 925 EXPECT_EQ(ASCIIToUTF16("query suggestion"), omnibox()->GetText()); |
| 1086 | 926 |
| 1087 // Kill the instant renderer and wait for instant support again. | 927 // Kill the instant renderer and wait for instant support again. |
| 1088 KillInstantRenderView(); | 928 KillInstantRenderView(); |
| 1089 FocusOmniboxAndWaitForInstantSupport(); | 929 FocusOmniboxAndWaitForInstantSupport(); |
| 1090 | 930 |
| 1091 SetOmniboxTextAndWaitForInstantToShow("qu"); | 931 SetOmniboxTextAndWaitForInstantToShow("qu"); |
| 1092 EXPECT_EQ(ASCIIToUTF16("query suggestion"), omnibox()->GetText()); | 932 EXPECT_EQ(ASCIIToUTF16("query suggestion"), omnibox()->GetText()); |
| 1093 } | 933 } |
| OLD | NEW |