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/utf_string_conversions.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 std::string
GetJSonData(const std::string
& full_name
,
13 const std::string
& given_name
,
14 const std::string
& url
,
15 const std::string
& locale
) {
16 std::stringstream stream
;
20 if (!full_name
.empty()) {
21 stream
<< "\"name\": \"" << full_name
<< "\"";
24 if (!given_name
.empty()) {
25 stream
<< (started
? ", " : "") << "\"given_name\": \"" << given_name
30 stream
<< (started
? ", " : "") << "\"picture\": \"" << url
<< "\"";
35 stream
<< (started
? ", " : "") << "\"locale\": \"" << locale
<< "\"";
43 class ProfileDownloaderTest
: public testing::Test
{
45 ProfileDownloaderTest() {
48 virtual ~ProfileDownloaderTest() {
51 void VerifyWithAccountData(const std::string
& full_name
,
52 const std::string
& given_name
,
53 const std::string
& url
,
54 const std::string
& expected_url
,
55 const std::string
& locale
,
57 base::string16 parsed_full_name
;
58 base::string16 parsed_given_name
;
59 std::string parsed_url
;
60 std::string parsed_locale
;
61 bool result
= ProfileDownloader::ParseProfileJSON(
62 GetJSonData(full_name
, given_name
, url
, locale
),
68 EXPECT_EQ(is_valid
, result
);
69 std::string parsed_full_name_utf8
= base::UTF16ToUTF8(parsed_full_name
);
70 std::string parsed_given_name_utf8
= base::UTF16ToUTF8(parsed_given_name
);
72 EXPECT_EQ(full_name
, parsed_full_name_utf8
);
73 EXPECT_EQ(given_name
, parsed_given_name_utf8
);
74 EXPECT_EQ(expected_url
, parsed_url
);
75 EXPECT_EQ(locale
, parsed_locale
);
79 TEST_F(ProfileDownloaderTest
, ParseData
) {
80 // URL without size specified.
81 VerifyWithAccountData(
84 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg",
85 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg",
89 // URL with size specified.
90 VerifyWithAccountData(
93 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s64-c/1234567890.jpg",
94 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s32-c/1234567890.jpg",
98 // URL with unknown format.
99 VerifyWithAccountData("Pat Smith",
101 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
102 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
106 // Try different locales. URL with size specified.
107 VerifyWithAccountData(
110 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s64-c/1234567890.jpg",
111 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s32-c/1234567890.jpg",
115 // URL with unknown format.
116 VerifyWithAccountData("Pat Smith",
118 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
119 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
123 // Data with only name.
124 VerifyWithAccountData(
125 "Pat Smith", "Pat", std::string(), std::string(), std::string(), true);
127 // Data with only URL.
128 VerifyWithAccountData(
131 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg",
132 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg",
136 // Data with only locale.
137 VerifyWithAccountData(
138 std::string(), std::string(), std::string(), std::string(), "fr", false);
140 // Data without name or URL or locale.
141 VerifyWithAccountData(std::string(),
148 // Data with an invalid URL.
149 VerifyWithAccountData(std::string(),
157 TEST_F(ProfileDownloaderTest
, DefaultURL
) {
158 // Empty URL should be default photo
159 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(std::string()));
160 // Picasa default photo
161 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(
162 "https://example.com/-4/AAAAAAAAAAA/AAAAAAAAAAE/G/s64-c/photo.jpg"));
163 // Not default G+ photo
164 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
165 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg"));
166 // Not default with 6 components
167 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
168 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg"));
169 // Not default with 7 components
170 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
171 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg"));