[Reland] Fix the decorating behavior of Chrome icon in the taskbar with Gaia image

This is a reland of issue 604849. The only change is migrating the unit test added
in profile_info_cache_unittest.cc from FILE thread to base/task_scheduler.

Original change's description:

Each profile has an icon, either one of built-in icons from Chrome, or from a G+
profile image (Gaia image). In the multi-profile scenario, the Chrome icon in the
taskbar is decorated with the profile icon.

There have been reports that when a profile's icon is from a G+ profile image, it
doesn't show up to decorate the Chrome icon sometimes. Instead, the built-in icon
shows up.

Here is the reason why this happens. The G+ profile image is read from disk
asynchronously during launching Chrome. Meanwhile, the program thinks there's no
such image so it uses the built-in icon to decorate the Chrome icon instead.

The correct behavior for the Gaia image decoration should be avoid displaying
anything until we've tried to load the image. If loading fails (because there is
no image) then we should fall back to a built-in icon. This CL implements this.

More code details and how this bug was introduced are discussed in
https://docs.google.com/document/d/1f40dVO3ScbjQzqdh9n6c5CZEmhNZXR66HBSV1Ix_8-M/edit#

Bug: 654499, 536553
Change-Id: Ib23898578833a37f6d66de491017c77a25ef1abf
Reviewed-on: https://chromium-review.googlesource.com/604849
Commit-Queue: Xi Cheng <[email protected]>
Reviewed-by: Scott Violet <[email protected]>
Reviewed-by: Anthony Vallee-Dubois <[email protected]>
Cr-Original-Commit-Position: refs/heads/master@{#494272}
Reviewed-on: https://chromium-review.googlesource.com/615217
Cr-Commit-Position: refs/heads/master@{#494528}
diff --git a/chrome/browser/profiles/profile_info_cache_unittest.cc b/chrome/browser/profiles/profile_info_cache_unittest.cc
index 59c193b..43db15b 100644
--- a/chrome/browser/profiles/profile_info_cache_unittest.cc
+++ b/chrome/browser/profiles/profile_info_cache_unittest.cc
@@ -18,6 +18,7 @@
 #include "base/time/time.h"
 #include "build/build_config.h"
 #include "chrome/browser/browser_process.h"
+#include "chrome/browser/profiles/avatar_menu.h"
 #include "chrome/browser/profiles/profile_avatar_downloader.h"
 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
 #include "chrome/browser/profiles/profile_info_cache.h"
@@ -775,6 +776,50 @@
   EXPECT_EQ(name_5, GetCache()->GetNameOfProfileAtIndex(
       GetCache()->GetIndexOfProfileWithPath(path_5)));
 }
+
+TEST_F(ProfileInfoCacheTest, GetGaiaImageForAvatarMenu) {
+  // The TestingProfileManager's ProfileInfoCache doesn't download avatars.
+  ProfileInfoCache profile_info_cache(
+      g_browser_process->local_state(),
+      testing_profile_manager_.profile_manager()->user_data_dir());
+
+  base::FilePath profile_path = GetProfilePath("path_1");
+
+  GetCache()->AddProfileToCache(profile_path, ASCIIToUTF16("name_1"),
+                                std::string(), base::string16(), 0,
+                                std::string());
+
+  gfx::Image gaia_image(gfx::test::CreateImage());
+  GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_image);
+
+  // Make sure everything has completed, and the file has been written to disk.
+  content::RunAllBlockingPoolTasksUntilIdle();
+
+  // Make sure this profile is using GAIA picture.
+  EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
+
+  ResetCache();
+
+  // We need to explicitly set the GAIA usage flag after resetting the cache.
+  GetCache()->SetIsUsingGAIAPictureOfProfileAtIndex(0, true);
+  EXPECT_TRUE(GetCache()->IsUsingGAIAPictureOfProfileAtIndex(0));
+
+  gfx::Image image_loaded;
+
+  // Try to get the GAIA image. For the first time, it triggers an async image
+  // load from disk. The load status indicates the image is still being loaded.
+  EXPECT_EQ(AvatarMenu::ImageLoadStatus::LOADING,
+            AvatarMenu::GetImageForMenuButton(profile_path, &image_loaded));
+  EXPECT_FALSE(gfx::test::AreImagesEqual(gaia_image, image_loaded));
+
+  // Wait until the async image load finishes.
+  content::RunAllBlockingPoolTasksUntilIdle();
+
+  // Since the GAIA image is loaded now, we can get it this time.
+  EXPECT_EQ(AvatarMenu::ImageLoadStatus::LOADED,
+            AvatarMenu::GetImageForMenuButton(profile_path, &image_loaded));
+  EXPECT_TRUE(gfx::test::AreImagesEqual(gaia_image, image_loaded));
+}
 #endif
 
 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)