OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <locale.h> | |
6 | 5 |
7 #include <string> | 6 #include <string> |
8 | 7 |
9 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
10 #include "base/string_piece.h" | 9 #include "base/string_piece.h" |
| 10 #include "base/test/test_util.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "base/sys_string_conversions.h" | 12 #include "base/sys_string_conversions.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 #ifdef WCHAR_T_IS_UTF32 | 15 #ifdef WCHAR_T_IS_UTF32 |
16 static const std::wstring kSysWideOldItalicLetterA = L"\x10300"; | 16 static const std::wstring kSysWideOldItalicLetterA = L"\x10300"; |
17 #else | 17 #else |
18 static const std::wstring kSysWideOldItalicLetterA = L"\xd800\xdf00"; | 18 static const std::wstring kSysWideOldItalicLetterA = L"\xd800\xdf00"; |
19 #endif | 19 #endif |
20 | 20 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 utf8_null.push_back('b'); | 67 utf8_null.push_back('b'); |
68 | 68 |
69 std::wstring expected_null(L"a"); | 69 std::wstring expected_null(L"a"); |
70 expected_null.push_back(0); | 70 expected_null.push_back(0); |
71 expected_null.push_back('b'); | 71 expected_null.push_back('b'); |
72 | 72 |
73 EXPECT_EQ(expected_null, SysUTF8ToWide(utf8_null)); | 73 EXPECT_EQ(expected_null, SysUTF8ToWide(utf8_null)); |
74 } | 74 } |
75 | 75 |
76 #if defined(OS_LINUX) // Tests depend on setting a specific Linux locale. | 76 #if defined(OS_LINUX) // Tests depend on setting a specific Linux locale. |
77 namespace { | |
78 | |
79 class ScopedSetLocale { | |
80 public: | |
81 explicit ScopedSetLocale(const char* locale) { | |
82 old_locale_ = setlocale(LC_ALL, NULL); | |
83 setlocale(LC_ALL, locale); | |
84 } | |
85 ~ScopedSetLocale() { | |
86 setlocale(LC_ALL, old_locale_.c_str()); | |
87 } | |
88 | |
89 private: | |
90 std::string old_locale_; | |
91 }; | |
92 | |
93 } // namespace | |
94 | 77 |
95 TEST(SysStrings, SysWideToNativeMB) { | 78 TEST(SysStrings, SysWideToNativeMB) { |
96 using base::SysWideToNativeMB; | 79 using base::SysWideToNativeMB; |
97 ScopedSetLocale locale("en_US.utf-8"); | 80 base::ScopedSetLocale locale("en_US.utf-8"); |
98 EXPECT_EQ("Hello, world", SysWideToNativeMB(L"Hello, world")); | 81 EXPECT_EQ("Hello, world", SysWideToNativeMB(L"Hello, world")); |
99 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")); |
100 | 83 |
101 // >16 bits | 84 // >16 bits |
102 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToNativeMB(kSysWideOldItalicLetterA)); | 85 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToNativeMB(kSysWideOldItalicLetterA)); |
103 | 86 |
104 // 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 |
105 // 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 |
106 // is invalid. | 89 // is invalid. |
107 // | 90 // |
(...skipping 10 matching lines...) Expand all Loading... |
118 std::string expected_null("a"); | 101 std::string expected_null("a"); |
119 expected_null.push_back(0); | 102 expected_null.push_back(0); |
120 expected_null.push_back('b'); | 103 expected_null.push_back('b'); |
121 | 104 |
122 EXPECT_EQ(expected_null, SysWideToNativeMB(wide_null)); | 105 EXPECT_EQ(expected_null, SysWideToNativeMB(wide_null)); |
123 } | 106 } |
124 | 107 |
125 // We assume the test is running in a UTF8 locale. | 108 // We assume the test is running in a UTF8 locale. |
126 TEST(SysStrings, SysNativeMBToWide) { | 109 TEST(SysStrings, SysNativeMBToWide) { |
127 using base::SysNativeMBToWide; | 110 using base::SysNativeMBToWide; |
128 ScopedSetLocale locale("en_US.utf-8"); | 111 base::ScopedSetLocale locale("en_US.utf-8"); |
129 EXPECT_EQ(L"Hello, world", SysNativeMBToWide("Hello, world")); | 112 EXPECT_EQ(L"Hello, world", SysNativeMBToWide("Hello, world")); |
130 EXPECT_EQ(L"\x4f60\x597d", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); | 113 EXPECT_EQ(L"\x4f60\x597d", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); |
131 // >16 bits | 114 // >16 bits |
132 EXPECT_EQ(kSysWideOldItalicLetterA, SysNativeMBToWide("\xF0\x90\x8C\x80")); | 115 EXPECT_EQ(kSysWideOldItalicLetterA, SysNativeMBToWide("\xF0\x90\x8C\x80")); |
133 | 116 |
134 // Error case. When Windows finds an invalid UTF-8 character, it just skips | 117 // Error case. When Windows finds an invalid UTF-8 character, it just skips |
135 // it. This seems weird because it's inconsistent with the reverse conversion. | 118 // it. This seems weird because it's inconsistent with the reverse conversion. |
136 // | 119 // |
137 // This is what XP does, but Vista has different behavior, so we don't bother | 120 // This is what XP does, but Vista has different behavior, so we don't bother |
138 // verifying it: | 121 // verifying it: |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 L"\xd807\xdd40\xd807\xdd41\xd807\xdd42\xd807\xdd43\xd807\xdd44", | 155 L"\xd807\xdd40\xd807\xdd41\xd807\xdd42\xd807\xdd43\xd807\xdd44", |
173 #elif defined(WCHAR_T_IS_UTF32) | 156 #elif defined(WCHAR_T_IS_UTF32) |
174 L"\x10300", | 157 L"\x10300", |
175 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E) | 158 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E) |
176 L"\x11d40\x11d41\x11d42\x11d43\x11d44", | 159 L"\x11d40\x11d41\x11d42\x11d43\x11d44", |
177 #endif | 160 #endif |
178 }; | 161 }; |
179 | 162 |
180 | 163 |
181 TEST(SysStrings, SysNativeMBAndWide) { | 164 TEST(SysStrings, SysNativeMBAndWide) { |
182 ScopedSetLocale locale("en_US.utf-8"); | 165 base::ScopedSetLocale locale("en_US.utf-8"); |
183 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { | 166 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { |
184 std::wstring wide = kConvertRoundtripCases[i]; | 167 std::wstring wide = kConvertRoundtripCases[i]; |
185 std::wstring trip = base::SysNativeMBToWide(base::SysWideToNativeMB(wide)); | 168 std::wstring trip = base::SysNativeMBToWide(base::SysWideToNativeMB(wide)); |
186 EXPECT_EQ(wide.size(), trip.size()); | 169 EXPECT_EQ(wide.size(), trip.size()); |
187 EXPECT_EQ(wide, trip); | 170 EXPECT_EQ(wide, trip); |
188 } | 171 } |
189 | 172 |
190 // We assume our test is running in UTF-8, so double check through ICU. | 173 // We assume our test is running in UTF-8, so double check through ICU. |
191 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { | 174 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { |
192 std::wstring wide = kConvertRoundtripCases[i]; | 175 std::wstring wide = kConvertRoundtripCases[i]; |
193 std::wstring trip = base::SysNativeMBToWide(WideToUTF8(wide)); | 176 std::wstring trip = base::SysNativeMBToWide(WideToUTF8(wide)); |
194 EXPECT_EQ(wide.size(), trip.size()); | 177 EXPECT_EQ(wide.size(), trip.size()); |
195 EXPECT_EQ(wide, trip); | 178 EXPECT_EQ(wide, trip); |
196 } | 179 } |
197 | 180 |
198 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { | 181 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { |
199 std::wstring wide = kConvertRoundtripCases[i]; | 182 std::wstring wide = kConvertRoundtripCases[i]; |
200 std::wstring trip = UTF8ToWide(base::SysWideToNativeMB(wide)); | 183 std::wstring trip = UTF8ToWide(base::SysWideToNativeMB(wide)); |
201 EXPECT_EQ(wide.size(), trip.size()); | 184 EXPECT_EQ(wide.size(), trip.size()); |
202 EXPECT_EQ(wide, trip); | 185 EXPECT_EQ(wide, trip); |
203 } | 186 } |
204 } | 187 } |
205 #endif // OS_LINUX | 188 #endif // OS_LINUX |
OLD | NEW |