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.
5 #include "chrome/browser/profiles/gaia_info_update_service.h"
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"
21 using ::testing::Return;
22 using ::testing::NiceMock;
26 class ProfileDownloaderMock : public ProfileDownloader {
28 explicit ProfileDownloaderMock(ProfileDownloaderDelegate* delegate)
29 : ProfileDownloader(delegate) {
32 virtual ~ProfileDownloaderMock() {
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());
42 class GAIAInfoUpdateServiceMock : public GAIAInfoUpdateService {
44 explicit GAIAInfoUpdateServiceMock(Profile* profile)
45 : GAIAInfoUpdateService(profile) {
48 virtual ~GAIAInfoUpdateServiceMock() {
51 MOCK_METHOD0(Update, void());
54 class GAIAInfoUpdateServiceTest : public ProfileInfoCacheTest {
56 GAIAInfoUpdateServiceTest() : profile_(NULL) {
61 profile_ = testing_profile_manager_.CreateTestingProfile("Person 1");
62 // The testing manager sets the profile name manually, which counts as
63 // a user-customized profile name. Reset this to match the default name
64 // we are actually using.
65 size_t index = GetCache()->GetIndexOfProfileWithPath(profile_->GetPath());
66 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index, true);
77 TEST_F(GAIAInfoUpdateServiceTest, DownloadSuccess) {
78 GAIAInfoUpdateService service(profile());
79 NiceMock<ProfileDownloaderMock> downloader(&service);
81 base::string16 name = base::ASCIIToUTF16("Pat Smith");
82 EXPECT_CALL(downloader, GetProfileFullName()).WillOnce(Return(name));
83 gfx::Image image = gfx::test::CreateImage();
84 const SkBitmap* bmp = image.ToSkBitmap();
85 EXPECT_CALL(downloader, GetProfilePicture()).WillOnce(Return(*bmp));
86 EXPECT_CALL(downloader, GetProfilePictureStatus()).
87 WillOnce(Return(ProfileDownloader::PICTURE_SUCCESS));
88 std::string url("foo.com");
89 EXPECT_CALL(downloader, GetProfilePictureURL()).WillOnce(Return(url));
91 // No URL should be cached yet.
92 EXPECT_EQ(std::string(), service.GetCachedPictureURL());
94 service.OnProfileDownloadSuccess(&downloader);
96 // On success both the profile info and GAIA info should be updated.
97 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath());
98 EXPECT_EQ(name, GetCache()->GetNameOfProfileAtIndex(index));
99 EXPECT_EQ(name, GetCache()->GetGAIANameOfProfileAtIndex(index));
100 EXPECT_TRUE(gfx::test::IsEqual(
101 image, GetCache()->GetAvatarIconOfProfileAtIndex(index)));
102 EXPECT_TRUE(gfx::test::IsEqual(
103 image, *GetCache()->GetGAIAPictureOfProfileAtIndex(index)));
104 EXPECT_EQ(url, service.GetCachedPictureURL());
107 TEST_F(GAIAInfoUpdateServiceTest, DownloadFailure) {
108 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath());
109 base::string16 old_name = GetCache()->GetNameOfProfileAtIndex(index);
110 gfx::Image old_image = GetCache()->GetAvatarIconOfProfileAtIndex(index);
112 GAIAInfoUpdateService service(profile());
113 EXPECT_EQ(std::string(), service.GetCachedPictureURL());
114 NiceMock<ProfileDownloaderMock> downloader(&service);
116 service.OnProfileDownloadFailure(&downloader,
117 ProfileDownloaderDelegate::SERVICE_ERROR);
119 // On failure nothing should be updated.
120 EXPECT_EQ(old_name, GetCache()->GetNameOfProfileAtIndex(index));
121 EXPECT_EQ(base::string16(), GetCache()->GetGAIANameOfProfileAtIndex(index));
122 EXPECT_TRUE(gfx::test::IsEqual(
123 old_image, GetCache()->GetAvatarIconOfProfileAtIndex(index)));
124 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(index));
125 EXPECT_EQ(std::string(), service.GetCachedPictureURL());
128 TEST_F(GAIAInfoUpdateServiceTest, ShouldUseGAIAProfileInfo) {
129 #if defined(OS_CHROMEOS)
130 // This feature should never be enabled on ChromeOS.
131 EXPECT_FALSE(GAIAInfoUpdateService::ShouldUseGAIAProfileInfo(profile()));
135 TEST_F(GAIAInfoUpdateServiceTest, ScheduleUpdate) {
136 GAIAInfoUpdateService service(profile());
137 EXPECT_TRUE(service.timer_.IsRunning());
138 service.timer_.Stop();
139 EXPECT_FALSE(service.timer_.IsRunning());
140 service.ScheduleNextUpdate();
141 EXPECT_TRUE(service.timer_.IsRunning());
144 TEST_F(GAIAInfoUpdateServiceTest, LogOut) {
145 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
147 base::string16 gaia_name = base::UTF8ToUTF16("Pat Foo");
148 GetCache()->SetGAIANameOfProfileAtIndex(0, gaia_name);
149 gfx::Image gaia_picture = gfx::test::CreateImage();
150 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_picture);
152 // Set a fake picture URL.
153 profile()->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL,
156 GAIAInfoUpdateService service(profile());
157 EXPECT_FALSE(service.GetCachedPictureURL().empty());
159 profile()->GetPrefs()
160 ->SetString(prefs::kGoogleServicesUsername, std::string());
162 // Verify that the GAIA name and picture, and picture URL are unset.
163 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty());
164 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
165 EXPECT_TRUE(service.GetCachedPictureURL().empty());
168 TEST_F(GAIAInfoUpdateServiceTest, LogIn) {
169 profile()->GetPrefs()
170 ->SetString(prefs::kGoogleServicesUsername, std::string());
171 GAIAInfoUpdateServiceMock service(profile());
174 EXPECT_CALL(service, Update());
175 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername,