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/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 namespace { | 11 namespace { |
11 | 12 |
12 std::string GetJSonData(const std::string& full_name, | 13 std::string GetJSonData(const std::string& full_name, |
13 const std::string& given_name, | 14 const std::string& given_name, |
14 const std::string& url, | 15 const std::string& url, |
15 const std::string& locale) { | 16 const std::string& locale, |
| 17 const std::string& hosted_domain, |
| 18 bool include_empty_hosted_domain) { |
16 std::stringstream stream; | 19 std::stringstream stream; |
17 bool started = false; | 20 bool started = false; |
18 | 21 |
19 stream << "{ "; | 22 stream << "{ "; |
20 if (!full_name.empty()) { | 23 if (!full_name.empty()) { |
21 stream << "\"name\": \"" << full_name << "\""; | 24 stream << "\"name\": \"" << full_name << "\""; |
22 started = true; | 25 started = true; |
23 } | 26 } |
24 if (!given_name.empty()) { | 27 if (!given_name.empty()) { |
25 stream << (started ? ", " : "") << "\"given_name\": \"" << given_name | 28 stream << (started ? ", " : "") << "\"given_name\": \"" << given_name |
26 << "\""; | 29 << "\""; |
27 started = true; | 30 started = true; |
28 } | 31 } |
29 if (!url.empty()) { | 32 if (!url.empty()) { |
30 stream << (started ? ", " : "") << "\"picture\": \"" << url << "\""; | 33 stream << (started ? ", " : "") << "\"picture\": \"" << url << "\""; |
31 started = true; | 34 started = true; |
32 } | 35 } |
33 | 36 |
34 if (!locale.empty()) | 37 if (!locale.empty()) |
35 stream << (started ? ", " : "") << "\"locale\": \"" << locale << "\""; | 38 stream << (started ? ", " : "") << "\"locale\": \"" << locale << "\""; |
36 | 39 |
| 40 if (!hosted_domain.empty() || include_empty_hosted_domain) |
| 41 stream << (started ? ", " : "") << "\"hd\": \"" << hosted_domain << "\""; |
| 42 |
37 stream << " }"; | 43 stream << " }"; |
38 return stream.str(); | 44 return stream.str(); |
39 } | 45 } |
40 | 46 |
41 } // namespace | 47 } // namespace |
42 | 48 |
43 class ProfileDownloaderTest : public testing::Test { | 49 class ProfileDownloaderTest : public testing::Test { |
44 protected: | 50 protected: |
45 ProfileDownloaderTest() { | 51 ProfileDownloaderTest() { |
46 } | 52 } |
47 | 53 |
48 virtual ~ProfileDownloaderTest() { | 54 virtual ~ProfileDownloaderTest() { |
49 } | 55 } |
50 | 56 |
51 void VerifyWithAccountData(const std::string& full_name, | 57 void VerifyWithAccountData(const std::string& full_name, |
52 const std::string& given_name, | 58 const std::string& given_name, |
53 const std::string& url, | 59 const std::string& url, |
54 const std::string& expected_url, | 60 const std::string& expected_url, |
55 const std::string& locale, | 61 const std::string& locale, |
| 62 const std::string& hosted_domain, |
| 63 bool include_empty_hosted_domain, |
56 bool is_valid) { | 64 bool is_valid) { |
57 base::string16 parsed_full_name; | 65 base::string16 parsed_full_name; |
58 base::string16 parsed_given_name; | 66 base::string16 parsed_given_name; |
59 std::string parsed_url; | 67 std::string parsed_url; |
60 std::string parsed_locale; | 68 std::string parsed_locale; |
| 69 base::string16 parsed_hosted_domain; |
61 bool result = ProfileDownloader::ParseProfileJSON( | 70 bool result = ProfileDownloader::ParseProfileJSON( |
62 GetJSonData(full_name, given_name, url, locale), | 71 GetJSonData(full_name, |
| 72 given_name, |
| 73 url, |
| 74 locale, |
| 75 hosted_domain, |
| 76 include_empty_hosted_domain), |
63 &parsed_full_name, | 77 &parsed_full_name, |
64 &parsed_given_name, | 78 &parsed_given_name, |
65 &parsed_url, | 79 &parsed_url, |
66 32, | 80 32, |
67 &parsed_locale); | 81 &parsed_locale, |
| 82 &parsed_hosted_domain); |
68 EXPECT_EQ(is_valid, result); | 83 EXPECT_EQ(is_valid, result); |
69 std::string parsed_full_name_utf8 = base::UTF16ToUTF8(parsed_full_name); | 84 std::string parsed_full_name_utf8 = base::UTF16ToUTF8(parsed_full_name); |
70 std::string parsed_given_name_utf8 = base::UTF16ToUTF8(parsed_given_name); | 85 std::string parsed_given_name_utf8 = base::UTF16ToUTF8(parsed_given_name); |
| 86 std::string parsed_hosted_domain_utf8 = |
| 87 base::UTF16ToUTF8(parsed_hosted_domain); |
71 | 88 |
72 EXPECT_EQ(full_name, parsed_full_name_utf8); | 89 EXPECT_EQ(full_name, parsed_full_name_utf8); |
73 EXPECT_EQ(given_name, parsed_given_name_utf8); | 90 EXPECT_EQ(given_name, parsed_given_name_utf8); |
74 EXPECT_EQ(expected_url, parsed_url); | 91 EXPECT_EQ(expected_url, parsed_url); |
75 EXPECT_EQ(locale, parsed_locale); | 92 EXPECT_EQ(locale, parsed_locale); |
| 93 EXPECT_EQ(hosted_domain, parsed_hosted_domain_utf8); |
76 } | 94 } |
77 }; | 95 }; |
78 | 96 |
79 TEST_F(ProfileDownloaderTest, ParseData) { | 97 TEST_F(ProfileDownloaderTest, ParseData) { |
80 // URL without size specified. | 98 // URL without size specified. |
81 VerifyWithAccountData( | 99 VerifyWithAccountData( |
82 "Pat Smith", | 100 "Pat Smith", |
83 "Pat", | 101 "Pat", |
84 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg", | 102 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg", |
85 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg", | 103 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg", |
86 "en-US", | 104 "en-US", |
| 105 "google.com", |
| 106 false, |
87 true); | 107 true); |
88 | 108 |
89 // URL with size specified. | 109 // URL with size specified. |
90 VerifyWithAccountData( | 110 VerifyWithAccountData( |
91 "Pat Smith", | 111 "Pat Smith", |
92 "Pat", | 112 "Pat", |
93 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s64-c/1234567890.jpg", | 113 "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", | 114 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s32-c/1234567890.jpg", |
95 "en-US", | 115 "en-US", |
| 116 "google.com", |
| 117 false, |
96 true); | 118 true); |
97 | 119 |
98 // URL with unknown format. | 120 // URL with unknown format. |
99 VerifyWithAccountData("Pat Smith", | 121 VerifyWithAccountData("Pat Smith", |
100 "Pat", | 122 "Pat", |
101 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/", | 123 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/", |
102 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/", | 124 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/", |
103 "en-US", | 125 "en-US", |
| 126 "google.com", |
| 127 false, |
104 true); | 128 true); |
105 | 129 |
106 // Try different locales. URL with size specified. | 130 // Try different locales. URL with size specified. |
107 VerifyWithAccountData( | 131 VerifyWithAccountData( |
108 "Pat Smith", | 132 "Pat Smith", |
109 "Pat", | 133 "Pat", |
110 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s64-c/1234567890.jpg", | 134 "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", | 135 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s32-c/1234567890.jpg", |
112 "jp", | 136 "jp", |
| 137 "google.com", |
| 138 false, |
113 true); | 139 true); |
114 | 140 |
115 // URL with unknown format. | 141 // URL with unknown format. |
116 VerifyWithAccountData("Pat Smith", | 142 VerifyWithAccountData("Pat Smith", |
117 "Pat", | 143 "Pat", |
118 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/", | 144 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/", |
119 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/", | 145 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/", |
120 "fr", | 146 "fr", |
| 147 "", |
| 148 false, |
121 true); | 149 true); |
122 | 150 |
123 // Data with only name. | 151 // Data with only name. |
124 VerifyWithAccountData( | 152 VerifyWithAccountData("Pat Smith", |
125 "Pat Smith", "Pat", std::string(), std::string(), std::string(), true); | 153 "Pat", |
| 154 std::string(), |
| 155 std::string(), |
| 156 std::string(), |
| 157 std::string(), |
| 158 false, |
| 159 true); |
| 160 |
| 161 // Data with only name and a blank but present hosted domain. |
| 162 VerifyWithAccountData("Pat Smith", |
| 163 "Pat", |
| 164 std::string(), |
| 165 std::string(), |
| 166 std::string(), |
| 167 std::string(), |
| 168 true, |
| 169 true); |
126 | 170 |
127 // Data with only URL. | 171 // Data with only URL. |
128 VerifyWithAccountData( | 172 VerifyWithAccountData( |
129 std::string(), | 173 std::string(), |
130 std::string(), | 174 std::string(), |
131 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg", | 175 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg", |
132 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg", | 176 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg", |
133 std::string(), | 177 std::string(), |
| 178 std::string(), |
| 179 false, |
134 true); | 180 true); |
135 | 181 |
136 // Data with only locale. | 182 // Data with only locale. |
137 VerifyWithAccountData( | 183 VerifyWithAccountData(std::string(), |
138 std::string(), std::string(), std::string(), std::string(), "fr", false); | 184 std::string(), |
| 185 std::string(), |
| 186 std::string(), |
| 187 "fr", |
| 188 std::string(), |
| 189 false, |
| 190 false); |
139 | 191 |
140 // Data without name or URL or locale. | 192 // Data without name or URL or locale. |
141 VerifyWithAccountData(std::string(), | 193 VerifyWithAccountData(std::string(), |
142 std::string(), | 194 std::string(), |
143 std::string(), | 195 std::string(), |
144 std::string(), | 196 std::string(), |
145 std::string(), | 197 std::string(), |
| 198 std::string(), |
| 199 false, |
146 false); | 200 false); |
147 | 201 |
148 // Data with an invalid URL. | 202 // Data with an invalid URL. |
149 VerifyWithAccountData(std::string(), | 203 VerifyWithAccountData(std::string(), |
150 std::string(), | 204 std::string(), |
151 "invalid url", | 205 "invalid url", |
152 std::string(), | 206 std::string(), |
153 std::string(), | 207 std::string(), |
| 208 std::string(), |
| 209 false, |
154 false); | 210 false); |
155 } | 211 } |
156 | 212 |
157 TEST_F(ProfileDownloaderTest, DefaultURL) { | 213 TEST_F(ProfileDownloaderTest, DefaultURL) { |
158 // Empty URL should be default photo | 214 // Empty URL should be default photo |
159 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(std::string())); | 215 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(std::string())); |
160 // Picasa default photo | 216 // Picasa default photo |
161 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL( | 217 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL( |
162 "https://example.com/-4/AAAAAAAAAAA/AAAAAAAAAAE/G/s64-c/photo.jpg")); | 218 "https://example.com/-4/AAAAAAAAAAA/AAAAAAAAAAE/G/s64-c/photo.jpg")); |
163 // Not default G+ photo | 219 // Not default G+ photo |
164 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( | 220 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( |
165 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg")); | 221 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg")); |
166 // Not default with 6 components | 222 // Not default with 6 components |
167 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( | 223 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( |
168 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg")); | 224 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg")); |
169 // Not default with 7 components | 225 // Not default with 7 components |
170 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( | 226 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( |
171 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg")); | 227 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg")); |
172 } | 228 } |
OLD | NEW |