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/strings/utf_string_conversions.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10namespace {
11
12std::string GetJSonData(const std::string& full_name,
13                        const std::string& given_name,
14                        const std::string& url,
15                        const std::string& locale) {
16  std::stringstream stream;
17  bool started = false;
18
19  stream << "{ ";
20  if (!full_name.empty()) {
21    stream << "\"name\": \"" << full_name << "\"";
22    started = true;
23  }
24  if (!given_name.empty()) {
25    stream << (started ? ", " : "") << "\"given_name\": \"" << given_name
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 << " }";
38  return stream.str();
39}
40
41} // namespace
42
43class ProfileDownloaderTest : public testing::Test {
44 protected:
45  ProfileDownloaderTest() {
46  }
47
48  virtual ~ProfileDownloaderTest() {
49  }
50
51  void VerifyWithAccountData(const std::string& full_name,
52                             const std::string& given_name,
53                             const std::string& url,
54                             const std::string& expected_url,
55                             const std::string& locale,
56                             bool is_valid) {
57    base::string16 parsed_full_name;
58    base::string16 parsed_given_name;
59    std::string parsed_url;
60    std::string parsed_locale;
61    bool result = ProfileDownloader::ParseProfileJSON(
62        GetJSonData(full_name, given_name, url, locale),
63        &parsed_full_name,
64        &parsed_given_name,
65        &parsed_url,
66        32,
67        &parsed_locale);
68    EXPECT_EQ(is_valid, result);
69    std::string parsed_full_name_utf8 = base::UTF16ToUTF8(parsed_full_name);
70    std::string parsed_given_name_utf8 = base::UTF16ToUTF8(parsed_given_name);
71
72    EXPECT_EQ(full_name, parsed_full_name_utf8);
73    EXPECT_EQ(given_name, parsed_given_name_utf8);
74    EXPECT_EQ(expected_url, parsed_url);
75    EXPECT_EQ(locale, parsed_locale);
76  }
77};
78
79TEST_F(ProfileDownloaderTest, ParseData) {
80  // URL without size specified.
81  VerifyWithAccountData(
82      "Pat Smith",
83      "Pat",
84      "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg",
85      "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg",
86      "en-US",
87      true);
88
89  // URL with size specified.
90  VerifyWithAccountData(
91      "Pat Smith",
92      "Pat",
93      "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",
95      "en-US",
96      true);
97
98  // URL with unknown format.
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(
108      "Pat Smith",
109      "Pat",
110      "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",
112      "jp",
113      true);
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
123  // Data with only name.
124  VerifyWithAccountData(
125      "Pat Smith", "Pat", std::string(), std::string(), std::string(), true);
126
127  // Data with only URL.
128  VerifyWithAccountData(
129      std::string(),
130      std::string(),
131      "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg",
132      "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg",
133      std::string(),
134      true);
135
136  // Data with only locale.
137  VerifyWithAccountData(
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);
147
148  // Data with an invalid URL.
149  VerifyWithAccountData(std::string(),
150                        std::string(),
151                        "invalid url",
152                        std::string(),
153                        std::string(),
154                        false);
155}
156
157TEST_F(ProfileDownloaderTest, DefaultURL) {
158  // Empty URL should be default photo
159  EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(std::string()));
160  // Picasa default photo
161  EXPECT_TRUE(ProfileDownloader::IsDefaultProfileImageURL(
162      "https://example.com/-4/AAAAAAAAAAA/AAAAAAAAAAE/G/s64-c/photo.jpg"));
163  // Not default G+ photo
164  EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
165      "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg"));
166  // Not default with 6 components
167  EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
168      "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg"));
169  // Not default with 7 components
170  EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL(
171      "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg"));
172}
173