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