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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/strings/string_piece.h" | 8 #include "base/strings/string_piece.h" |
9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 std::wstring expected_null(L"a"); | 68 std::wstring expected_null(L"a"); |
69 expected_null.push_back(0); | 69 expected_null.push_back(0); |
70 expected_null.push_back('b'); | 70 expected_null.push_back('b'); |
71 | 71 |
72 EXPECT_EQ(expected_null, SysUTF8ToWide(utf8_null)); | 72 EXPECT_EQ(expected_null, SysUTF8ToWide(utf8_null)); |
73 } | 73 } |
74 | 74 |
75 #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. |
76 | 76 |
77 TEST(SysStrings, SysWideToNativeMB) { | 77 TEST(SysStrings, SysWideToNativeMB) { |
| 78 #if !defined(SYSTEM_NATIVE_UTF8) |
78 ScopedLocale locale("en_US.utf-8"); | 79 ScopedLocale locale("en_US.utf-8"); |
| 80 #endif |
79 EXPECT_EQ("Hello, world", SysWideToNativeMB(L"Hello, world")); | 81 EXPECT_EQ("Hello, world", SysWideToNativeMB(L"Hello, world")); |
80 EXPECT_EQ("\xe4\xbd\xa0\xe5\xa5\xbd", SysWideToNativeMB(L"\x4f60\x597d")); | 82 EXPECT_EQ("\xe4\xbd\xa0\xe5\xa5\xbd", SysWideToNativeMB(L"\x4f60\x597d")); |
81 | 83 |
82 // >16 bits | 84 // >16 bits |
83 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToNativeMB(kSysWideOldItalicLetterA)); | 85 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToNativeMB(kSysWideOldItalicLetterA)); |
84 | 86 |
85 // Error case. When Windows finds a UTF-16 character going off the end of | 87 // Error case. When Windows finds a UTF-16 character going off the end of |
86 // a string, it just converts that literal value to UTF-8, even though this | 88 // a string, it just converts that literal value to UTF-8, even though this |
87 // is invalid. | 89 // is invalid. |
88 // | 90 // |
89 // This is what XP does, but Vista has different behavior, so we don't bother | 91 // This is what XP does, but Vista has different behavior, so we don't bother |
90 // verifying it: | 92 // verifying it: |
91 // EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw", | 93 // EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw", |
92 // SysWideToNativeMB(L"\x4f60\xd800zyxw")); | 94 // SysWideToNativeMB(L"\x4f60\xd800zyxw")); |
93 | 95 |
94 // Test embedded NULLs. | 96 // Test embedded NULLs. |
95 std::wstring wide_null(L"a"); | 97 std::wstring wide_null(L"a"); |
96 wide_null.push_back(0); | 98 wide_null.push_back(0); |
97 wide_null.push_back('b'); | 99 wide_null.push_back('b'); |
98 | 100 |
99 std::string expected_null("a"); | 101 std::string expected_null("a"); |
100 expected_null.push_back(0); | 102 expected_null.push_back(0); |
101 expected_null.push_back('b'); | 103 expected_null.push_back('b'); |
102 | 104 |
103 EXPECT_EQ(expected_null, SysWideToNativeMB(wide_null)); | 105 EXPECT_EQ(expected_null, SysWideToNativeMB(wide_null)); |
104 } | 106 } |
105 | 107 |
106 // We assume the test is running in a UTF8 locale. | 108 // We assume the test is running in a UTF8 locale. |
107 TEST(SysStrings, SysNativeMBToWide) { | 109 TEST(SysStrings, SysNativeMBToWide) { |
| 110 #if !defined(SYSTEM_NATIVE_UTF8) |
108 ScopedLocale locale("en_US.utf-8"); | 111 ScopedLocale locale("en_US.utf-8"); |
| 112 #endif |
109 EXPECT_EQ(L"Hello, world", SysNativeMBToWide("Hello, world")); | 113 EXPECT_EQ(L"Hello, world", SysNativeMBToWide("Hello, world")); |
110 EXPECT_EQ(L"\x4f60\x597d", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); | 114 EXPECT_EQ(L"\x4f60\x597d", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); |
111 // >16 bits | 115 // >16 bits |
112 EXPECT_EQ(kSysWideOldItalicLetterA, SysNativeMBToWide("\xF0\x90\x8C\x80")); | 116 EXPECT_EQ(kSysWideOldItalicLetterA, SysNativeMBToWide("\xF0\x90\x8C\x80")); |
113 | 117 |
114 // Error case. When Windows finds an invalid UTF-8 character, it just skips | 118 // Error case. When Windows finds an invalid UTF-8 character, it just skips |
115 // it. This seems weird because it's inconsistent with the reverse conversion. | 119 // it. This seems weird because it's inconsistent with the reverse conversion. |
116 // | 120 // |
117 // This is what XP does, but Vista has different behavior, so we don't bother | 121 // This is what XP does, but Vista has different behavior, so we don't bother |
118 // verifying it: | 122 // verifying it: |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 L"\xd807\xdd40\xd807\xdd41\xd807\xdd42\xd807\xdd43\xd807\xdd44", | 156 L"\xd807\xdd40\xd807\xdd41\xd807\xdd42\xd807\xdd43\xd807\xdd44", |
153 #elif defined(WCHAR_T_IS_UTF32) | 157 #elif defined(WCHAR_T_IS_UTF32) |
154 L"\x10300", | 158 L"\x10300", |
155 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E) | 159 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E) |
156 L"\x11d40\x11d41\x11d42\x11d43\x11d44", | 160 L"\x11d40\x11d41\x11d42\x11d43\x11d44", |
157 #endif | 161 #endif |
158 }; | 162 }; |
159 | 163 |
160 | 164 |
161 TEST(SysStrings, SysNativeMBAndWide) { | 165 TEST(SysStrings, SysNativeMBAndWide) { |
| 166 #if !defined(SYSTEM_NATIVE_UTF8) |
162 ScopedLocale locale("en_US.utf-8"); | 167 ScopedLocale locale("en_US.utf-8"); |
| 168 #endif |
163 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { | 169 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { |
164 std::wstring wide = kConvertRoundtripCases[i]; | 170 std::wstring wide = kConvertRoundtripCases[i]; |
165 std::wstring trip = SysNativeMBToWide(SysWideToNativeMB(wide)); | 171 std::wstring trip = SysNativeMBToWide(SysWideToNativeMB(wide)); |
166 EXPECT_EQ(wide.size(), trip.size()); | 172 EXPECT_EQ(wide.size(), trip.size()); |
167 EXPECT_EQ(wide, trip); | 173 EXPECT_EQ(wide, trip); |
168 } | 174 } |
169 | 175 |
170 // We assume our test is running in UTF-8, so double check through ICU. | 176 // We assume our test is running in UTF-8, so double check through ICU. |
171 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { | 177 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { |
172 std::wstring wide = kConvertRoundtripCases[i]; | 178 std::wstring wide = kConvertRoundtripCases[i]; |
173 std::wstring trip = SysNativeMBToWide(WideToUTF8(wide)); | 179 std::wstring trip = SysNativeMBToWide(WideToUTF8(wide)); |
174 EXPECT_EQ(wide.size(), trip.size()); | 180 EXPECT_EQ(wide.size(), trip.size()); |
175 EXPECT_EQ(wide, trip); | 181 EXPECT_EQ(wide, trip); |
176 } | 182 } |
177 | 183 |
178 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { | 184 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { |
179 std::wstring wide = kConvertRoundtripCases[i]; | 185 std::wstring wide = kConvertRoundtripCases[i]; |
180 std::wstring trip = UTF8ToWide(SysWideToNativeMB(wide)); | 186 std::wstring trip = UTF8ToWide(SysWideToNativeMB(wide)); |
181 EXPECT_EQ(wide.size(), trip.size()); | 187 EXPECT_EQ(wide.size(), trip.size()); |
182 EXPECT_EQ(wide, trip); | 188 EXPECT_EQ(wide, trip); |
183 } | 189 } |
184 } | 190 } |
185 #endif // OS_LINUX | 191 #endif // OS_LINUX |
186 | 192 |
187 } // namespace base | 193 } // namespace base |
OLD | NEW |