Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / managed_mode / managed_user_sync_service_unittest.cc
blobead1441e3dbf5fb09851da21278982c72f44792b
1 // Copyright 2013 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/prefs/scoped_user_pref_update.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/threading/sequenced_worker_pool.h"
11 #include "chrome/browser/managed_mode/managed_user_sync_service.h"
12 #include "chrome/browser/managed_mode/managed_user_sync_service_factory.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "sync/api/sync_change.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 sync_pb::ManagedUserSpecifics;
22 using syncer::MANAGED_USERS;
23 using syncer::SyncChange;
24 using syncer::SyncChangeList;
25 using syncer::SyncChangeProcessor;
26 using syncer::SyncData;
27 using syncer::SyncDataList;
28 using syncer::SyncError;
29 using syncer::SyncErrorFactory;
30 using syncer::SyncMergeResult;
32 namespace {
34 class MockChangeProcessor : public SyncChangeProcessor {
35 public:
36 MockChangeProcessor() {}
37 virtual ~MockChangeProcessor() {}
39 // SyncChangeProcessor implementation:
40 virtual SyncError ProcessSyncChanges(
41 const tracked_objects::Location& from_here,
42 const SyncChangeList& change_list) OVERRIDE;
44 virtual SyncDataList GetAllSyncData(syncer::ModelType type) const
45 OVERRIDE {
46 return SyncDataList();
49 const SyncChangeList& changes() const { return change_list_; }
50 SyncChange GetChange(const std::string& id) const;
52 private:
53 SyncChangeList change_list_;
56 SyncError MockChangeProcessor::ProcessSyncChanges(
57 const tracked_objects::Location& from_here,
58 const SyncChangeList& change_list) {
59 change_list_ = change_list;
60 return SyncError();
63 SyncChange MockChangeProcessor::GetChange(const std::string& id) const {
64 for (SyncChangeList::const_iterator it = change_list_.begin();
65 it != change_list_.end(); ++it) {
66 if (it->sync_data().GetSpecifics().managed_user().id() == id)
67 return *it;
69 return SyncChange();
72 // Callback for ManagedUserSyncService::GetManagedUsersAsync().
73 void GetManagedUsersCallback(const base::DictionaryValue** dict,
74 const base::DictionaryValue* managed_users) {
75 *dict = managed_users;
78 } // namespace
80 class ManagedUserSyncServiceTest : public ::testing::Test {
81 public:
82 ManagedUserSyncServiceTest();
83 virtual ~ManagedUserSyncServiceTest();
85 protected:
86 scoped_ptr<SyncChangeProcessor> CreateChangeProcessor();
87 scoped_ptr<SyncErrorFactory> CreateErrorFactory();
88 SyncData CreateRemoteData(const std::string& id,
89 const std::string& name,
90 const std::string& avatar);
92 PrefService* prefs() { return profile_.GetPrefs(); }
93 ManagedUserSyncService* service() { return service_; }
94 MockChangeProcessor* change_processor() { return change_processor_; }
96 private:
97 TestingProfile profile_;
98 ManagedUserSyncService* service_;
100 // Owned by the ManagedUserSyncService.
101 MockChangeProcessor* change_processor_;
103 // A unique ID for creating "remote" Sync data.
104 int64 sync_data_id_;
107 ManagedUserSyncServiceTest::ManagedUserSyncServiceTest()
108 : change_processor_(NULL),
109 sync_data_id_(0) {
110 service_ = ManagedUserSyncServiceFactory::GetForProfile(&profile_);
113 ManagedUserSyncServiceTest::~ManagedUserSyncServiceTest() {}
115 scoped_ptr<SyncChangeProcessor>
116 ManagedUserSyncServiceTest::CreateChangeProcessor() {
117 EXPECT_FALSE(change_processor_);
118 change_processor_ = new MockChangeProcessor();
119 return scoped_ptr<SyncChangeProcessor>(change_processor_);
122 scoped_ptr<SyncErrorFactory>
123 ManagedUserSyncServiceTest::CreateErrorFactory() {
124 return scoped_ptr<SyncErrorFactory>(new syncer::SyncErrorFactoryMock());
127 SyncData ManagedUserSyncServiceTest::CreateRemoteData(
128 const std::string& id,
129 const std::string& name,
130 const std::string& chrome_avatar) {
131 ::sync_pb::EntitySpecifics specifics;
132 specifics.mutable_managed_user()->set_id(id);
133 specifics.mutable_managed_user()->set_name(name);
134 specifics.mutable_managed_user()->set_acknowledged(true);
135 if (!chrome_avatar.empty())
136 specifics.mutable_managed_user()->set_chrome_avatar(chrome_avatar);
138 return SyncData::CreateRemoteData(++sync_data_id_, specifics, base::Time());
141 TEST_F(ManagedUserSyncServiceTest, MergeEmpty) {
142 SyncMergeResult result =
143 service()->MergeDataAndStartSyncing(MANAGED_USERS,
144 SyncDataList(),
145 CreateChangeProcessor(),
146 CreateErrorFactory());
147 EXPECT_FALSE(result.error().IsSet());
148 EXPECT_EQ(0, result.num_items_added());
149 EXPECT_EQ(0, result.num_items_modified());
150 EXPECT_EQ(0, result.num_items_deleted());
151 EXPECT_EQ(0, result.num_items_before_association());
152 EXPECT_EQ(0, result.num_items_after_association());
153 EXPECT_EQ(0u, service()->GetManagedUsers()->size());
154 EXPECT_EQ(0u, change_processor()->changes().size());
156 service()->StopSyncing(MANAGED_USERS);
157 service()->Shutdown();
160 TEST_F(ManagedUserSyncServiceTest, MergeExisting) {
161 const char kNameKey[] = "name";
162 const char kAcknowledgedKey[] = "acknowledged";
163 const char kChromeAvatarKey[] = "chromeAvatar";
165 const char kUserId1[] = "aaaaa";
166 const char kUserId2[] = "bbbbb";
167 const char kUserId3[] = "ccccc";
168 const char kUserId4[] = "ddddd";
169 const char kName1[] = "Anchor";
170 const char kName2[] = "Buzz";
171 const char kName3[] = "Crush";
172 const char kName4[] = "Dory";
173 const char kAvatar1[] = "";
174 #if defined(OS_CHROMEOS)
175 const char kAvatar2[] = "chromeos-avatar-index:0";
176 const char kAvatar3[] = "chromeos-avatar-index:20";
177 #else
178 const char kAvatar2[] = "chrome-avatar-index:0";
179 const char kAvatar3[] = "chrome-avatar-index:20";
180 #endif
181 const char kAvatar4[] = "";
183 DictionaryPrefUpdate update(prefs(), prefs::kManagedUsers);
184 base::DictionaryValue* managed_users = update.Get();
185 base::DictionaryValue* dict = new base::DictionaryValue;
186 dict->SetString(kNameKey, kName1);
187 managed_users->Set(kUserId1, dict);
188 dict = new base::DictionaryValue;
189 dict->SetString(kNameKey, kName2);
190 dict->SetBoolean(kAcknowledgedKey, true);
191 managed_users->Set(kUserId2, dict);
194 const base::DictionaryValue* async_managed_users = NULL;
195 service()->GetManagedUsersAsync(
196 base::Bind(&GetManagedUsersCallback, &async_managed_users));
198 SyncDataList initial_sync_data;
199 initial_sync_data.push_back(CreateRemoteData(kUserId2, kName2, kAvatar2));
200 initial_sync_data.push_back(CreateRemoteData(kUserId3, kName3, kAvatar3));
201 initial_sync_data.push_back(CreateRemoteData(kUserId4, kName4, kAvatar4));
203 SyncMergeResult result =
204 service()->MergeDataAndStartSyncing(MANAGED_USERS,
205 initial_sync_data,
206 CreateChangeProcessor(),
207 CreateErrorFactory());
208 EXPECT_FALSE(result.error().IsSet());
209 EXPECT_EQ(2, result.num_items_added());
210 EXPECT_EQ(1, result.num_items_modified());
211 EXPECT_EQ(0, result.num_items_deleted());
212 EXPECT_EQ(2, result.num_items_before_association());
213 EXPECT_EQ(4, result.num_items_after_association());
215 const base::DictionaryValue* managed_users = service()->GetManagedUsers();
216 EXPECT_EQ(4u, managed_users->size());
217 EXPECT_TRUE(async_managed_users);
218 EXPECT_TRUE(managed_users->Equals(async_managed_users));
221 const base::DictionaryValue* managed_user = NULL;
222 ASSERT_TRUE(managed_users->GetDictionary(kUserId2, &managed_user));
223 ASSERT_TRUE(managed_user);
224 std::string name;
225 EXPECT_TRUE(managed_user->GetString(kNameKey, &name));
226 EXPECT_EQ(kName2, name);
227 bool acknowledged = false;
228 EXPECT_TRUE(managed_user->GetBoolean(kAcknowledgedKey, &acknowledged));
229 EXPECT_TRUE(acknowledged);
230 std::string avatar;
231 EXPECT_TRUE(managed_user->GetString(kChromeAvatarKey, &avatar));
232 EXPECT_EQ(kAvatar2, avatar);
235 const base::DictionaryValue* managed_user = NULL;
236 ASSERT_TRUE(managed_users->GetDictionary(kUserId3, &managed_user));
237 ASSERT_TRUE(managed_user);
238 std::string name;
239 EXPECT_TRUE(managed_user->GetString(kNameKey, &name));
240 EXPECT_EQ(kName3, name);
241 bool acknowledged = false;
242 EXPECT_TRUE(managed_user->GetBoolean(kAcknowledgedKey, &acknowledged));
243 EXPECT_TRUE(acknowledged);
244 std::string avatar;
245 EXPECT_TRUE(managed_user->GetString(kChromeAvatarKey, &avatar));
246 EXPECT_EQ(kAvatar3, avatar);
249 const base::DictionaryValue* managed_user = NULL;
250 ASSERT_TRUE(managed_users->GetDictionary(kUserId4, &managed_user));
251 ASSERT_TRUE(managed_user);
252 std::string name;
253 EXPECT_TRUE(managed_user->GetString(kNameKey, &name));
254 EXPECT_EQ(kName4, name);
255 bool acknowledged = false;
256 EXPECT_TRUE(managed_user->GetBoolean(kAcknowledgedKey, &acknowledged));
257 EXPECT_TRUE(acknowledged);
258 std::string avatar;
259 EXPECT_TRUE(managed_user->GetString(kChromeAvatarKey, &avatar));
260 EXPECT_EQ(kAvatar4, avatar);
263 EXPECT_EQ(1u, change_processor()->changes().size());
265 SyncChange change = change_processor()->GetChange(kUserId1);
266 ASSERT_TRUE(change.IsValid());
267 EXPECT_EQ(SyncChange::ACTION_ADD, change.change_type());
268 const ManagedUserSpecifics& managed_user =
269 change.sync_data().GetSpecifics().managed_user();
270 EXPECT_EQ(kName1, managed_user.name());
271 EXPECT_FALSE(managed_user.acknowledged());
272 EXPECT_EQ(kAvatar1, managed_user.chrome_avatar());
276 TEST_F(ManagedUserSyncServiceTest, GetAvatarIndex) {
277 int avatar = 100;
278 EXPECT_TRUE(ManagedUserSyncService::GetAvatarIndex(std::string(), &avatar));
279 EXPECT_EQ(ManagedUserSyncService::kNoAvatar, avatar);
281 std::string avatar_str = ManagedUserSyncService::BuildAvatarString(24);
282 #if defined(OS_CHROMEOS)
283 EXPECT_EQ("chromeos-avatar-index:24", avatar_str);
284 #else
285 EXPECT_EQ("chrome-avatar-index:24", avatar_str);
286 #endif
287 EXPECT_TRUE(ManagedUserSyncService::GetAvatarIndex(avatar_str, &avatar));
288 EXPECT_EQ(24, avatar);
290 avatar_str = ManagedUserSyncService::BuildAvatarString(0);
291 #if defined(OS_CHROMEOS)
292 EXPECT_EQ("chromeos-avatar-index:0", avatar_str);
293 #else
294 EXPECT_EQ("chrome-avatar-index:0", avatar_str);
295 #endif
296 EXPECT_TRUE(ManagedUserSyncService::GetAvatarIndex(avatar_str, &avatar));
297 EXPECT_EQ(0, avatar);
299 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("wrong-prefix:5",
300 &avatar));
301 #if defined(OS_CHROMEOS)
302 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chromeos-avatar-indes:2",
303 &avatar));
305 EXPECT_FALSE(
306 ManagedUserSyncService::GetAvatarIndex("chromeos-avatar-indexxx:2",
307 &avatar));
309 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chromeos-avatar-index:",
310 &avatar));
312 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chromeos-avatar-index:x",
313 &avatar));
315 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chrome-avatar-index:5",
316 &avatar));
317 #else
318 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chrome-avatar-indes:2",
319 &avatar));
321 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chrome-avatar-indexxx:2",
322 &avatar));
324 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chrome-avatar-index:",
325 &avatar));
327 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chrome-avatar-index:x",
328 &avatar));
330 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chromeos-avatar-index:5",
331 &avatar));
332 #endif