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