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. 4 5 #include "chrome/browser/profiles/gaia_info_update_service.h" 6 7 #include "base/prefs/pref_service.h" 8 #include "base/strings/utf_string_conversions.h" 9 #include "chrome/browser/browser_process.h" 10 #include "chrome/browser/profiles/profile_downloader.h" 11 #include "chrome/browser/profiles/profile_info_cache.h" 12 #include "chrome/browser/profiles/profile_info_cache_unittest.h" 13 #include "chrome/common/pref_names.h" 14 #include "chrome/test/base/testing_browser_process.h" 15 #include "chrome/test/base/testing_profile.h" 16 #include "chrome/test/base/testing_profile_manager.h" 17 #include "testing/gmock/include/gmock/gmock.h" 18 #include "ui/gfx/image/image.h" 19 #include "ui/gfx/image/image_unittest_util.h" 20 21 using ::testing::Return; 22 using ::testing::NiceMock; 23 24 namespace { 25 26 class ProfileDownloaderMock : public ProfileDownloader { 27 public: 28 explicit ProfileDownloaderMock(ProfileDownloaderDelegate* delegate) 29 : ProfileDownloader(delegate) { 30 } 31 32 virtual ~ProfileDownloaderMock() { 33 } 34 35 MOCK_CONST_METHOD0(GetProfileFullName, base::string16()); 36 MOCK_CONST_METHOD0(GetProfilePicture, SkBitmap()); 37 MOCK_CONST_METHOD0(GetProfilePictureStatus, 38 ProfileDownloader::PictureStatus()); 39 MOCK_CONST_METHOD0(GetProfilePictureURL, std::string()); 40 }; 41 42 class GAIAInfoUpdateServiceMock : public GAIAInfoUpdateService { 43 public: 44 explicit GAIAInfoUpdateServiceMock(Profile* profile) 45 : GAIAInfoUpdateService(profile) { 46 } 47 48 virtual ~GAIAInfoUpdateServiceMock() { 49 } 50 51 MOCK_METHOD0(Update, void()); 52 }; 53 54 class GAIAInfoUpdateServiceTest : public ProfileInfoCacheTest { 55 protected: 56 GAIAInfoUpdateServiceTest() : profile_(NULL) { 57 } 58 59 Profile* profile() { 60 if (!profile_) 61 profile_ = testing_profile_manager_.CreateTestingProfile("profile_1"); 62 return profile_; 63 } 64 65 private: 66 Profile* profile_; 67 }; 68 69 } // namespace 70 71 TEST_F(GAIAInfoUpdateServiceTest, DownloadSuccess) { 72 GAIAInfoUpdateService service(profile()); 73 NiceMock<ProfileDownloaderMock> downloader(&service); 74 75 base::string16 name = ASCIIToUTF16("Pat Smith"); 76 EXPECT_CALL(downloader, GetProfileFullName()).WillOnce(Return(name)); 77 gfx::Image image = gfx::test::CreateImage(); 78 const SkBitmap* bmp = image.ToSkBitmap(); 79 EXPECT_CALL(downloader, GetProfilePicture()).WillOnce(Return(*bmp)); 80 EXPECT_CALL(downloader, GetProfilePictureStatus()). 81 WillOnce(Return(ProfileDownloader::PICTURE_SUCCESS)); 82 std::string url("foo.com"); 83 EXPECT_CALL(downloader, GetProfilePictureURL()).WillOnce(Return(url)); 84 85 // No URL should be cached yet. 86 EXPECT_EQ(std::string(), service.GetCachedPictureURL()); 87 88 service.OnProfileDownloadSuccess(&downloader); 89 90 // On success both the profile info and GAIA info should be updated. 91 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); 92 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(index)); 93 EXPECT_EQ(name, GetCache()->GetNameOfProfileAtIndex(index)); 94 EXPECT_EQ(name, GetCache()->GetGAIANameOfProfileAtIndex(index)); 95 EXPECT_TRUE(gfx::test::IsEqual( 96 image, GetCache()->GetAvatarIconOfProfileAtIndex(index))); 97 EXPECT_TRUE(gfx::test::IsEqual( 98 image, *GetCache()->GetGAIAPictureOfProfileAtIndex(index))); 99 EXPECT_EQ(url, service.GetCachedPictureURL()); 100 } 101 102 TEST_F(GAIAInfoUpdateServiceTest, DownloadFailure) { 103 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); 104 base::string16 old_name = GetCache()->GetNameOfProfileAtIndex(index); 105 gfx::Image old_image = GetCache()->GetAvatarIconOfProfileAtIndex(index); 106 107 GAIAInfoUpdateService service(profile()); 108 EXPECT_EQ(std::string(), service.GetCachedPictureURL()); 109 NiceMock<ProfileDownloaderMock> downloader(&service); 110 111 service.OnProfileDownloadFailure(&downloader, 112 ProfileDownloaderDelegate::SERVICE_ERROR); 113 114 // On failure nothing should be updated. 115 EXPECT_FALSE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(index)); 116 EXPECT_EQ(old_name, GetCache()->GetNameOfProfileAtIndex(index)); 117 EXPECT_EQ(base::string16(), GetCache()->GetGAIANameOfProfileAtIndex(index)); 118 EXPECT_TRUE(gfx::test::IsEqual( 119 old_image, GetCache()->GetAvatarIconOfProfileAtIndex(index))); 120 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(index)); 121 EXPECT_EQ(std::string(), service.GetCachedPictureURL()); 122 } 123 124 TEST_F(GAIAInfoUpdateServiceTest, NoMigration) { 125 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); 126 base::string16 old_name = GetCache()->GetNameOfProfileAtIndex(index); 127 gfx::Image old_image = GetCache()->GetAvatarIconOfProfileAtIndex(index); 128 129 // Mark the profile as migrated. 130 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(index, true); 131 132 GAIAInfoUpdateService service(profile()); 133 NiceMock<ProfileDownloaderMock> downloader(&service); 134 base::string16 new_name = ASCIIToUTF16("Pat Smith"); 135 EXPECT_CALL(downloader, GetProfileFullName()).WillOnce(Return(new_name)); 136 gfx::Image new_image = gfx::test::CreateImage(); 137 const SkBitmap* new_bmp = new_image.ToSkBitmap(); 138 EXPECT_CALL(downloader, GetProfilePicture()).WillOnce(Return(*new_bmp)); 139 EXPECT_CALL(downloader, GetProfilePictureStatus()). 140 WillOnce(Return(ProfileDownloader::PICTURE_SUCCESS)); 141 EXPECT_CALL(downloader, GetProfilePictureURL()).WillOnce(Return("")); 142 143 service.OnProfileDownloadSuccess(&downloader); 144 145 // On success with no migration the profile info should not be updated but 146 // the GAIA info should be updated. 147 EXPECT_TRUE(GetCache()->GetHasMigratedToGAIAInfoOfProfileAtIndex(index)); 148 EXPECT_EQ(old_name, GetCache()->GetNameOfProfileAtIndex(index)); 149 EXPECT_EQ(new_name, GetCache()->GetGAIANameOfProfileAtIndex(index)); 150 EXPECT_TRUE(gfx::test::IsEqual( 151 old_image, GetCache()->GetAvatarIconOfProfileAtIndex(index))); 152 EXPECT_TRUE(gfx::test::IsEqual( 153 new_image, *GetCache()->GetGAIAPictureOfProfileAtIndex(index))); 154 } 155 156 TEST_F(GAIAInfoUpdateServiceTest, ShouldUseGAIAProfileInfo) { 157 #if defined(OS_CHROMEOS) 158 // This feature should never be enabled on ChromeOS. 159 EXPECT_FALSE(GAIAInfoUpdateService::ShouldUseGAIAProfileInfo(profile())); 160 #endif 161 } 162 163 TEST_F(GAIAInfoUpdateServiceTest, ScheduleUpdate) { 164 GAIAInfoUpdateService service(profile()); 165 EXPECT_TRUE(service.timer_.IsRunning()); 166 service.timer_.Stop(); 167 EXPECT_FALSE(service.timer_.IsRunning()); 168 service.ScheduleNextUpdate(); 169 EXPECT_TRUE(service.timer_.IsRunning()); 170 } 171 172 TEST_F(GAIAInfoUpdateServiceTest, LogOut) { 173 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername, 174 "pat (at) example.com"); 175 base::string16 gaia_name = UTF8ToUTF16("Pat Foo"); 176 GetCache()->SetGAIANameOfProfileAtIndex(0, gaia_name); 177 gfx::Image gaia_picture = gfx::test::CreateImage(); 178 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_picture); 179 180 // Set a fake picture URL. 181 profile()->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL, 182 "example.com"); 183 184 GAIAInfoUpdateService service(profile()); 185 EXPECT_FALSE(service.GetCachedPictureURL().empty()); 186 // Log out. 187 profile()->GetPrefs() 188 ->SetString(prefs::kGoogleServicesUsername, std::string()); 189 190 // Verify that the GAIA name and picture, and picture URL are unset. 191 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty()); 192 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); 193 EXPECT_TRUE(service.GetCachedPictureURL().empty()); 194 } 195 196 TEST_F(GAIAInfoUpdateServiceTest, LogIn) { 197 profile()->GetPrefs() 198 ->SetString(prefs::kGoogleServicesUsername, std::string()); 199 GAIAInfoUpdateServiceMock service(profile()); 200 201 // Log in. 202 EXPECT_CALL(service, Update()); 203 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername, 204 "pat (at) example.com"); 205 } 206