| 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 model()->Add(t_url2); | 499 model()->Add(t_url2); |
| 500 VerifyObserverCount(2); | 500 VerifyObserverCount(2); |
| 501 EXPECT_EQ(t_url2, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 501 EXPECT_EQ(t_url2, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
| 502 EXPECT_EQ(ASCIIToUTF16("fourth"), t_url2->short_name()); | 502 EXPECT_EQ(ASCIIToUTF16("fourth"), t_url2->short_name()); |
| 503 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url2->keyword()); | 503 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url2->keyword()); |
| 504 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name()); | 504 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name()); |
| 505 EXPECT_EQ(ASCIIToUTF16("test2"), t_url->keyword()); | 505 EXPECT_EQ(ASCIIToUTF16("test2"), t_url->keyword()); |
| 506 } | 506 } |
| 507 | 507 |
| 508 TEST_F(TemplateURLServiceTest, AddExtensionKeyword) { | 508 TEST_F(TemplateURLServiceTest, AddExtensionKeyword) { |
| 509 test_util_.VerifyLoad(); |
| 510 |
| 509 TemplateURL* original1 = AddKeywordWithDate( | 511 TemplateURL* original1 = AddKeywordWithDate( |
| 510 "replaceable", "keyword1", "http://test1", std::string(), std::string(), | 512 "replaceable", "keyword1", "http://test1", std::string(), std::string(), |
| 511 std::string(), true, "UTF-8", Time(), Time()); | 513 std::string(), true, "UTF-8", Time(), Time()); |
| 512 TemplateURL* original2 = AddKeywordWithDate( | 514 TemplateURL* original2 = AddKeywordWithDate( |
| 513 "nonreplaceable", "keyword2", "http://test2", std::string(), | 515 "nonreplaceable", "keyword2", "http://test2", std::string(), |
| 514 std::string(), std::string(), false, "UTF-8", Time(), Time()); | 516 std::string(), std::string(), false, "UTF-8", Time(), Time()); |
| 515 TemplateURL* original3 = AddKeywordWithDate( | 517 TemplateURL* original3 = AddKeywordWithDate( |
| 516 "extension", "keyword3", | 518 "extension", "keyword3", |
| 517 std::string(extensions::kExtensionScheme) + "://test3", std::string(), | 519 std::string(extensions::kExtensionScheme) + "://test3", std::string(), |
| 518 std::string(), std::string(), false, "UTF-8", Time(), Time()); | 520 std::string(), std::string(), false, "UTF-8", Time(), Time()); |
| 519 | 521 |
| 520 // Add an extension keyword that conflicts with each of the above three | 522 // Add an extension keyword that conflicts with each of the above three |
| 521 // keywords. | 523 // keywords. |
| 522 TemplateURLData data; | 524 TemplateURLData data; |
| 523 data.short_name = ASCIIToUTF16("test"); | 525 data.short_name = ASCIIToUTF16("test"); |
| 524 data.SetKeyword(ASCIIToUTF16("keyword1")); | 526 data.SetKeyword(ASCIIToUTF16("keyword1")); |
| 525 data.SetURL(std::string(extensions::kExtensionScheme) + "://test4"); | 527 data.SetURL(std::string(extensions::kExtensionScheme) + "://test4"); |
| 526 data.safe_for_autoreplace = false; | 528 data.safe_for_autoreplace = false; |
| 527 | 529 |
| 528 // Extension keywords should override replaceable keywords. | 530 // Both replaceable and non-replaceable keywords should be uniquified. |
| 529 TemplateURL* extension1 = new TemplateURL(test_util_.profile(), data); | 531 TemplateURL* extension1 = new TemplateURL(test_util_.profile(), data); |
| 530 model()->Add(extension1); | 532 model()->Add(extension1); |
| 531 ASSERT_EQ(extension1, | 533 ASSERT_EQ(extension1, |
| 532 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1"))); | 534 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1"))); |
| 533 model()->Remove(extension1); | |
| 534 EXPECT_EQ(original1, | 535 EXPECT_EQ(original1, |
| 535 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1"))); | 536 model()->GetTemplateURLForKeyword(ASCIIToUTF16("test1"))); |
| 536 | |
| 537 // They should not override non-replaceable keywords. | |
| 538 data.SetKeyword(ASCIIToUTF16("keyword2")); | 537 data.SetKeyword(ASCIIToUTF16("keyword2")); |
| 539 TemplateURL* extension2 = new TemplateURL(test_util_.profile(), data); | 538 TemplateURL* extension2 = new TemplateURL(test_util_.profile(), data); |
| 540 model()->Add(extension2); | 539 model()->Add(extension2); |
| 541 ASSERT_EQ(original2, | 540 ASSERT_EQ(extension2, |
| 542 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2"))); | 541 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2"))); |
| 543 model()->Remove(original2); | 542 EXPECT_EQ(original2, |
| 544 EXPECT_EQ(extension2, | 543 model()->GetTemplateURLForKeyword(ASCIIToUTF16("test2"))); |
| 545 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2"))); | |
| 546 | 544 |
| 547 // They should override extension keywords added earlier. | 545 // They should override extension keywords added earlier. |
| 548 data.SetKeyword(ASCIIToUTF16("keyword3")); | 546 data.SetKeyword(ASCIIToUTF16("keyword3")); |
| 549 TemplateURL* extension3 = new TemplateURL(test_util_.profile(), data); | 547 TemplateURL* extension3 = new TemplateURL(test_util_.profile(), data); |
| 550 model()->Add(extension3); | 548 model()->Add(extension3); |
| 551 ASSERT_EQ(extension3, | 549 ASSERT_EQ(extension3, |
| 552 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3"))); | 550 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3"))); |
| 553 model()->Remove(extension3); | |
| 554 EXPECT_EQ(original3, | 551 EXPECT_EQ(original3, |
| 555 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3"))); | 552 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3_"))); |
| 556 } | 553 } |
| 557 | 554 |
| 558 TEST_F(TemplateURLServiceTest, AddSameKeywordWithExtensionPresent) { | 555 TEST_F(TemplateURLServiceTest, AddSameKeywordWithExtensionPresent) { |
| 559 test_util_.VerifyLoad(); | 556 test_util_.VerifyLoad(); |
| 560 | 557 |
| 561 // Similar to the AddSameKeyword test, but with an extension keyword masking a | 558 // Similar to the AddSameKeyword test, but with an extension keyword masking a |
| 562 // replaceable TemplateURL. We should still do correct conflict resolution | 559 // replaceable TemplateURL. We should still do correct conflict resolution |
| 563 // between the non-template URLs. | 560 // between the non-template URLs. |
| 564 AddKeywordWithDate( | |
| 565 "replaceable", "keyword", "http://test1", std::string(), std::string(), | |
| 566 std::string(), true, "UTF-8", Time(), Time()); | |
| 567 TemplateURL* extension = AddKeywordWithDate( | 561 TemplateURL* extension = AddKeywordWithDate( |
| 568 "extension", "keyword", | 562 "extension", "keyword", |
| 569 std::string(extensions::kExtensionScheme) + "://test2", std::string(), | 563 std::string(extensions::kExtensionScheme) + "://test2", std::string(), |
| 570 std::string(), std::string(), false, "UTF-8", Time(), Time()); | 564 std::string(), std::string(), false, "UTF-8", Time(), Time()); |
| 565 // Adding a keyword that matches the extension should cause the extension |
| 566 // to uniquify. |
| 567 AddKeywordWithDate( |
| 568 "replaceable", "keyword", "http://test1", std::string(), std::string(), |
| 569 std::string(), true, "UTF-8", Time(), Time()); |
| 571 | 570 |
| 572 // Adding another replaceable keyword should remove the existing one, but | 571 // Adding another replaceable keyword should remove the existing one, but |
| 573 // leave the extension as the associated URL for this keyword. | 572 // leave the extension as is. |
| 574 TemplateURLData data; | 573 TemplateURLData data; |
| 575 data.short_name = ASCIIToUTF16("name1"); | 574 data.short_name = ASCIIToUTF16("name1"); |
| 576 data.SetKeyword(ASCIIToUTF16("keyword")); | 575 data.SetKeyword(ASCIIToUTF16("keyword")); |
| 577 data.SetURL("http://test3"); | 576 data.SetURL("http://test3"); |
| 578 data.safe_for_autoreplace = true; | 577 data.safe_for_autoreplace = true; |
| 579 TemplateURL* t_url = new TemplateURL(test_util_.profile(), data); | 578 TemplateURL* t_url = new TemplateURL(test_util_.profile(), data); |
| 580 model()->Add(t_url); | 579 model()->Add(t_url); |
| 581 EXPECT_EQ(extension, | 580 EXPECT_EQ(extension, |
| 582 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 581 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword_"))); |
| 583 EXPECT_TRUE(model()->GetTemplateURLForHost("test1") == NULL); | 582 EXPECT_TRUE(model()->GetTemplateURLForHost("test1") == NULL); |
| 584 EXPECT_EQ(t_url, model()->GetTemplateURLForHost("test3")); | 583 EXPECT_EQ(t_url, model()->GetTemplateURLForHost("test3")); |
| 585 | 584 |
| 586 // Adding a nonreplaceable keyword should remove the existing replaceable | 585 // Adding a nonreplaceable keyword should remove the existing replaceable |
| 587 // keyword and replace the extension as the associated URL for this keyword, | 586 // keyword. |
| 588 // but not evict the extension from the service entirely. | |
| 589 data.short_name = ASCIIToUTF16("name2"); | 587 data.short_name = ASCIIToUTF16("name2"); |
| 590 data.SetURL("http://test4"); | 588 data.SetURL("http://test4"); |
| 591 data.safe_for_autoreplace = false; | 589 data.safe_for_autoreplace = false; |
| 592 TemplateURL* t_url2 = new TemplateURL(test_util_.profile(), data); | 590 TemplateURL* t_url2 = new TemplateURL(test_util_.profile(), data); |
| 593 model()->Add(t_url2); | 591 model()->Add(t_url2); |
| 594 EXPECT_EQ(t_url2, | 592 EXPECT_EQ(t_url2, |
| 595 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 593 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
| 596 EXPECT_TRUE(model()->GetTemplateURLForHost("test3") == NULL); | 594 EXPECT_TRUE(model()->GetTemplateURLForHost("test3") == NULL); |
| 597 // Note that extensions don't use host-based keywords, so we can't check that | 595 EXPECT_EQ(extension, |
| 598 // the extension is still in the model using GetTemplateURLForHost(); and we | 596 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword_"))); |
| 599 // have to create a full-fledged Extension to use | |
| 600 // GetTemplateURLForExtension(), which is annoying, so just grab all the URLs | |
| 601 // from the model and search for |extension| within them. | |
| 602 TemplateURLService::TemplateURLVector template_urls( | |
| 603 model()->GetTemplateURLs()); | |
| 604 EXPECT_FALSE(std::find(template_urls.begin(), template_urls.end(), | |
| 605 extension) == template_urls.end()); | |
| 606 } | 597 } |
| 607 | 598 |
| 608 TEST_F(TemplateURLServiceTest, GenerateKeyword) { | 599 TEST_F(TemplateURLServiceTest, GenerateKeyword) { |
| 609 ASSERT_EQ(ASCIIToUTF16("foo"), | 600 ASSERT_EQ(ASCIIToUTF16("foo"), |
| 610 TemplateURLService::GenerateKeyword(GURL("http://foo"))); | 601 TemplateURLService::GenerateKeyword(GURL("http://foo"))); |
| 611 // www. should be stripped. | 602 // www. should be stripped. |
| 612 ASSERT_EQ(ASCIIToUTF16("foo"), | 603 ASSERT_EQ(ASCIIToUTF16("foo"), |
| 613 TemplateURLService::GenerateKeyword(GURL("http://www.foo"))); | 604 TemplateURLService::GenerateKeyword(GURL("http://www.foo"))); |
| 614 // Make sure we don't get a trailing '/'. | 605 // Make sure we don't get a trailing '/'. |
| 615 ASSERT_EQ(ASCIIToUTF16("blah"), | 606 ASSERT_EQ(ASCIIToUTF16("blah"), |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 940 | 931 |
| 941 // Non-extension URLs should go away. Default search engine is Google again. | 932 // Non-extension URLs should go away. Default search engine is Google again. |
| 942 model()->ResetNonExtensionURLs(); | 933 model()->ResetNonExtensionURLs(); |
| 943 default_provider = model()->GetDefaultSearchProvider(); | 934 default_provider = model()->GetDefaultSearchProvider(); |
| 944 ASSERT_TRUE(default_provider); | 935 ASSERT_TRUE(default_provider); |
| 945 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, | 936 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, |
| 946 TemplateURLPrepopulateData::GetEngineType(default_provider->url())); | 937 TemplateURLPrepopulateData::GetEngineType(default_provider->url())); |
| 947 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext_keyword"))); | 938 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext_keyword"))); |
| 948 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 939 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
| 949 | 940 |
| 950 // Reload URLs. Result should be the same except that extension keywords | 941 // Reload URLs. Result should be the same, as extension keywords are now |
| 951 // aren't persisted. | 942 // persisted. |
| 952 test_util_.ResetModel(true); | 943 test_util_.ResetModel(true); |
| 953 default_provider = model()->GetDefaultSearchProvider(); | 944 default_provider = model()->GetDefaultSearchProvider(); |
| 954 ASSERT_TRUE(default_provider); | 945 ASSERT_TRUE(default_provider); |
| 955 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, | 946 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, |
| 956 TemplateURLPrepopulateData::GetEngineType(default_provider->url())); | 947 TemplateURLPrepopulateData::GetEngineType(default_provider->url())); |
| 957 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext_keyword"))); | 948 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext_keyword"))); |
| 958 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | 949 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
| 959 } | 950 } |
| 960 | 951 |
| 961 TEST_F(TemplateURLServiceTest, ResetURLsWithManagedDefault) { | 952 TEST_F(TemplateURLServiceTest, ResetURLsWithManagedDefault) { |
| 962 // Set a managed preference that establishes a default search provider. | 953 // Set a managed preference that establishes a default search provider. |
| 963 const char kName[] = "test1"; | 954 const char kName[] = "test1"; |
| 964 const char kKeyword[] = "test.com"; | 955 const char kKeyword[] = "test.com"; |
| 965 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; | 956 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; |
| 966 const char kIconURL[] = "http://test.com/icon.jpg"; | 957 const char kIconURL[] = "http://test.com/icon.jpg"; |
| 967 const char kEncodings[] = "UTF-16;UTF-32"; | 958 const char kEncodings[] = "UTF-16;UTF-32"; |
| (...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1575 EXPECT_EQ(8U, loaded_url->input_encodings().size()); | 1566 EXPECT_EQ(8U, loaded_url->input_encodings().size()); |
| 1576 | 1567 |
| 1577 // Reload the model to verify it was actually saved to the database and the | 1568 // Reload the model to verify it was actually saved to the database and the |
| 1578 // duplicate encodings were removed. | 1569 // duplicate encodings were removed. |
| 1579 test_util_.ResetModel(true); | 1570 test_util_.ResetModel(true); |
| 1580 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1571 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
| 1581 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); | 1572 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); |
| 1582 ASSERT_FALSE(loaded_url == NULL); | 1573 ASSERT_FALSE(loaded_url == NULL); |
| 1583 EXPECT_EQ(4U, loaded_url->input_encodings().size()); | 1574 EXPECT_EQ(4U, loaded_url->input_encodings().size()); |
| 1584 } | 1575 } |
| OLD | NEW |