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 through overrides pref. | |
vasilii
2017/02/13 14:37:54
Does it overwrite the user prefs?
Alexander Yashkin
2017/02/14 12:04:43
Where is user pref kSearchProviderOverrides that c
| |
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 through overrides. | |
303 auto prefs = test_util()->profile()->GetTestingPrefService(); | |
304 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, | |
305 new base::FundamentalValue(1)); | |
306 auto overrides_list = base::MakeUnique<base::ListValue>(); | |
vasilii
2017/02/13 14:37:54
I'd move this definition down to where it's used.
Peter Kasting
2017/02/13 22:24:57
This, and adjust the other declarations and blank
Alexander Yashkin
2017/02/14 12:04:43
Done
| |
307 auto entry = base::MakeUnique<base::DictionaryValue>(); | |
308 | |
309 entry->SetString("name", "override_name"); | |
310 entry->SetString("keyword", "override_keyword"); | |
311 entry->SetString("search_url", "http://override.com/s?q={searchTerms}"); | |
312 entry->SetString("favicon_url", "http://override.com/favicon.ico"); | |
313 entry->SetString("encoding", "UTF-8"); | |
314 entry->SetInteger("id", 1001); | |
315 entry->SetString("suggest_url", | |
316 "http://override.com/suggest?q={searchTerms}"); | |
317 entry->SetString("instant_url", | |
318 "http://override.com/instant?q={searchTerms}"); | |
319 auto alternate_urls = base::MakeUnique<base::ListValue>(); | |
320 alternate_urls->AppendString("http://override.com/alternate?q={searchTerms}"); | |
321 entry->Set("alternate_urls", std::move(alternate_urls)); | |
322 overrides_list->Append(std::move(entry)); | |
323 | |
324 prefs->SetUserPref(prefs::kSearchProviderOverrides, overrides_list.release()); | |
325 } | |
326 | |
295 void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) { | 327 void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) { |
296 EXPECT_EQ(expected_changed_count, test_util_->GetObserverCount()); | 328 EXPECT_EQ(expected_changed_count, test_util_->GetObserverCount()); |
297 test_util_->ResetObserverCount(); | 329 test_util_->ResetObserverCount(); |
298 } | 330 } |
299 | 331 |
300 void TemplateURLServiceTest::VerifyObserverFired() { | 332 void TemplateURLServiceTest::VerifyObserverFired() { |
301 EXPECT_LE(1, test_util_->GetObserverCount()); | 333 EXPECT_LE(1, test_util_->GetObserverCount()); |
302 test_util_->ResetObserverCount(); | 334 test_util_->ResetObserverCount(); |
303 } | 335 } |
304 | 336 |
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
886 ExpectSimilar(expected_managed_default.get(), actual_managed_default); | 918 ExpectSimilar(expected_managed_default.get(), actual_managed_default); |
887 | 919 |
888 // The following call has no effect on the managed search engine. | 920 // The following call has no effect on the managed search engine. |
889 model()->RepairPrepopulatedSearchEngines(); | 921 model()->RepairPrepopulatedSearchEngines(); |
890 | 922 |
891 EXPECT_TRUE(model()->is_default_search_managed()); | 923 EXPECT_TRUE(model()->is_default_search_managed()); |
892 actual_managed_default = model()->GetDefaultSearchProvider(); | 924 actual_managed_default = model()->GetDefaultSearchProvider(); |
893 ExpectSimilar(expected_managed_default.get(), actual_managed_default); | 925 ExpectSimilar(expected_managed_default.get(), actual_managed_default); |
894 } | 926 } |
895 | 927 |
928 // Test checks that RepairPrepopulatedEngines correctly updates sync guid for | |
Peter Kasting
2017/02/13 22:24:57
Nit: Test checks -> Checks (3 places)
Alexander Yashkin
2017/02/14 12:04:43
Done
| |
929 // default search. Repair is considered a user action and new DSE must | |
930 // be synced to other devices as well. Otherwise previous user selected engine | |
931 // will arrive on next sync attempt. | |
932 TEST_F(TemplateURLServiceTest, RepairPrepopulatedEnginesUpdatesSyncGuid) { | |
933 test_util()->VerifyLoad(); | |
934 | |
935 // The synced DSE GUID should be empty until the user selects something or | |
936 // there is sync activity. | |
937 EXPECT_TRUE(test_util() | |
938 ->profile() | |
939 ->GetTestingPrefService() | |
940 ->GetString(prefs::kSyncedDefaultSearchProviderGUID) | |
941 .empty()); | |
942 | |
943 // Google engine must exist. | |
Peter Kasting
2017/02/13 22:24:57
Rather than looking explicitly for Google, would i
Alexander Yashkin
2017/02/14 12:04:43
Done.
| |
944 TemplateURL* google = | |
945 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.com")); | |
946 ASSERT_TRUE(google); | |
947 | |
948 // Add user provided default search engine. | |
949 TemplateURL* user_dse = AddKeywordWithDate( | |
950 "user_dse", "user_dse.com", "http://www.user_dse.com/s?q={searchTerms}", | |
951 std::string(), std::string(), std::string(), true, "UTF-8", Time(), | |
952 Time(), Time()); | |
953 model()->SetUserSelectedDefaultSearchProvider(user_dse); | |
954 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); | |
955 | |
956 // Check that user DSE guid is stored in kSyncedDefaultSearchProviderGUID. | |
957 EXPECT_EQ(user_dse->sync_guid(), | |
958 test_util()->profile()->GetTestingPrefService()->GetString( | |
959 prefs::kSyncedDefaultSearchProviderGUID)); | |
960 | |
961 model()->RepairPrepopulatedSearchEngines(); | |
962 | |
963 // Check that Google is default after repair. | |
964 ASSERT_EQ(google, model()->GetDefaultSearchProvider()); | |
965 // Check that google guid is stored in kSyncedDefaultSearchProviderGUID. | |
966 const std::string dse_guid = | |
967 test_util()->profile()->GetTestingPrefService()->GetString( | |
968 prefs::kSyncedDefaultSearchProviderGUID); | |
969 EXPECT_EQ(google->sync_guid(), dse_guid); | |
970 EXPECT_EQ(google->keyword(), | |
971 model()->GetTemplateURLForGUID(dse_guid)->keyword()); | |
972 } | |
973 | |
974 // Test checks that RepairPrepopulatedEngines correctly updates sync guid for | |
975 // default search when search engines are overridden using pref. | |
976 TEST_F(TemplateURLServiceTest, | |
977 RepairPrepopulatedEnginesWithOverridesUpdatesSyncGuid) { | |
vasilii
2017/02/13 14:37:54
I fail to understand how this test works and why i
Peter Kasting
2017/02/13 22:24:57
It sets the kSearchProviderOverrides pref, which t
Alexander Yashkin
2017/02/14 12:04:43
I tried to describe how kSearchProviderOverrides w
| |
978 SetOverriddenEngines(); | |
979 test_util()->VerifyLoad(); | |
980 | |
981 // The synced DSE GUID should be empty until the user selects something or | |
982 // there is sync activity. | |
983 EXPECT_TRUE(test_util() | |
984 ->profile() | |
985 ->GetTestingPrefService() | |
986 ->GetString(prefs::kSyncedDefaultSearchProviderGUID) | |
987 .empty()); | |
988 | |
989 TemplateURL* overridden_engine = | |
990 model()->GetTemplateURLForKeyword(ASCIIToUTF16("override_keyword")); | |
991 ASSERT_TRUE(overridden_engine); | |
992 | |
993 EXPECT_EQ(overridden_engine, model()->GetDefaultSearchProvider()); | |
994 | |
995 // Add user provided default search engine. | |
996 TemplateURL* user_dse = AddKeywordWithDate( | |
997 "user_dse", "user_dse.com", "http://www.user_dse.com/s?q={searchTerms}", | |
998 std::string(), std::string(), std::string(), true, "UTF-8", Time(), | |
999 Time(), Time()); | |
1000 model()->SetUserSelectedDefaultSearchProvider(user_dse); | |
1001 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); | |
1002 | |
1003 // Check that user DSE guid is stored in kSyncedDefaultSearchProviderGUID. | |
1004 EXPECT_EQ(user_dse->sync_guid(), | |
1005 test_util()->profile()->GetTestingPrefService()->GetString( | |
1006 prefs::kSyncedDefaultSearchProviderGUID)); | |
1007 | |
1008 model()->RepairPrepopulatedSearchEngines(); | |
1009 | |
1010 // Check that overridden engine is returned as default after repair. | |
1011 ASSERT_EQ(overridden_engine, model()->GetDefaultSearchProvider()); | |
1012 // Check that overridden_engine guid is stored in | |
1013 // kSyncedDefaultSearchProviderGUID. | |
1014 const std::string dse_guid = | |
1015 test_util()->profile()->GetTestingPrefService()->GetString( | |
1016 prefs::kSyncedDefaultSearchProviderGUID); | |
1017 EXPECT_EQ(overridden_engine->sync_guid(), dse_guid); | |
1018 EXPECT_EQ(overridden_engine->keyword(), | |
1019 model()->GetTemplateURLForGUID(dse_guid)->keyword()); | |
1020 } | |
1021 | |
1022 // Test checks that RepairPrepopulatedEngines correctly updates sync guid for | |
1023 // default search when search engines is overridden by extension. | |
1024 TEST_F(TemplateURLServiceTest, | |
1025 RepairPrepopulatedEnginesWithExtensionUpdatesSyncGuid) { | |
1026 test_util()->VerifyLoad(); | |
1027 | |
1028 // The synced DSE GUID should be empty until the user selects something or | |
1029 // there is sync activity. | |
1030 EXPECT_TRUE(test_util() | |
1031 ->profile() | |
1032 ->GetTestingPrefService() | |
1033 ->GetString(prefs::kSyncedDefaultSearchProviderGUID) | |
1034 .empty()); | |
1035 // Add user provided default search engine. | |
Peter Kasting
2017/02/13 22:24:57
Nit: Blank line above this
Alexander Yashkin
2017/02/14 12:04:43
Done
| |
1036 TemplateURL* user_dse = model()->Add( | |
1037 base::MakeUnique<TemplateURL>(*GenerateDummyTemplateURLData("user_dse"))); | |
1038 model()->SetUserSelectedDefaultSearchProvider(user_dse); | |
1039 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); | |
1040 | |
1041 // Check that user DSE guid is stored in kSyncedDefaultSearchProviderGUID. | |
1042 EXPECT_EQ(user_dse->sync_guid(), | |
1043 test_util()->profile()->GetTestingPrefService()->GetString( | |
1044 prefs::kSyncedDefaultSearchProviderGUID)); | |
1045 | |
1046 // Add extension controlled default search engine. | |
1047 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> extension_info( | |
1048 new TemplateURL::AssociatedExtensionInfo("extension")); | |
1049 extension_info->wants_to_be_default_engine = true; | |
1050 TemplateURL* extension_dse = model()->AddExtensionControlledTURL( | |
1051 base::MakeUnique<TemplateURL>( | |
1052 *GenerateDummyTemplateURLData("extension_dse"), | |
1053 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION), | |
1054 std::move(extension_info)); | |
1055 EXPECT_EQ(extension_dse, model()->GetDefaultSearchProvider()); | |
1056 // Check that user DSE guid is still stored in | |
1057 // kSyncedDefaultSearchProviderGUID. | |
1058 EXPECT_EQ(user_dse->sync_guid(), | |
1059 test_util()->profile()->GetTestingPrefService()->GetString( | |
1060 prefs::kSyncedDefaultSearchProviderGUID)); | |
1061 | |
1062 model()->RepairPrepopulatedSearchEngines(); | |
1063 // Check that extension engine is still default but sync guid is updated to | |
1064 // google. | |
Peter Kasting
2017/02/13 22:24:57
Again, I'd try to write this to check for the prio
Alexander Yashkin
2017/02/14 12:04:43
Fixed, thanks.
| |
1065 EXPECT_EQ(extension_dse, model()->GetDefaultSearchProvider()); | |
1066 | |
1067 TemplateURL* google = | |
1068 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.com")); | |
1069 ASSERT_TRUE(google); | |
1070 | |
1071 EXPECT_EQ(google->sync_guid(), | |
1072 test_util()->profile()->GetTestingPrefService()->GetString( | |
1073 prefs::kSyncedDefaultSearchProviderGUID)); | |
1074 } | |
1075 | |
896 TEST_F(TemplateURLServiceTest, UpdateKeywordSearchTermsForURL) { | 1076 TEST_F(TemplateURLServiceTest, UpdateKeywordSearchTermsForURL) { |
897 struct TestData { | 1077 struct TestData { |
898 const std::string url; | 1078 const std::string url; |
899 const base::string16 term; | 1079 const base::string16 term; |
900 } data[] = { | 1080 } data[] = { |
901 { "http://foo/", base::string16() }, | 1081 { "http://foo/", base::string16() }, |
902 { "http://foo/foo?q=xx", base::string16() }, | 1082 { "http://foo/foo?q=xx", base::string16() }, |
903 { "http://x/bar?q=xx", base::string16() }, | 1083 { "http://x/bar?q=xx", base::string16() }, |
904 { "http://x/foo?y=xx", base::string16() }, | 1084 { "http://x/foo?y=xx", base::string16() }, |
905 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, | 1085 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, |
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1542 TemplateURL* update_url = | 1722 TemplateURL* update_url = |
1543 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); | 1723 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); |
1544 const base::Time update_last_modified = update_url->last_modified(); | 1724 const base::Time update_last_modified = update_url->last_modified(); |
1545 model()->SetUserSelectedDefaultSearchProvider(update_url); | 1725 model()->SetUserSelectedDefaultSearchProvider(update_url); |
1546 TemplateURL* reloaded_url = | 1726 TemplateURL* reloaded_url = |
1547 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); | 1727 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); |
1548 const base::Time reloaded_last_modified = reloaded_url->last_modified(); | 1728 const base::Time reloaded_last_modified = reloaded_url->last_modified(); |
1549 EXPECT_NE(original_last_modified, reloaded_last_modified); | 1729 EXPECT_NE(original_last_modified, reloaded_last_modified); |
1550 EXPECT_EQ(update_last_modified, reloaded_last_modified); | 1730 EXPECT_EQ(update_last_modified, reloaded_last_modified); |
1551 } | 1731 } |
OLD | NEW |