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/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
;
34 class MockChangeProcessor
: public SyncChangeProcessor
{
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
46 return SyncDataList();
49 const SyncChangeList
& changes() const { return change_list_
; }
50 SyncChange
GetChange(const std::string
& id
) const;
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
;
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
)
72 // Callback for ManagedUserSyncService::GetManagedUsersAsync().
73 void GetManagedUsersCallback(const base::DictionaryValue
** dict
,
74 const base::DictionaryValue
* managed_users
) {
75 *dict
= managed_users
;
80 class ManagedUserSyncServiceTest
: public ::testing::Test
{
82 ManagedUserSyncServiceTest();
83 virtual ~ManagedUserSyncServiceTest();
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_
; }
97 TestingProfile profile_
;
98 ManagedUserSyncService
* service_
;
100 // Owned by the ManagedUserSyncService.
101 MockChangeProcessor
* change_processor_
;
103 // A unique ID for creating "remote" Sync data.
107 ManagedUserSyncServiceTest::ManagedUserSyncServiceTest()
108 : change_processor_(NULL
),
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
,
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";
178 const char kAvatar2
[] = "chrome-avatar-index:0";
179 const char kAvatar3
[] = "chrome-avatar-index:20";
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
,
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
);
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
);
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
);
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
);
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
);
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
);
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
) {
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
);
285 EXPECT_EQ("chrome-avatar-index:24", avatar_str
);
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
);
294 EXPECT_EQ("chrome-avatar-index:0", avatar_str
);
296 EXPECT_TRUE(ManagedUserSyncService::GetAvatarIndex(avatar_str
, &avatar
));
297 EXPECT_EQ(0, avatar
);
299 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("wrong-prefix:5",
301 #if defined(OS_CHROMEOS)
302 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chromeos-avatar-indes:2",
306 ManagedUserSyncService::GetAvatarIndex("chromeos-avatar-indexxx:2",
309 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chromeos-avatar-index:",
312 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chromeos-avatar-index:x",
315 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chrome-avatar-index:5",
318 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chrome-avatar-indes:2",
321 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chrome-avatar-indexxx:2",
324 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chrome-avatar-index:",
327 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chrome-avatar-index:x",
330 EXPECT_FALSE(ManagedUserSyncService::GetAvatarIndex("chromeos-avatar-index:5",