Chromium Code Reviews| Index: chrome/browser/profiles/profile_info_cache_unittest.cc |
| diff --git a/chrome/browser/profiles/profile_info_cache_unittest.cc b/chrome/browser/profiles/profile_info_cache_unittest.cc |
| index be03de0e212b18eada802e2eb763b1749a28c62f..895f64b6612b7296d156508beb21140ca104ccdb 100644 |
| --- a/chrome/browser/profiles/profile_info_cache_unittest.cc |
| +++ b/chrome/browser/profiles/profile_info_cache_unittest.cc |
| @@ -8,9 +8,12 @@ |
| #include "base/utf_string_conversions.h" |
| #include "chrome/browser/browser_process.h" |
| #include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| #include "chrome/test/base/testing_browser_process.h" |
| #include "chrome/test/base/testing_pref_service.h" |
| #include "chrome/test/base/testing_profile_manager.h" |
| +#include "content/public/browser/notification_observer.h" |
| +#include "content/public/browser/notification_registrar.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "third_party/skia/include/core/SkBitmap.h" |
| #include "ui/base/resource/resource_bundle.h" |
| @@ -18,6 +21,71 @@ |
| namespace { |
| +bool IsEqual(const gfx::Image& image1, |
| + const gfx::Image& image2) { |
| + const SkBitmap& bmp1 = *image1.ToSkBitmap(); |
| + const SkBitmap& bmp2 = *image2.ToSkBitmap(); |
| + |
| + if (bmp1.width() != bmp2.width() || |
| + bmp1.height() != bmp2.height() || |
| + bmp1.config() != SkBitmap::kARGB_8888_Config || |
| + bmp2.config() != SkBitmap::kARGB_8888_Config) { |
| + return false; |
| + } |
| + |
| + SkAutoLockPixels lock1(bmp1); |
| + SkAutoLockPixels lock2(bmp2); |
| + if (!bmp1.getPixels() || !bmp2.getPixels()) |
| + return false; |
| + |
| + for (int y = 0; y < bmp1.height(); ++y) { |
| + for (int x = 0; x < bmp1.width(); ++x) { |
| + if (*bmp1.getAddr32(x,y) != *bmp2.getAddr32(x,y)) |
| + return false; |
| + } |
| + } |
| + |
| + return true; |
| +} |
| + |
| +gfx::Image CreateTestImage() { |
| + SkBitmap bitmap; |
| + bitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 50); |
| + bitmap.allocPixels(); |
| + bitmap.eraseRGB(0xff, 0, 0); |
| + return gfx::Image(new SkBitmap(bitmap)); |
| +} |
| + |
| +class WaitUntilNotification : public content::NotificationObserver { |
|
Paweł Hajdan Jr.
2011/11/17 09:56:19
Is this just trying to emulate WindowedNotificatio
sail
2011/11/21 23:52:47
Done.
|
| + public: |
| + explicit WaitUntilNotification(int type) |
| + : wait_started_(false), |
| + notification_received_(false) { |
| + registrar_.Add(this, type, content::NotificationService::AllSources()); |
| + } |
| + |
| + void Wait() { |
| + ASSERT_FALSE(wait_started_); |
| + wait_started_ = true; |
| + if (!notification_received_) |
| + MessageLoop::current()->Run(); |
| + } |
| + |
| + private: |
| + virtual void Observe(int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) OVERRIDE { |
| + notification_received_ = true; |
| + if (wait_started_) |
| + MessageLoop::current()->Quit(); |
| + } |
| + |
| + int type_; |
| + bool wait_started_; |
| + bool notification_received_; |
| + content::NotificationRegistrar registrar_; |
| +}; |
| + |
| class ProfileInfoCacheUnittests : public testing::Test { |
| protected: |
| ProfileInfoCacheUnittests() |
| @@ -45,6 +113,10 @@ class ProfileInfoCacheUnittests : public testing::Test { |
| #endif |
| } |
| + void ResetCache() { |
| + testing_profile_manager_.DeleteProfileInfoCache(); |
| + } |
| + |
| private: |
| TestingProfileManager testing_profile_manager_; |
| }; |
| @@ -60,7 +132,10 @@ TEST_F(ProfileInfoCacheUnittests, AddProfiles) { |
| const SkBitmap& icon = ResourceBundle::GetSharedInstance().GetImageNamed( |
| ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(i)); |
| - GetCache()->AddProfileToCache(profile_path, profile_name, string16(), 0); |
| + GetCache()->AddProfileToCache(profile_path, profile_name, string16(), i); |
| + GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true); |
| + string16 gaia_name = ASCIIToUTF16(StringPrintf("gaia_%ud", i)); |
| + GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name); |
| EXPECT_EQ(i + 1, GetCache()->GetNumberOfProfiles()); |
| EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
| @@ -69,6 +144,23 @@ TEST_F(ProfileInfoCacheUnittests, AddProfiles) { |
| EXPECT_EQ(icon.width(), actual_icon.width()); |
| EXPECT_EQ(icon.height(), actual_icon.height()); |
| } |
| + |
| + // Reset the cache and test the it reloads correctly. |
| + ResetCache(); |
| + |
| + EXPECT_EQ(4u, GetCache()->GetNumberOfProfiles()); |
| + for (uint32 i = 0; i < 4; ++i) { |
| + std::string base_name = StringPrintf("path_%ud", i); |
| + FilePath profile_path = |
| + GetUserDataDir().Append(StringToFilePath(base_name)); |
| + EXPECT_EQ(i, GetCache()->GetIndexOfProfileWithPath(profile_path)); |
| + string16 profile_name = ASCIIToUTF16(StringPrintf("name_%ud", i)); |
| + EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(i)); |
| + EXPECT_EQ(i, GetCache()->GetAvatarIconIndexOfProfileAtIndex(i)); |
| + EXPECT_EQ(true, GetCache()->GetBackgroundStatusOfProfileAtIndex(i)); |
| + string16 gaia_name = ASCIIToUTF16(StringPrintf("gaia_%ud", i)); |
| + EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(i)); |
| + } |
| } |
| TEST_F(ProfileInfoCacheUnittests, DeleteProfile) { |
| @@ -114,6 +206,46 @@ TEST_F(ProfileInfoCacheUnittests, MutateProfile) { |
| GetCache()->GetAvatarIconOfProfileAtIndex(1); |
| } |
| +TEST_F(ProfileInfoCacheUnittests, Sort) { |
| + string16 name_a = ASCIIToUTF16("apple"); |
| + GetCache()->AddProfileToCache(GetUserDataDir().Append( |
| + StringToFilePath("path_a")), name_a, string16(), 0); |
| + |
| + string16 name_c = ASCIIToUTF16("cat"); |
| + GetCache()->AddProfileToCache(GetUserDataDir().Append( |
| + StringToFilePath("path_c")), name_c, string16(), 0); |
| + |
| + // Sanity check the initial order. |
| + EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0)); |
| + EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1)); |
| + |
| + // Add a new profile (start with a capital to test case insensitive sorting. |
| + string16 name_b = ASCIIToUTF16("Banana"); |
| + GetCache()->AddProfileToCache(GetUserDataDir().Append( |
| + StringToFilePath("path_b")), name_b, string16(), 0); |
| + |
| + // Verify the new order. |
| + EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(0)); |
| + EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(1)); |
| + EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(2)); |
| + |
| + // Change the name of an existing profile. |
| + name_a = UTF8ToUTF16("dog"); |
| + GetCache()->SetNameOfProfileAtIndex(0, name_a); |
| + |
| + // Verify the new order. |
| + EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0)); |
| + EXPECT_EQ(name_c, GetCache()->GetNameOfProfileAtIndex(1)); |
| + EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(2)); |
| + |
| + // Delete a profile. |
| + GetCache()->DeleteProfileFromCache(StringToFilePath("path_c")); |
| + |
| + // Verify the new order. |
| + EXPECT_EQ(name_b, GetCache()->GetNameOfProfileAtIndex(0)); |
| + EXPECT_EQ(name_a, GetCache()->GetNameOfProfileAtIndex(1)); |
| +} |
| + |
| TEST_F(ProfileInfoCacheUnittests, BackgroundModeStatus) { |
| GetCache()->AddProfileToCache( |
| GetUserDataDir().Append(StringToFilePath("path_1")), |
| @@ -141,4 +273,141 @@ TEST_F(ProfileInfoCacheUnittests, BackgroundModeStatus) { |
| EXPECT_FALSE(GetCache()->GetBackgroundStatusOfProfileAtIndex(1)); |
| } |
| +TEST_F(ProfileInfoCacheUnittests, HasMigrated) { |
| + GetCache()->AddProfileToCache( |
| + GetUserDataDir().Append(StringToFilePath("path_1")), |
| + ASCIIToUTF16("name_1"), string16(), 0); |
| + GetCache()->AddProfileToCache( |
| + GetUserDataDir().Append(StringToFilePath("path_2")), |
| + ASCIIToUTF16("name_2"), string16(), 0); |
| + |
| + // Sanity check. |
| + EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0)); |
| + EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1)); |
| + |
| + // Set migrated state for 2nd profile. |
| + GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, true); |
| + EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0)); |
| + EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1)); |
| + |
| + // Set migrated state for 1st profile. |
| + GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(0, true); |
| + EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0)); |
| + EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1)); |
| + |
| + // Unset migrated state for 2nd profile. |
| + GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(1, false); |
| + EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(0)); |
| + EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(1)); |
| +} |
| + |
| +TEST_F(ProfileInfoCacheUnittests, GAIAName) { |
| + GetCache()->AddProfileToCache( |
| + GetUserDataDir().Append(StringToFilePath("path_1")), |
| + ASCIIToUTF16("name_1"), string16(), 0); |
| + string16 profile_name(ASCIIToUTF16("profile name 2")); |
| + GetCache()->AddProfileToCache( |
| + GetUserDataDir().Append(StringToFilePath("path_2")), |
| + profile_name, string16(), 0); |
| + |
| + // Sanity check. |
| + EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty()); |
| + EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(1).empty()); |
| + EXPECT_FALSE(GetCache()->GetIsUsingGAIANameOfProfileAtIndex(0)); |
| + EXPECT_FALSE(GetCache()->GetIsUsingGAIANameOfProfileAtIndex(1)); |
| + |
| + // Set GAIA name. |
| + string16 gaia_name(ASCIIToUTF16("Pat Smith")); |
| + GetCache()->SetGAIANameOfProfileAtIndex(1, gaia_name); |
| + EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty()); |
| + EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1)); |
| + EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1)); |
| + |
| + // Use GAIA name as profile name. |
| + GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, true); |
| + |
| + EXPECT_EQ(gaia_name, GetCache()->GetNameOfProfileAtIndex(1)); |
| + EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1)); |
| + |
| + // Don't use GAIA name as profile name. |
| + GetCache()->SetIsUsingGAIANameOfProfileAtIndex(1, false); |
| + EXPECT_EQ(profile_name, GetCache()->GetNameOfProfileAtIndex(1)); |
| + EXPECT_EQ(gaia_name, GetCache()->GetGAIANameOfProfileAtIndex(1)); |
| +} |
| + |
| +TEST_F(ProfileInfoCacheUnittests, GAIAPicture) { |
| + GetCache()->AddProfileToCache( |
| + GetUserDataDir().Append(StringToFilePath("path_1")), |
| + ASCIIToUTF16("name_1"), string16(), 0); |
| + GetCache()->AddProfileToCache( |
| + GetUserDataDir().Append(StringToFilePath("path_2")), |
| + ASCIIToUTF16("name_2"), string16(), 0); |
| + |
| + // Sanity check. |
| + EXPECT_TRUE( |
| + GetCache()->GetGAIAPictureOfProfileAtIndex(0).ToSkBitmap()->isNull()); |
| + EXPECT_TRUE( |
| + GetCache()->GetGAIAPictureOfProfileAtIndex(1).ToSkBitmap()->isNull()); |
| + EXPECT_FALSE(GetCache()->GetIsUsingGAIAPictureOfProfileAtIndex(0)); |
| + EXPECT_FALSE(GetCache()->GetIsUsingGAIAPictureOfProfileAtIndex(1)); |
| + |
| + // The profile icon should be the default one. |
| + int id = ProfileInfoCache::GetDefaultAvatarIconResourceIDAtIndex(0); |
| + const gfx::Image& profile_image( |
| + ResourceBundle::GetSharedInstance().GetImageNamed(id)); |
| + EXPECT_TRUE(IsEqual( |
| + profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| + |
| + // Set GAIA picture. |
| + gfx::Image gaia_image(CreateTestImage()); |
| + GetCache()->SetGAIAPictureOfProfileAtIndex(1, gaia_image); |
| + EXPECT_TRUE( |
| + GetCache()->GetGAIAPictureOfProfileAtIndex(0).ToSkBitmap()->isNull()); |
| + EXPECT_TRUE(IsEqual( |
| + gaia_image, GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
| + EXPECT_TRUE(IsEqual( |
| + profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| + |
| + // Use GAIA picture as profile picture. |
| + GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true); |
| + EXPECT_TRUE(IsEqual( |
| + gaia_image, GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
| + EXPECT_TRUE(IsEqual( |
| + gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| + |
| + // Don't use GAIA picture as profile picture. |
| + GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false); |
| + EXPECT_TRUE(IsEqual( |
| + gaia_image, GetCache()->GetGAIAPictureOfProfileAtIndex(1))); |
| + EXPECT_TRUE(IsEqual( |
| + profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1))); |
| +} |
| + |
| +TEST_F(ProfileInfoCacheUnittests, PersistGAIAPicture) { |
| + GetCache()->AddProfileToCache( |
| + GetUserDataDir().Append(StringToFilePath("path_1")), |
| + ASCIIToUTF16("name_1"), string16(), 0); |
| + gfx::Image gaia_image(CreateTestImage()); |
| + |
| + WaitUntilNotification save_wait( |
| + chrome::NOTIFICATION_PROFILE_CACHE_PICTURE_SAVED); |
| + GetCache()->SetGAIAPictureOfProfileAtIndex(0, gaia_image); |
| + EXPECT_TRUE(IsEqual( |
| + gaia_image, GetCache()->GetGAIAPictureOfProfileAtIndex(0))); |
| + |
| + // Wait for the file to be written to disk then reset the cache. |
| + save_wait.Wait(); |
| + ResetCache(); |
| + |
| + // Try to get the GAIA picture. This should return NULL until the read from |
| + // disk is done. |
| + WaitUntilNotification read_wait( |
| + chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED); |
| + EXPECT_TRUE( |
| + GetCache()->GetGAIAPictureOfProfileAtIndex(0).ToSkBitmap()->isNull()); |
| + read_wait.Wait(); |
| + EXPECT_TRUE(IsEqual( |
| + gaia_image, GetCache()->GetGAIAPictureOfProfileAtIndex(0))); |
| +} |
| + |
| } // namespace |