| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/profile_downloader.h" | 5 #include "chrome/browser/profiles/profile_downloader.h" |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/profiles/profile_downloader_delegate.h" |
| 10 #include "chrome/browser/signin/account_tracker_service_factory.h" |
| 11 #include "chrome/browser/signin/chrome_signin_client_factory.h" |
| 12 #include "chrome/browser/signin/fake_account_tracker_service.h" |
| 13 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" |
| 14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 15 #include "chrome/browser/signin/test_signin_client_builder.h" |
| 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "components/signin/core/browser/test_signin_client.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "net/url_request/test_url_fetcher_factory.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 21 |
| 11 namespace { | 22 namespace { |
| 12 | 23 |
| 13 void GetJSonData(const std::string& full_name, | 24 const std::string kTestEmail = "test@example.com"; |
| 14 const std::string& given_name, | 25 const std::string kTestGaia = "gaia"; |
| 15 const std::string& url, | 26 const std::string kTestHostedDomain = "google.com"; |
| 16 const std::string& locale, | 27 const std::string kTestFullName = "full_name"; |
| 17 const std::string& hosted_domain, | 28 const std::string kTestGivenName = "given_name"; |
| 18 bool include_empty_hosted_domain, | 29 const std::string kTestLocale = "locale"; |
| 19 base::DictionaryValue* dict) { | 30 const std::string kTestPictureURL = "http://www.google.com/"; |
| 20 if (!full_name.empty()) | |
| 21 dict->SetString("name", full_name); | |
| 22 | |
| 23 if (!given_name.empty()) | |
| 24 dict->SetString("given_name", given_name); | |
| 25 | |
| 26 if (!url.empty()) | |
| 27 dict->SetString("picture", url); | |
| 28 | |
| 29 if (!locale.empty()) | |
| 30 dict->SetString("locale", locale); | |
| 31 | |
| 32 if (!hosted_domain.empty() || include_empty_hosted_domain) | |
| 33 dict->SetString("hd", hosted_domain); | |
| 34 } | |
| 35 | 31 |
| 36 } // namespace | 32 } // namespace |
| 37 | 33 |
| 38 class ProfileDownloaderTest : public testing::Test { | 34 class ProfileDownloaderTest : public testing::Test, |
| 35 public ProfileDownloaderDelegate { |
| 39 protected: | 36 protected: |
| 40 ProfileDownloaderTest() { | 37 ProfileDownloaderTest() |
| 38 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 39 ~ProfileDownloaderTest() override {} |
| 40 |
| 41 void SetUp() override { |
| 42 TestingProfile::Builder builder; |
| 43 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 44 &BuildAutoIssuingFakeProfileOAuth2TokenService); |
| 45 builder.AddTestingFactory(AccountTrackerServiceFactory::GetInstance(), |
| 46 FakeAccountTrackerService::Build); |
| 47 builder.AddTestingFactory(ChromeSigninClientFactory::GetInstance(), |
| 48 signin::BuildTestSigninClient); |
| 49 |
| 50 profile_ = builder.Build(); |
| 51 account_tracker_service_ = static_cast<FakeAccountTrackerService*>( |
| 52 AccountTrackerServiceFactory::GetForProfile(profile_.get())); |
| 53 signin_client_ = static_cast<TestSigninClient*>( |
| 54 ChromeSigninClientFactory::GetForProfile(profile_.get())); |
| 55 signin_client_->SetURLRequestContext(profile_->GetRequestContext()); |
| 56 profile_downloader_.reset( |
| 57 new ProfileDownloader(this, signin_client_)); |
| 41 } | 58 } |
| 42 | 59 |
| 43 ~ProfileDownloaderTest() override {} | 60 bool NeedsProfilePicture() const override { return true; }; |
| 61 int GetDesiredImageSideLength() const override { return 128; }; |
| 62 std::string GetCachedPictureURL() const override { return ""; }; |
| 63 Profile* GetBrowserProfile() override { return profile_.get(); }; |
| 64 void OnProfileDownloadSuccess(ProfileDownloader* downloader) override { |
| 44 | 65 |
| 45 void VerifyWithAccountData(const std::string& full_name, | 66 } |
| 46 const std::string& given_name, | 67 void OnProfileDownloadFailure( |
| 47 const std::string& url, | 68 ProfileDownloader* downloader, |
| 48 const std::string& expected_url, | 69 ProfileDownloaderDelegate::FailureReason reason) override {} |
| 49 const std::string& locale, | |
| 50 const std::string& hosted_domain, | |
| 51 bool include_empty_hosted_domain, | |
| 52 bool is_valid) { | |
| 53 base::string16 parsed_full_name; | |
| 54 base::string16 parsed_given_name; | |
| 55 std::string parsed_url; | |
| 56 std::string parsed_locale; | |
| 57 base::string16 parsed_hosted_domain; | |
| 58 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | |
| 59 GetJSonData(full_name, given_name, url, locale, hosted_domain, | |
| 60 include_empty_hosted_domain, dict.get()); | |
| 61 bool result = ProfileDownloader::ParseProfileJSON( | |
| 62 dict.get(), | |
| 63 &parsed_full_name, | |
| 64 &parsed_given_name, | |
| 65 &parsed_url, | |
| 66 32, | |
| 67 &parsed_locale, | |
| 68 &parsed_hosted_domain); | |
| 69 EXPECT_EQ(is_valid, result); | |
| 70 std::string parsed_full_name_utf8 = base::UTF16ToUTF8(parsed_full_name); | |
| 71 std::string parsed_given_name_utf8 = base::UTF16ToUTF8(parsed_given_name); | |
| 72 std::string parsed_hosted_domain_utf8 = | |
| 73 base::UTF16ToUTF8(parsed_hosted_domain); | |
| 74 | 70 |
| 75 EXPECT_EQ(full_name, parsed_full_name_utf8); | 71 void SimulateUserInfoSuccess() { |
| 76 EXPECT_EQ(given_name, parsed_given_name_utf8); | 72 account_tracker_service_->FakeUserInfoFetchSuccess( |
| 77 EXPECT_EQ(expected_url, parsed_url); | 73 kTestEmail, |
| 78 EXPECT_EQ(locale, parsed_locale); | 74 kTestGaia, |
| 79 EXPECT_EQ(hosted_domain, parsed_hosted_domain_utf8); | 75 kTestHostedDomain, |
| 76 kTestFullName, |
| 77 kTestGivenName, |
| 78 kTestLocale, |
| 79 kTestPictureURL); |
| 80 } | 80 } |
| 81 |
| 82 FakeAccountTrackerService* account_tracker_service_; |
| 83 content::TestBrowserThreadBundle thread_bundle_; |
| 84 TestSigninClient* signin_client_; |
| 85 scoped_ptr<Profile> profile_; |
| 86 scoped_ptr<ProfileDownloader> profile_downloader_; |
| 81 }; | 87 }; |
| 82 | 88 |
| 83 TEST_F(ProfileDownloaderTest, ParseData) { | 89 TEST_F(ProfileDownloaderTest, AccountInfoReady) { |
| 84 // URL without size specified. | 90 account_tracker_service_->SeedAccountInfo(kTestGaia, kTestEmail); |
| 85 VerifyWithAccountData( | 91 SimulateUserInfoSuccess(); |
| 86 "Pat Smith", | |
| 87 "Pat", | |
| 88 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg", | |
| 89 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg", | |
| 90 "en-US", | |
| 91 "google.com", | |
| 92 false, | |
| 93 true); | |
| 94 | 92 |
| 95 // URL with size specified. | 93 ASSERT_EQ(ProfileDownloader::PICTURE_FAILED, |
| 96 VerifyWithAccountData( | 94 profile_downloader_->GetProfilePictureStatus()); |
| 97 "Pat Smith", | 95 profile_downloader_->StartForAccount(kTestEmail); |
| 98 "Pat", | 96 profile_downloader_->StartFetchingImage(); |
| 99 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s64-c/1234567890.jpg", | 97 ASSERT_EQ(kTestPictureURL, profile_downloader_->GetProfilePictureURL()); |
| 100 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s32-c/1234567890.jpg", | 98 } |
| 101 "en-US", | |
| 102 "google.com", | |
| 103 false, | |
| 104 true); | |
| 105 | 99 |
| 106 // URL with unknown format. | 100 TEST_F(ProfileDownloaderTest, AccountInfoNotReady) { |
| 107 VerifyWithAccountData("Pat Smith", | 101 account_tracker_service_->SeedAccountInfo(kTestGaia, kTestEmail); |
| 108 "Pat", | |
| 109 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/", | |
| 110 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/", | |
| 111 "en-US", | |
| 112 "google.com", | |
| 113 false, | |
| 114 true); | |
| 115 | 102 |
| 116 // Try different locales. URL with size specified. | 103 ASSERT_EQ(ProfileDownloader::PICTURE_FAILED, |
| 117 VerifyWithAccountData( | 104 profile_downloader_->GetProfilePictureStatus()); |
| 118 "Pat Smith", | 105 profile_downloader_->StartForAccount(kTestEmail); |
| 119 "Pat", | 106 profile_downloader_->StartFetchingImage(); |
| 120 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s64-c/1234567890.jpg", | 107 SimulateUserInfoSuccess(); |
| 121 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s32-c/1234567890.jpg", | 108 ASSERT_EQ(kTestPictureURL, profile_downloader_->GetProfilePictureURL()); |
| 122 "jp", | |
| 123 "google.com", | |
| 124 false, | |
| 125 true); | |
| 126 | |
| 127 // URL with unknown format. | |
| 128 VerifyWithAccountData("Pat Smith", | |
| 129 "Pat", | |
| 130 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/", | |
| 131 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/", | |
| 132 "fr", | |
| 133 "", | |
| 134 false, | |
| 135 true); | |
| 136 | |
| 137 // Data with only name. | |
| 138 VerifyWithAccountData("Pat Smith", | |
| 139 "Pat", | |
| 140 std::string(), | |
| 141 std::string(), | |
| 142 std::string(), | |
| 143 std::string(), | |
| 144 false, | |
| 145 true); | |
| 146 | |
| 147 // Data with only name and a blank but present hosted domain. | |
| 148 VerifyWithAccountData("Pat Smith", | |
| 149 "Pat", | |
| 150 std::string(), | |
| 151 std::string(), | |
| 152 std::string(), | |
| 153 std::string(), | |
| 154 true, | |
| 155 true); | |
| 156 | |
| 157 // Data with only URL. | |
| 158 VerifyWithAccountData( | |
| 159 std::string(), | |
| 160 std::string(), | |
| 161 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg", | |
| 162 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg", | |
| 163 std::string(), | |
| 164 std::string(), | |
| 165 false, | |
| 166 true); | |
| 167 | |
| 168 // Data with only locale. | |
| 169 VerifyWithAccountData(std::string(), | |
| 170 std::string(), | |
| 171 std::string(), | |
| 172 std::string(), | |
| 173 "fr", | |
| 174 std::string(), | |
| 175 false, | |
| 176 false); | |
| 177 | |
| 178 // Data without name or URL or locale. | |
| 179 VerifyWithAccountData(std::string(), | |
| 180 std::string(), | |
| 181 std::string(), | |
| 182 std::string(), | |
| 183 std::string(), | |
| 184 std::string(), | |
| 185 false, | |
| 186 false); | |
| 187 | |
| 188 // Data with an invalid URL. | |
| 189 VerifyWithAccountData(std::string(), | |
| 190 std::string(), | |
| 191 "invalid url", | |
| 192 std::string(), | |
| 193 std::string(), | |
| 194 std::string(), | |
| 195 false, | |
| 196 false); | |
| 197 } | 109 } |
| 198 | 110 |
| 199 TEST_F(ProfileDownloaderTest, DefaultURL) { | 111 TEST_F(ProfileDownloaderTest, DefaultURL) { |
| 200 // Empty URL should be default photo | 112 // Empty URL should be default photo |
| 201 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(std::string())); | 113 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(std::string())); |
| 202 // Picasa default photo | 114 // Picasa default photo |
| 203 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL( | 115 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL( |
| 204 "https://example.com/-4/AAAAAAAAAAA/AAAAAAAAAAE/G/s64-c/photo.jpg")); | 116 "https://example.com/-4/AAAAAAAAAAA/AAAAAAAAAAE/G/s64-c/photo.jpg")); |
| 205 // Not default G+ photo | 117 // Not default G+ photo |
| 206 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( | 118 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( |
| 207 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg")); | 119 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg")); |
| 208 // Not default with 6 components | 120 // Not default with 6 components |
| 209 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( | 121 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( |
| 210 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg")); | 122 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg")); |
| 211 // Not default with 7 components | 123 // Not default with 7 components |
| 212 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( | 124 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( |
| 213 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg")); | 125 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg")); |
| 214 } | 126 } |
| OLD | NEW |