Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / profiles / profile_info_cache_unittest.cc
blobd3e6d474ce679c59650fa5d027dbdb9dda37689d
1 // Copyright (c) 2012 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 "chrome/browser/profiles/profile_info_cache_unittest.h"
7 #include <vector>
9 #include "base/command_line.h"
10 #include "base/files/file_util.h"
11 #include "base/prefs/testing_pref_service.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/prefs/pref_service_syncable.h"
18 #include "chrome/browser/profiles/profile_avatar_downloader.h"
19 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
20 #include "chrome/browser/profiles/profile_info_cache.h"
21 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/test/base/testing_browser_process.h"
25 #include "components/signin/core/common/profile_management_switches.h"
26 #include "content/public/browser/notification_observer.h"
27 #include "content/public/browser/notification_registrar.h"
28 #include "content/public/browser/notification_service.h"
29 #include "content/public/test/test_browser_thread_bundle.h"
30 #include "content/public/test/test_utils.h"
31 #include "third_party/skia/include/core/SkBitmap.h"
32 #include "ui/base/resource/resource_bundle.h"
33 #include "ui/gfx/image/image.h"
34 #include "ui/gfx/image/image_unittest_util.h"
36 using base::ASCIIToUTF16;
37 using base::UTF8ToUTF16;
38 using content::BrowserThread;
40 ProfileNameVerifierObserver::ProfileNameVerifierObserver(
41 TestingProfileManager* testing_profile_manager)
42 : testing_profile_manager_(testing_profile_manager) {
43 DCHECK(testing_profile_manager_);
46 ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
49 void ProfileNameVerifierObserver::OnProfileAdded(
50 const base::FilePath& profile_path) {
51 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
52 GetCache()->GetIndexOfProfileWithPath(profile_path));
53 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
54 profile_names_.insert(profile_name);
57 void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
58 const base::FilePath& profile_path) {
59 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
60 GetCache()->GetIndexOfProfileWithPath(profile_path));
61 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
62 profile_names_.erase(profile_name);
65 void ProfileNameVerifierObserver::OnProfileWasRemoved(
66 const base::FilePath& profile_path,
67 const base::string16& profile_name) {
68 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
71 void ProfileNameVerifierObserver::OnProfileNameChanged(
72 const base::FilePath& profile_path,
73 const base::string16& old_profile_name) {
74 base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
75 GetCache()->GetIndexOfProfileWithPath(profile_path));
76 EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end());
77 EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end());
78 profile_names_.erase(old_profile_name);
79 profile_names_.insert(new_profile_name);
82 void ProfileNameVerifierObserver::OnProfileAvatarChanged(
83 const base::FilePath& profile_path) {
84 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
85 GetCache()->GetIndexOfProfileWithPath(profile_path));
86 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
89 ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
90 return testing_profile_manager_->profile_info_cache();
93 ProfileInfoCacheTest::ProfileInfoCacheTest()
94 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
95 name_observer_(&testing_profile_manager_) {
98 ProfileInfoCacheTest::~ProfileInfoCacheTest() {
101 void ProfileInfoCacheTest::SetUp() {
102 ASSERT_TRUE(testing_profile_manager_.SetUp());
103 testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_);
106 void ProfileInfoCacheTest::TearDown() {
107 // Drain the UI thread to make sure all tasks are completed. This prevents
108 // memory leaks.
109 base::RunLoop().RunUntilIdle();
112 ProfileInfoCache* ProfileInfoCacheTest::GetCache() {
113 return testing_profile_manager_.profile_info_cache();
116 base::FilePath ProfileInfoCacheTest::GetProfilePath(
117 const std::string& base_name) {
118 return testing_profile_manager_.profile_manager()->user_data_dir().
119 AppendASCII(base_name);
122 void ProfileInfoCacheTest::ResetCache() {
123 testing_profile_manager_.DeleteProfileInfoCache();
126 TEST_F(ProfileInfoCacheTest, AddProfiles) {
127 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
129 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
130 for (uint32 i = 0; i < 4; ++i) {
131 base::FilePath profile_path =
132 GetProfilePath(base::StringPrintf("path_%ud", i));
133 base::string16 profile_name =
134 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
135 const SkBitmap* icon = rb.GetImageNamed(
136 profiles::GetDefaultAvatarIconResourceIDAtIndex(
137 i)).ToSkBitmap();
138 std::string supervised_user_id = i == 3 ? "TEST_ID" : "";
140 GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(),
141 i, supervised_user_id);
142 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
143 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
144 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
146 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
147 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
148 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
149 const SkBitmap* actual_icon =
150 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
151 EXPECT_EQ(icon->width(), actual_icon->width());
152 EXPECT_EQ(icon->height(), actual_icon->height());
153 EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i));
154 EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i));
155 EXPECT_EQ(supervised_user_id,
156 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
159 // Reset the cache and test the it reloads correctly.
160 ResetCache();
162 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
163 for (uint32 i = 0; i < 4; ++i) {
164 base::FilePath profile_path =
165 GetProfilePath(base::StringPrintf("path_%ud", i));
166 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
167 base::string16 profile_name =
168 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
169 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
170 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
171 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
172 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
173 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
177 TEST_F(ProfileInfoCacheTest, DeleteProfile) {
178 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
180 base::FilePath path_1 = GetProfilePath("path_1");
181 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
182 base::string16(), 0, std::string());
183 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
185 base::FilePath path_2 = GetProfilePath("path_2");
186 base::string16 name_2 = ASCIIToUTF16("name_2");
187 GetCache()->AddProfileToCache(path_2, name_2, base::string16(), 0,
188 std::string());
189 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
191 GetCache()->DeleteProfileFromCache(path_1);
192 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
193 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
195 GetCache()->DeleteProfileFromCache(path_2);
196 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
199 TEST_F(ProfileInfoCacheTest, MutateProfile) {
200 GetCache()->AddProfileToCache(
201 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
202 base::string16(), 0, std::string());
203 GetCache()->AddProfileToCache(
204 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
205 base::string16(), 0, std::string());
207 base::string16 new_name = ASCIIToUTF16("new_name");
208 GetCache()->SetNameOfProfileAtIndex(1, new_name);
209 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
210 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
212 base::string16 new_user_name = ASCIIToUTF16("user_name");
213 GetCache()->SetUserNameOfProfileAtIndex(1, new_user_name);
214 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
215 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
217 size_t new_icon_index = 3;
218 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
219 // Not much to test.
220 GetCache()->GetAvatarIconOfProfileAtIndex(1);
223 TEST_F(ProfileInfoCacheTest, Sort) {
224 base::string16 name_a = ASCIIToUTF16("apple");
225 GetCache()->AddProfileToCache(
226 GetProfilePath("path_a"), name_a, base::string16(), 0, std::string());
228 base::string16 name_c = ASCIIToUTF16("cat");
229 GetCache()->AddProfileToCache(
230 GetProfilePath("path_c"), name_c, base::string16(), 0, std::string());
232 // Sanity check the initial order.
233 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
234 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
236 // Add a new profile (start with a capital to test case insensitive sorting.
237 base::string16 name_b = ASCIIToUTF16("Banana");
238 GetCache()->AddProfileToCache(
239 GetProfilePath("path_b"), name_b, base::string16(), 0, std::string());
241 // Verify the new order.
242 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
243 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
244 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
246 // Change the name of an existing profile.
247 name_a = UTF8ToUTF16("dog");
248 GetCache()->SetNameOfProfileAtIndex(0, name_a);
250 // Verify the new order.
251 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
252 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
253 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
255 // Delete a profile.
256 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
258 // Verify the new order.
259 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
260 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
263 TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
264 GetCache()->AddProfileToCache(
265 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
266 base::string16(), 0, std::string());
267 GetCache()->AddProfileToCache(
268 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
269 base::string16(), 0, std::string());
271 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
272 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
274 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
276 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
277 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
279 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
281 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
282 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
284 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
286 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
287 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
290 TEST_F(ProfileInfoCacheTest, ProfileActiveTime) {
291 GetCache()->AddProfileToCache(
292 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
293 base::string16(), 0, std::string());
294 EXPECT_EQ(base::Time(), GetCache()->GetProfileActiveTimeAtIndex(0));
295 // Before & After times are artificially shifted because just relying upon
296 // the system time can yield problems due to inaccuracies in the
297 // underlying storage system (which uses a double with only 52 bits of
298 // precision to store the 64-bit "time" number). http://crbug.com/346827
299 base::Time before = base::Time::Now();
300 before -= base::TimeDelta::FromSeconds(1);
301 GetCache()->SetProfileActiveTimeAtIndex(0);
302 base::Time after = base::Time::Now();
303 after += base::TimeDelta::FromSeconds(1);
304 EXPECT_LE(before, GetCache()->GetProfileActiveTimeAtIndex(0));
305 EXPECT_GE(after, GetCache()->GetProfileActiveTimeAtIndex(0));
308 TEST_F(ProfileInfoCacheTest, GAIAName) {
309 GetCache()->AddProfileToCache(
310 GetProfilePath("path_1"), ASCIIToUTF16("Person 1"),
311 base::string16(), 0, std::string());
312 base::string16 profile_name(ASCIIToUTF16("Person 2"));
313 GetCache()->AddProfileToCache(
314 GetProfilePath("path_2"), profile_name, base::string16(), 0,
315 std::string());
317 int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
318 int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
320 // Sanity check.
321 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
322 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty());
324 // Set GAIA name. This re-sorts the cache.
325 base::string16 gaia_name(ASCIIToUTF16("Pat Smith"));
326 GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name);
327 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
328 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
330 // Since there is a GAIA name, we use that as a display name.
331 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
332 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
333 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2));
335 // Don't use GAIA name as profile name. This re-sorts the cache.
336 base::string16 custom_name(ASCIIToUTF16("Custom name"));
337 GetCache()->SetNameOfProfileAtIndex(index2, custom_name);
338 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index2, false);
340 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
341 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
343 EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2));
344 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
347 TEST_F(ProfileInfoCacheTest, GAIAPicture) {
348 const int kDefaultAvatarIndex = 0;
349 const int kOtherAvatarIndex = 1;
350 const int kGaiaPictureSize = 256; // Standard size of a Gaia account picture.
351 GetCache()->AddProfileToCache(
352 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
353 base::string16(), kDefaultAvatarIndex, std::string());
354 GetCache()->AddProfileToCache(
355 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
356 base::string16(), kDefaultAvatarIndex, std::string());
358 // Sanity check.
359 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
360 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
361 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
362 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
364 // The profile icon should be the default one.
365 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(0));
366 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
367 int default_avatar_id =
368 profiles::GetDefaultAvatarIconResourceIDAtIndex(kDefaultAvatarIndex);
369 const gfx::Image& default_avatar_image(
370 ResourceBundle::GetSharedInstance().GetImageNamed(default_avatar_id));
371 EXPECT_TRUE(gfx::test::IsEqual(
372 default_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
374 // Set GAIA picture.
375 gfx::Image gaia_image(gfx::test::CreateImage(
376 kGaiaPictureSize, kGaiaPictureSize));
377 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
378 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
379 EXPECT_TRUE(gfx::test::IsEqual(
380 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
381 // Since we're still using the default avatar, the GAIA image should be
382 // preferred over the generic avatar image.
383 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
384 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
385 EXPECT_TRUE(gfx::test::IsEqual(
386 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
388 // Set a non-default avatar. This should be preferred over the GAIA image.
389 GetCache()->SetAvatarIconOfProfileAtIndex(1, kOtherAvatarIndex);
390 GetCache()->SetProfileIsUsingDefaultAvatarAtIndex(1, false);
391 EXPECT_FALSE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
392 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
393 int other_avatar_id =
394 profiles::GetDefaultAvatarIconResourceIDAtIndex(kOtherAvatarIndex);
395 const gfx::Image& other_avatar_image(
396 ResourceBundle::GetSharedInstance().GetImageNamed(other_avatar_id));
397 EXPECT_TRUE(gfx::test::IsEqual(
398 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
400 // Explicitly setting the GAIA picture should make it preferred again.
401 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
402 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
403 EXPECT_TRUE(gfx::test::IsEqual(
404 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
405 EXPECT_TRUE(gfx::test::IsEqual(
406 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
408 // Clearing the IsUsingGAIAPicture flag should result in the generic image
409 // being used again.
410 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
411 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
412 EXPECT_TRUE(gfx::test::IsEqual(
413 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
414 EXPECT_TRUE(gfx::test::IsEqual(
415 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
418 TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
419 GetCache()->AddProfileToCache(
420 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
421 base::string16(), 0, std::string());
422 gfx::Image gaia_image(gfx::test::CreateImage());
424 content::WindowedNotificationObserver save_observer(
425 chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED,
426 content::NotificationService::AllSources());
427 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
428 EXPECT_TRUE(gfx::test::IsEqual(
429 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
431 // Wait for the file to be written to disk then reset the cache.
432 save_observer.Wait();
433 ResetCache();
435 // Try to get the GAIA picture. This should return NULL until the read from
436 // disk is done.
437 content::WindowedNotificationObserver read_observer(
438 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
439 content::NotificationService::AllSources());
440 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
441 read_observer.Wait();
442 EXPECT_TRUE(gfx::test::IsEqual(
443 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
446 TEST_F(ProfileInfoCacheTest, SetSupervisedUserId) {
447 GetCache()->AddProfileToCache(
448 GetProfilePath("test"), ASCIIToUTF16("Test"),
449 base::string16(), 0, std::string());
450 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
452 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, "TEST_ID");
453 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
454 EXPECT_EQ("TEST_ID", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
456 ResetCache();
457 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
459 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, std::string());
460 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
461 EXPECT_EQ("", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
464 TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
465 base::string16 profile_name = ASCIIToUTF16("name_1");
466 int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0);
467 const gfx::Image& profile_image(
468 ResourceBundle::GetSharedInstance().GetImageNamed(id));
470 GetCache()->AddProfileToCache(
471 GetProfilePath("path_1"), profile_name, base::string16(), 0,
472 std::string());
474 // Set empty GAIA info.
475 GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16());
476 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
477 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
479 // Verify that the profile name and picture are not empty.
480 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
481 EXPECT_TRUE(gfx::test::IsEqual(
482 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
485 TEST_F(ProfileInfoCacheTest, CreateSupervisedTestingProfile) {
486 testing_profile_manager_.CreateTestingProfile("default");
487 base::string16 supervised_user_name = ASCIIToUTF16("Supervised User");
488 testing_profile_manager_.CreateTestingProfile(
489 "test1", scoped_ptr<PrefServiceSyncable>(),
490 supervised_user_name, 0, "TEST_ID", TestingProfile::TestingFactories());
491 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
492 bool is_supervised =
493 GetCache()->GetNameOfProfileAtIndex(i) == supervised_user_name;
494 EXPECT_EQ(is_supervised, GetCache()->ProfileIsSupervisedAtIndex(i));
495 std::string supervised_user_id = is_supervised ? "TEST_ID" : "";
496 EXPECT_EQ(supervised_user_id,
497 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
500 // Supervised profiles have a custom theme, which needs to be deleted on the
501 // FILE thread. Reset the profile manager now so everything is deleted while
502 // we still have a FILE thread.
503 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
506 TEST_F(ProfileInfoCacheTest, AddStubProfile) {
507 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
509 // Add some profiles with and without a '.' in their paths.
510 const struct {
511 const char* profile_path;
512 const char* profile_name;
513 } kTestCases[] = {
514 { "path.test0", "name_0" },
515 { "path_test1", "name_1" },
516 { "path.test2", "name_2" },
517 { "path_test3", "name_3" },
520 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
521 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
522 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
524 GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(),
525 i, "");
527 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
528 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
531 ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles());
533 // Check that the profiles can be extracted from the local state.
534 std::vector<base::string16> names;
535 PrefService* local_state = g_browser_process->local_state();
536 const base::DictionaryValue* cache = local_state->GetDictionary(
537 prefs::kProfileInfoCache);
538 base::string16 name;
539 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd();
540 it.Advance()) {
541 const base::DictionaryValue* info = NULL;
542 it.value().GetAsDictionary(&info);
543 info->GetString("name", &name);
544 names.push_back(name);
547 for (size_t i = 0; i < 4; i++)
548 ASSERT_FALSE(names[i].empty());
551 // High res avatar downloading is only supported on desktop.
552 #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
553 TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
554 switches::EnableNewAvatarMenuForTesting(
555 base::CommandLine::ForCurrentProcess());
557 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
558 base::FilePath path_1 = GetProfilePath("path_1");
559 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
560 base::string16(), 0, std::string());
561 EXPECT_EQ(1U, GetCache()->GetNumberOfProfiles());
562 base::RunLoop().RunUntilIdle();
564 // We haven't downloaded any high-res avatars yet.
565 EXPECT_EQ(0U, GetCache()->cached_avatar_images_.size());
567 // After adding a new profile, the download of high-res avatar will be
568 // triggered if the flag kNewAvatarMenu has been set. But the downloader
569 // won't ever call OnFetchComplete in the test.
570 EXPECT_EQ(1U, GetCache()->avatar_images_downloads_in_progress_.size());
572 EXPECT_FALSE(GetCache()->GetHighResAvatarOfProfileAtIndex(0));
574 // Simulate downloading a high-res avatar.
575 const size_t kIconIndex = 0;
576 ProfileAvatarDownloader avatar_downloader(
577 kIconIndex, GetCache()->GetPathOfProfileAtIndex(0), GetCache());
579 // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels.
580 SkBitmap bitmap;
581 bitmap.allocN32Pixels(2, 2);
582 bitmap.eraseColor(SK_ColorGREEN);
584 avatar_downloader.OnFetchComplete(
585 GURL("http://www.google.com/avatar.png"), &bitmap);
587 // Now the download should not be in progress anymore.
588 EXPECT_EQ(0U, GetCache()->avatar_images_downloads_in_progress_.size());
590 std::string file_name =
591 profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex);
593 // The file should have been cached and saved.
594 EXPECT_EQ(1U, GetCache()->cached_avatar_images_.size());
595 EXPECT_TRUE(GetCache()->GetHighResAvatarOfProfileAtIndex(0));
596 EXPECT_EQ(GetCache()->cached_avatar_images_[file_name],
597 GetCache()->GetHighResAvatarOfProfileAtIndex(0));
599 // Make sure everything has completed, and the file has been written to disk.
600 base::RunLoop().RunUntilIdle();
602 // Clean up.
603 base::FilePath icon_path =
604 profiles::GetPathOfHighResAvatarAtIndex(kIconIndex);
605 EXPECT_NE(std::string::npos, icon_path.MaybeAsASCII().find(file_name));
606 EXPECT_TRUE(base::PathExists(icon_path));
607 EXPECT_TRUE(base::DeleteFile(icon_path, true));
608 EXPECT_FALSE(base::PathExists(icon_path));
611 TEST_F(ProfileInfoCacheTest, MigrateLegacyProfileNamesWithNewAvatarMenu) {
612 switches::EnableNewAvatarMenuForTesting(
613 base::CommandLine::ForCurrentProcess());
614 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
616 base::FilePath path_1 = GetProfilePath("path_1");
617 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("Default Profile"),
618 base::string16(), 0, std::string());
619 base::FilePath path_2 = GetProfilePath("path_2");
620 GetCache()->AddProfileToCache(path_2, ASCIIToUTF16("First user"),
621 base::string16(), 1, std::string());
622 base::string16 name_3 = ASCIIToUTF16("Lemonade");
623 base::FilePath path_3 = GetProfilePath("path_3");
624 GetCache()->AddProfileToCache(path_3, name_3,
625 base::string16(), 2, std::string());
626 base::string16 name_4 = ASCIIToUTF16("Batman");
627 base::FilePath path_4 = GetProfilePath("path_4");
628 GetCache()->AddProfileToCache(path_4, name_4,
629 base::string16(), 3, std::string());
630 base::string16 name_5 = ASCIIToUTF16("Person 2");
631 base::FilePath path_5 = GetProfilePath("path_5");
632 GetCache()->AddProfileToCache(path_5, name_5,
633 base::string16(), 2, std::string());
635 EXPECT_EQ(5U, GetCache()->GetNumberOfProfiles());
638 ResetCache();
640 // Legacy profile names like "Default Profile" and "First user" should be
641 // migrated to "Person %n" type names.
642 EXPECT_EQ(ASCIIToUTF16("Person 1"), GetCache()->GetNameOfProfileAtIndex(
643 GetCache()->GetIndexOfProfileWithPath(path_1)));
644 EXPECT_EQ(ASCIIToUTF16("Person 3"), GetCache()->GetNameOfProfileAtIndex(
645 GetCache()->GetIndexOfProfileWithPath(path_2)));
647 // Other profile names should not be migrated even if they're the old
648 // default cartoon profile names.
649 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
650 GetCache()->GetIndexOfProfileWithPath(path_3)));
651 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
652 GetCache()->GetIndexOfProfileWithPath(path_4)));
653 EXPECT_EQ(name_5, GetCache()->GetNameOfProfileAtIndex(
654 GetCache()->GetIndexOfProfileWithPath(path_5)));
656 #endif
658 TEST_F(ProfileInfoCacheTest,
659 DontMigrateLegacyProfileNamesWithoutNewAvatarMenu) {
660 switches::DisableNewAvatarMenuForTesting(
661 base::CommandLine::ForCurrentProcess());
663 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
665 base::string16 name_1 = ASCIIToUTF16("Default Profile");
666 base::FilePath path_1 = GetProfilePath("path_1");
667 GetCache()->AddProfileToCache(path_1, name_1,
668 base::string16(), 0, std::string());
669 base::string16 name_2 = ASCIIToUTF16("First user");
670 base::FilePath path_2 = GetProfilePath("path_2");
671 GetCache()->AddProfileToCache(path_2, name_2,
672 base::string16(), 1, std::string());
673 base::string16 name_3 = ASCIIToUTF16("Lemonade");
674 base::FilePath path_3 = GetProfilePath("path_3");
675 GetCache()->AddProfileToCache(path_3, name_3,
676 base::string16(), 2, std::string());
677 base::string16 name_4 = ASCIIToUTF16("Batman");
678 base::FilePath path_4 = GetProfilePath("path_4");
679 GetCache()->AddProfileToCache(path_4, name_4,
680 base::string16(), 3, std::string());
681 EXPECT_EQ(4U, GetCache()->GetNumberOfProfiles());
683 ResetCache();
685 // Profile names should have been preserved.
686 EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex(
687 GetCache()->GetIndexOfProfileWithPath(path_1)));
688 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(
689 GetCache()->GetIndexOfProfileWithPath(path_2)));
690 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
691 GetCache()->GetIndexOfProfileWithPath(path_3)));
692 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
693 GetCache()->GetIndexOfProfileWithPath(path_4)));