OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/profiles/gaia_info_update_service.h" | 5 #include "chrome/browser/profiles/gaia_info_update_service.h" |
6 | 6 |
7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/profiles/profile_downloader.h" | 10 #include "chrome/browser/profiles/profile_downloader.h" |
(...skipping 16 matching lines...) Expand all Loading... | |
27 class ProfileDownloaderMock : public ProfileDownloader { | 27 class ProfileDownloaderMock : public ProfileDownloader { |
28 public: | 28 public: |
29 explicit ProfileDownloaderMock(ProfileDownloaderDelegate* delegate) | 29 explicit ProfileDownloaderMock(ProfileDownloaderDelegate* delegate) |
30 : ProfileDownloader(delegate) { | 30 : ProfileDownloader(delegate) { |
31 } | 31 } |
32 | 32 |
33 virtual ~ProfileDownloaderMock() { | 33 virtual ~ProfileDownloaderMock() { |
34 } | 34 } |
35 | 35 |
36 MOCK_CONST_METHOD0(GetProfileFullName, base::string16()); | 36 MOCK_CONST_METHOD0(GetProfileFullName, base::string16()); |
37 MOCK_CONST_METHOD0(GetProfileGivenName, base::string16()); | |
37 MOCK_CONST_METHOD0(GetProfilePicture, SkBitmap()); | 38 MOCK_CONST_METHOD0(GetProfilePicture, SkBitmap()); |
38 MOCK_CONST_METHOD0(GetProfilePictureStatus, | 39 MOCK_CONST_METHOD0(GetProfilePictureStatus, |
39 ProfileDownloader::PictureStatus()); | 40 ProfileDownloader::PictureStatus()); |
40 MOCK_CONST_METHOD0(GetProfilePictureURL, std::string()); | 41 MOCK_CONST_METHOD0(GetProfilePictureURL, std::string()); |
41 }; | 42 }; |
42 | 43 |
43 class GAIAInfoUpdateServiceMock : public GAIAInfoUpdateService { | 44 class GAIAInfoUpdateServiceMock : public GAIAInfoUpdateService { |
44 public: | 45 public: |
45 explicit GAIAInfoUpdateServiceMock(Profile* profile) | 46 explicit GAIAInfoUpdateServiceMock(Profile* profile) |
46 : GAIAInfoUpdateService(profile) { | 47 : GAIAInfoUpdateService(profile) { |
47 } | 48 } |
48 | 49 |
49 virtual ~GAIAInfoUpdateServiceMock() { | 50 virtual ~GAIAInfoUpdateServiceMock() { |
50 } | 51 } |
51 | 52 |
52 MOCK_METHOD0(Update, void()); | 53 MOCK_METHOD0(Update, void()); |
53 }; | 54 }; |
54 | 55 |
55 class GAIAInfoUpdateServiceTest : public ProfileInfoCacheTest { | 56 class GAIAInfoUpdateServiceTest : public ProfileInfoCacheTest { |
56 protected: | 57 protected: |
57 GAIAInfoUpdateServiceTest() : profile_(NULL) { | 58 GAIAInfoUpdateServiceTest() : profile_(NULL) { |
58 } | 59 } |
59 | 60 |
60 Profile* profile() { | 61 Profile* profile() { |
61 if (!profile_) { | 62 if (!profile_) |
62 profile_ = testing_profile_manager_.CreateTestingProfile("Person 1"); | 63 profile_ = CreateProfile("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_; | 64 return profile_; |
70 } | 65 } |
71 | 66 |
72 NiceMock<GAIAInfoUpdateServiceMock>* service() { return service_.get(); } | 67 NiceMock<GAIAInfoUpdateServiceMock>* service() { return service_.get(); } |
73 NiceMock<ProfileDownloaderMock>* downloader() { return downloader_.get(); } | 68 NiceMock<ProfileDownloaderMock>* downloader() { return downloader_.get(); } |
74 | 69 |
70 Profile* CreateProfile(const std::string& name) { | |
71 Profile* profile = testing_profile_manager_.CreateTestingProfile(name); | |
72 // The testing manager sets the profile name manually, which counts as | |
73 // a user-customized profile name. Reset this to match the default name | |
74 // we are actually using. | |
75 size_t index = GetCache()->GetIndexOfProfileWithPath(profile->GetPath()); | |
76 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index, true); | |
77 return profile; | |
78 } | |
79 | |
80 static std::string GivenName(const std::string& id) { | |
81 return id + "first"; | |
82 } | |
83 static std::string FullName(const std::string& id) { | |
84 return GivenName(id) + " " + id + "last"; | |
85 } | |
86 static base::string16 GivenName16(const std::string& id) { | |
87 return base::UTF8ToUTF16(GivenName(id)); | |
88 } | |
89 static base::string16 FullName16(const std::string& id) { | |
90 return base::UTF8ToUTF16(FullName(id)); | |
91 } | |
92 | |
93 void ProfileDownloadSuccess( | |
94 const base::string16& full_name, | |
95 const base::string16& given_name, | |
96 const gfx::Image& image, | |
97 const std::string& url) { | |
98 EXPECT_CALL(*downloader(), GetProfileFullName()). | |
99 WillOnce(Return(full_name)); | |
100 EXPECT_CALL(*downloader(), GetProfileGivenName()). | |
101 WillOnce(Return(given_name)); | |
102 const SkBitmap* bmp = image.ToSkBitmap(); | |
103 EXPECT_CALL(*downloader(), GetProfilePicture()).WillOnce(Return(*bmp)); | |
104 EXPECT_CALL(*downloader(), GetProfilePictureStatus()). | |
105 WillOnce(Return(ProfileDownloader::PICTURE_SUCCESS)); | |
106 EXPECT_CALL(*downloader(), GetProfilePictureURL()).WillOnce(Return(url)); | |
107 | |
108 service()->OnProfileDownloadSuccess(downloader()); | |
109 } | |
110 | |
111 void RenameProfile(const base::string16& full_name, | |
112 const base::string16& given_name) { | |
113 gfx::Image image = gfx::test::CreateImage(); | |
114 std::string url("foo.com"); | |
115 ProfileDownloadSuccess(full_name, given_name, image, url); | |
116 | |
117 // Make sure the right profile was updated correctly. | |
118 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); | |
119 EXPECT_EQ(full_name, GetCache()->GetGAIANameOfProfileAtIndex(index)); | |
120 EXPECT_EQ(given_name, GetCache()->GetGAIAGivenNameOfProfileAtIndex(index)); | |
121 } | |
122 | |
75 private: | 123 private: |
76 virtual void SetUp() OVERRIDE; | 124 virtual void SetUp() OVERRIDE; |
77 virtual void TearDown() OVERRIDE; | 125 virtual void TearDown() OVERRIDE; |
78 | 126 |
79 Profile* profile_; | 127 Profile* profile_; |
80 scoped_ptr<NiceMock<GAIAInfoUpdateServiceMock> > service_; | 128 scoped_ptr<NiceMock<GAIAInfoUpdateServiceMock> > service_; |
81 scoped_ptr<NiceMock<ProfileDownloaderMock> > downloader_; | 129 scoped_ptr<NiceMock<ProfileDownloaderMock> > downloader_; |
82 }; | 130 }; |
83 | 131 |
84 void GAIAInfoUpdateServiceTest::SetUp() { | 132 void GAIAInfoUpdateServiceTest::SetUp() { |
85 ProfileInfoCacheTest::SetUp(); | 133 ProfileInfoCacheTest::SetUp(); |
86 service_.reset(new NiceMock<GAIAInfoUpdateServiceMock>(profile())); | 134 service_.reset(new NiceMock<GAIAInfoUpdateServiceMock>(profile())); |
87 downloader_.reset(new NiceMock<ProfileDownloaderMock>(service())); | 135 downloader_.reset(new NiceMock<ProfileDownloaderMock>(service())); |
88 } | 136 } |
89 | 137 |
90 void GAIAInfoUpdateServiceTest::TearDown() { | 138 void GAIAInfoUpdateServiceTest::TearDown() { |
91 downloader_.reset(); | 139 downloader_.reset(); |
92 service_->Shutdown(); | 140 service_->Shutdown(); |
93 service_.reset(); | 141 service_.reset(); |
94 ProfileInfoCacheTest::TearDown(); | 142 ProfileInfoCacheTest::TearDown(); |
95 } | 143 } |
96 | 144 |
97 } // namespace | 145 } // namespace |
98 | 146 |
99 TEST_F(GAIAInfoUpdateServiceTest, DownloadSuccess) { | 147 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. | 148 // No URL should be cached yet. |
111 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); | 149 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); |
112 | 150 |
113 service()->OnProfileDownloadSuccess(downloader()); | 151 base::string16 name = base::ASCIIToUTF16("Pat Smith"); |
152 base::string16 given_name = base::ASCIIToUTF16("Pat"); | |
153 gfx::Image image = gfx::test::CreateImage(); | |
154 std::string url("foo.com"); | |
155 ProfileDownloadSuccess(name, given_name, image, url); | |
114 | 156 |
115 // On success both the profile info and GAIA info should be updated. | 157 // On success the GAIA info should be updated. |
116 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); | 158 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); |
117 EXPECT_EQ(name, GetCache()->GetNameOfProfileAtIndex(index)); | |
Marc Treib
2014/07/11 12:25:26
The GAIAInfoUpdateService does *not* actually upda
noms (inactive)
2014/07/11 13:25:08
Hmm. GetNameOfProfile() returns either the Gaia na
Marc Treib
2014/07/11 13:30:28
Not quite - it uses the local profile name if it's
noms (inactive)
2014/07/11 13:52:29
I agree it should not be tested here. Ok.
On 2014
| |
118 EXPECT_EQ(name, GetCache()->GetGAIANameOfProfileAtIndex(index)); | 159 EXPECT_EQ(name, GetCache()->GetGAIANameOfProfileAtIndex(index)); |
160 EXPECT_EQ(given_name, GetCache()->GetGAIAGivenNameOfProfileAtIndex(index)); | |
119 EXPECT_TRUE(gfx::test::IsEqual( | 161 EXPECT_TRUE(gfx::test::IsEqual( |
120 image, *GetCache()->GetGAIAPictureOfProfileAtIndex(index))); | 162 image, *GetCache()->GetGAIAPictureOfProfileAtIndex(index))); |
121 EXPECT_EQ(url, service()->GetCachedPictureURL()); | 163 EXPECT_EQ(url, service()->GetCachedPictureURL()); |
122 } | 164 } |
123 | 165 |
124 TEST_F(GAIAInfoUpdateServiceTest, DownloadFailure) { | 166 TEST_F(GAIAInfoUpdateServiceTest, DownloadFailure) { |
125 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); | 167 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); |
126 base::string16 old_name = GetCache()->GetNameOfProfileAtIndex(index); | 168 base::string16 old_name = GetCache()->GetNameOfProfileAtIndex(index); |
127 gfx::Image old_image = GetCache()->GetAvatarIconOfProfileAtIndex(index); | 169 gfx::Image old_image = GetCache()->GetAvatarIconOfProfileAtIndex(index); |
128 | 170 |
129 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); | 171 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); |
130 | 172 |
131 service()->OnProfileDownloadFailure(downloader(), | 173 service()->OnProfileDownloadFailure(downloader(), |
132 ProfileDownloaderDelegate::SERVICE_ERROR); | 174 ProfileDownloaderDelegate::SERVICE_ERROR); |
133 | 175 |
134 // On failure nothing should be updated. | 176 // On failure nothing should be updated. |
135 EXPECT_EQ(old_name, GetCache()->GetNameOfProfileAtIndex(index)); | 177 EXPECT_EQ(old_name, GetCache()->GetNameOfProfileAtIndex(index)); |
136 EXPECT_EQ(base::string16(), GetCache()->GetGAIANameOfProfileAtIndex(index)); | 178 EXPECT_EQ(base::string16(), GetCache()->GetGAIANameOfProfileAtIndex(index)); |
179 EXPECT_EQ(base::string16(), | |
180 GetCache()->GetGAIAGivenNameOfProfileAtIndex(index)); | |
137 EXPECT_TRUE(gfx::test::IsEqual( | 181 EXPECT_TRUE(gfx::test::IsEqual( |
138 old_image, GetCache()->GetAvatarIconOfProfileAtIndex(index))); | 182 old_image, GetCache()->GetAvatarIconOfProfileAtIndex(index))); |
139 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(index)); | 183 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(index)); |
140 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); | 184 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); |
141 } | 185 } |
142 | 186 |
187 TEST_F(GAIAInfoUpdateServiceTest, HandlesProfileReordering) { | |
188 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); | |
189 GetCache()->SetNameOfProfileAtIndex(index, FullName16("B")); | |
190 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index, true); | |
191 | |
192 CreateProfile(FullName("A")); | |
193 CreateProfile(FullName("C")); | |
194 CreateProfile(FullName("E")); | |
195 | |
196 size_t index_before = | |
197 GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); | |
198 | |
199 // Rename our profile. | |
200 RenameProfile(FullName16("D"), GivenName16("D")); | |
201 // Profiles should have been reordered in the cache. | |
202 EXPECT_NE(index_before, | |
203 GetCache()->GetIndexOfProfileWithPath(profile()->GetPath())); | |
204 // Rename the profile back to the original name, it should go back to its | |
205 // original position. | |
206 RenameProfile(FullName16("B"), GivenName16("B")); | |
207 EXPECT_EQ(index_before, | |
208 GetCache()->GetIndexOfProfileWithPath(profile()->GetPath())); | |
209 | |
210 // Rename only the given name of our profile. | |
211 RenameProfile(FullName16("B"), GivenName16("D")); | |
212 // Rename the profile back to the original name, it should go back to its | |
213 // original position. | |
214 RenameProfile(FullName16("B"), GivenName16("B")); | |
215 EXPECT_EQ(index_before, | |
216 GetCache()->GetIndexOfProfileWithPath(profile()->GetPath())); | |
217 | |
218 // Rename only the full name of our profile. | |
219 RenameProfile(FullName16("D"), GivenName16("B")); | |
220 // Rename the profile back to the original name, it should go back to its | |
221 // original position. | |
222 RenameProfile(FullName16("B"), GivenName16("B")); | |
223 EXPECT_EQ(index_before, | |
224 GetCache()->GetIndexOfProfileWithPath(profile()->GetPath())); | |
225 } | |
226 | |
143 TEST_F(GAIAInfoUpdateServiceTest, ShouldUseGAIAProfileInfo) { | 227 TEST_F(GAIAInfoUpdateServiceTest, ShouldUseGAIAProfileInfo) { |
144 #if defined(OS_CHROMEOS) | 228 #if defined(OS_CHROMEOS) |
145 // This feature should never be enabled on ChromeOS. | 229 // This feature should never be enabled on ChromeOS. |
146 EXPECT_FALSE(GAIAInfoUpdateService::ShouldUseGAIAProfileInfo(profile())); | 230 EXPECT_FALSE(GAIAInfoUpdateService::ShouldUseGAIAProfileInfo(profile())); |
147 #endif | 231 #endif |
148 } | 232 } |
149 | 233 |
150 TEST_F(GAIAInfoUpdateServiceTest, ScheduleUpdate) { | 234 TEST_F(GAIAInfoUpdateServiceTest, ScheduleUpdate) { |
151 EXPECT_TRUE(service()->timer_.IsRunning()); | 235 EXPECT_TRUE(service()->timer_.IsRunning()); |
152 service()->timer_.Stop(); | 236 service()->timer_.Stop(); |
(...skipping 29 matching lines...) Expand all Loading... | |
182 | 266 |
183 TEST_F(GAIAInfoUpdateServiceTest, LogIn) { | 267 TEST_F(GAIAInfoUpdateServiceTest, LogIn) { |
184 // Log in. | 268 // Log in. |
185 EXPECT_CALL(*service(), Update()); | 269 EXPECT_CALL(*service(), Update()); |
186 SigninManager* signin_manager = | 270 SigninManager* signin_manager = |
187 SigninManagerFactory::GetForProfile(profile()); | 271 SigninManagerFactory::GetForProfile(profile()); |
188 signin_manager->OnExternalSigninCompleted("pat@example.com"); | 272 signin_manager->OnExternalSigninCompleted("pat@example.com"); |
189 } | 273 } |
190 | 274 |
191 #endif | 275 #endif |
OLD | NEW |