OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/instant/instant_test_utils.h" | 5 #include "chrome/browser/instant/instant_test_utils.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/path_service.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "chrome/browser/instant/instant_controller.h" |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/instant/instant_overlay_model.h" |
| 10 #include "chrome/browser/instant/instant_overlay_model_observer.h" |
| 11 #include "chrome/browser/instant/instant_preloader.h" |
| 12 #include "chrome/browser/instant/instant_service.h" |
| 13 #include "chrome/browser/instant/instant_service_factory.h" |
10 #include "chrome/browser/search_engines/template_url_service.h" | 14 #include "chrome/browser/search_engines/template_url_service.h" |
11 #include "chrome/browser/search_engines/template_url_service_factory.h" | 15 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 16 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/browser_instant_controller.h" |
| 18 #include "chrome/browser/ui/browser_window.h" |
| 19 #include "chrome/browser/ui/omnibox/location_bar.h" |
12 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 20 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
| 21 #include "chrome/common/chrome_constants.h" |
13 #include "chrome/common/chrome_notification_types.h" | 22 #include "chrome/common/chrome_notification_types.h" |
14 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_paths.h" |
15 #include "chrome/common/pref_names.h" | 24 #include "chrome/test/base/testing_profile.h" |
16 #include "chrome/test/base/interactive_test_utils.h" | |
17 #include "chrome/test/base/ui_test_utils.h" | 25 #include "chrome/test/base/ui_test_utils.h" |
18 #include "content/public/browser/notification_service.h" | |
19 #include "content/public/browser/render_process_host.h" | 26 #include "content/public/browser/render_process_host.h" |
20 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
21 #include "content/public/common/result_codes.h" | 28 #include "content/public/common/result_codes.h" |
22 #include "content/public/test/browser_test_utils.h" | 29 #include "content/public/test/browser_test_utils.h" |
23 | 30 |
24 namespace { | 31 namespace { |
25 | 32 |
26 std::string WrapScript(const std::string& script) { | 33 std::string WrapScript(const std::string& script) { |
27 return "domAutomationController.send(" + script + ")"; | 34 return "domAutomationController.send(" + script + ")"; |
28 } | 35 } |
29 | 36 |
| 37 class InstantTestOverlayModelObserver : public InstantOverlayModelObserver { |
| 38 public: |
| 39 InstantTestOverlayModelObserver(InstantController* controller, |
| 40 bool expected_to_show); |
| 41 virtual ~InstantTestOverlayModelObserver(); |
| 42 |
| 43 void WaitForExpectedOverlayState(); |
| 44 |
| 45 private: |
| 46 // Overridden from InstantOverlayModelObserver: |
| 47 virtual void OverlayStateChanged(const InstantOverlayModel& model) OVERRIDE; |
| 48 |
| 49 bool IsExpectedState() const; |
| 50 |
| 51 InstantController* const controller_; |
| 52 const bool expected_to_show_; |
| 53 base::RunLoop run_loop_; |
| 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(InstantTestOverlayModelObserver); |
| 56 }; |
| 57 |
| 58 InstantTestOverlayModelObserver::InstantTestOverlayModelObserver( |
| 59 InstantController* controller, |
| 60 bool expected_to_show) |
| 61 : controller_(controller), |
| 62 expected_to_show_(expected_to_show) { |
| 63 controller_->AddOverlayModelObserver(this); |
| 64 if (IsExpectedState()) |
| 65 run_loop_.Quit(); |
| 66 } |
| 67 |
| 68 InstantTestOverlayModelObserver::~InstantTestOverlayModelObserver() { |
| 69 controller_->RemoveOverlayModelObserver(this); |
| 70 } |
| 71 |
| 72 void InstantTestOverlayModelObserver::WaitForExpectedOverlayState() { |
| 73 run_loop_.Run(); |
| 74 } |
| 75 |
| 76 void InstantTestOverlayModelObserver::OverlayStateChanged( |
| 77 const InstantOverlayModel& /* model */) { |
| 78 if (IsExpectedState()) |
| 79 run_loop_.Quit(); |
| 80 } |
| 81 |
| 82 bool InstantTestOverlayModelObserver::IsExpectedState() const { |
| 83 return expected_to_show_ == (controller_->model()->contents() != NULL); |
| 84 } |
| 85 |
30 } // namespace | 86 } // namespace |
31 | 87 |
32 // InstantTestModelObserver -------------------------------------------------- | 88 // InstantTestBase ------------------------------------------------------------ |
33 | 89 |
34 InstantTestModelObserver::InstantTestModelObserver( | 90 InstantTestBase::InstantTestBase() |
35 InstantOverlayModel* model, | 91 : https_test_server_( |
36 chrome::search::Mode::Type desired_mode_type) | 92 net::TestServer::TYPE_HTTPS, |
37 : model_(model), | 93 net::BaseTestServer::SSLOptions(), |
38 desired_mode_type_(desired_mode_type) { | 94 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))), |
39 model_->AddObserver(this); | 95 browser_(NULL) { |
40 } | 96 } |
41 | 97 |
42 InstantTestModelObserver::~InstantTestModelObserver() { | 98 InstantTestBase::~InstantTestBase() { |
43 model_->RemoveObserver(this); | |
44 } | 99 } |
45 | 100 |
46 void InstantTestModelObserver::WaitForDesiredOverlayState() { | 101 // Set up the profile dir to explicitly null out any Instant URL. Without this, |
47 run_loop_.Run(); | 102 // the dir ends up with a default "google.com" Instant URL which we might try |
| 103 // to load at startup. |
| 104 bool InstantTestBase::DisableInstantOnStartup() { |
| 105 base::FilePath profile_dir; |
| 106 if (!PathService::Get(chrome::DIR_USER_DATA, &profile_dir)) |
| 107 return false; |
| 108 |
| 109 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir); |
| 110 if (!file_util::CreateDirectory(profile_dir)) |
| 111 return false; |
| 112 |
| 113 base::FilePath source_dir; |
| 114 if (!PathService::Get(chrome::DIR_TEST_DATA, &source_dir)) |
| 115 return false; |
| 116 |
| 117 source_dir = source_dir.AppendASCII("profiles").AppendASCII("instant"); |
| 118 |
| 119 if (!file_util::CopyFile(source_dir.AppendASCII("Preferences"), |
| 120 profile_dir.Append(chrome::kPreferencesFilename))) |
| 121 return false; |
| 122 |
| 123 if (!file_util::CopyFile(source_dir.AppendASCII("Web Data"), |
| 124 profile_dir.Append(chrome::kWebDataFilename))) |
| 125 return false; |
| 126 |
| 127 return true; |
48 } | 128 } |
49 | 129 |
50 void InstantTestModelObserver::OverlayStateChanged( | 130 void InstantTestBase::SetupDefaultSearchProvider( |
51 const InstantOverlayModel& model) { | 131 const TemplateURLData& template_data) { |
52 if (model.mode().mode == desired_mode_type_) | |
53 run_loop_.Quit(); | |
54 } | |
55 | |
56 // InstantTestBase ----------------------------------------------------------- | |
57 | |
58 void InstantTestBase::SetupInstant(Browser* browser) { | |
59 browser_ = browser; | |
60 TemplateURLService* service = | 132 TemplateURLService* service = |
61 TemplateURLServiceFactory::GetForProfile(browser_->profile()); | 133 TemplateURLServiceFactory::GetForProfile(browser_->profile()); |
62 ui_test_utils::WaitForTemplateURLServiceToLoad(service); | 134 ui_test_utils::WaitForTemplateURLServiceToLoad(service); |
63 | 135 |
64 TemplateURLData data; | 136 TemplateURL* template_url = |
65 // Necessary to use exact URL for both the main URL and the alternate URL for | 137 new TemplateURL(browser_->profile(), template_data); |
66 // search term extraction to work in InstantExtended. | |
67 data.SetURL(instant_url_.spec() + "q={searchTerms}"); | |
68 data.instant_url = instant_url_.spec(); | |
69 data.alternate_urls.push_back(instant_url_.spec() + "#q={searchTerms}"); | |
70 data.search_terms_replacement_key = "strk"; | |
71 | |
72 TemplateURL* template_url = new TemplateURL(browser_->profile(), data); | |
73 service->Add(template_url); // Takes ownership of |template_url|. | 138 service->Add(template_url); // Takes ownership of |template_url|. |
74 service->SetDefaultSearchProvider(template_url); | 139 service->SetDefaultSearchProvider(template_url); |
75 | |
76 browser_->profile()->GetPrefs()->SetBoolean(prefs::kInstantEnabled, true); | |
77 | |
78 // TODO(shishir): Fix this ugly hack. | |
79 instant()->SetInstantEnabled(false, true); | |
80 instant()->SetInstantEnabled(true, false); | |
81 } | 140 } |
82 | 141 |
83 void InstantTestBase::Init(const GURL& instant_url) { | 142 InstantService* InstantTestBase::service() const { |
84 instant_url_ = instant_url; | 143 return InstantServiceFactory::GetForProfile(browser_->profile()); |
85 } | 144 } |
86 | 145 |
87 void InstantTestBase::KillInstantRenderView() { | 146 InstantController* InstantTestBase::instant() const { |
88 base::KillProcess( | 147 return browser_->instant_controller()->instant(); |
89 instant()->GetOverlayContents()->GetRenderProcessHost()->GetHandle(), | 148 } |
90 content::RESULT_CODE_KILLED, | 149 |
91 false); | 150 OmniboxView* InstantTestBase::omnibox() const { |
| 151 return browser_->window()->GetLocationBar()->GetLocationEntry(); |
92 } | 152 } |
93 | 153 |
94 void InstantTestBase::FocusOmnibox() { | 154 void InstantTestBase::FocusOmnibox() { |
95 // If the omnibox already has focus, just notify Instant. | 155 // If the omnibox already has focus, just notify Instant. |
96 if (omnibox()->model()->has_focus()) { | 156 if (omnibox()->model()->has_focus()) { |
97 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, | 157 instant()->OmniboxFocusChanged(OMNIBOX_FOCUS_VISIBLE, |
98 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); | 158 OMNIBOX_FOCUS_CHANGE_EXPLICIT, NULL); |
99 } else { | 159 } else { |
100 browser_->window()->GetLocationBar()->FocusLocation(false); | 160 browser_->window()->GetLocationBar()->FocusLocation(false); |
101 } | 161 } |
102 } | 162 } |
103 | 163 |
| 164 void InstantTestBase::WaitForInstantSupport() { |
| 165 if (!service()->preloader()->supports_instant()) { |
| 166 content::WindowedNotificationObserver( |
| 167 chrome::NOTIFICATION_INSTANT_SUPPORT_DETERMINED, |
| 168 content::NotificationService::AllSources()). |
| 169 Wait(); |
| 170 } |
| 171 } |
| 172 |
104 void InstantTestBase::FocusOmniboxAndWaitForInstantSupport() { | 173 void InstantTestBase::FocusOmniboxAndWaitForInstantSupport() { |
105 content::WindowedNotificationObserver observer( | |
106 chrome::NOTIFICATION_INSTANT_OVERLAY_SUPPORT_DETERMINED, | |
107 content::NotificationService::AllSources()); | |
108 FocusOmnibox(); | 174 FocusOmnibox(); |
109 observer.Wait(); | 175 WaitForInstantSupport(); |
110 } | |
111 | |
112 void InstantTestBase::FocusOmniboxAndWaitForInstantExtendedSupport() { | |
113 content::WindowedNotificationObserver ntp_observer( | |
114 chrome::NOTIFICATION_INSTANT_NTP_SUPPORT_DETERMINED, | |
115 content::NotificationService::AllSources()); | |
116 content::WindowedNotificationObserver overlay_observer( | |
117 chrome::NOTIFICATION_INSTANT_OVERLAY_SUPPORT_DETERMINED, | |
118 content::NotificationService::AllSources()); | |
119 FocusOmnibox(); | |
120 ntp_observer.Wait(); | |
121 overlay_observer.Wait(); | |
122 } | 176 } |
123 | 177 |
124 void InstantTestBase::SetOmniboxText(const std::string& text) { | 178 void InstantTestBase::SetOmniboxText(const std::string& text) { |
125 FocusOmnibox(); | |
126 omnibox()->SetUserText(UTF8ToUTF16(text)); | 179 omnibox()->SetUserText(UTF8ToUTF16(text)); |
127 } | 180 } |
128 | 181 |
| 182 void InstantTestBase::WaitForOverlayToShow() { |
| 183 InstantTestOverlayModelObserver(instant(), true). |
| 184 WaitForExpectedOverlayState(); |
| 185 } |
| 186 |
| 187 void InstantTestBase::WaitForOverlayToHide() { |
| 188 InstantTestOverlayModelObserver(instant(), false). |
| 189 WaitForExpectedOverlayState(); |
| 190 } |
| 191 |
129 void InstantTestBase::SetOmniboxTextAndWaitForOverlayToShow( | 192 void InstantTestBase::SetOmniboxTextAndWaitForOverlayToShow( |
130 const std::string& text) { | 193 const std::string& text) { |
131 InstantTestModelObserver observer( | |
132 instant()->model(), chrome::search::Mode::MODE_SEARCH_SUGGESTIONS); | |
133 SetOmniboxText(text); | 194 SetOmniboxText(text); |
134 observer.WaitForDesiredOverlayState(); | 195 WaitForOverlayToShow(); |
135 } | 196 } |
136 | 197 |
137 bool InstantTestBase::GetBoolFromJS(content::WebContents* contents, | 198 bool InstantTestBase::GetBoolFromJS(content::WebContents* contents, |
138 const std::string& script, | 199 const std::string& script, |
139 bool* result) { | 200 bool* result) { |
140 return content::ExecuteScriptAndExtractBool( | 201 return content::ExecuteScriptAndExtractBool( |
141 contents, WrapScript(script), result); | 202 contents, WrapScript(script), result); |
142 } | 203 } |
143 | 204 |
144 bool InstantTestBase::GetIntFromJS(content::WebContents* contents, | 205 bool InstantTestBase::GetIntFromJS(content::WebContents* contents, |
145 const std::string& script, | 206 const std::string& script, |
146 int* result) { | 207 int* result) { |
147 return content::ExecuteScriptAndExtractInt( | 208 return content::ExecuteScriptAndExtractInt( |
148 contents, WrapScript(script), result); | 209 contents, WrapScript(script), result); |
149 } | 210 } |
150 | 211 |
151 bool InstantTestBase::GetStringFromJS(content::WebContents* contents, | 212 bool InstantTestBase::GetStringFromJS(content::WebContents* contents, |
152 const std::string& script, | 213 const std::string& script, |
153 std::string* result) { | 214 std::string* result) { |
154 return content::ExecuteScriptAndExtractString( | 215 return content::ExecuteScriptAndExtractString( |
155 contents, WrapScript(script), result); | 216 contents, WrapScript(script), result); |
156 } | 217 } |
157 | 218 |
158 bool InstantTestBase::ExecuteScript(const std::string& script) { | |
159 return content::ExecuteScript(instant()->GetOverlayContents(), script); | |
160 } | |
161 | |
162 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents, | 219 bool InstantTestBase::CheckVisibilityIs(content::WebContents* contents, |
163 bool expected) { | 220 bool expected) { |
164 bool actual = !expected; // Purposely start with a mis-match. | 221 bool actual = !expected; // Purposely start with a mis-match. |
165 // We can only use ASSERT_*() in a method that returns void, hence this | 222 // We can only use ASSERT_*() in a method that returns void, hence this |
166 // convoluted check. | 223 // convoluted check. |
167 return GetBoolFromJS(contents, "!document.webkitHidden", &actual) && | 224 return GetBoolFromJS(contents, "!document.webkitHidden", &actual) && |
168 actual == expected; | 225 actual == expected; |
169 } | 226 } |
170 | 227 |
171 bool InstantTestBase::HasUserInputInProgress() { | 228 void InstantTestBase::KillOverlayRenderView() { |
172 return omnibox()->model()->user_input_in_progress_; | 229 base::KillProcess( |
| 230 instant()->GetOverlayContents()->GetRenderProcessHost()->GetHandle(), |
| 231 content::RESULT_CODE_KILLED, |
| 232 false); |
173 } | 233 } |
174 | 234 |
175 bool InstantTestBase::HasTemporaryText() { | 235 bool InstantTestBase::LoadImage(content::WebContents* contents, |
176 return omnibox()->model()->has_temporary_text_; | 236 const std::string& image_url, |
177 } | |
178 | |
179 bool InstantTestBase::LoadImage(content::RenderViewHost* rvh, | |
180 const std::string& image, | |
181 bool* loaded) { | 237 bool* loaded) { |
182 std::string js_chrome = | 238 std::string script = |
183 "var img = document.createElement('img');" | 239 "var img = document.createElement('img');" |
184 "img.onerror = function() { domAutomationController.send(false); };" | 240 "img.onerror = function() { domAutomationController.send(false); };" |
185 "img.onload = function() { domAutomationController.send(true); };" | 241 "img.onload = function() { domAutomationController.send(true); };" |
186 "img.src = '" + image + "';"; | 242 "img.src = '" + image_url + "';"; |
187 return content::ExecuteScriptAndExtractBool(rvh, js_chrome, loaded); | 243 return content::ExecuteScriptAndExtractBool(contents, script, loaded); |
188 } | 244 } |
OLD | NEW |