Cleanup: new common GetProfileSwitcherTextForItem for use on both Win and Mac
[chromium-blink-merge.git] / chrome / browser / profiles / profile_info_cache_unittest.cc
blob1f66b58c948ac7611dc7e2c8986a2649e1b248bd
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/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_switches.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/test/base/testing_browser_process.h"
24 #include "components/signin/core/common/profile_management_switches.h"
25 #include "content/public/test/test_browser_thread_bundle.h"
26 #include "content/public/test/test_utils.h"
27 #include "third_party/skia/include/core/SkBitmap.h"
28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/gfx/image/image.h"
30 #include "ui/gfx/image/image_unittest_util.h"
32 using base::ASCIIToUTF16;
33 using base::UTF8ToUTF16;
34 using content::BrowserThread;
36 ProfileNameVerifierObserver::ProfileNameVerifierObserver(
37 TestingProfileManager* testing_profile_manager)
38 : testing_profile_manager_(testing_profile_manager) {
39 DCHECK(testing_profile_manager_);
42 ProfileNameVerifierObserver::~ProfileNameVerifierObserver() {
45 void ProfileNameVerifierObserver::OnProfileAdded(
46 const base::FilePath& profile_path) {
47 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
48 GetCache()->GetIndexOfProfileWithPath(profile_path));
49 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
50 profile_names_.insert(profile_name);
53 void ProfileNameVerifierObserver::OnProfileWillBeRemoved(
54 const base::FilePath& profile_path) {
55 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
56 GetCache()->GetIndexOfProfileWithPath(profile_path));
57 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
58 profile_names_.erase(profile_name);
61 void ProfileNameVerifierObserver::OnProfileWasRemoved(
62 const base::FilePath& profile_path,
63 const base::string16& profile_name) {
64 EXPECT_TRUE(profile_names_.find(profile_name) == profile_names_.end());
67 void ProfileNameVerifierObserver::OnProfileNameChanged(
68 const base::FilePath& profile_path,
69 const base::string16& old_profile_name) {
70 base::string16 new_profile_name = GetCache()->GetNameOfProfileAtIndex(
71 GetCache()->GetIndexOfProfileWithPath(profile_path));
72 EXPECT_TRUE(profile_names_.find(old_profile_name) != profile_names_.end());
73 EXPECT_TRUE(profile_names_.find(new_profile_name) == profile_names_.end());
74 profile_names_.erase(old_profile_name);
75 profile_names_.insert(new_profile_name);
78 void ProfileNameVerifierObserver::OnProfileAvatarChanged(
79 const base::FilePath& profile_path) {
80 base::string16 profile_name = GetCache()->GetNameOfProfileAtIndex(
81 GetCache()->GetIndexOfProfileWithPath(profile_path));
82 EXPECT_TRUE(profile_names_.find(profile_name) != profile_names_.end());
85 ProfileInfoCache* ProfileNameVerifierObserver::GetCache() {
86 return testing_profile_manager_->profile_info_cache();
89 ProfileInfoCacheTest::ProfileInfoCacheTest()
90 : testing_profile_manager_(TestingBrowserProcess::GetGlobal()),
91 name_observer_(&testing_profile_manager_) {
94 ProfileInfoCacheTest::~ProfileInfoCacheTest() {
97 void ProfileInfoCacheTest::SetUp() {
98 ASSERT_TRUE(testing_profile_manager_.SetUp());
99 testing_profile_manager_.profile_info_cache()->AddObserver(&name_observer_);
102 void ProfileInfoCacheTest::TearDown() {
103 // Drain the UI thread to make sure all tasks are completed. This prevents
104 // memory leaks.
105 base::RunLoop().RunUntilIdle();
108 ProfileInfoCache* ProfileInfoCacheTest::GetCache() {
109 return testing_profile_manager_.profile_info_cache();
112 base::FilePath ProfileInfoCacheTest::GetProfilePath(
113 const std::string& base_name) {
114 return testing_profile_manager_.profile_manager()->user_data_dir().
115 AppendASCII(base_name);
118 void ProfileInfoCacheTest::ResetCache() {
119 testing_profile_manager_.DeleteProfileInfoCache();
122 TEST_F(ProfileInfoCacheTest, AddProfiles) {
123 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
125 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
126 for (uint32 i = 0; i < 4; ++i) {
127 base::FilePath profile_path =
128 GetProfilePath(base::StringPrintf("path_%ud", i));
129 base::string16 profile_name =
130 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
131 const SkBitmap* icon = rb.GetImageNamed(
132 profiles::GetDefaultAvatarIconResourceIDAtIndex(
133 i)).ToSkBitmap();
134 std::string supervised_user_id = i == 3 ? "TEST_ID" : "";
136 GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(),
137 i, supervised_user_id);
138 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
139 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
140 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
142 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles());
143 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
144 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
145 const SkBitmap* actual_icon =
146 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
147 EXPECT_EQ(icon->width(), actual_icon->width());
148 EXPECT_EQ(icon->height(), actual_icon->height());
149 EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i));
150 EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i));
151 EXPECT_EQ(supervised_user_id,
152 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
155 // Reset the cache and test the it reloads correctly.
156 ResetCache();
158 EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles());
159 for (uint32 i = 0; i < 4; ++i) {
160 base::FilePath profile_path =
161 GetProfilePath(base::StringPrintf("path_%ud", i));
162 EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path));
163 base::string16 profile_name =
164 ASCIIToUTF16(base::StringPrintf("name_%ud", i));
165 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
166 EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i));
167 EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i));
168 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
169 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i));
173 TEST_F(ProfileInfoCacheTest, DeleteProfile) {
174 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
176 base::FilePath path_1 = GetProfilePath("path_1");
177 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
178 base::string16(), 0, std::string());
179 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
181 base::FilePath path_2 = GetProfilePath("path_2");
182 base::string16 name_2 = ASCIIToUTF16("name_2");
183 GetCache()->AddProfileToCache(path_2, name_2, base::string16(), 0,
184 std::string());
185 EXPECT_EQ(2u, GetCache()->GetNumberOfProfiles());
187 GetCache()->DeleteProfileFromCache(path_1);
188 EXPECT_EQ(1u, GetCache()->GetNumberOfProfiles());
189 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(0));
191 GetCache()->DeleteProfileFromCache(path_2);
192 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
195 TEST_F(ProfileInfoCacheTest, MutateProfile) {
196 GetCache()->AddProfileToCache(
197 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
198 base::string16(), 0, std::string());
199 GetCache()->AddProfileToCache(
200 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
201 base::string16(), 0, std::string());
203 base::string16 new_name = ASCIIToUTF16("new_name");
204 GetCache()->SetNameOfProfileAtIndex(1, new_name);
205 EXPECT_EQ(new_name, GetCache()->GetNameOfProfileAtIndex(1));
206 EXPECT_NE(new_name, GetCache()->GetNameOfProfileAtIndex(0));
208 base::string16 new_user_name = ASCIIToUTF16("user_name");
209 GetCache()->SetUserNameOfProfileAtIndex(1, new_user_name);
210 EXPECT_EQ(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(1));
211 EXPECT_NE(new_user_name, GetCache()->GetUserNameOfProfileAtIndex(0));
213 size_t new_icon_index = 3;
214 GetCache()->SetAvatarIconOfProfileAtIndex(1, new_icon_index);
215 // Not much to test.
216 GetCache()->GetAvatarIconOfProfileAtIndex(1);
219 TEST_F(ProfileInfoCacheTest, Sort) {
220 base::string16 name_a = ASCIIToUTF16("apple");
221 GetCache()->AddProfileToCache(
222 GetProfilePath("path_a"), name_a, base::string16(), 0, std::string());
224 base::string16 name_c = ASCIIToUTF16("cat");
225 GetCache()->AddProfileToCache(
226 GetProfilePath("path_c"), name_c, base::string16(), 0, std::string());
228 // Sanity check the initial order.
229 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
230 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
232 // Add a new profile (start with a capital to test case insensitive sorting.
233 base::string16 name_b = ASCIIToUTF16("Banana");
234 GetCache()->AddProfileToCache(
235 GetProfilePath("path_b"), name_b, base::string16(), 0, std::string());
237 // Verify the new order.
238 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0));
239 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1));
240 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2));
242 // Change the name of an existing profile.
243 name_a = UTF8ToUTF16("dog");
244 GetCache()->SetNameOfProfileAtIndex(0, name_a);
246 // Verify the new order.
247 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
248 EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1));
249 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2));
251 // Delete a profile.
252 GetCache()->DeleteProfileFromCache(GetProfilePath("path_c"));
254 // Verify the new order.
255 EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0));
256 EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1));
259 TEST_F(ProfileInfoCacheTest, BackgroundModeStatus) {
260 GetCache()->AddProfileToCache(
261 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
262 base::string16(), 0, std::string());
263 GetCache()->AddProfileToCache(
264 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
265 base::string16(), 0, std::string());
267 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
268 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
270 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, true);
272 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
273 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
275 GetCache()->SetBackgroundStatusOfProfileAtIndex(0, true);
277 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
278 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
280 GetCache()->SetBackgroundStatusOfProfileAtIndex(1, false);
282 EXPECT_TRUE(GetCache()->GetBackgroundStatusOfProfileAtIndex(0));
283 EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1));
286 TEST_F(ProfileInfoCacheTest, ProfileActiveTime) {
287 GetCache()->AddProfileToCache(
288 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
289 base::string16(), 0, std::string());
290 EXPECT_EQ(base::Time(), GetCache()->GetProfileActiveTimeAtIndex(0));
291 // Before & After times are artificially shifted because just relying upon
292 // the system time can yield problems due to inaccuracies in the
293 // underlying storage system (which uses a double with only 52 bits of
294 // precision to store the 64-bit "time" number). http://crbug.com/346827
295 base::Time before = base::Time::Now();
296 before -= base::TimeDelta::FromSeconds(1);
297 GetCache()->SetProfileActiveTimeAtIndex(0);
298 base::Time after = base::Time::Now();
299 after += base::TimeDelta::FromSeconds(1);
300 EXPECT_LE(before, GetCache()->GetProfileActiveTimeAtIndex(0));
301 EXPECT_GE(after, GetCache()->GetProfileActiveTimeAtIndex(0));
304 TEST_F(ProfileInfoCacheTest, GAIAName) {
305 GetCache()->AddProfileToCache(
306 GetProfilePath("path_1"), ASCIIToUTF16("Person 1"),
307 base::string16(), 0, std::string());
308 base::string16 profile_name(ASCIIToUTF16("Person 2"));
309 GetCache()->AddProfileToCache(
310 GetProfilePath("path_2"), profile_name, base::string16(), 0,
311 std::string());
313 int index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
314 int index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
316 // Sanity check.
317 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
318 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index2).empty());
320 // Set GAIA name. This re-sorts the cache.
321 base::string16 gaia_name(ASCIIToUTF16("Pat Smith"));
322 GetCache()->SetGAIANameOfProfileAtIndex(index2, gaia_name);
323 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
324 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
326 // Since there is a GAIA name, we use that as a display name.
327 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(index1).empty());
328 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
329 EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(index2));
331 // Don't use GAIA name as profile name. This re-sorts the cache.
332 base::string16 custom_name(ASCIIToUTF16("Custom name"));
333 GetCache()->SetNameOfProfileAtIndex(index2, custom_name);
334 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index2, false);
336 index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
337 index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
339 EXPECT_EQ(custom_name, GetCache()->GetNameOfProfileAtIndex(index2));
340 EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(index2));
343 TEST_F(ProfileInfoCacheTest, GAIAPicture) {
344 const int kDefaultAvatarIndex = 0;
345 const int kOtherAvatarIndex = 1;
346 const int kGaiaPictureSize = 256; // Standard size of a Gaia account picture.
347 GetCache()->AddProfileToCache(
348 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
349 base::string16(), kDefaultAvatarIndex, std::string());
350 GetCache()->AddProfileToCache(
351 GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
352 base::string16(), kDefaultAvatarIndex, std::string());
354 // Sanity check.
355 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
356 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(1));
357 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
358 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
360 // The profile icon should be the default one.
361 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(0));
362 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
363 int default_avatar_id =
364 profiles::GetDefaultAvatarIconResourceIDAtIndex(kDefaultAvatarIndex);
365 const gfx::Image& default_avatar_image(
366 ResourceBundle::GetSharedInstance().GetImageNamed(default_avatar_id));
367 EXPECT_TRUE(gfx::test::IsEqual(
368 default_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
370 // Set GAIA picture.
371 gfx::Image gaia_image(gfx::test::CreateImage(
372 kGaiaPictureSize, kGaiaPictureSize));
373 GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
374 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
375 EXPECT_TRUE(gfx::test::IsEqual(
376 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
377 // Since we're still using the default avatar, the GAIA image should be
378 // preferred over the generic avatar image.
379 EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
380 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
381 EXPECT_TRUE(gfx::test::IsEqual(
382 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
384 // Set a non-default avatar. This should be preferred over the GAIA image.
385 GetCache()->SetAvatarIconOfProfileAtIndex(1, kOtherAvatarIndex);
386 GetCache()->SetProfileIsUsingDefaultAvatarAtIndex(1, false);
387 EXPECT_FALSE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
388 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
389 int other_avatar_id =
390 profiles::GetDefaultAvatarIconResourceIDAtIndex(kOtherAvatarIndex);
391 const gfx::Image& other_avatar_image(
392 ResourceBundle::GetSharedInstance().GetImageNamed(other_avatar_id));
393 EXPECT_TRUE(gfx::test::IsEqual(
394 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
396 // Explicitly setting the GAIA picture should make it preferred again.
397 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
398 EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
399 EXPECT_TRUE(gfx::test::IsEqual(
400 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
401 EXPECT_TRUE(gfx::test::IsEqual(
402 gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
404 // Clearing the IsUsingGAIAPicture flag should result in the generic image
405 // being used again.
406 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
407 EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
408 EXPECT_TRUE(gfx::test::IsEqual(
409 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
410 EXPECT_TRUE(gfx::test::IsEqual(
411 other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
414 TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
415 GetCache()->AddProfileToCache(
416 GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
417 base::string16(), 0, std::string());
418 gfx::Image gaia_image(gfx::test::CreateImage());
420 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
422 // Make sure everything has completed, and the file has been written to disk.
423 base::RunLoop().RunUntilIdle();
425 EXPECT_TRUE(gfx::test::IsEqual(
426 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
428 ResetCache();
429 // Try to get the GAIA picture. This should return NULL until the read from
430 // disk is done.
431 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
432 base::RunLoop().RunUntilIdle();
434 EXPECT_TRUE(gfx::test::IsEqual(
435 gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
438 TEST_F(ProfileInfoCacheTest, SetSupervisedUserId) {
439 GetCache()->AddProfileToCache(
440 GetProfilePath("test"), ASCIIToUTF16("Test"),
441 base::string16(), 0, std::string());
442 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
444 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, "TEST_ID");
445 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
446 EXPECT_EQ("TEST_ID", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
448 ResetCache();
449 EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
451 GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, std::string());
452 EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
453 EXPECT_EQ("", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
456 TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
457 base::string16 profile_name = ASCIIToUTF16("name_1");
458 int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0);
459 const gfx::Image& profile_image(
460 ResourceBundle::GetSharedInstance().GetImageNamed(id));
462 GetCache()->AddProfileToCache(
463 GetProfilePath("path_1"), profile_name, base::string16(), 0,
464 std::string());
466 // Set empty GAIA info.
467 GetCache()->SetGAIANameOfProfileAtIndex(0, base::string16());
468 GetCache()->SetGAIAPictureOfProfileAtIndex(0, NULL);
469 GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
471 // Verify that the profile name and picture are not empty.
472 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(0));
473 EXPECT_TRUE(gfx::test::IsEqual(
474 profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
477 TEST_F(ProfileInfoCacheTest, CreateSupervisedTestingProfile) {
478 testing_profile_manager_.CreateTestingProfile("default");
479 base::string16 supervised_user_name = ASCIIToUTF16("Supervised User");
480 testing_profile_manager_.CreateTestingProfile(
481 "test1", scoped_ptr<PrefServiceSyncable>(),
482 supervised_user_name, 0, "TEST_ID", TestingProfile::TestingFactories());
483 for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
484 bool is_supervised =
485 GetCache()->GetNameOfProfileAtIndex(i) == supervised_user_name;
486 EXPECT_EQ(is_supervised, GetCache()->ProfileIsSupervisedAtIndex(i));
487 std::string supervised_user_id = is_supervised ? "TEST_ID" : "";
488 EXPECT_EQ(supervised_user_id,
489 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
492 // Supervised profiles have a custom theme, which needs to be deleted on the
493 // FILE thread. Reset the profile manager now so everything is deleted while
494 // we still have a FILE thread.
495 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
498 TEST_F(ProfileInfoCacheTest, AddStubProfile) {
499 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles());
501 // Add some profiles with and without a '.' in their paths.
502 const struct {
503 const char* profile_path;
504 const char* profile_name;
505 } kTestCases[] = {
506 { "path.test0", "name_0" },
507 { "path_test1", "name_1" },
508 { "path.test2", "name_2" },
509 { "path_test3", "name_3" },
512 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
513 base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
514 base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
516 GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(),
517 i, "");
519 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i));
520 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i));
523 ASSERT_EQ(4U, GetCache()->GetNumberOfProfiles());
525 // Check that the profiles can be extracted from the local state.
526 std::vector<base::string16> names;
527 PrefService* local_state = g_browser_process->local_state();
528 const base::DictionaryValue* cache = local_state->GetDictionary(
529 prefs::kProfileInfoCache);
530 base::string16 name;
531 for (base::DictionaryValue::Iterator it(*cache); !it.IsAtEnd();
532 it.Advance()) {
533 const base::DictionaryValue* info = NULL;
534 it.value().GetAsDictionary(&info);
535 info->GetString("name", &name);
536 names.push_back(name);
539 for (size_t i = 0; i < 4; i++)
540 ASSERT_FALSE(names[i].empty());
543 // High res avatar downloading is only supported on desktop.
544 #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
545 TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
546 switches::EnableNewAvatarMenuForTesting(
547 base::CommandLine::ForCurrentProcess());
549 // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
550 ProfileInfoCache profile_info_cache(g_browser_process->local_state(),
551 testing_profile_manager_.profile_manager()->user_data_dir());
553 // // Make sure there are no avatars already on disk.
554 const size_t kIconIndex = 0;
555 base::FilePath icon_path =
556 profiles::GetPathOfHighResAvatarAtIndex(kIconIndex);
557 EXPECT_FALSE(base::PathExists(icon_path));
559 EXPECT_EQ(0U, profile_info_cache.GetNumberOfProfiles());
560 base::FilePath path_1 = GetProfilePath("path_1");
561 profile_info_cache.AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
562 base::string16(), kIconIndex, std::string());
563 EXPECT_EQ(1U, profile_info_cache.GetNumberOfProfiles());
564 base::RunLoop().RunUntilIdle();
566 // We haven't downloaded any high-res avatars yet.
567 EXPECT_EQ(0U, profile_info_cache.cached_avatar_images_.size());
569 // After adding a new profile, the download of high-res avatar will be
570 // triggered if the flag kNewAvatarMenu has been set. But the downloader
571 // won't ever call OnFetchComplete in the test.
572 EXPECT_EQ(1U, profile_info_cache.avatar_images_downloads_in_progress_.size());
574 EXPECT_FALSE(profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
576 // Simulate downloading a high-res avatar.
577 ProfileAvatarDownloader avatar_downloader(
578 kIconIndex, profile_info_cache.GetPathOfProfileAtIndex(0),
579 &profile_info_cache);
581 // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels.
582 SkBitmap bitmap;
583 bitmap.allocN32Pixels(2, 2);
584 bitmap.eraseColor(SK_ColorGREEN);
586 avatar_downloader.OnFetchComplete(
587 GURL("http://www.google.com/avatar.png"), &bitmap);
589 // Now the download should not be in progress anymore.
590 EXPECT_EQ(0U, profile_info_cache.avatar_images_downloads_in_progress_.size());
592 std::string file_name =
593 profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex);
595 // The file should have been cached and saved.
596 EXPECT_EQ(1U, profile_info_cache.cached_avatar_images_.size());
597 EXPECT_TRUE(profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
598 EXPECT_EQ(profile_info_cache.cached_avatar_images_[file_name],
599 profile_info_cache.GetHighResAvatarOfProfileAtIndex(0));
601 // Make sure everything has completed, and the file has been written to disk.
602 base::RunLoop().RunUntilIdle();
604 // Clean up.
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)));