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

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: Fix a bug that causes unit tests without debug code to fail. 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, std::unique_ptr<syncable_prefs::PrefServiceSyncable>(), 96 name, std::unique_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 EXPECT_TRUE(storage()->GetProfileAttributesWithPath(profile->GetPath(),
105 &entry));
106 entry->SetIsUsingDefaultName(true);
96 return profile; 107 return profile;
97 } 108 }
98 109
99 static std::string GivenName(const std::string& id) { 110 static std::string GivenName(const std::string& id) {
100 return id + "first"; 111 return id + "first";
101 } 112 }
102 static std::string FullName(const std::string& id) { 113 static std::string FullName(const std::string& id) {
103 return GivenName(id) + " " + id + "last"; 114 return GivenName(id) + " " + id + "last";
104 } 115 }
105 static base::string16 GivenName16(const std::string& id) { 116 static base::string16 GivenName16(const std::string& id) {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 GetString(prefs::kGoogleServicesHostedDomain)); 184 GetString(prefs::kGoogleServicesHostedDomain));
174 185
175 base::string16 name = base::ASCIIToUTF16("Pat Smith"); 186 base::string16 name = base::ASCIIToUTF16("Pat Smith");
176 base::string16 given_name = base::ASCIIToUTF16("Pat"); 187 base::string16 given_name = base::ASCIIToUTF16("Pat");
177 gfx::Image image = gfx::test::CreateImage(256, 256); 188 gfx::Image image = gfx::test::CreateImage(256, 256);
178 std::string url("foo.com"); 189 std::string url("foo.com");
179 base::string16 hosted_domain(base::ASCIIToUTF16("")); 190 base::string16 hosted_domain(base::ASCIIToUTF16(""));
180 ProfileDownloadSuccess(name, given_name, image, url, hosted_domain); 191 ProfileDownloadSuccess(name, given_name, image, url, hosted_domain);
181 192
182 // On success the GAIA info should be updated. 193 // On success the GAIA info should be updated.
183 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); 194 ProfileAttributesEntry* entry;
184 EXPECT_EQ(name, GetCache()->GetGAIANameOfProfileAtIndex(index)); 195 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(profile()->GetPath(),
185 EXPECT_EQ(given_name, GetCache()->GetGAIAGivenNameOfProfileAtIndex(index)); 196 &entry));
186 EXPECT_TRUE(gfx::test::AreImagesEqual( 197 EXPECT_EQ(name, entry->GetGAIAName());
187 image, *GetCache()->GetGAIAPictureOfProfileAtIndex(index))); 198 EXPECT_EQ(given_name, entry->GetGAIAGivenName());
199 EXPECT_TRUE(gfx::test::AreImagesEqual(image, *entry->GetGAIAPicture()));
188 EXPECT_EQ(url, service()->GetCachedPictureURL()); 200 EXPECT_EQ(url, service()->GetCachedPictureURL());
189 EXPECT_EQ(Profile::kNoHostedDomainFound, profile()->GetPrefs()-> 201 EXPECT_EQ(Profile::kNoHostedDomainFound, profile()->GetPrefs()->
190 GetString(prefs::kGoogleServicesHostedDomain)); 202 GetString(prefs::kGoogleServicesHostedDomain));
191 } 203 }
192 204
193 TEST_F(GAIAInfoUpdateServiceTest, DownloadFailure) { 205 TEST_F(GAIAInfoUpdateServiceTest, DownloadFailure) {
194 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); 206 ProfileAttributesEntry* entry;
195 base::string16 old_name = GetCache()->GetNameOfProfileAtIndex(index); 207 ASSERT_TRUE(storage()->GetProfileAttributesWithPath(profile()->GetPath(),
196 gfx::Image old_image = GetCache()->GetAvatarIconOfProfileAtIndex(index); 208 &entry));
209 base::string16 old_name = entry->GetName();
210 gfx::Image old_image = entry->GetAvatarIcon();
197 211
198 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); 212 EXPECT_EQ(std::string(), service()->GetCachedPictureURL());
199 213
200 service()->OnProfileDownloadFailure(downloader(), 214 service()->OnProfileDownloadFailure(downloader(),
201 ProfileDownloaderDelegate::SERVICE_ERROR); 215 ProfileDownloaderDelegate::SERVICE_ERROR);
202 216
203 // On failure nothing should be updated. 217 // On failure nothing should be updated.
204 EXPECT_EQ(old_name, GetCache()->GetNameOfProfileAtIndex(index)); 218 EXPECT_EQ(old_name, entry->GetName());
205 EXPECT_EQ(base::string16(), GetCache()->GetGAIANameOfProfileAtIndex(index)); 219 EXPECT_EQ(base::string16(), entry->GetGAIAName());
206 EXPECT_EQ(base::string16(), 220 EXPECT_EQ(base::string16(), entry->GetGAIAGivenName());
207 GetCache()->GetGAIAGivenNameOfProfileAtIndex(index)); 221 EXPECT_TRUE(gfx::test::AreImagesEqual(old_image, entry->GetAvatarIcon()));
208 EXPECT_TRUE(gfx::test::AreImagesEqual( 222 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()); 223 EXPECT_EQ(std::string(), service()->GetCachedPictureURL());
212 EXPECT_EQ(std::string(), 224 EXPECT_EQ(std::string(),
213 profile()->GetPrefs()->GetString(prefs::kGoogleServicesHostedDomain)); 225 profile()->GetPrefs()->GetString(prefs::kGoogleServicesHostedDomain));
214 } 226 }
215 227
216 TEST_F(GAIAInfoUpdateServiceTest, ProfileLockEnabledForWhitelist) { 228 TEST_F(GAIAInfoUpdateServiceTest, ProfileLockEnabledForWhitelist) {
217 // No URL should be cached yet. 229 // No URL should be cached yet.
218 EXPECT_EQ(std::string(), service()->GetCachedPictureURL()); 230 EXPECT_EQ(std::string(), service()->GetCachedPictureURL());
219 231
220 base::string16 name = base::ASCIIToUTF16("Pat Smith"); 232 base::string16 name = base::ASCIIToUTF16("Pat Smith");
221 base::string16 given_name = base::ASCIIToUTF16("Pat"); 233 base::string16 given_name = base::ASCIIToUTF16("Pat");
222 gfx::Image image = gfx::test::CreateImage(256, 256); 234 gfx::Image image = gfx::test::CreateImage(256, 256);
223 std::string url("foo.com"); 235 std::string url("foo.com");
224 base::string16 hosted_domain(base::ASCIIToUTF16("google.com")); 236 base::string16 hosted_domain(base::ASCIIToUTF16("google.com"));
225 ProfileDownloadSuccess(name, given_name, image, url, hosted_domain); 237 ProfileDownloadSuccess(name, given_name, image, url, hosted_domain);
226 238
227 EXPECT_EQ("google.com", profile()->GetPrefs()-> 239 EXPECT_EQ("google.com", profile()->GetPrefs()->
228 GetString(prefs::kGoogleServicesHostedDomain)); 240 GetString(prefs::kGoogleServicesHostedDomain));
229 } 241 }
230 242
243 // TODO(anthonyvd) : remove or update test once the refactoring of the internals
244 // of ProfileInfoCache is complete.
231 TEST_F(GAIAInfoUpdateServiceTest, HandlesProfileReordering) { 245 TEST_F(GAIAInfoUpdateServiceTest, HandlesProfileReordering) {
232 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath()); 246 size_t index = GetCache()->GetIndexOfProfileWithPath(profile()->GetPath());
233 GetCache()->SetNameOfProfileAtIndex(index, FullName16("B")); 247 GetCache()->SetNameOfProfileAtIndex(index, FullName16("B"));
234 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index, true); 248 GetCache()->SetProfileIsUsingDefaultNameAtIndex(index, true);
235 249
236 CreateProfile(FullName("A")); 250 CreateProfile(FullName("A"));
237 CreateProfile(FullName("C")); 251 CreateProfile(FullName("C"));
238 CreateProfile(FullName("E")); 252 CreateProfile(FullName("E"));
239 253
240 size_t index_before = 254 size_t index_before =
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 EXPECT_TRUE(service()->timer_.IsRunning()); 297 EXPECT_TRUE(service()->timer_.IsRunning());
284 } 298 }
285 299
286 #if !defined(OS_CHROMEOS) 300 #if !defined(OS_CHROMEOS)
287 301
288 TEST_F(GAIAInfoUpdateServiceTest, LogOut) { 302 TEST_F(GAIAInfoUpdateServiceTest, LogOut) {
289 SigninManager* signin_manager = 303 SigninManager* signin_manager =
290 SigninManagerFactory::GetForProfile(profile()); 304 SigninManagerFactory::GetForProfile(profile());
291 signin_manager->SetAuthenticatedAccountInfo("gaia_id", "pat@example.com"); 305 signin_manager->SetAuthenticatedAccountInfo("gaia_id", "pat@example.com");
292 base::string16 gaia_name = base::UTF8ToUTF16("Pat Foo"); 306 base::string16 gaia_name = base::UTF8ToUTF16("Pat Foo");
293 GetCache()->SetGAIANameOfProfileAtIndex(0, gaia_name); 307
308 ASSERT_EQ(1u, storage()->GetNumberOfProfiles());
309 ProfileAttributesEntry* entry = storage()->GetAllProfilesAttributes().front();
310 entry->SetGAIAName(gaia_name);
294 gfx::Image gaia_picture = gfx::test::CreateImage(256, 256); 311 gfx::Image gaia_picture = gfx::test::CreateImage(256, 256);
295 GetCache()->SetGAIAPictureOfProfileAtIndex(0, &gaia_picture); 312 entry->SetGAIAPicture(&gaia_picture);
296 313
297 // Set a fake picture URL. 314 // Set a fake picture URL.
298 profile()->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL, 315 profile()->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL,
299 "example.com"); 316 "example.com");
300 317
301 EXPECT_FALSE(service()->GetCachedPictureURL().empty()); 318 EXPECT_FALSE(service()->GetCachedPictureURL().empty());
302 319
303 // Log out. 320 // Log out.
304 signin_manager->SignOut(signin_metrics::SIGNOUT_TEST, 321 signin_manager->SignOut(signin_metrics::SIGNOUT_TEST,
305 signin_metrics::SignoutDelete::IGNORE_METRIC); 322 signin_metrics::SignoutDelete::IGNORE_METRIC);
306 // Verify that the GAIA name and picture, and picture URL are unset. 323 // Verify that the GAIA name and picture, and picture URL are unset.
307 EXPECT_TRUE(GetCache()->GetGAIANameOfProfileAtIndex(0).empty()); 324 EXPECT_TRUE(entry->GetGAIAName().empty());
308 EXPECT_EQ(NULL, GetCache()->GetGAIAPictureOfProfileAtIndex(0)); 325 EXPECT_EQ(nullptr, entry->GetGAIAPicture());
309 EXPECT_TRUE(service()->GetCachedPictureURL().empty()); 326 EXPECT_TRUE(service()->GetCachedPictureURL().empty());
310 } 327 }
311 328
312 TEST_F(GAIAInfoUpdateServiceTest, LogIn) { 329 TEST_F(GAIAInfoUpdateServiceTest, LogIn) {
313 // Log in. 330 // Log in.
314 EXPECT_CALL(*service(), Update()); 331 EXPECT_CALL(*service(), Update());
315 AccountTrackerServiceFactory::GetForProfile(profile()) 332 AccountTrackerServiceFactory::GetForProfile(profile())
316 ->SeedAccountInfo("gaia_id", "pat@example.com"); 333 ->SeedAccountInfo("gaia_id", "pat@example.com");
317 SigninManager* signin_manager = 334 SigninManager* signin_manager =
318 SigninManagerFactory::GetForProfile(profile()); 335 SigninManagerFactory::GetForProfile(profile());
319 signin_manager->OnExternalSigninCompleted("pat@example.com"); 336 signin_manager->OnExternalSigninCompleted("pat@example.com");
320 } 337 }
321 338
322 #endif 339 #endif
OLDNEW
« no previous file with comments | « chrome/browser/profiles/gaia_info_update_service.cc ('k') | chrome/browser/profiles/profile_attributes_entry.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698