Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: chrome/browser/profiles/gaia_info_update_service_unittest.cc

Issue 1242793005: Refactor most c/b/profiles calls to ProfileInfoCache. Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Windows unit test and ChromeOS build Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/prefs/pref_service_syncable.h" 10 #include "chrome/browser/prefs/pref_service_syncable.h"
11 #include "chrome/browser/profiles/profile_attributes_entry.h"
12 #include "chrome/browser/profiles/profile_attributes_storage.h"
11 #include "chrome/browser/profiles/profile_downloader.h" 13 #include "chrome/browser/profiles/profile_downloader.h"
12 #include "chrome/browser/profiles/profile_info_cache.h" 14 #include "chrome/browser/profiles/profile_info_cache.h"
13 #include "chrome/browser/profiles/profile_info_cache_unittest.h" 15 #include "chrome/browser/profiles/profile_info_cache_unittest.h"
14 #include "chrome/browser/profiles/profiles_state.h" 16 #include "chrome/browser/profiles/profiles_state.h"
15 #include "chrome/browser/signin/account_tracker_service_factory.h" 17 #include "chrome/browser/signin/account_tracker_service_factory.h"
16 #include "chrome/browser/signin/chrome_signin_client_factory.h" 18 #include "chrome/browser/signin/chrome_signin_client_factory.h"
17 #include "chrome/browser/signin/signin_manager_factory.h" 19 #include "chrome/browser/signin/signin_manager_factory.h"
18 #include "chrome/browser/signin/test_signin_client_builder.h" 20 #include "chrome/browser/signin/test_signin_client_builder.h"
19 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/testing_browser_process.h" 22 #include "chrome/test/base/testing_browser_process.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 protected: 66 protected:
65 GAIAInfoUpdateServiceTest() : profile_(NULL) { 67 GAIAInfoUpdateServiceTest() : profile_(NULL) {
66 } 68 }
67 69
68 Profile* profile() { 70 Profile* profile() {
69 if (!profile_) 71 if (!profile_)
70 profile_ = CreateProfile("Person 1"); 72 profile_ = CreateProfile("Person 1");
71 return profile_; 73 return profile_;
72 } 74 }
73 75
76 ProfileAttributesStorage* storage() {
77 return GetCache();
Mike Lerman 2015/08/06 16:06:19 Do we still need a GetCache() method?
78 }
79
74 NiceMock<GAIAInfoUpdateServiceMock>* service() { return service_.get(); } 80 NiceMock<GAIAInfoUpdateServiceMock>* service() { return service_.get(); }
75 NiceMock<ProfileDownloaderMock>* downloader() { return downloader_.get(); } 81 NiceMock<ProfileDownloaderMock>* downloader() { return downloader_.get(); }
76 82
77 Profile* CreateProfile(const std::string& name) { 83 Profile* CreateProfile(const std::string& name) {
78 TestingProfile::TestingFactories testing_factories; 84 TestingProfile::TestingFactories testing_factories;
79 testing_factories.push_back(std::make_pair( 85 testing_factories.push_back(std::make_pair(
80 ChromeSigninClientFactory::GetInstance(), 86 ChromeSigninClientFactory::GetInstance(),
81 signin::BuildTestSigninClient)); 87 signin::BuildTestSigninClient));
82 Profile* profile = testing_profile_manager_.CreateTestingProfile(name, 88 Profile* profile = testing_profile_manager_.CreateTestingProfile(name,
83 scoped_ptr<PrefServiceSyncable>(), base::UTF8ToUTF16(name), 0, 89 scoped_ptr<PrefServiceSyncable>(), base::UTF8ToUTF16(name), 0,
84 std::string(), testing_factories); 90 std::string(), testing_factories);
85 // The testing manager sets the profile name manually, which counts as 91 // The testing manager sets the profile name manually, which counts as
86 // a user-customized profile name. Reset this to match the default name 92 // a user-customized profile name. Reset this to match the default name
87 // we are actually using. 93 // we are actually using.
88 size_t index = GetCache()->GetIndexOfProfileWithPath(profile->GetPath()); 94 ProfileAttributesEntry* entry;
89 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index, true); 95 if (storage()->GetProfileAttributesWithPath(profile->GetPath(), &entry)) {
96 entry->SetIsUsingDefaultName(true);
97 }
90 return profile; 98 return profile;
91 } 99 }
92 100
93 static std::string GivenName(const std::string& id) { 101 static std::string GivenName(const std::string& id) {
94 return id + "first"; 102 return id + "first";
95 } 103 }
96 static std::string FullName(const std::string& id) { 104 static std::string FullName(const std::string& id) {
97 return GivenName(id) + " " + id + "last"; 105 return GivenName(id) + " " + id + "last";
98 } 106 }
99 static base::string16 GivenName16(const std::string& id) { 107 static base::string16 GivenName16(const std::string& id) {
(...skipping 24 matching lines...) Expand all
124 service()->OnProfileDownloadSuccess(downloader()); 132 service()->OnProfileDownloadSuccess(downloader());
125 } 133 }
126 134
127 void RenameProfile(const base::string16& full_name, 135 void RenameProfile(const base::string16& full_name,
128 const base::string16& given_name) { 136 const base::string16& given_name) {
129 gfx::Image image = gfx::test::CreateImage(256,256); 137 gfx::Image image = gfx::test::CreateImage(256,256);
130 std::string url("foo.com"); 138 std::string url("foo.com");
131 ProfileDownloadSuccess(full_name, given_name, image, url, base::string16()); 139 ProfileDownloadSuccess(full_name, given_name, image, url, base::string16());
132 140
133 // Make sure the right profile was updated correctly. 141 // Make sure the right profile was updated correctly.
134 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); 142 ProfileAttributesEntry* entry;
135 EXPECT_EQ(full_name, GetCache()->GetGAIANameOfProfileAtIndex(index)); 143 ASSERT_TRUE(
136 EXPECT_EQ(given_name, GetCache()->GetGAIAGivenNameOfProfileAtIndex(index)); 144 storage()->GetProfileAttributesWithPath(profile()->GetPath(), &entry));
145 EXPECT_EQ(full_name, entry->GetGAIAName());
146 EXPECT_EQ(given_name, entry->GetGAIAGivenName());
137 } 147 }
138 148
139 private: 149 private:
140 void SetUp() override; 150 void SetUp() override;
141 void TearDown() override; 151 void TearDown() override;
142 152
143 Profile* profile_; 153 Profile* profile_;
144 scoped_ptr<NiceMock<GAIAInfoUpdateServiceMock> > service_; 154 scoped_ptr<NiceMock<GAIAInfoUpdateServiceMock> > service_;
145 scoped_ptr<NiceMock<ProfileDownloaderMock> > downloader_; 155 scoped_ptr<NiceMock<ProfileDownloaderMock> > downloader_;
146 }; 156 };
(...skipping 20 matching lines...) Expand all
167 GetString(prefs::kGoogleServicesHostedDomain)); 177 GetString(prefs::kGoogleServicesHostedDomain));
168 178
169 base::string16 name = base::ASCIIToUTF16("Pat Smith"); 179 base::string16 name = base::ASCIIToUTF16("Pat Smith");
170 base::string16 given_name = base::ASCIIToUTF16("Pat"); 180 base::string16 given_name = base::ASCIIToUTF16("Pat");
171 gfx::Image image = gfx::test::CreateImage(256, 256); 181 gfx::Image image = gfx::test::CreateImage(256, 256);
172 std::string url("foo.com"); 182 std::string url("foo.com");
173 base::string16 hosted_domain(base::ASCIIToUTF16("")); 183 base::string16 hosted_domain(base::ASCIIToUTF16(""));
174 ProfileDownloadSuccess(name, given_name, image, url, hosted_domain); 184 ProfileDownloadSuccess(name, given_name, image, url, hosted_domain);
175 185
176 // On success the GAIA info should be updated. 186 // On success the GAIA info should be updated.
177 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); 187 ProfileAttributesEntry* entry;
178 EXPECT_EQ(name, GetCache()->GetGAIANameOfProfileAtIndex(index)); 188 ASSERT_TRUE(
179 EXPECT_EQ(given_name, GetCache()->GetGAIAGivenNameOfProfileAtIndex(index)); 189 storage()->GetProfileAttributesWithPath(profile()->GetPath(), &entry));
180 EXPECT_TRUE(gfx::test::IsEqual( 190 EXPECT_EQ(name, entry->GetGAIAName());
181 image, *GetCache()->GetGAIAPictureOfProfileAtIndex(index))); 191 EXPECT_EQ(given_name, entry->GetGAIAGivenName());
192 EXPECT_TRUE(gfx::test::IsEqual(image, *entry->GetGAIAPicture()));
182 EXPECT_EQ(url, service()->GetCachedPictureURL()); 193 EXPECT_EQ(url, service()->GetCachedPictureURL());
183 EXPECT_EQ(Profile::kNoHostedDomainFound, profile()->GetPrefs()-> 194 EXPECT_EQ(Profile::kNoHostedDomainFound, profile()->GetPrefs()->
184 GetString(prefs::kGoogleServicesHostedDomain)); 195 GetString(prefs::kGoogleServicesHostedDomain));
185 } 196 }
186 197
187 TEST_F(GAIAInfoUpdateServiceTest, DownloadFailure) { 198 TEST_F(GAIAInfoUpdateServiceTest, DownloadFailure) {
188 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); 199 ProfileAttributesEntry* entry;
189 base::string16 old_name = GetCache()->GetNameOfProfileAtIndex(index); 200 ASSERT_TRUE(
190 gfx::Image old_image = GetCache()->GetAvatarIconOfProfileAtIndex(index); 201 storage()->GetProfileAttributesWithPath(profile()->GetPath(), &entry));
202 base::string16 old_name = entry->GetName();
203 gfx::Image old_image = entry->GetAvatarIcon();
191 204
192 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); 205 EXPECT_EQ(std::string(), service()->GetCachedPictureURL());
193 206
194 service()->OnProfileDownloadFailure(downloader(), 207 service()->OnProfileDownloadFailure(downloader(),
195 ProfileDownloaderDelegate::SERVICE_ERROR); 208 ProfileDownloaderDelegate::SERVICE_ERROR);
196 209
197 // On failure nothing should be updated. 210 // On failure nothing should be updated.
198 EXPECT_EQ(old_name, GetCache()->GetNameOfProfileAtIndex(index)); 211 EXPECT_EQ(old_name, entry->GetName());
199 EXPECT_EQ(base::string16(), GetCache()->GetGAIANameOfProfileAtIndex(index)); 212 EXPECT_EQ(base::string16(), entry->GetGAIAName());
200 EXPECT_EQ(base::string16(), 213 EXPECT_EQ(base::string16(), entry->GetGAIAGivenName());
201 GetCache()->GetGAIAGivenNameOfProfileAtIndex(index)); 214 EXPECT_TRUE(gfx::test::IsEqual(old_image, entry->GetAvatarIcon()));
202 EXPECT_TRUE(gfx::test::IsEqual( 215 EXPECT_EQ(NULL, entry->GetGAIAPicture());
203 old_image, GetCache()->GetAvatarIconOfProfileAtIndex(index)));
204 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(index));
205 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); 216 EXPECT_EQ(std::string(), service()->GetCachedPictureURL());
206 EXPECT_EQ(std::string(), 217 EXPECT_EQ(std::string(),
207 profile()->GetPrefs()->GetString(prefs::kGoogleServicesHostedDomain)); 218 profile()->GetPrefs()->GetString(prefs::kGoogleServicesHostedDomain));
208 } 219 }
209 220
210 TEST_F(GAIAInfoUpdateServiceTest, ProfileLockEnabledForWhitelist) { 221 TEST_F(GAIAInfoUpdateServiceTest, ProfileLockEnabledForWhitelist) {
211 // No URL should be cached yet. 222 // No URL should be cached yet.
212 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); 223 EXPECT_EQ(std::string(), service()->GetCachedPictureURL());
213 224
214 base::string16 name = base::ASCIIToUTF16("Pat Smith"); 225 base::string16 name = base::ASCIIToUTF16("Pat Smith");
215 base::string16 given_name = base::ASCIIToUTF16("Pat"); 226 base::string16 given_name = base::ASCIIToUTF16("Pat");
216 gfx::Image image = gfx::test::CreateImage(256, 256); 227 gfx::Image image = gfx::test::CreateImage(256, 256);
217 std::string url("foo.com"); 228 std::string url("foo.com");
218 base::string16 hosted_domain(base::ASCIIToUTF16("google.com")); 229 base::string16 hosted_domain(base::ASCIIToUTF16("google.com"));
219 ProfileDownloadSuccess(name, given_name, image, url, hosted_domain); 230 ProfileDownloadSuccess(name, given_name, image, url, hosted_domain);
220 231
221 EXPECT_EQ("google.com", profile()->GetPrefs()-> 232 EXPECT_EQ("google.com", profile()->GetPrefs()->
222 GetString(prefs::kGoogleServicesHostedDomain)); 233 GetString(prefs::kGoogleServicesHostedDomain));
223 } 234 }
224 235
225 TEST_F(GAIAInfoUpdateServiceTest, HandlesProfileReordering) {
226 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath());
227 GetCache()->SetNameOfProfileAtIndex(index, FullName16("B"));
228 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index, true);
229
230 CreateProfile(FullName("A"));
231 CreateProfile(FullName("C"));
232 CreateProfile(FullName("E"));
233
234 size_t index_before =
235 GetCache()->GetIndexOfProfileWithPath(profile()->GetPath());
236
237 // Rename our profile.
238 RenameProfile(FullName16("D"), GivenName16("D"));
239 // Profiles should have been reordered in the cache.
240 EXPECT_NE(index_before,
241 GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()));
242 // Rename the profile back to the original name, it should go back to its
243 // original position.
244 RenameProfile(FullName16("B"), GivenName16("B"));
245 EXPECT_EQ(index_before,
246 GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()));
247
248 // Rename only the given name of our profile.
249 RenameProfile(FullName16("B"), GivenName16("D"));
250 // Rename the profile back to the original name, it should go back to its
251 // original position.
252 RenameProfile(FullName16("B"), GivenName16("B"));
253 EXPECT_EQ(index_before,
254 GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()));
255
256 // Rename only the full name of our profile.
257 RenameProfile(FullName16("D"), GivenName16("B"));
258 // Rename the profile back to the original name, it should go back to its
259 // original position.
260 RenameProfile(FullName16("B"), GivenName16("B"));
261 EXPECT_EQ(index_before,
262 GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()));
263 }
264
265 TEST_F(GAIAInfoUpdateServiceTest, ShouldUseGAIAProfileInfo) { 236 TEST_F(GAIAInfoUpdateServiceTest, ShouldUseGAIAProfileInfo) {
266 #if defined(OS_CHROMEOS) 237 #if defined(OS_CHROMEOS)
267 // This feature should never be enabled on ChromeOS. 238 // This feature should never be enabled on ChromeOS.
268 EXPECT_FALSE(GAIAInfoUpdateService::ShouldUseGAIAProfileInfo(profile())); 239 EXPECT_FALSE(GAIAInfoUpdateService::ShouldUseGAIAProfileInfo(profile()));
269 #endif 240 #endif
270 } 241 }
271 242
272 TEST_F(GAIAInfoUpdateServiceTest, ScheduleUpdate) { 243 TEST_F(GAIAInfoUpdateServiceTest, ScheduleUpdate) {
273 EXPECT_TRUE(service()->timer_.IsRunning()); 244 EXPECT_TRUE(service()->timer_.IsRunning());
274 service()->timer_.Stop(); 245 service()->timer_.Stop();
275 EXPECT_FALSE(service()->timer_.IsRunning()); 246 EXPECT_FALSE(service()->timer_.IsRunning());
276 service()->ScheduleNextUpdate(); 247 service()->ScheduleNextUpdate();
277 EXPECT_TRUE(service()->timer_.IsRunning()); 248 EXPECT_TRUE(service()->timer_.IsRunning());
278 } 249 }
279 250
280 #if !defined(OS_CHROMEOS) 251 #if !defined(OS_CHROMEOS)
281 252
282 TEST_F(GAIAInfoUpdateServiceTest, LogOut) { 253 TEST_F(GAIAInfoUpdateServiceTest, LogOut) {
283 SigninManager* signin_manager = 254 SigninManager* signin_manager =
284 SigninManagerFactory::GetForProfile(profile()); 255 SigninManagerFactory::GetForProfile(profile());
285 signin_manager->SetAuthenticatedAccountInfo("gaia_id", "pat@example.com"); 256 signin_manager->SetAuthenticatedAccountInfo("gaia_id", "pat@example.com");
286 base::string16 gaia_name = base::UTF8ToUTF16("Pat Foo"); 257 base::string16 gaia_name = base::UTF8ToUTF16("Pat Foo");
287 GetCache()->SetGAIANameOfProfileAtIndex(0, gaia_name); 258 ProfileAttributesEntry* entry;
259 ASSERT_TRUE(
260 storage()->GetProfileAttributesWithPath(profile()->GetPath(), &entry));
261 entry->SetGAIAName(gaia_name);
288 gfx::Image gaia_picture = gfx::test::CreateImage(256,256); 262 gfx::Image gaia_picture = gfx::test::CreateImage(256,256);
289 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_picture); 263 entry->SetGAIAPicture(&gaia_picture);
290 264
291 // Set a fake picture URL. 265 // Set a fake picture URL.
292 profile()->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL, 266 profile()->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL,
293 "example.com"); 267 "example.com");
294 268
295 EXPECT_FALSE(service()->GetCachedPictureURL().empty()); 269 EXPECT_FALSE(service()->GetCachedPictureURL().empty());
296 270
297 // Log out. 271 // Log out.
298 signin_manager->SignOut(signin_metrics::SIGNOUT_TEST); 272 signin_manager->SignOut(signin_metrics::SIGNOUT_TEST);
299 // Verify that the GAIA name and picture, and picture URL are unset. 273 // Verify that the GAIA name and picture, and picture URL are unset.
300 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty()); 274 EXPECT_TRUE(entry->GetGAIAName().empty());
301 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); 275 EXPECT_EQ(NULL, entry->GetGAIAPicture());
302 EXPECT_TRUE(service()->GetCachedPictureURL().empty()); 276 EXPECT_TRUE(service()->GetCachedPictureURL().empty());
303 } 277 }
304 278
305 TEST_F(GAIAInfoUpdateServiceTest, LogIn) { 279 TEST_F(GAIAInfoUpdateServiceTest, LogIn) {
306 // Log in. 280 // Log in.
307 EXPECT_CALL(*service(), Update()); 281 EXPECT_CALL(*service(), Update());
308 std::string account_id = 282 std::string account_id =
309 AccountTrackerServiceFactory::GetForProfile(profile()) 283 AccountTrackerServiceFactory::GetForProfile(profile())
310 ->SeedAccountInfo("gaia_id", "pat@example.com"); 284 ->SeedAccountInfo("gaia_id", "pat@example.com");
311 SigninManager* signin_manager = 285 SigninManager* signin_manager =
312 SigninManagerFactory::GetForProfile(profile()); 286 SigninManagerFactory::GetForProfile(profile());
313 signin_manager->OnExternalSigninCompleted(account_id); 287 signin_manager->OnExternalSigninCompleted(account_id);
314 } 288 }
315 289
316 #endif 290 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698