OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/profiles/profile_info_cache_unittest.h" | 5 #include "chrome/browser/profiles/profile_info_cache_unittest.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 AppendASCII(base_name); | 115 AppendASCII(base_name); |
116 } | 116 } |
117 | 117 |
118 void ProfileInfoCacheTest::ResetCache() { | 118 void ProfileInfoCacheTest::ResetCache() { |
119 testing_profile_manager_.DeleteProfileInfoCache(); | 119 testing_profile_manager_.DeleteProfileInfoCache(); |
120 } | 120 } |
121 | 121 |
122 TEST_F(ProfileInfoCacheTest, AddProfiles) { | 122 TEST_F(ProfileInfoCacheTest, AddProfiles) { |
123 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); | 123 EXPECT_EQ(0u, GetCache()->GetNumberOfProfiles()); |
124 | 124 |
125 // A bitmap for the high res avatar cache. Icon-sized bitmap of green pixels. | |
126 SkBitmap bitmap; | |
127 bitmap.allocN32Pixels(38, 31); | |
128 bitmap.eraseColor(SK_ColorGREEN); | |
129 | |
130 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 125 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
131 for (uint32 i = 0; i < 4; ++i) { | 126 for (uint32 i = 0; i < 4; ++i) { |
132 base::FilePath profile_path = | 127 base::FilePath profile_path = |
133 GetProfilePath(base::StringPrintf("path_%ud", i)); | 128 GetProfilePath(base::StringPrintf("path_%ud", i)); |
134 base::string16 profile_name = | 129 base::string16 profile_name = |
135 ASCIIToUTF16(base::StringPrintf("name_%ud", i)); | 130 ASCIIToUTF16(base::StringPrintf("name_%ud", i)); |
136 const SkBitmap* icon = rb.GetImageNamed( | 131 const SkBitmap* icon = rb.GetImageNamed( |
137 profiles::GetDefaultAvatarIconResourceIDAtIndex( | 132 profiles::GetDefaultAvatarIconResourceIDAtIndex( |
138 i)).ToSkBitmap(); | 133 i)).ToSkBitmap(); |
139 std::string supervised_user_id = i == 3 ? "TEST_ID" : ""; | 134 std::string supervised_user_id = i == 3 ? "TEST_ID" : ""; |
140 | 135 |
141 GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(), | 136 GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(), |
142 i, supervised_user_id); | 137 i, supervised_user_id); |
143 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true); | 138 GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true); |
144 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i)); | 139 base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i)); |
145 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name); | 140 GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name); |
146 | 141 |
147 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles()); | 142 EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles()); |
148 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); | 143 EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
149 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i)); | 144 EXPECT_EQ(profile_path, GetCache()->GetPathOfProfileAtIndex(i)); |
150 | |
151 ProfileAvatarDownloader avatar_downloader( | |
152 GetCache()->GetAvatarIconIndexOfProfileAtIndex(i), | |
153 profile_path, GetCache()); | |
154 avatar_downloader.OnFetchComplete( | |
155 GURL("http://www.google.com/avatar.png"), &bitmap); | |
156 const SkBitmap* actual_icon = | 145 const SkBitmap* actual_icon = |
157 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap(); | 146 GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap(); |
158 EXPECT_EQ(icon->width(), actual_icon->width()); | 147 EXPECT_EQ(icon->width(), actual_icon->width()); |
159 EXPECT_EQ(icon->height(), actual_icon->height()); | 148 EXPECT_EQ(icon->height(), actual_icon->height()); |
160 EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i)); | 149 EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i)); |
161 EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i)); | 150 EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i)); |
162 EXPECT_EQ(supervised_user_id, | 151 EXPECT_EQ(supervised_user_id, |
163 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i)); | 152 GetCache()->GetSupervisedUserIdOfProfileAtIndex(i)); |
164 } | 153 } |
165 | 154 |
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 for (size_t i = 0; i < 4; i++) | 539 for (size_t i = 0; i < 4; i++) |
551 ASSERT_FALSE(names[i].empty()); | 540 ASSERT_FALSE(names[i].empty()); |
552 } | 541 } |
553 | 542 |
554 // High res avatar downloading is only supported on desktop. | 543 // High res avatar downloading is only supported on desktop. |
555 #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS) | 544 #if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS) |
556 TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) { | 545 TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) { |
557 switches::EnableNewAvatarMenuForTesting( | 546 switches::EnableNewAvatarMenuForTesting( |
558 base::CommandLine::ForCurrentProcess()); | 547 base::CommandLine::ForCurrentProcess()); |
559 | 548 |
560 // The TestingProfileManager's ProfileInfoCache auto-downloads avatars. | 549 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles()); |
561 ProfileInfoCache profile_info_cache(g_browser_process->local_state(), | |
562 testing_profile_manager_.profile_manager()->user_data_dir()); | |
563 | |
564 // // Make sure there are no avatars already on disk. | |
565 const size_t kIconIndex = 0; | |
566 base::FilePath icon_path = | |
567 profiles::GetPathOfHighResAvatarAtIndex(kIconIndex); | |
568 EXPECT_TRUE(base::PathExists(icon_path)); | |
569 EXPECT_TRUE(base::DeleteFile(icon_path, true)); | |
570 EXPECT_FALSE(base::PathExists(icon_path)); | |
571 | |
572 EXPECT_EQ(0U, profile_info_cache.GetNumberOfProfiles()); | |
573 base::FilePath path_1 = GetProfilePath("path_1"); | 550 base::FilePath path_1 = GetProfilePath("path_1"); |
574 profile_info_cache.AddProfileToCache(path_1, ASCIIToUTF16("name_1"), | 551 GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"), |
575 base::string16(), kIconIndex, std::string()); | 552 base::string16(), 0, std::string()); |
576 EXPECT_EQ(1U, profile_info_cache.GetNumberOfProfiles()); | 553 EXPECT_EQ(1U, GetCache()->GetNumberOfProfiles()); |
577 base::RunLoop().RunUntilIdle(); | 554 base::RunLoop().RunUntilIdle(); |
578 | 555 |
579 // We haven't downloaded any high-res avatars yet. | 556 // We haven't downloaded any high-res avatars yet. |
580 EXPECT_EQ(0U, profile_info_cache.cached_avatar_images_.size()); | 557 EXPECT_EQ(0U, GetCache()->cached_avatar_images_.size()); |
581 | 558 |
582 // After adding a new profile, the download of high-res avatar will be | 559 // After adding a new profile, the download of high-res avatar will be |
583 // triggered if the flag kNewAvatarMenu has been set. But the downloader | 560 // triggered if the flag kNewAvatarMenu has been set. But the downloader |
584 // won't ever call OnFetchComplete in the test. | 561 // won't ever call OnFetchComplete in the test. |
585 EXPECT_EQ(1U, profile_info_cache.avatar_images_downloads_in_progress_.size()); | 562 EXPECT_EQ(1U, GetCache()->avatar_images_downloads_in_progress_.size()); |
586 | 563 |
587 EXPECT_FALSE(profile_info_cache.GetHighResAvatarOfProfileAtIndex(0)); | 564 EXPECT_FALSE(GetCache()->GetHighResAvatarOfProfileAtIndex(0)); |
588 | 565 |
589 // Simulate downloading a high-res avatar. | 566 // Simulate downloading a high-res avatar. |
| 567 const size_t kIconIndex = 0; |
590 ProfileAvatarDownloader avatar_downloader( | 568 ProfileAvatarDownloader avatar_downloader( |
591 kIconIndex, profile_info_cache.GetPathOfProfileAtIndex(0), | 569 kIconIndex, GetCache()->GetPathOfProfileAtIndex(0), GetCache()); |
592 &profile_info_cache); | |
593 | 570 |
594 // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels. | 571 // Put a real bitmap into "bitmap". 2x2 bitmap of green 32 bit pixels. |
595 SkBitmap bitmap; | 572 SkBitmap bitmap; |
596 bitmap.allocN32Pixels(2, 2); | 573 bitmap.allocN32Pixels(2, 2); |
597 bitmap.eraseColor(SK_ColorGREEN); | 574 bitmap.eraseColor(SK_ColorGREEN); |
598 | 575 |
599 avatar_downloader.OnFetchComplete( | 576 avatar_downloader.OnFetchComplete( |
600 GURL("http://www.google.com/avatar.png"), &bitmap); | 577 GURL("http://www.google.com/avatar.png"), &bitmap); |
601 | 578 |
602 // Now the download should not be in progress anymore. | 579 // Now the download should not be in progress anymore. |
603 EXPECT_EQ(0U, profile_info_cache.avatar_images_downloads_in_progress_.size()); | 580 EXPECT_EQ(0U, GetCache()->avatar_images_downloads_in_progress_.size()); |
604 | 581 |
605 std::string file_name = | 582 std::string file_name = |
606 profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex); | 583 profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex); |
607 | 584 |
608 // The file should have been cached and saved. | 585 // The file should have been cached and saved. |
609 EXPECT_EQ(1U, profile_info_cache.cached_avatar_images_.size()); | 586 EXPECT_EQ(1U, GetCache()->cached_avatar_images_.size()); |
610 EXPECT_TRUE(profile_info_cache.GetHighResAvatarOfProfileAtIndex(0)); | 587 EXPECT_TRUE(GetCache()->GetHighResAvatarOfProfileAtIndex(0)); |
611 EXPECT_EQ(profile_info_cache.cached_avatar_images_[file_name], | 588 EXPECT_EQ(GetCache()->cached_avatar_images_[file_name], |
612 profile_info_cache.GetHighResAvatarOfProfileAtIndex(0)); | 589 GetCache()->GetHighResAvatarOfProfileAtIndex(0)); |
613 | 590 |
614 // Make sure everything has completed, and the file has been written to disk. | 591 // Make sure everything has completed, and the file has been written to disk. |
615 base::RunLoop().RunUntilIdle(); | 592 base::RunLoop().RunUntilIdle(); |
616 | 593 |
617 // Clean up. | 594 // Clean up. |
| 595 base::FilePath icon_path = |
| 596 profiles::GetPathOfHighResAvatarAtIndex(kIconIndex); |
618 EXPECT_NE(std::string::npos, icon_path.MaybeAsASCII().find(file_name)); | 597 EXPECT_NE(std::string::npos, icon_path.MaybeAsASCII().find(file_name)); |
619 EXPECT_TRUE(base::PathExists(icon_path)); | 598 EXPECT_TRUE(base::PathExists(icon_path)); |
620 EXPECT_TRUE(base::DeleteFile(icon_path, true)); | 599 EXPECT_TRUE(base::DeleteFile(icon_path, true)); |
621 EXPECT_FALSE(base::PathExists(icon_path)); | 600 EXPECT_FALSE(base::PathExists(icon_path)); |
622 } | 601 } |
623 | 602 |
624 TEST_F(ProfileInfoCacheTest, MigrateLegacyProfileNamesWithNewAvatarMenu) { | 603 TEST_F(ProfileInfoCacheTest, MigrateLegacyProfileNamesWithNewAvatarMenu) { |
625 switches::EnableNewAvatarMenuForTesting( | 604 switches::EnableNewAvatarMenuForTesting( |
626 base::CommandLine::ForCurrentProcess()); | 605 base::CommandLine::ForCurrentProcess()); |
627 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles()); | 606 EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles()); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 // Profile names should have been preserved. | 677 // Profile names should have been preserved. |
699 EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex( | 678 EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex( |
700 GetCache()->GetIndexOfProfileWithPath(path_1))); | 679 GetCache()->GetIndexOfProfileWithPath(path_1))); |
701 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex( | 680 EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex( |
702 GetCache()->GetIndexOfProfileWithPath(path_2))); | 681 GetCache()->GetIndexOfProfileWithPath(path_2))); |
703 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex( | 682 EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex( |
704 GetCache()->GetIndexOfProfileWithPath(path_3))); | 683 GetCache()->GetIndexOfProfileWithPath(path_3))); |
705 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex( | 684 EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex( |
706 GetCache()->GetIndexOfProfileWithPath(path_4))); | 685 GetCache()->GetIndexOfProfileWithPath(path_4))); |
707 } | 686 } |
OLD | NEW |