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

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

Issue 1794353003: Refactor ProfileInfoCache in c/b/profiles (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reverting a few changes Created 4 years, 8 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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include <string> 9 #include <string>
10 10
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "build/build_config.h" 12 #include "build/build_config.h"
13 #include "chrome/browser/browser_process.h" 13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/profiles/profile_attributes_entry.h"
15 #include "chrome/browser/profiles/profile_attributes_storage.h"
14 #include "chrome/browser/profiles/profile_downloader.h" 16 #include "chrome/browser/profiles/profile_downloader.h"
15 #include "chrome/browser/profiles/profile_info_cache.h" 17 #include "chrome/browser/profiles/profile_info_cache.h"
16 #include "chrome/browser/profiles/profile_info_cache_unittest.h" 18 #include "chrome/browser/profiles/profile_info_cache_unittest.h"
17 #include "chrome/browser/profiles/profiles_state.h" 19 #include "chrome/browser/profiles/profiles_state.h"
18 #include "chrome/browser/signin/account_tracker_service_factory.h" 20 #include "chrome/browser/signin/account_tracker_service_factory.h"
19 #include "chrome/browser/signin/chrome_signin_client_factory.h" 21 #include "chrome/browser/signin/chrome_signin_client_factory.h"
20 #include "chrome/browser/signin/signin_manager_factory.h" 22 #include "chrome/browser/signin/signin_manager_factory.h"
21 #include "chrome/browser/signin/test_signin_client_builder.h" 23 #include "chrome/browser/signin/test_signin_client_builder.h"
22 #include "chrome/common/pref_names.h" 24 #include "chrome/common/pref_names.h"
23 #include "chrome/test/base/testing_browser_process.h" 25 #include "chrome/test/base/testing_browser_process.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 explicit GAIAInfoUpdateServiceMock(Profile* profile) 61 explicit GAIAInfoUpdateServiceMock(Profile* profile)
60 : GAIAInfoUpdateService(profile) { 62 : GAIAInfoUpdateService(profile) {
61 } 63 }
62 64
63 virtual ~GAIAInfoUpdateServiceMock() { 65 virtual ~GAIAInfoUpdateServiceMock() {
64 } 66 }
65 67
66 MOCK_METHOD0(Update, void()); 68 MOCK_METHOD0(Update, void());
67 }; 69 };
68 70
71 // TODO(anthonyvd) : remove ProfileInfoCacheTest from the test fixture.
69 class GAIAInfoUpdateServiceTest : public ProfileInfoCacheTest { 72 class GAIAInfoUpdateServiceTest : public ProfileInfoCacheTest {
70 protected: 73 protected:
71 GAIAInfoUpdateServiceTest() : profile_(NULL) { 74 GAIAInfoUpdateServiceTest() : profile_(NULL) {
72 } 75 }
73 76
74 Profile* profile() { 77 Profile* profile() {
75 if (!profile_) 78 if (!profile_)
76 profile_ = CreateProfile("Person 1"); 79 profile_ = CreateProfile("Person 1");
77 return profile_; 80 return profile_;
78 } 81 }
79 82
83 ProfileAttributesStorage* storage() {
84 return testing_profile_manager_.profile_attributes_storage();
85 }
86
80 NiceMock<GAIAInfoUpdateServiceMock>* service() { return service_.get(); } 87 NiceMock<GAIAInfoUpdateServiceMock>* service() { return service_.get(); }
81 NiceMock<ProfileDownloaderMock>* downloader() { return downloader_.get(); } 88 NiceMock<ProfileDownloaderMock>* downloader() { return downloader_.get(); }
82 89
83 Profile* CreateProfile(const std::string& name) { 90 Profile* CreateProfile(const std::string& name) {
84 TestingProfile::TestingFactories testing_factories; 91 TestingProfile::TestingFactories testing_factories;
85 testing_factories.push_back(std::make_pair( 92 testing_factories.push_back(std::make_pair(
86 ChromeSigninClientFactory::GetInstance(), 93 ChromeSigninClientFactory::GetInstance(),
87 signin::BuildTestSigninClient)); 94 signin::BuildTestSigninClient));
88 Profile* profile = testing_profile_manager_.CreateTestingProfile( 95 Profile* profile = testing_profile_manager_.CreateTestingProfile(
89 name, scoped_ptr<syncable_prefs::PrefServiceSyncable>(), 96 name, scoped_ptr<syncable_prefs::PrefServiceSyncable>(),
90 base::UTF8ToUTF16(name), 0, std::string(), testing_factories); 97 base::UTF8ToUTF16(name), 0, std::string(), testing_factories);
91 // The testing manager sets the profile name manually, which counts as 98 // The testing manager sets the profile name manually, which counts as
92 // a user-customized profile name. Reset this to match the default name 99 // a user-customized profile name. Reset this to match the default name
93 // we are actually using. 100 // we are actually using.
94 size_t index = GetCache()->GetIndexOfProfileWithPath(profile->GetPath()); 101 ProfileAttributesEntry* entry = nullptr;
95 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index, true); 102 // TODO(anthonyvd) : refactor the function so the following assertion can be
103 // changed to ASSERT_TRUE.
104 DCHECK(storage()->GetProfileAttributesWithPath(profile->GetPath(), &entry));
105 entry->SetIsUsingDefaultName(true);
96 return profile; 106 return profile;
97 } 107 }
98 108
99 static std::string GivenName(const std::string& id) { 109 static std::string GivenName(const std::string& id) {
Mike Lerman 2016/03/30 01:18:11 These functions don't at all explain what they're
lwchkg 2016/03/31 19:32:26 Sorry for poor rebase. These functions are used by
100 return id + "first"; 110 return id + "first";
101 } 111 }
102 static std::string FullName(const std::string& id) { 112 static std::string FullName(const std::string& id) {
103 return GivenName(id) + " " + id + "last"; 113 return GivenName(id) + " " + id + "last";
104 } 114 }
105 static base::string16 GivenName16(const std::string& id) { 115 static base::string16 GivenName16(const std::string& id) {
106 return base::UTF8ToUTF16(GivenName(id)); 116 return base::UTF8ToUTF16(GivenName(id));
107 } 117 }
108 static base::string16 FullName16(const std::string& id) { 118 static base::string16 FullName16(const std::string& id) {
109 return base::UTF8ToUTF16(FullName(id)); 119 return base::UTF8ToUTF16(FullName(id));
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 GetString(prefs::kGoogleServicesHostedDomain)); 183 GetString(prefs::kGoogleServicesHostedDomain));
174 184
175 base::string16 name = base::ASCIIToUTF16("Pat Smith"); 185 base::string16 name = base::ASCIIToUTF16("Pat Smith");
176 base::string16 given_name = base::ASCIIToUTF16("Pat"); 186 base::string16 given_name = base::ASCIIToUTF16("Pat");
177 gfx::Image image = gfx::test::CreateImage(256, 256); 187 gfx::Image image = gfx::test::CreateImage(256, 256);
178 std::string url("foo.com"); 188 std::string url("foo.com");
179 base::string16 hosted_domain(base::ASCIIToUTF16("")); 189 base::string16 hosted_domain(base::ASCIIToUTF16(""));
180 ProfileDownloadSuccess(name, given_name, image, url, hosted_domain); 190 ProfileDownloadSuccess(name, given_name, image, url, hosted_domain);
181 191
182 // On success the GAIA info should be updated. 192 // On success the GAIA info should be updated.
183 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); 193 ProfileAttributesEntry* entry;
184 EXPECT_EQ(name, GetCache()->GetGAIANameOfProfileAtIndex(index)); 194 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(profile()->GetPath(),
185 EXPECT_EQ(given_name, GetCache()->GetGAIAGivenNameOfProfileAtIndex(index)); 195 &entry));
186 EXPECT_TRUE(gfx::test::AreImagesEqual( 196 EXPECT_EQ(name, entry->GetGAIAName());
187 image, *GetCache()->GetGAIAPictureOfProfileAtIndex(index))); 197 EXPECT_EQ(given_name, entry->GetGAIAGivenName());
198 EXPECT_TRUE(gfx::test::AreImagesEqual(image, *entry->GetGAIAPicture()));
188 EXPECT_EQ(url, service()->GetCachedPictureURL()); 199 EXPECT_EQ(url, service()->GetCachedPictureURL());
189 EXPECT_EQ(Profile::kNoHostedDomainFound, profile()->GetPrefs()-> 200 EXPECT_EQ(Profile::kNoHostedDomainFound, profile()->GetPrefs()->
190 GetString(prefs::kGoogleServicesHostedDomain)); 201 GetString(prefs::kGoogleServicesHostedDomain));
191 } 202 }
192 203
193 TEST_F(GAIAInfoUpdateServiceTest, DownloadFailure) { 204 TEST_F(GAIAInfoUpdateServiceTest, DownloadFailure) {
194 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); 205 ProfileAttributesEntry* entry;
195 base::string16 old_name = GetCache()->GetNameOfProfileAtIndex(index); 206 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(profile()->GetPath(),
196 gfx::Image old_image = GetCache()->GetAvatarIconOfProfileAtIndex(index); 207 &entry));
208 base::string16 old_name = entry->GetName();
209 gfx::Image old_image = entry->GetAvatarIcon();
197 210
198 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); 211 EXPECT_EQ(std::string(), service()->GetCachedPictureURL());
199 212
200 service()->OnProfileDownloadFailure(downloader(), 213 service()->OnProfileDownloadFailure(downloader(),
201 ProfileDownloaderDelegate::SERVICE_ERROR); 214 ProfileDownloaderDelegate::SERVICE_ERROR);
202 215
203 // On failure nothing should be updated. 216 // On failure nothing should be updated.
204 EXPECT_EQ(old_name, GetCache()->GetNameOfProfileAtIndex(index)); 217 EXPECT_EQ(old_name, entry->GetName());
205 EXPECT_EQ(base::string16(), GetCache()->GetGAIANameOfProfileAtIndex(index)); 218 EXPECT_EQ(base::string16(), entry->GetGAIAName());
206 EXPECT_EQ(base::string16(), 219 EXPECT_EQ(base::string16(), entry->GetGAIAGivenName());
207 GetCache()->GetGAIAGivenNameOfProfileAtIndex(index)); 220 EXPECT_TRUE(gfx::test::AreImagesEqual(old_image, entry->GetAvatarIcon()));
208 EXPECT_TRUE(gfx::test::AreImagesEqual( 221 EXPECT_EQ(nullptr, entry->GetGAIAPicture());
209 old_image, GetCache()->GetAvatarIconOfProfileAtIndex(index)));
210 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(index));
211 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); 222 EXPECT_EQ(std::string(), service()->GetCachedPictureURL());
212 EXPECT_EQ(std::string(), 223 EXPECT_EQ(std::string(),
213 profile()->GetPrefs()->GetString(prefs::kGoogleServicesHostedDomain)); 224 profile()->GetPrefs()->GetString(prefs::kGoogleServicesHostedDomain));
214 } 225 }
215 226
216 TEST_F(GAIAInfoUpdateServiceTest, ProfileLockEnabledForWhitelist) { 227 TEST_F(GAIAInfoUpdateServiceTest, ProfileLockEnabledForWhitelist) {
217 // No URL should be cached yet. 228 // No URL should be cached yet.
218 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); 229 EXPECT_EQ(std::string(), service()->GetCachedPictureURL());
219 230
220 base::string16 name = base::ASCIIToUTF16("Pat Smith"); 231 base::string16 name = base::ASCIIToUTF16("Pat Smith");
221 base::string16 given_name = base::ASCIIToUTF16("Pat"); 232 base::string16 given_name = base::ASCIIToUTF16("Pat");
222 gfx::Image image = gfx::test::CreateImage(256, 256); 233 gfx::Image image = gfx::test::CreateImage(256, 256);
223 std::string url("foo.com"); 234 std::string url("foo.com");
224 base::string16 hosted_domain(base::ASCIIToUTF16("google.com")); 235 base::string16 hosted_domain(base::ASCIIToUTF16("google.com"));
225 ProfileDownloadSuccess(name, given_name, image, url, hosted_domain); 236 ProfileDownloadSuccess(name, given_name, image, url, hosted_domain);
226 237
227 EXPECT_EQ("google.com", profile()->GetPrefs()-> 238 EXPECT_EQ("google.com", profile()->GetPrefs()->
228 GetString(prefs::kGoogleServicesHostedDomain)); 239 GetString(prefs::kGoogleServicesHostedDomain));
229 } 240 }
230 241
242 // TODO(anthonyvd) : remove or update test once the refactoring of the internals
243 // of ProfileInfoCache is complete.
231 TEST_F(GAIAInfoUpdateServiceTest, HandlesProfileReordering) { 244 TEST_F(GAIAInfoUpdateServiceTest, HandlesProfileReordering) {
232 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); 245 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath());
233 GetCache()->SetNameOfProfileAtIndex(index, FullName16("B")); 246 GetCache()->SetNameOfProfileAtIndex(index, FullName16("B"));
234 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index, true); 247 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index, true);
235 248
236 CreateProfile(FullName("A")); 249 CreateProfile(FullName("A"));
237 CreateProfile(FullName("C")); 250 CreateProfile(FullName("C"));
238 CreateProfile(FullName("E")); 251 CreateProfile(FullName("E"));
239 252
240 size_t index_before = 253 size_t index_before =
Mike Lerman 2016/03/30 01:18:11 before what? the index of the profile before mine?
lwchkg 2016/03/31 19:32:26 Sorry again for poor rebase. The whole test looks
241 GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); 254 GetCache()->GetIndexOfProfileWithPath(profile()->GetPath());
242 255
243 // Rename our profile. 256 // Rename our profile.
244 RenameProfile(FullName16("D"), GivenName16("D")); 257 RenameProfile(FullName16("D"), GivenName16("D"));
245 // Profiles should have been reordered in the cache. 258 // Profiles should have been reordered in the cache.
246 EXPECT_NE(index_before, 259 EXPECT_NE(index_before,
247 GetCache()->GetIndexOfProfileWithPath(profile()->GetPath())); 260 GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()));
248 // Rename the profile back to the original name, it should go back to its 261 // Rename the profile back to the original name, it should go back to its
249 // original position. 262 // original position.
250 RenameProfile(FullName16("B"), GivenName16("B")); 263 RenameProfile(FullName16("B"), GivenName16("B"));
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 EXPECT_TRUE(service()->timer_.IsRunning()); 296 EXPECT_TRUE(service()->timer_.IsRunning());
284 } 297 }
285 298
286 #if !defined(OS_CHROMEOS) 299 #if !defined(OS_CHROMEOS)
287 300
288 TEST_F(GAIAInfoUpdateServiceTest, LogOut) { 301 TEST_F(GAIAInfoUpdateServiceTest, LogOut) {
289 SigninManager* signin_manager = 302 SigninManager* signin_manager =
290 SigninManagerFactory::GetForProfile(profile()); 303 SigninManagerFactory::GetForProfile(profile());
291 signin_manager->SetAuthenticatedAccountInfo("gaia_id", "pat@example.com"); 304 signin_manager->SetAuthenticatedAccountInfo("gaia_id", "pat@example.com");
292 base::string16 gaia_name = base::UTF8ToUTF16("Pat Foo"); 305 base::string16 gaia_name = base::UTF8ToUTF16("Pat Foo");
293 GetCache()->SetGAIANameOfProfileAtIndex(0, gaia_name); 306
307 ASSERT_EQ(1u, storage()->GetNumberOfProfiles());
308 ProfileAttributesEntry* entry = storage()->GetAllProfilesAttributes().front();
309 entry->SetGAIAName(gaia_name);
294 gfx::Image gaia_picture = gfx::test::CreateImage(256, 256); 310 gfx::Image gaia_picture = gfx::test::CreateImage(256, 256);
295 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_picture); 311 entry->SetGAIAPicture(&gaia_picture);
296 312
297 // Set a fake picture URL. 313 // Set a fake picture URL.
298 profile()->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL, 314 profile()->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL,
299 "example.com"); 315 "example.com");
300 316
301 EXPECT_FALSE(service()->GetCachedPictureURL().empty()); 317 EXPECT_FALSE(service()->GetCachedPictureURL().empty());
302 318
303 // Log out. 319 // Log out.
304 signin_manager->SignOut(signin_metrics::SIGNOUT_TEST, 320 signin_manager->SignOut(signin_metrics::SIGNOUT_TEST,
305 signin_metrics::SignoutDelete::IGNORE_METRIC); 321 signin_metrics::SignoutDelete::IGNORE_METRIC);
306 // Verify that the GAIA name and picture, and picture URL are unset. 322 // Verify that the GAIA name and picture, and picture URL are unset.
307 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty()); 323 EXPECT_TRUE(entry->GetGAIAName().empty());
308 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); 324 EXPECT_EQ(nullptr, entry->GetGAIAPicture());
309 EXPECT_TRUE(service()->GetCachedPictureURL().empty()); 325 EXPECT_TRUE(service()->GetCachedPictureURL().empty());
310 } 326 }
311 327
312 TEST_F(GAIAInfoUpdateServiceTest, LogIn) { 328 TEST_F(GAIAInfoUpdateServiceTest, LogIn) {
313 // Log in. 329 // Log in.
314 EXPECT_CALL(*service(), Update()); 330 EXPECT_CALL(*service(), Update());
315 AccountTrackerServiceFactory::GetForProfile(profile()) 331 AccountTrackerServiceFactory::GetForProfile(profile())
316 ->SeedAccountInfo("gaia_id", "pat@example.com"); 332 ->SeedAccountInfo("gaia_id", "pat@example.com");
317 SigninManager* signin_manager = 333 SigninManager* signin_manager =
318 SigninManagerFactory::GetForProfile(profile()); 334 SigninManagerFactory::GetForProfile(profile());
319 signin_manager->OnExternalSigninCompleted("pat@example.com"); 335 signin_manager->OnExternalSigninCompleted("pat@example.com");
320 } 336 }
321 337
322 #endif 338 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698