| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/profiles/profile_downloader.h" | 5 #include "chrome/browser/profiles/profile_downloader.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/strings/string_piece.h" |
| 7 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/values.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 13 |
| 10 namespace { | 14 namespace { |
| 11 | 15 |
| 12 std::string GetJSonData(const std::string& full_name, | 16 void GetJSonData(const std::string& full_name, |
| 13 const std::string& given_name, | 17 const std::string& given_name, |
| 14 const std::string& url, | 18 const std::string& url, |
| 15 const std::string& locale) { | 19 const std::string& locale, |
| 16 std::stringstream stream; | 20 base::DictionaryValue* dict) { |
| 17 bool started = false; | 21 if (!full_name.empty()) |
| 22 dict->SetString("displayName", full_name); |
| 18 | 23 |
| 19 stream << "{ "; | 24 if (!given_name.empty()) |
| 20 if (!full_name.empty()) { | 25 dict->SetString("name.givenName", given_name); |
| 21 stream << "\"name\": \"" << full_name << "\""; | 26 |
| 22 started = true; | 27 if (!url.empty()) |
| 23 } | 28 dict->SetString("image.url", url); |
| 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 | 29 |
| 34 if (!locale.empty()) | 30 if (!locale.empty()) |
| 35 stream << (started ? ", " : "") << "\"locale\": \"" << locale << "\""; | 31 dict->SetString("language", locale); |
| 36 | |
| 37 stream << " }"; | |
| 38 return stream.str(); | |
| 39 } | 32 } |
| 40 | 33 |
| 41 } // namespace | 34 } // namespace |
| 42 | 35 |
| 43 class ProfileDownloaderTest : public testing::Test { | 36 class ProfileDownloaderTest : public testing::Test { |
| 44 protected: | 37 protected: |
| 45 ProfileDownloaderTest() { | 38 ProfileDownloaderTest() { |
| 46 } | 39 } |
| 47 | 40 |
| 48 virtual ~ProfileDownloaderTest() { | 41 virtual ~ProfileDownloaderTest() { |
| 49 } | 42 } |
| 50 | 43 |
| 51 void VerifyWithAccountData(const std::string& full_name, | 44 void VerifyWithAccountData(const std::string& full_name, |
| 52 const std::string& given_name, | 45 const std::string& given_name, |
| 53 const std::string& url, | 46 const std::string& url, |
| 54 const std::string& expected_url, | 47 const std::string& expected_url, |
| 55 const std::string& locale, | 48 const std::string& locale, |
| 56 bool is_valid) { | 49 bool is_valid) { |
| 57 base::string16 parsed_full_name; | 50 base::string16 parsed_full_name; |
| 58 base::string16 parsed_given_name; | 51 base::string16 parsed_given_name; |
| 59 std::string parsed_url; | 52 std::string parsed_url; |
| 60 std::string parsed_locale; | 53 std::string parsed_locale; |
| 54 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
| 55 GetJSonData(full_name, given_name, url, locale, dict.get()); |
| 61 bool result = ProfileDownloader::ParseProfileJSON( | 56 bool result = ProfileDownloader::ParseProfileJSON( |
| 62 GetJSonData(full_name, given_name, url, locale), | 57 dict.get(), |
| 63 &parsed_full_name, | 58 &parsed_full_name, |
| 64 &parsed_given_name, | 59 &parsed_given_name, |
| 65 &parsed_url, | 60 &parsed_url, |
| 66 32, | 61 32, |
| 67 &parsed_locale); | 62 &parsed_locale); |
| 68 EXPECT_EQ(is_valid, result); | 63 EXPECT_EQ(is_valid, result); |
| 69 std::string parsed_full_name_utf8 = base::UTF16ToUTF8(parsed_full_name); | 64 std::string parsed_full_name_utf8 = base::UTF16ToUTF8(parsed_full_name); |
| 70 std::string parsed_given_name_utf8 = base::UTF16ToUTF8(parsed_given_name); | 65 std::string parsed_given_name_utf8 = base::UTF16ToUTF8(parsed_given_name); |
| 71 | 66 |
| 72 EXPECT_EQ(full_name, parsed_full_name_utf8); | 67 EXPECT_EQ(full_name, parsed_full_name_utf8); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 // Not default G+ photo | 158 // Not default G+ photo |
| 164 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( | 159 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( |
| 165 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg")); | 160 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAAAA/G/photo.jpg")); |
| 166 // Not default with 6 components | 161 // Not default with 6 components |
| 167 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( | 162 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( |
| 168 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg")); | 163 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/photo.jpg")); |
| 169 // Not default with 7 components | 164 // Not default with 7 components |
| 170 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( | 165 EXPECT_FALSE(ProfileDownloader::IsDefaultProfileImageURL( |
| 171 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg")); | 166 "https://example.com/-4/AAAAAAAAAAI/AAAAAAAAACQ/Efg/s32-c/photo.jpg")); |
| 172 } | 167 } |
| OLD | NEW |