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" | |
32 #include "components/search_engines/search_engines_test_util.h" | |
31 #include "components/search_engines/search_host_to_urls_map.h" | 33 #include "components/search_engines/search_host_to_urls_map.h" |
32 #include "components/search_engines/search_terms_data.h" | 34 #include "components/search_engines/search_terms_data.h" |
33 #include "components/search_engines/template_url.h" | 35 #include "components/search_engines/template_url.h" |
34 #include "components/search_engines/template_url_prepopulate_data.h" | 36 #include "components/search_engines/template_url_prepopulate_data.h" |
37 #include "components/sync_preferences/testing_pref_service_syncable.h" | |
35 #include "content/public/test/test_browser_thread_bundle.h" | 38 #include "content/public/test/test_browser_thread_bundle.h" |
36 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
37 | 40 |
38 using base::ASCIIToUTF16; | 41 using base::ASCIIToUTF16; |
39 using base::Time; | 42 using base::Time; |
40 using base::TimeDelta; | 43 using base::TimeDelta; |
41 | 44 |
42 namespace { | 45 namespace { |
43 | 46 |
44 // A prepopulated ID to set for engines we want to show in the default list. | 47 // 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 | 182 // Verifies the two timestamps are equal, within the expected degree of |
180 // precision. | 183 // precision. |
181 void AssertTimesEqual(const base::Time& expected, const base::Time& actual); | 184 void AssertTimesEqual(const base::Time& expected, const base::Time& actual); |
182 | 185 |
183 // Create an URL that appears to have been prepopulated, but won't be in the | 186 // Create an URL that appears to have been prepopulated, but won't be in the |
184 // current data. | 187 // current data. |
185 std::unique_ptr<TemplateURL> CreatePreloadedTemplateURL( | 188 std::unique_ptr<TemplateURL> CreatePreloadedTemplateURL( |
186 bool safe_for_autoreplace, | 189 bool safe_for_autoreplace, |
187 int prepopulate_id); | 190 int prepopulate_id); |
188 | 191 |
192 // Set custom search engine as default fallback through overrides pref. | |
193 void SetOverriddenEngines(); | |
194 | |
189 // Helper methods to make calling TemplateURLServiceTestUtil methods less | 195 // Helper methods to make calling TemplateURLServiceTestUtil methods less |
190 // visually noisy in the test code. | 196 // visually noisy in the test code. |
191 void VerifyObserverCount(int expected_changed_count); | 197 void VerifyObserverCount(int expected_changed_count); |
192 void VerifyObserverFired(); | 198 void VerifyObserverFired(); |
193 TemplateURLServiceTestUtil* test_util() { return test_util_.get(); } | 199 TemplateURLServiceTestUtil* test_util() { return test_util_.get(); } |
194 TemplateURLService* model() { return test_util_->model(); } | 200 TemplateURLService* model() { return test_util_->model(); } |
195 const SearchTermsData& search_terms_data() { | 201 const SearchTermsData& search_terms_data() { |
196 return model()->search_terms_data(); | 202 return model()->search_terms_data(); |
197 } | 203 } |
198 | 204 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
285 data.favicon_url = GURL("http://favicon.url"); | 291 data.favicon_url = GURL("http://favicon.url"); |
286 data.safe_for_autoreplace = safe_for_autoreplace; | 292 data.safe_for_autoreplace = safe_for_autoreplace; |
287 data.input_encodings.push_back("UTF-8"); | 293 data.input_encodings.push_back("UTF-8"); |
288 data.date_created = Time::FromTimeT(100); | 294 data.date_created = Time::FromTimeT(100); |
289 data.last_modified = Time::FromTimeT(100); | 295 data.last_modified = Time::FromTimeT(100); |
290 data.last_visited = Time::FromTimeT(100); | 296 data.last_visited = Time::FromTimeT(100); |
291 data.prepopulate_id = prepopulate_id; | 297 data.prepopulate_id = prepopulate_id; |
292 return base::MakeUnique<TemplateURL>(data); | 298 return base::MakeUnique<TemplateURL>(data); |
293 } | 299 } |
294 | 300 |
301 void TemplateURLServiceTest::SetOverriddenEngines() { | |
302 // Set custom search engine as default fallback through overrides. | |
303 auto entry = base::MakeUnique<base::DictionaryValue>(); | |
304 entry->SetString("name", "override_name"); | |
305 entry->SetString("keyword", "override_keyword"); | |
306 entry->SetString("search_url", "http://override.com/s?q={searchTerms}"); | |
307 entry->SetString("favicon_url", "http://override.com/favicon.ico"); | |
308 entry->SetString("encoding", "UTF-8"); | |
309 entry->SetInteger("id", 1001); | |
310 entry->SetString("suggest_url", | |
311 "http://override.com/suggest?q={searchTerms}"); | |
312 entry->SetString("instant_url", | |
313 "http://override.com/instant?q={searchTerms}"); | |
314 | |
Peter Kasting
2017/02/15 02:09:57
Nit: Up to you, but I wouldn't add this, because |
Alexander Yashkin
2017/02/15 12:46:10
Removed, thanks.
Peter Kasting
2017/02/16 00:12:10
Oh, I meant the blank line -- but if tests don't n
| |
315 auto alternate_urls = base::MakeUnique<base::ListValue>(); | |
316 alternate_urls->AppendString("http://override.com/alternate?q={searchTerms}"); | |
317 entry->Set("alternate_urls", std::move(alternate_urls)); | |
318 | |
319 auto overrides_list = base::MakeUnique<base::ListValue>(); | |
320 overrides_list->Append(std::move(entry)); | |
321 | |
322 auto prefs = test_util()->profile()->GetTestingPrefService(); | |
323 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, | |
324 new base::FundamentalValue(1)); | |
325 prefs->SetUserPref(prefs::kSearchProviderOverrides, overrides_list.release()); | |
326 } | |
327 | |
295 void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) { | 328 void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) { |
296 EXPECT_EQ(expected_changed_count, test_util_->GetObserverCount()); | 329 EXPECT_EQ(expected_changed_count, test_util_->GetObserverCount()); |
297 test_util_->ResetObserverCount(); | 330 test_util_->ResetObserverCount(); |
298 } | 331 } |
299 | 332 |
300 void TemplateURLServiceTest::VerifyObserverFired() { | 333 void TemplateURLServiceTest::VerifyObserverFired() { |
301 EXPECT_LE(1, test_util_->GetObserverCount()); | 334 EXPECT_LE(1, test_util_->GetObserverCount()); |
302 test_util_->ResetObserverCount(); | 335 test_util_->ResetObserverCount(); |
303 } | 336 } |
304 | 337 |
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
886 ExpectSimilar(expected_managed_default.get(), actual_managed_default); | 919 ExpectSimilar(expected_managed_default.get(), actual_managed_default); |
887 | 920 |
888 // The following call has no effect on the managed search engine. | 921 // The following call has no effect on the managed search engine. |
889 model()->RepairPrepopulatedSearchEngines(); | 922 model()->RepairPrepopulatedSearchEngines(); |
890 | 923 |
891 EXPECT_TRUE(model()->is_default_search_managed()); | 924 EXPECT_TRUE(model()->is_default_search_managed()); |
892 actual_managed_default = model()->GetDefaultSearchProvider(); | 925 actual_managed_default = model()->GetDefaultSearchProvider(); |
893 ExpectSimilar(expected_managed_default.get(), actual_managed_default); | 926 ExpectSimilar(expected_managed_default.get(), actual_managed_default); |
894 } | 927 } |
895 | 928 |
929 // Checks that RepairPrepopulatedEngines correctly updates sync guid for default | |
930 // search. Repair is considered a user action and new DSE must be synced to | |
931 // other devices as well. Otherwise previous user selected engine will arrive on | |
932 // next sync attempt. | |
933 TEST_F(TemplateURLServiceTest, RepairPrepopulatedEnginesUpdatesSyncGuid) { | |
934 test_util()->VerifyLoad(); | |
935 | |
936 // The synced DSE GUID should be empty until the user selects something or | |
937 // there is sync activity. | |
938 EXPECT_TRUE(test_util() | |
939 ->profile() | |
940 ->GetTestingPrefService() | |
941 ->GetString(prefs::kSyncedDefaultSearchProviderGUID) | |
942 .empty()); | |
943 | |
944 TemplateURL* initial_dse = model()->GetDefaultSearchProvider(); | |
945 ASSERT_TRUE(initial_dse); | |
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 // Check that user dse is different from initial. | |
955 EXPECT_NE(initial_dse, user_dse); | |
956 | |
957 // Check that user DSE guid is stored in kSyncedDefaultSearchProviderGUID. | |
958 EXPECT_EQ(user_dse->sync_guid(), | |
959 test_util()->profile()->GetTestingPrefService()->GetString( | |
960 prefs::kSyncedDefaultSearchProviderGUID)); | |
961 | |
962 model()->RepairPrepopulatedSearchEngines(); | |
963 | |
964 // Check that initial search engine is returned as default after repair. | |
965 ASSERT_EQ(initial_dse, model()->GetDefaultSearchProvider()); | |
966 // Check that initial_dse guid is stored in kSyncedDefaultSearchProviderGUID. | |
967 const std::string dse_guid = | |
968 test_util()->profile()->GetTestingPrefService()->GetString( | |
969 prefs::kSyncedDefaultSearchProviderGUID); | |
970 EXPECT_EQ(initial_dse->sync_guid(), dse_guid); | |
971 EXPECT_EQ(initial_dse->keyword(), | |
972 model()->GetTemplateURLForGUID(dse_guid)->keyword()); | |
973 } | |
974 | |
975 // Checks that RepairPrepopulatedEngines correctly updates sync guid for default | |
976 // search when search engines are overridden using pref. | |
977 TEST_F(TemplateURLServiceTest, | |
978 RepairPrepopulatedEnginesWithOverridesUpdatesSyncGuid) { | |
979 SetOverriddenEngines(); | |
980 test_util()->VerifyLoad(); | |
981 | |
982 // The synced DSE GUID should be empty until the user selects something or | |
983 // there is sync activity. | |
984 EXPECT_TRUE(test_util() | |
985 ->profile() | |
986 ->GetTestingPrefService() | |
987 ->GetString(prefs::kSyncedDefaultSearchProviderGUID) | |
988 .empty()); | |
989 | |
990 TemplateURL* overridden_engine = | |
991 model()->GetTemplateURLForKeyword(ASCIIToUTF16("override_keyword")); | |
992 ASSERT_TRUE(overridden_engine); | |
993 | |
994 EXPECT_EQ(overridden_engine, model()->GetDefaultSearchProvider()); | |
995 | |
996 // Add user provided default search engine. | |
997 TemplateURL* user_dse = AddKeywordWithDate( | |
998 "user_dse", "user_dse.com", "http://www.user_dse.com/s?q={searchTerms}", | |
999 std::string(), std::string(), std::string(), true, "UTF-8", Time(), | |
1000 Time(), Time()); | |
1001 model()->SetUserSelectedDefaultSearchProvider(user_dse); | |
1002 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); | |
1003 | |
1004 // Check that user DSE guid is stored in kSyncedDefaultSearchProviderGUID. | |
1005 EXPECT_EQ(user_dse->sync_guid(), | |
1006 test_util()->profile()->GetTestingPrefService()->GetString( | |
1007 prefs::kSyncedDefaultSearchProviderGUID)); | |
1008 | |
1009 model()->RepairPrepopulatedSearchEngines(); | |
1010 | |
1011 // Check that overridden engine is returned as default after repair. | |
1012 ASSERT_EQ(overridden_engine, model()->GetDefaultSearchProvider()); | |
1013 // Check that overridden_engine guid is stored in | |
1014 // kSyncedDefaultSearchProviderGUID. | |
1015 const std::string dse_guid = | |
1016 test_util()->profile()->GetTestingPrefService()->GetString( | |
1017 prefs::kSyncedDefaultSearchProviderGUID); | |
1018 EXPECT_EQ(overridden_engine->sync_guid(), dse_guid); | |
1019 EXPECT_EQ(overridden_engine->keyword(), | |
1020 model()->GetTemplateURLForGUID(dse_guid)->keyword()); | |
1021 } | |
1022 | |
1023 // Checks that RepairPrepopulatedEngines correctly updates sync guid for default | |
1024 // search when search engines is overridden by extension. | |
1025 TEST_F(TemplateURLServiceTest, | |
1026 RepairPrepopulatedEnginesWithExtensionUpdatesSyncGuid) { | |
1027 test_util()->VerifyLoad(); | |
1028 | |
1029 // The synced DSE GUID should be empty until the user selects something or | |
1030 // there is sync activity. | |
1031 EXPECT_TRUE(test_util() | |
1032 ->profile() | |
1033 ->GetTestingPrefService() | |
1034 ->GetString(prefs::kSyncedDefaultSearchProviderGUID) | |
1035 .empty()); | |
1036 | |
1037 // Get initial DSE to check later its guid. | |
Peter Kasting
2017/02/15 02:09:57
Nit: later its guid -> its guid later
Alexander Yashkin
2017/02/15 12:46:10
Done
| |
1038 TemplateURL* initial_dse = model()->GetDefaultSearchProvider(); | |
1039 ASSERT_TRUE(initial_dse); | |
1040 | |
1041 // Add user provided default search engine. | |
1042 TemplateURL* user_dse = model()->Add( | |
1043 base::MakeUnique<TemplateURL>(*GenerateDummyTemplateURLData("user_dse"))); | |
1044 model()->SetUserSelectedDefaultSearchProvider(user_dse); | |
1045 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); | |
1046 | |
1047 // Check that user DSE guid is stored in kSyncedDefaultSearchProviderGUID. | |
1048 EXPECT_EQ(user_dse->sync_guid(), | |
1049 test_util()->profile()->GetTestingPrefService()->GetString( | |
1050 prefs::kSyncedDefaultSearchProviderGUID)); | |
1051 | |
1052 // Add extension controlled default search engine. | |
1053 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> extension_info( | |
1054 new TemplateURL::AssociatedExtensionInfo("extension")); | |
1055 extension_info->wants_to_be_default_engine = true; | |
1056 TemplateURL* extension_dse = model()->AddExtensionControlledTURL( | |
1057 base::MakeUnique<TemplateURL>( | |
1058 *GenerateDummyTemplateURLData("extension_dse"), | |
1059 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION), | |
1060 std::move(extension_info)); | |
1061 EXPECT_EQ(extension_dse, model()->GetDefaultSearchProvider()); | |
1062 // Check that user DSE guid is still stored in | |
1063 // kSyncedDefaultSearchProviderGUID. | |
1064 EXPECT_EQ(user_dse->sync_guid(), | |
1065 test_util()->profile()->GetTestingPrefService()->GetString( | |
1066 prefs::kSyncedDefaultSearchProviderGUID)); | |
1067 | |
1068 model()->RepairPrepopulatedSearchEngines(); | |
1069 // Check that extension engine is still default but sync guid is updated to | |
1070 // initial dse guid. | |
1071 EXPECT_EQ(extension_dse, model()->GetDefaultSearchProvider()); | |
1072 EXPECT_EQ(initial_dse->sync_guid(), | |
1073 test_util()->profile()->GetTestingPrefService()->GetString( | |
1074 prefs::kSyncedDefaultSearchProviderGUID)); | |
1075 } | |
1076 | |
896 TEST_F(TemplateURLServiceTest, UpdateKeywordSearchTermsForURL) { | 1077 TEST_F(TemplateURLServiceTest, UpdateKeywordSearchTermsForURL) { |
897 struct TestData { | 1078 struct TestData { |
898 const std::string url; | 1079 const std::string url; |
899 const base::string16 term; | 1080 const base::string16 term; |
900 } data[] = { | 1081 } data[] = { |
901 { "http://foo/", base::string16() }, | 1082 { "http://foo/", base::string16() }, |
902 { "http://foo/foo?q=xx", base::string16() }, | 1083 { "http://foo/foo?q=xx", base::string16() }, |
903 { "http://x/bar?q=xx", base::string16() }, | 1084 { "http://x/bar?q=xx", base::string16() }, |
904 { "http://x/foo?y=xx", base::string16() }, | 1085 { "http://x/foo?y=xx", base::string16() }, |
905 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, | 1086 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, |
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1542 TemplateURL* update_url = | 1723 TemplateURL* update_url = |
1543 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); | 1724 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); |
1544 const base::Time update_last_modified = update_url->last_modified(); | 1725 const base::Time update_last_modified = update_url->last_modified(); |
1545 model()->SetUserSelectedDefaultSearchProvider(update_url); | 1726 model()->SetUserSelectedDefaultSearchProvider(update_url); |
1546 TemplateURL* reloaded_url = | 1727 TemplateURL* reloaded_url = |
1547 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); | 1728 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); |
1548 const base::Time reloaded_last_modified = reloaded_url->last_modified(); | 1729 const base::Time reloaded_last_modified = reloaded_url->last_modified(); |
1549 EXPECT_NE(original_last_modified, reloaded_last_modified); | 1730 EXPECT_NE(original_last_modified, reloaded_last_modified); |
1550 EXPECT_EQ(update_last_modified, reloaded_last_modified); | 1731 EXPECT_EQ(update_last_modified, reloaded_last_modified); |
1551 } | 1732 } |
OLD | NEW |