Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / profile_downloader_unittest.cc
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/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"
13
14 namespace {
15
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);
23
24   if (!given_name.empty())
25     dict->SetString("name.givenName", given_name);
26
27   if (!url.empty())
28     dict->SetString("image.url", url);
29
30   if (!locale.empty())
31     dict->SetString("language", locale);
32 }
33
34 } // namespace
35
36 class ProfileDownloaderTest : public testing::Test {
37  protected:
38   ProfileDownloaderTest() {
39   }
40
41   virtual ~ProfileDownloaderTest() {
42   }
43
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,
49                              bool is_valid) {
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(
57         dict.get(),
58         &parsed_full_name,
59         &parsed_given_name,
60         &parsed_url,
61         32,
62         &parsed_locale);
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);
66
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);
71   }
72 };
73
74 TEST_F(ProfileDownloaderTest, ParseData) {
75   // URL without size specified.
76   VerifyWithAccountData(
77       "Pat Smith",
78       "Pat",
79       "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg",
80       "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg",
81       "en-US",
82       true);
83
84   // URL with size specified.
85   VerifyWithAccountData(
86       "Pat Smith",
87       "Pat",
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",
90       "en-US",
91       true);
92
93   // URL with unknown format.
94   VerifyWithAccountData("Pat Smith",
95                         "Pat",
96                         "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
97                         "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
98                         "en-US",
99                         true);
100
101   // Try different locales. URL with size specified.
102   VerifyWithAccountData(
103       "Pat Smith",
104       "Pat",
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",
107       "jp",
108       true);
109
110   // URL with unknown format.
111   VerifyWithAccountData("Pat Smith",
112                         "Pat",
113                         "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
114                         "http://lh0.ggpht.com/-abcd1aBCDEf/AAAA/AAA_A/",
115                         "fr",
116                         true);
117
118   // Data with only name.
119   VerifyWithAccountData(
120       "Pat Smith", "Pat", std::string(), std::string(), std::string(), true);
121
122   // Data with only URL.
123   VerifyWithAccountData(
124       std::string(),
125       std::string(),
126       "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg",
127       "https://example.com/--Abc/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg",
128       std::string(),
129       true);
130
131   // Data with only locale.
132   VerifyWithAccountData(
133       std::string(), std::string(), std::string(), std::string(), "fr", false);
134
135   // Data without name or URL or locale.
136   VerifyWithAccountData(std::string(),
137                         std::string(),
138                         std::string(),
139                         std::string(),
140                         std::string(),
141                         false);
142
143   // Data with an invalid URL.
144   VerifyWithAccountData(std::string(),
145                         std::string(),
146                         "invalid url",
147                         std::string(),
148                         std::string(),
149                         false);
150 }
151
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"));
167 }