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

Side by Side Diff: chrome/browser/sync/profile_sync_service_preference_unittest.cc

Issue 6905044: Refactor preference syncing. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Really fix. And rebase Created 9 years, 7 months 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
« no previous file with comments | « chrome/browser/sync/profile_sync_service.cc ('k') | chrome/browser/sync/signin_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <map> 5 #include <map>
6 #include <string> 6 #include <string>
7 7
8 #include "base/json/json_reader.h" 8 #include "base/json/json_reader.h"
9 #include "base/stl_util-inl.h" 9 #include "base/stl_util-inl.h"
10 #include "base/string_piece.h" 10 #include "base/string_piece.h"
11 #include "base/task.h" 11 #include "base/task.h"
12 #include "chrome/browser/prefs/pref_model_associator.h"
12 #include "chrome/browser/prefs/scoped_user_pref_update.h" 13 #include "chrome/browser/prefs/scoped_user_pref_update.h"
13 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" 14 #include "chrome/browser/sync/abstract_profile_sync_service_test.h"
14 #include "chrome/browser/sync/engine/syncapi.h" 15 #include "chrome/browser/sync/engine/syncapi.h"
15 #include "chrome/browser/sync/glue/preference_change_processor.h" 16 #include "chrome/browser/sync/glue/generic_change_processor.h"
16 #include "chrome/browser/sync/glue/preference_data_type_controller.h" 17 #include "chrome/browser/sync/glue/preference_data_type_controller.h"
17 #include "chrome/browser/sync/glue/preference_model_associator.h"
18 #include "chrome/browser/sync/glue/sync_backend_host.h" 18 #include "chrome/browser/sync/glue/sync_backend_host.h"
19 #include "chrome/browser/sync/profile_sync_test_util.h" 19 #include "chrome/browser/sync/profile_sync_test_util.h"
20 #include "chrome/browser/sync/protocol/preference_specifics.pb.h" 20 #include "chrome/browser/sync/protocol/preference_specifics.pb.h"
21 #include "chrome/browser/sync/syncable/model_type.h" 21 #include "chrome/browser/sync/syncable/model_type.h"
22 #include "chrome/browser/sync/test_profile_sync_service.h" 22 #include "chrome/browser/sync/test_profile_sync_service.h"
23 #include "chrome/common/net/gaia/gaia_constants.h" 23 #include "chrome/common/net/gaia/gaia_constants.h"
24 #include "chrome/common/pref_names.h" 24 #include "chrome/common/pref_names.h"
25 #include "chrome/test/testing_pref_service.h" 25 #include "chrome/test/testing_pref_service.h"
26 #include "chrome/test/testing_profile.h" 26 #include "chrome/test/testing_profile.h"
27 #include "content/common/json_value_serializer.h" 27 #include "content/common/json_value_serializer.h"
28 #include "testing/gmock/include/gmock/gmock.h" 28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
30 30
31 using base::JSONReader; 31 using base::JSONReader;
32 using browser_sync::PreferenceChangeProcessor; 32 using browser_sync::GenericChangeProcessor;
33 using browser_sync::PreferenceDataTypeController; 33 using browser_sync::PreferenceDataTypeController;
34 using browser_sync::PreferenceModelAssociator;
35 using browser_sync::SyncBackendHost; 34 using browser_sync::SyncBackendHost;
36 using sync_api::SyncManager; 35 using sync_api::SyncManager;
37 using testing::_; 36 using testing::_;
38 using testing::Return; 37 using testing::Return;
39 38
40 typedef std::map<const std::string, const Value*> PreferenceValues; 39 typedef std::map<const std::string, const Value*> PreferenceValues;
41 40
42 class ProfileSyncServicePreferenceTest 41 class ProfileSyncServicePreferenceTest
43 : public AbstractProfileSyncServiceTest { 42 : public AbstractProfileSyncServiceTest {
44 protected: 43 protected:
45 ProfileSyncServicePreferenceTest() 44 ProfileSyncServicePreferenceTest()
46 : example_url0_("http://example.com/0"), 45 : example_url0_("http://example.com/0"),
47 example_url1_("http://example.com/1"), 46 example_url1_("http://example.com/1"),
48 example_url2_("http://example.com/2"), 47 example_url2_("http://example.com/2"),
49 not_synced_preference_name_("nonsense_pref_name"), 48 not_synced_preference_name_("nonsense_pref_name"),
50 not_synced_preference_default_value_("default"), 49 not_synced_preference_default_value_("default"),
51 non_default_charset_value_("foo") {} 50 non_default_charset_value_("foo") {}
52 51
53 virtual void SetUp() { 52 virtual void SetUp() {
54 AbstractProfileSyncServiceTest::SetUp(); 53 AbstractProfileSyncServiceTest::SetUp();
55 profile_.reset(new TestingProfile()); 54 profile_.reset(new TestingProfile());
56 profile_->CreateRequestContext(); 55 profile_->CreateRequestContext();
57 prefs_ = profile_->GetTestingPrefService(); 56 prefs_ = profile_->GetTestingPrefService();
58 57
59 prefs_->RegisterStringPref(not_synced_preference_name_.c_str(), 58 prefs_->RegisterStringPref(not_synced_preference_name_.c_str(),
60 not_synced_preference_default_value_); 59 not_synced_preference_default_value_,
60 PrefService::UNSYNCABLE_PREF);
61 } 61 }
62 62
63 virtual void TearDown() { 63 virtual void TearDown() {
64 service_.reset(); 64 service_.reset();
65 profile_.reset(); 65 profile_.reset();
66 AbstractProfileSyncServiceTest::TearDown(); 66 AbstractProfileSyncServiceTest::TearDown();
67 } 67 }
68 68
69 bool StartSyncService(Task* task, bool will_fail_association) { 69 bool StartSyncService(Task* task, bool will_fail_association) {
70 if (service_.get()) 70 if (service_.get())
71 return false; 71 return false;
72 72
73 service_.reset(new TestProfileSyncService( 73 service_.reset(new TestProfileSyncService(
74 &factory_, profile_.get(), "test", false, task)); 74 &factory_, profile_.get(), "test", false, task));
75
76 // Register the preference data type.
77 model_associator_ = 75 model_associator_ =
78 new PreferenceModelAssociator(service_.get()); 76 reinterpret_cast<PrefModelAssociator*>(prefs_->GetSyncableService());
79 change_processor_ = new PreferenceChangeProcessor(model_associator_, 77 change_processor_ = new GenericChangeProcessor(model_associator_,
80 service_.get()); 78 service_.get());
81 EXPECT_CALL(factory_, CreatePreferenceSyncComponents(_, _)). 79 EXPECT_CALL(factory_, CreatePreferenceSyncComponents(_, _)).
82 WillOnce(Return(ProfileSyncFactory::SyncComponents( 80 WillOnce(Return(ProfileSyncFactory::SyncComponents(
83 model_associator_, change_processor_))); 81 model_associator_, change_processor_)));
84 82
85 EXPECT_CALL(factory_, CreateDataTypeManager(_, _)). 83 EXPECT_CALL(factory_, CreateDataTypeManager(_, _)).
86 WillOnce(ReturnNewDataTypeManager()); 84 WillOnce(ReturnNewDataTypeManager());
87 85
88 service_->RegisterDataTypeController( 86 dtc_ = new PreferenceDataTypeController(&factory_,
89 new PreferenceDataTypeController(&factory_, 87 profile_.get(),
90 profile_.get(), 88 service_.get());
91 service_.get())); 89 service_->RegisterDataTypeController(dtc_);
92 profile_->GetTokenService()->IssueAuthTokenForTest( 90 profile_->GetTokenService()->IssueAuthTokenForTest(
93 GaiaConstants::kSyncService, "token"); 91 GaiaConstants::kSyncService, "token");
92
94 service_->Initialize(); 93 service_->Initialize();
95 MessageLoop::current()->Run(); 94 MessageLoop::current()->Run();
96 return true; 95 return true;
97 } 96 }
98 97
99 const Value& GetPreferenceValue(const std::string& name) { 98 const Value& GetPreferenceValue(const std::string& name) {
100 const PrefService::Preference* preference = 99 const PrefService::Preference* preference =
101 prefs_->FindPreference(name.c_str()); 100 prefs_->FindPreference(name.c_str());
102 return *preference->GetValue(); 101 return *preference->GetValue();
103 } 102 }
(...skipping 12 matching lines...) Expand all
116 const sync_pb::PreferenceSpecifics& specifics( 115 const sync_pb::PreferenceSpecifics& specifics(
117 node.GetPreferenceSpecifics()); 116 node.GetPreferenceSpecifics());
118 117
119 JSONReader reader; 118 JSONReader reader;
120 return reader.JsonToValue(specifics.value(), false, false); 119 return reader.JsonToValue(specifics.value(), false, false);
121 } 120 }
122 121
123 int64 WriteSyncedValue(const std::string& name, 122 int64 WriteSyncedValue(const std::string& name,
124 const Value& value, 123 const Value& value,
125 sync_api::WriteNode* node) { 124 sync_api::WriteNode* node) {
126 if (!PreferenceModelAssociator::WritePreferenceToNode(name, value, node)) 125 if (!PrefModelAssociator::WritePreferenceToNode(name, value, node))
127 return sync_api::kInvalidId; 126 return sync_api::kInvalidId;
128 return node->GetId(); 127 return node->GetId();
129 } 128 }
130 129
131 int64 SetSyncedValue(const std::string& name, const Value& value) { 130 int64 SetSyncedValue(const std::string& name, const Value& value) {
132 sync_api::WriteTransaction trans(service_->GetUserShare()); 131 sync_api::WriteTransaction trans(service_->GetUserShare());
133 sync_api::ReadNode root(&trans); 132 sync_api::ReadNode root(&trans);
134 if (!root.InitByTagLookup(browser_sync::kPreferencesTag)) 133 if (!root.InitByTagLookup(
134 syncable::ModelTypeToRootTag(syncable::PREFERENCES))) {
135 return sync_api::kInvalidId; 135 return sync_api::kInvalidId;
136 }
136 137
137 sync_api::WriteNode tag_node(&trans); 138 sync_api::WriteNode tag_node(&trans);
138 sync_api::WriteNode node(&trans); 139 sync_api::WriteNode node(&trans);
139 140
140 int64 node_id = model_associator_->GetSyncIdFromChromeId(name); 141 int64 node_id = model_associator_->GetSyncIdFromChromeId(name);
141 if (node_id == sync_api::kInvalidId) { 142 if (node_id == sync_api::kInvalidId) {
142 if (tag_node.InitByClientTagLookup(syncable::PREFERENCES, name)) 143 if (tag_node.InitByClientTagLookup(syncable::PREFERENCES, name))
143 return WriteSyncedValue(name, value, &tag_node); 144 return WriteSyncedValue(name, value, &tag_node);
144 if (node.InitUniqueByCreation(syncable::PREFERENCES, root, name)) 145 if (node.InitUniqueByCreation(syncable::PREFERENCES, root, name))
145 return WriteSyncedValue(name, value, &node); 146 return WriteSyncedValue(name, value, &node);
(...skipping 21 matching lines...) Expand all
167 JSONStringValueSerializer json(&serialized); 168 JSONStringValueSerializer json(&serialized);
168 json.Serialize(value); 169 json.Serialize(value);
169 return serialized; 170 return serialized;
170 } 171 }
171 172
172 friend class AddPreferenceEntriesTask; 173 friend class AddPreferenceEntriesTask;
173 174
174 scoped_ptr<TestingProfile> profile_; 175 scoped_ptr<TestingProfile> profile_;
175 TestingPrefService* prefs_; 176 TestingPrefService* prefs_;
176 177
177 PreferenceModelAssociator* model_associator_; 178 PreferenceDataTypeController* dtc_;
178 PreferenceChangeProcessor* change_processor_; 179 PrefModelAssociator* model_associator_;
180 GenericChangeProcessor* change_processor_;
181
179 std::string example_url0_; 182 std::string example_url0_;
180 std::string example_url1_; 183 std::string example_url1_;
181 std::string example_url2_; 184 std::string example_url2_;
182 std::string not_synced_preference_name_; 185 std::string not_synced_preference_name_;
183 std::string not_synced_preference_default_value_; 186 std::string not_synced_preference_default_value_;
184 std::string non_default_charset_value_; 187 std::string non_default_charset_value_;
185 }; 188 };
186 189
187 class AddPreferenceEntriesTask : public Task { 190 class AddPreferenceEntriesTask : public Task {
188 public: 191 public:
(...skipping 27 matching lines...) Expand all
216 ASSERT_TRUE(StartSyncService(&task, false)); 219 ASSERT_TRUE(StartSyncService(&task, false));
217 ASSERT_TRUE(task.success()); 220 ASSERT_TRUE(task.success());
218 221
219 const PrefService::Preference* pref = 222 const PrefService::Preference* pref =
220 prefs_->FindPreference(prefs::kHomePage); 223 prefs_->FindPreference(prefs::kHomePage);
221 sync_api::WriteTransaction trans(service_->GetUserShare()); 224 sync_api::WriteTransaction trans(service_->GetUserShare());
222 sync_api::WriteNode node(&trans); 225 sync_api::WriteNode node(&trans);
223 EXPECT_TRUE(node.InitByClientTagLookup(syncable::PREFERENCES, 226 EXPECT_TRUE(node.InitByClientTagLookup(syncable::PREFERENCES,
224 prefs::kHomePage)); 227 prefs::kHomePage));
225 228
226 EXPECT_TRUE(PreferenceModelAssociator::WritePreferenceToNode( 229 EXPECT_TRUE(PrefModelAssociator::WritePreferenceToNode(
227 pref->name(), *pref->GetValue(), &node)); 230 pref->name(), *pref->GetValue(), &node));
228 EXPECT_EQ(UTF8ToWide(prefs::kHomePage), node.GetTitle()); 231 EXPECT_EQ(UTF8ToWide(prefs::kHomePage), node.GetTitle());
229 const sync_pb::PreferenceSpecifics& specifics(node.GetPreferenceSpecifics()); 232 const sync_pb::PreferenceSpecifics& specifics(node.GetPreferenceSpecifics());
230 EXPECT_EQ(std::string(prefs::kHomePage), specifics.name()); 233 EXPECT_EQ(std::string(prefs::kHomePage), specifics.name());
231 234
232 base::JSONReader reader; 235 base::JSONReader reader;
233 scoped_ptr<Value> value(reader.JsonToValue(specifics.value(), false, false)); 236 scoped_ptr<Value> value(reader.JsonToValue(specifics.value(), false, false));
234 EXPECT_TRUE(pref->GetValue()->Equals(value.get())); 237 EXPECT_TRUE(pref->GetValue()->Equals(value.get()));
235 } 238 }
236 239
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 490
488 // The pref value should still be the one dictated by policy. 491 // The pref value should still be the one dictated by policy.
489 EXPECT_TRUE(managed_value->Equals(&GetPreferenceValue(prefs::kHomePage))); 492 EXPECT_TRUE(managed_value->Equals(&GetPreferenceValue(prefs::kHomePage)));
490 493
491 // Switch kHomePage back to unmanaged. 494 // Switch kHomePage back to unmanaged.
492 profile_->GetTestingPrefService()->RemoveManagedPref(prefs::kHomePage); 495 profile_->GetTestingPrefService()->RemoveManagedPref(prefs::kHomePage);
493 496
494 // Sync value should be picked up. 497 // Sync value should be picked up.
495 EXPECT_TRUE(sync_value->Equals(&GetPreferenceValue(prefs::kHomePage))); 498 EXPECT_TRUE(sync_value->Equals(&GetPreferenceValue(prefs::kHomePage)));
496 } 499 }
OLDNEW
« no previous file with comments | « chrome/browser/sync/profile_sync_service.cc ('k') | chrome/browser/sync/signin_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698