Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / profile_info_cache_unittest.cc
index d1a39f9..d465c5a 100644 (file)
@@ -6,7 +6,8 @@
 
 #include <vector>
 
-#include "base/file_util.h"
+#include "base/command_line.h"
+#include "base/files/file_util.h"
 #include "base/prefs/testing_pref_service.h"
 #include "base/strings/stringprintf.h"
 #include "base/strings/utf_string_conversions.h"
@@ -18,7 +19,9 @@
 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
 #include "chrome/browser/profiles/profile_info_cache.h"
 #include "chrome/browser/profiles/profile_manager.h"
+#include "chrome/common/chrome_switches.h"
 #include "chrome/test/base/testing_browser_process.h"
+#include "components/signin/core/common/profile_management_switches.h"
 #include "content/public/browser/notification_observer.h"
 #include "content/public/browser/notification_registrar.h"
 #include "content/public/browser/notification_service.h"
@@ -131,10 +134,10 @@ TEST_F(ProfileInfoCacheTest, AddProfiles) {
     const SkBitmap* icon = rb.GetImageNamed(
         profiles::GetDefaultAvatarIconResourceIDAtIndex(
             i)).ToSkBitmap();
-    std::string managed_user_id = i == 3 ? "TEST_ID" : "";
+    std::string supervised_user_id = i == 3 ? "TEST_ID" : "";
 
     GetCache()->AddProfileToCache(profile_path, profile_name, base::string16(),
-                                  i, managed_user_id);
+                                  i, supervised_user_id);
     GetCache()->SetBackgroundStatusOfProfileAtIndex(i, true);
     base::string16 gaia_name = ASCIIToUTF16(base::StringPrintf("gaia_%ud", i));
     GetCache()->SetGAIANameOfProfileAtIndex(i, gaia_name);
@@ -146,9 +149,10 @@ TEST_F(ProfileInfoCacheTest, AddProfiles) {
         GetCache()->GetAvatarIconOfProfileAtIndex(i).ToSkBitmap();
     EXPECT_EQ(icon->width(), actual_icon->width());
     EXPECT_EQ(icon->height(), actual_icon->height());
-    EXPECT_EQ(i == 3, GetCache()->ProfileIsManagedAtIndex(i));
+    EXPECT_EQ(i == 3, GetCache()->ProfileIsSupervisedAtIndex(i));
     EXPECT_EQ(i == 3, GetCache()->IsOmittedProfileAtIndex(i));
-    EXPECT_EQ(managed_user_id, GetCache()->GetManagedUserIdOfProfileAtIndex(i));
+    EXPECT_EQ(supervised_user_id,
+              GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
   }
 
   // Reset the cache and test the it reloads correctly.
@@ -330,6 +334,8 @@ TEST_F(ProfileInfoCacheTest, GAIAName) {
   // Don't use GAIA name as profile name. This re-sorts the cache.
   base::string16 custom_name(ASCIIToUTF16("Custom name"));
   GetCache()->SetNameOfProfileAtIndex(index2, custom_name);
+  GetCache()->SetProfileIsUsingDefaultNameAtIndex(index2, false);
+
   index1 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_1"));
   index2 = GetCache()->GetIndexOfProfileWithPath(GetProfilePath("path_2"));
 
@@ -338,12 +344,15 @@ TEST_F(ProfileInfoCacheTest, GAIAName) {
 }
 
 TEST_F(ProfileInfoCacheTest, GAIAPicture) {
+  const int kDefaultAvatarIndex = 0;
+  const int kOtherAvatarIndex = 1;
+  const int kGaiaPictureSize = 256;  // Standard size of a Gaia account picture.
   GetCache()->AddProfileToCache(
       GetProfilePath("path_1"), ASCIIToUTF16("name_1"),
-      base::string16(), 0, std::string());
+      base::string16(), kDefaultAvatarIndex, std::string());
   GetCache()->AddProfileToCache(
       GetProfilePath("path_2"), ASCIIToUTF16("name_2"),
-      base::string16(), 0, std::string());
+      base::string16(), kDefaultAvatarIndex, std::string());
 
   // Sanity check.
   EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
@@ -352,34 +361,57 @@ TEST_F(ProfileInfoCacheTest, GAIAPicture) {
   EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
 
   // The profile icon should be the default one.
-  int id = profiles::GetDefaultAvatarIconResourceIDAtIndex(0);
-  const gfx::Image& profile_image(
-      ResourceBundle::GetSharedInstance().GetImageNamed(id));
+  EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(0));
+  EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
+  int default_avatar_id =
+      profiles::GetDefaultAvatarIconResourceIDAtIndex(kDefaultAvatarIndex);
+  const gfx::Image& default_avatar_image(
+      ResourceBundle::GetSharedInstance().GetImageNamed(default_avatar_id));
   EXPECT_TRUE(gfx::test::IsEqual(
-      profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
+      default_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
 
   // Set GAIA picture.
-  gfx::Image gaia_image(gfx::test::CreateImage());
+  gfx::Image gaia_image(gfx::test::CreateImage(
+      kGaiaPictureSize, kGaiaPictureSize));
   GetCache()->SetGAIAPictureOfProfileAtIndex(1, &gaia_image);
   EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
   EXPECT_TRUE(gfx::test::IsEqual(
       gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
+  // Since we're still using the default avatar, the GAIA image should be
+  // preferred over the generic avatar image.
+  EXPECT_TRUE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
+  EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
   EXPECT_TRUE(gfx::test::IsEqual(
-      profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
+      gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
 
-  // Use GAIA picture as profile picture.
+  // Set a non-default avatar. This should be preferred over the GAIA image.
+  GetCache()->SetAvatarIconOfProfileAtIndex(1, kOtherAvatarIndex);
+  GetCache()->SetProfileIsUsingDefaultAvatarAtIndex(1, false);
+  EXPECT_FALSE(GetCache()->ProfileIsUsingDefaultAvatarAtIndex(1));
+  EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
+  int other_avatar_id =
+      profiles::GetDefaultAvatarIconResourceIDAtIndex(kOtherAvatarIndex);
+  const gfx::Image& other_avatar_image(
+      ResourceBundle::GetSharedInstance().GetImageNamed(other_avatar_id));
+  EXPECT_TRUE(gfx::test::IsEqual(
+      other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
+
+  // Explicitly setting the GAIA picture should make it preferred again.
   GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, true);
+  EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
   EXPECT_TRUE(gfx::test::IsEqual(
       gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
   EXPECT_TRUE(gfx::test::IsEqual(
       gaia_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
 
-  // Don't use GAIA picture as profile picture.
+  // Clearing the IsUsingGAIAPicture flag should result in the generic image
+  // being used again.
   GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(1, false);
+  EXPECT_FALSE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(1));
   EXPECT_TRUE(gfx::test::IsEqual(
       gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(1)));
   EXPECT_TRUE(gfx::test::IsEqual(
-      profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
+      other_avatar_image, GetCache()->GetAvatarIconOfProfileAtIndex(1)));
 }
 
 TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
@@ -410,22 +442,22 @@ TEST_F(ProfileInfoCacheTest, PersistGAIAPicture) {
     gaia_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(0)));
 }
 
-TEST_F(ProfileInfoCacheTest, SetManagedUserId) {
+TEST_F(ProfileInfoCacheTest, SetSupervisedUserId) {
   GetCache()->AddProfileToCache(
       GetProfilePath("test"), ASCIIToUTF16("Test"),
       base::string16(), 0, std::string());
-  EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0));
+  EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
 
-  GetCache()->SetManagedUserIdOfProfileAtIndex(0, "TEST_ID");
-  EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0));
-  EXPECT_EQ("TEST_ID", GetCache()->GetManagedUserIdOfProfileAtIndex(0));
+  GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, "TEST_ID");
+  EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
+  EXPECT_EQ("TEST_ID", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
 
   ResetCache();
-  EXPECT_TRUE(GetCache()->ProfileIsManagedAtIndex(0));
+  EXPECT_TRUE(GetCache()->ProfileIsSupervisedAtIndex(0));
 
-  GetCache()->SetManagedUserIdOfProfileAtIndex(0, std::string());
-  EXPECT_FALSE(GetCache()->ProfileIsManagedAtIndex(0));
-  EXPECT_EQ("", GetCache()->GetManagedUserIdOfProfileAtIndex(0));
+  GetCache()->SetSupervisedUserIdOfProfileAtIndex(0, std::string());
+  EXPECT_FALSE(GetCache()->ProfileIsSupervisedAtIndex(0));
+  EXPECT_EQ("", GetCache()->GetSupervisedUserIdOfProfileAtIndex(0));
 }
 
 TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
@@ -449,23 +481,24 @@ TEST_F(ProfileInfoCacheTest, EmptyGAIAInfo) {
       profile_image, GetCache()->GetAvatarIconOfProfileAtIndex(0)));
 }
 
-TEST_F(ProfileInfoCacheTest, CreateManagedTestingProfile) {
+TEST_F(ProfileInfoCacheTest, CreateSupervisedTestingProfile) {
   testing_profile_manager_.CreateTestingProfile("default");
-  base::string16 managed_user_name = ASCIIToUTF16("Supervised User");
+  base::string16 supervised_user_name = ASCIIToUTF16("Supervised User");
   testing_profile_manager_.CreateTestingProfile(
       "test1", scoped_ptr<PrefServiceSyncable>(),
-      managed_user_name, 0, "TEST_ID", TestingProfile::TestingFactories());
+      supervised_user_name, 0, "TEST_ID", TestingProfile::TestingFactories());
   for (size_t i = 0; i < GetCache()->GetNumberOfProfiles(); i++) {
-    bool is_managed =
-        GetCache()->GetNameOfProfileAtIndex(i) == managed_user_name;
-    EXPECT_EQ(is_managed, GetCache()->ProfileIsManagedAtIndex(i));
-    std::string managed_user_id = is_managed ? "TEST_ID" : "";
-    EXPECT_EQ(managed_user_id, GetCache()->GetManagedUserIdOfProfileAtIndex(i));
+    bool is_supervised =
+        GetCache()->GetNameOfProfileAtIndex(i) == supervised_user_name;
+    EXPECT_EQ(is_supervised, GetCache()->ProfileIsSupervisedAtIndex(i));
+    std::string supervised_user_id = is_supervised ? "TEST_ID" : "";
+    EXPECT_EQ(supervised_user_id,
+              GetCache()->GetSupervisedUserIdOfProfileAtIndex(i));
   }
 
-  // Managed profiles have a custom theme, which needs to be deleted on the FILE
-  // thread. Reset the profile manager now so everything is deleted while we
-  // still have a FILE thread.
+  // Supervised profiles have a custom theme, which needs to be deleted on the
+  // FILE thread. Reset the profile manager now so everything is deleted while
+  // we still have a FILE thread.
   TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
 }
 
@@ -483,7 +516,7 @@ TEST_F(ProfileInfoCacheTest, AddStubProfile) {
     { "path_test3", "name_3" },
   };
 
-  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
+  for (size_t i = 0; i < arraysize(kTestCases); ++i) {
     base::FilePath profile_path = GetProfilePath(kTestCases[i].profile_path);
     base::string16 profile_name = ASCIIToUTF16(kTestCases[i].profile_name);
 
@@ -502,7 +535,11 @@ TEST_F(ProfileInfoCacheTest, AddStubProfile) {
     ASSERT_FALSE(names[i].empty());
 }
 
+// High res avatar downloading is only supported on desktop.
+#if !defined(OS_ANDROID) && !defined(OS_IOS) && !defined(OS_CHROMEOS)
 TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
+  switches::EnableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess());
+
   EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
   base::FilePath path_1 = GetProfilePath("path_1");
   GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("name_1"),
@@ -511,17 +548,22 @@ TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
 
   // We haven't downloaded any high-res avatars yet.
   EXPECT_EQ(0U, GetCache()->cached_avatar_images_.size());
-  EXPECT_EQ(0U, GetCache()->avatar_images_downloads_in_progress_.size());
+
+  // After adding a new profile, the download of high-res avatar will be
+  // triggered if the flag kNewAvatarMenu has been set. But the downloader
+  // won't ever call OnFetchComplete in the test.
+  EXPECT_EQ(1U, GetCache()->avatar_images_downloads_in_progress_.size());
+
   EXPECT_FALSE(GetCache()->GetHighResAvatarOfProfileAtIndex(0));
 
   // Simulate downloading a high-res avatar.
   const size_t kIconIndex = 0;
-  ProfileAvatarDownloader avatar_downloader(kIconIndex, GetCache());
+  ProfileAvatarDownloader avatar_downloader(
+      kIconIndex, GetCache()->GetPathOfProfileAtIndex(0), GetCache());
 
   // Put a real bitmap into "bitmap".  2x2 bitmap of green 32 bit pixels.
   SkBitmap bitmap;
-  bitmap.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
-  bitmap.allocPixels();
+  bitmap.allocN32Pixels(2, 2);
   bitmap.eraseColor(SK_ColorGREEN);
 
   avatar_downloader.OnFetchComplete(
@@ -531,7 +573,7 @@ TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
       profiles::GetDefaultAvatarIconFileNameAtIndex(kIconIndex);
 
   // The file should have been cached and saved.
-  EXPECT_EQ(0U, GetCache()->avatar_images_downloads_in_progress_.size());
+  EXPECT_EQ(1U, GetCache()->avatar_images_downloads_in_progress_.size());
   EXPECT_EQ(1U, GetCache()->cached_avatar_images_.size());
   EXPECT_TRUE(GetCache()->GetHighResAvatarOfProfileAtIndex(0));
   EXPECT_EQ(GetCache()->cached_avatar_images_[file_name],
@@ -548,3 +590,87 @@ TEST_F(ProfileInfoCacheTest, DownloadHighResAvatarTest) {
   EXPECT_TRUE(base::DeleteFile(icon_path, true));
   EXPECT_FALSE(base::PathExists(icon_path));
 }
+
+TEST_F(ProfileInfoCacheTest, MigrateLegacyProfileNamesWithNewAvatarMenu) {
+  switches::EnableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess());
+  EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
+
+  base::FilePath path_1 = GetProfilePath("path_1");
+  GetCache()->AddProfileToCache(path_1, ASCIIToUTF16("Default Profile"),
+                                base::string16(), 0, std::string());
+  base::FilePath path_2 = GetProfilePath("path_2");
+  GetCache()->AddProfileToCache(path_2, ASCIIToUTF16("First user"),
+                                base::string16(), 1, std::string());
+  base::string16 name_3 = ASCIIToUTF16("Lemonade");
+  base::FilePath path_3 = GetProfilePath("path_3");
+  GetCache()->AddProfileToCache(path_3, name_3,
+                                base::string16(), 2, std::string());
+  base::string16 name_4 = ASCIIToUTF16("Batman");
+  base::FilePath path_4 = GetProfilePath("path_4");
+  GetCache()->AddProfileToCache(path_4, name_4,
+                                base::string16(), 3, std::string());
+  base::string16 name_5 = ASCIIToUTF16("Person 2");
+  base::FilePath path_5 = GetProfilePath("path_5");
+  GetCache()->AddProfileToCache(path_5, name_5,
+                                base::string16(), 2, std::string());
+
+  EXPECT_EQ(5U, GetCache()->GetNumberOfProfiles());
+
+
+  ResetCache();
+
+  // Legacy profile names like "Default Profile" and "First user" should be
+  // migrated to "Person %n" type names.
+  EXPECT_EQ(ASCIIToUTF16("Person 1"), GetCache()->GetNameOfProfileAtIndex(
+      GetCache()->GetIndexOfProfileWithPath(path_1)));
+  EXPECT_EQ(ASCIIToUTF16("Person 3"), GetCache()->GetNameOfProfileAtIndex(
+      GetCache()->GetIndexOfProfileWithPath(path_2)));
+
+  // Other profile names should not be migrated even if they're the old
+  // default cartoon profile names.
+  EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
+      GetCache()->GetIndexOfProfileWithPath(path_3)));
+  EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
+      GetCache()->GetIndexOfProfileWithPath(path_4)));
+  EXPECT_EQ(name_5, GetCache()->GetNameOfProfileAtIndex(
+      GetCache()->GetIndexOfProfileWithPath(path_5)));
+}
+#endif
+
+TEST_F(ProfileInfoCacheTest,
+       DontMigrateLegacyProfileNamesWithoutNewAvatarMenu) {
+  switches::DisableNewAvatarMenuForTesting(CommandLine::ForCurrentProcess());
+
+  EXPECT_EQ(0U, GetCache()->GetNumberOfProfiles());
+
+  base::string16 name_1 = ASCIIToUTF16("Default Profile");
+  base::FilePath path_1 = GetProfilePath("path_1");
+  GetCache()->AddProfileToCache(path_1, name_1,
+                                base::string16(), 0, std::string());
+  base::string16 name_2 = ASCIIToUTF16("First user");
+  base::FilePath path_2 = GetProfilePath("path_2");
+  GetCache()->AddProfileToCache(path_2, name_2,
+                                base::string16(), 1, std::string());
+  base::string16 name_3 = ASCIIToUTF16("Lemonade");
+  base::FilePath path_3 = GetProfilePath("path_3");
+  GetCache()->AddProfileToCache(path_3, name_3,
+                                base::string16(), 2, std::string());
+  base::string16 name_4 = ASCIIToUTF16("Batman");
+  base::FilePath path_4 = GetProfilePath("path_4");
+  GetCache()->AddProfileToCache(path_4, name_4,
+                                base::string16(), 3, std::string());
+  EXPECT_EQ(4U, GetCache()->GetNumberOfProfiles());
+
+  ResetCache();
+
+  // Profile names should have been preserved.
+  EXPECT_EQ(name_1, GetCache()->GetNameOfProfileAtIndex(
+      GetCache()->GetIndexOfProfileWithPath(path_1)));
+  EXPECT_EQ(name_2, GetCache()->GetNameOfProfileAtIndex(
+      GetCache()->GetIndexOfProfileWithPath(path_2)));
+  EXPECT_EQ(name_3, GetCache()->GetNameOfProfileAtIndex(
+      GetCache()->GetIndexOfProfileWithPath(path_3)));
+  EXPECT_EQ(name_4, GetCache()->GetNameOfProfileAtIndex(
+      GetCache()->GetIndexOfProfileWithPath(path_4)));
+}
+