Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / profiles / gaia_info_update_service_unittest.cc
blobe4c04d707220d61aa987912479e7fd662a9835fd
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;
24 namespace {
26 class ProfileDownloaderMock : public ProfileDownloader {
27 public:
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 {
43 public:
44 explicit GAIAInfoUpdateServiceMock(Profile* profile)
45 : GAIAInfoUpdateService(profile) {
48 virtual ~GAIAInfoUpdateServiceMock() {
51 MOCK_METHOD0(Update, void());
54 class GAIAInfoUpdateServiceTest : public ProfileInfoCacheTest {
55 protected:
56 GAIAInfoUpdateServiceTest() : profile_(NULL) {
59 Profile* profile() {
60 if (!profile_)
61 profile_ = testing_profile_manager_.CreateTestingProfile("profile_1");
62 return profile_;
65 private:
66 Profile* profile_;
69 } // namespace
71 TEST_F(GAIAInfoUpdateServiceTest, DownloadSuccess) {
72 GAIAInfoUpdateService service(profile());
73 NiceMock<ProfileDownloaderMock> downloader(&service);
75 base::string16 name = base::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));
85 // No URL should be cached yet.
86 EXPECT_EQ(std::string(), service.GetCachedPictureURL());
88 service.OnProfileDownloadSuccess(&downloader);
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());
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);
107 GAIAInfoUpdateService service(profile());
108 EXPECT_EQ(std::string(), service.GetCachedPictureURL());
109 NiceMock<ProfileDownloaderMock> downloader(&service);
111 service.OnProfileDownloadFailure(&downloader,
112 ProfileDownloaderDelegate::SERVICE_ERROR);
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());
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);
129 // Mark the profile as migrated.
130 GetCache()->SetHasMigratedToGAIAInfoOfProfileAtIndex(index, true);
132 GAIAInfoUpdateService service(profile());
133 NiceMock<ProfileDownloaderMock> downloader(&service);
134 base::string16 new_name = base::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(""));
143 service.OnProfileDownloadSuccess(&downloader);
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)));
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
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());
172 TEST_F(GAIAInfoUpdateServiceTest, LogOut) {
173 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
174 "pat@example.com");
175 base::string16 gaia_name = base::UTF8ToUTF16("Pat Foo");
176 GetCache()->SetGAIANameOfProfileAtIndex(0, gaia_name);
177 gfx::Image gaia_picture = gfx::test::CreateImage();
178 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_picture);
180 // Set a fake picture URL.
181 profile()->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL,
182 "example.com");
184 GAIAInfoUpdateService service(profile());
185 EXPECT_FALSE(service.GetCachedPictureURL().empty());
186 // Log out.
187 profile()->GetPrefs()
188 ->SetString(prefs::kGoogleServicesUsername, std::string());
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());
196 TEST_F(GAIAInfoUpdateServiceTest, LogIn) {
197 profile()->GetPrefs()
198 ->SetString(prefs::kGoogleServicesUsername, std::string());
199 GAIAInfoUpdateServiceMock service(profile());
201 // Log in.
202 EXPECT_CALL(service, Update());
203 profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
204 "pat@example.com");