| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/metrics/histogram.h" | 6 #include "base/metrics/histogram.h" |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/protector/histograms.h" | 9 #include "chrome/browser/protector/histograms.h" |
| 10 #include "chrome/browser/protector/mock_protector_service.h" | 10 #include "chrome/browser/protector/mock_protector_service.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 using ::testing::Return; | 26 using ::testing::Return; |
| 27 | 27 |
| 28 namespace protector { | 28 namespace protector { |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 // Keyword names and URLs used for testing. | 32 // Keyword names and URLs used for testing. |
| 33 | 33 |
| 34 const string16 example_info = ASCIIToUTF16("Example.info"); | 34 const string16 example_info = ASCIIToUTF16("Example.info"); |
| 35 const string16 example_info_long = ASCIIToUTF16("ExampleSearchEngine.info"); | 35 const string16 example_info_long = ASCIIToUTF16("ExampleSearchEngine.info"); |
| 36 const std::string http_example_info = "http://example.info/%s"; | 36 const std::string http_example_info = "http://example.info/?q={searchTerms}"; |
| 37 const std::string example_info_domain = "example.info"; |
| 37 const string16 example_com = ASCIIToUTF16("Example.com"); | 38 const string16 example_com = ASCIIToUTF16("Example.com"); |
| 38 const string16 example_com_long = ASCIIToUTF16("ExampleSearchEngine.com"); | 39 const string16 example_com_long = ASCIIToUTF16("ExampleSearchEngine.com"); |
| 39 const std::string http_example_com = "http://example.com/%s"; | 40 const std::string http_example_com = "http://example.com/?q={searchTerms}"; |
| 41 const std::string example_com_domain = "example.com"; |
| 40 const string16 example_net = ASCIIToUTF16("Example.net"); | 42 const string16 example_net = ASCIIToUTF16("Example.net"); |
| 41 const std::string http_example_net = "http://example.net/%s"; | 43 const std::string http_example_net = "http://example.net/?q={searchTerms}"; |
| 44 const std::string example_domain = "example.net"; |
| 45 |
| 46 const BaseSettingChange::DisplayName kNoDisplayName( |
| 47 BaseSettingChange::kDefaultNamePriority, string16()); |
| 42 | 48 |
| 43 // Convenience function. | 49 // Convenience function. |
| 44 TemplateURL* MakeTemplateURL(const string16& short_name, | 50 TemplateURL* MakeTemplateURL(const string16& short_name, |
| 45 const string16& keyword, | 51 const string16& keyword, |
| 46 const std::string& url) { | 52 const std::string& url) { |
| 47 TemplateURLData data; | 53 TemplateURLData data; |
| 48 data.short_name = short_name; | 54 data.short_name = short_name; |
| 49 if (keyword.empty()) | 55 if (keyword.empty()) |
| 50 data.SetAutogenerateKeyword(true); | 56 data.SetAutogenerateKeyword(true); |
| 51 else | 57 else |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 current_histogram_id, 1); | 176 current_histogram_id, 1); |
| 171 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, | 177 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, |
| 172 backup_histogram_id, 1); | 178 backup_histogram_id, 1); |
| 173 | 179 |
| 174 // Verify text messages. | 180 // Verify text messages. |
| 175 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); | 181 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); |
| 176 EXPECT_EQ(GetChangeSearchButtonText(example_com), | 182 EXPECT_EQ(GetChangeSearchButtonText(example_com), |
| 177 change->GetApplyButtonText()); | 183 change->GetApplyButtonText()); |
| 178 EXPECT_EQ(GetKeepSearchButtonText(example_info), | 184 EXPECT_EQ(GetKeepSearchButtonText(example_info), |
| 179 change->GetDiscardButtonText()); | 185 change->GetDiscardButtonText()); |
| 186 EXPECT_EQ(example_com_domain, change->GetApplyKeyword()); |
| 187 EXPECT_EQ(example_com, change->GetApplyDisplayName().second); |
| 180 | 188 |
| 181 // Discard does nothing - backup was already active. | 189 // Discard does nothing - backup was already active. |
| 182 change->Discard(browser()); | 190 change->Discard(browser()); |
| 183 EXPECT_EQ(FindTemplateURL(http_example_info), | 191 EXPECT_EQ(FindTemplateURL(http_example_info), |
| 184 turl_service_->GetDefaultSearchProvider()); | 192 turl_service_->GetDefaultSearchProvider()); |
| 185 ExpectHistogramCount(kProtectorHistogramSearchProviderDiscarded, | 193 ExpectHistogramCount(kProtectorHistogramSearchProviderDiscarded, |
| 186 current_histogram_id, 1); | 194 current_histogram_id, 1); |
| 187 | 195 |
| 188 // Verify that Apply switches back to |current_url|. | 196 // Verify that Apply switches back to |current_url|. |
| 189 change->Apply(browser()); | 197 change->Apply(browser()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 212 scoped_ptr<BaseSettingChange> change( | 220 scoped_ptr<BaseSettingChange> change( |
| 213 CreateDefaultSearchProviderChange(current_url, backup_url_long)); | 221 CreateDefaultSearchProviderChange(current_url, backup_url_long)); |
| 214 ASSERT_TRUE(change.get()); | 222 ASSERT_TRUE(change.get()); |
| 215 ASSERT_TRUE(change->Init(browser()->profile())); | 223 ASSERT_TRUE(change->Init(browser()->profile())); |
| 216 | 224 |
| 217 // Verify text messages. | 225 // Verify text messages. |
| 218 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); | 226 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); |
| 219 EXPECT_EQ(GetChangeSearchButtonText(example_com), | 227 EXPECT_EQ(GetChangeSearchButtonText(example_com), |
| 220 change->GetApplyButtonText()); | 228 change->GetApplyButtonText()); |
| 221 EXPECT_EQ(GetKeepSearchButtonText(), change->GetDiscardButtonText()); | 229 EXPECT_EQ(GetKeepSearchButtonText(), change->GetDiscardButtonText()); |
| 230 EXPECT_EQ(example_com_domain, change->GetApplyKeyword()); |
| 231 EXPECT_EQ(example_com, change->GetApplyDisplayName().second); |
| 222 } | 232 } |
| 223 | 233 |
| 224 { | 234 { |
| 225 // Current name too long. | 235 // Current name too long. |
| 226 turl_service_->Add(new TemplateURL(backup_url->data())); | 236 turl_service_->Add(new TemplateURL(backup_url->data())); |
| 227 AddAndSetDefault(current_url_long); | 237 AddAndSetDefault(current_url_long); |
| 228 | 238 |
| 229 scoped_ptr<BaseSettingChange> change( | 239 scoped_ptr<BaseSettingChange> change( |
| 230 CreateDefaultSearchProviderChange(current_url_long, backup_url)); | 240 CreateDefaultSearchProviderChange(current_url_long, backup_url)); |
| 231 ASSERT_TRUE(change.get()); | 241 ASSERT_TRUE(change.get()); |
| 232 ASSERT_TRUE(change->Init(browser()->profile())); | 242 ASSERT_TRUE(change->Init(browser()->profile())); |
| 233 | 243 |
| 234 // Verify text messages. | 244 // Verify text messages. |
| 235 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); | 245 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); |
| 236 EXPECT_EQ(GetChangeSearchButtonText(), change->GetApplyButtonText()); | 246 EXPECT_EQ(GetChangeSearchButtonText(), change->GetApplyButtonText()); |
| 237 EXPECT_EQ(GetKeepSearchButtonText(example_info), | 247 EXPECT_EQ(GetKeepSearchButtonText(example_info), |
| 238 change->GetDiscardButtonText()); | 248 change->GetDiscardButtonText()); |
| 249 EXPECT_EQ(example_com_domain, change->GetApplyKeyword()); |
| 250 EXPECT_EQ(kNoDisplayName, change->GetApplyDisplayName()); |
| 239 } | 251 } |
| 240 } | 252 } |
| 241 | 253 |
| 242 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, BackupInvalid) { | 254 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, BackupInvalid) { |
| 243 // Backup is invalid, current search provider exists, fallback to the | 255 // Backup is invalid, current search provider exists, fallback to the |
| 244 // prepopulated default search, which exists among keywords. | 256 // prepopulated default search, which exists among keywords. |
| 245 int prepopulated_histogram_id = | 257 int prepopulated_histogram_id = |
| 246 protector::GetSearchProviderHistogramID(prepopulated_url_.get()); | 258 protector::GetSearchProviderHistogramID(prepopulated_url_.get()); |
| 247 TemplateURL* current_url = | 259 TemplateURL* current_url = |
| 248 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); | 260 MakeTemplateURL(example_com, ASCIIToUTF16("b"), http_example_com); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 prepopulated_histogram_id, 1); | 340 prepopulated_histogram_id, 1); |
| 329 ExpectHistogramCount(kProtectorHistogramSearchProviderMissing, | 341 ExpectHistogramCount(kProtectorHistogramSearchProviderMissing, |
| 330 prepopulated_histogram_id, 1); | 342 prepopulated_histogram_id, 1); |
| 331 | 343 |
| 332 // Verify text messages. | 344 // Verify text messages. |
| 333 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), | 345 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), |
| 334 change->GetBubbleMessage()); | 346 change->GetBubbleMessage()); |
| 335 EXPECT_EQ(GetChangeSearchButtonText(example_com), | 347 EXPECT_EQ(GetChangeSearchButtonText(example_com), |
| 336 change->GetApplyButtonText()); | 348 change->GetApplyButtonText()); |
| 337 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); | 349 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); |
| 350 EXPECT_EQ(std::string(), change->GetApplyKeyword()); |
| 351 EXPECT_EQ(example_com, change->GetApplyDisplayName().second); |
| 338 | 352 |
| 339 // Verify that search engine settings are opened by Discard. | 353 // Verify that search engine settings are opened by Discard. |
| 340 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | 354 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); |
| 341 change->Discard(browser()); | 355 change->Discard(browser()); |
| 342 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | 356 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), |
| 343 turl_service_->GetDefaultSearchProvider()); | 357 turl_service_->GetDefaultSearchProvider()); |
| 344 | 358 |
| 345 // Verify that Apply switches back to |current_url|. | 359 // Verify that Apply switches back to |current_url|. |
| 346 change->Apply(browser()); | 360 change->Apply(browser()); |
| 347 EXPECT_EQ(FindTemplateURL(http_example_com), | 361 EXPECT_EQ(FindTemplateURL(http_example_com), |
| (...skipping 23 matching lines...) Expand all Loading... |
| 371 ExpectHistogramCount(kProtectorHistogramSearchProviderHijacked, | 385 ExpectHistogramCount(kProtectorHistogramSearchProviderHijacked, |
| 372 protector::GetSearchProviderHistogramID(NULL), 1); | 386 protector::GetSearchProviderHistogramID(NULL), 1); |
| 373 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, | 387 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, |
| 374 backup_histogram_id, 1); | 388 backup_histogram_id, 1); |
| 375 | 389 |
| 376 // Verify text messages. | 390 // Verify text messages. |
| 377 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); | 391 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); |
| 378 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetApplyButtonText()); | 392 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetApplyButtonText()); |
| 379 EXPECT_EQ(GetKeepSearchButtonText(example_info), | 393 EXPECT_EQ(GetKeepSearchButtonText(example_info), |
| 380 change->GetDiscardButtonText()); | 394 change->GetDiscardButtonText()); |
| 395 EXPECT_EQ(std::string(), change->GetApplyKeyword()); |
| 396 EXPECT_EQ(kNoDisplayName, change->GetApplyDisplayName()); |
| 381 | 397 |
| 382 // Discard does nothing - backup was already active. | 398 // Discard does nothing - backup was already active. |
| 383 change->Discard(browser()); | 399 change->Discard(browser()); |
| 384 EXPECT_EQ(FindTemplateURL(http_example_info), | 400 EXPECT_EQ(FindTemplateURL(http_example_info), |
| 385 turl_service_->GetDefaultSearchProvider()); | 401 turl_service_->GetDefaultSearchProvider()); |
| 386 | 402 |
| 387 // Verify that search engine settings are opened by Apply (no other changes). | 403 // Verify that search engine settings are opened by Apply (no other changes). |
| 388 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | 404 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); |
| 389 change->Apply(browser()); | 405 change->Apply(browser()); |
| 390 EXPECT_EQ(FindTemplateURL(http_example_info), | 406 EXPECT_EQ(FindTemplateURL(http_example_info), |
| (...skipping 23 matching lines...) Expand all Loading... |
| 414 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, | 430 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, |
| 415 prepopulated_histogram_id, 1); | 431 prepopulated_histogram_id, 1); |
| 416 ExpectHistogramCount(kProtectorHistogramSearchProviderFallback, | 432 ExpectHistogramCount(kProtectorHistogramSearchProviderFallback, |
| 417 prepopulated_histogram_id, 1); | 433 prepopulated_histogram_id, 1); |
| 418 | 434 |
| 419 // Verify text messages. | 435 // Verify text messages. |
| 420 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), | 436 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), |
| 421 change->GetBubbleMessage()); | 437 change->GetBubbleMessage()); |
| 422 EXPECT_EQ(string16(), change->GetApplyButtonText()); | 438 EXPECT_EQ(string16(), change->GetApplyButtonText()); |
| 423 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); | 439 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); |
| 440 EXPECT_EQ(std::string(), change->GetApplyKeyword()); |
| 441 EXPECT_EQ(kNoDisplayName, change->GetApplyDisplayName()); |
| 424 | 442 |
| 425 // Verify that search engine settings are opened by Discard. | 443 // Verify that search engine settings are opened by Discard. |
| 426 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | 444 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); |
| 427 change->Discard(browser()); | 445 change->Discard(browser()); |
| 428 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), | 446 EXPECT_EQ(FindTemplateURL(prepopulated_url_->url()), |
| 429 turl_service_->GetDefaultSearchProvider()); | 447 turl_service_->GetDefaultSearchProvider()); |
| 430 } | 448 } |
| 431 | 449 |
| 432 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, | 450 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, |
| 433 BackupInvalidFallbackSameAsCurrent) { | 451 BackupInvalidFallbackSameAsCurrent) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 455 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, | 473 ExpectHistogramCount(kProtectorHistogramSearchProviderRestored, |
| 456 prepopulated_histogram_id, 1); | 474 prepopulated_histogram_id, 1); |
| 457 ExpectHistogramCount(kProtectorHistogramSearchProviderFallback, | 475 ExpectHistogramCount(kProtectorHistogramSearchProviderFallback, |
| 458 prepopulated_histogram_id, 1); | 476 prepopulated_histogram_id, 1); |
| 459 | 477 |
| 460 // Verify text messages. | 478 // Verify text messages. |
| 461 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), | 479 EXPECT_EQ(GetBubbleMessage(prepopulated_url_->short_name()), |
| 462 change->GetBubbleMessage()); | 480 change->GetBubbleMessage()); |
| 463 EXPECT_EQ(string16(), change->GetApplyButtonText()); | 481 EXPECT_EQ(string16(), change->GetApplyButtonText()); |
| 464 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); | 482 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetDiscardButtonText()); |
| 483 EXPECT_EQ(std::string(), change->GetApplyKeyword()); |
| 484 EXPECT_EQ(current_url->short_name(), change->GetApplyDisplayName().second); |
| 465 | 485 |
| 466 // Verify that search engine settings are opened by Discard. | 486 // Verify that search engine settings are opened by Discard. |
| 467 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | 487 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); |
| 468 change->Discard(browser()); | 488 change->Discard(browser()); |
| 469 EXPECT_EQ(current_url, turl_service_->GetDefaultSearchProvider()); | 489 EXPECT_EQ(current_url, turl_service_->GetDefaultSearchProvider()); |
| 470 } | 490 } |
| 471 | 491 |
| 472 | 492 |
| 473 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, | 493 IN_PROC_BROWSER_TEST_F(DefaultSearchProviderChangeTest, |
| 474 DefaultSearchProviderChangedByUser) { | 494 DefaultSearchProviderChangedByUser) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 EXPECT_EQ(FindTemplateURL(http_example_info), | 539 EXPECT_EQ(FindTemplateURL(http_example_info), |
| 520 turl_service_->GetDefaultSearchProvider()); | 540 turl_service_->GetDefaultSearchProvider()); |
| 521 | 541 |
| 522 turl_service_->Remove(current_url); | 542 turl_service_->Remove(current_url); |
| 523 | 543 |
| 524 // Verify that text messages altered after removing |current_url|. | 544 // Verify that text messages altered after removing |current_url|. |
| 525 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); | 545 EXPECT_EQ(GetBubbleMessage(), change->GetBubbleMessage()); |
| 526 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetApplyButtonText()); | 546 EXPECT_EQ(GetOpenSettingsButtonText(), change->GetApplyButtonText()); |
| 527 EXPECT_EQ(GetKeepSearchButtonText(example_info), | 547 EXPECT_EQ(GetKeepSearchButtonText(example_info), |
| 528 change->GetDiscardButtonText()); | 548 change->GetDiscardButtonText()); |
| 549 EXPECT_EQ(std::string(), change->GetApplyKeyword()); |
| 550 EXPECT_EQ(kNoDisplayName, change->GetApplyDisplayName()); |
| 529 | 551 |
| 530 // Verify that search engine settings are opened by Apply. | 552 // Verify that search engine settings are opened by Apply. |
| 531 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); | 553 ExpectSettingsOpened(chrome::kSearchEnginesSubPage); |
| 532 change->Apply(browser()); | 554 change->Apply(browser()); |
| 533 EXPECT_EQ(FindTemplateURL(http_example_info), | 555 EXPECT_EQ(FindTemplateURL(http_example_info), |
| 534 turl_service_->GetDefaultSearchProvider()); | 556 turl_service_->GetDefaultSearchProvider()); |
| 535 | 557 |
| 536 // Discard does nothing - backup was already active. | 558 // Discard does nothing - backup was already active. |
| 537 change->Discard(browser()); | 559 change->Discard(browser()); |
| 538 EXPECT_EQ(FindTemplateURL(http_example_info), | 560 EXPECT_EQ(FindTemplateURL(http_example_info), |
| 539 turl_service_->GetDefaultSearchProvider()); | 561 turl_service_->GetDefaultSearchProvider()); |
| 540 } | 562 } |
| 541 | 563 |
| 542 } // namespace protector | 564 } // namespace protector |
| OLD | NEW |