OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/metrics/field_trial.h" | 6 #include "base/metrics/field_trial.h" |
7 #include "base/metrics/histogram_base.h" | 7 #include "base/metrics/histogram_base.h" |
8 #include "base/metrics/histogram_samples.h" | 8 #include "base/metrics/histogram_samples.h" |
9 #include "base/metrics/statistics_recorder.h" | 9 #include "base/metrics/statistics_recorder.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "chrome/browser/search/instant_service.h" | 11 #include "chrome/browser/search/instant_service.h" |
12 #include "chrome/browser/search/instant_service_factory.h" | 12 #include "chrome/browser/search/instant_service_factory.h" |
13 #include "chrome/browser/search/search.h" | 13 #include "chrome/browser/search/search.h" |
| 14 #include "chrome/browser/search/search_test_utils.h" |
14 #include "chrome/browser/search_engines/search_terms_data.h" | 15 #include "chrome/browser/search_engines/search_terms_data.h" |
15 #include "chrome/browser/search_engines/template_url_service.h" | 16 #include "chrome/browser/search_engines/template_url_service.h" |
16 #include "chrome/browser/search_engines/template_url_service_factory.h" | 17 #include "chrome/browser/search_engines/template_url_service_factory.h" |
17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
18 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/metrics/entropy_provider.h" | 20 #include "chrome/common/metrics/entropy_provider.h" |
20 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
21 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
22 #include "chrome/test/base/browser_with_test_window_test.h" | 23 #include "chrome/test/base/browser_with_test_window_test.h" |
23 #include "chrome/test/base/ui_test_utils.h" | 24 #include "chrome/test/base/ui_test_utils.h" |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 protected: | 178 protected: |
178 virtual void SetUp() OVERRIDE { | 179 virtual void SetUp() OVERRIDE { |
179 BrowserWithTestWindowTest::SetUp(); | 180 BrowserWithTestWindowTest::SetUp(); |
180 field_trial_list_.reset(new base::FieldTrialList( | 181 field_trial_list_.reset(new base::FieldTrialList( |
181 new metrics::SHA1EntropyProvider("42"))); | 182 new metrics::SHA1EntropyProvider("42"))); |
182 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 183 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
183 profile(), &TemplateURLServiceFactory::BuildInstanceFor); | 184 profile(), &TemplateURLServiceFactory::BuildInstanceFor); |
184 TemplateURLService* template_url_service = | 185 TemplateURLService* template_url_service = |
185 TemplateURLServiceFactory::GetForProfile(profile()); | 186 TemplateURLServiceFactory::GetForProfile(profile()); |
186 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service); | 187 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service); |
187 SetSearchProvider(); | 188 search_test_utils::SetSearchProvider(profile()); |
188 } | |
189 | |
190 void SetSearchProvider() { | |
191 TemplateURLService* template_url_service = | |
192 TemplateURLServiceFactory::GetForProfile(profile()); | |
193 TemplateURLData data; | |
194 data.SetURL("http://foo.com/url?bar={searchTerms}"); | |
195 data.instant_url = "http://foo.com/instant?" | |
196 "{google:omniboxStartMarginParameter}foo=foo#foo=foo&strk"; | |
197 data.alternate_urls.push_back("http://foo.com/alt#quux={searchTerms}"); | |
198 data.search_terms_replacement_key = "strk"; | |
199 | |
200 TemplateURL* template_url = new TemplateURL(profile(), data); | |
201 // Takes ownership of |template_url|. | |
202 template_url_service->Add(template_url); | |
203 template_url_service->SetDefaultSearchProvider(template_url); | |
204 } | |
205 | |
206 // Build an Instant URL with or without a valid search terms replacement key | |
207 // as per |has_search_term_replacement_key|. Set that URL as the instant URL | |
208 // for the default search provider. | |
209 void SetDefaultInstantTemplateUrl(bool has_search_term_replacement_key) { | |
210 TemplateURLService* template_url_service = | |
211 TemplateURLServiceFactory::GetForProfile(profile()); | |
212 | |
213 static const char kInstantURLWithStrk[] = | |
214 "http://foo.com/instant?foo=foo#foo=foo&strk"; | |
215 static const char kInstantURLNoStrk[] = | |
216 "http://foo.com/instant?foo=foo#foo=foo"; | |
217 | |
218 TemplateURLData data; | |
219 data.SetURL("http://foo.com/url?bar={searchTerms}"); | |
220 data.instant_url = (has_search_term_replacement_key ? | |
221 kInstantURLWithStrk : kInstantURLNoStrk); | |
222 data.search_terms_replacement_key = "strk"; | |
223 | |
224 TemplateURL* template_url = new TemplateURL(profile(), data); | |
225 // Takes ownership of |template_url|. | |
226 template_url_service->Add(template_url); | |
227 template_url_service->SetDefaultSearchProvider(template_url); | |
228 } | 189 } |
229 | 190 |
230 bool InInstantProcess(const content::WebContents* contents) { | 191 bool InInstantProcess(const content::WebContents* contents) { |
231 InstantService* instant_service = | 192 InstantService* instant_service = |
232 InstantServiceFactory::GetForProfile(profile()); | 193 InstantServiceFactory::GetForProfile(profile()); |
233 return instant_service->IsInstantProcess( | 194 return instant_service->IsInstantProcess( |
234 contents->GetRenderProcessHost()->GetID()); | 195 contents->GetRenderProcessHost()->GetID()); |
235 } | 196 } |
236 | 197 |
237 scoped_ptr<base::FieldTrialList> field_trial_list_; | 198 scoped_ptr<base::FieldTrialList> field_trial_list_; |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 << test.url << " " << test.comment; | 477 << test.url << " " << test.comment; |
517 } | 478 } |
518 } | 479 } |
519 | 480 |
520 TEST_F(SearchTest, GetInstantURLExtendedEnabled) { | 481 TEST_F(SearchTest, GetInstantURLExtendedEnabled) { |
521 // Instant is disabled, so no Instant URL. | 482 // Instant is disabled, so no Instant URL. |
522 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); | 483 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); |
523 | 484 |
524 // Enable Instant. Still no Instant URL because "strk" is missing. | 485 // Enable Instant. Still no Instant URL because "strk" is missing. |
525 EnableInstantExtendedAPIForTesting(); | 486 EnableInstantExtendedAPIForTesting(); |
526 SetDefaultInstantTemplateUrl(false); | 487 search_test_utils::SetDefaultInstantTemplateUrl(profile(), false); |
527 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); | 488 EXPECT_EQ(GURL(), GetInstantURL(profile(), kDisableStartMargin)); |
528 | 489 |
529 // Set an Instant URL with a valid search terms replacement key. | 490 // Set an Instant URL with a valid search terms replacement key. |
530 SetDefaultInstantTemplateUrl(true); | 491 search_test_utils::SetDefaultInstantTemplateUrl(profile(), true); |
531 | 492 |
532 // Now there should be a valid Instant URL. Note the HTTPS "upgrade". | 493 // Now there should be a valid Instant URL. Note the HTTPS "upgrade". |
533 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"), | 494 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"), |
534 GetInstantURL(profile(), kDisableStartMargin)); | 495 GetInstantURL(profile(), kDisableStartMargin)); |
535 | 496 |
536 // Enable suggest. No difference. | 497 // Enable suggest. No difference. |
537 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); | 498 profile()->GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); |
538 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"), | 499 EXPECT_EQ(GURL("https://foo.com/instant?foo=foo#foo=foo&strk"), |
539 GetInstantURL(profile(), kDisableStartMargin)); | 500 GetInstantURL(profile(), kDisableStartMargin)); |
540 | 501 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 } | 581 } |
621 | 582 |
622 TEST_F(SearchTest, ShouldShowInstantNTP_DisabledByInstantNewTabURLSwitch) { | 583 TEST_F(SearchTest, ShouldShowInstantNTP_DisabledByInstantNewTabURLSwitch) { |
623 EnableInstantExtendedAPIForTesting(); | 584 EnableInstantExtendedAPIForTesting(); |
624 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 585 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
625 switches::kInstantNewTabURL, "http://example.com/newtab"); | 586 switches::kInstantNewTabURL, "http://example.com/newtab"); |
626 EXPECT_FALSE(ShouldShowInstantNTP()); | 587 EXPECT_FALSE(ShouldShowInstantNTP()); |
627 } | 588 } |
628 | 589 |
629 } // namespace chrome | 590 } // namespace chrome |
OLD | NEW |