| 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" |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 protected: | 171 protected: |
| 172 virtual void SetUp() OVERRIDE { | 172 virtual void SetUp() OVERRIDE { |
| 173 BrowserWithTestWindowTest::SetUp(); | 173 BrowserWithTestWindowTest::SetUp(); |
| 174 field_trial_list_.reset(new base::FieldTrialList( | 174 field_trial_list_.reset(new base::FieldTrialList( |
| 175 new metrics::SHA1EntropyProvider("42"))); | 175 new metrics::SHA1EntropyProvider("42"))); |
| 176 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 176 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 177 profile(), &TemplateURLServiceFactory::BuildInstanceFor); | 177 profile(), &TemplateURLServiceFactory::BuildInstanceFor); |
| 178 TemplateURLService* template_url_service = | 178 TemplateURLService* template_url_service = |
| 179 TemplateURLServiceFactory::GetForProfile(profile()); | 179 TemplateURLServiceFactory::GetForProfile(profile()); |
| 180 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service); | 180 ui_test_utils::WaitForTemplateURLServiceToLoad(template_url_service); |
| 181 SetSearchProvider(false); | 181 SetSearchProvider(); |
| 182 } | 182 } |
| 183 | 183 |
| 184 void SetSearchProvider(bool is_google) { | 184 void SetSearchProvider() { |
| 185 TemplateURLService* template_url_service = | 185 TemplateURLService* template_url_service = |
| 186 TemplateURLServiceFactory::GetForProfile(profile()); | 186 TemplateURLServiceFactory::GetForProfile(profile()); |
| 187 TemplateURLData data; | 187 TemplateURLData data; |
| 188 if (is_google) { | 188 data.SetURL("http://foo.com/url?bar={searchTerms}"); |
| 189 data.SetURL("http://www.google.com/"); | 189 data.instant_url = "http://foo.com/instant?" |
| 190 data.instant_url = "http://www.google.com/"; | 190 "{google:omniboxStartMarginParameter}foo=foo#foo=foo&strk"; |
| 191 } else { | 191 data.alternate_urls.push_back("http://foo.com/alt#quux={searchTerms}"); |
| 192 data.SetURL("http://foo.com/url?bar={searchTerms}"); | 192 data.search_terms_replacement_key = "strk"; |
| 193 data.instant_url = "http://foo.com/instant?" | 193 |
| 194 "{google:omniboxStartMarginParameter}foo=foo#foo=foo&strk"; | |
| 195 data.alternate_urls.push_back("http://foo.com/alt#quux={searchTerms}"); | |
| 196 data.search_terms_replacement_key = "strk"; | |
| 197 } | |
| 198 TemplateURL* template_url = new TemplateURL(profile(), data); | 194 TemplateURL* template_url = new TemplateURL(profile(), data); |
| 199 // Takes ownership of |template_url|. | 195 // Takes ownership of |template_url|. |
| 200 template_url_service->Add(template_url); | 196 template_url_service->Add(template_url); |
| 201 template_url_service->SetDefaultSearchProvider(template_url); | 197 template_url_service->SetDefaultSearchProvider(template_url); |
| 202 } | 198 } |
| 203 | 199 |
| 204 // Build an Instant URL with or without a valid search terms replacement key | 200 // Build an Instant URL with or without a valid search terms replacement key |
| 205 // as per |has_search_term_replacement_key|. Set that URL as the instant URL | 201 // as per |has_search_term_replacement_key|. Set that URL as the instant URL |
| 206 // for the default search provider. | 202 // for the default search provider. |
| 207 void SetDefaultInstantTemplateUrl(bool has_search_term_replacement_key) { | 203 void SetDefaultInstantTemplateUrl(bool has_search_term_replacement_key) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 ShouldAssignURLToInstantRenderer(GURL(test.url), profile())) | 260 ShouldAssignURLToInstantRenderer(GURL(test.url), profile())) |
| 265 << test.url << " " << test.comment; | 261 << test.url << " " << test.comment; |
| 266 } | 262 } |
| 267 } | 263 } |
| 268 | 264 |
| 269 TEST_F(SearchTest, ShouldAssignURLToInstantRendererExtendedEnabled) { | 265 TEST_F(SearchTest, ShouldAssignURLToInstantRendererExtendedEnabled) { |
| 270 EnableInstantExtendedAPIForTesting(); | 266 EnableInstantExtendedAPIForTesting(); |
| 271 | 267 |
| 272 const SearchTestCase kTestCases[] = { | 268 const SearchTestCase kTestCases[] = { |
| 273 {chrome::kChromeSearchLocalNtpUrl, true, ""}, | 269 {chrome::kChromeSearchLocalNtpUrl, true, ""}, |
| 274 {chrome::kChromeSearchLocalGoogleNtpUrl, true, ""}, | |
| 275 {"https://foo.com/instant?strk", true, ""}, | 270 {"https://foo.com/instant?strk", true, ""}, |
| 276 {"https://foo.com/instant#strk", true, ""}, | 271 {"https://foo.com/instant#strk", true, ""}, |
| 277 {"https://foo.com/instant?strk=0", true, ""}, | 272 {"https://foo.com/instant?strk=0", true, ""}, |
| 278 {"https://foo.com/url?strk", true, ""}, | 273 {"https://foo.com/url?strk", true, ""}, |
| 279 {"https://foo.com/alt?strk", true, ""}, | 274 {"https://foo.com/alt?strk", true, ""}, |
| 280 {"http://foo.com/instant", false, "Non-HTTPS"}, | 275 {"http://foo.com/instant", false, "Non-HTTPS"}, |
| 281 {"http://foo.com/instant?strk", false, "Non-HTTPS"}, | 276 {"http://foo.com/instant?strk", false, "Non-HTTPS"}, |
| 282 {"http://foo.com/instant?strk=1", false, "Non-HTTPS"}, | 277 {"http://foo.com/instant?strk=1", false, "Non-HTTPS"}, |
| 283 {"https://foo.com/instant", false, "No search terms replacement"}, | 278 {"https://foo.com/instant", false, "No search terms replacement"}, |
| 284 {"https://foo.com/?strk", false, "Non-exact path"}, | 279 {"https://foo.com/?strk", false, "Non-exact path"}, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 301 {"https://foo.com/alt#strk", true, "Valid alternative URL"}, | 296 {"https://foo.com/alt#strk", true, "Valid alternative URL"}, |
| 302 {"https://foo.com/url?strk&bar=", true, "No query terms"}, | 297 {"https://foo.com/url?strk&bar=", true, "No query terms"}, |
| 303 {"https://foo.com/url?strk&q=abc", true, "No query terms key"}, | 298 {"https://foo.com/url?strk&q=abc", true, "No query terms key"}, |
| 304 {"https://foo.com/url?strk#bar=abc", true, "Query terms key in ref"}, | 299 {"https://foo.com/url?strk#bar=abc", true, "Query terms key in ref"}, |
| 305 {"https://foo.com/url?strk&bar=abc", false, "Has query terms"}, | 300 {"https://foo.com/url?strk&bar=abc", false, "Has query terms"}, |
| 306 {"http://foo.com/instant?strk=1", false, "Insecure URL"}, | 301 {"http://foo.com/instant?strk=1", false, "Insecure URL"}, |
| 307 {"https://foo.com/instant", false, "No search term replacement"}, | 302 {"https://foo.com/instant", false, "No search term replacement"}, |
| 308 {"chrome://blank/", false, "Chrome scheme"}, | 303 {"chrome://blank/", false, "Chrome scheme"}, |
| 309 {"chrome-search://foo", false, "Chrome-search scheme"}, | 304 {"chrome-search://foo", false, "Chrome-search scheme"}, |
| 310 {chrome::kChromeSearchLocalNtpUrl, true, "Local new tab page"}, | 305 {chrome::kChromeSearchLocalNtpUrl, true, "Local new tab page"}, |
| 311 {chrome::kChromeSearchLocalGoogleNtpUrl, true, "Local new tab page"}, | |
| 312 {"https://bar.com/instant?strk=1", false, "Random non-search page"}, | 306 {"https://bar.com/instant?strk=1", false, "Random non-search page"}, |
| 313 }; | 307 }; |
| 314 | 308 |
| 315 TEST_F(SearchTest, InstantNTPExtendedEnabled) { | 309 TEST_F(SearchTest, InstantNTPExtendedEnabled) { |
| 316 EnableInstantExtendedAPIForTesting(); | 310 EnableInstantExtendedAPIForTesting(); |
| 317 AddTab(browser(), GURL("chrome://blank")); | 311 AddTab(browser(), GURL("chrome://blank")); |
| 318 for (size_t i = 0; i < arraysize(kInstantNTPTestCases); ++i) { | 312 for (size_t i = 0; i < arraysize(kInstantNTPTestCases); ++i) { |
| 319 const SearchTestCase& test = kInstantNTPTestCases[i]; | 313 const SearchTestCase& test = kInstantNTPTestCases[i]; |
| 320 NavigateAndCommitActiveTab(GURL(test.url)); | 314 NavigateAndCommitActiveTab(GURL(test.url)); |
| 321 SetSearchProvider(test.url == chrome::kChromeSearchLocalGoogleNtpUrl); | |
| 322 const content::WebContents* contents = | 315 const content::WebContents* contents = |
| 323 browser()->tab_strip_model()->GetWebContentsAt(0); | 316 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 324 EXPECT_EQ(test.expected_result, IsInstantNTP(contents)) | 317 EXPECT_EQ(test.expected_result, IsInstantNTP(contents)) |
| 325 << test.url << " " << test.comment; | 318 << test.url << " " << test.comment; |
| 326 } | 319 } |
| 327 } | 320 } |
| 328 | 321 |
| 329 TEST_F(SearchTest, InstantNTPExtendedDisabled) { | 322 TEST_F(SearchTest, InstantNTPExtendedDisabled) { |
| 330 AddTab(browser(), GURL("chrome://blank")); | 323 AddTab(browser(), GURL("chrome://blank")); |
| 331 for (size_t i = 0; i < arraysize(kInstantNTPTestCases); ++i) { | 324 for (size_t i = 0; i < arraysize(kInstantNTPTestCases); ++i) { |
| 332 const SearchTestCase& test = kInstantNTPTestCases[i]; | 325 const SearchTestCase& test = kInstantNTPTestCases[i]; |
| 333 NavigateAndCommitActiveTab(GURL(test.url)); | 326 NavigateAndCommitActiveTab(GURL(test.url)); |
| 334 const content::WebContents* contents = | 327 const content::WebContents* contents = |
| 335 browser()->tab_strip_model()->GetWebContentsAt(0); | 328 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 336 EXPECT_FALSE(IsInstantNTP(contents)) << test.url << " " << test.comment; | 329 EXPECT_FALSE(IsInstantNTP(contents)) << test.url << " " << test.comment; |
| 337 } | 330 } |
| 338 } | 331 } |
| 339 | 332 |
| 340 TEST_F(SearchTest, InstantNTPCustomNavigationEntry) { | 333 TEST_F(SearchTest, InstantNTPCustomNavigationEntry) { |
| 341 EnableInstantExtendedAPIForTesting(); | 334 EnableInstantExtendedAPIForTesting(); |
| 342 AddTab(browser(), GURL("chrome://blank")); | 335 AddTab(browser(), GURL("chrome://blank")); |
| 343 for (size_t i = 0; i < arraysize(kInstantNTPTestCases); ++i) { | 336 for (size_t i = 0; i < arraysize(kInstantNTPTestCases); ++i) { |
| 344 const SearchTestCase& test = kInstantNTPTestCases[i]; | 337 const SearchTestCase& test = kInstantNTPTestCases[i]; |
| 345 NavigateAndCommitActiveTab(GURL(test.url)); | 338 NavigateAndCommitActiveTab(GURL(test.url)); |
| 346 SetSearchProvider(test.url == chrome::kChromeSearchLocalGoogleNtpUrl); | |
| 347 content::WebContents* contents = | 339 content::WebContents* contents = |
| 348 browser()->tab_strip_model()->GetWebContentsAt(0); | 340 browser()->tab_strip_model()->GetWebContentsAt(0); |
| 349 content::NavigationController& controller = contents->GetController(); | 341 content::NavigationController& controller = contents->GetController(); |
| 350 controller.SetTransientEntry( | 342 controller.SetTransientEntry( |
| 351 controller.CreateNavigationEntry(GURL("chrome://blank"), | 343 controller.CreateNavigationEntry(GURL("chrome://blank"), |
| 352 content::Referrer(), | 344 content::Referrer(), |
| 353 content::PAGE_TRANSITION_LINK, | 345 content::PAGE_TRANSITION_LINK, |
| 354 false, | 346 false, |
| 355 std::string(), | 347 std::string(), |
| 356 contents->GetBrowserContext())); | 348 contents->GetBrowserContext())); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 GetInstantURL(profile(), kDisableStartMargin)); | 393 GetInstantURL(profile(), kDisableStartMargin)); |
| 402 | 394 |
| 403 // With start margin. | 395 // With start margin. |
| 404 EXPECT_EQ(GURL("https://foo.com/instant?es_sm=10&foo=foo#foo=foo&strk"), | 396 EXPECT_EQ(GURL("https://foo.com/instant?es_sm=10&foo=foo#foo=foo&strk"), |
| 405 GetInstantURL(profile(), 10)); | 397 GetInstantURL(profile(), 10)); |
| 406 } | 398 } |
| 407 | 399 |
| 408 TEST_F(SearchTest, CommandLineOverrides) { | 400 TEST_F(SearchTest, CommandLineOverrides) { |
| 409 EnableInstantExtendedAPIForTesting(); | 401 EnableInstantExtendedAPIForTesting(); |
| 410 | 402 |
| 411 // GetLocalInstantURL() should default to the non-Google local NTP. | |
| 412 SetSearchProvider(false); | |
| 413 GURL local_instant_url(GetLocalInstantURL(profile())); | 403 GURL local_instant_url(GetLocalInstantURL(profile())); |
| 414 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), local_instant_url); | 404 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), local_instant_url); |
| 415 | 405 |
| 416 TemplateURLService* template_url_service = | 406 TemplateURLService* template_url_service = |
| 417 TemplateURLServiceFactory::GetForProfile(profile()); | 407 TemplateURLServiceFactory::GetForProfile(profile()); |
| 418 TemplateURLData data; | 408 TemplateURLData data; |
| 419 data.SetURL("{google:baseURL}search?q={searchTerms}"); | 409 data.SetURL("{google:baseURL}search?q={searchTerms}"); |
| 420 data.instant_url = "{google:baseURL}webhp?strk"; | 410 data.instant_url = "{google:baseURL}webhp?strk"; |
| 421 data.search_terms_replacement_key = "strk"; | 411 data.search_terms_replacement_key = "strk"; |
| 422 TemplateURL* template_url = new TemplateURL(profile(), data); | 412 TemplateURL* template_url = new TemplateURL(profile(), data); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 437 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL, | 427 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kGoogleBaseURL, |
| 438 "http://www.bar.com/"); | 428 "http://www.bar.com/"); |
| 439 instant_url = GetInstantURL(profile(), kDisableStartMargin); | 429 instant_url = GetInstantURL(profile(), kDisableStartMargin); |
| 440 ASSERT_TRUE(instant_url.is_valid()); | 430 ASSERT_TRUE(instant_url.is_valid()); |
| 441 EXPECT_EQ("http://www.bar.com/webhp?strk", instant_url.spec()); | 431 EXPECT_EQ("http://www.bar.com/webhp?strk", instant_url.spec()); |
| 442 | 432 |
| 443 // Similarly, setting a Google base URL on the command line should allow you | 433 // Similarly, setting a Google base URL on the command line should allow you |
| 444 // to get the Google version of the local NTP, even though search provider's | 434 // to get the Google version of the local NTP, even though search provider's |
| 445 // URL doesn't contain "google". | 435 // URL doesn't contain "google". |
| 446 local_instant_url = GetLocalInstantURL(profile()); | 436 local_instant_url = GetLocalInstantURL(profile()); |
| 447 EXPECT_EQ(GURL(chrome::kChromeSearchLocalGoogleNtpUrl), local_instant_url); | 437 EXPECT_EQ(GURL(chrome::kChromeSearchLocalNtpUrl), local_instant_url); |
| 448 | 438 |
| 449 // If we specify extra search query params, they should be inserted into the | 439 // If we specify extra search query params, they should be inserted into the |
| 450 // query portion of the instant URL. | 440 // query portion of the instant URL. |
| 451 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 441 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 452 switches::kExtraSearchQueryParams, "a=b"); | 442 switches::kExtraSearchQueryParams, "a=b"); |
| 453 instant_url = GetInstantURL(profile(), kDisableStartMargin); | 443 instant_url = GetInstantURL(profile(), kDisableStartMargin); |
| 454 ASSERT_TRUE(instant_url.is_valid()); | 444 ASSERT_TRUE(instant_url.is_valid()); |
| 455 EXPECT_EQ("http://www.bar.com/webhp?a=b&strk", instant_url.spec()); | 445 EXPECT_EQ("http://www.bar.com/webhp?a=b&strk", instant_url.spec()); |
| 456 } | 446 } |
| 457 | 447 |
| 458 } // namespace chrome | 448 } // namespace chrome |
| OLD | NEW |