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