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 904 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
915 EXPECT_EQ("http://instant", default_turl->instant_url()); | 915 EXPECT_EQ("http://instant", default_turl->instant_url()); |
916 EXPECT_EQ(id, default_turl->id()); | 916 EXPECT_EQ(id, default_turl->id()); |
917 | 917 |
918 // Now do a load and make sure the default search provider really takes. | 918 // Now do a load and make sure the default search provider really takes. |
919 test_util_.VerifyLoad(); | 919 test_util_.VerifyLoad(); |
920 | 920 |
921 ASSERT_TRUE(model()->GetDefaultSearchProvider()); | 921 ASSERT_TRUE(model()->GetDefaultSearchProvider()); |
922 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider()); | 922 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider()); |
923 } | 923 } |
924 | 924 |
| 925 TEST_F(TemplateURLServiceTest, DefaultSearchProviderClearFromPrefs) { |
| 926 test_util_.VerifyLoad(); |
| 927 |
| 928 TemplateURLData data; |
| 929 data.short_name = ASCIIToUTF16("a"); |
| 930 data.safe_for_autoreplace = true; |
| 931 data.SetURL("http://url/{searchTerms}"); |
| 932 data.suggestions_url = "http://url2"; |
| 933 data.instant_url = "http://instant"; |
| 934 data.date_created = Time::FromTimeT(100); |
| 935 data.last_modified = Time::FromTimeT(100); |
| 936 TemplateURL* t_url = new TemplateURL(test_util_.profile(), data); |
| 937 model()->Add(t_url); |
| 938 model()->SetDefaultSearchProvider(t_url); |
| 939 // This should overwrite default search engine in user prefs. |
| 940 model()->ClearDefaultProviderFromPrefs(); |
| 941 |
| 942 test_util_.ResetModel(false); |
| 943 const TemplateURL* default_turl = model()->GetDefaultSearchProvider(); |
| 944 scoped_ptr<TemplateURL> default_prepopulated( |
| 945 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch( |
| 946 test_util_.profile())); |
| 947 ExpectSimilar(default_prepopulated.get(), default_turl); |
| 948 } |
| 949 |
| 950 TEST_F(TemplateURLServiceTest, ResetNonExtensionURLs) { |
| 951 test_util_.VerifyLoad(); |
| 952 |
| 953 TemplateURL* new_provider = AddKeywordWithDate( |
| 954 "short_name", "keyword", "http://test.com/search?t={searchTerms}", |
| 955 std::string(), std::string(), std::string(), |
| 956 true, "UTF-8", Time(), Time()); |
| 957 model()->SetDefaultSearchProvider(new_provider); |
| 958 AddKeywordWithDate( |
| 959 "extension1", "ext_keyword", |
| 960 std::string(extensions::kExtensionScheme) + "://test1", std::string(), |
| 961 std::string(), std::string(), false, "UTF-8", Time(), Time()); |
| 962 TemplateURL* default_provider = model()->GetDefaultSearchProvider(); |
| 963 EXPECT_NE(SEARCH_ENGINE_GOOGLE, |
| 964 TemplateURLPrepopulateData::GetEngineType(default_provider->url())); |
| 965 |
| 966 // Non-extension URLs should go away. Default search engine is Google again. |
| 967 model()->ResetNonExtensionURLs(); |
| 968 default_provider = model()->GetDefaultSearchProvider(); |
| 969 ASSERT_TRUE(default_provider); |
| 970 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, |
| 971 TemplateURLPrepopulateData::GetEngineType(default_provider->url())); |
| 972 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext_keyword"))); |
| 973 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
| 974 |
| 975 // Reload URLs. Result should be the same except that extension keywords |
| 976 // aren't persisted. |
| 977 test_util_.ResetModel(true); |
| 978 default_provider = model()->GetDefaultSearchProvider(); |
| 979 ASSERT_TRUE(default_provider); |
| 980 EXPECT_EQ(SEARCH_ENGINE_GOOGLE, |
| 981 TemplateURLPrepopulateData::GetEngineType(default_provider->url())); |
| 982 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext_keyword"))); |
| 983 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); |
| 984 } |
| 985 |
| 986 TEST_F(TemplateURLServiceTest, ResetURLsWithManagedDefault) { |
| 987 // Set a managed preference that establishes a default search provider. |
| 988 const char kName[] = "test1"; |
| 989 const char kKeyword[] = "test.com"; |
| 990 const char kSearchURL[] = "http://test.com/search?t={searchTerms}"; |
| 991 const char kIconURL[] = "http://test.com/icon.jpg"; |
| 992 const char kEncodings[] = "UTF-16;UTF-32"; |
| 993 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}"; |
| 994 const char kSearchTermsReplacementKey[] = "espv"; |
| 995 test_util_.SetManagedDefaultSearchPreferences(true, kName, kKeyword, |
| 996 kSearchURL, std::string(), |
| 997 kIconURL, kEncodings, |
| 998 kAlternateURL, |
| 999 kSearchTermsReplacementKey); |
| 1000 test_util_.VerifyLoad(); |
| 1001 // Verify that the default manager we are getting is the managed one. |
| 1002 TemplateURLData data; |
| 1003 data.short_name = ASCIIToUTF16(kName); |
| 1004 data.SetKeyword(ASCIIToUTF16(kKeyword)); |
| 1005 data.SetURL(kSearchURL); |
| 1006 data.favicon_url = GURL(kIconURL); |
| 1007 data.show_in_default_list = true; |
| 1008 base::SplitString(kEncodings, ';', &data.input_encodings); |
| 1009 data.alternate_urls.push_back(kAlternateURL); |
| 1010 data.search_terms_replacement_key = kSearchTermsReplacementKey; |
| 1011 Profile* profile = test_util_.profile(); |
| 1012 scoped_ptr<TemplateURL> expected_managed_default(new TemplateURL(profile, |
| 1013 data)); |
| 1014 EXPECT_TRUE(model()->is_default_search_managed()); |
| 1015 const TemplateURL* actual_managed_default = |
| 1016 model()->GetDefaultSearchProvider(); |
| 1017 ExpectSimilar(expected_managed_default.get(), actual_managed_default); |
| 1018 |
| 1019 // The following calls have no effect on managed search engine. |
| 1020 model()->ClearDefaultProviderFromPrefs(); |
| 1021 model()->ResetNonExtensionURLs(); |
| 1022 |
| 1023 EXPECT_TRUE(model()->is_default_search_managed()); |
| 1024 actual_managed_default = model()->GetDefaultSearchProvider(); |
| 1025 ExpectSimilar(expected_managed_default.get(), actual_managed_default); |
| 1026 } |
| 1027 |
925 TEST_F(TemplateURLServiceTest, UpdateKeywordSearchTermsForURL) { | 1028 TEST_F(TemplateURLServiceTest, UpdateKeywordSearchTermsForURL) { |
926 struct TestData { | 1029 struct TestData { |
927 const std::string url; | 1030 const std::string url; |
928 const string16 term; | 1031 const string16 term; |
929 } data[] = { | 1032 } data[] = { |
930 { "http://foo/", string16() }, | 1033 { "http://foo/", string16() }, |
931 { "http://foo/foo?q=xx", string16() }, | 1034 { "http://foo/foo?q=xx", string16() }, |
932 { "http://x/bar?q=xx", string16() }, | 1035 { "http://x/bar?q=xx", string16() }, |
933 { "http://x/foo?y=xx", string16() }, | 1036 { "http://x/foo?y=xx", string16() }, |
934 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, | 1037 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, |
(...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1498 EXPECT_EQ(8U, loaded_url->input_encodings().size()); | 1601 EXPECT_EQ(8U, loaded_url->input_encodings().size()); |
1499 | 1602 |
1500 // Reload the model to verify it was actually saved to the database and the | 1603 // Reload the model to verify it was actually saved to the database and the |
1501 // duplicate encodings were removed. | 1604 // duplicate encodings were removed. |
1502 test_util_.ResetModel(true); | 1605 test_util_.ResetModel(true); |
1503 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1606 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
1504 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); | 1607 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); |
1505 ASSERT_FALSE(loaded_url == NULL); | 1608 ASSERT_FALSE(loaded_url == NULL); |
1506 EXPECT_EQ(4U, loaded_url->input_encodings().size()); | 1609 EXPECT_EQ(4U, loaded_url->input_encodings().size()); |
1507 } | 1610 } |
OLD | NEW |