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.
8 #include "base/prefs/scoped_user_pref_update.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/threading/sequenced_worker_pool.h"
12 #include "chrome/browser/managed_mode/managed_user_sync_service.h"
13 #include "chrome/browser/managed_mode/managed_user_sync_service_factory.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/testing_profile.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 #if defined(OS_CHROMEOS)
22 #include "chrome/browser/chromeos/login/default_user_images.h"
25 using sync_pb::ManagedUserSpecifics
;
26 using syncer::MANAGED_USERS
;
27 using syncer::SyncChange
;
28 using syncer::SyncChangeList
;
29 using syncer::SyncChangeProcessor
;
30 using syncer::SyncData
;
31 using syncer::SyncDataList
;
32 using syncer::SyncError
;
33 using syncer::SyncErrorFactory
;
34 using syncer::SyncMergeResult
;
38 class MockChangeProcessor
: public SyncChangeProcessor
{
40 MockChangeProcessor() {}
41 virtual ~MockChangeProcessor() {}
43 // SyncChangeProcessor implementation:
44 virtual SyncError
ProcessSyncChanges(
45 const tracked_objects::Location
& from_here
,
46 const SyncChangeList
& change_list
) OVERRIDE
;
48 virtual SyncDataList
GetAllSyncData(syncer::ModelType type
) const
50 return SyncDataList();
53 const SyncChangeList
& changes() const { return change_list_
; }
54 SyncChange
GetChange(const std::string
& id
) const;
57 SyncChangeList change_list_
;
60 SyncError
MockChangeProcessor::ProcessSyncChanges(
61 const tracked_objects::Location
& from_here
,
62 const SyncChangeList
& change_list
) {
63 change_list_
= change_list
;
67 SyncChange
MockChangeProcessor::GetChange(const std::string
& id
) const {
68 for (SyncChangeList::const_iterator it
= change_list_
.begin();
69 it
!= change_list_
.end(); ++it
) {
70 if (it
->sync_data().GetSpecifics().managed_user().id() == id
)
76 // Callback for ManagedUserSyncService::GetManagedUsersAsync().
77 void GetManagedUsersCallback(const base::DictionaryValue
** dict
,
78 const base::DictionaryValue
* managed_users
) {
79 *dict
= managed_users
;
84 class ManagedUserSyncServiceTest
: public ::testing::Test
{
86 ManagedUserSyncServiceTest();
87 virtual ~ManagedUserSyncServiceTest();
90 scoped_ptr
<SyncChangeProcessor
> CreateChangeProcessor();
91 scoped_ptr
<SyncErrorFactory
> CreateErrorFactory();
92 SyncData
CreateRemoteData(const std::string
& id
,
93 const std::string
& name
,
94 const std::string
& avatar
);
96 PrefService
* prefs() { return profile_
.GetPrefs(); }
97 ManagedUserSyncService
* service() { return service_
; }
98 MockChangeProcessor
* change_processor() { return change_processor_
; }
101 TestingProfile profile_
;
102 ManagedUserSyncService
* service_
;
104 // Owned by the ManagedUserSyncService.
105 MockChangeProcessor
* change_processor_
;
107 // A unique ID for creating "remote" Sync data.
111 ManagedUserSyncServiceTest::ManagedUserSyncServiceTest()
112 : change_processor_(NULL
),
114 service_
= ManagedUserSyncServiceFactory::GetForProfile(&profile_
);
117 ManagedUserSyncServiceTest::~ManagedUserSyncServiceTest() {}
119 scoped_ptr
<SyncChangeProcessor
>
120 ManagedUserSyncServiceTest::CreateChangeProcessor() {
121 EXPECT_FALSE(change_processor_
);
122 change_processor_
= new MockChangeProcessor();
123 return scoped_ptr
<SyncChangeProcessor
>(change_processor_
);
126 scoped_ptr
<SyncErrorFactory
>
127 ManagedUserSyncServiceTest::CreateErrorFactory() {
128 return scoped_ptr
<SyncErrorFactory
>(new syncer::SyncErrorFactoryMock());
131 SyncData
ManagedUserSyncServiceTest::CreateRemoteData(
132 const std::string
& id
,
133 const std::string
& name
,
134 const std::string
& chrome_avatar
) {
135 ::sync_pb::EntitySpecifics specifics
;
136 specifics
.mutable_managed_user()->set_id(id
);
137 specifics
.mutable_managed_user()->set_name(name
);
138 specifics
.mutable_managed_user()->set_acknowledged(true);
139 if (!chrome_avatar
.empty())
140 specifics
.mutable_managed_user()->set_chrome_avatar(chrome_avatar
);
142 return SyncData::CreateRemoteData(++sync_data_id_
, specifics
, base::Time());
145 TEST_F(ManagedUserSyncServiceTest
, MergeEmpty
) {
146 SyncMergeResult result
=
147 service()->MergeDataAndStartSyncing(MANAGED_USERS
,
149 CreateChangeProcessor(),
150 CreateErrorFactory());
151 EXPECT_FALSE(result
.error().IsSet());
152 EXPECT_EQ(0, result
.num_items_added());
153 EXPECT_EQ(0, result
.num_items_modified());
154 EXPECT_EQ(0, result
.num_items_deleted());
155 EXPECT_EQ(0, result
.num_items_before_association());
156 EXPECT_EQ(0, result
.num_items_after_association());
157 EXPECT_EQ(0u, service()->GetManagedUsers()->size());
158 EXPECT_EQ(0u, change_processor()->changes().size());
160 service()->StopSyncing(MANAGED_USERS
);
161 service()->Shutdown();
164 TEST_F(ManagedUserSyncServiceTest
, MergeExisting
) {
165 const char kNameKey
[] = "name";
166 const char kAcknowledgedKey
[] = "acknowledged";
167 const char kChromeAvatarKey
[] = "chromeAvatar";
169 const char kUserId1
[] = "aaaaa";
170 const char kUserId2
[] = "bbbbb";
171 const char kUserId3
[] = "ccccc";
172 const char kUserId4
[] = "ddddd";
173 const char kName1
[] = "Anchor";
174 const char kName2
[] = "Buzz";
175 const char kName3
[] = "Crush";
176 const char kName4
[] = "Dory";
177 const char kAvatar1
[] = "";
178 #if defined(OS_CHROMEOS)
179 const char kAvatar2
[] = "chromeos-avatar-index:0";
180 const char kAvatar3
[] = "chromeos-avatar-index:20";
182 const char kAvatar2
[] = "chrome-avatar-index:0";
183 const char kAvatar3
[] = "chrome-avatar-index:20";
185 const char kAvatar4
[] = "";
187 DictionaryPrefUpdate
update(prefs(), prefs::kManagedUsers
);
188 base::DictionaryValue
* managed_users
= update
.Get();
189 base::DictionaryValue
* dict
= new base::DictionaryValue
;
190 dict
->SetString(kNameKey
, kName1
);
191 managed_users
->Set(kUserId1
, dict
);
192 dict
= new base::DictionaryValue
;
193 dict
->SetString(kNameKey
, kName2
);
194 dict
->SetBoolean(kAcknowledgedKey
, true);
195 managed_users
->Set(kUserId2
, dict
);
198 const base::DictionaryValue
* async_managed_users
= NULL
;
199 service()->GetManagedUsersAsync(
200 base::Bind(&GetManagedUsersCallback
, &async_managed_users
));
202 SyncDataList initial_sync_data
;
203 initial_sync_data
.push_back(CreateRemoteData(kUserId2
, kName2
, kAvatar2
));
204 initial_sync_data
.push_back(CreateRemoteData(kUserId3
, kName3
, kAvatar3
));
205 initial_sync_data
.push_back(CreateRemoteData(kUserId4
, kName4
, kAvatar4
));
207 SyncMergeResult result
=
208 service()->MergeDataAndStartSyncing(MANAGED_USERS
,
210 CreateChangeProcessor(),
211 CreateErrorFactory());
212 EXPECT_FALSE(result
.error().IsSet());
213 EXPECT_EQ(2, result
.num_items_added());
214 EXPECT_EQ(1, result
.num_items_modified());
215 EXPECT_EQ(0, result
.num_items_deleted());
216 EXPECT_EQ(2, result
.num_items_before_association());
217 EXPECT_EQ(4, result
.num_items_after_association());
219 const base::DictionaryValue
* managed_users
= service()->GetManagedUsers();
220 EXPECT_EQ(4u, managed_users
->size());
221 EXPECT_TRUE(async_managed_users
);
222 EXPECT_TRUE(managed_users
->Equals(async_managed_users
));
225 const base::DictionaryValue
* managed_user
= NULL
;
226 ASSERT_TRUE(managed_users
->GetDictionary(kUserId2
, &managed_user
));
227 ASSERT_TRUE(managed_user
);
229 EXPECT_TRUE(managed_user
->GetString(kNameKey
, &name
));
230 EXPECT_EQ(kName2
, name
);
231 bool acknowledged
= false;
232 EXPECT_TRUE(managed_user
->GetBoolean(kAcknowledgedKey
, &acknowledged
));
233 EXPECT_TRUE(acknowledged
);
235 EXPECT_TRUE(managed_user
->GetString(kChromeAvatarKey
, &avatar
));
236 EXPECT_EQ(kAvatar2
, avatar
);
239 const base::DictionaryValue
* managed_user
= NULL
;
240 ASSERT_TRUE(managed_users
->GetDictionary(kUserId3
, &managed_user
));
241 ASSERT_TRUE(managed_user
);
243 EXPECT_TRUE(managed_user
->GetString(kNameKey
, &name
));
244 EXPECT_EQ(kName3
, name
);
245 bool acknowledged
= false;
246 EXPECT_TRUE(managed_user
->GetBoolean(kAcknowledgedKey
, &acknowledged
));
247 EXPECT_TRUE(acknowledged
);
249 EXPECT_TRUE(managed_user
->GetString(kChromeAvatarKey
, &avatar
));
250 EXPECT_EQ(kAvatar3
, avatar
);
253 const base::DictionaryValue
* managed_user
= NULL
;
254 ASSERT_TRUE(managed_users
->GetDictionary(kUserId4
, &managed_user
));
255 ASSERT_TRUE(managed_user
);
257 EXPECT_TRUE(managed_user
->GetString(kNameKey
, &name
));
258 EXPECT_EQ(kName4
, name
);
259 bool acknowledged
= false;
260 EXPECT_TRUE(managed_user
->GetBoolean(kAcknowledgedKey
, &acknowledged
));
261 EXPECT_TRUE(acknowledged
);
263 EXPECT_TRUE(managed_user
->GetString(kChromeAvatarKey
, &avatar
));
264 EXPECT_EQ(kAvatar4
, avatar
);
267 EXPECT_EQ(1u, change_processor()->changes().size());
269 SyncChange change
= change_processor()->GetChange(kUserId1
);
270 ASSERT_TRUE(change
.IsValid());
271 EXPECT_EQ(SyncChange::ACTION_ADD
, change
.change_type());
272 const ManagedUserSpecifics
& managed_user
=
273 change
.sync_data().GetSpecifics().managed_user();
274 EXPECT_EQ(kName1
, managed_user
.name());
275 EXPECT_FALSE(managed_user
.acknowledged());
276 EXPECT_EQ(kAvatar1
, managed_user
.chrome_avatar());
280 TEST_F(ManagedUserSyncServiceTest
, GetAvatarIndex
) {
282 EXPECT_TRUE(ManagedUserSyncService::GetAvatarIndex(std::string(), &avatar
));
283 EXPECT_EQ(ManagedUserSyncService::kNoAvatar
, avatar
);
285 int avatar_index
= 4;
286 #if defined(OS_CHROMEOS)
287 avatar_index
+= chromeos::kFirstDefaultImageIndex
;
289 std::string avatar_str
=
290 ManagedUserSyncService::BuildAvatarString(avatar_index
);
291 #if defined(OS_CHROMEOS)
292 EXPECT_EQ(base::StringPrintf("chromeos-avatar-index:%d", avatar_index
),
295 EXPECT_EQ(base::StringPrintf("chrome-avatar-index:%d", avatar_index
),
298 EXPECT_TRUE(ManagedUserSyncService::GetAvatarIndex(avatar_str
, &avatar
));
299 EXPECT_EQ(avatar_index
, avatar
);
302 #if defined(OS_CHROMEOS)
303 avatar_index
+= chromeos::kFirstDefaultImageIndex
;
305 avatar_str
= ManagedUserSyncService::BuildAvatarString(avatar_index
);
306 #if defined(OS_CHROMEOS)
307 EXPECT_EQ(base::StringPrintf("chromeos-avatar-index:%d", avatar_index
),
310 EXPECT_EQ(base::StringPrintf("chrome-avatar-index:%d", avatar_index
),
313 EXPECT_TRUE(ManagedUserSyncService::GetAvatarIndex(avatar_str
, &avatar
));
314 EXPECT_EQ(avatar_index
, avatar
);
316 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("wrong-prefix:5",
318 #if defined(OS_CHROMEOS)
319 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chromeos-avatar-indes:2",
323 ManagedUserSyncService::GetAvatarIndex("chromeos-avatar-indexxx:2",
326 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chromeos-avatar-index:",
329 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chromeos-avatar-index:x",
332 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chrome-avatar-index:5",
335 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chrome-avatar-indes:2",
338 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chrome-avatar-indexxx:2",
341 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chrome-avatar-index:",
344 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chrome-avatar-index:x",
347 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chromeos-avatar-index:5",