Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: chrome/browser/search_engines/template_url_service_sync_unittest.cc

Issue 8334030: Merge search engines sync data type with Preferences. Sync the default search provider. Add some ... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Merge to TOT and fixed additional conflicts from rsimha. Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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"
11 #include "chrome/browser/search_engines/template_url_service_factory.h"
11 #include "chrome/browser/sync/protocol/search_engine_specifics.pb.h" 12 #include "chrome/browser/sync/protocol/search_engine_specifics.pb.h"
13 #include "chrome/common/chrome_notification_types.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/testing_pref_service.h"
12 #include "chrome/test/base/testing_profile.h" 16 #include "chrome/test/base/testing_profile.h"
17 #include "content/public/browser/notification_service.h"
13 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
14 19
15 using base::Time; 20 using base::Time;
16 21
17 namespace { 22 namespace {
18 23
19 // Extract the GUID from a search engine SyncData. 24 // Extract the GUID from a search engine SyncData.
20 std::string GetGUID(const SyncData& sync_data) { 25 std::string GetGUID(const SyncData& sync_data) {
21 return sync_data.GetSpecifics().GetExtension( 26 return sync_data.GetSpecifics().GetExtension(
22 sync_pb::search_engine).sync_guid(); 27 sync_pb::search_engine).sync_guid();
23 } 28 }
24 29
25 // Extract the keyword from a search engine SyncData. 30 // Extract the URL from a search engine SyncData.
26 std::string GetURL(const SyncData& sync_data) { 31 std::string GetURL(const SyncData& sync_data) {
27 return sync_data.GetSpecifics().GetExtension( 32 return sync_data.GetSpecifics().GetExtension(
28 sync_pb::search_engine).url(); 33 sync_pb::search_engine).url();
29 } 34 }
30 35
31 // Extract the keyword from a search engine SyncData. 36 // Extract the keyword from a search engine SyncData.
32 std::string GetKeyword(const SyncData& sync_data) { 37 std::string GetKeyword(const SyncData& sync_data) {
33 return sync_data.GetSpecifics().GetExtension( 38 return sync_data.GetSpecifics().GetExtension(
34 sync_pb::search_engine).keyword(); 39 sync_pb::search_engine).keyword();
35 } 40 }
36 41
42 // TODO(stevet): Share these with template_url_service_unittest.
43 // Set the managed preferences for the default search provider and trigger
44 // notification.
45 void SetManagedDefaultSearchPreferences(TemplateURLService* turl_service,
46 TestingProfile* profile,
47 bool enabled,
48 const char* name,
49 const char* search_url,
50 const char* suggest_url,
51 const char* icon_url,
52 const char* encodings,
53 const char* keyword) {
54 TestingPrefService* pref_service = profile->GetTestingPrefService();
55 pref_service->SetManagedPref(prefs::kDefaultSearchProviderEnabled,
56 Value::CreateBooleanValue(enabled));
57 pref_service->SetManagedPref(prefs::kDefaultSearchProviderName,
58 Value::CreateStringValue(name));
59 pref_service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL,
60 Value::CreateStringValue(search_url));
61 pref_service->SetManagedPref(prefs::kDefaultSearchProviderSuggestURL,
62 Value::CreateStringValue(suggest_url));
63 pref_service->SetManagedPref(prefs::kDefaultSearchProviderIconURL,
64 Value::CreateStringValue(icon_url));
65 pref_service->SetManagedPref(prefs::kDefaultSearchProviderEncodings,
66 Value::CreateStringValue(encodings));
67 pref_service->SetManagedPref(prefs::kDefaultSearchProviderKeyword,
68 Value::CreateStringValue(keyword));
69 }
70
71 // Remove all the managed preferences for the default search provider and
72 // trigger notification.
73 void RemoveManagedDefaultSearchPreferences(TemplateURLService* turl_service,
74 TestingProfile* profile) {
75 TestingPrefService* pref_service = profile->GetTestingPrefService();
76 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSearchURL);
77 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEnabled);
78 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderName);
79 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSuggestURL);
80 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderIconURL);
81 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEncodings);
82 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderKeyword);
83 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderID);
84 pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderPrepopulateID);
85 }
86
37 // Dummy SyncChangeProcessor used to help review what SyncChanges are pushed 87 // Dummy SyncChangeProcessor used to help review what SyncChanges are pushed
38 // back up to Sync. 88 // back up to Sync.
39 class TestChangeProcessor : public SyncChangeProcessor { 89 class TestChangeProcessor : public SyncChangeProcessor {
40 public: 90 public:
41 TestChangeProcessor() : erroneous_(false) { 91 TestChangeProcessor() : erroneous_(false) {
42 } 92 }
43 virtual ~TestChangeProcessor() { } 93 virtual ~TestChangeProcessor() { }
44 94
45 // Store a copy of all the changes passed in so we can examine them later. 95 // Store a copy of all the changes passed in so we can examine them later.
46 virtual SyncError ProcessSyncChanges( 96 virtual SyncError ProcessSyncChanges(
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 }; 130 };
81 131
82 class TemplateURLServiceSyncTest : public testing::Test { 132 class TemplateURLServiceSyncTest : public testing::Test {
83 public: 133 public:
84 typedef TemplateURLService::SyncDataMap SyncDataMap; 134 typedef TemplateURLService::SyncDataMap SyncDataMap;
85 135
86 TemplateURLServiceSyncTest() {} 136 TemplateURLServiceSyncTest() {}
87 137
88 virtual void SetUp() { 138 virtual void SetUp() {
89 profile_a_.reset(new TestingProfile); 139 profile_a_.reset(new TestingProfile);
140 TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile(
141 profile_a_.get());
90 model_a_.reset(new TemplateURLService(profile_a_.get())); 142 model_a_.reset(new TemplateURLService(profile_a_.get()));
91 model_a_->Load(); 143 model_a_->Load();
92 profile_b_.reset(new TestingProfile); 144 profile_b_.reset(new TestingProfile);
145 TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile(
146 profile_b_.get());
93 model_b_.reset(new TemplateURLService(profile_b_.get())); 147 model_b_.reset(new TemplateURLService(profile_b_.get()));
94 model_b_->Load(); 148 model_b_->Load();
95 } 149 }
96 150
97 virtual void TearDown() { } 151 virtual void TearDown() { }
98 152
99 TemplateURLService* model() { return model_a_.get(); } 153 TemplateURLService* model() { return model_a_.get(); }
100 // For readability, we redefine an accessor for Model A for use in tests that 154 // For readability, we redefine an accessor for Model A for use in tests that
101 // involve syncing two models. 155 // involve syncing two models.
102 TemplateURLService* model_a() { return model_a_.get(); } 156 TemplateURLService* model_a() { return model_a_.get(); }
(...skipping 951 matching lines...) Expand 10 before | Expand all | Expand 10 after
1054 error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 1108 error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1055 initial_data, 1109 initial_data,
1056 processor()); 1110 processor());
1057 ASSERT_FALSE(error.IsSet()); 1111 ASSERT_FALSE(error.IsSet());
1058 1112
1059 // Check that the TemplateURL was not modified. 1113 // Check that the TemplateURL was not modified.
1060 const TemplateURL* reupdated_turl = model()->GetTemplateURLForGUID("key1"); 1114 const TemplateURL* reupdated_turl = model()->GetTemplateURLForGUID("key1");
1061 ASSERT_TRUE(reupdated_turl); 1115 ASSERT_TRUE(reupdated_turl);
1062 AssertEquals(updated_turl, *reupdated_turl); 1116 AssertEquals(updated_turl, *reupdated_turl);
1063 } 1117 }
1118
1119 TEST_F(TemplateURLServiceSyncTest, SyncedDefaultGUIDArrivesFirst) {
1120 SyncDataList initial_data = CreateInitialSyncData();
1121 model()->MergeDataAndStartSyncing(
1122 syncable::SEARCH_ENGINES,
1123 initial_data,
1124 processor());
1125 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2"));
1126
1127 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1128 const TemplateURL* default_search = model()->GetDefaultSearchProvider();
1129 ASSERT_TRUE(default_search);
1130
1131 // Change kSyncedDefaultSearchProviderGUID to a GUID that does not exist in
1132 // the model yet. Ensure that the default has not changed in any way.
1133 profile_a_->GetTestingPrefService()->SetString(
1134 prefs::kSyncedDefaultSearchProviderGUID,
1135 "newdefault");
1136
1137 ASSERT_EQ(default_search, model()->GetDefaultSearchProvider());
1138
1139 // Bring in a random new search engine with a different GUID. Ensure that
1140 // it doesn't change the default.
1141 SyncChangeList changes1;
1142 changes1.push_back(CreateTestSyncChange(
1143 SyncChange::ACTION_ADD,
1144 CreateTestTemplateURL("random", "http://random.com", "random")));
1145 model()->ProcessSyncChanges(FROM_HERE, changes1);
1146
1147 EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1148 ASSERT_EQ(default_search, model()->GetDefaultSearchProvider());
1149
1150 // Finally, bring in the expected entry with the right GUID. Ensure that
1151 // the default has changed to the new search engine.
1152 SyncChangeList changes2;
1153 changes2.push_back(CreateTestSyncChange(
1154 SyncChange::ACTION_ADD,
1155 CreateTestTemplateURL("new", "http://new.com", "newdefault")));
1156 model()->ProcessSyncChanges(FROM_HERE, changes2);
1157
1158 EXPECT_EQ(5U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1159 ASSERT_NE(default_search, model()->GetDefaultSearchProvider());
1160 ASSERT_EQ("newdefault", model()->GetDefaultSearchProvider()->sync_guid());
1161 }
1162
1163 TEST_F(TemplateURLServiceSyncTest, SyncedDefaultArrivesAfterStartup) {
1164 // Start with the default set to something in the model before we start
1165 // syncing.
1166 model()->Add(CreateTestTemplateURL(
1167 "what", "http://thewhat.com", "initdefault"));
1168 model()->SetDefaultSearchProvider(
1169 model()->GetTemplateURLForGUID("initdefault"));
1170
1171 const TemplateURL* default_search = model()->GetDefaultSearchProvider();
1172 ASSERT_TRUE(default_search);
1173
1174 // Set kSyncedDefaultSearchProviderGUID to something that is not yet in
1175 // the model but is expected in the initial sync. Ensure that this doesn't
1176 // change our default since we're not quite syncing yet.
1177 profile_a_->GetTestingPrefService()->SetString(
1178 prefs::kSyncedDefaultSearchProviderGUID,
1179 "key2");
1180
1181 EXPECT_EQ(default_search, model()->GetDefaultSearchProvider());
1182
1183 // Now sync the initial data, which will include the search engine entry
1184 // destined to become the new default.
1185 SyncDataList initial_data = CreateInitialSyncData();
1186 model()->MergeDataAndStartSyncing(
1187 syncable::SEARCH_ENGINES,
1188 initial_data,
1189 processor());
1190
1191 // Ensure that the new default has been set.
1192 EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1193 ASSERT_NE(default_search, model()->GetDefaultSearchProvider());
1194 ASSERT_EQ("key2", model()->GetDefaultSearchProvider()->sync_guid());
1195 }
1196
1197 TEST_F(TemplateURLServiceSyncTest, SyncedDefaultAlreadySetOnStartup) {
1198 // Start with the default set to something in the model before we start
1199 // syncing.
1200 const char kGUID[] = "initdefault";
1201 model()->Add(CreateTestTemplateURL("what", "http://thewhat.com", kGUID));
1202 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID(kGUID));
1203
1204 const TemplateURL* default_search = model()->GetDefaultSearchProvider();
1205 ASSERT_TRUE(default_search);
1206
1207 // Set kSyncedDefaultSearchProviderGUID to the current default.
1208 profile_a_->GetTestingPrefService()->SetString(
1209 prefs::kSyncedDefaultSearchProviderGUID,
1210 kGUID);
1211
1212 EXPECT_EQ(default_search, model()->GetDefaultSearchProvider());
1213
1214 // Now sync the initial data.
1215 SyncDataList initial_data = CreateInitialSyncData();
1216 model()->MergeDataAndStartSyncing(
1217 syncable::SEARCH_ENGINES,
1218 initial_data,
1219 processor());
1220
1221 // Ensure that the new entries were added and the default has not changed.
1222 EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1223 ASSERT_EQ(default_search, model()->GetDefaultSearchProvider());
1224 }
1225
1226 TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) {
1227 // First start off with a few entries and make sure we can set an unmanaged
1228 // default search provider.
1229 SyncDataList initial_data = CreateInitialSyncData();
1230 model()->MergeDataAndStartSyncing(
1231 syncable::SEARCH_ENGINES,
1232 initial_data,
1233 processor());
1234 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2"));
1235
1236 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1237 ASSERT_FALSE(model()->is_default_search_managed());
1238 ASSERT_TRUE(model()->GetDefaultSearchProvider());
1239
1240 // Change the default search provider to a managed one.
1241 const char kName[] = "manageddefault";
1242 const char kSearchURL[] = "http://manageddefault.com/search?t={searchTerms}";
1243 const char kIconURL[] = "http://manageddefault.com/icon.jpg";
1244 const char kEncodings[] = "UTF-16;UTF-32";
1245 SetManagedDefaultSearchPreferences(model(), profile_a_.get(), true, kName,
1246 kSearchURL, "", kIconURL, kEncodings, "");
1247 const TemplateURL* dsp_turl = model()->GetDefaultSearchProvider();
1248
1249 EXPECT_TRUE(model()->is_default_search_managed());
1250
1251 // Add a new entry from Sync. It should still sync in despite the default
1252 // being managed.
1253 SyncChangeList changes;
1254 changes.push_back(CreateTestSyncChange(
1255 SyncChange::ACTION_ADD,
1256 CreateTestTemplateURL("newkeyword", "http://new.com", "newdefault")));
1257 model()->ProcessSyncChanges(FROM_HERE, changes);
1258
1259 EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1260
1261 // Change kSyncedDefaultSearchProviderGUID to point to the new entry and
1262 // ensure that the DSP remains managed.
1263 profile_a_->GetTestingPrefService()->SetString(
1264 prefs::kSyncedDefaultSearchProviderGUID,
1265 "newdefault");
1266
1267 EXPECT_EQ(dsp_turl, model()->GetDefaultSearchProvider());
1268 EXPECT_TRUE(model()->is_default_search_managed());
1269
1270 // Go unmanaged. Ensure that the DSP changes to the expected pending entry
1271 // from Sync.
1272 const TemplateURL* expected_default =
1273 model()->GetTemplateURLForGUID("newdefault");
1274 RemoveManagedDefaultSearchPreferences(model(), profile_a_.get());
1275
1276 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider());
1277 }
OLDNEW
« no previous file with comments | « chrome/browser/search_engines/template_url_service_factory.cc ('k') | chrome/browser/sync/profile_sync_factory_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698