Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / managed_mode / managed_user_shared_settings_service_unittest.cc
blob4b786e0f901870d2f91761f6ce5d1d89cc074520
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/json/json_writer.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/managed_mode/managed_user_shared_settings_service.h"
9 #include "chrome/common/pref_names.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "sync/api/sync_change.h"
12 #include "sync/api/sync_error_factory_mock.h"
13 #include "sync/protocol/sync.pb.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using base::DictionaryValue;
17 using base::FundamentalValue;
18 using base::StringValue;
19 using base::Value;
20 using sync_pb::ManagedUserSharedSettingSpecifics;
21 using syncer::MANAGED_USER_SHARED_SETTINGS;
22 using syncer::SyncChange;
23 using syncer::SyncChangeList;
24 using syncer::SyncChangeProcessor;
25 using syncer::SyncData;
26 using syncer::SyncDataList;
27 using syncer::SyncError;
28 using syncer::SyncErrorFactory;
29 using syncer::SyncMergeResult;
31 namespace {
33 class MockChangeProcessor : public syncer::SyncChangeProcessor {
34 public:
35 MockChangeProcessor() {}
36 virtual ~MockChangeProcessor() {}
38 // SyncChangeProcessor implementation:
39 virtual syncer::SyncError ProcessSyncChanges(
40 const tracked_objects::Location& from_here,
41 const syncer::SyncChangeList& change_list) OVERRIDE;
42 virtual syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const
43 OVERRIDE;
45 const syncer::SyncChangeList& changes() const { return change_list_; }
47 private:
48 syncer::SyncChangeList change_list_;
50 DISALLOW_COPY_AND_ASSIGN(MockChangeProcessor);
53 syncer::SyncError MockChangeProcessor::ProcessSyncChanges(
54 const tracked_objects::Location& from_here,
55 const syncer::SyncChangeList& change_list) {
56 change_list_ = change_list;
57 return syncer::SyncError();
60 syncer::SyncDataList
61 MockChangeProcessor::GetAllSyncData(syncer::ModelType type) const {
62 return syncer::SyncDataList();
65 class MockSyncErrorFactory : public syncer::SyncErrorFactory {
66 public:
67 explicit MockSyncErrorFactory(syncer::ModelType type);
68 virtual ~MockSyncErrorFactory();
70 // SyncErrorFactory implementation:
71 virtual syncer::SyncError CreateAndUploadError(
72 const tracked_objects::Location& location,
73 const std::string& message) OVERRIDE;
75 private:
76 syncer::ModelType type_;
78 DISALLOW_COPY_AND_ASSIGN(MockSyncErrorFactory);
81 MockSyncErrorFactory::MockSyncErrorFactory(syncer::ModelType type)
82 : type_(type) {}
84 MockSyncErrorFactory::~MockSyncErrorFactory() {}
86 syncer::SyncError MockSyncErrorFactory::CreateAndUploadError(
87 const tracked_objects::Location& location,
88 const std::string& message) {
89 return syncer::SyncError(location, SyncError::DATATYPE_ERROR, message, type_);
92 // Convenience method to allow us to use EXPECT_EQ to compare values.
93 std::string ToJson(const Value* value) {
94 if (!value)
95 return std::string();
97 std::string json_value;
98 base::JSONWriter::Write(value, &json_value);
99 return json_value;
102 } // namespace
104 class ManagedUserSharedSettingsServiceTest : public ::testing::Test {
105 protected:
106 typedef base::CallbackList<void(const std::string&, const std::string&)>
107 CallbackList;
109 ManagedUserSharedSettingsServiceTest()
110 : settings_service_(profile_.GetPrefs()) {}
111 virtual ~ManagedUserSharedSettingsServiceTest() {}
113 void StartSyncing(const syncer::SyncDataList& initial_sync_data) {
114 sync_processor_ = new MockChangeProcessor();
115 scoped_ptr<syncer::SyncErrorFactory> error_handler(
116 new MockSyncErrorFactory(MANAGED_USER_SHARED_SETTINGS));
117 SyncMergeResult result = settings_service_.MergeDataAndStartSyncing(
118 MANAGED_USER_SHARED_SETTINGS,
119 initial_sync_data,
120 scoped_ptr<SyncChangeProcessor>(sync_processor_),
121 error_handler.Pass());
122 EXPECT_FALSE(result.error().IsSet());
125 const base::DictionaryValue* GetAllSettings() {
126 return profile_.GetPrefs()->GetDictionary(
127 prefs::kManagedUserSharedSettings);
130 void VerifySyncChanges() {
131 const SyncChangeList& changes = sync_processor_->changes();
132 for (SyncChangeList::const_iterator it = changes.begin();
133 it != changes.end();
134 ++it) {
135 const sync_pb::ManagedUserSharedSettingSpecifics& setting =
136 it->sync_data().GetSpecifics().managed_user_shared_setting();
137 EXPECT_EQ(
138 setting.value(),
139 ToJson(settings_service_.GetValue(setting.mu_id(), setting.key())));
143 // testing::Test overrides:
144 virtual void SetUp() OVERRIDE {
145 subscription_ = settings_service_.Subscribe(
146 base::Bind(&ManagedUserSharedSettingsServiceTest::OnSettingChanged,
147 base::Unretained(this)));
150 virtual void TearDown() OVERRIDE { settings_service_.Shutdown(); }
152 void OnSettingChanged(const std::string& mu_id, const std::string& key) {
153 const Value* value = settings_service_.GetValue(mu_id, key);
154 ASSERT_TRUE(value);
155 changed_settings_.push_back(
156 ManagedUserSharedSettingsService::CreateSyncDataForSetting(
157 mu_id, key, *value, true));
160 TestingProfile profile_;
161 ManagedUserSharedSettingsService settings_service_;
162 SyncDataList changed_settings_;
164 scoped_ptr<CallbackList::Subscription> subscription_;
166 // Owned by the ManagedUserSettingsService.
167 MockChangeProcessor* sync_processor_;
170 TEST_F(ManagedUserSharedSettingsServiceTest, Empty) {
171 StartSyncing(SyncDataList());
172 EXPECT_EQ(0u, sync_processor_->changes().size());
173 EXPECT_EQ(0u, changed_settings_.size());
174 EXPECT_EQ(
176 settings_service_.GetAllSyncData(MANAGED_USER_SHARED_SETTINGS).size());
177 EXPECT_EQ(0u, GetAllSettings()->size());
180 TEST_F(ManagedUserSharedSettingsServiceTest, SetAndGet) {
181 StartSyncing(SyncDataList());
183 const char kIdA[] = "aaaaaa";
184 const char kIdB[] = "bbbbbb";
185 const char kIdC[] = "cccccc";
187 StringValue name("Jack");
188 FundamentalValue age(8);
189 StringValue bar("bar");
190 settings_service_.SetValue(kIdA, "name", name);
191 ASSERT_EQ(1u, sync_processor_->changes().size());
192 VerifySyncChanges();
193 settings_service_.SetValue(kIdA, "age", FundamentalValue(6));
194 ASSERT_EQ(1u, sync_processor_->changes().size());
195 VerifySyncChanges();
196 settings_service_.SetValue(kIdA, "age", age);
197 ASSERT_EQ(1u, sync_processor_->changes().size());
198 VerifySyncChanges();
199 settings_service_.SetValue(kIdB, "foo", bar);
200 ASSERT_EQ(1u, sync_processor_->changes().size());
201 VerifySyncChanges();
203 EXPECT_EQ(
205 settings_service_.GetAllSyncData(MANAGED_USER_SHARED_SETTINGS).size());
207 EXPECT_EQ(ToJson(&name), ToJson(settings_service_.GetValue(kIdA, "name")));
208 EXPECT_EQ(ToJson(&age), ToJson(settings_service_.GetValue(kIdA, "age")));
209 EXPECT_EQ(ToJson(&bar), ToJson(settings_service_.GetValue(kIdB, "foo")));
210 EXPECT_FALSE(settings_service_.GetValue(kIdA, "foo"));
211 EXPECT_FALSE(settings_service_.GetValue(kIdB, "name"));
212 EXPECT_FALSE(settings_service_.GetValue(kIdC, "name"));
215 TEST_F(ManagedUserSharedSettingsServiceTest, Merge) {
216 // Set initial values, then stop syncing so we can restart.
217 StartSyncing(SyncDataList());
219 const char kIdA[] = "aaaaaa";
220 const char kIdB[] = "bbbbbb";
221 const char kIdC[] = "cccccc";
223 FundamentalValue age(8);
224 StringValue bar("bar");
225 settings_service_.SetValue(kIdA, "name", StringValue("Jack"));
226 settings_service_.SetValue(kIdA, "age", age);
227 settings_service_.SetValue(kIdB, "foo", bar);
229 settings_service_.StopSyncing(MANAGED_USER_SHARED_SETTINGS);
231 StringValue name("Jill");
232 StringValue blurp("blurp");
233 SyncDataList sync_data;
234 sync_data.push_back(
235 ManagedUserSharedSettingsService::CreateSyncDataForSetting(
236 kIdA, "name", name, true));
237 sync_data.push_back(
238 ManagedUserSharedSettingsService::CreateSyncDataForSetting(
239 kIdC, "baz", blurp, true));
241 StartSyncing(sync_data);
242 EXPECT_EQ(2u, sync_processor_->changes().size());
243 VerifySyncChanges();
244 EXPECT_EQ(2u, changed_settings_.size());
246 EXPECT_EQ(
248 settings_service_.GetAllSyncData(MANAGED_USER_SHARED_SETTINGS).size());
249 EXPECT_EQ(ToJson(&name),
250 ToJson(settings_service_.GetValue(kIdA, "name")));
251 EXPECT_EQ(ToJson(&age), ToJson(settings_service_.GetValue(kIdA, "age")));
252 EXPECT_EQ(ToJson(&bar), ToJson(settings_service_.GetValue(kIdB, "foo")));
253 EXPECT_EQ(ToJson(&blurp), ToJson(settings_service_.GetValue(kIdC, "baz")));
254 EXPECT_FALSE(settings_service_.GetValue(kIdA, "foo"));
255 EXPECT_FALSE(settings_service_.GetValue(kIdB, "name"));
256 EXPECT_FALSE(settings_service_.GetValue(kIdC, "name"));
259 TEST_F(ManagedUserSharedSettingsServiceTest, ProcessChanges) {
260 StartSyncing(SyncDataList());
262 const char kIdA[] = "aaaaaa";
263 const char kIdB[] = "bbbbbb";
264 const char kIdC[] = "cccccc";
266 FundamentalValue age(8);
267 StringValue bar("bar");
268 settings_service_.SetValue(kIdA, "name", StringValue("Jack"));
269 settings_service_.SetValue(kIdA, "age", age);
270 settings_service_.SetValue(kIdB, "foo", bar);
272 StringValue name("Jill");
273 StringValue blurp("blurp");
274 SyncChangeList changes;
275 changes.push_back(
276 SyncChange(FROM_HERE,
277 SyncChange::ACTION_UPDATE,
278 ManagedUserSharedSettingsService::CreateSyncDataForSetting(
279 kIdA, "name", name, true)));
280 changes.push_back(
281 SyncChange(FROM_HERE,
282 SyncChange::ACTION_ADD,
283 ManagedUserSharedSettingsService::CreateSyncDataForSetting(
284 kIdC, "baz", blurp, true)));
285 SyncError error = settings_service_.ProcessSyncChanges(FROM_HERE, changes);
286 EXPECT_FALSE(error.IsSet()) << error.ToString();
287 EXPECT_EQ(2u, changed_settings_.size());
289 EXPECT_EQ(
291 settings_service_.GetAllSyncData(MANAGED_USER_SHARED_SETTINGS).size());
292 EXPECT_EQ(ToJson(&name),
293 ToJson(settings_service_.GetValue(kIdA, "name")));
294 EXPECT_EQ(ToJson(&age), ToJson(settings_service_.GetValue(kIdA, "age")));
295 EXPECT_EQ(ToJson(&bar), ToJson(settings_service_.GetValue(kIdB, "foo")));
296 EXPECT_EQ(ToJson(&blurp), ToJson(settings_service_.GetValue(kIdC, "baz")));
297 EXPECT_FALSE(settings_service_.GetValue(kIdA, "foo"));
298 EXPECT_FALSE(settings_service_.GetValue(kIdB, "name"));
299 EXPECT_FALSE(settings_service_.GetValue(kIdC, "name"));