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 "components/search_engines/template_url_service.h" | 5 #include "components/search_engines/template_url_service.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 const std::string& url, | 169 const std::string& url, |
170 const std::string& suggest_url, | 170 const std::string& suggest_url, |
171 const std::string& alternate_url, | 171 const std::string& alternate_url, |
172 const std::string& favicon_url, | 172 const std::string& favicon_url, |
173 bool safe_for_autoreplace, | 173 bool safe_for_autoreplace, |
174 const std::string& encodings, | 174 const std::string& encodings, |
175 Time date_created, | 175 Time date_created, |
176 Time last_modified, | 176 Time last_modified, |
177 Time last_visited); | 177 Time last_visited); |
178 | 178 |
| 179 // Add extension controlled search engine with |keyword| to model. |
| 180 TemplateURL* AddExtensionSearchEngine(const std::string& keyword, |
| 181 const std::string& extension_name, |
| 182 bool wants_to_be_default_engine, |
| 183 const base::Time& install_time); |
| 184 |
179 // Verifies the two TemplateURLs are equal. | 185 // Verifies the two TemplateURLs are equal. |
180 void AssertEquals(const TemplateURL& expected, const TemplateURL& actual); | 186 void AssertEquals(const TemplateURL& expected, const TemplateURL& actual); |
181 | 187 |
182 // Verifies the two timestamps are equal, within the expected degree of | 188 // Verifies the two timestamps are equal, within the expected degree of |
183 // precision. | 189 // precision. |
184 void AssertTimesEqual(const base::Time& expected, const base::Time& actual); | 190 void AssertTimesEqual(const base::Time& expected, const base::Time& actual); |
185 | 191 |
186 // Create an URL that appears to have been prepopulated, but won't be in the | 192 // Create an URL that appears to have been prepopulated, but won't be in the |
187 // current data. | 193 // current data. |
188 std::unique_ptr<TemplateURL> CreatePreloadedTemplateURL( | 194 std::unique_ptr<TemplateURL> CreatePreloadedTemplateURL( |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 const std::string& encodings, | 252 const std::string& encodings, |
247 Time date_created, | 253 Time date_created, |
248 Time last_modified, | 254 Time last_modified, |
249 Time last_visited) { | 255 Time last_visited) { |
250 return ::AddKeywordWithDate(model(), short_name, keyword, url, suggest_url, | 256 return ::AddKeywordWithDate(model(), short_name, keyword, url, suggest_url, |
251 alternate_url, favicon_url, safe_for_autoreplace, | 257 alternate_url, favicon_url, safe_for_autoreplace, |
252 encodings, date_created, last_modified, | 258 encodings, date_created, last_modified, |
253 last_visited); | 259 last_visited); |
254 } | 260 } |
255 | 261 |
| 262 TemplateURL* TemplateURLServiceTest::AddExtensionSearchEngine( |
| 263 const std::string& keyword, |
| 264 const std::string& extension_name, |
| 265 bool wants_to_be_default_engine, |
| 266 const base::Time& install_time) { |
| 267 std::unique_ptr<TemplateURLData> turl_data = |
| 268 GenerateDummyTemplateURLData(keyword); |
| 269 turl_data->safe_for_autoreplace = false; |
| 270 |
| 271 auto ext_dse = base::MakeUnique<TemplateURL>( |
| 272 *turl_data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| 273 auto extension_info = |
| 274 base::MakeUnique<TemplateURL::AssociatedExtensionInfo>(extension_name); |
| 275 extension_info->wants_to_be_default_engine = wants_to_be_default_engine; |
| 276 extension_info->install_time = install_time; |
| 277 |
| 278 return model()->AddExtensionControlledTURL(std::move(ext_dse), |
| 279 std::move(extension_info)); |
| 280 } |
| 281 |
256 void TemplateURLServiceTest::AssertEquals(const TemplateURL& expected, | 282 void TemplateURLServiceTest::AssertEquals(const TemplateURL& expected, |
257 const TemplateURL& actual) { | 283 const TemplateURL& actual) { |
258 ASSERT_EQ(expected.short_name(), actual.short_name()); | 284 ASSERT_EQ(expected.short_name(), actual.short_name()); |
259 ASSERT_EQ(expected.keyword(), actual.keyword()); | 285 ASSERT_EQ(expected.keyword(), actual.keyword()); |
260 ASSERT_EQ(expected.url(), actual.url()); | 286 ASSERT_EQ(expected.url(), actual.url()); |
261 ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url()); | 287 ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url()); |
262 ASSERT_EQ(expected.favicon_url(), actual.favicon_url()); | 288 ASSERT_EQ(expected.favicon_url(), actual.favicon_url()); |
263 ASSERT_EQ(expected.alternate_urls(), actual.alternate_urls()); | 289 ASSERT_EQ(expected.alternate_urls(), actual.alternate_urls()); |
264 ASSERT_EQ(expected.prepopulate_id(), actual.prepopulate_id()); | 290 ASSERT_EQ(expected.prepopulate_id(), actual.prepopulate_id()); |
265 ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace()); | 291 ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace()); |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 data.safe_for_autoreplace = false; | 483 data.safe_for_autoreplace = false; |
458 TemplateURL* t_url2 = model()->Add(base::MakeUnique<TemplateURL>(data)); | 484 TemplateURL* t_url2 = model()->Add(base::MakeUnique<TemplateURL>(data)); |
459 VerifyObserverCount(1); | 485 VerifyObserverCount(1); |
460 EXPECT_EQ(t_url2, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 486 EXPECT_EQ(t_url2, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
461 EXPECT_EQ(ASCIIToUTF16("fourth"), t_url2->short_name()); | 487 EXPECT_EQ(ASCIIToUTF16("fourth"), t_url2->short_name()); |
462 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url2->keyword()); | 488 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url2->keyword()); |
463 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name()); | 489 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name()); |
464 EXPECT_EQ(ASCIIToUTF16("test2"), t_url->keyword()); | 490 EXPECT_EQ(ASCIIToUTF16("test2"), t_url->keyword()); |
465 } | 491 } |
466 | 492 |
467 TEST_F(TemplateURLServiceTest, AddExtensionKeyword) { | 493 TEST_F(TemplateURLServiceTest, AddOmniboxExtensionKeyword) { |
468 test_util()->VerifyLoad(); | 494 test_util()->VerifyLoad(); |
469 | 495 |
470 AddKeywordWithDate("replaceable", "keyword1", "http://test1", std::string(), | 496 AddKeywordWithDate("replaceable", "keyword1", "http://test1", std::string(), |
471 std::string(), std::string(), true, "UTF-8", Time(), | 497 std::string(), std::string(), true, "UTF-8", Time(), |
472 Time(), Time()); | 498 Time(), Time()); |
473 TemplateURL* original2 = AddKeywordWithDate( | 499 AddKeywordWithDate("nonreplaceable", "keyword2", "http://test2", |
474 "nonreplaceable", "keyword2", "http://test2", std::string(), | 500 std::string(), std::string(), std::string(), false, |
475 std::string(), std::string(), false, "UTF-8", Time(), Time(), Time()); | 501 "UTF-8", Time(), Time(), Time()); |
476 model()->RegisterOmniboxKeyword("test3", "extension", "keyword3", | 502 model()->RegisterOmniboxKeyword("test3", "extension", "keyword3", |
477 "http://test3"); | 503 "http://test3", Time::FromDoubleT(1)); |
478 TemplateURL* original3 = | 504 TemplateURL* original3 = |
479 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3")); | 505 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3")); |
480 ASSERT_TRUE(original3); | 506 ASSERT_TRUE(original3); |
481 | 507 |
482 // Extension keywords should override replaceable keywords. | 508 // Extension keywords should override replaceable keywords. |
483 model()->RegisterOmniboxKeyword("id1", "test", "keyword1", "http://test4"); | 509 model()->RegisterOmniboxKeyword("id1", "test", "keyword1", "http://test4", |
| 510 Time()); |
484 TemplateURL* extension1 = model()->FindTemplateURLForExtension( | 511 TemplateURL* extension1 = model()->FindTemplateURLForExtension( |
485 "id1", TemplateURL::OMNIBOX_API_EXTENSION); | 512 "id1", TemplateURL::OMNIBOX_API_EXTENSION); |
486 EXPECT_TRUE(extension1); | 513 EXPECT_TRUE(extension1); |
487 EXPECT_EQ(extension1, | 514 EXPECT_EQ(extension1, |
488 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1"))); | 515 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1"))); |
489 | 516 |
490 // They should not override non-replaceable keywords. | 517 // They should also override non-replaceable keywords. |
491 model()->RegisterOmniboxKeyword("id2", "test", "keyword2", "http://test5"); | 518 model()->RegisterOmniboxKeyword("id2", "test", "keyword2", "http://test5", |
| 519 Time()); |
492 TemplateURL* extension2 = model()->FindTemplateURLForExtension( | 520 TemplateURL* extension2 = model()->FindTemplateURLForExtension( |
493 "id2", TemplateURL::OMNIBOX_API_EXTENSION); | 521 "id2", TemplateURL::OMNIBOX_API_EXTENSION); |
494 ASSERT_TRUE(extension2); | 522 ASSERT_TRUE(extension2); |
495 EXPECT_EQ(original2, | 523 EXPECT_EQ(extension2, |
496 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2"))); | 524 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2"))); |
497 | 525 |
498 // They should override extension keywords added earlier. | 526 // They should override extension keywords added earlier. |
499 model()->RegisterOmniboxKeyword("id3", "test", "keyword3", "http://test6"); | 527 model()->RegisterOmniboxKeyword("id3", "test", "keyword3", "http://test6", |
| 528 Time::FromDoubleT(4)); |
500 TemplateURL* extension3 = model()->FindTemplateURLForExtension( | 529 TemplateURL* extension3 = model()->FindTemplateURLForExtension( |
501 "id3", TemplateURL::OMNIBOX_API_EXTENSION); | 530 "id3", TemplateURL::OMNIBOX_API_EXTENSION); |
502 ASSERT_TRUE(extension3); | 531 ASSERT_TRUE(extension3); |
503 EXPECT_EQ(extension3, | 532 EXPECT_EQ(extension3, |
504 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3"))); | 533 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3"))); |
505 } | 534 } |
506 | 535 |
507 TEST_F(TemplateURLServiceTest, AddSameKeywordWithExtensionPresent) { | 536 TEST_F(TemplateURLServiceTest, AddSameKeywordWithOmniboxExtensionPresent) { |
508 test_util()->VerifyLoad(); | 537 test_util()->VerifyLoad(); |
509 | 538 |
510 // Similar to the AddSameKeyword test, but with an extension keyword masking a | 539 // Similar to the AddSameKeyword test, but with an extension keyword masking a |
511 // replaceable TemplateURL. We should still do correct conflict resolution | 540 // replaceable TemplateURL. We should still do correct conflict resolution |
512 // between the non-template URLs. | 541 // between the non-template URLs. |
513 model()->RegisterOmniboxKeyword("test2", "extension", "keyword", | 542 model()->RegisterOmniboxKeyword("test2", "extension", "keyword", |
514 "http://test2"); | 543 "http://test2", Time()); |
515 TemplateURL* extension = | 544 TemplateURL* extension = |
516 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); | 545 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); |
517 ASSERT_TRUE(extension); | 546 ASSERT_TRUE(extension); |
518 // Adding a keyword that matches the extension. | 547 // Adding a keyword that matches the extension. |
519 AddKeywordWithDate("replaceable", "keyword", "http://test1", std::string(), | 548 AddKeywordWithDate("replaceable", "keyword", "http://test1", std::string(), |
520 std::string(), std::string(), true, "UTF-8", Time(), | 549 std::string(), std::string(), true, "UTF-8", Time(), |
521 Time(), Time()); | 550 Time(), Time()); |
522 | 551 |
523 // Adding another replaceable keyword should remove the existing one, but | 552 // Adding another replaceable keyword should remove the existing one, but |
524 // leave the extension as is. | 553 // leave the extension as is. |
525 TemplateURLData data; | 554 TemplateURLData data; |
526 data.SetShortName(ASCIIToUTF16("name1")); | 555 data.SetShortName(ASCIIToUTF16("name1")); |
527 data.SetKeyword(ASCIIToUTF16("keyword")); | 556 data.SetKeyword(ASCIIToUTF16("keyword")); |
528 data.SetURL("http://test3"); | 557 data.SetURL("http://test3"); |
529 data.safe_for_autoreplace = true; | 558 data.safe_for_autoreplace = true; |
530 TemplateURL* t_url = model()->Add(base::MakeUnique<TemplateURL>(data)); | 559 TemplateURL* t_url = model()->Add(base::MakeUnique<TemplateURL>(data)); |
531 EXPECT_EQ(extension, | 560 EXPECT_EQ(extension, |
532 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 561 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
533 EXPECT_EQ(t_url, model()->GetTemplateURLForHost("test3")); | 562 EXPECT_EQ(t_url, model()->GetTemplateURLForHost("test3")); |
| 563 // Check that previous replaceable engine with keyword is removed. |
| 564 EXPECT_FALSE(model()->GetTemplateURLForHost("test1")); |
534 | 565 |
535 // Adding a nonreplaceable keyword should remove the existing replaceable | 566 // Adding a nonreplaceable keyword should remove the existing replaceable |
536 // keyword and replace the extension as the associated URL for this keyword, | 567 // keyword, yet extension must still be set as the associated URL for this |
537 // but not evict the extension from the service entirely. | 568 // keyword. |
538 data.SetShortName(ASCIIToUTF16("name2")); | 569 data.SetShortName(ASCIIToUTF16("name2")); |
539 data.SetURL("http://test4"); | 570 data.SetURL("http://test4"); |
540 data.safe_for_autoreplace = false; | 571 data.safe_for_autoreplace = false; |
541 TemplateURL* t_url2 = model()->Add(base::MakeUnique<TemplateURL>(data)); | 572 TemplateURL* nonreplaceable = |
542 EXPECT_EQ(t_url2, | 573 model()->Add(base::MakeUnique<TemplateURL>(data)); |
| 574 EXPECT_EQ(extension, |
543 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 575 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
| 576 EXPECT_EQ(nonreplaceable, model()->GetTemplateURLForHost("test4")); |
| 577 // Check that previous replaceable engine with keyword is removed. |
| 578 EXPECT_FALSE(model()->GetTemplateURLForHost("test3")); |
544 } | 579 } |
545 | 580 |
546 TEST_F(TemplateURLServiceTest, NotPersistOmniboxExtensionKeyword) { | 581 TEST_F(TemplateURLServiceTest, NotPersistOmniboxExtensionKeyword) { |
547 test_util()->VerifyLoad(); | 582 test_util()->VerifyLoad(); |
548 | 583 |
549 // Register an omnibox keyword. | 584 // Register an omnibox keyword. |
550 model()->RegisterOmniboxKeyword("test", "extension", "keyword", | 585 model()->RegisterOmniboxKeyword("test", "extension", "keyword", |
551 "chrome-extension://test"); | 586 "chrome-extension://test", Time()); |
552 ASSERT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 587 ASSERT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
553 | 588 |
554 // Reload the data. | 589 // Reload the data. |
555 test_util()->ResetModel(true); | 590 test_util()->ResetModel(true); |
556 | 591 |
557 // Ensure the omnibox keyword is not persisted. | 592 // Ensure the omnibox keyword is not persisted. |
558 ASSERT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 593 ASSERT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
559 } | 594 } |
560 | 595 |
561 TEST_F(TemplateURLServiceTest, ClearBrowsingData_Keywords) { | 596 TEST_F(TemplateURLServiceTest, ClearBrowsingData_Keywords) { |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
872 | 907 |
873 // Remove bing. | 908 // Remove bing. |
874 TemplateURL* bing = model()->GetTemplateURLForKeyword( | 909 TemplateURL* bing = model()->GetTemplateURLForKeyword( |
875 ASCIIToUTF16("bing.com")); | 910 ASCIIToUTF16("bing.com")); |
876 ASSERT_TRUE(bing); | 911 ASSERT_TRUE(bing); |
877 model()->Remove(bing); | 912 model()->Remove(bing); |
878 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("bing.com"))); | 913 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("bing.com"))); |
879 | 914 |
880 // Register an extension with bing keyword. | 915 // Register an extension with bing keyword. |
881 model()->RegisterOmniboxKeyword("abcdefg", "extension_name", "bing.com", | 916 model()->RegisterOmniboxKeyword("abcdefg", "extension_name", "bing.com", |
882 "http://abcdefg"); | 917 "http://abcdefg", Time()); |
883 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("bing.com"))); | 918 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("bing.com"))); |
884 | 919 |
885 model()->RepairPrepopulatedSearchEngines(); | 920 model()->RepairPrepopulatedSearchEngines(); |
886 | 921 |
887 // Google is default. | 922 // Google is default. |
888 ASSERT_EQ(google, model()->GetDefaultSearchProvider()); | 923 ASSERT_EQ(google, model()->GetDefaultSearchProvider()); |
889 // The keyword wasn't reverted. | 924 // The keyword wasn't reverted. |
890 EXPECT_EQ(ASCIIToUTF16("trash"), google->short_name()); | 925 EXPECT_EQ(ASCIIToUTF16("trash"), google->short_name()); |
891 EXPECT_EQ("www.google.com", | 926 EXPECT_EQ("www.google.com", |
892 google->GenerateSearchURL(model()->search_terms_data()).host()); | 927 google->GenerateSearchURL(model()->search_terms_data()).host()); |
893 | 928 |
894 // Bing was repaired. | 929 // Bing was repaired. |
895 bing = model()->GetTemplateURLForKeyword(ASCIIToUTF16("bing.com")); | 930 bing = |
| 931 model()->FindNonExtensionTemplateURLForKeyword(ASCIIToUTF16("bing.com")); |
896 ASSERT_TRUE(bing); | 932 ASSERT_TRUE(bing); |
897 EXPECT_EQ(TemplateURL::NORMAL, bing->type()); | 933 EXPECT_EQ(TemplateURL::NORMAL, bing->type()); |
898 | 934 |
899 // User search engine is preserved. | 935 // User search engine is preserved. |
900 EXPECT_EQ(user_dse, model()->GetTemplateURLForHost("www.goo.com")); | 936 EXPECT_EQ(user_dse, model()->GetTemplateURLForHost("www.goo.com")); |
901 EXPECT_EQ(ASCIIToUTF16("google.com"), user_dse->keyword()); | 937 EXPECT_EQ(ASCIIToUTF16("google.com"), user_dse->keyword()); |
902 } | 938 } |
903 | 939 |
904 TEST_F(TemplateURLServiceTest, RepairSearchEnginesWithManagedDefault) { | 940 TEST_F(TemplateURLServiceTest, RepairSearchEnginesWithManagedDefault) { |
905 // Set a managed preference that establishes a default search provider. | 941 // Set a managed preference that establishes a default search provider. |
(...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1651 std::move(ext_dse), std::move(extension_info)); | 1687 std::move(ext_dse), std::move(extension_info)); |
1652 EXPECT_EQ(ext_dse_ptr, model()->GetDefaultSearchProvider()); | 1688 EXPECT_EQ(ext_dse_ptr, model()->GetDefaultSearchProvider()); |
1653 | 1689 |
1654 test_util()->ResetModel(true); | 1690 test_util()->ResetModel(true); |
1655 user_dse = model()->GetTemplateURLForKeyword(ASCIIToUTF16("user")); | 1691 user_dse = model()->GetTemplateURLForKeyword(ASCIIToUTF16("user")); |
1656 ExpectSimilar(user_dse, model()->GetDefaultSearchProvider()); | 1692 ExpectSimilar(user_dse, model()->GetDefaultSearchProvider()); |
1657 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); | 1693 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); |
1658 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext2"))); | 1694 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext2"))); |
1659 } | 1695 } |
1660 | 1696 |
| 1697 // Checks that correct priority is applied when resolving conflicts between the |
| 1698 // omnibox extension, search engine extension and user search engines with same |
| 1699 // keyword. |
| 1700 TEST_F(TemplateURLServiceTest, CheckEnginesWithSameKeywords) { |
| 1701 test_util()->VerifyLoad(); |
| 1702 // TemplateURLData used for user engines. |
| 1703 std::unique_ptr<TemplateURLData> turl_data = |
| 1704 GenerateDummyTemplateURLData("common_keyword"); |
| 1705 turl_data->safe_for_autoreplace = false; |
| 1706 |
| 1707 // Add non replaceable user engine. |
| 1708 const TemplateURL* user1 = |
| 1709 model()->Add(base::MakeUnique<TemplateURL>(*turl_data)); |
| 1710 |
| 1711 // Add default extension engine with same keyword as user engine. |
| 1712 const TemplateURL* extension = AddExtensionSearchEngine( |
| 1713 "common_keyword", "extension_id", true, Time::FromDoubleT(2)); |
| 1714 |
| 1715 // Add another non replaceable user engine with same keyword as extension. |
| 1716 const TemplateURL* user2 = |
| 1717 model()->Add(base::MakeUnique<TemplateURL>(*turl_data)); |
| 1718 |
| 1719 // Check extension DSE is set as default and its keyword is not changed. |
| 1720 const TemplateURL* current_dse = model()->GetDefaultSearchProvider(); |
| 1721 EXPECT_EQ(extension, current_dse); |
| 1722 EXPECT_EQ(ASCIIToUTF16("common_keyword"), current_dse->keyword()); |
| 1723 |
| 1724 // Register omnibox keyword with same keyword as extension. |
| 1725 // Use |install_time| value less than in AddExtensionSearchEngine call above |
| 1726 // to check that omnibox api keyword is ranked higher even if installed |
| 1727 // earlier. |
| 1728 model()->RegisterOmniboxKeyword("omnibox_api_extension_id", "extension_name", |
| 1729 "common_keyword", "http://test3", |
| 1730 Time::FromDoubleT(1)); |
| 1731 TemplateURL* omnibox_api = model()->FindTemplateURLForExtension( |
| 1732 "omnibox_api_extension_id", TemplateURL::OMNIBOX_API_EXTENSION); |
| 1733 |
| 1734 // Expect that first non replaceable user engine keyword is changed because of |
| 1735 // conflict. Second user engine will keep its keyword. |
| 1736 EXPECT_NE(ASCIIToUTF16("common_keyword"), user1->keyword()); |
| 1737 EXPECT_EQ(ASCIIToUTF16("common_keyword"), user2->keyword()); |
| 1738 |
| 1739 // Check that extensions kept their keywords. |
| 1740 EXPECT_EQ(ASCIIToUTF16("common_keyword"), extension->keyword()); |
| 1741 EXPECT_EQ(ASCIIToUTF16("common_keyword"), omnibox_api->keyword()); |
| 1742 |
| 1743 // Omnibox api is accessible by keyword as most relevant. |
| 1744 EXPECT_EQ(omnibox_api, |
| 1745 model()->GetTemplateURLForKeyword(ASCIIToUTF16("common_keyword"))); |
| 1746 // Extension controlled search engine is still set as default and can be found |
| 1747 // in TemplateURLService. |
| 1748 EXPECT_EQ(extension, model()->GetDefaultSearchProvider()); |
| 1749 EXPECT_EQ(extension, |
| 1750 model()->FindTemplateURLForExtension( |
| 1751 "extension_id", TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)); |
| 1752 |
| 1753 // Test removing engines. |
| 1754 // Remove omnibox api extension. |
| 1755 model()->RemoveExtensionControlledTURL("omnibox_api_extension_id", |
| 1756 TemplateURL::OMNIBOX_API_EXTENSION); |
| 1757 // Expect that keyword is now corresponds to extension search engine. |
| 1758 EXPECT_EQ(extension, |
| 1759 model()->GetTemplateURLForKeyword(ASCIIToUTF16("common_keyword"))); |
| 1760 // Remove extension engine. |
| 1761 model()->RemoveExtensionControlledTURL( |
| 1762 "extension_id", TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| 1763 EXPECT_NE(extension, model()->GetDefaultSearchProvider()); |
| 1764 // Now latest user engine is returned for keyword. |
| 1765 EXPECT_EQ(user2, |
| 1766 model()->GetTemplateURLForKeyword(ASCIIToUTF16("common_keyword"))); |
| 1767 } |
| 1768 |
| 1769 // Check that two extensions with the same engine are handled correctly. |
| 1770 TEST_F(TemplateURLServiceTest, ExtensionsWithSameKeywords) { |
| 1771 test_util()->VerifyLoad(); |
| 1772 |
| 1773 // Add non default extension engine. |
| 1774 const TemplateURL* extension1 = AddExtensionSearchEngine( |
| 1775 "common_keyword", "extension_id1", false, Time::FromDoubleT(1)); |
| 1776 |
| 1777 // Check that GetTemplateURLForKeyword returns last installed extension. |
| 1778 EXPECT_EQ(extension1, |
| 1779 model()->GetTemplateURLForKeyword(ASCIIToUTF16("common_keyword"))); |
| 1780 |
| 1781 // Add default extension engine with the same keyword. |
| 1782 const TemplateURL* extension2 = AddExtensionSearchEngine( |
| 1783 "common_keyword", "extension_id2", true, Time::FromDoubleT(2)); |
| 1784 |
| 1785 // Check that GetTemplateURLForKeyword now returns extension2 because it was |
| 1786 // installed later. |
| 1787 EXPECT_EQ(extension2, |
| 1788 model()->GetTemplateURLForKeyword(ASCIIToUTF16("common_keyword"))); |
| 1789 |
| 1790 // Add another non default extension with same keyword. This action must not |
| 1791 // change any keyword due to conflict. |
| 1792 const TemplateURL* extension3 = AddExtensionSearchEngine( |
| 1793 "common_keyword", "extension_id3", false, Time::FromDoubleT(3)); |
| 1794 |
| 1795 // Check that extension2 is set as default. |
| 1796 EXPECT_EQ(extension2, model()->GetDefaultSearchProvider()); |
| 1797 |
| 1798 // Check that GetTemplateURLForKeyword returns last installed extension. |
| 1799 EXPECT_EQ(extension3, |
| 1800 model()->GetTemplateURLForKeyword(ASCIIToUTF16("common_keyword"))); |
| 1801 // Check that all keywords for extensions are left unchanged. |
| 1802 EXPECT_EQ(ASCIIToUTF16("common_keyword"), extension1->keyword()); |
| 1803 EXPECT_EQ(ASCIIToUTF16("common_keyword"), extension2->keyword()); |
| 1804 EXPECT_EQ(ASCIIToUTF16("common_keyword"), extension3->keyword()); |
| 1805 } |
| 1806 |
1661 TEST_F(TemplateURLServiceTest, ExtensionEngineVsPolicy) { | 1807 TEST_F(TemplateURLServiceTest, ExtensionEngineVsPolicy) { |
1662 // Set a managed preference that establishes a default search provider. | 1808 // Set a managed preference that establishes a default search provider. |
1663 std::unique_ptr<TemplateURLData> managed = CreateTestSearchEngine(); | 1809 std::unique_ptr<TemplateURLData> managed = CreateTestSearchEngine(); |
1664 SetManagedDefaultSearchPreferences(*managed, true, test_util()->profile()); | 1810 SetManagedDefaultSearchPreferences(*managed, true, test_util()->profile()); |
1665 test_util()->VerifyLoad(); | 1811 test_util()->VerifyLoad(); |
1666 // Verify that the default manager we are getting is the managed one. | 1812 // Verify that the default manager we are getting is the managed one. |
1667 auto expected_managed_default = base::MakeUnique<TemplateURL>(*managed); | 1813 auto expected_managed_default = base::MakeUnique<TemplateURL>(*managed); |
1668 EXPECT_TRUE(model()->is_default_search_managed()); | 1814 EXPECT_TRUE(model()->is_default_search_managed()); |
1669 const TemplateURL* actual_managed_default = | 1815 const TemplateURL* actual_managed_default = |
1670 model()->GetDefaultSearchProvider(); | 1816 model()->GetDefaultSearchProvider(); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1719 TemplateURL* update_url = | 1865 TemplateURL* update_url = |
1720 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); | 1866 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); |
1721 const base::Time update_last_modified = update_url->last_modified(); | 1867 const base::Time update_last_modified = update_url->last_modified(); |
1722 model()->SetUserSelectedDefaultSearchProvider(update_url); | 1868 model()->SetUserSelectedDefaultSearchProvider(update_url); |
1723 TemplateURL* reloaded_url = | 1869 TemplateURL* reloaded_url = |
1724 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); | 1870 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); |
1725 const base::Time reloaded_last_modified = reloaded_url->last_modified(); | 1871 const base::Time reloaded_last_modified = reloaded_url->last_modified(); |
1726 EXPECT_NE(original_last_modified, reloaded_last_modified); | 1872 EXPECT_NE(original_last_modified, reloaded_last_modified); |
1727 EXPECT_EQ(update_last_modified, reloaded_last_modified); | 1873 EXPECT_EQ(update_last_modified, reloaded_last_modified); |
1728 } | 1874 } |
OLD | NEW |