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

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

Issue 1091363002: Change ProfileDownloader to use AccountTrackerService. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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) 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 "net/url_request/test_url_fetcher_factory.h"
9 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
10 19
11 namespace { 20 namespace {
12 21
13 void GetJSonData(const std::string& full_name, 22 const std::string kTestEmail = "test@example.com";
14 const std::string& given_name, 23 const std::string kTestGaia = "gaia";
15 const std::string& url, 24 const std::string kTestHostedDomain = "google.com";
16 const std::string& locale, 25 const std::string kTestFullName = "full_name";
17 const std::string& hosted_domain, 26 const std::string kTestGivenName = "given_name";
18 bool include_empty_hosted_domain, 27 const std::string kTestLocale = "locale";
19 base::DictionaryValue* dict) { 28 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 29
36 } // namespace 30 } // namespace
37 31
38 class ProfileDownloaderTest : public testing::Test { 32 class ProfileDownloaderTest : public testing::Test,
33 public ProfileDownloaderDelegate {
39 protected: 34 protected:
40 ProfileDownloaderTest() { 35 ProfileDownloaderTest() {}
36 ~ProfileDownloaderTest() override {}
37
38 void SetUp() override {
39 TestingProfile::Builder builder;
40 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
41 &BuildAutoIssuingFakeProfileOAuth2TokenService);
42 builder.AddTestingFactory(AccountTrackerServiceFactory::GetInstance(),
43 FakeAccountTrackerService::Build);
44 builder.AddTestingFactory(ChromeSigninClientFactory::GetInstance(),
45 signin::BuildTestSigninClient);
46
47 profile_ = builder.Build();
48 account_tracker_service_ = static_cast<FakeAccountTrackerService*>(
49 AccountTrackerServiceFactory::GetForProfile(profile_.get()));
50 profile_downloader_.reset(new ProfileDownloader(this));
41 } 51 }
42 52
43 ~ProfileDownloaderTest() override {} 53 bool NeedsProfilePicture() const override { return true; };
54 int GetDesiredImageSideLength() const override { return 128; };
55 std::string GetCachedPictureURL() const override { return ""; };
56 Profile* GetBrowserProfile() override { return profile_.get(); };
57 void OnProfileDownloadSuccess(ProfileDownloader* downloader) override {
44 58
45 void VerifyWithAccountData(const std::string& full_name, 59 }
46 const std::string& given_name, 60 void OnProfileDownloadFailure(
47 const std::string& url, 61 ProfileDownloader* downloader,
48 const std::string& expected_url, 62 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 63
75 EXPECT_EQ(full_name, parsed_full_name_utf8); 64 void SimulateUserInfoSuccess() {
76 EXPECT_EQ(given_name, parsed_given_name_utf8); 65 account_tracker_service_->FakeUserInfoFetchSuccess(
77 EXPECT_EQ(expected_url, parsed_url); 66 kTestEmail,
78 EXPECT_EQ(locale, parsed_locale); 67 kTestGaia,
79 EXPECT_EQ(hosted_domain, parsed_hosted_domain_utf8); 68 kTestHostedDomain,
69 kTestFullName,
70 kTestGivenName,
71 kTestLocale,
72 kTestPictureURL);
80 } 73 }
74
75 FakeAccountTrackerService* account_tracker_service_;
76 scoped_ptr<Profile> profile_;
77 scoped_ptr<ProfileDownloader> profile_downloader_;
81 }; 78 };
82 79
83 TEST_F(ProfileDownloaderTest, ParseData) { 80 TEST_F(ProfileDownloaderTest, AccountInfoReady) {
84 // URL without size specified. 81 account_tracker_service_->SeedAccountInfo(kTestGaia, kTestEmail);
85 VerifyWithAccountData( 82 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 83
95 // URL with size specified. 84 ASSERT_EQ(ProfileDownloader::PICTURE_FAILED,
96 VerifyWithAccountData( 85 profile_downloader_->GetProfilePictureStatus());
97 "Pat Smith", 86 profile_downloader_->StartForAccount(kTestEmail, account_tracker_service_);
98 "Pat", 87 profile_downloader_->StartFetchingImageForTesting(
99 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s64-c/1234567890.jpg", 88 new net::TestURLFetcher(0, GURL(kTestPictureURL), nullptr));
100 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s32-c/1234567890.jpg", 89 ASSERT_EQ(kTestPictureURL, profile_downloader_->GetProfilePictureURL());
101 "en-US", 90 }
102 "google.com",
103 false,
104 true);
105 91
106 // URL with unknown format. 92 TEST_F(ProfileDownloaderTest, AccountInfoNotReady) {
107 VerifyWithAccountData("Pat Smith", 93 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 94
116 // Try different locales. URL with size specified. 95 ASSERT_EQ(ProfileDownloader::PICTURE_FAILED,
117 VerifyWithAccountData( 96 profile_downloader_->GetProfilePictureStatus());
118 "Pat Smith", 97 profile_downloader_->StartForAccount(kTestEmail, account_tracker_service_);
119 "Pat", 98 profile_downloader_->StartFetchingImageForTesting(
120 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s64-c/1234567890.jpg", 99 new net::TestURLFetcher(0, GURL(kTestPictureURL), nullptr));
121 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s32-c/1234567890.jpg", 100 SimulateUserInfoSuccess();
122 "jp", 101 ASSERT_EQ(kTestPictureURL, profile_downloader_->GetProfilePictureURL());
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 } 102 }
198 103
199 TEST_F(ProfileDownloaderTest, DefaultURL) { 104 TEST_F(ProfileDownloaderTest, DefaultURL) {
200 // Empty URL should be default photo 105 // Empty URL should be default photo
201 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(std::string())); 106 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(std::string()));
202 // Picasa default photo 107 // Picasa default photo
203 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL( 108 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(
204 "https://example.com/-4/AAAAAAAAAAA/AAAAAAAAAAE/G/s64-c/photo.jpg")); 109 "https://example.com/-4/AAAAAAAAAAA/AAAAAAAAAAE/G/s64-c/photo.jpg"));
205 // Not default G+ photo 110 // Not default G+ photo
206 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( 111 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
207 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg")); 112 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg"));
208 // Not default with 6 components 113 // Not default with 6 components
209 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( 114 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
210 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg")); 115 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg"));
211 // Not default with 7 components 116 // Not default with 7 components
212 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( 117 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
213 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg")); 118 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg"));
214 } 119 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698