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 "testing/gtest/include/gtest/gtest.h"
13 void GetJSonData(const std::string& full_name,
14 const std::string& given_name,
15 const std::string& url,
16 const std::string& locale,
17 const std::string& hosted_domain,
18 bool include_empty_hosted_domain,
19 base::DictionaryValue* dict) {
20 if (!full_name.empty())
21 dict->SetString("name", full_name);
23 if (!given_name.empty())
24 dict->SetString("given_name", given_name);
27 dict->SetString("picture", url);
30 dict->SetString("locale", locale);
32 if (!hosted_domain.empty() || include_empty_hosted_domain)
33 dict->SetString("hd", hosted_domain);
38 class ProfileDownloaderTest : public testing::Test {
40 ProfileDownloaderTest() {
43 ~ProfileDownloaderTest() override {}
45 void VerifyWithAccountData(const std::string& full_name,
46 const std::string& given_name,
47 const std::string& url,
48 const std::string& expected_url,
49 const std::string& locale,
50 const std::string& hosted_domain,
51 bool include_empty_hosted_domain,
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(
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);
75 EXPECT_EQ(full_name, parsed_full_name_utf8);
76 EXPECT_EQ(given_name, parsed_given_name_utf8);
77 EXPECT_EQ(expected_url, parsed_url);
78 EXPECT_EQ(locale, parsed_locale);
79 EXPECT_EQ(hosted_domain, parsed_hosted_domain_utf8);
83 TEST_F(ProfileDownloaderTest, ParseData) {
84 // URL without size specified.
85 VerifyWithAccountData(
88 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg",
89 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg",
95 // URL with size specified.
96 VerifyWithAccountData(
99 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s64-c/1234567890.jpg",
100 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s32-c/1234567890.jpg",
106 // URL with unknown format.
107 VerifyWithAccountData("Pat Smith",
109 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
110 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
116 // Try different locales. URL with size specified.
117 VerifyWithAccountData(
120 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s64-c/1234567890.jpg",
121 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s32-c/1234567890.jpg",
127 // URL with unknown format.
128 VerifyWithAccountData("Pat Smith",
130 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
131 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
137 // Data with only name.
138 VerifyWithAccountData("Pat Smith",
147 // Data with only name and a blank but present hosted domain.
148 VerifyWithAccountData("Pat Smith",
157 // Data with only URL.
158 VerifyWithAccountData(
161 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg",
162 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg",
168 // Data with only locale.
169 VerifyWithAccountData(std::string(),
178 // Data without name or URL or locale.
179 VerifyWithAccountData(std::string(),
188 // Data with an invalid URL.
189 VerifyWithAccountData(std::string(),
199 TEST_F(ProfileDownloaderTest, DefaultURL) {
200 // Empty URL should be default photo
201 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(std::string()));
202 // Picasa default photo
203 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(
204 "https://example.com/-4/AAAAAAAAAAA/AAAAAAAAAAE/G/s64-c/photo.jpg"));
205 // Not default G+ photo
206 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
207 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg"));
208 // Not default with 6 components
209 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
210 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg"));
211 // Not default with 7 components
212 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
213 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg"));