GoogleURLTrackerInfoBarDelegate: Initialize uninitialized member in constructor.
[chromium-blink-merge.git] / chrome / browser / profiles / gaia_info_update_service_unittest.cc
blobbb9105249aa347969100f5783c6f50c14a3e0285
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/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"
22 using ::testing::Return;
23 using ::testing::NiceMock;
25 namespace {
27 class ProfileDownloaderMock : public ProfileDownloader {
28 public:
29 explicit ProfileDownloaderMock(ProfileDownloaderDelegate* delegate)
30 : ProfileDownloader(delegate) {
33 virtual ~ProfileDownloaderMock() {
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());
43 class GAIAInfoUpdateServiceMock : public GAIAInfoUpdateService {
44 public:
45 explicit GAIAInfoUpdateServiceMock(Profile* profile)
46 : GAIAInfoUpdateService(profile) {
49 virtual ~GAIAInfoUpdateServiceMock() {
52 MOCK_METHOD0(Update, void());
55 class GAIAInfoUpdateServiceTest : public ProfileInfoCacheTest {
56 protected:
57 GAIAInfoUpdateServiceTest() : profile_(NULL) {
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);
69 return profile_;
72 NiceMock<GAIAInfoUpdateServiceMock>* service() { return service_.get(); }
73 NiceMock<ProfileDownloaderMock>* downloader() { return downloader_.get(); }
75 private:
76 virtual void SetUp() OVERRIDE;
77 virtual void TearDown() OVERRIDE;
79 Profile* profile_;
80 scoped_ptr<NiceMock<GAIAInfoUpdateServiceMock> > service_;
81 scoped_ptr<NiceMock<ProfileDownloaderMock> > downloader_;
84 void GAIAInfoUpdateServiceTest::SetUp() {
85 ProfileInfoCacheTest::SetUp();
86 service_.reset(new NiceMock<GAIAInfoUpdateServiceMock>(profile()));
87 downloader_.reset(new NiceMock<ProfileDownloaderMock>(service()));
90 void GAIAInfoUpdateServiceTest::TearDown() {
91 downloader_.reset();
92 service_->Shutdown();
93 service_.reset();
94 ProfileInfoCacheTest::TearDown();
97 } // namespace
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));
110 // No URL should be cached yet.
111 EXPECT_EQ(std::string(), service()->GetCachedPictureURL());
113 service()->OnProfileDownloadSuccess(downloader());
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());
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);
129 EXPECT_EQ(std::string(), service()->GetCachedPictureURL());
131 service()->OnProfileDownloadFailure(downloader(),
132 ProfileDownloaderDelegate::SERVICE_ERROR);
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());
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
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());
158 #if !defined(OS_CHROMEOS)
160 TEST_F(GAIAInfoUpdateServiceTest, LogOut) {
161 SigninManager* signin_manager =
162 SigninManagerFactory::GetForProfile(profile());
163 signin_manager->SetAuthenticatedUsername("pat@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);
169 // Set a fake picture URL.
170 profile()->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL,
171 "example.com");
173 EXPECT_FALSE(service()->GetCachedPictureURL().empty());
175 // Log out.
176 signin_manager->SignOut();
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());
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@example.com");
191 #endif