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