Upstream version 6.35.121.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/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"
20
21 using ::testing::Return;
22 using ::testing::NiceMock;
23
24 namespace {
25
26 class ProfileDownloaderMock : public ProfileDownloader {
27  public:
28   explicit ProfileDownloaderMock(ProfileDownloaderDelegate* delegate)
29       : ProfileDownloader(delegate) {
30   }
31
32   virtual ~ProfileDownloaderMock() {
33   }
34
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());
40 };
41
42 class GAIAInfoUpdateServiceMock : public GAIAInfoUpdateService {
43  public:
44   explicit GAIAInfoUpdateServiceMock(Profile* profile)
45       : GAIAInfoUpdateService(profile) {
46   }
47
48   virtual ~GAIAInfoUpdateServiceMock() {
49   }
50
51   MOCK_METHOD0(Update, void());
52 };
53
54 class GAIAInfoUpdateServiceTest : public ProfileInfoCacheTest {
55  protected:
56   GAIAInfoUpdateServiceTest() : profile_(NULL) {
57   }
58
59   Profile* profile() {
60     if (!profile_) {
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);
67     }
68     return profile_;
69   }
70
71  private:
72   Profile* profile_;
73 };
74
75 } // namespace
76
77 TEST_F(GAIAInfoUpdateServiceTest, DownloadSuccess) {
78   GAIAInfoUpdateService service(profile());
79   NiceMock<ProfileDownloaderMock> downloader(&service);
80
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));
90
91   // No URL should be cached yet.
92   EXPECT_EQ(std::string(), service.GetCachedPictureURL());
93
94   service.OnProfileDownloadSuccess(&downloader);
95
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());
105 }
106
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);
111
112   GAIAInfoUpdateService service(profile());
113   EXPECT_EQ(std::string(), service.GetCachedPictureURL());
114   NiceMock<ProfileDownloaderMock> downloader(&service);
115
116   service.OnProfileDownloadFailure(&downloader,
117                                    ProfileDownloaderDelegate::SERVICE_ERROR);
118
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());
126 }
127
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()));
132 #endif
133 }
134
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());
142 }
143
144 TEST_F(GAIAInfoUpdateServiceTest, LogOut) {
145   profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
146                                    "pat@example.com");
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);
151
152   // Set a fake picture URL.
153   profile()->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL,
154                                    "example.com");
155
156   GAIAInfoUpdateService service(profile());
157   EXPECT_FALSE(service.GetCachedPictureURL().empty());
158   // Log out.
159   profile()->GetPrefs()
160       ->SetString(prefs::kGoogleServicesUsername, std::string());
161
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());
166 }
167
168 TEST_F(GAIAInfoUpdateServiceTest, LogIn) {
169   profile()->GetPrefs()
170       ->SetString(prefs::kGoogleServicesUsername, std::string());
171   GAIAInfoUpdateServiceMock service(profile());
172
173   // Log in.
174   EXPECT_CALL(service, Update());
175   profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
176                                    "pat@example.com");
177 }