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.
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
;
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
;
39 class MockSyncErrorFactory
: public syncer::SyncErrorFactory
{
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
;
50 syncer::ModelType type_
;
52 DISALLOW_COPY_AND_ASSIGN(MockSyncErrorFactory
);
55 MockSyncErrorFactory::MockSyncErrorFactory(syncer::ModelType 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
) {
71 std::string json_value
;
72 base::JSONWriter::Write(*value
, &json_value
);
78 class SupervisedUserSharedSettingsServiceTest
: public ::testing::Test
{
80 typedef base::CallbackList
<void(const std::string
&, const std::string
&)>
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
,
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();
112 ToJson(settings_service_
.GetValue(setting
.mu_id(), setting
.key())));
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
);
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());
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();
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
;
209 SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
210 kIdA
, "name", name
, true));
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());
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
;
250 SyncChange(FROM_HERE
,
251 SyncChange::ACTION_UPDATE
,
252 SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
253 kIdA
, "name", name
, true)));
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());
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"));