Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / supervised_user / legacy / supervised_user_shared_settings_service_unittest.cc
blob522fe7ced23bf3b94ee707a14d7dd6ca78c4910e
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 "sync/api/fake_sync_change_processor.h"
14 #include "sync/api/sync_change.h"
15 #include "sync/api/sync_change_processor_wrapper_for_test.h"
16 #include "sync/api/sync_error_factory_mock.h"
17 #include "sync/protocol/sync.pb.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using base::DictionaryValue;
21 using base::FundamentalValue;
22 using base::StringValue;
23 using base::Value;
24 using sync_pb::ManagedUserSharedSettingSpecifics;
25 using syncer::SUPERVISED_USER_SHARED_SETTINGS;
26 using syncer::SyncChange;
27 using syncer::SyncChangeList;
28 using syncer::SyncChangeProcessor;
29 using syncer::SyncChangeProcessorWrapperForTest;
30 using syncer::SyncData;
31 using syncer::SyncDataList;
32 using syncer::SyncError;
33 using syncer::SyncErrorFactory;
34 using syncer::SyncMergeResult;
36 namespace {
38 class MockSyncErrorFactory : public syncer::SyncErrorFactory {
39 public:
40 explicit MockSyncErrorFactory(syncer::ModelType type);
41 ~MockSyncErrorFactory() override;
43 // SyncErrorFactory implementation:
44 syncer::SyncError CreateAndUploadError(
45 const tracked_objects::Location& location,
46 const std::string& message) override;
48 private:
49 syncer::ModelType type_;
51 DISALLOW_COPY_AND_ASSIGN(MockSyncErrorFactory);
54 MockSyncErrorFactory::MockSyncErrorFactory(syncer::ModelType type)
55 : type_(type) {}
57 MockSyncErrorFactory::~MockSyncErrorFactory() {}
59 syncer::SyncError MockSyncErrorFactory::CreateAndUploadError(
60 const tracked_objects::Location& location,
61 const std::string& message) {
62 return syncer::SyncError(location, SyncError::DATATYPE_ERROR, message, type_);
65 // Convenience method to allow us to use EXPECT_EQ to compare values.
66 std::string ToJson(const Value* value) {
67 if (!value)
68 return std::string();
70 std::string json_value;
71 base::JSONWriter::Write(value, &json_value);
72 return json_value;
75 } // namespace
77 class SupervisedUserSharedSettingsServiceTest : public ::testing::Test {
78 protected:
79 typedef base::CallbackList<void(const std::string&, const std::string&)>
80 CallbackList;
82 SupervisedUserSharedSettingsServiceTest()
83 : settings_service_(profile_.GetPrefs()) {}
84 ~SupervisedUserSharedSettingsServiceTest() override {}
86 void StartSyncing(const syncer::SyncDataList& initial_sync_data) {
87 sync_processor_.reset(new syncer::FakeSyncChangeProcessor);
88 scoped_ptr<syncer::SyncErrorFactory> error_handler(
89 new MockSyncErrorFactory(SUPERVISED_USER_SHARED_SETTINGS));
90 SyncMergeResult result = settings_service_.MergeDataAndStartSyncing(
91 SUPERVISED_USER_SHARED_SETTINGS,
92 initial_sync_data,
93 scoped_ptr<SyncChangeProcessor>(
94 new SyncChangeProcessorWrapperForTest(sync_processor_.get())),
95 error_handler.Pass());
96 EXPECT_FALSE(result.error().IsSet());
99 const base::DictionaryValue* GetAllSettings() {
100 return profile_.GetPrefs()->GetDictionary(
101 prefs::kSupervisedUserSharedSettings);
104 void VerifySyncChangesAndClear() {
105 SyncChangeList& changes = sync_processor_->changes();
106 for (const SyncChange& sync_change : changes) {
107 const sync_pb::ManagedUserSharedSettingSpecifics& setting =
108 sync_change.sync_data().GetSpecifics().managed_user_shared_setting();
109 EXPECT_EQ(
110 setting.value(),
111 ToJson(settings_service_.GetValue(setting.mu_id(), setting.key())));
113 changes.clear();
116 // testing::Test overrides:
117 void SetUp() override {
118 subscription_ = settings_service_.Subscribe(
119 base::Bind(&SupervisedUserSharedSettingsServiceTest::OnSettingChanged,
120 base::Unretained(this)));
123 void TearDown() override { settings_service_.Shutdown(); }
125 void OnSettingChanged(const std::string& su_id, const std::string& key) {
126 const Value* value = settings_service_.GetValue(su_id, key);
127 ASSERT_TRUE(value);
128 changed_settings_.push_back(
129 SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
130 su_id, key, *value, true));
133 TestingProfile profile_;
134 SupervisedUserSharedSettingsService settings_service_;
135 SyncDataList changed_settings_;
137 scoped_ptr<CallbackList::Subscription> subscription_;
139 scoped_ptr<syncer::FakeSyncChangeProcessor> sync_processor_;
142 TEST_F(SupervisedUserSharedSettingsServiceTest, Empty) {
143 StartSyncing(SyncDataList());
144 EXPECT_EQ(0u, sync_processor_->changes().size());
145 EXPECT_EQ(0u, changed_settings_.size());
146 EXPECT_EQ(
148 settings_service_.GetAllSyncData(SUPERVISED_USER_SHARED_SETTINGS).size());
149 EXPECT_EQ(0u, GetAllSettings()->size());
152 TEST_F(SupervisedUserSharedSettingsServiceTest, SetAndGet) {
153 StartSyncing(SyncDataList());
155 const char kIdA[] = "aaaaaa";
156 const char kIdB[] = "bbbbbb";
157 const char kIdC[] = "cccccc";
159 StringValue name("Jack");
160 FundamentalValue age(8);
161 StringValue bar("bar");
162 settings_service_.SetValue(kIdA, "name", name);
163 ASSERT_EQ(1u, sync_processor_->changes().size());
164 VerifySyncChangesAndClear();
165 settings_service_.SetValue(kIdA, "age", FundamentalValue(6));
166 ASSERT_EQ(1u, sync_processor_->changes().size());
167 VerifySyncChangesAndClear();
168 settings_service_.SetValue(kIdA, "age", age);
169 ASSERT_EQ(1u, sync_processor_->changes().size());
170 VerifySyncChangesAndClear();
171 settings_service_.SetValue(kIdB, "foo", bar);
172 ASSERT_EQ(1u, sync_processor_->changes().size());
173 VerifySyncChangesAndClear();
175 EXPECT_EQ(
177 settings_service_.GetAllSyncData(SUPERVISED_USER_SHARED_SETTINGS).size());
179 EXPECT_EQ(ToJson(&name), ToJson(settings_service_.GetValue(kIdA, "name")));
180 EXPECT_EQ(ToJson(&age), ToJson(settings_service_.GetValue(kIdA, "age")));
181 EXPECT_EQ(ToJson(&bar), ToJson(settings_service_.GetValue(kIdB, "foo")));
182 EXPECT_FALSE(settings_service_.GetValue(kIdA, "foo"));
183 EXPECT_FALSE(settings_service_.GetValue(kIdB, "name"));
184 EXPECT_FALSE(settings_service_.GetValue(kIdC, "name"));
187 TEST_F(SupervisedUserSharedSettingsServiceTest, Merge) {
188 // Set initial values, then stop syncing so we can restart.
189 StartSyncing(SyncDataList());
191 const char kIdA[] = "aaaaaa";
192 const char kIdB[] = "bbbbbb";
193 const char kIdC[] = "cccccc";
195 FundamentalValue age(8);
196 StringValue bar("bar");
197 settings_service_.SetValue(kIdA, "name", StringValue("Jack"));
198 settings_service_.SetValue(kIdA, "age", age);
199 settings_service_.SetValue(kIdB, "foo", bar);
201 settings_service_.StopSyncing(SUPERVISED_USER_SHARED_SETTINGS);
203 StringValue name("Jill");
204 StringValue blurp("blurp");
205 SyncDataList sync_data;
206 sync_data.push_back(
207 SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
208 kIdA, "name", name, true));
209 sync_data.push_back(
210 SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
211 kIdC, "baz", blurp, true));
213 StartSyncing(sync_data);
214 EXPECT_EQ(2u, sync_processor_->changes().size());
215 VerifySyncChangesAndClear();
216 EXPECT_EQ(2u, changed_settings_.size());
218 EXPECT_EQ(
220 settings_service_.GetAllSyncData(SUPERVISED_USER_SHARED_SETTINGS).size());
221 EXPECT_EQ(ToJson(&name),
222 ToJson(settings_service_.GetValue(kIdA, "name")));
223 EXPECT_EQ(ToJson(&age), ToJson(settings_service_.GetValue(kIdA, "age")));
224 EXPECT_EQ(ToJson(&bar), ToJson(settings_service_.GetValue(kIdB, "foo")));
225 EXPECT_EQ(ToJson(&blurp), ToJson(settings_service_.GetValue(kIdC, "baz")));
226 EXPECT_FALSE(settings_service_.GetValue(kIdA, "foo"));
227 EXPECT_FALSE(settings_service_.GetValue(kIdB, "name"));
228 EXPECT_FALSE(settings_service_.GetValue(kIdC, "name"));
231 TEST_F(SupervisedUserSharedSettingsServiceTest, ProcessChanges) {
232 StartSyncing(SyncDataList());
234 const char kIdA[] = "aaaaaa";
235 const char kIdB[] = "bbbbbb";
236 const char kIdC[] = "cccccc";
238 FundamentalValue age(8);
239 StringValue bar("bar");
240 settings_service_.SetValue(kIdA, "name", StringValue("Jack"));
241 settings_service_.SetValue(kIdA, "age", age);
242 settings_service_.SetValue(kIdB, "foo", bar);
244 StringValue name("Jill");
245 StringValue blurp("blurp");
246 SyncChangeList changes;
247 changes.push_back(
248 SyncChange(FROM_HERE,
249 SyncChange::ACTION_UPDATE,
250 SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
251 kIdA, "name", name, true)));
252 changes.push_back(
253 SyncChange(FROM_HERE,
254 SyncChange::ACTION_ADD,
255 SupervisedUserSharedSettingsService::CreateSyncDataForSetting(
256 kIdC, "baz", blurp, true)));
257 SyncError error = settings_service_.ProcessSyncChanges(FROM_HERE, changes);
258 EXPECT_FALSE(error.IsSet()) << error.ToString();
259 EXPECT_EQ(2u, changed_settings_.size());
261 EXPECT_EQ(
263 settings_service_.GetAllSyncData(SUPERVISED_USER_SHARED_SETTINGS).size());
264 EXPECT_EQ(ToJson(&name),
265 ToJson(settings_service_.GetValue(kIdA, "name")));
266 EXPECT_EQ(ToJson(&age), ToJson(settings_service_.GetValue(kIdA, "age")));
267 EXPECT_EQ(ToJson(&bar), ToJson(settings_service_.GetValue(kIdB, "foo")));
268 EXPECT_EQ(ToJson(&blurp), ToJson(settings_service_.GetValue(kIdC, "baz")));
269 EXPECT_FALSE(settings_service_.GetValue(kIdA, "foo"));
270 EXPECT_FALSE(settings_service_.GetValue(kIdB, "name"));
271 EXPECT_FALSE(settings_service_.GetValue(kIdC, "name"));