Chromium Code Reviews| 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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 Time last_modified, | 175 Time last_modified, |
| 174 Time last_visited); | 176 Time last_visited); |
| 175 | 177 |
| 176 // Verifies the two TemplateURLs are equal. | 178 // Verifies the two TemplateURLs are equal. |
| 177 void AssertEquals(const TemplateURL& expected, const TemplateURL& actual); | 179 void AssertEquals(const TemplateURL& expected, const TemplateURL& actual); |
| 178 | 180 |
| 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 |
|
Peter Kasting
2017/02/02 00:04:27
Nit: While here, can you fix "Create" to "Creates"
| |
| 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 void SetOverridenEngines(); | |
|
Peter Kasting
2017/02/02 00:04:27
Nit: Comment what this does.
Alexander Yashkin
2017/02/03 14:44:26
Done
Alexander Yashkin
2017/02/03 14:44:26
Done
| |
| 192 | |
| 189 // Helper methods to make calling TemplateURLServiceTestUtil methods less | 193 // Helper methods to make calling TemplateURLServiceTestUtil methods less |
| 190 // visually noisy in the test code. | 194 // visually noisy in the test code. |
| 191 void VerifyObserverCount(int expected_changed_count); | 195 void VerifyObserverCount(int expected_changed_count); |
| 192 void VerifyObserverFired(); | 196 void VerifyObserverFired(); |
| 193 TemplateURLServiceTestUtil* test_util() { return test_util_.get(); } | 197 TemplateURLServiceTestUtil* test_util() { return test_util_.get(); } |
| 194 TemplateURLService* model() { return test_util_->model(); } | 198 TemplateURLService* model() { return test_util_->model(); } |
| 195 const SearchTermsData& search_terms_data() { | 199 const SearchTermsData& search_terms_data() { |
| 196 return model()->search_terms_data(); | 200 return model()->search_terms_data(); |
| 197 } | 201 } |
| 198 | 202 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 285 data.favicon_url = GURL("http://favicon.url"); | 289 data.favicon_url = GURL("http://favicon.url"); |
| 286 data.safe_for_autoreplace = safe_for_autoreplace; | 290 data.safe_for_autoreplace = safe_for_autoreplace; |
| 287 data.input_encodings.push_back("UTF-8"); | 291 data.input_encodings.push_back("UTF-8"); |
| 288 data.date_created = Time::FromTimeT(100); | 292 data.date_created = Time::FromTimeT(100); |
| 289 data.last_modified = Time::FromTimeT(100); | 293 data.last_modified = Time::FromTimeT(100); |
| 290 data.last_visited = Time::FromTimeT(100); | 294 data.last_visited = Time::FromTimeT(100); |
| 291 data.prepopulate_id = prepopulate_id; | 295 data.prepopulate_id = prepopulate_id; |
| 292 return base::MakeUnique<TemplateURL>(data); | 296 return base::MakeUnique<TemplateURL>(data); |
| 293 } | 297 } |
| 294 | 298 |
| 299 void TemplateURLServiceTest::SetOverridenEngines() { | |
| 300 // Set custom search engine as default through overrides. | |
| 301 auto prefs = test_util()->profile()->GetTestingPrefService(); | |
| 302 prefs->SetUserPref(prefs::kSearchProviderOverridesVersion, | |
| 303 new base::FundamentalValue(1)); | |
|
Peter Kasting
2017/02/02 00:04:26
Nit: Not for this CL, but it'd be nice if this API
Alexander Yashkin
2017/02/03 14:44:26
Agree.
| |
| 304 auto overrides_list = base::MakeUnique<base::ListValue>(); | |
| 305 auto entry = base::MakeUnique<base::DictionaryValue>(); | |
| 306 | |
| 307 entry->SetString("name", "override_name"); | |
| 308 entry->SetString("keyword", "override_keyword"); | |
| 309 entry->SetString("search_url", "http://override.com/s?q={searchTerms}"); | |
| 310 entry->SetString("favicon_url", "http://override.com/favicon.ico"); | |
| 311 entry->SetString("encoding", "UTF-8"); | |
| 312 entry->SetInteger("id", 1001); | |
| 313 entry->SetString("suggest_url", | |
| 314 "http://override.com/suggest?q={searchTerms}"); | |
| 315 entry->SetString("instant_url", | |
| 316 "http://override.com/instant?q={searchTerms}"); | |
| 317 auto alternate_urls = base::MakeUnique<base::ListValue>(); | |
| 318 alternate_urls->AppendString("http://override.com/alternate?q={searchTerms}"); | |
| 319 entry->Set("alternate_urls", std::move(alternate_urls)); | |
| 320 overrides_list->Append(std::move(entry)); | |
| 321 | |
| 322 prefs->SetUserPref(prefs::kSearchProviderOverrides, overrides_list.release()); | |
| 323 } | |
| 324 | |
| 295 void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) { | 325 void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) { |
| 296 EXPECT_EQ(expected_changed_count, test_util_->GetObserverCount()); | 326 EXPECT_EQ(expected_changed_count, test_util_->GetObserverCount()); |
| 297 test_util_->ResetObserverCount(); | 327 test_util_->ResetObserverCount(); |
| 298 } | 328 } |
| 299 | 329 |
| 300 void TemplateURLServiceTest::VerifyObserverFired() { | 330 void TemplateURLServiceTest::VerifyObserverFired() { |
| 301 EXPECT_LE(1, test_util_->GetObserverCount()); | 331 EXPECT_LE(1, test_util_->GetObserverCount()); |
| 302 test_util_->ResetObserverCount(); | 332 test_util_->ResetObserverCount(); |
| 303 } | 333 } |
| 304 | 334 |
| (...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 886 ExpectSimilar(expected_managed_default.get(), actual_managed_default); | 916 ExpectSimilar(expected_managed_default.get(), actual_managed_default); |
| 887 | 917 |
| 888 // The following call has no effect on the managed search engine. | 918 // The following call has no effect on the managed search engine. |
| 889 model()->RepairPrepopulatedSearchEngines(); | 919 model()->RepairPrepopulatedSearchEngines(); |
| 890 | 920 |
| 891 EXPECT_TRUE(model()->is_default_search_managed()); | 921 EXPECT_TRUE(model()->is_default_search_managed()); |
| 892 actual_managed_default = model()->GetDefaultSearchProvider(); | 922 actual_managed_default = model()->GetDefaultSearchProvider(); |
| 893 ExpectSimilar(expected_managed_default.get(), actual_managed_default); | 923 ExpectSimilar(expected_managed_default.get(), actual_managed_default); |
| 894 } | 924 } |
| 895 | 925 |
| 926 TEST_F(TemplateURLServiceTest, RepairPrepopulatedEnginesUpdatesSyncGuid) { | |
|
Peter Kasting
2017/02/02 00:04:27
Nit: Maybe add a sentence above this declaration a
Alexander Yashkin
2017/02/03 14:44:25
Done
| |
| 927 test_util()->VerifyLoad(); | |
| 928 | |
| 929 // Expect no synced DSE guid until user selected something or sync come in. | |
|
Peter Kasting
2017/02/02 00:04:27
Nit: Maybe "The synced DSE GUID should be empty un
Alexander Yashkin
2017/02/03 14:44:26
Done, thanks.
| |
| 930 EXPECT_TRUE(test_util() | |
| 931 ->profile() | |
| 932 ->GetTestingPrefService() | |
| 933 ->GetString(prefs::kSyncedDefaultSearchProviderGUID) | |
| 934 .empty()); | |
| 935 | |
| 936 // Google engine must exist. | |
| 937 TemplateURL* google = | |
| 938 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.com")); | |
| 939 ASSERT_TRUE(google); | |
| 940 // Add third-party default search engine. | |
|
Peter Kasting
2017/02/02 00:04:27
Nit: Blank line above this.
It also seems a littl
Alexander Yashkin
2017/02/03 14:44:26
Done
| |
| 941 TemplateURL* user_dse = AddKeywordWithDate( | |
|
Peter Kasting
2017/02/02 00:04:27
Nit: Probably not for this CL, but it'd be nice if
Alexander Yashkin
2017/02/03 14:44:26
Agree.
| |
| 942 "user_dse", "user_dse.com", "http://www.user_dse.com/s?q={searchTerms}", | |
| 943 std::string(), std::string(), std::string(), true, "UTF-8", Time(), | |
| 944 Time(), Time()); | |
| 945 model()->SetUserSelectedDefaultSearchProvider(user_dse); | |
| 946 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); | |
| 947 | |
| 948 // Check that user DSE guid is stored in kSyncedDefaultSearchProviderGUID. | |
| 949 EXPECT_EQ(user_dse->sync_guid(), | |
| 950 test_util()->profile()->GetTestingPrefService()->GetString( | |
| 951 prefs::kSyncedDefaultSearchProviderGUID)); | |
| 952 | |
| 953 model()->RepairPrepopulatedSearchEngines(); | |
| 954 | |
| 955 // Check that Google is default after repair. | |
| 956 ASSERT_EQ(google, model()->GetDefaultSearchProvider()); | |
| 957 // Check that google guid is stored in kSyncedDefaultSearchProviderGUID. | |
| 958 const std::string dse_guid = | |
| 959 test_util()->profile()->GetTestingPrefService()->GetString( | |
| 960 prefs::kSyncedDefaultSearchProviderGUID); | |
| 961 EXPECT_EQ(google->sync_guid(), dse_guid); | |
| 962 EXPECT_EQ(google->keyword(), | |
| 963 model()->GetTemplateURLForGUID(dse_guid)->keyword()); | |
| 964 } | |
| 965 | |
| 966 TEST_F(TemplateURLServiceTest, | |
| 967 RepairPrepopulatedEnginesWithOverridesUpdatesSyncGuid) { | |
| 968 SetOverridenEngines(); | |
|
Peter Kasting
2017/02/02 00:04:26
Nit: "overridden" has two 'd's (many places)
Alexander Yashkin
2017/02/03 14:44:26
Done
| |
| 969 test_util()->VerifyLoad(); | |
| 970 | |
| 971 TemplateURL* overriden_engine = | |
| 972 model()->GetTemplateURLForKeyword(ASCIIToUTF16("override_keyword")); | |
| 973 ASSERT_TRUE(overriden_engine); | |
| 974 | |
| 975 TemplateURL* dse = model()->GetDefaultSearchProvider(); | |
|
Peter Kasting
2017/02/02 00:04:27
Nit: Can inline below
Alexander Yashkin
2017/02/03 14:44:26
Done
| |
| 976 ASSERT_EQ(overriden_engine, dse); | |
|
Peter Kasting
2017/02/02 00:04:26
Nit: Can maybe be EXPECT?
Alexander Yashkin
2017/02/03 14:44:26
Done
| |
| 977 | |
| 978 // Add third-party default search engine. | |
| 979 TemplateURL* user_dse = AddKeywordWithDate( | |
| 980 "user_dse", "user_dse.com", "http://www.user_dse.com/s?q={searchTerms}", | |
| 981 std::string(), std::string(), std::string(), true, "UTF-8", Time(), | |
| 982 Time(), Time()); | |
| 983 model()->SetUserSelectedDefaultSearchProvider(user_dse); | |
| 984 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider()); | |
| 985 | |
| 986 // Check that user DSE guid is stored in kSyncedDefaultSearchProviderGUID. | |
| 987 EXPECT_EQ(user_dse->sync_guid(), | |
| 988 test_util()->profile()->GetTestingPrefService()->GetString( | |
| 989 prefs::kSyncedDefaultSearchProviderGUID)); | |
| 990 | |
| 991 model()->RepairPrepopulatedSearchEngines(); | |
| 992 | |
| 993 // Check that overriden engine is returned as default after repair. | |
| 994 ASSERT_EQ(overriden_engine, model()->GetDefaultSearchProvider()); | |
| 995 // Check that overriden_engine guid is stored in | |
| 996 // kSyncedDefaultSearchProviderGUID. | |
| 997 const std::string dse_guid = | |
| 998 test_util()->profile()->GetTestingPrefService()->GetString( | |
| 999 prefs::kSyncedDefaultSearchProviderGUID); | |
| 1000 EXPECT_EQ(overriden_engine->sync_guid(), dse_guid); | |
| 1001 EXPECT_EQ(overriden_engine->keyword(), | |
| 1002 model()->GetTemplateURLForGUID(dse_guid)->keyword()); | |
| 1003 } | |
| 1004 | |
| 896 TEST_F(TemplateURLServiceTest, UpdateKeywordSearchTermsForURL) { | 1005 TEST_F(TemplateURLServiceTest, UpdateKeywordSearchTermsForURL) { |
| 897 struct TestData { | 1006 struct TestData { |
| 898 const std::string url; | 1007 const std::string url; |
| 899 const base::string16 term; | 1008 const base::string16 term; |
| 900 } data[] = { | 1009 } data[] = { |
| 901 { "http://foo/", base::string16() }, | 1010 { "http://foo/", base::string16() }, |
| 902 { "http://foo/foo?q=xx", base::string16() }, | 1011 { "http://foo/foo?q=xx", base::string16() }, |
| 903 { "http://x/bar?q=xx", base::string16() }, | 1012 { "http://x/bar?q=xx", base::string16() }, |
| 904 { "http://x/foo?y=xx", base::string16() }, | 1013 { "http://x/foo?y=xx", base::string16() }, |
| 905 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, | 1014 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, |
| (...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1542 TemplateURL* update_url = | 1651 TemplateURL* update_url = |
| 1543 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); | 1652 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); |
| 1544 const base::Time update_last_modified = update_url->last_modified(); | 1653 const base::Time update_last_modified = update_url->last_modified(); |
| 1545 model()->SetUserSelectedDefaultSearchProvider(update_url); | 1654 model()->SetUserSelectedDefaultSearchProvider(update_url); |
| 1546 TemplateURL* reloaded_url = | 1655 TemplateURL* reloaded_url = |
| 1547 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); | 1656 model()->GetTemplateURLForKeyword(ASCIIToUTF16("engine_keyword")); |
| 1548 const base::Time reloaded_last_modified = reloaded_url->last_modified(); | 1657 const base::Time reloaded_last_modified = reloaded_url->last_modified(); |
| 1549 EXPECT_NE(original_last_modified, reloaded_last_modified); | 1658 EXPECT_NE(original_last_modified, reloaded_last_modified); |
| 1550 EXPECT_EQ(update_last_modified, reloaded_last_modified); | 1659 EXPECT_EQ(update_last_modified, reloaded_last_modified); |
| 1551 } | 1660 } |
| OLD | NEW |