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/browser/signin/signin_manager_factory.h" 14 #include "chrome/common/pref_names.h" 15 #include "chrome/test/base/testing_browser_process.h" 16 #include "chrome/test/base/testing_profile.h" 17 #include "chrome/test/base/testing_profile_manager.h" 18 #include "testing/gmock/include/gmock/gmock.h" 19 #include "ui/gfx/image/image.h" 20 #include "ui/gfx/image/image_unittest_util.h" 21 22 using ::testing::Return; 23 using ::testing::NiceMock; 24 25 namespace { 26 27 class ProfileDownloaderMock : public ProfileDownloader { 28 public: 29 explicit ProfileDownloaderMock(ProfileDownloaderDelegate* delegate) 30 : ProfileDownloader(delegate) { 31 } 32 33 virtual ~ProfileDownloaderMock() { 34 } 35 36 MOCK_CONST_METHOD0(GetProfileFullName, base::string16()); 37 MOCK_CONST_METHOD0(GetProfilePicture, SkBitmap()); 38 MOCK_CONST_METHOD0(GetProfilePictureStatus, 39 ProfileDownloader::PictureStatus()); 40 MOCK_CONST_METHOD0(GetProfilePictureURL, std::string()); 41 }; 42 43 class GAIAInfoUpdateServiceMock : public GAIAInfoUpdateService { 44 public: 45 explicit GAIAInfoUpdateServiceMock(Profile* profile) 46 : GAIAInfoUpdateService(profile) { 47 } 48 49 virtual ~GAIAInfoUpdateServiceMock() { 50 } 51 52 MOCK_METHOD0(Update, void()); 53 }; 54 55 class GAIAInfoUpdateServiceTest : public ProfileInfoCacheTest { 56 protected: 57 GAIAInfoUpdateServiceTest() : profile_(NULL) { 58 } 59 60 Profile* profile() { 61 if (!profile_) { 62 profile_ = testing_profile_manager_.CreateTestingProfile("Person 1"); 63 // The testing manager sets the profile name manually, which counts as 64 // a user-customized profile name. Reset this to match the default name 65 // we are actually using. 66 size_t index = GetCache()->GetIndexOfProfileWithPath(profile_->GetPath()); 67 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index, true); 68 } 69 return profile_; 70 } 71 72 NiceMock<GAIAInfoUpdateServiceMock>* service() { return service_.get(); } 73 NiceMock<ProfileDownloaderMock>* downloader() { return downloader_.get(); } 74 75 private: 76 virtual void SetUp() OVERRIDE; 77 virtual void TearDown() OVERRIDE; 78 79 Profile* profile_; 80 scoped_ptr<NiceMock<GAIAInfoUpdateServiceMock> > service_; 81 scoped_ptr<NiceMock<ProfileDownloaderMock> > downloader_; 82 }; 83 84 void GAIAInfoUpdateServiceTest::SetUp() { 85 ProfileInfoCacheTest::SetUp(); 86 service_.reset(new NiceMock<GAIAInfoUpdateServiceMock>(profile())); 87 downloader_.reset(new NiceMock<ProfileDownloaderMock>(service())); 88 } 89 90 void GAIAInfoUpdateServiceTest::TearDown() { 91 downloader_.reset(); 92 service_->Shutdown(); 93 service_.reset(); 94 ProfileInfoCacheTest::TearDown(); 95 } 96 97 } // namespace 98 99 TEST_F(GAIAInfoUpdateServiceTest, DownloadSuccess) { 100 base::string16 name = base::ASCIIToUTF16("Pat Smith"); 101 EXPECT_CALL(*downloader(), GetProfileFullName()).WillOnce(Return(name)); 102 gfx::Image image = gfx::test::CreateImage(); 103 const SkBitmap* bmp = image.ToSkBitmap(); 104 EXPECT_CALL(*downloader(), GetProfilePicture()).WillOnce(Return(*bmp)); 105 EXPECT_CALL(*downloader(), GetProfilePictureStatus()). 106 WillOnce(Return(ProfileDownloader::PICTURE_SUCCESS)); 107 std::string url("foo.com"); 108 EXPECT_CALL(*downloader(), GetProfilePictureURL()).WillOnce(Return(url)); 109 110 // No URL should be cached yet. 111 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); 112 113 service()->OnProfileDownloadSuccess(downloader()); 114 115 // On success both the profile info and GAIA info should be updated. 116 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); 117 EXPECT_EQ(name, GetCache()->GetNameOfProfileAtIndex(index)); 118 EXPECT_EQ(name, GetCache()->GetGAIANameOfProfileAtIndex(index)); 119 EXPECT_TRUE(gfx::test::IsEqual( 120 image, *GetCache()->GetGAIAPictureOfProfileAtIndex(index))); 121 EXPECT_EQ(url, service()->GetCachedPictureURL()); 122 } 123 124 TEST_F(GAIAInfoUpdateServiceTest, DownloadFailure) { 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 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); 130 131 service()->OnProfileDownloadFailure(downloader(), 132 ProfileDownloaderDelegate::SERVICE_ERROR); 133 134 // On failure nothing should be updated. 135 EXPECT_EQ(old_name, GetCache()->GetNameOfProfileAtIndex(index)); 136 EXPECT_EQ(base::string16(), GetCache()->GetGAIANameOfProfileAtIndex(index)); 137 EXPECT_TRUE(gfx::test::IsEqual( 138 old_image, GetCache()->GetAvatarIconOfProfileAtIndex(index))); 139 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(index)); 140 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); 141 } 142 143 TEST_F(GAIAInfoUpdateServiceTest, ShouldUseGAIAProfileInfo) { 144 #if defined(OS_CHROMEOS) 145 // This feature should never be enabled on ChromeOS. 146 EXPECT_FALSE(GAIAInfoUpdateService::ShouldUseGAIAProfileInfo(profile())); 147 #endif 148 } 149 150 TEST_F(GAIAInfoUpdateServiceTest, ScheduleUpdate) { 151 EXPECT_TRUE(service()->timer_.IsRunning()); 152 service()->timer_.Stop(); 153 EXPECT_FALSE(service()->timer_.IsRunning()); 154 service()->ScheduleNextUpdate(); 155 EXPECT_TRUE(service()->timer_.IsRunning()); 156 } 157 158 #if !defined(OS_CHROMEOS) 159 160 TEST_F(GAIAInfoUpdateServiceTest, LogOut) { 161 SigninManager* signin_manager = 162 SigninManagerFactory::GetForProfile(profile()); 163 signin_manager->SetAuthenticatedUsername("pat (at) example.com"); 164 base::string16 gaia_name = base::UTF8ToUTF16("Pat Foo"); 165 GetCache()->SetGAIANameOfProfileAtIndex(0, gaia_name); 166 gfx::Image gaia_picture = gfx::test::CreateImage(); 167 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_picture); 168 169 // Set a fake picture URL. 170 profile()->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL, 171 "example.com"); 172 173 EXPECT_FALSE(service()->GetCachedPictureURL().empty()); 174 175 // Log out. 176 signin_manager->SignOut(signin_metrics::SIGNOUT_TEST); 177 // Verify that the GAIA name and picture, and picture URL are unset. 178 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty()); 179 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); 180 EXPECT_TRUE(service()->GetCachedPictureURL().empty()); 181 } 182 183 TEST_F(GAIAInfoUpdateServiceTest, LogIn) { 184 // Log in. 185 EXPECT_CALL(*service(), Update()); 186 SigninManager* signin_manager = 187 SigninManagerFactory::GetForProfile(profile()); 188 signin_manager->OnExternalSigninCompleted("pat (at) example.com"); 189 } 190 191 #endif 192