Cleanup: new common GetProfileSwitcherTextForItem for use on both Win and Mac
[chromium-blink-merge.git] / chrome / browser / profiles / profile_downloader_unittest.cc
blob00e3e3b070123a91ded94bb21a08dea3739d9782
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/strings/string_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace {
13 void GetJSonData(const std::string& full_name,
14 const std::string& given_name,
15 const std::string& url,
16 const std::string& locale,
17 const std::string& hosted_domain,
18 bool include_empty_hosted_domain,
19 base::DictionaryValue* dict) {
20 if (!full_name.empty())
21 dict->SetString("name", full_name);
23 if (!given_name.empty())
24 dict->SetString("given_name", given_name);
26 if (!url.empty())
27 dict->SetString("picture", url);
29 if (!locale.empty())
30 dict->SetString("locale", locale);
32 if (!hosted_domain.empty() || include_empty_hosted_domain)
33 dict->SetString("hd", hosted_domain);
36 } // namespace
38 class ProfileDownloaderTest : public testing::Test {
39 protected:
40 ProfileDownloaderTest() {
43 ~ProfileDownloaderTest() override {}
45 void VerifyWithAccountData(const std::string& full_name,
46 const std::string& given_name,
47 const std::string& url,
48 const std::string& expected_url,
49 const std::string& locale,
50 const std::string& hosted_domain,
51 bool include_empty_hosted_domain,
52 bool is_valid) {
53 base::string16 parsed_full_name;
54 base::string16 parsed_given_name;
55 std::string parsed_url;
56 std::string parsed_locale;
57 base::string16 parsed_hosted_domain;
58 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
59 GetJSonData(full_name, given_name, url, locale, hosted_domain,
60 include_empty_hosted_domain, dict.get());
61 bool result = ProfileDownloader::ParseProfileJSON(
62 dict.get(),
63 &parsed_full_name,
64 &parsed_given_name,
65 &parsed_url,
66 32,
67 &parsed_locale,
68 &parsed_hosted_domain);
69 EXPECT_EQ(is_valid, result);
70 std::string parsed_full_name_utf8 = base::UTF16ToUTF8(parsed_full_name);
71 std::string parsed_given_name_utf8 = base::UTF16ToUTF8(parsed_given_name);
72 std::string parsed_hosted_domain_utf8 =
73 base::UTF16ToUTF8(parsed_hosted_domain);
75 EXPECT_EQ(full_name, parsed_full_name_utf8);
76 EXPECT_EQ(given_name, parsed_given_name_utf8);
77 EXPECT_EQ(expected_url, parsed_url);
78 EXPECT_EQ(locale, parsed_locale);
79 EXPECT_EQ(hosted_domain, parsed_hosted_domain_utf8);
83 TEST_F(ProfileDownloaderTest, ParseData) {
84 // URL without size specified.
85 VerifyWithAccountData(
86 "Pat Smith",
87 "Pat",
88 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg",
89 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg",
90 "en-US",
91 "google.com",
92 false,
93 true);
95 // URL with size specified.
96 VerifyWithAccountData(
97 "Pat Smith",
98 "Pat",
99 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s64-c/1234567890.jpg",
100 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s32-c/1234567890.jpg",
101 "en-US",
102 "google.com",
103 false,
104 true);
106 // URL with unknown format.
107 VerifyWithAccountData("Pat Smith",
108 "Pat",
109 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
110 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
111 "en-US",
112 "google.com",
113 false,
114 true);
116 // Try different locales. URL with size specified.
117 VerifyWithAccountData(
118 "Pat Smith",
119 "Pat",
120 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s64-c/1234567890.jpg",
121 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/abc12/s32-c/1234567890.jpg",
122 "jp",
123 "google.com",
124 false,
125 true);
127 // URL with unknown format.
128 VerifyWithAccountData("Pat Smith",
129 "Pat",
130 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
131 "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
132 "fr",
134 false,
135 true);
137 // Data with only name.
138 VerifyWithAccountData("Pat Smith",
139 "Pat",
140 std::string(),
141 std::string(),
142 std::string(),
143 std::string(),
144 false,
145 true);
147 // Data with only name and a blank but present hosted domain.
148 VerifyWithAccountData("Pat Smith",
149 "Pat",
150 std::string(),
151 std::string(),
152 std::string(),
153 std::string(),
154 true,
155 true);
157 // Data with only URL.
158 VerifyWithAccountData(
159 std::string(),
160 std::string(),
161 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg",
162 "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg",
163 std::string(),
164 std::string(),
165 false,
166 true);
168 // Data with only locale.
169 VerifyWithAccountData(std::string(),
170 std::string(),
171 std::string(),
172 std::string(),
173 "fr",
174 std::string(),
175 false,
176 false);
178 // Data without name or URL or locale.
179 VerifyWithAccountData(std::string(),
180 std::string(),
181 std::string(),
182 std::string(),
183 std::string(),
184 std::string(),
185 false,
186 false);
188 // Data with an invalid URL.
189 VerifyWithAccountData(std::string(),
190 std::string(),
191 "invalid url",
192 std::string(),
193 std::string(),
194 std::string(),
195 false,
196 false);
199 TEST_F(ProfileDownloaderTest, DefaultURL) {
200 // Empty URL should be default photo
201 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(std::string()));
202 // Picasa default photo
203 EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(
204 "https://example.com/-4/AAAAAAAAAAA/AAAAAAAAAAE/G/s64-c/photo.jpg"));
205 // Not default G+ photo
206 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
207 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg"));
208 // Not default with 6 components
209 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
210 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg"));
211 // Not default with 7 components
212 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
213 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg"));