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/string_util.h" | 6 #include "base/string_util.h" |
7 #include "base/time.h" | 7 #include "base/time.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/search_engines/template_url.h" | 9 #include "chrome/browser/search_engines/template_url.h" |
10 #include "chrome/browser/search_engines/template_url_service.h" | 10 #include "chrome/browser/search_engines/template_url_service.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 return sync_data.GetSpecifics().search_engine().keyword(); | 37 return sync_data.GetSpecifics().search_engine().keyword(); |
38 } | 38 } |
39 | 39 |
40 // TODO(stevet): Share these with template_url_service_unittest. | 40 // TODO(stevet): Share these with template_url_service_unittest. |
41 // Set the managed preferences for the default search provider and trigger | 41 // Set the managed preferences for the default search provider and trigger |
42 // notification. | 42 // notification. |
43 void SetManagedDefaultSearchPreferences(TemplateURLService* turl_service, | 43 void SetManagedDefaultSearchPreferences(TemplateURLService* turl_service, |
44 TestingProfile* profile, | 44 TestingProfile* profile, |
45 bool enabled, | 45 bool enabled, |
46 const std::string& name, | 46 const std::string& name, |
| 47 const std::string& keyword, |
47 const std::string& search_url, | 48 const std::string& search_url, |
48 const std::string& suggest_url, | 49 const std::string& suggest_url, |
49 const std::string& icon_url, | 50 const std::string& icon_url, |
50 const std::string& encodings, | 51 const std::string& encodings) { |
51 const std::string& keyword) { | |
52 TestingPrefService* pref_service = profile->GetTestingPrefService(); | 52 TestingPrefService* pref_service = profile->GetTestingPrefService(); |
53 pref_service->SetManagedPref(prefs::kDefaultSearchProviderEnabled, | 53 pref_service->SetManagedPref(prefs::kDefaultSearchProviderEnabled, |
54 Value::CreateBooleanValue(enabled)); | 54 Value::CreateBooleanValue(enabled)); |
55 pref_service->SetManagedPref(prefs::kDefaultSearchProviderName, | 55 pref_service->SetManagedPref(prefs::kDefaultSearchProviderName, |
56 Value::CreateStringValue(name)); | 56 Value::CreateStringValue(name)); |
| 57 pref_service->SetManagedPref(prefs::kDefaultSearchProviderKeyword, |
| 58 Value::CreateStringValue(keyword)); |
57 pref_service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL, | 59 pref_service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL, |
58 Value::CreateStringValue(search_url)); | 60 Value::CreateStringValue(search_url)); |
59 pref_service->SetManagedPref(prefs::kDefaultSearchProviderSuggestURL, | 61 pref_service->SetManagedPref(prefs::kDefaultSearchProviderSuggestURL, |
60 Value::CreateStringValue(suggest_url)); | 62 Value::CreateStringValue(suggest_url)); |
61 pref_service->SetManagedPref(prefs::kDefaultSearchProviderIconURL, | 63 pref_service->SetManagedPref(prefs::kDefaultSearchProviderIconURL, |
62 Value::CreateStringValue(icon_url)); | 64 Value::CreateStringValue(icon_url)); |
63 pref_service->SetManagedPref(prefs::kDefaultSearchProviderEncodings, | 65 pref_service->SetManagedPref(prefs::kDefaultSearchProviderEncodings, |
64 Value::CreateStringValue(encodings)); | 66 Value::CreateStringValue(encodings)); |
65 pref_service->SetManagedPref(prefs::kDefaultSearchProviderKeyword, | |
66 Value::CreateStringValue(keyword)); | |
67 } | 67 } |
68 | 68 |
69 // Remove all the managed preferences for the default search provider and | 69 // Remove all the managed preferences for the default search provider and |
70 // trigger notification. | 70 // trigger notification. |
71 void RemoveManagedDefaultSearchPreferences(TemplateURLService* turl_service, | 71 void RemoveManagedDefaultSearchPreferences(TemplateURLService* turl_service, |
72 TestingProfile* profile) { | 72 TestingProfile* profile) { |
73 TestingPrefService* pref_service = profile->GetTestingPrefService(); | 73 TestingPrefService* pref_service = profile->GetTestingPrefService(); |
74 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSearchURL); | |
75 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEnabled); | 74 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEnabled); |
76 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderName); | 75 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderName); |
| 76 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderKeyword); |
| 77 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSearchURL); |
77 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSuggestURL); | 78 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSuggestURL); |
78 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderIconURL); | 79 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderIconURL); |
79 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEncodings); | 80 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEncodings); |
80 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderKeyword); | |
81 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderID); | 81 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderID); |
82 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderPrepopulateID); | 82 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderPrepopulateID); |
83 } | 83 } |
84 | 84 |
85 | 85 |
86 // TestChangeProcessor -------------------------------------------------------- | 86 // TestChangeProcessor -------------------------------------------------------- |
87 | 87 |
88 // Dummy SyncChangeProcessor used to help review what SyncChanges are pushed | 88 // Dummy SyncChangeProcessor used to help review what SyncChanges are pushed |
89 // back up to Sync. | 89 // back up to Sync. |
90 class TestChangeProcessor : public SyncChangeProcessor { | 90 class TestChangeProcessor : public SyncChangeProcessor { |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 scoped_ptr<SyncChangeProcessor> TemplateURLServiceSyncTest::PassProcessor() { | 260 scoped_ptr<SyncChangeProcessor> TemplateURLServiceSyncTest::PassProcessor() { |
261 return sync_processor_delegate_.PassAs<SyncChangeProcessor>(); | 261 return sync_processor_delegate_.PassAs<SyncChangeProcessor>(); |
262 } | 262 } |
263 | 263 |
264 TemplateURL* TemplateURLServiceSyncTest::CreateTestTemplateURL( | 264 TemplateURL* TemplateURLServiceSyncTest::CreateTestTemplateURL( |
265 const string16& keyword, | 265 const string16& keyword, |
266 const std::string& url, | 266 const std::string& url, |
267 const std::string& guid, | 267 const std::string& guid, |
268 time_t last_mod, | 268 time_t last_mod, |
269 bool created_by_policy) const { | 269 bool created_by_policy) const { |
270 TemplateURL* turl = new TemplateURL(); | 270 TemplateURLData data; |
271 turl->set_short_name(ASCIIToUTF16("unittest")); | 271 data.short_name = ASCIIToUTF16("unittest"); |
272 turl->set_keyword(keyword); | 272 data.SetKeyword(keyword); |
273 turl->set_safe_for_autoreplace(true); | 273 data.SetURL(url); |
274 turl->set_date_created(Time::FromTimeT(100)); | 274 data.favicon_url = GURL("http://favicon.url"); |
275 turl->set_last_modified(Time::FromTimeT(last_mod)); | 275 data.safe_for_autoreplace = true; |
276 turl->set_created_by_policy(created_by_policy); | 276 data.date_created = Time::FromTimeT(100); |
277 turl->SetPrepopulateId(999999); | 277 data.last_modified = Time::FromTimeT(last_mod); |
| 278 data.created_by_policy = created_by_policy; |
| 279 data.prepopulate_id = 999999; |
278 if (!guid.empty()) | 280 if (!guid.empty()) |
279 turl->set_sync_guid(guid); | 281 data.sync_guid = guid; |
280 turl->SetURL(url); | 282 return new TemplateURL(data); |
281 turl->set_favicon_url(GURL("http://favicon.url")); | |
282 return turl; | |
283 } | 283 } |
284 | 284 |
285 void TemplateURLServiceSyncTest::AssertEquals(const TemplateURL& expected, | 285 void TemplateURLServiceSyncTest::AssertEquals(const TemplateURL& expected, |
286 const TemplateURL& actual) const { | 286 const TemplateURL& actual) const { |
287 ASSERT_EQ(expected.short_name(), actual.short_name()); | 287 ASSERT_EQ(expected.short_name(), actual.short_name()); |
| 288 ASSERT_EQ(expected.keyword(), actual.keyword()); |
288 ASSERT_EQ(expected.url(), actual.url()); | 289 ASSERT_EQ(expected.url(), actual.url()); |
289 ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url()); | 290 ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url()); |
290 ASSERT_EQ(expected.keyword(), actual.keyword()); | 291 ASSERT_EQ(expected.favicon_url(), actual.favicon_url()); |
291 ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list()); | 292 ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list()); |
292 ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace()); | 293 ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace()); |
293 ASSERT_EQ(expected.favicon_url(), actual.favicon_url()); | |
294 ASSERT_EQ(expected.input_encodings(), actual.input_encodings()); | 294 ASSERT_EQ(expected.input_encodings(), actual.input_encodings()); |
295 ASSERT_EQ(expected.date_created(), actual.date_created()); | 295 ASSERT_EQ(expected.date_created(), actual.date_created()); |
296 ASSERT_EQ(expected.last_modified(), actual.last_modified()); | 296 ASSERT_EQ(expected.last_modified(), actual.last_modified()); |
297 } | 297 } |
298 | 298 |
299 void TemplateURLServiceSyncTest::AssertEquals(const SyncDataList& data1, | 299 void TemplateURLServiceSyncTest::AssertEquals(const SyncDataList& data1, |
300 const SyncDataList& data2) const { | 300 const SyncDataList& data2) const { |
301 SyncDataMap map1 = TemplateURLService::CreateGUIDToSyncDataMap(data1); | 301 SyncDataMap map1 = TemplateURLService::CreateGUIDToSyncDataMap(data1); |
302 SyncDataMap map2 = TemplateURLService::CreateGUIDToSyncDataMap(data2); | 302 SyncDataMap map2 = TemplateURLService::CreateGUIDToSyncDataMap(data2); |
303 | 303 |
(...skipping 30 matching lines...) Expand all Loading... |
334 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); | 334 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); |
335 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", | 335 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", |
336 "key3", 90)); | 336 "key3", 90)); |
337 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); | 337 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); |
338 | 338 |
339 return list; | 339 return list; |
340 } | 340 } |
341 | 341 |
342 TemplateURL* TemplateURLServiceSyncTest::Deserialize( | 342 TemplateURL* TemplateURLServiceSyncTest::Deserialize( |
343 const SyncData& sync_data) { | 343 const SyncData& sync_data) { |
344 return TemplateURLService::CreateTemplateURLFromSyncData(sync_data); | 344 return TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData(NULL, |
| 345 sync_data); |
345 } | 346 } |
346 | 347 |
347 | 348 |
348 // Actual tests --------------------------------------------------------------- | 349 // Actual tests --------------------------------------------------------------- |
349 | 350 |
350 TEST_F(TemplateURLServiceSyncTest, SerializeDeserialize) { | 351 TEST_F(TemplateURLServiceSyncTest, SerializeDeserialize) { |
351 // Create a TemplateURL and convert it into a sync specific type. | 352 // Create a TemplateURL and convert it into a sync specific type. |
352 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("unittest"), | 353 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("unittest"), |
353 "http://www.unittest.com/")); | 354 "http://www.unittest.com/")); |
354 SyncData sync_data = TemplateURLService::CreateSyncDataFromTemplateURL(*turl); | 355 SyncData sync_data = TemplateURLService::CreateSyncDataFromTemplateURL(*turl); |
(...skipping 735 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1090 | 1091 |
1091 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", | 1092 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", |
1092 "key1", 10)); // earlier | 1093 "key1", 10)); // earlier |
1093 | 1094 |
1094 SyncError error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, | 1095 SyncError error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, |
1095 initial_data, PassProcessor()); | 1096 initial_data, PassProcessor()); |
1096 ASSERT_FALSE(error.IsSet()); | 1097 ASSERT_FALSE(error.IsSet()); |
1097 | 1098 |
1098 // We should have updated the original TemplateURL with Sync's version. | 1099 // We should have updated the original TemplateURL with Sync's version. |
1099 // Keep a copy of it so we can compare it after we re-merge. | 1100 // Keep a copy of it so we can compare it after we re-merge. |
1100 ASSERT_TRUE(model()->GetTemplateURLForGUID("key1")); | 1101 const TemplateURL* key1_url = model()->GetTemplateURLForGUID("key1"); |
1101 TemplateURL updated_turl(*model()->GetTemplateURLForGUID("key1")); | 1102 ASSERT_TRUE(key1_url); |
1102 EXPECT_EQ(Time::FromTimeT(90), updated_turl.last_modified()); | 1103 scoped_ptr<TemplateURL> updated_turl(new TemplateURL(key1_url->data())); |
| 1104 EXPECT_EQ(Time::FromTimeT(90), updated_turl->last_modified()); |
1103 | 1105 |
1104 // Modify a single field of the initial data. This should not be updated in | 1106 // Modify a single field of the initial data. This should not be updated in |
1105 // the second merge, as the last_modified timestamp remains the same. | 1107 // the second merge, as the last_modified timestamp remains the same. |
1106 scoped_ptr<TemplateURL> temp_turl(Deserialize(initial_data[0])); | 1108 scoped_ptr<TemplateURL> temp_turl(Deserialize(initial_data[0])); |
1107 temp_turl->set_short_name(ASCIIToUTF16("SomethingDifferent")); | 1109 TemplateURLData data(temp_turl->data()); |
| 1110 data.short_name = ASCIIToUTF16("SomethingDifferent"); |
| 1111 temp_turl.reset(new TemplateURL(data)); |
1108 initial_data.clear(); | 1112 initial_data.clear(); |
1109 initial_data.push_back( | 1113 initial_data.push_back( |
1110 TemplateURLService::CreateSyncDataFromTemplateURL(*temp_turl)); | 1114 TemplateURLService::CreateSyncDataFromTemplateURL(*temp_turl)); |
1111 | 1115 |
1112 // Remerge the data again. This simulates shutting down and syncing again | 1116 // Remerge the data again. This simulates shutting down and syncing again |
1113 // at a different time, but the cloud data has not changed. | 1117 // at a different time, but the cloud data has not changed. |
1114 model()->StopSyncing(syncable::SEARCH_ENGINES); | 1118 model()->StopSyncing(syncable::SEARCH_ENGINES); |
1115 sync_processor_delegate_.reset(new SyncChangeProcessorDelegate( | 1119 sync_processor_delegate_.reset(new SyncChangeProcessorDelegate( |
1116 sync_processor_.get())); | 1120 sync_processor_.get())); |
1117 error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, | 1121 error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, |
1118 initial_data, PassProcessor()); | 1122 initial_data, PassProcessor()); |
1119 ASSERT_FALSE(error.IsSet()); | 1123 ASSERT_FALSE(error.IsSet()); |
1120 | 1124 |
1121 // Check that the TemplateURL was not modified. | 1125 // Check that the TemplateURL was not modified. |
1122 const TemplateURL* reupdated_turl = model()->GetTemplateURLForGUID("key1"); | 1126 const TemplateURL* reupdated_turl = model()->GetTemplateURLForGUID("key1"); |
1123 ASSERT_TRUE(reupdated_turl); | 1127 ASSERT_TRUE(reupdated_turl); |
1124 AssertEquals(updated_turl, *reupdated_turl); | 1128 AssertEquals(*updated_turl, *reupdated_turl); |
1125 } | 1129 } |
1126 | 1130 |
1127 TEST_F(TemplateURLServiceSyncTest, SyncedDefaultGUIDArrivesFirst) { | 1131 TEST_F(TemplateURLServiceSyncTest, SyncedDefaultGUIDArrivesFirst) { |
1128 SyncDataList initial_data = CreateInitialSyncData(); | 1132 SyncDataList initial_data = CreateInitialSyncData(); |
1129 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data, | 1133 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data, |
1130 PassProcessor()); | 1134 PassProcessor()); |
1131 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2")); | 1135 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2")); |
1132 | 1136 |
1133 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); | 1137 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); |
1134 const TemplateURL* default_search = model()->GetDefaultSearchProvider(); | 1138 const TemplateURL* default_search = model()->GetDefaultSearchProvider(); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1284 | 1288 |
1285 // The key1 entry should be a duplicate of the default. | 1289 // The key1 entry should be a duplicate of the default. |
1286 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, | 1290 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, |
1287 CreateInitialSyncData(), PassProcessor()); | 1291 CreateInitialSyncData(), PassProcessor()); |
1288 | 1292 |
1289 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); | 1293 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); |
1290 EXPECT_FALSE(model()->GetTemplateURLForGUID("whateverguid")); | 1294 EXPECT_FALSE(model()->GetTemplateURLForGUID("whateverguid")); |
1291 EXPECT_EQ(model()->GetDefaultSearchProvider(), | 1295 EXPECT_EQ(model()->GetDefaultSearchProvider(), |
1292 model()->GetTemplateURLForGUID("key1")); | 1296 model()->GetTemplateURLForGUID("key1")); |
1293 } | 1297 } |
OLD | NEW |