| 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 | |
| 6 #include <string> | 5 #include <string> |
| 7 | 6 |
| 8 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 9 #include "base/string_piece.h" | 8 #include "base/string_piece.h" |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "base/test/scoped_locale.h" | 10 #include "base/test/scoped_locale.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/sys_string_conversions.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 13 |
| 15 #ifdef WCHAR_T_IS_UTF32 | 14 #ifdef WCHAR_T_IS_UTF32 |
| 16 static const std::wstring kSysWideOldItalicLetterA = L"\x10300"; | 15 static const std::wstring kSysWideOldItalicLetterA = L"\x10300"; |
| 17 #else | 16 #else |
| 18 static const std::wstring kSysWideOldItalicLetterA = L"\xd800\xdf00"; | 17 static const std::wstring kSysWideOldItalicLetterA = L"\xd800\xdf00"; |
| 19 #endif | 18 #endif |
| 20 | 19 |
| 20 namespace base { |
| 21 |
| 21 TEST(SysStrings, SysWideToUTF8) { | 22 TEST(SysStrings, SysWideToUTF8) { |
| 22 using base::SysWideToUTF8; | |
| 23 EXPECT_EQ("Hello, world", SysWideToUTF8(L"Hello, world")); | 23 EXPECT_EQ("Hello, world", SysWideToUTF8(L"Hello, world")); |
| 24 EXPECT_EQ("\xe4\xbd\xa0\xe5\xa5\xbd", SysWideToUTF8(L"\x4f60\x597d")); | 24 EXPECT_EQ("\xe4\xbd\xa0\xe5\xa5\xbd", SysWideToUTF8(L"\x4f60\x597d")); |
| 25 | 25 |
| 26 // >16 bits | 26 // >16 bits |
| 27 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToUTF8(kSysWideOldItalicLetterA)); | 27 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToUTF8(kSysWideOldItalicLetterA)); |
| 28 | 28 |
| 29 // Error case. When Windows finds a UTF-16 character going off the end of | 29 // Error case. When Windows finds a UTF-16 character going off the end of |
| 30 // a string, it just converts that literal value to UTF-8, even though this | 30 // a string, it just converts that literal value to UTF-8, even though this |
| 31 // is invalid. | 31 // is invalid. |
| 32 // | 32 // |
| 33 // This is what XP does, but Vista has different behavior, so we don't bother | 33 // This is what XP does, but Vista has different behavior, so we don't bother |
| 34 // verifying it: | 34 // verifying it: |
| 35 // EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw", | 35 // EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw", |
| 36 // SysWideToUTF8(L"\x4f60\xd800zyxw")); | 36 // SysWideToUTF8(L"\x4f60\xd800zyxw")); |
| 37 | 37 |
| 38 // Test embedded NULLs. | 38 // Test embedded NULLs. |
| 39 std::wstring wide_null(L"a"); | 39 std::wstring wide_null(L"a"); |
| 40 wide_null.push_back(0); | 40 wide_null.push_back(0); |
| 41 wide_null.push_back('b'); | 41 wide_null.push_back('b'); |
| 42 | 42 |
| 43 std::string expected_null("a"); | 43 std::string expected_null("a"); |
| 44 expected_null.push_back(0); | 44 expected_null.push_back(0); |
| 45 expected_null.push_back('b'); | 45 expected_null.push_back('b'); |
| 46 | 46 |
| 47 EXPECT_EQ(expected_null, SysWideToUTF8(wide_null)); | 47 EXPECT_EQ(expected_null, SysWideToUTF8(wide_null)); |
| 48 } | 48 } |
| 49 | 49 |
| 50 TEST(SysStrings, SysUTF8ToWide) { | 50 TEST(SysStrings, SysUTF8ToWide) { |
| 51 using base::SysUTF8ToWide; | |
| 52 EXPECT_EQ(L"Hello, world", SysUTF8ToWide("Hello, world")); | 51 EXPECT_EQ(L"Hello, world", SysUTF8ToWide("Hello, world")); |
| 53 EXPECT_EQ(L"\x4f60\x597d", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); | 52 EXPECT_EQ(L"\x4f60\x597d", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); |
| 54 // >16 bits | 53 // >16 bits |
| 55 EXPECT_EQ(kSysWideOldItalicLetterA, SysUTF8ToWide("\xF0\x90\x8C\x80")); | 54 EXPECT_EQ(kSysWideOldItalicLetterA, SysUTF8ToWide("\xF0\x90\x8C\x80")); |
| 56 | 55 |
| 57 // Error case. When Windows finds an invalid UTF-8 character, it just skips | 56 // Error case. When Windows finds an invalid UTF-8 character, it just skips |
| 58 // it. This seems weird because it's inconsistent with the reverse conversion. | 57 // it. This seems weird because it's inconsistent with the reverse conversion. |
| 59 // | 58 // |
| 60 // This is what XP does, but Vista has different behavior, so we don't bother | 59 // This is what XP does, but Vista has different behavior, so we don't bother |
| 61 // verifying it: | 60 // verifying it: |
| 62 // EXPECT_EQ(L"\x4f60zyxw", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5zyxw")); | 61 // EXPECT_EQ(L"\x4f60zyxw", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5zyxw")); |
| 63 | 62 |
| 64 // Test embedded NULLs. | 63 // Test embedded NULLs. |
| 65 std::string utf8_null("a"); | 64 std::string utf8_null("a"); |
| 66 utf8_null.push_back(0); | 65 utf8_null.push_back(0); |
| 67 utf8_null.push_back('b'); | 66 utf8_null.push_back('b'); |
| 68 | 67 |
| 69 std::wstring expected_null(L"a"); | 68 std::wstring expected_null(L"a"); |
| 70 expected_null.push_back(0); | 69 expected_null.push_back(0); |
| 71 expected_null.push_back('b'); | 70 expected_null.push_back('b'); |
| 72 | 71 |
| 73 EXPECT_EQ(expected_null, SysUTF8ToWide(utf8_null)); | 72 EXPECT_EQ(expected_null, SysUTF8ToWide(utf8_null)); |
| 74 } | 73 } |
| 75 | 74 |
| 76 #if defined(OS_LINUX) // Tests depend on setting a specific Linux locale. | 75 #if defined(OS_LINUX) // Tests depend on setting a specific Linux locale. |
| 77 | 76 |
| 78 TEST(SysStrings, SysWideToNativeMB) { | 77 TEST(SysStrings, SysWideToNativeMB) { |
| 79 using base::SysWideToNativeMB; | 78 ScopedLocale locale("en_US.utf-8"); |
| 80 base::ScopedLocale locale("en_US.utf-8"); | |
| 81 EXPECT_EQ("Hello, world", SysWideToNativeMB(L"Hello, world")); | 79 EXPECT_EQ("Hello, world", SysWideToNativeMB(L"Hello, world")); |
| 82 EXPECT_EQ("\xe4\xbd\xa0\xe5\xa5\xbd", SysWideToNativeMB(L"\x4f60\x597d")); | 80 EXPECT_EQ("\xe4\xbd\xa0\xe5\xa5\xbd", SysWideToNativeMB(L"\x4f60\x597d")); |
| 83 | 81 |
| 84 // >16 bits | 82 // >16 bits |
| 85 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToNativeMB(kSysWideOldItalicLetterA)); | 83 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToNativeMB(kSysWideOldItalicLetterA)); |
| 86 | 84 |
| 87 // Error case. When Windows finds a UTF-16 character going off the end of | 85 // Error case. When Windows finds a UTF-16 character going off the end of |
| 88 // a string, it just converts that literal value to UTF-8, even though this | 86 // a string, it just converts that literal value to UTF-8, even though this |
| 89 // is invalid. | 87 // is invalid. |
| 90 // | 88 // |
| 91 // This is what XP does, but Vista has different behavior, so we don't bother | 89 // This is what XP does, but Vista has different behavior, so we don't bother |
| 92 // verifying it: | 90 // verifying it: |
| 93 // EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw", | 91 // EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw", |
| 94 // SysWideToNativeMB(L"\x4f60\xd800zyxw")); | 92 // SysWideToNativeMB(L"\x4f60\xd800zyxw")); |
| 95 | 93 |
| 96 // Test embedded NULLs. | 94 // Test embedded NULLs. |
| 97 std::wstring wide_null(L"a"); | 95 std::wstring wide_null(L"a"); |
| 98 wide_null.push_back(0); | 96 wide_null.push_back(0); |
| 99 wide_null.push_back('b'); | 97 wide_null.push_back('b'); |
| 100 | 98 |
| 101 std::string expected_null("a"); | 99 std::string expected_null("a"); |
| 102 expected_null.push_back(0); | 100 expected_null.push_back(0); |
| 103 expected_null.push_back('b'); | 101 expected_null.push_back('b'); |
| 104 | 102 |
| 105 EXPECT_EQ(expected_null, SysWideToNativeMB(wide_null)); | 103 EXPECT_EQ(expected_null, SysWideToNativeMB(wide_null)); |
| 106 } | 104 } |
| 107 | 105 |
| 108 // We assume the test is running in a UTF8 locale. | 106 // We assume the test is running in a UTF8 locale. |
| 109 TEST(SysStrings, SysNativeMBToWide) { | 107 TEST(SysStrings, SysNativeMBToWide) { |
| 110 using base::SysNativeMBToWide; | 108 ScopedLocale locale("en_US.utf-8"); |
| 111 base::ScopedLocale locale("en_US.utf-8"); | |
| 112 EXPECT_EQ(L"Hello, world", SysNativeMBToWide("Hello, world")); | 109 EXPECT_EQ(L"Hello, world", SysNativeMBToWide("Hello, world")); |
| 113 EXPECT_EQ(L"\x4f60\x597d", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); | 110 EXPECT_EQ(L"\x4f60\x597d", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); |
| 114 // >16 bits | 111 // >16 bits |
| 115 EXPECT_EQ(kSysWideOldItalicLetterA, SysNativeMBToWide("\xF0\x90\x8C\x80")); | 112 EXPECT_EQ(kSysWideOldItalicLetterA, SysNativeMBToWide("\xF0\x90\x8C\x80")); |
| 116 | 113 |
| 117 // Error case. When Windows finds an invalid UTF-8 character, it just skips | 114 // Error case. When Windows finds an invalid UTF-8 character, it just skips |
| 118 // it. This seems weird because it's inconsistent with the reverse conversion. | 115 // it. This seems weird because it's inconsistent with the reverse conversion. |
| 119 // | 116 // |
| 120 // This is what XP does, but Vista has different behavior, so we don't bother | 117 // This is what XP does, but Vista has different behavior, so we don't bother |
| 121 // verifying it: | 118 // verifying it: |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 L"\xd807\xdd40\xd807\xdd41\xd807\xdd42\xd807\xdd43\xd807\xdd44", | 152 L"\xd807\xdd40\xd807\xdd41\xd807\xdd42\xd807\xdd43\xd807\xdd44", |
| 156 #elif defined(WCHAR_T_IS_UTF32) | 153 #elif defined(WCHAR_T_IS_UTF32) |
| 157 L"\x10300", | 154 L"\x10300", |
| 158 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E) | 155 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E) |
| 159 L"\x11d40\x11d41\x11d42\x11d43\x11d44", | 156 L"\x11d40\x11d41\x11d42\x11d43\x11d44", |
| 160 #endif | 157 #endif |
| 161 }; | 158 }; |
| 162 | 159 |
| 163 | 160 |
| 164 TEST(SysStrings, SysNativeMBAndWide) { | 161 TEST(SysStrings, SysNativeMBAndWide) { |
| 165 base::ScopedLocale locale("en_US.utf-8"); | 162 ScopedLocale locale("en_US.utf-8"); |
| 166 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { | 163 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { |
| 167 std::wstring wide = kConvertRoundtripCases[i]; | 164 std::wstring wide = kConvertRoundtripCases[i]; |
| 168 std::wstring trip = base::SysNativeMBToWide(base::SysWideToNativeMB(wide)); | 165 std::wstring trip = SysNativeMBToWide(SysWideToNativeMB(wide)); |
| 169 EXPECT_EQ(wide.size(), trip.size()); | 166 EXPECT_EQ(wide.size(), trip.size()); |
| 170 EXPECT_EQ(wide, trip); | 167 EXPECT_EQ(wide, trip); |
| 171 } | 168 } |
| 172 | 169 |
| 173 // We assume our test is running in UTF-8, so double check through ICU. | 170 // We assume our test is running in UTF-8, so double check through ICU. |
| 174 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { | 171 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { |
| 175 std::wstring wide = kConvertRoundtripCases[i]; | 172 std::wstring wide = kConvertRoundtripCases[i]; |
| 176 std::wstring trip = base::SysNativeMBToWide(WideToUTF8(wide)); | 173 std::wstring trip = SysNativeMBToWide(WideToUTF8(wide)); |
| 177 EXPECT_EQ(wide.size(), trip.size()); | 174 EXPECT_EQ(wide.size(), trip.size()); |
| 178 EXPECT_EQ(wide, trip); | 175 EXPECT_EQ(wide, trip); |
| 179 } | 176 } |
| 180 | 177 |
| 181 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { | 178 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { |
| 182 std::wstring wide = kConvertRoundtripCases[i]; | 179 std::wstring wide = kConvertRoundtripCases[i]; |
| 183 std::wstring trip = UTF8ToWide(base::SysWideToNativeMB(wide)); | 180 std::wstring trip = UTF8ToWide(SysWideToNativeMB(wide)); |
| 184 EXPECT_EQ(wide.size(), trip.size()); | 181 EXPECT_EQ(wide.size(), trip.size()); |
| 185 EXPECT_EQ(wide, trip); | 182 EXPECT_EQ(wide, trip); |
| 186 } | 183 } |
| 187 } | 184 } |
| 188 #endif // OS_LINUX | 185 #endif // OS_LINUX |
| 186 |
| 187 } // namespace base |
| OLD | NEW |