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.
5 #include "chrome/browser/profiles/profile_downloader.h"
7 #include "base/json/json_reader.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/string_piece.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 void GetJSonData(const std::string& full_name,
17 const std::string& given_name,
18 const std::string& url,
19 const std::string& locale,
20 base::DictionaryValue* dict) {
21 if (!full_name.empty())
22 dict->SetString("displayName", full_name);
24 if (!given_name.empty())
25 dict->SetString("name.givenName", given_name);
28 dict->SetString("image.url", url);
31 dict->SetString("language", locale);
36 class ProfileDownloaderTest : public testing::Test {
38 ProfileDownloaderTest() {
41 virtual ~ProfileDownloaderTest() {
44 void VerifyWithAccountData(const std::string& full_name,
45 const std::string& given_name,
46 const std::string& url,
47 const std::string& expected_url,
48 const std::string& locale,
50 base::string16 parsed_full_name;
51 base::string16 parsed_given_name;
52 std::string parsed_url;
53 std::string parsed_locale;
54 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
55 GetJSonData(full_name, given_name, url, locale, dict.get());
56 bool result = ProfileDownloader::ParseProfileJSON(
63 EXPECT_EQ(is_valid, result);
64 std::string parsed_full_name_utf8 = base::UTF16ToUTF8(parsed_full_name);
65 std::string parsed_given_name_utf8 = base::UTF16ToUTF8(parsed_given_name);
67 EXPECT_EQ(full_name, parsed_full_name_utf8);
68 EXPECT_EQ(given_name, parsed_given_name_utf8);
69 EXPECT_EQ(expected_url, parsed_url);
70 EXPECT_EQ(locale, parsed_locale);
74 TEST_F(ProfileDownloaderTest, ParseData) {
75 // URL without size specified.
76 VerifyWithAccountData(
79 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg",
80 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg",
84 // URL with size specified.
85 VerifyWithAccountData(
88 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s64-c/1234567890.jpg",
89 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s32-c/1234567890.jpg",
93 // URL with unknown format.
94 VerifyWithAccountData("Pat Smith",
96 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
97 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
101 // Try different locales. URL with size specified.
102 VerifyWithAccountData(
105 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s64-c/1234567890.jpg",
106 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s32-c/1234567890.jpg",
110 // URL with unknown format.
111 VerifyWithAccountData("Pat Smith",
113 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
114 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
118 // Data with only name.
119 VerifyWithAccountData(
120 "Pat Smith", "Pat", std::string(), std::string(), std::string(), true);
122 // Data with only URL.
123 VerifyWithAccountData(
126 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg",
127 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg",
131 // Data with only locale.
132 VerifyWithAccountData(
133 std::string(), std::string(), std::string(), std::string(), "fr", false);
135 // Data without name or URL or locale.
136 VerifyWithAccountData(std::string(),
143 // Data with an invalid URL.
144 VerifyWithAccountData(std::string(),
152 TEST_F(ProfileDownloaderTest, DefaultURL) {
153 // Empty URL should be default photo
154 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(std::string()));
155 // Picasa default photo
156 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(
157 "https://example.com/-4/AAAAAAAAAAA/AAAAAAAAAAE/G/s64-c/photo.jpg"));
158 // Not default G+ photo
159 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
160 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg"));
161 // Not default with 6 components
162 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
163 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg"));
164 // Not default with 7 components
165 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
166 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg"));