1 // Copyright (c) 2011 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.
5 #include "chrome/browser/profiles/profile_downloader.h"
7 #include "base/strings/string_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/profiles/profile_downloader_delegate.h"
10 #include "chrome/browser/signin/account_fetcher_service_factory.h"
11 #include "chrome/browser/signin/account_tracker_service_factory.h"
12 #include "chrome/browser/signin/chrome_signin_client_factory.h"
13 #include "chrome/browser/signin/fake_account_fetcher_service.h"
14 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
16 #include "chrome/browser/signin/test_signin_client_builder.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "components/signin/core/browser/account_tracker_service.h"
19 #include "components/signin/core/browser/test_signin_client.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "net/url_request/test_url_fetcher_factory.h"
22 #include "testing/gtest/include/gtest/gtest.h"
26 const std::string kTestEmail
= "test@example.com";
27 const std::string kTestGaia
= "gaia";
28 const std::string kTestHostedDomain
= "google.com";
29 const std::string kTestFullName
= "full_name";
30 const std::string kTestGivenName
= "given_name";
31 const std::string kTestLocale
= "locale";
32 const std::string kTestPictureURL
= "http://www.google.com/";
36 class ProfileDownloaderTest
: public testing::Test
,
37 public ProfileDownloaderDelegate
{
39 ProfileDownloaderTest()
40 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
) {}
41 ~ProfileDownloaderTest() override
{}
43 void SetUp() override
{
44 TestingProfile::Builder builder
;
45 builder
.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
46 &BuildAutoIssuingFakeProfileOAuth2TokenService
);
47 builder
.AddTestingFactory(AccountFetcherServiceFactory::GetInstance(),
48 FakeAccountFetcherService::BuildForTests
);
49 builder
.AddTestingFactory(ChromeSigninClientFactory::GetInstance(),
50 signin::BuildTestSigninClient
);
52 profile_
= builder
.Build();
53 account_tracker_service_
=
54 AccountTrackerServiceFactory::GetForProfile(profile_
.get());
55 account_fetcher_service_
= static_cast<FakeAccountFetcherService
*>(
56 AccountFetcherServiceFactory::GetForProfile(profile_
.get()));
57 signin_client_
= static_cast<TestSigninClient
*>(
58 ChromeSigninClientFactory::GetForProfile(profile_
.get()));
59 signin_client_
->SetURLRequestContext(profile_
->GetRequestContext());
60 profile_downloader_
.reset(new ProfileDownloader(this));
63 bool NeedsProfilePicture() const override
{ return true; };
64 int GetDesiredImageSideLength() const override
{ return 128; };
65 std::string
GetCachedPictureURL() const override
{ return ""; };
66 Profile
* GetBrowserProfile() override
{ return profile_
.get(); };
67 void OnProfileDownloadSuccess(ProfileDownloader
* downloader
) override
{
70 void OnProfileDownloadFailure(
71 ProfileDownloader
* downloader
,
72 ProfileDownloaderDelegate::FailureReason reason
) override
{}
74 void SimulateUserInfoSuccess() {
75 account_fetcher_service_
->FakeUserInfoFetchSuccess(
85 AccountTrackerService
* account_tracker_service_
;
86 FakeAccountFetcherService
* account_fetcher_service_
;
87 content::TestBrowserThreadBundle thread_bundle_
;
88 TestSigninClient
* signin_client_
;
89 scoped_ptr
<Profile
> profile_
;
90 scoped_ptr
<ProfileDownloader
> profile_downloader_
;
93 TEST_F(ProfileDownloaderTest
, AccountInfoReady
) {
94 account_tracker_service_
->SeedAccountInfo(kTestGaia
, kTestEmail
);
95 SimulateUserInfoSuccess();
97 ASSERT_EQ(ProfileDownloader::PICTURE_FAILED
,
98 profile_downloader_
->GetProfilePictureStatus());
99 profile_downloader_
->StartForAccount(kTestEmail
);
100 profile_downloader_
->StartFetchingImage();
101 ASSERT_EQ(kTestPictureURL
, profile_downloader_
->GetProfilePictureURL());
104 TEST_F(ProfileDownloaderTest
, AccountInfoNotReady
) {
105 account_tracker_service_
->SeedAccountInfo(kTestGaia
, kTestEmail
);
107 ASSERT_EQ(ProfileDownloader::PICTURE_FAILED
,
108 profile_downloader_
->GetProfilePictureStatus());
109 profile_downloader_
->StartForAccount(kTestEmail
);
110 profile_downloader_
->StartFetchingImage();
111 SimulateUserInfoSuccess();
112 ASSERT_EQ(kTestPictureURL
, profile_downloader_
->GetProfilePictureURL());
115 TEST_F(ProfileDownloaderTest
, DefaultURL
) {
116 // Empty URL should be default photo
117 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(std::string()));
118 // Picasa default photo
119 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(
120 "https://example.com/-4/AAAAAAAAAAA/AAAAAAAAAAE/G/s64-c/photo.jpg"));
121 // Not default G+ photo
122 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
123 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg"));
124 // Not default with 6 components
125 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
126 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg"));
127 // Not default with 7 components
128 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
129 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg"));