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.
6 #include "base/callback.h"
7 #include "base/json/json_reader.h"
8 #include "base/prefs/testing_pref_store.h"
9 #include "base/strings/string_util.h"
10 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
11 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "sync/api/fake_sync_change_processor.h"
13 #include "sync/api/sync_change.h"
14 #include "sync/api/sync_change_processor_wrapper_for_test.h"
15 #include "sync/api/sync_error_factory_mock.h"
16 #include "sync/protocol/sync.pb.h"
17 #include "testing/gtest/include/gtest/gtest.h"
21 class MockSyncErrorFactory
: public syncer::SyncErrorFactory
{
23 explicit MockSyncErrorFactory(syncer::ModelType type
);
24 ~MockSyncErrorFactory() override
;
26 // SyncErrorFactory implementation:
27 syncer::SyncError
CreateAndUploadError(
28 const tracked_objects::Location
& location
,
29 const std::string
& message
) override
;
32 syncer::ModelType type_
;
34 DISALLOW_COPY_AND_ASSIGN(MockSyncErrorFactory
);
37 MockSyncErrorFactory::MockSyncErrorFactory(syncer::ModelType type
)
40 MockSyncErrorFactory::~MockSyncErrorFactory() {}
42 syncer::SyncError
MockSyncErrorFactory::CreateAndUploadError(
43 const tracked_objects::Location
& location
,
44 const std::string
& message
) {
45 return syncer::SyncError(location
,
46 syncer::SyncError::DATATYPE_ERROR
,
53 const char kAtomicItemName
[] = "X-Wombat";
54 const char kSettingsName
[] = "TestingSetting";
55 const char kSettingsValue
[] = "SettingsValue";
56 const char kSplitItemName
[] = "X-SuperMoosePowers";
58 class SupervisedUserSettingsServiceTest
: public ::testing::Test
{
60 SupervisedUserSettingsServiceTest() {}
61 ~SupervisedUserSettingsServiceTest() override
{}
63 scoped_ptr
<syncer::SyncChangeProcessor
> CreateSyncProcessor() {
64 sync_processor_
.reset(new syncer::FakeSyncChangeProcessor
);
65 return scoped_ptr
<syncer::SyncChangeProcessor
>(
66 new syncer::SyncChangeProcessorWrapperForTest(sync_processor_
.get()));
69 void StartSyncing(const syncer::SyncDataList
& initial_sync_data
) {
70 scoped_ptr
<syncer::SyncErrorFactory
> error_handler(
71 new MockSyncErrorFactory(syncer::SUPERVISED_USER_SETTINGS
));
72 syncer::SyncMergeResult result
= settings_service_
.MergeDataAndStartSyncing(
73 syncer::SUPERVISED_USER_SETTINGS
,
75 CreateSyncProcessor(),
76 error_handler
.Pass());
77 EXPECT_FALSE(result
.error().IsSet());
80 void UploadSplitItem(const std::string
& key
, const std::string
& value
) {
81 split_items_
.SetStringWithoutPathExpansion(key
, value
);
82 settings_service_
.UploadItem(
83 SupervisedUserSettingsService::MakeSplitSettingKey(kSplitItemName
,
85 scoped_ptr
<base::Value
>(new base::StringValue(value
)));
88 void UploadAtomicItem(const std::string
& value
) {
89 atomic_setting_value_
.reset(new base::StringValue(value
));
90 settings_service_
.UploadItem(
92 scoped_ptr
<base::Value
>(new base::StringValue(value
)));
95 void VerifySyncDataItem(syncer::SyncData sync_data
) {
96 const sync_pb::ManagedUserSettingSpecifics
& supervised_user_setting
=
97 sync_data
.GetSpecifics().managed_user_setting();
98 base::Value
* expected_value
= NULL
;
99 if (supervised_user_setting
.name() == kAtomicItemName
) {
100 expected_value
= atomic_setting_value_
.get();
102 EXPECT_TRUE(base::StartsWith(supervised_user_setting
.name(),
103 std::string(kSplitItemName
) + ':',
104 base::CompareCase::SENSITIVE
));
106 supervised_user_setting
.name().substr(strlen(kSplitItemName
) + 1);
107 EXPECT_TRUE(split_items_
.GetWithoutPathExpansion(key
, &expected_value
));
110 scoped_ptr
<base::Value
> value(
111 base::JSONReader::DeprecatedRead(supervised_user_setting
.value()));
112 EXPECT_TRUE(expected_value
->Equals(value
.get()));
115 void OnNewSettingsAvailable(const base::DictionaryValue
* settings
) {
119 settings_
.reset(settings
->DeepCopy());
122 // testing::Test overrides:
123 void SetUp() override
{
124 TestingPrefStore
* pref_store
= new TestingPrefStore
;
125 settings_service_
.Init(pref_store
);
126 settings_service_
.Subscribe(
127 base::Bind(&SupervisedUserSettingsServiceTest::OnNewSettingsAvailable
,
128 base::Unretained(this)));
129 pref_store
->SetInitializationCompleted();
130 ASSERT_FALSE(settings_
);
131 settings_service_
.SetActive(true);
132 ASSERT_TRUE(settings_
);
135 void TearDown() override
{ settings_service_
.Shutdown(); }
137 content::TestBrowserThreadBundle thread_bundle_
;
138 base::DictionaryValue split_items_
;
139 scoped_ptr
<base::Value
> atomic_setting_value_
;
140 SupervisedUserSettingsService settings_service_
;
141 scoped_ptr
<base::DictionaryValue
> settings_
;
143 scoped_ptr
<syncer::FakeSyncChangeProcessor
> sync_processor_
;
146 TEST_F(SupervisedUserSettingsServiceTest
, ProcessAtomicSetting
) {
147 StartSyncing(syncer::SyncDataList());
148 ASSERT_TRUE(settings_
);
149 const base::Value
* value
= NULL
;
150 EXPECT_FALSE(settings_
->GetWithoutPathExpansion(kSettingsName
, &value
));
153 syncer::SyncData data
=
154 SupervisedUserSettingsService::CreateSyncDataForSetting(
155 kSettingsName
, base::StringValue(kSettingsValue
));
156 syncer::SyncChangeList change_list
;
157 change_list
.push_back(
158 syncer::SyncChange(FROM_HERE
, syncer::SyncChange::ACTION_ADD
, data
));
159 syncer::SyncError error
=
160 settings_service_
.ProcessSyncChanges(FROM_HERE
, change_list
);
161 EXPECT_FALSE(error
.IsSet()) << error
.ToString();
162 ASSERT_TRUE(settings_
);
163 ASSERT_TRUE(settings_
->GetWithoutPathExpansion(kSettingsName
, &value
));
164 std::string string_value
;
165 EXPECT_TRUE(value
->GetAsString(&string_value
));
166 EXPECT_EQ(kSettingsValue
, string_value
);
169 TEST_F(SupervisedUserSettingsServiceTest
, ProcessSplitSetting
) {
170 StartSyncing(syncer::SyncDataList());
171 ASSERT_TRUE(settings_
);
172 const base::Value
* value
= NULL
;
173 EXPECT_FALSE(settings_
->GetWithoutPathExpansion(kSettingsName
, &value
));
175 base::DictionaryValue dict
;
176 dict
.SetString("foo", "bar");
177 dict
.SetBoolean("awesomesauce", true);
178 dict
.SetInteger("eaudecologne", 4711);
181 syncer::SyncChangeList change_list
;
182 for (base::DictionaryValue::Iterator
it(dict
); !it
.IsAtEnd(); it
.Advance()) {
183 syncer::SyncData data
=
184 SupervisedUserSettingsService::CreateSyncDataForSetting(
185 SupervisedUserSettingsService::MakeSplitSettingKey(kSettingsName
,
188 change_list
.push_back(
189 syncer::SyncChange(FROM_HERE
, syncer::SyncChange::ACTION_ADD
, data
));
191 syncer::SyncError error
=
192 settings_service_
.ProcessSyncChanges(FROM_HERE
, change_list
);
193 EXPECT_FALSE(error
.IsSet()) << error
.ToString();
194 ASSERT_TRUE(settings_
);
195 ASSERT_TRUE(settings_
->GetWithoutPathExpansion(kSettingsName
, &value
));
196 const base::DictionaryValue
* dict_value
= NULL
;
197 ASSERT_TRUE(value
->GetAsDictionary(&dict_value
));
198 EXPECT_TRUE(dict_value
->Equals(&dict
));
201 TEST_F(SupervisedUserSettingsServiceTest
, SetLocalSetting
) {
202 const base::Value
* value
= NULL
;
203 EXPECT_FALSE(settings_
->GetWithoutPathExpansion(kSettingsName
, &value
));
206 settings_service_
.SetLocalSetting(
208 scoped_ptr
<base::Value
>(new base::StringValue(kSettingsValue
)));
209 ASSERT_TRUE(settings_
);
210 ASSERT_TRUE(settings_
->GetWithoutPathExpansion(kSettingsName
, &value
));
211 std::string string_value
;
212 EXPECT_TRUE(value
->GetAsString(&string_value
));
213 EXPECT_EQ(kSettingsValue
, string_value
);
216 TEST_F(SupervisedUserSettingsServiceTest
, UploadItem
) {
217 UploadSplitItem("foo", "bar");
218 UploadSplitItem("blurp", "baz");
219 UploadAtomicItem("hurdle");
221 // Uploading should produce changes when we start syncing.
222 StartSyncing(syncer::SyncDataList());
223 ASSERT_EQ(3u, sync_processor_
->changes().size());
224 for (const syncer::SyncChange
& sync_change
: sync_processor_
->changes()) {
225 ASSERT_TRUE(sync_change
.IsValid());
226 EXPECT_EQ(syncer::SyncChange::ACTION_ADD
, sync_change
.change_type());
227 VerifySyncDataItem(sync_change
.sync_data());
230 // It should also show up in local Sync data.
231 syncer::SyncDataList sync_data
=
232 settings_service_
.GetAllSyncData(syncer::SUPERVISED_USER_SETTINGS
);
233 EXPECT_EQ(3u, sync_data
.size());
234 for (const syncer::SyncData
& sync_data_item
: sync_data
)
235 VerifySyncDataItem(sync_data_item
);
237 // Uploading after we have started syncing should work too.
238 sync_processor_
->changes().clear();
239 UploadSplitItem("froodle", "narf");
240 ASSERT_EQ(1u, sync_processor_
->changes().size());
241 syncer::SyncChange change
= sync_processor_
->changes()[0];
242 ASSERT_TRUE(change
.IsValid());
243 EXPECT_EQ(syncer::SyncChange::ACTION_ADD
, change
.change_type());
244 VerifySyncDataItem(change
.sync_data());
246 sync_data
= settings_service_
.GetAllSyncData(
247 syncer::SUPERVISED_USER_SETTINGS
);
248 EXPECT_EQ(4u, sync_data
.size());
249 for (const syncer::SyncData
& sync_data_item
: sync_data
)
250 VerifySyncDataItem(sync_data_item
);
252 // Uploading an item with a previously seen key should create an UPDATE
254 sync_processor_
->changes().clear();
255 UploadSplitItem("blurp", "snarl");
256 ASSERT_EQ(1u, sync_processor_
->changes().size());
257 change
= sync_processor_
->changes()[0];
258 ASSERT_TRUE(change
.IsValid());
259 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE
, change
.change_type());
260 VerifySyncDataItem(change
.sync_data());
262 sync_data
= settings_service_
.GetAllSyncData(
263 syncer::SUPERVISED_USER_SETTINGS
);
264 EXPECT_EQ(4u, sync_data
.size());
265 for (const syncer::SyncData
& sync_data_item
: sync_data
)
266 VerifySyncDataItem(sync_data_item
);
268 sync_processor_
->changes().clear();
269 UploadAtomicItem("fjord");
270 ASSERT_EQ(1u, sync_processor_
->changes().size());
271 change
= sync_processor_
->changes()[0];
272 ASSERT_TRUE(change
.IsValid());
273 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE
, change
.change_type());
274 VerifySyncDataItem(change
.sync_data());
276 sync_data
= settings_service_
.GetAllSyncData(
277 syncer::SUPERVISED_USER_SETTINGS
);
278 EXPECT_EQ(4u, sync_data
.size());
279 for (const syncer::SyncData
& sync_data_item
: sync_data
)
280 VerifySyncDataItem(sync_data_item
);
282 // The uploaded items should not show up as settings.
283 const base::Value
* value
= NULL
;
284 EXPECT_FALSE(settings_
->GetWithoutPathExpansion(kAtomicItemName
, &value
));
285 EXPECT_FALSE(settings_
->GetWithoutPathExpansion(kSplitItemName
, &value
));
287 // Restarting sync should not create any new changes.
288 settings_service_
.StopSyncing(syncer::SUPERVISED_USER_SETTINGS
);
289 StartSyncing(sync_data
);
290 ASSERT_EQ(0u, sync_processor_
->changes().size());