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"
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/prefs/pref_service_syncable.h"
17 #include "chrome/browser/profiles/profile_avatar_downloader.h"
18 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
19 #include "chrome/browser/profiles/profile_info_cache.h"
20 #include "chrome/browser/profiles/profile_manager.h"
21 #include "chrome/common/chrome_paths.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/test/test_browser_thread_bundle.h"
27 #include "content/public/test/test_utils.h"
28 #include "third_party/skia/include/core/SkBitmap.h"
29 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/gfx/image/image.h"
31 #include "ui/gfx/image/image_unittest_util.h"
33 using base::ASCIIToUTF16
;
34 using base::UTF8ToUTF16
;
35 using content::BrowserThread
;
37 ProfileNameVerifierObserver::ProfileNameVerifierObserver(
38 TestingProfileManager
* testing_profile_manager
)
39 : testing_profile_manager_(testing_profile_manager
) {
40 DCHECK(testing_profile_manager_
);
43 ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
46 void ProfileNameVerifierObserver::OnProfileAdded(
47 const base::FilePath
& profile_path
) {
48 base::string16 profile_name
= GetCache()->GetNameOfProfileAtIndex(
49 GetCache()->GetIndexOfProfileWithPath(profile_path
));
50 EXPECT_TRUE(profile_names_
.find(profile_name
) == profile_names_
.end());
51 profile_names_
.insert(profile_name
);
54 void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
55 const base::FilePath
& profile_path
) {
56 base::string16 profile_name
= GetCache()->GetNameOfProfileAtIndex(
57 GetCache()->GetIndexOfProfileWithPath(profile_path
));
58 EXPECT_TRUE(profile_names_
.find(profile_name
) != profile_names_
.end());
59 profile_names_
.erase(profile_name
);
62 void ProfileNameVerifierObserver::OnProfileWasRemoved(
63 const base::FilePath
& profile_path
,
64 const base::string16
& profile_name
) {
65 EXPECT_TRUE(profile_names_
.find(profile_name
) == profile_names_
.end());
68 void ProfileNameVerifierObserver::OnProfileNameChanged(
69 const base::FilePath
& profile_path
,
70 const base::string16
& old_profile_name
) {
71 base::string16 new_profile_name
= GetCache()->GetNameOfProfileAtIndex(
72 GetCache()->GetIndexOfProfileWithPath(profile_path
));
73 EXPECT_TRUE(profile_names_
.find(old_profile_name
) != profile_names_
.end());
74 EXPECT_TRUE(profile_names_
.find(new_profile_name
) == profile_names_
.end());
75 profile_names_
.erase(old_profile_name
);
76 profile_names_
.insert(new_profile_name
);
79 void ProfileNameVerifierObserver::OnProfileAvatarChanged(
80 const base::FilePath
& profile_path
) {
81 base::string16 profile_name
= GetCache()->GetNameOfProfileAtIndex(
82 GetCache()->GetIndexOfProfileWithPath(profile_path
));
83 EXPECT_TRUE(profile_names_
.find(profile_name
) != profile_names_
.end());
86 ProfileInfoCache
* ProfileNameVerifierObserver::GetCache() {
87 return testing_profile_manager_
->profile_info_cache();
90 ProfileInfoCacheTest::ProfileInfoCacheTest()
91 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
92 name_observer_(&testing_profile_manager_
),
93 user_data_dir_override_(chrome::DIR_USER_DATA
) {
96 ProfileInfoCacheTest::~ProfileInfoCacheTest() {
99 void ProfileInfoCacheTest::SetUp() {
100 ASSERT_TRUE(testing_profile_manager_
.SetUp());
101 testing_profile_manager_
.profile_info_cache()->AddObserver(&name_observer_
);
104 void ProfileInfoCacheTest::TearDown() {
105 // Drain the UI thread to make sure all tasks are completed. This prevents
107 base::RunLoop().RunUntilIdle();
110 ProfileInfoCache
* ProfileInfoCacheTest::GetCache() {
111 return testing_profile_manager_
.profile_info_cache();
114 base::FilePath
ProfileInfoCacheTest::GetProfilePath(
115 const std::string
& base_name
) {
116 return testing_profile_manager_
.profile_manager()->user_data_dir().
117 AppendASCII(base_name
);
120 void ProfileInfoCacheTest::ResetCache() {
121 testing_profile_manager_
.DeleteProfileInfoCache();
124 TEST_F(ProfileInfoCacheTest
, AddProfiles
) {
125 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
127 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
128 for (uint32 i
= 0; i
< 4; ++i
) {
129 base::FilePath profile_path
=
130 GetProfilePath(base::StringPrintf("path_%ud", i
));
131 base::string16 profile_name
=
132 ASCIIToUTF16(base::StringPrintf("name_%ud", i
));
133 const SkBitmap
* icon
= rb
.GetImageNamed(
134 profiles::GetDefaultAvatarIconResourceIDAtIndex(
136 std::string supervised_user_id
= i
== 3 ? "TEST_ID" : "";
138 GetCache()->AddProfileToCache(profile_path
, profile_name
, std::string(),
139 base::string16(), i
, supervised_user_id
);
140 GetCache()->SetBackgroundStatusOfProfileAtIndex(i
, true);
141 base::string16 gaia_name
= ASCIIToUTF16(base::StringPrintf("gaia_%ud", i
));
142 GetCache()->SetGAIANameOfProfileAtIndex(i
, gaia_name
);
144 EXPECT_EQ(i
+ 1, GetCache()->GetNumberOfProfiles());
145 EXPECT_EQ(profile_name
, GetCache()->GetNameOfProfileAtIndex(i
));
146 EXPECT_EQ(profile_path
, GetCache()->GetPathOfProfileAtIndex(i
));
147 const SkBitmap
* actual_icon
=
148 GetCache()->GetAvatarIconOfProfileAtIndex(i
).ToSkBitmap();
149 EXPECT_EQ(icon
->width(), actual_icon
->width());
150 EXPECT_EQ(icon
->height(), actual_icon
->height());
151 EXPECT_EQ(i
== 3, GetCache()->ProfileIsSupervisedAtIndex(i
));
152 EXPECT_EQ(i
== 3, GetCache()->IsOmittedProfileAtIndex(i
));
153 EXPECT_EQ(supervised_user_id
,
154 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i
));
157 // Reset the cache and test the it reloads correctly.
160 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
161 for (uint32 i
= 0; i
< 4; ++i
) {
162 base::FilePath profile_path
=
163 GetProfilePath(base::StringPrintf("path_%ud", i
));
164 EXPECT_EQ(i
, GetCache()->GetIndexOfProfileWithPath(profile_path
));
165 base::string16 profile_name
=
166 ASCIIToUTF16(base::StringPrintf("name_%ud", i
));
167 EXPECT_EQ(profile_name
, GetCache()->GetNameOfProfileAtIndex(i
));
168 EXPECT_EQ(i
, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i
));
169 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i
));
170 base::string16 gaia_name
= ASCIIToUTF16(base::StringPrintf("gaia_%ud", i
));
171 EXPECT_EQ(gaia_name
, GetCache()->GetGAIANameOfProfileAtIndex(i
));
175 TEST_F(ProfileInfoCacheTest
, DeleteProfile
) {
176 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
178 base::FilePath path_1
= GetProfilePath("path_1");
179 GetCache()->AddProfileToCache(path_1
, ASCIIToUTF16("name_1"),
180 std::string(), base::string16(), 0,
182 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
184 base::FilePath path_2
= GetProfilePath("path_2");
185 base::string16 name_2
= ASCIIToUTF16("name_2");
186 GetCache()->AddProfileToCache(path_2
, name_2
, std::string(), base::string16(),
188 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
190 GetCache()->DeleteProfileFromCache(path_1
);
191 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
192 EXPECT_EQ(name_2
, GetCache()->GetNameOfProfileAtIndex(0));
194 GetCache()->DeleteProfileFromCache(path_2
);
195 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
198 TEST_F(ProfileInfoCacheTest
, MutateProfile
) {
199 GetCache()->AddProfileToCache(
200 GetProfilePath("path_1"), ASCIIToUTF16("name_1"), std::string(),
201 base::string16(), 0, std::string());
202 GetCache()->AddProfileToCache(
203 GetProfilePath("path_2"), ASCIIToUTF16("name_2"), std::string(),
204 base::string16(), 0, std::string());
206 base::string16 new_name
= ASCIIToUTF16("new_name");
207 GetCache()->SetNameOfProfileAtIndex(1, new_name
);
208 EXPECT_EQ(new_name
, GetCache()->GetNameOfProfileAtIndex(1));
209 EXPECT_NE(new_name
, GetCache()->GetNameOfProfileAtIndex(0));
211 base::string16 new_user_name
= ASCIIToUTF16("user_name");
212 std::string new_gaia_id
= "12345";
213 GetCache()->SetAuthInfoOfProfileAtIndex(1, new_gaia_id
, new_user_name
);
214 EXPECT_EQ(new_user_name
, GetCache()->GetUserNameOfProfileAtIndex(1));
215 EXPECT_EQ(new_gaia_id
, GetCache()->GetGAIAIdOfProfileAtIndex(1));
216 EXPECT_NE(new_user_name
, GetCache()->GetUserNameOfProfileAtIndex(0));
218 size_t new_icon_index
= 3;
219 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index
);
221 GetCache()->GetAvatarIconOfProfileAtIndex(1);
224 TEST_F(ProfileInfoCacheTest
, Sort
) {
225 base::string16 name_a
= ASCIIToUTF16("apple");
226 GetCache()->AddProfileToCache(
227 GetProfilePath("path_a"), name_a
, std::string(), base::string16(), 0,
230 base::string16 name_c
= ASCIIToUTF16("cat");
231 GetCache()->AddProfileToCache(
232 GetProfilePath("path_c"), name_c
, std::string(), base::string16(), 0,
235 // Sanity check the initial order.
236 EXPECT_EQ(name_a
, GetCache()->GetNameOfProfileAtIndex(0));
237 EXPECT_EQ(name_c
, GetCache()->GetNameOfProfileAtIndex(1));
239 // Add a new profile (start with a capital to test case insensitive sorting.
240 base::string16 name_b
= ASCIIToUTF16("Banana");
241 GetCache()->AddProfileToCache(
242 GetProfilePath("path_b"), name_b
, std::string(), base::string16(), 0,
245 // Verify the new order.
246 EXPECT_EQ(name_a
, GetCache()->GetNameOfProfileAtIndex(0));
247 EXPECT_EQ(name_b
, GetCache()->GetNameOfProfileAtIndex(1));
248 EXPECT_EQ(name_c
, GetCache()->GetNameOfProfileAtIndex(2));
250 // Change the name of an existing profile.
251 name_a
= UTF8ToUTF16("dog");
252 GetCache()->SetNameOfProfileAtIndex(0, name_a
);
254 // Verify the new order.
255 EXPECT_EQ(name_b
, GetCache()->GetNameOfProfileAtIndex(0));
256 EXPECT_EQ(name_c
, GetCache()->GetNameOfProfileAtIndex(1));
257 EXPECT_EQ(name_a
, GetCache()->GetNameOfProfileAtIndex(2));
260 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
262 // Verify the new order.
263 EXPECT_EQ(name_b
, GetCache()->GetNameOfProfileAtIndex(0));
264 EXPECT_EQ(name_a
, GetCache()->GetNameOfProfileAtIndex(1));
267 TEST_F(ProfileInfoCacheTest
, BackgroundModeStatus
) {
268 GetCache()->AddProfileToCache(
269 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
270 std::string(), base::string16(), 0, std::string());
271 GetCache()->AddProfileToCache(
272 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
273 std::string(), base::string16(), 0, std::string());
275 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
276 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
278 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
280 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
281 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
283 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
285 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
286 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
288 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
290 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
291 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
294 TEST_F(ProfileInfoCacheTest
, ProfileActiveTime
) {
295 GetCache()->AddProfileToCache(
296 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
297 std::string(), base::string16(), 0, std::string());
298 EXPECT_EQ(base::Time(), GetCache()->GetProfileActiveTimeAtIndex(0));
299 // Before & After times are artificially shifted because just relying upon
300 // the system time can yield problems due to inaccuracies in the
301 // underlying storage system (which uses a double with only 52 bits of
302 // precision to store the 64-bit "time" number). http://crbug.com/346827
303 base::Time before
= base::Time::Now();
304 before
-= base::TimeDelta::FromSeconds(1);
305 GetCache()->SetProfileActiveTimeAtIndex(0);
306 base::Time after
= base::Time::Now();
307 after
+= base::TimeDelta::FromSeconds(1);
308 EXPECT_LE(before
, GetCache()->GetProfileActiveTimeAtIndex(0));
309 EXPECT_GE(after
, GetCache()->GetProfileActiveTimeAtIndex(0));
312 TEST_F(ProfileInfoCacheTest
, GAIAName
) {
313 GetCache()->AddProfileToCache(
314 GetProfilePath("path_1"), ASCIIToUTF16("Person 1"),
315 std::string(), base::string16(), 0, std::string());
316 base::string16
profile_name(ASCIIToUTF16("Person 2"));
317 GetCache()->AddProfileToCache(
318 GetProfilePath("path_2"), profile_name
, std::string(), base::string16(),
321 int index1
= GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
322 int index2
= GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
325 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1
).empty());
326 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2
).empty());
328 // Set GAIA name. This re-sorts the cache.
329 base::string16
gaia_name(ASCIIToUTF16("Pat Smith"));
330 GetCache()->SetGAIANameOfProfileAtIndex(index2
, gaia_name
);
331 index1
= GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
332 index2
= GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
334 // Since there is a GAIA name, we use that as a display name.
335 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1
).empty());
336 EXPECT_EQ(gaia_name
, GetCache()->GetGAIANameOfProfileAtIndex(index2
));
337 EXPECT_EQ(gaia_name
, GetCache()->GetNameOfProfileAtIndex(index2
));
339 // Don't use GAIA name as profile name. This re-sorts the cache.
340 base::string16
custom_name(ASCIIToUTF16("Custom name"));
341 GetCache()->SetNameOfProfileAtIndex(index2
, custom_name
);
342 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index2
, false);
344 index1
= GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
345 index2
= GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
347 EXPECT_EQ(custom_name
, GetCache()->GetNameOfProfileAtIndex(index2
));
348 EXPECT_EQ(gaia_name
, GetCache()->GetGAIANameOfProfileAtIndex(index2
));
351 TEST_F(ProfileInfoCacheTest
, GAIAPicture
) {
352 const int kDefaultAvatarIndex
= 0;
353 const int kOtherAvatarIndex
= 1;
354 const int kGaiaPictureSize
= 256; // Standard size of a Gaia account picture.
355 GetCache()->AddProfileToCache(
356 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
357 std::string(), base::string16(), kDefaultAvatarIndex
, std::string());
358 GetCache()->AddProfileToCache(
359 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
360 std::string(), base::string16(), kDefaultAvatarIndex
, std::string());
363 EXPECT_EQ(NULL
, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
364 EXPECT_EQ(NULL
, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
365 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
366 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
368 // The profile icon should be the default one.
369 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(0));
370 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
371 int default_avatar_id
=
372 profiles::GetDefaultAvatarIconResourceIDAtIndex(kDefaultAvatarIndex
);
373 const gfx::Image
& default_avatar_image(
374 ResourceBundle::GetSharedInstance().GetImageNamed(default_avatar_id
));
375 EXPECT_TRUE(gfx::test::IsEqual(
376 default_avatar_image
, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
379 gfx::Image
gaia_image(gfx::test::CreateImage(
380 kGaiaPictureSize
, kGaiaPictureSize
));
381 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image
);
382 EXPECT_EQ(NULL
, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
383 EXPECT_TRUE(gfx::test::IsEqual(
384 gaia_image
, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
385 // Since we're still using the default avatar, the GAIA image should be
386 // preferred over the generic avatar image.
387 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
388 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
389 EXPECT_TRUE(gfx::test::IsEqual(
390 gaia_image
, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
392 // Set a non-default avatar. This should be preferred over the GAIA image.
393 GetCache()->SetAvatarIconOfProfileAtIndex(1, kOtherAvatarIndex
);
394 GetCache()->SetProfileIsUsingDefaultAvatarAtIndex(1, false);
395 EXPECT_FALSE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
396 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
397 int other_avatar_id
=
398 profiles::GetDefaultAvatarIconResourceIDAtIndex(kOtherAvatarIndex
);
399 const gfx::Image
& other_avatar_image(
400 ResourceBundle::GetSharedInstance().GetImageNamed(other_avatar_id
));
401 EXPECT_TRUE(gfx::test::IsEqual(
402 other_avatar_image
, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
404 // Explicitly setting the GAIA picture should make it preferred again.
405 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
406 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
407 EXPECT_TRUE(gfx::test::IsEqual(
408 gaia_image
, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
409 EXPECT_TRUE(gfx::test::IsEqual(
410 gaia_image
, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
412 // Clearing the IsUsingGAIAPicture flag should result in the generic image
414 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
415 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
416 EXPECT_TRUE(gfx::test::IsEqual(
417 gaia_image
, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
418 EXPECT_TRUE(gfx::test::IsEqual(
419 other_avatar_image
, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
422 TEST_F(ProfileInfoCacheTest
, PersistGAIAPicture
) {
423 GetCache()->AddProfileToCache(
424 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
425 std::string(), base::string16(), 0, std::string());
426 gfx::Image
gaia_image(gfx::test::CreateImage());
428 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image
);
430 // Make sure everything has completed, and the file has been written to disk.
431 base::RunLoop().RunUntilIdle();
433 EXPECT_TRUE(gfx::test::IsEqual(
434 gaia_image
, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
437 // Try to get the GAIA picture. This should return NULL until the read from
439 EXPECT_EQ(NULL
, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
440 base::RunLoop().RunUntilIdle();
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 std::string(), 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));
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
, std::string(), base::string16(),
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
++) {
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.
511 const char* profile_path
;
512 const char* profile_name
;
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
, std::string(),
525 base::string16(), 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
);
539 for (base::DictionaryValue::Iterator
it(*cache
); !it
.IsAtEnd();
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 // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
558 ProfileInfoCache
profile_info_cache(g_browser_process
->local_state(),
559 testing_profile_manager_
.profile_manager()->user_data_dir());
561 // Make sure there are no avatars already on disk.
562 const size_t kIconIndex
= 0;
563 base::FilePath icon_path
=
564 profiles::GetPathOfHighResAvatarAtIndex(kIconIndex
);
565 EXPECT_FALSE(base::PathExists(icon_path
));
567 EXPECT_EQ(0U, profile_info_cache
.GetNumberOfProfiles());
568 base::FilePath path_1
= GetProfilePath("path_1");
569 profile_info_cache
.AddProfileToCache(path_1
, ASCIIToUTF16("name_1"),
570 std::string(), base::string16(), kIconIndex
, std::string());
571 EXPECT_EQ(1U, profile_info_cache
.GetNumberOfProfiles());
572 base::RunLoop().RunUntilIdle();
574 // We haven't downloaded any high-res avatars yet.
575 EXPECT_EQ(0U, profile_info_cache
.cached_avatar_images_
.size());
577 // After adding a new profile, the download of high-res avatar will be
578 // triggered if the flag kNewAvatarMenu has been set. But the downloader
579 // won't ever call OnFetchComplete in the test.
580 EXPECT_EQ(1U, profile_info_cache
.avatar_images_downloads_in_progress_
.size());
582 EXPECT_FALSE(profile_info_cache
.GetHighResAvatarOfProfileAtIndex(0));
584 // Simulate downloading a high-res avatar.
585 ProfileAvatarDownloader
avatar_downloader(
586 kIconIndex
, profile_info_cache
.GetPathOfProfileAtIndex(0),
587 &profile_info_cache
);
589 // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels.
591 bitmap
.allocN32Pixels(2, 2);
592 bitmap
.eraseColor(SK_ColorGREEN
);
594 avatar_downloader
.OnFetchComplete(
595 GURL("http://www.google.com/avatar.png"), &bitmap
);
597 // Now the download should not be in progress anymore.
598 EXPECT_EQ(0U, profile_info_cache
.avatar_images_downloads_in_progress_
.size());
600 std::string file_name
=
601 profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex
);
603 // The file should have been cached and saved.
604 EXPECT_EQ(1U, profile_info_cache
.cached_avatar_images_
.size());
605 EXPECT_TRUE(profile_info_cache
.GetHighResAvatarOfProfileAtIndex(0));
606 EXPECT_EQ(profile_info_cache
.cached_avatar_images_
[file_name
],
607 profile_info_cache
.GetHighResAvatarOfProfileAtIndex(0));
609 // Make sure everything has completed, and the file has been written to disk.
610 base::RunLoop().RunUntilIdle();
613 EXPECT_NE(std::string::npos
, icon_path
.MaybeAsASCII().find(file_name
));
614 EXPECT_TRUE(base::PathExists(icon_path
));
615 EXPECT_TRUE(base::DeleteFile(icon_path
, true));
616 EXPECT_FALSE(base::PathExists(icon_path
));
619 TEST_F(ProfileInfoCacheTest
, NothingToDownloadHighResAvatarTest
) {
620 switches::EnableNewAvatarMenuForTesting(
621 base::CommandLine::ForCurrentProcess());
623 // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
624 ProfileInfoCache
profile_info_cache(
625 g_browser_process
->local_state(),
626 testing_profile_manager_
.profile_manager()->user_data_dir());
628 const size_t kIconIndex
= profiles::GetPlaceholderAvatarIndex();
630 EXPECT_EQ(0U, profile_info_cache
.GetNumberOfProfiles());
631 base::FilePath path_1
= GetProfilePath("path_1");
632 profile_info_cache
.AddProfileToCache(path_1
, ASCIIToUTF16("name_1"),
633 std::string(), base::string16(),
634 kIconIndex
, std::string());
635 EXPECT_EQ(1U, profile_info_cache
.GetNumberOfProfiles());
636 base::RunLoop().RunUntilIdle();
638 // We haven't tried to download any high-res avatars as the specified icon is
639 // just a placeholder.
640 EXPECT_EQ(0U, profile_info_cache
.cached_avatar_images_
.size());
641 EXPECT_EQ(0U, profile_info_cache
.avatar_images_downloads_in_progress_
.size());
644 TEST_F(ProfileInfoCacheTest
, MigrateLegacyProfileNamesWithNewAvatarMenu
) {
645 switches::EnableNewAvatarMenuForTesting(
646 base::CommandLine::ForCurrentProcess());
647 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
649 base::FilePath path_1
= GetProfilePath("path_1");
650 GetCache()->AddProfileToCache(path_1
, ASCIIToUTF16("Default Profile"),
651 std::string(), base::string16(), 0,
653 base::FilePath path_2
= GetProfilePath("path_2");
654 GetCache()->AddProfileToCache(path_2
, ASCIIToUTF16("First user"),
655 std::string(), base::string16(), 1,
657 base::string16 name_3
= ASCIIToUTF16("Lemonade");
658 base::FilePath path_3
= GetProfilePath("path_3");
659 GetCache()->AddProfileToCache(path_3
, name_3
,
660 std::string(), base::string16(), 2,
662 base::string16 name_4
= ASCIIToUTF16("Batman");
663 base::FilePath path_4
= GetProfilePath("path_4");
664 GetCache()->AddProfileToCache(path_4
, name_4
,
665 std::string(), base::string16(), 3,
667 base::string16 name_5
= ASCIIToUTF16("Person 2");
668 base::FilePath path_5
= GetProfilePath("path_5");
669 GetCache()->AddProfileToCache(path_5
, name_5
,
670 std::string(), base::string16(), 2,
673 EXPECT_EQ(5U, GetCache()->GetNumberOfProfiles());
678 // Legacy profile names like "Default Profile" and "First user" should be
679 // migrated to "Person %n" type names.
680 EXPECT_EQ(ASCIIToUTF16("Person 1"), GetCache()->GetNameOfProfileAtIndex(
681 GetCache()->GetIndexOfProfileWithPath(path_1
)));
682 EXPECT_EQ(ASCIIToUTF16("Person 3"), GetCache()->GetNameOfProfileAtIndex(
683 GetCache()->GetIndexOfProfileWithPath(path_2
)));
685 // Other profile names should not be migrated even if they're the old
686 // default cartoon profile names.
687 EXPECT_EQ(name_3
, GetCache()->GetNameOfProfileAtIndex(
688 GetCache()->GetIndexOfProfileWithPath(path_3
)));
689 EXPECT_EQ(name_4
, GetCache()->GetNameOfProfileAtIndex(
690 GetCache()->GetIndexOfProfileWithPath(path_4
)));
691 EXPECT_EQ(name_5
, GetCache()->GetNameOfProfileAtIndex(
692 GetCache()->GetIndexOfProfileWithPath(path_5
)));
696 TEST_F(ProfileInfoCacheTest
,
697 DontMigrateLegacyProfileNamesWithoutNewAvatarMenu
) {
698 switches::DisableNewAvatarMenuForTesting(
699 base::CommandLine::ForCurrentProcess());
701 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
703 base::string16 name_1
= ASCIIToUTF16("Default Profile");
704 base::FilePath path_1
= GetProfilePath("path_1");
705 GetCache()->AddProfileToCache(path_1
, name_1
,
706 std::string(), base::string16(), 0,
708 base::string16 name_2
= ASCIIToUTF16("First user");
709 base::FilePath path_2
= GetProfilePath("path_2");
710 GetCache()->AddProfileToCache(path_2
, name_2
,
711 std::string(), base::string16(), 1,
713 base::string16 name_3
= ASCIIToUTF16("Lemonade");
714 base::FilePath path_3
= GetProfilePath("path_3");
715 GetCache()->AddProfileToCache(path_3
, name_3
,
716 std::string(), base::string16(), 2,
718 base::string16 name_4
= ASCIIToUTF16("Batman");
719 base::FilePath path_4
= GetProfilePath("path_4");
720 GetCache()->AddProfileToCache(path_4
, name_4
,
721 std::string(), base::string16(), 3,
723 EXPECT_EQ(4U, GetCache()->GetNumberOfProfiles());
727 // Profile names should have been preserved.
728 EXPECT_EQ(name_1
, GetCache()->GetNameOfProfileAtIndex(
729 GetCache()->GetIndexOfProfileWithPath(path_1
)));
730 EXPECT_EQ(name_2
, GetCache()->GetNameOfProfileAtIndex(
731 GetCache()->GetIndexOfProfileWithPath(path_2
)));
732 EXPECT_EQ(name_3
, GetCache()->GetNameOfProfileAtIndex(
733 GetCache()->GetIndexOfProfileWithPath(path_3
)));
734 EXPECT_EQ(name_4
, GetCache()->GetNameOfProfileAtIndex(
735 GetCache()->GetIndexOfProfileWithPath(path_4
)));