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_builder.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/fake_account_fetcher_service.h"
20 #include "components/signin/core/browser/test_signin_client.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "net/url_request/test_url_fetcher_factory.h"
23 #include "testing/gtest/include/gtest/gtest.h"
27 const std::string kTestEmail
= "test@example.com";
28 const std::string kTestGaia
= "gaia";
29 const std::string kTestHostedDomain
= "google.com";
30 const std::string kTestFullName
= "full_name";
31 const std::string kTestGivenName
= "given_name";
32 const std::string kTestLocale
= "locale";
33 const std::string kTestPictureURL
= "http://www.google.com/";
37 class ProfileDownloaderTest
: public testing::Test
,
38 public ProfileDownloaderDelegate
{
40 ProfileDownloaderTest()
41 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
) {}
42 ~ProfileDownloaderTest() override
{}
44 void SetUp() override
{
45 TestingProfile::Builder builder
;
46 builder
.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
47 &BuildAutoIssuingFakeProfileOAuth2TokenService
);
48 builder
.AddTestingFactory(AccountFetcherServiceFactory::GetInstance(),
49 FakeAccountFetcherServiceBuilder::BuildForTests
);
50 builder
.AddTestingFactory(ChromeSigninClientFactory::GetInstance(),
51 signin::BuildTestSigninClient
);
53 profile_
= builder
.Build();
54 account_tracker_service_
=
55 AccountTrackerServiceFactory::GetForProfile(profile_
.get());
56 account_fetcher_service_
= static_cast<FakeAccountFetcherService
*>(
57 AccountFetcherServiceFactory::GetForProfile(profile_
.get()));
58 signin_client_
= static_cast<TestSigninClient
*>(
59 ChromeSigninClientFactory::GetForProfile(profile_
.get()));
60 signin_client_
->SetURLRequestContext(profile_
->GetRequestContext());
61 profile_downloader_
.reset(new ProfileDownloader(this));
64 bool NeedsProfilePicture() const override
{ return true; };
65 int GetDesiredImageSideLength() const override
{ return 128; };
66 std::string
GetCachedPictureURL() const override
{ return ""; };
67 Profile
* GetBrowserProfile() override
{ return profile_
.get(); };
68 bool IsPreSignin() const override
{ return false; }
69 void OnProfileDownloadSuccess(ProfileDownloader
* downloader
) override
{
72 void OnProfileDownloadFailure(
73 ProfileDownloader
* downloader
,
74 ProfileDownloaderDelegate::FailureReason reason
) override
{}
76 void SimulateUserInfoSuccess() {
77 account_fetcher_service_
->FakeUserInfoFetchSuccess(
78 account_tracker_service_
->PickAccountIdForAccount(kTestGaia
, kTestEmail
),
88 AccountTrackerService
* account_tracker_service_
;
89 FakeAccountFetcherService
* account_fetcher_service_
;
90 content::TestBrowserThreadBundle thread_bundle_
;
91 TestSigninClient
* signin_client_
;
92 scoped_ptr
<Profile
> profile_
;
93 scoped_ptr
<ProfileDownloader
> profile_downloader_
;
96 TEST_F(ProfileDownloaderTest
, AccountInfoReady
) {
97 std::string account_id
=
98 account_tracker_service_
->SeedAccountInfo(kTestGaia
, kTestEmail
);
99 SimulateUserInfoSuccess();
101 ASSERT_EQ(ProfileDownloader::PICTURE_FAILED
,
102 profile_downloader_
->GetProfilePictureStatus());
103 profile_downloader_
->StartForAccount(account_id
);
104 profile_downloader_
->StartFetchingImage();
105 ASSERT_EQ(kTestPictureURL
, profile_downloader_
->GetProfilePictureURL());
108 TEST_F(ProfileDownloaderTest
, AccountInfoNotReady
) {
109 std::string account_id
=
110 account_tracker_service_
->SeedAccountInfo(kTestGaia
, kTestEmail
);
112 ASSERT_EQ(ProfileDownloader::PICTURE_FAILED
,
113 profile_downloader_
->GetProfilePictureStatus());
114 profile_downloader_
->StartForAccount(account_id
);
115 profile_downloader_
->StartFetchingImage();
116 SimulateUserInfoSuccess();
117 ASSERT_EQ(kTestPictureURL
, profile_downloader_
->GetProfilePictureURL());
120 TEST_F(ProfileDownloaderTest
, DefaultURL
) {
121 // Empty URL should be default photo
122 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(std::string()));
123 // Picasa default photo
124 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(
125 "https://example.com/-4/AAAAAAAAAAA/AAAAAAAAAAE/G/s64-c/photo.jpg"));
126 // Not default G+ photo
127 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
128 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg"));
129 // Not default with 6 components
130 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
131 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg"));
132 // Not default with 7 components
133 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
134 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg"));