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