Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / supervised_user / supervised_user_sync_service_unittest.cc
blob1b5f2a71521081186fed8040caad129a10deaf41
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/message_loop/message_loop.h"
9 #include "base/prefs/scoped_user_pref_update.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "chrome/browser/supervised_user/supervised_user_sync_service.h"
14 #include "chrome/browser/supervised_user/supervised_user_sync_service_factory.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "sync/api/attachments/attachment_id.h"
18 #include "sync/api/sync_change.h"
19 #include "sync/api/sync_error_factory_mock.h"
20 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test.h"
21 #include "sync/protocol/sync.pb.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 #if defined(OS_CHROMEOS)
25 #include "components/user_manager/user_image/default_user_images.h"
26 #endif
28 using sync_pb::ManagedUserSpecifics;
29 using syncer::SUPERVISED_USERS;
30 using syncer::SyncChange;
31 using syncer::SyncChangeList;
32 using syncer::SyncChangeProcessor;
33 using syncer::SyncData;
34 using syncer::SyncDataList;
35 using syncer::SyncError;
36 using syncer::SyncErrorFactory;
37 using syncer::SyncMergeResult;
39 namespace {
41 class MockChangeProcessor : public SyncChangeProcessor {
42 public:
43 MockChangeProcessor() {}
44 virtual ~MockChangeProcessor() {}
46 // SyncChangeProcessor implementation:
47 virtual SyncError ProcessSyncChanges(
48 const tracked_objects::Location& from_here,
49 const SyncChangeList& change_list) override;
51 virtual SyncDataList GetAllSyncData(syncer::ModelType type) const
52 override {
53 return SyncDataList();
56 const SyncChangeList& changes() const { return change_list_; }
57 SyncChange GetChange(const std::string& id) const;
59 private:
60 SyncChangeList change_list_;
63 SyncError MockChangeProcessor::ProcessSyncChanges(
64 const tracked_objects::Location& from_here,
65 const SyncChangeList& change_list) {
66 change_list_ = change_list;
67 return SyncError();
70 SyncChange MockChangeProcessor::GetChange(const std::string& id) const {
71 for (const SyncChange& sync_change : change_list_) {
72 if (sync_change.sync_data().GetSpecifics().managed_user().id() == id)
73 return sync_change;
75 return SyncChange();
78 // Callback for SupervisedUserSyncService::GetSupervisedUsersAsync().
79 void GetSupervisedUsersCallback(const base::DictionaryValue** dict,
80 const base::DictionaryValue* supervised_users) {
81 *dict = supervised_users;
84 } // namespace
86 class SupervisedUserSyncServiceTest : public ::testing::Test {
87 public:
88 SupervisedUserSyncServiceTest();
89 virtual ~SupervisedUserSyncServiceTest();
91 protected:
92 scoped_ptr<SyncChangeProcessor> CreateChangeProcessor();
93 scoped_ptr<SyncErrorFactory> CreateErrorFactory();
94 SyncData CreateRemoteData(const std::string& id,
95 const std::string& name,
96 const std::string& avatar);
98 PrefService* prefs() { return profile_.GetPrefs(); }
99 SupervisedUserSyncService* service() { return service_; }
100 MockChangeProcessor* change_processor() { return change_processor_; }
102 private:
103 base::MessageLoop message_loop;
104 TestingProfile profile_;
105 SupervisedUserSyncService* service_;
107 // Owned by the SupervisedUserSyncService.
108 MockChangeProcessor* change_processor_;
110 // A unique ID for creating "remote" Sync data.
111 int64 sync_data_id_;
114 SupervisedUserSyncServiceTest::SupervisedUserSyncServiceTest()
115 : change_processor_(NULL),
116 sync_data_id_(0) {
117 service_ = SupervisedUserSyncServiceFactory::GetForProfile(&profile_);
120 SupervisedUserSyncServiceTest::~SupervisedUserSyncServiceTest() {}
122 scoped_ptr<SyncChangeProcessor>
123 SupervisedUserSyncServiceTest::CreateChangeProcessor() {
124 EXPECT_FALSE(change_processor_);
125 change_processor_ = new MockChangeProcessor();
126 return scoped_ptr<SyncChangeProcessor>(change_processor_);
129 scoped_ptr<SyncErrorFactory>
130 SupervisedUserSyncServiceTest::CreateErrorFactory() {
131 return scoped_ptr<SyncErrorFactory>(new syncer::SyncErrorFactoryMock());
134 SyncData SupervisedUserSyncServiceTest::CreateRemoteData(
135 const std::string& id,
136 const std::string& name,
137 const std::string& chrome_avatar) {
138 ::sync_pb::EntitySpecifics specifics;
139 specifics.mutable_managed_user()->set_id(id);
140 specifics.mutable_managed_user()->set_name(name);
141 specifics.mutable_managed_user()->set_acknowledged(true);
142 if (!chrome_avatar.empty())
143 specifics.mutable_managed_user()->set_chrome_avatar(chrome_avatar);
145 return SyncData::CreateRemoteData(
146 ++sync_data_id_,
147 specifics,
148 base::Time(),
149 syncer::AttachmentIdList(),
150 syncer::AttachmentServiceProxyForTest::Create());
153 TEST_F(SupervisedUserSyncServiceTest, MergeEmpty) {
154 SyncMergeResult result =
155 service()->MergeDataAndStartSyncing(SUPERVISED_USERS,
156 SyncDataList(),
157 CreateChangeProcessor(),
158 CreateErrorFactory());
159 EXPECT_FALSE(result.error().IsSet());
160 EXPECT_EQ(0, result.num_items_added());
161 EXPECT_EQ(0, result.num_items_modified());
162 EXPECT_EQ(0, result.num_items_deleted());
163 EXPECT_EQ(0, result.num_items_before_association());
164 EXPECT_EQ(0, result.num_items_after_association());
165 EXPECT_EQ(0u, service()->GetSupervisedUsers()->size());
166 EXPECT_EQ(0u, change_processor()->changes().size());
168 service()->StopSyncing(SUPERVISED_USERS);
169 service()->Shutdown();
172 TEST_F(SupervisedUserSyncServiceTest, MergeExisting) {
173 const char kNameKey[] = "name";
174 const char kAcknowledgedKey[] = "acknowledged";
175 const char kChromeAvatarKey[] = "chromeAvatar";
177 const char kUserId1[] = "aaaaa";
178 const char kUserId2[] = "bbbbb";
179 const char kUserId3[] = "ccccc";
180 const char kUserId4[] = "ddddd";
181 const char kName1[] = "Anchor";
182 const char kName2[] = "Buzz";
183 const char kName3[] = "Crush";
184 const char kName4[] = "Dory";
185 const char kAvatar1[] = "";
186 #if defined(OS_CHROMEOS)
187 const char kAvatar2[] = "chromeos-avatar-index:0";
188 const char kAvatar3[] = "chromeos-avatar-index:20";
189 #else
190 const char kAvatar2[] = "chrome-avatar-index:0";
191 const char kAvatar3[] = "chrome-avatar-index:20";
192 #endif
193 const char kAvatar4[] = "";
195 DictionaryPrefUpdate update(prefs(), prefs::kSupervisedUsers);
196 base::DictionaryValue* supervised_users = update.Get();
197 base::DictionaryValue* dict = new base::DictionaryValue;
198 dict->SetString(kNameKey, kName1);
199 supervised_users->Set(kUserId1, dict);
200 dict = new base::DictionaryValue;
201 dict->SetString(kNameKey, kName2);
202 dict->SetBoolean(kAcknowledgedKey, true);
203 supervised_users->Set(kUserId2, dict);
206 const base::DictionaryValue* async_supervised_users = NULL;
207 service()->GetSupervisedUsersAsync(
208 base::Bind(&GetSupervisedUsersCallback, &async_supervised_users));
210 SyncDataList initial_sync_data;
211 initial_sync_data.push_back(CreateRemoteData(kUserId2, kName2, kAvatar2));
212 initial_sync_data.push_back(CreateRemoteData(kUserId3, kName3, kAvatar3));
213 initial_sync_data.push_back(CreateRemoteData(kUserId4, kName4, kAvatar4));
215 SyncMergeResult result =
216 service()->MergeDataAndStartSyncing(SUPERVISED_USERS,
217 initial_sync_data,
218 CreateChangeProcessor(),
219 CreateErrorFactory());
220 EXPECT_FALSE(result.error().IsSet());
221 EXPECT_EQ(2, result.num_items_added());
222 EXPECT_EQ(1, result.num_items_modified());
223 EXPECT_EQ(0, result.num_items_deleted());
224 EXPECT_EQ(2, result.num_items_before_association());
225 EXPECT_EQ(4, result.num_items_after_association());
227 const base::DictionaryValue* supervised_users =
228 service()->GetSupervisedUsers();
229 EXPECT_EQ(4u, supervised_users->size());
230 EXPECT_TRUE(async_supervised_users);
231 EXPECT_TRUE(supervised_users->Equals(async_supervised_users));
234 const base::DictionaryValue* supervised_user = NULL;
235 ASSERT_TRUE(supervised_users->GetDictionary(kUserId2, &supervised_user));
236 ASSERT_TRUE(supervised_user);
237 std::string name;
238 EXPECT_TRUE(supervised_user->GetString(kNameKey, &name));
239 EXPECT_EQ(kName2, name);
240 bool acknowledged = false;
241 EXPECT_TRUE(supervised_user->GetBoolean(kAcknowledgedKey, &acknowledged));
242 EXPECT_TRUE(acknowledged);
243 std::string avatar;
244 EXPECT_TRUE(supervised_user->GetString(kChromeAvatarKey, &avatar));
245 EXPECT_EQ(kAvatar2, avatar);
248 const base::DictionaryValue* supervised_user = NULL;
249 ASSERT_TRUE(supervised_users->GetDictionary(kUserId3, &supervised_user));
250 ASSERT_TRUE(supervised_user);
251 std::string name;
252 EXPECT_TRUE(supervised_user->GetString(kNameKey, &name));
253 EXPECT_EQ(kName3, name);
254 bool acknowledged = false;
255 EXPECT_TRUE(supervised_user->GetBoolean(kAcknowledgedKey, &acknowledged));
256 EXPECT_TRUE(acknowledged);
257 std::string avatar;
258 EXPECT_TRUE(supervised_user->GetString(kChromeAvatarKey, &avatar));
259 EXPECT_EQ(kAvatar3, avatar);
262 const base::DictionaryValue* supervised_user = NULL;
263 ASSERT_TRUE(supervised_users->GetDictionary(kUserId4, &supervised_user));
264 ASSERT_TRUE(supervised_user);
265 std::string name;
266 EXPECT_TRUE(supervised_user->GetString(kNameKey, &name));
267 EXPECT_EQ(kName4, name);
268 bool acknowledged = false;
269 EXPECT_TRUE(supervised_user->GetBoolean(kAcknowledgedKey, &acknowledged));
270 EXPECT_TRUE(acknowledged);
271 std::string avatar;
272 EXPECT_TRUE(supervised_user->GetString(kChromeAvatarKey, &avatar));
273 EXPECT_EQ(kAvatar4, avatar);
276 EXPECT_EQ(1u, change_processor()->changes().size());
278 SyncChange change = change_processor()->GetChange(kUserId1);
279 ASSERT_TRUE(change.IsValid());
280 EXPECT_EQ(SyncChange::ACTION_ADD, change.change_type());
281 const ManagedUserSpecifics& supervised_user =
282 change.sync_data().GetSpecifics().managed_user();
283 EXPECT_EQ(kName1, supervised_user.name());
284 EXPECT_FALSE(supervised_user.acknowledged());
285 EXPECT_EQ(kAvatar1, supervised_user.chrome_avatar());
289 TEST_F(SupervisedUserSyncServiceTest, GetAvatarIndex) {
290 int avatar = 100;
291 EXPECT_TRUE(SupervisedUserSyncService::GetAvatarIndex(std::string(),
292 &avatar));
293 EXPECT_EQ(SupervisedUserSyncService::kNoAvatar, avatar);
295 int avatar_index = 4;
296 #if defined(OS_CHROMEOS)
297 avatar_index += user_manager::kFirstDefaultImageIndex;
298 #endif
299 std::string avatar_str =
300 SupervisedUserSyncService::BuildAvatarString(avatar_index);
301 #if defined(OS_CHROMEOS)
302 EXPECT_EQ(base::StringPrintf("chromeos-avatar-index:%d", avatar_index),
303 avatar_str);
304 #else
305 EXPECT_EQ(base::StringPrintf("chrome-avatar-index:%d", avatar_index),
306 avatar_str);
307 #endif
308 EXPECT_TRUE(SupervisedUserSyncService::GetAvatarIndex(avatar_str, &avatar));
309 EXPECT_EQ(avatar_index, avatar);
311 avatar_index = 0;
312 #if defined(OS_CHROMEOS)
313 avatar_index += user_manager::kFirstDefaultImageIndex;
314 #endif
315 avatar_str = SupervisedUserSyncService::BuildAvatarString(avatar_index);
316 #if defined(OS_CHROMEOS)
317 EXPECT_EQ(base::StringPrintf("chromeos-avatar-index:%d", avatar_index),
318 avatar_str);
319 #else
320 EXPECT_EQ(base::StringPrintf("chrome-avatar-index:%d", avatar_index),
321 avatar_str);
322 #endif
323 EXPECT_TRUE(SupervisedUserSyncService::GetAvatarIndex(avatar_str, &avatar));
324 EXPECT_EQ(avatar_index, avatar);
326 EXPECT_FALSE(SupervisedUserSyncService::GetAvatarIndex("wrong-prefix:5",
327 &avatar));
328 #if defined(OS_CHROMEOS)
329 EXPECT_FALSE(SupervisedUserSyncService::GetAvatarIndex(
330 "chromeos-avatar-indes:2",
331 &avatar));
333 EXPECT_FALSE(
334 SupervisedUserSyncService::GetAvatarIndex("chromeos-avatar-indexxx:2",
335 &avatar));
337 EXPECT_FALSE(SupervisedUserSyncService::GetAvatarIndex(
338 "chromeos-avatar-index:",
339 &avatar));
341 EXPECT_FALSE(SupervisedUserSyncService::GetAvatarIndex(
342 "chromeos-avatar-index:x",
343 &avatar));
345 EXPECT_FALSE(SupervisedUserSyncService::GetAvatarIndex(
346 "chrome-avatar-index:5",
347 &avatar));
348 #else
349 EXPECT_FALSE(SupervisedUserSyncService::GetAvatarIndex(
350 "chrome-avatar-indes:2",
351 &avatar));
353 EXPECT_FALSE(SupervisedUserSyncService::GetAvatarIndex(
354 "chrome-avatar-indexxx:2",
355 &avatar));
357 EXPECT_FALSE(SupervisedUserSyncService::GetAvatarIndex(
358 "chrome-avatar-index:",
359 &avatar));
361 EXPECT_FALSE(SupervisedUserSyncService::GetAvatarIndex(
362 "chrome-avatar-index:x",
363 &avatar));
365 EXPECT_FALSE(SupervisedUserSyncService::GetAvatarIndex(
366 "chromeos-avatar-index:5",
367 &avatar));
368 #endif