OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/profiles/profile_downloader.h" |
| 6 |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace { |
| 11 |
| 12 std::string GetJSonData(const std::string& name, const std::string& url) { |
| 13 std::stringstream stream; |
| 14 stream << "{ "; |
| 15 if (!name.empty()) |
| 16 stream << "\"name\": \"" << name << "\", "; |
| 17 if (!url.empty()) |
| 18 stream << "\"picture\": \"" << url << "\", "; |
| 19 stream << "\"locale\": \"en-US\" }"; |
| 20 return stream.str(); |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 class ProfileDownloaderTest : public testing::Test { |
| 26 protected: |
| 27 ProfileDownloaderTest() { |
| 28 } |
| 29 |
| 30 virtual ~ProfileDownloaderTest() { |
| 31 } |
| 32 |
| 33 void VerifyWithNameAndURL(const std::string& name, |
| 34 const std::string& url, |
| 35 const std::string& expected_url, |
| 36 bool is_valid) { |
| 37 string16 parsed_name; |
| 38 std::string parsed_url; |
| 39 bool result = ProfileDownloader::GetProfileNameAndImageURL( |
| 40 GetJSonData(name, url), &parsed_name, &parsed_url, 32); |
| 41 EXPECT_EQ(is_valid, result); |
| 42 std::string parsed_name_utf8 = UTF16ToUTF8(parsed_name); |
| 43 EXPECT_EQ(name, parsed_name_utf8); |
| 44 EXPECT_EQ(expected_url, parsed_url); |
| 45 } |
| 46 }; |
| 47 |
| 48 TEST_F(ProfileDownloaderTest, ParseData) { |
| 49 // URL without size specified. |
| 50 VerifyWithNameAndURL( |
| 51 "Pat Smith", |
| 52 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg", |
| 53 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg", |
| 54 true); |
| 55 |
| 56 // URL with size specified. |
| 57 VerifyWithNameAndURL( |
| 58 "Pat Smith", |
| 59 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s64-c/1234567890.jpg", |
| 60 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s32-c/1234567890.jpg", |
| 61 true); |
| 62 |
| 63 // URL with unknown format. |
| 64 VerifyWithNameAndURL( |
| 65 "Pat Smith", |
| 66 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/", |
| 67 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/", |
| 68 true); |
| 69 |
| 70 // Data with only name. |
| 71 VerifyWithNameAndURL("Pat Smith", "", "", true); |
| 72 |
| 73 // Data with only URL. |
| 74 VerifyWithNameAndURL( |
| 75 "", |
| 76 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg", |
| 77 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg", |
| 78 true); |
| 79 |
| 80 // Data without name or URL. |
| 81 VerifyWithNameAndURL("", "", "", false); |
| 82 |
| 83 // Data with an invalid URL. |
| 84 VerifyWithNameAndURL( "", "invalid url", "", false); |
| 85 } |
| 86 |
| 87 TEST_F(ProfileDownloaderTest, DefaultURL) { |
| 88 // Empty URL should be default photo |
| 89 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL("")); |
| 90 // Picasa default photo |
| 91 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL( |
| 92 "https://example.com/-4/AAAAAAAAAAA/AAAAAAAAAAE/G/s64-c/photo.jpg")); |
| 93 // G+ default photo |
| 94 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL( |
| 95 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg")); |
| 96 // Not default with 6 components |
| 97 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( |
| 98 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg")); |
| 99 // Not default with 7 components |
| 100 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( |
| 101 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg")); |
| 102 // Not default with invalid URL |
| 103 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL("invalid url")); |
| 104 } |
OLD | NEW |