Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / gaia_info_update_service_unittest.cc
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()->GetAvatarIconOfProfileAtIndex(index)));
121   EXPECT_TRUE(gfx::test::IsEqual(
122       image, *GetCache()->GetGAIAPictureOfProfileAtIndex(index)));
123   EXPECT_EQ(url, service()->GetCachedPictureURL());
124 }
125
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);
130
131   EXPECT_EQ(std::string(), service()->GetCachedPictureURL());
132
133   service()->OnProfileDownloadFailure(downloader(),
134                                     ProfileDownloaderDelegate::SERVICE_ERROR);
135
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());
143 }
144
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
150 }
151
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());
158 }
159
160 #if !defined(OS_CHROMEOS)
161
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);
170
171   // Set a fake picture URL.
172   profile()->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL,
173                                    "example.com");
174
175   EXPECT_FALSE(service()->GetCachedPictureURL().empty());
176
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());
183 }
184
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");
191 }
192
193 #endif