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