[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / profiles / gaia_info_update_service_unittest.cc
blob04d2ff6f5cecfe0d5c802e33fa797a9ca79a12dd
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()->GetAvatarIconOfProfileAtIndex(index)));
121 EXPECT_TRUE(gfx::test::IsEqual(
122 image, *GetCache()->GetGAIAPictureOfProfileAtIndex(index)));
123 EXPECT_EQ(url, service()->GetCachedPictureURL());
126 TEST_F(GAIAInfoUpdateServiceTest, DownloadFailure) {
127 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath());
128 base::string16 old_name = GetCache()->GetNameOfProfileAtIndex(index);
129 gfx::Image old_image = GetCache()->GetAvatarIconOfProfileAtIndex(index);
131 EXPECT_EQ(std::string(), service()->GetCachedPictureURL());
133 service()->OnProfileDownloadFailure(downloader(),
134 ProfileDownloaderDelegate::SERVICE_ERROR);
136 // On failure nothing should be updated.
137 EXPECT_EQ(old_name, GetCache()->GetNameOfProfileAtIndex(index));
138 EXPECT_EQ(base::string16(), GetCache()->GetGAIANameOfProfileAtIndex(index));
139 EXPECT_TRUE(gfx::test::IsEqual(
140 old_image, GetCache()->GetAvatarIconOfProfileAtIndex(index)));
141 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(index));
142 EXPECT_EQ(std::string(), service()->GetCachedPictureURL());
145 TEST_F(GAIAInfoUpdateServiceTest, ShouldUseGAIAProfileInfo) {
146 #if defined(OS_CHROMEOS)
147 // This feature should never be enabled on ChromeOS.
148 EXPECT_FALSE(GAIAInfoUpdateService::ShouldUseGAIAProfileInfo(profile()));
149 #endif
152 TEST_F(GAIAInfoUpdateServiceTest, ScheduleUpdate) {
153 EXPECT_TRUE(service()->timer_.IsRunning());
154 service()->timer_.Stop();
155 EXPECT_FALSE(service()->timer_.IsRunning());
156 service()->ScheduleNextUpdate();
157 EXPECT_TRUE(service()->timer_.IsRunning());
160 #if !defined(OS_CHROMEOS)
162 TEST_F(GAIAInfoUpdateServiceTest, LogOut) {
163 SigninManager* signin_manager =
164 SigninManagerFactory::GetForProfile(profile());
165 signin_manager->SetAuthenticatedUsername("pat@example.com");
166 base::string16 gaia_name = base::UTF8ToUTF16("Pat Foo");
167 GetCache()->SetGAIANameOfProfileAtIndex(0, gaia_name);
168 gfx::Image gaia_picture = gfx::test::CreateImage();
169 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_picture);
171 // Set a fake picture URL.
172 profile()->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL,
173 "example.com");
175 EXPECT_FALSE(service()->GetCachedPictureURL().empty());
177 // Log out.
178 signin_manager->SignOut();
179 // Verify that the GAIA name and picture, and picture URL are unset.
180 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty());
181 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0));
182 EXPECT_TRUE(service()->GetCachedPictureURL().empty());
185 TEST_F(GAIAInfoUpdateServiceTest, LogIn) {
186 // Log in.
187 EXPECT_CALL(*service(), Update());
188 SigninManager* signin_manager =
189 SigninManagerFactory::GetForProfile(profile());
190 signin_manager->OnExternalSigninCompleted("pat@example.com");
193 #endif