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 10 matching lines...) Expand all Loading... |
21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
22 #include "base/task/cancelable_task_tracker.h" | 22 #include "base/task/cancelable_task_tracker.h" |
23 #include "base/test/simple_test_clock.h" | 23 #include "base/test/simple_test_clock.h" |
24 #include "base/threading/thread.h" | 24 #include "base/threading/thread.h" |
25 #include "base/time/time.h" | 25 #include "base/time/time.h" |
26 #include "chrome/browser/history/history_service_factory.h" | 26 #include "chrome/browser/history/history_service_factory.h" |
27 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 27 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
28 #include "chrome/test/base/testing_profile.h" | 28 #include "chrome/test/base/testing_profile.h" |
29 #include "components/history/core/browser/history_service.h" | 29 #include "components/history/core/browser/history_service.h" |
30 #include "components/search_engines/keyword_web_data_service.h" | 30 #include "components/search_engines/keyword_web_data_service.h" |
| 31 #include "components/search_engines/search_engines_pref_names.h" |
31 #include "components/search_engines/search_host_to_urls_map.h" | 32 #include "components/search_engines/search_host_to_urls_map.h" |
32 #include "components/search_engines/search_terms_data.h" | 33 #include "components/search_engines/search_terms_data.h" |
33 #include "components/search_engines/template_url.h" | 34 #include "components/search_engines/template_url.h" |
34 #include "components/search_engines/template_url_prepopulate_data.h" | 35 #include "components/search_engines/template_url_prepopulate_data.h" |
| 36 #include "components/sync_preferences/testing_pref_service_syncable.h" |
35 #include "content/public/test/test_browser_thread_bundle.h" | 37 #include "content/public/test/test_browser_thread_bundle.h" |
36 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
37 | 39 |
38 using base::ASCIIToUTF16; | 40 using base::ASCIIToUTF16; |
39 using base::Time; | 41 using base::Time; |
40 using base::TimeDelta; | 42 using base::TimeDelta; |
41 | 43 |
42 namespace { | 44 namespace { |
43 | 45 |
44 // A prepopulated ID to set for engines we want to show in the default list. | 46 // A prepopulated ID to set for engines we want to show in the default list. |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 // Verifies the two timestamps are equal, within the expected degree of | 181 // Verifies the two timestamps are equal, within the expected degree of |
180 // precision. | 182 // precision. |
181 void AssertTimesEqual(const base::Time& expected, const base::Time& actual); | 183 void AssertTimesEqual(const base::Time& expected, const base::Time& actual); |
182 | 184 |
183 // Create an URL that appears to have been prepopulated, but won't be in the | 185 // Create an URL that appears to have been prepopulated, but won't be in the |
184 // current data. | 186 // current data. |
185 std::unique_ptr<TemplateURL> CreatePreloadedTemplateURL( | 187 std::unique_ptr<TemplateURL> CreatePreloadedTemplateURL( |
186 bool safe_for_autoreplace, | 188 bool safe_for_autoreplace, |
187 int prepopulate_id); | 189 int prepopulate_id); |
188 | 190 |
| 191 // Set custom search engine as default through overrides pref. |
| 192 void SetOverriddenEngines(); |
| 193 |
189 // Helper methods to make calling TemplateURLServiceTestUtil methods less | 194 // Helper methods to make calling TemplateURLServiceTestUtil methods less |
190 // visually noisy in the test code. | 195 // visually noisy in the test code. |
191 void VerifyObserverCount(int expected_changed_count); | 196 void VerifyObserverCount(int expected_changed_count); |
192 void VerifyObserverFired(); | 197 void VerifyObserverFired(); |
193 TemplateURLServiceTestUtil* test_util() { return test_util_.get(); } | 198 TemplateURLServiceTestUtil* test_util() { return test_util_.get(); } |
194 TemplateURLService* model() { return test_util_->model(); } | 199 TemplateURLService* model() { return test_util_->model(); } |
195 const SearchTermsData& search_terms_data() { | 200 const SearchTermsData& search_terms_data() { |
196 return model()->search_terms_data(); | 201 return model()->search_terms_data(); |
197 } | 202 } |
198 | 203 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 data.favicon_url = GURL("http://favicon.url"); | 290 data.favicon_url = GURL("http://favicon.url"); |
286 data.safe_for_autoreplace = safe_for_autoreplace; | 291 data.safe_for_autoreplace = safe_for_autoreplace; |
287 data.input_encodings.push_back("UTF-8"); | 292 data.input_encodings.push_back("UTF-8"); |
288 data.date_created = Time::FromTimeT(100); | 293 data.date_created = Time::FromTimeT(100); |
289 data.last_modified = Time::FromTimeT(100); | 294 data.last_modified = Time::FromTimeT(100); |
290 data.last_visited = Time::FromTimeT(100); | 295 data.last_visited = Time::FromTimeT(100); |
291 data.prepopulate_id = prepopulate_id; | 296 data.prepopulate_id = prepopulate_id; |
292 return base::MakeUnique<TemplateURL>(data); | 297 return base::MakeUnique<TemplateURL>(data); |
293 } | 298 } |
294 | 299 |
| 300 void TemplateURLServiceTest::SetOverriddenEngines() { |
| 301 // Set custom search engine as default through overrides. |
| 302 auto prefs = test_util()->profile()->GetTestingPrefService(); |
| 303 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, |
| 304 new base::FundamentalValue(1)); |
| 305 auto overrides_list = base::MakeUnique<base::ListValue>(); |
| 306 auto entry = base::MakeUnique<base::DictionaryValue>(); |
| 307 |
| 308 entry->SetString("name", "override_name"); |
| 309 entry->SetString("keyword", "override_keyword"); |
| 310 entry->SetString("search_url", "http://override.com/s?q={searchTerms}"); |
| 311 entry->SetString("favicon_url", "http://override.com/favicon.ico"); |
| 312 entry->SetString("encoding", "UTF-8"); |
| 313 entry->SetInteger("id", 1001); |
| 314 entry->SetString("suggest_url", |
| 315 "http://override.com/suggest?q={searchTerms}"); |
| 316 entry->SetString("instant_url", |
| 317 "http://override.com/instant?q={searchTerms}"); |
| 318 auto alternate_urls = base::MakeUnique<base::ListValue>(); |
| 319 alternate_urls->AppendString("http://override.com/alternate?q={searchTerms}"); |
| 320 entry->Set("alternate_urls", std::move(alternate_urls)); |
| 321 overrides_list->Append(std::move(entry)); |
| 322 |
| 323 prefs->SetUserPref(prefs::kSearchProviderOverrides, overrides_list.release()); |
| 324 } |
| 325 |
295 void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) { | 326 void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) { |
296 EXPECT_EQ(expected_changed_count, test_util_->GetObserverCount()); | 327 EXPECT_EQ(expected_changed_count, test_util_->GetObserverCount()); |
297 test_util_->ResetObserverCount(); | 328 test_util_->ResetObserverCount(); |
298 } | 329 } |
299 | 330 |
300 void TemplateURLServiceTest::VerifyObserverFired() { | 331 void TemplateURLServiceTest::VerifyObserverFired() { |
301 EXPECT_LE(1, test_util_->GetObserverCount()); | 332 EXPECT_LE(1, test_util_->GetObserverCount()); |
302 test_util_->ResetObserverCount(); | 333 test_util_->ResetObserverCount(); |
303 } | 334 } |
304 | 335 |
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
886 ExpectSimilar(expected_managed_default.get(), actual_managed_default); | 917 ExpectSimilar(expected_managed_default.get(), actual_managed_default); |
887 | 918 |
888 // The following call has no effect on the managed search engine. | 919 // The following call has no effect on the managed search engine. |
889 model()->RepairPrepopulatedSearchEngines(); | 920 model()->RepairPrepopulatedSearchEngines(); |
890 | 921 |
891 EXPECT_TRUE(model()->is_default_search_managed()); | 922 EXPECT_TRUE(model()->is_default_search_managed()); |
892 actual_managed_default = model()->GetDefaultSearchProvider(); | 923 actual_managed_default = model()->GetDefaultSearchProvider(); |
893 ExpectSimilar(expected_managed_default.get(), actual_managed_default); | 924 ExpectSimilar(expected_managed_default.get(), actual_managed_default); |
894 } | 925 } |
895 | 926 |
| 927 // Test checks that RepairPrepopulatedEngines correctly updates sync guid for |
| 928 // default search. Repair is considered a user action and new DSE must |
| 929 // be synced to other devices as well. Otherwise previous user selected engine |
| 930 // will arrive on next sync attempt. |
| 931 TEST_F(TemplateURLServiceTest, RepairPrepopulatedEnginesUpdatesSyncGuid) { |
| 932 test_util()->VerifyLoad(); |
| 933 |
| 934 // The synced DSE GUID should be empty until the user selects something or |
| 935 // there is sync activity. |
| 936 EXPECT_TRUE(test_util() |
| 937 ->profile() |
| 938 ->GetTestingPrefService() |
| 939 ->GetString(prefs::kSyncedDefaultSearchProviderGUID) |
| 940 .empty()); |
| 941 |
| 942 // Google engine must exist. |
| 943 TemplateURL* google = |
| 944 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.com")); |
| 945 ASSERT_TRUE(google); |
| 946 |
| 947 // Add user provided default search engine. |
| 948 TemplateURL* user_dse = AddKeywordWithDate( |
| 949 "user_dse", "user_dse.com", "http://www.user_dse.com/s?q={searchTerms}", |
| 950 std::string(), std::string(), std::string(), true, "UTF-8", Time(), |
| 951 Time(), Time()); |
| 952 model()->SetUserSelectedDefaultSearchProvider(user_dse); |
| 953 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); |
| 954 |
| 955 // Check that user DSE guid is stored in kSyncedDefaultSearchProviderGUID. |
| 956 EXPECT_EQ(user_dse->sync_guid(), |
| 957 test_util()->profile()->GetTestingPrefService()->GetString( |
| 958 prefs::kSyncedDefaultSearchProviderGUID)); |
| 959 |
| 960 model()->RepairPrepopulatedSearchEngines(); |
| 961 |
| 962 // Check that Google is default after repair. |
| 963 ASSERT_EQ(google, model()->GetDefaultSearchProvider()); |
| 964 // Check that google guid is stored in kSyncedDefaultSearchProviderGUID. |
| 965 const std::string dse_guid = |
| 966 test_util()->profile()->GetTestingPrefService()->GetString( |
| 967 prefs::kSyncedDefaultSearchProviderGUID); |
| 968 EXPECT_EQ(google->sync_guid(), dse_guid); |
| 969 EXPECT_EQ(google->keyword(), |
| 970 model()->GetTemplateURLForGUID(dse_guid)->keyword()); |
| 971 } |
| 972 |
| 973 // Test checks that RepairPrepopulatedEngines correctly updates sync guid for |
| 974 // default search when search engines are overridden using pref. |
| 975 TEST_F(TemplateURLServiceTest, |
| 976 RepairPrepopulatedEnginesWithOverridesUpdatesSyncGuid) { |
| 977 SetOverriddenEngines(); |
| 978 test_util()->VerifyLoad(); |
| 979 |
| 980 // The synced DSE GUID should be empty until the user selects something or |
| 981 // there is sync activity. |
| 982 EXPECT_TRUE(test_util() |
| 983 ->profile() |
| 984 ->GetTestingPrefService() |
| 985 ->GetString(prefs::kSyncedDefaultSearchProviderGUID) |
| 986 .empty()); |
| 987 |
| 988 TemplateURL* overridden_engine = |
| 989 model()->GetTemplateURLForKeyword(ASCIIToUTF16("override_keyword")); |
| 990 ASSERT_TRUE(overridden_engine); |
| 991 |
| 992 EXPECT_EQ(overridden_engine, model()->GetDefaultSearchProvider()); |
| 993 |
| 994 // Add user provided default search engine. |
| 995 TemplateURL* user_dse = AddKeywordWithDate( |
| 996 "user_dse", "user_dse.com", "http://www.user_dse.com/s?q={searchTerms}", |
| 997 std::string(), std::string(), std::string(), true, "UTF-8", Time(), |
| 998 Time(), Time()); |
| 999 model()->SetUserSelectedDefaultSearchProvider(user_dse); |
| 1000 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); |
| 1001 |
| 1002 // Check that user DSE guid is stored in kSyncedDefaultSearchProviderGUID. |
| 1003 EXPECT_EQ(user_dse->sync_guid(), |
| 1004 test_util()->profile()->GetTestingPrefService()->GetString( |
| 1005 prefs::kSyncedDefaultSearchProviderGUID)); |
| 1006 |
| 1007 model()->RepairPrepopulatedSearchEngines(); |
| 1008 |
| 1009 // Check that overridden engine is returned as default after repair. |
| 1010 ASSERT_EQ(overridden_engine, model()->GetDefaultSearchProvider()); |
| 1011 // Check that overridden_engine guid is stored in |
| 1012 // kSyncedDefaultSearchProviderGUID. |
| 1013 const std::string dse_guid = |
| 1014 test_util()->profile()->GetTestingPrefService()->GetString( |
| 1015 prefs::kSyncedDefaultSearchProviderGUID); |
| 1016 EXPECT_EQ(overridden_engine->sync_guid(), dse_guid); |
| 1017 EXPECT_EQ(overridden_engine->keyword(), |
| 1018 model()->GetTemplateURLForGUID(dse_guid)->keyword()); |
| 1019 } |
| 1020 |
896 TEST_F(TemplateURLServiceTest, UpdateKeywordSearchTermsForURL) { | 1021 TEST_F(TemplateURLServiceTest, UpdateKeywordSearchTermsForURL) { |
897 struct TestData { | 1022 struct TestData { |
898 const std::string url; | 1023 const std::string url; |
899 const base::string16 term; | 1024 const base::string16 term; |
900 } data[] = { | 1025 } data[] = { |
901 { "http://foo/", base::string16() }, | 1026 { "http://foo/", base::string16() }, |
902 { "http://foo/foo?q=xx", base::string16() }, | 1027 { "http://foo/foo?q=xx", base::string16() }, |
903 { "http://x/bar?q=xx", base::string16() }, | 1028 { "http://x/bar?q=xx", base::string16() }, |
904 { "http://x/foo?y=xx", base::string16() }, | 1029 { "http://x/foo?y=xx", base::string16() }, |
905 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, | 1030 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, |
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1542 TemplateURL* update_url = | 1667 TemplateURL* update_url = |
1543 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); | 1668 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); |
1544 const base::Time update_last_modified = update_url->last_modified(); | 1669 const base::Time update_last_modified = update_url->last_modified(); |
1545 model()->SetUserSelectedDefaultSearchProvider(update_url); | 1670 model()->SetUserSelectedDefaultSearchProvider(update_url); |
1546 TemplateURL* reloaded_url = | 1671 TemplateURL* reloaded_url = |
1547 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); | 1672 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); |
1548 const base::Time reloaded_last_modified = reloaded_url->last_modified(); | 1673 const base::Time reloaded_last_modified = reloaded_url->last_modified(); |
1549 EXPECT_NE(original_last_modified, reloaded_last_modified); | 1674 EXPECT_NE(original_last_modified, reloaded_last_modified); |
1550 EXPECT_EQ(update_last_modified, reloaded_last_modified); | 1675 EXPECT_EQ(update_last_modified, reloaded_last_modified); |
1551 } | 1676 } |
OLD | NEW |