| 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/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/memory/scoped_vector.h" | 6 #include "base/memory/scoped_vector.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "sync/protocol/search_engine_specifics.pb.h" | 25 #include "sync/protocol/search_engine_specifics.pb.h" |
| 26 #include "sync/protocol/sync.pb.h" | 26 #include "sync/protocol/sync.pb.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 28 |
| 29 using base::ASCIIToUTF16; | 29 using base::ASCIIToUTF16; |
| 30 using base::UTF8ToUTF16; | 30 using base::UTF8ToUTF16; |
| 31 using base::Time; | 31 using base::Time; |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 const char kOmniboxScheme[] = "omnibox"; |
| 36 |
| 35 // Extract the GUID from a search engine syncer::SyncData. | 37 // Extract the GUID from a search engine syncer::SyncData. |
| 36 std::string GetGUID(const syncer::SyncData& sync_data) { | 38 std::string GetGUID(const syncer::SyncData& sync_data) { |
| 37 return sync_data.GetSpecifics().search_engine().sync_guid(); | 39 return sync_data.GetSpecifics().search_engine().sync_guid(); |
| 38 } | 40 } |
| 39 | 41 |
| 40 // Extract the URL from a search engine syncer::SyncData. | 42 // Extract the URL from a search engine syncer::SyncData. |
| 41 std::string GetURL(const syncer::SyncData& sync_data) { | 43 std::string GetURL(const syncer::SyncData& sync_data) { |
| 42 return sync_data.GetSpecifics().search_engine().url(); | 44 return sync_data.GetSpecifics().search_engine().url(); |
| 43 } | 45 } |
| 44 | 46 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 "Some error.", | 136 "Some error.", |
| 135 syncer::SEARCH_ENGINES); | 137 syncer::SEARCH_ENGINES); |
| 136 | 138 |
| 137 change_map_.erase(change_map_.begin(), change_map_.end()); | 139 change_map_.erase(change_map_.begin(), change_map_.end()); |
| 138 for (syncer::SyncChangeList::const_iterator iter = change_list.begin(); | 140 for (syncer::SyncChangeList::const_iterator iter = change_list.begin(); |
| 139 iter != change_list.end(); ++iter) | 141 iter != change_list.end(); ++iter) |
| 140 change_map_[GetGUID(iter->sync_data())] = *iter; | 142 change_map_[GetGUID(iter->sync_data())] = *iter; |
| 141 return syncer::SyncError(); | 143 return syncer::SyncError(); |
| 142 } | 144 } |
| 143 | 145 |
| 146 class TestTemplateURLServiceClient : public TemplateURLServiceClient { |
| 147 public: |
| 148 virtual ~TestTemplateURLServiceClient() override {} |
| 149 |
| 150 virtual void Shutdown() override {} |
| 151 |
| 152 virtual void SetOwner(TemplateURLService* owner) override {} |
| 153 |
| 154 virtual void DeleteAllSearchTermsForKeyword(TemplateURLID id) override {} |
| 155 |
| 156 virtual void SetKeywordSearchTermsForURL( |
| 157 const GURL& url, |
| 158 TemplateURLID id, |
| 159 const base::string16& term) override {} |
| 160 |
| 161 virtual void AddKeywordGeneratedVisit(const GURL& url) override {} |
| 162 |
| 163 virtual void RestoreExtensionInfoIfNecessary( |
| 164 TemplateURL* template_url) override; |
| 165 }; |
| 166 |
| 167 void TestTemplateURLServiceClient::RestoreExtensionInfoIfNecessary( |
| 168 TemplateURL* template_url) { |
| 169 const TemplateURLData& data = template_url->data(); |
| 170 GURL url(data.url()); |
| 171 if (url.SchemeIs(kOmniboxScheme)) { |
| 172 const std::string& extension_id = url.host(); |
| 173 template_url->set_extension_info(make_scoped_ptr( |
| 174 new TemplateURL::AssociatedExtensionInfo( |
| 175 TemplateURL::OMNIBOX_API_EXTENSION, extension_id))); |
| 176 } |
| 177 } |
| 144 | 178 |
| 145 } // namespace | 179 } // namespace |
| 146 | 180 |
| 147 | 181 |
| 148 // TemplateURLServiceSyncTest ------------------------------------------------- | 182 // TemplateURLServiceSyncTest ------------------------------------------------- |
| 149 | 183 |
| 150 class TemplateURLServiceSyncTest : public testing::Test { | 184 class TemplateURLServiceSyncTest : public testing::Test { |
| 151 public: | 185 public: |
| 152 typedef TemplateURLService::SyncDataMap SyncDataMap; | 186 typedef TemplateURLService::SyncDataMap SyncDataMap; |
| 153 | 187 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 syncer::SyncChange::SyncChangeType type, | 224 syncer::SyncChange::SyncChangeType type, |
| 191 TemplateURL* turl) const; | 225 TemplateURL* turl) const; |
| 192 | 226 |
| 193 // Helper that creates some initial sync data. We cheat a little by specifying | 227 // Helper that creates some initial sync data. We cheat a little by specifying |
| 194 // GUIDs for easy identification later. We also make the last_modified times | 228 // GUIDs for easy identification later. We also make the last_modified times |
| 195 // slightly older than CreateTestTemplateURL's default, to test conflict | 229 // slightly older than CreateTestTemplateURL's default, to test conflict |
| 196 // resolution. | 230 // resolution. |
| 197 syncer::SyncDataList CreateInitialSyncData() const; | 231 syncer::SyncDataList CreateInitialSyncData() const; |
| 198 | 232 |
| 199 // Syntactic sugar. | 233 // Syntactic sugar. |
| 200 TemplateURL* Deserialize(const syncer::SyncData& sync_data); | 234 scoped_ptr<TemplateURL> Deserialize(const syncer::SyncData& sync_data); |
| 201 | 235 |
| 202 // Creates a new TemplateURL copying the fields of |turl| but replacing | 236 // Creates a new TemplateURL copying the fields of |turl| but replacing |
| 203 // the |url| and |guid| and initializing the date_created and last_modified | 237 // the |url| and |guid| and initializing the date_created and last_modified |
| 204 // timestamps to a default value of 100. The caller owns the returned | 238 // timestamps to a default value of 100. The caller owns the returned |
| 205 // TemplateURL*. | 239 // TemplateURL*. |
| 206 TemplateURL* CopyTemplateURL(const TemplateURLData* turl, | 240 TemplateURL* CopyTemplateURL(const TemplateURLData* turl, |
| 207 const std::string& url, | 241 const std::string& url, |
| 208 const std::string& guid); | 242 const std::string& guid); |
| 209 | 243 |
| 210 protected: | 244 protected: |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com", | 362 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com", |
| 329 "key2", 90)); | 363 "key2", 90)); |
| 330 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); | 364 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); |
| 331 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", | 365 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", |
| 332 "key3", 90)); | 366 "key3", 90)); |
| 333 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); | 367 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); |
| 334 | 368 |
| 335 return list; | 369 return list; |
| 336 } | 370 } |
| 337 | 371 |
| 338 TemplateURL* TemplateURLServiceSyncTest::Deserialize( | 372 scoped_ptr<TemplateURL> TemplateURLServiceSyncTest::Deserialize( |
| 339 const syncer::SyncData& sync_data) { | 373 const syncer::SyncData& sync_data) { |
| 340 syncer::SyncChangeList dummy; | 374 syncer::SyncChangeList dummy; |
| 375 TestTemplateURLServiceClient client; |
| 341 return TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData( | 376 return TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData( |
| 342 NULL, SearchTermsData(), NULL, sync_data, &dummy); | 377 &client, NULL, SearchTermsData(), NULL, sync_data, &dummy); |
| 343 } | 378 } |
| 344 | 379 |
| 345 TemplateURL* TemplateURLServiceSyncTest::CopyTemplateURL( | 380 TemplateURL* TemplateURLServiceSyncTest::CopyTemplateURL( |
| 346 const TemplateURLData* turl, | 381 const TemplateURLData* turl, |
| 347 const std::string& url, | 382 const std::string& url, |
| 348 const std::string& guid) { | 383 const std::string& guid) { |
| 349 TemplateURLData data = *turl; | 384 TemplateURLData data = *turl; |
| 350 data.SetURL(url); | 385 data.SetURL(url); |
| 351 data.date_created = Time::FromTimeT(100); | 386 data.date_created = Time::FromTimeT(100); |
| 352 data.last_modified = Time::FromTimeT(100); | 387 data.last_modified = Time::FromTimeT(100); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 std::string guid = GetGUID(*iter); | 419 std::string guid = GetGUID(*iter); |
| 385 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); | 420 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); |
| 386 scoped_ptr<TemplateURL> deserialized(Deserialize(*iter)); | 421 scoped_ptr<TemplateURL> deserialized(Deserialize(*iter)); |
| 387 AssertEquals(*service_turl, *deserialized); | 422 AssertEquals(*service_turl, *deserialized); |
| 388 } | 423 } |
| 389 } | 424 } |
| 390 | 425 |
| 391 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataWithExtension) { | 426 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataWithExtension) { |
| 392 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); | 427 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); |
| 393 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); | 428 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); |
| 394 model()->RegisterOmniboxKeyword("blahblahblah", "unittest", "key3", | 429 std::string fake_id("blahblahblah"); |
| 395 "http://blahblahblah"); | 430 std::string fake_url = std::string(kOmniboxScheme) + "://" + fake_id; |
| 431 model()->RegisterOmniboxKeyword(fake_id, "unittest", "key3", fake_url); |
| 396 syncer::SyncDataList all_sync_data = | 432 syncer::SyncDataList all_sync_data = |
| 397 model()->GetAllSyncData(syncer::SEARCH_ENGINES); | 433 model()->GetAllSyncData(syncer::SEARCH_ENGINES); |
| 398 | 434 |
| 399 EXPECT_EQ(3U, all_sync_data.size()); | 435 EXPECT_EQ(2U, all_sync_data.size()); |
| 400 | 436 |
| 401 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin(); | 437 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin(); |
| 402 iter != all_sync_data.end(); ++iter) { | 438 iter != all_sync_data.end(); ++iter) { |
| 403 std::string guid = GetGUID(*iter); | 439 std::string guid = GetGUID(*iter); |
| 404 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); | 440 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); |
| 405 scoped_ptr<TemplateURL> deserialized(Deserialize(*iter)); | 441 scoped_ptr<TemplateURL> deserialized(Deserialize(*iter)); |
| 406 AssertEquals(*service_turl, *deserialized); | 442 AssertEquals(*service_turl, *deserialized); |
| 407 } | 443 } |
| 408 } | 444 } |
| 409 | 445 |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 EXPECT_EQ(6, merge_result.num_items_after_association()); | 697 EXPECT_EQ(6, merge_result.num_items_after_association()); |
| 662 } | 698 } |
| 663 | 699 |
| 664 TEST_F(TemplateURLServiceSyncTest, MergeSyncIsTheSame) { | 700 TEST_F(TemplateURLServiceSyncTest, MergeSyncIsTheSame) { |
| 665 // The local data is the same as the sync data merged in. i.e. - There have | 701 // The local data is the same as the sync data merged in. i.e. - There have |
| 666 // been no changes since the last time we synced. Even the last_modified | 702 // been no changes since the last time we synced. Even the last_modified |
| 667 // timestamps are the same. | 703 // timestamps are the same. |
| 668 syncer::SyncDataList initial_data = CreateInitialSyncData(); | 704 syncer::SyncDataList initial_data = CreateInitialSyncData(); |
| 669 for (syncer::SyncDataList::const_iterator iter = initial_data.begin(); | 705 for (syncer::SyncDataList::const_iterator iter = initial_data.begin(); |
| 670 iter != initial_data.end(); ++iter) { | 706 iter != initial_data.end(); ++iter) { |
| 671 TemplateURL* converted = Deserialize(*iter); | 707 scoped_ptr<TemplateURL> converted(Deserialize(*iter)); |
| 672 model()->Add(converted); | 708 model()->Add(converted.release()); |
| 673 } | 709 } |
| 674 | 710 |
| 675 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing( | 711 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing( |
| 676 syncer::SEARCH_ENGINES, initial_data, | 712 syncer::SEARCH_ENGINES, initial_data, |
| 677 PassProcessor(), CreateAndPassSyncErrorFactory()); | 713 PassProcessor(), CreateAndPassSyncErrorFactory()); |
| 678 | 714 |
| 679 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); | 715 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); |
| 680 for (syncer::SyncDataList::const_iterator iter = initial_data.begin(); | 716 for (syncer::SyncDataList::const_iterator iter = initial_data.begin(); |
| 681 iter != initial_data.end(); ++iter) { | 717 iter != initial_data.end(); ++iter) { |
| 682 std::string guid = GetGUID(*iter); | 718 std::string guid = GetGUID(*iter); |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1055 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, | 1091 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, |
| 1056 CreateInitialSyncData(), PassProcessor(), | 1092 CreateInitialSyncData(), PassProcessor(), |
| 1057 CreateAndPassSyncErrorFactory()); | 1093 CreateAndPassSyncErrorFactory()); |
| 1058 | 1094 |
| 1059 // Add some extension keywords locally. | 1095 // Add some extension keywords locally. |
| 1060 model()->RegisterOmniboxKeyword("extension1", "unittest", "keyword1", | 1096 model()->RegisterOmniboxKeyword("extension1", "unittest", "keyword1", |
| 1061 "http://extension1"); | 1097 "http://extension1"); |
| 1062 TemplateURL* extension1 = | 1098 TemplateURL* extension1 = |
| 1063 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1")); | 1099 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1")); |
| 1064 ASSERT_TRUE(extension1); | 1100 ASSERT_TRUE(extension1); |
| 1065 EXPECT_EQ(1U, processor()->change_list_size()); | 1101 EXPECT_EQ(0U, processor()->change_list_size()); |
| 1066 | 1102 |
| 1067 model()->RegisterOmniboxKeyword("extension2", "unittest", "keyword2", | 1103 model()->RegisterOmniboxKeyword("extension2", "unittest", "keyword2", |
| 1068 "http://extension2"); | 1104 "http://extension2"); |
| 1069 TemplateURL* extension2 = | 1105 TemplateURL* extension2 = |
| 1070 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2")); | 1106 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2")); |
| 1071 ASSERT_TRUE(extension2); | 1107 ASSERT_TRUE(extension2); |
| 1072 EXPECT_EQ(1U, processor()->change_list_size()); | 1108 EXPECT_EQ(0U, processor()->change_list_size()); |
| 1073 | 1109 |
| 1074 // Create some sync changes that will conflict with the extension keywords. | 1110 // Create some sync changes that will conflict with the extension keywords. |
| 1075 syncer::SyncChangeList changes; | 1111 syncer::SyncChangeList changes; |
| 1076 changes.push_back(CreateTestSyncChange(syncer::SyncChange::ACTION_ADD, | 1112 changes.push_back(CreateTestSyncChange(syncer::SyncChange::ACTION_ADD, |
| 1077 CreateTestTemplateURL(ASCIIToUTF16("keyword1"), "http://aaa.com", | 1113 CreateTestTemplateURL(ASCIIToUTF16("keyword1"), "http://aaa.com", |
| 1078 std::string(), 100, true))); | 1114 std::string(), 100, true))); |
| 1079 changes.push_back(CreateTestSyncChange(syncer::SyncChange::ACTION_ADD, | 1115 changes.push_back(CreateTestSyncChange(syncer::SyncChange::ACTION_ADD, |
| 1080 CreateTestTemplateURL(ASCIIToUTF16("keyword2"), "http://bbb.com"))); | 1116 CreateTestTemplateURL(ASCIIToUTF16("keyword2"), "http://bbb.com"))); |
| 1081 model()->ProcessSyncChanges(FROM_HERE, changes); | 1117 model()->ProcessSyncChanges(FROM_HERE, changes); |
| 1082 | 1118 |
| 1083 // The existing extension keywords should be uniquified. | |
| 1084 EXPECT_FALSE(model()->GetTemplateURLForHost("aaa.com") == NULL); | 1119 EXPECT_FALSE(model()->GetTemplateURLForHost("aaa.com") == NULL); |
| 1085 EXPECT_EQ(model()->GetTemplateURLForHost("aaa.com"), | 1120 EXPECT_EQ(extension1, |
| 1086 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1"))); | 1121 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1"))); |
| 1087 TemplateURL* url_for_keyword2 = | 1122 TemplateURL* url_for_keyword2 = |
| 1088 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2")); | 1123 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2")); |
| 1089 EXPECT_NE(extension2, url_for_keyword2); | 1124 EXPECT_NE(extension2, url_for_keyword2); |
| 1090 EXPECT_EQ("http://bbb.com", url_for_keyword2->url()); | 1125 EXPECT_EQ("http://bbb.com", url_for_keyword2->url()); |
| 1091 | 1126 |
| 1092 // Replaced extension keywords should be uniquified. | |
| 1093 EXPECT_EQ(extension1, | 1127 EXPECT_EQ(extension1, |
| 1094 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1_"))); | 1128 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1"))); |
| 1095 EXPECT_EQ(extension2, | 1129 EXPECT_EQ(model()->GetTemplateURLForHost("bbb.com"), |
| 1096 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2_"))); | 1130 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2"))); |
| 1097 } | 1131 } |
| 1098 | 1132 |
| 1099 TEST_F(TemplateURLServiceSyncTest, AutogeneratedKeywordMigrated) { | 1133 TEST_F(TemplateURLServiceSyncTest, AutogeneratedKeywordMigrated) { |
| 1100 // Create a couple of sync entries with autogenerated keywords. | 1134 // Create a couple of sync entries with autogenerated keywords. |
| 1101 syncer::SyncDataList initial_data; | 1135 syncer::SyncDataList initial_data; |
| 1102 scoped_ptr<TemplateURL> turl( | 1136 scoped_ptr<TemplateURL> turl( |
| 1103 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", "key1")); | 1137 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", "key1")); |
| 1104 initial_data.push_back( | 1138 initial_data.push_back( |
| 1105 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid())); | 1139 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid())); |
| 1106 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), | 1140 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), |
| (...skipping 1124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2231 const char kNewGUID[] = "newdefault"; | 2265 const char kNewGUID[] = "newdefault"; |
| 2232 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("what"), | 2266 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("what"), |
| 2233 "http://thewhat.com/{searchTerms}", | 2267 "http://thewhat.com/{searchTerms}", |
| 2234 kNewGUID)); | 2268 kNewGUID)); |
| 2235 model()->SetUserSelectedDefaultSearchProvider( | 2269 model()->SetUserSelectedDefaultSearchProvider( |
| 2236 model()->GetTemplateURLForGUID(kNewGUID)); | 2270 model()->GetTemplateURLForGUID(kNewGUID)); |
| 2237 | 2271 |
| 2238 EXPECT_EQ(kNewGUID, profile_a()->GetTestingPrefService()->GetString( | 2272 EXPECT_EQ(kNewGUID, profile_a()->GetTestingPrefService()->GetString( |
| 2239 prefs::kSyncedDefaultSearchProviderGUID)); | 2273 prefs::kSyncedDefaultSearchProviderGUID)); |
| 2240 } | 2274 } |
| OLD | NEW |