QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / chrome / browser / supervised_user / legacy / supervised_user_shared_settings_service_unittest.cc
blobffa3940b610d887a63fcdd0bf668cd952eea0712
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 <string>
7 #include "base/bind.h"
8 #include "base/json/json_writer.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_service.h"
11 #include "chrome/common/pref_names.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "sync/api/fake_sync_change_processor.h"
15 #include "sync/api/sync_change.h"
16 #include "sync/api/sync_change_processor_wrapper_for_test.h"
17 #include "sync/api/sync_error_factory_mock.h"
18 #include "sync/protocol/sync.pb.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using base::DictionaryValue;
22 using base::FundamentalValue;
23 using base::StringValue;
24 using base::Value;
25 using sync_pb::ManagedUserSharedSettingSpecifics;
26 using syncer::SUPERVISED_USER_SHARED_SETTINGS;
27 using syncer::SyncChange;
28 using syncer::SyncChangeList;
29 using syncer::SyncChangeProcessor;
30 using syncer::SyncChangeProcessorWrapperForTest;
31 using syncer::SyncData;
32 using syncer::SyncDataList;
33 using syncer::SyncError;
34 using syncer::SyncErrorFactory;
35 using syncer::SyncMergeResult;
37 namespace {
39 class MockSyncErrorFactory : public syncer::SyncErrorFactory {
40 public:
41 explicit MockSyncErrorFactory(syncer::ModelType type);
42 ~MockSyncErrorFactory() override;
44 // SyncErrorFactory implementation:
45 syncer::SyncError CreateAndUploadError(
46 const tracked_objects::Location& location,
47 const std::string& message) override;
49 private:
50 syncer::ModelType type_;
52 DISALLOW_COPY_AND_ASSIGN(MockSyncErrorFactory);
55 MockSyncErrorFactory::MockSyncErrorFactory(syncer::ModelType type)
56 : type_(type) {}
58 MockSyncErrorFactory::~MockSyncErrorFactory() {}
60 syncer::SyncError MockSyncErrorFactory::CreateAndUploadError(
61 const tracked_objects::Location& location,
62 const std::string& message) {
63 return syncer::SyncError(location, SyncError::DATATYPE_ERROR, message, type_);
66 // Convenience method to allow us to use EXPECT_EQ to compare values.
67 std::string ToJson(const Value* value) {
68 if (!value)
69 return std::string();
71 std::string json_value;
72 base::JSONWriter::Write(*value, &json_value);
73 return json_value;
76 } // namespace
78 class SupervisedUserSharedSettingsServiceTest : public ::testing::Test {
79 protected:
80 typedef base::CallbackList<void(const std::string&, const std::string&)>
81 CallbackList;
83 SupervisedUserSharedSettingsServiceTest()
84 : settings_service_(profile_.GetPrefs()) {}
85 ~SupervisedUserSharedSettingsServiceTest() override {}
87 void StartSyncing(const syncer::SyncDataList& initial_sync_data) {
88 sync_processor_.reset(new syncer::FakeSyncChangeProcessor);
89 scoped_ptr<syncer::SyncErrorFactory> error_handler(
90 new MockSyncErrorFactory(SUPERVISED_USER_SHARED_SETTINGS));
91 SyncMergeResult result = settings_service_.MergeDataAndStartSyncing(
92 SUPERVISED_USER_SHARED_SETTINGS,
93 initial_sync_data,
94 scoped_ptr<SyncChangeProcessor>(
95 new SyncChangeProcessorWrapperForTest(sync_processor_.get())),
96 error_handler.Pass());
97 EXPECT_FALSE(result.error().IsSet());
100 const base::DictionaryValue* GetAllSettings() {
101 return profile_.GetPrefs()->GetDictionary(
102 prefs::kSupervisedUserSharedSettings);
105 void VerifySyncChangesAndClear() {
106 SyncChangeList& changes = sync_processor_->changes();
107 for (const SyncChange& sync_change : changes) {
108 const sync_pb::ManagedUserSharedSettingSpecifics& setting =
109 sync_change.sync_data().GetSpecifics().managed_user_shared_setting();
110 EXPECT_EQ(
111 setting.value(),
112 ToJson(settings_service_.GetValue(setting.mu_id(), setting.key())));
114 changes.clear();
117 // testing::Test overrides:
118 void SetUp() override {
119 subscription_ = settings_service_.Subscribe(
120 base::Bind(&SupervisedUserSharedSettingsServiceTest::OnSettingChanged,
121 base::Unretained(this)));
124 void TearDown() override { settings_service_.Shutdown(); }
126 void OnSettingChanged(const std::string& su_id, const std::string& key) {
127 const Value* value = settings_service_.GetValue(su_id, key);
128 ASSERT_TRUE(value);
129 changed_settings_.push_back(
130 SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
131 su_id, key, *value, true));
134 content::TestBrowserThreadBundle thread_bundle_;
135 TestingProfile profile_;
136 SupervisedUserSharedSettingsService settings_service_;
137 SyncDataList changed_settings_;
139 scoped_ptr<CallbackList::Subscription> subscription_;
141 scoped_ptr<syncer::FakeSyncChangeProcessor> sync_processor_;
144 TEST_F(SupervisedUserSharedSettingsServiceTest, Empty) {
145 StartSyncing(SyncDataList());
146 EXPECT_EQ(0u, sync_processor_->changes().size());
147 EXPECT_EQ(0u, changed_settings_.size());
148 EXPECT_EQ(
150 settings_service_.GetAllSyncData(SUPERVISED_USER_SHARED_SETTINGS).size());
151 EXPECT_EQ(0u, GetAllSettings()->size());
154 TEST_F(SupervisedUserSharedSettingsServiceTest, SetAndGet) {
155 StartSyncing(SyncDataList());
157 const char kIdA[] = "aaaaaa";
158 const char kIdB[] = "bbbbbb";
159 const char kIdC[] = "cccccc";
161 StringValue name("Jack");
162 FundamentalValue age(8);
163 StringValue bar("bar");
164 settings_service_.SetValue(kIdA, "name", name);
165 ASSERT_EQ(1u, sync_processor_->changes().size());
166 VerifySyncChangesAndClear();
167 settings_service_.SetValue(kIdA, "age", FundamentalValue(6));
168 ASSERT_EQ(1u, sync_processor_->changes().size());
169 VerifySyncChangesAndClear();
170 settings_service_.SetValue(kIdA, "age", age);
171 ASSERT_EQ(1u, sync_processor_->changes().size());
172 VerifySyncChangesAndClear();
173 settings_service_.SetValue(kIdB, "foo", bar);
174 ASSERT_EQ(1u, sync_processor_->changes().size());
175 VerifySyncChangesAndClear();
177 EXPECT_EQ(
179 settings_service_.GetAllSyncData(SUPERVISED_USER_SHARED_SETTINGS).size());
181 EXPECT_EQ(ToJson(&name), ToJson(settings_service_.GetValue(kIdA, "name")));
182 EXPECT_EQ(ToJson(&age), ToJson(settings_service_.GetValue(kIdA, "age")));
183 EXPECT_EQ(ToJson(&bar), ToJson(settings_service_.GetValue(kIdB, "foo")));
184 EXPECT_FALSE(settings_service_.GetValue(kIdA, "foo"));
185 EXPECT_FALSE(settings_service_.GetValue(kIdB, "name"));
186 EXPECT_FALSE(settings_service_.GetValue(kIdC, "name"));
189 TEST_F(SupervisedUserSharedSettingsServiceTest, Merge) {
190 // Set initial values, then stop syncing so we can restart.
191 StartSyncing(SyncDataList());
193 const char kIdA[] = "aaaaaa";
194 const char kIdB[] = "bbbbbb";
195 const char kIdC[] = "cccccc";
197 FundamentalValue age(8);
198 StringValue bar("bar");
199 settings_service_.SetValue(kIdA, "name", StringValue("Jack"));
200 settings_service_.SetValue(kIdA, "age", age);
201 settings_service_.SetValue(kIdB, "foo", bar);
203 settings_service_.StopSyncing(SUPERVISED_USER_SHARED_SETTINGS);
205 StringValue name("Jill");
206 StringValue blurp("blurp");
207 SyncDataList sync_data;
208 sync_data.push_back(
209 SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
210 kIdA, "name", name, true));
211 sync_data.push_back(
212 SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
213 kIdC, "baz", blurp, true));
215 StartSyncing(sync_data);
216 EXPECT_EQ(2u, sync_processor_->changes().size());
217 VerifySyncChangesAndClear();
218 EXPECT_EQ(2u, changed_settings_.size());
220 EXPECT_EQ(
222 settings_service_.GetAllSyncData(SUPERVISED_USER_SHARED_SETTINGS).size());
223 EXPECT_EQ(ToJson(&name),
224 ToJson(settings_service_.GetValue(kIdA, "name")));
225 EXPECT_EQ(ToJson(&age), ToJson(settings_service_.GetValue(kIdA, "age")));
226 EXPECT_EQ(ToJson(&bar), ToJson(settings_service_.GetValue(kIdB, "foo")));
227 EXPECT_EQ(ToJson(&blurp), ToJson(settings_service_.GetValue(kIdC, "baz")));
228 EXPECT_FALSE(settings_service_.GetValue(kIdA, "foo"));
229 EXPECT_FALSE(settings_service_.GetValue(kIdB, "name"));
230 EXPECT_FALSE(settings_service_.GetValue(kIdC, "name"));
233 TEST_F(SupervisedUserSharedSettingsServiceTest, ProcessChanges) {
234 StartSyncing(SyncDataList());
236 const char kIdA[] = "aaaaaa";
237 const char kIdB[] = "bbbbbb";
238 const char kIdC[] = "cccccc";
240 FundamentalValue age(8);
241 StringValue bar("bar");
242 settings_service_.SetValue(kIdA, "name", StringValue("Jack"));
243 settings_service_.SetValue(kIdA, "age", age);
244 settings_service_.SetValue(kIdB, "foo", bar);
246 StringValue name("Jill");
247 StringValue blurp("blurp");
248 SyncChangeList changes;
249 changes.push_back(
250 SyncChange(FROM_HERE,
251 SyncChange::ACTION_UPDATE,
252 SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
253 kIdA, "name", name, true)));
254 changes.push_back(
255 SyncChange(FROM_HERE,
256 SyncChange::ACTION_ADD,
257 SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
258 kIdC, "baz", blurp, true)));
259 SyncError error = settings_service_.ProcessSyncChanges(FROM_HERE, changes);
260 EXPECT_FALSE(error.IsSet()) << error.ToString();
261 EXPECT_EQ(2u, changed_settings_.size());
263 EXPECT_EQ(
265 settings_service_.GetAllSyncData(SUPERVISED_USER_SHARED_SETTINGS).size());
266 EXPECT_EQ(ToJson(&name),
267 ToJson(settings_service_.GetValue(kIdA, "name")));
268 EXPECT_EQ(ToJson(&age), ToJson(settings_service_.GetValue(kIdA, "age")));
269 EXPECT_EQ(ToJson(&bar), ToJson(settings_service_.GetValue(kIdB, "foo")));
270 EXPECT_EQ(ToJson(&blurp), ToJson(settings_service_.GetValue(kIdC, "baz")));
271 EXPECT_FALSE(settings_service_.GetValue(kIdA, "foo"));
272 EXPECT_FALSE(settings_service_.GetValue(kIdB, "name"));
273 EXPECT_FALSE(settings_service_.GetValue(kIdC, "name"));