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 "base/string_piece.h" | 5 #include "base/string_piece.h" |
| 6 #include "base/string_util.h" |
6 #include "base/sys_string_conversions.h" | 7 #include "base/sys_string_conversions.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
8 | 9 |
9 #ifdef WCHAR_T_IS_UTF32 | 10 #ifdef WCHAR_T_IS_UTF32 |
10 static const std::wstring kSysWideOldItalicLetterA = L"\x10300"; | 11 static const std::wstring kSysWideOldItalicLetterA = L"\x10300"; |
11 #else | 12 #else |
12 static const std::wstring kSysWideOldItalicLetterA = L"\xd800\xdf00"; | 13 static const std::wstring kSysWideOldItalicLetterA = L"\xd800\xdf00"; |
13 #endif | 14 #endif |
14 | 15 |
15 TEST(SysStrings, SysWideToUTF8) { | 16 TEST(SysStrings, SysWideToUTF8) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 std::string utf8_null("a"); | 60 std::string utf8_null("a"); |
60 utf8_null.push_back(0); | 61 utf8_null.push_back(0); |
61 utf8_null.push_back('b'); | 62 utf8_null.push_back('b'); |
62 | 63 |
63 std::wstring expected_null(L"a"); | 64 std::wstring expected_null(L"a"); |
64 expected_null.push_back(0); | 65 expected_null.push_back(0); |
65 expected_null.push_back('b'); | 66 expected_null.push_back('b'); |
66 | 67 |
67 EXPECT_EQ(expected_null, SysUTF8ToWide(utf8_null)); | 68 EXPECT_EQ(expected_null, SysUTF8ToWide(utf8_null)); |
68 } | 69 } |
| 70 |
| 71 // We assume the test is running in a UTF8 locale. |
| 72 TEST(SysStrings, SysWideToNativeMB) { |
| 73 using base::SysWideToNativeMB; |
| 74 EXPECT_EQ("Hello, world", SysWideToNativeMB(L"Hello, world")); |
| 75 EXPECT_EQ("\xe4\xbd\xa0\xe5\xa5\xbd", SysWideToNativeMB(L"\x4f60\x597d")); |
| 76 |
| 77 // >16 bits |
| 78 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToNativeMB(kSysWideOldItalicLetterA)); |
| 79 |
| 80 // Error case. When Windows finds a UTF-16 character going off the end of |
| 81 // a string, it just converts that literal value to UTF-8, even though this |
| 82 // is invalid. |
| 83 // |
| 84 // This is what XP does, but Vista has different behavior, so we don't bother |
| 85 // verifying it: |
| 86 //EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw", |
| 87 // SysWideToNativeMB(L"\x4f60\xd800zyxw")); |
| 88 |
| 89 // Test embedded NULLs. |
| 90 std::wstring wide_null(L"a"); |
| 91 wide_null.push_back(0); |
| 92 wide_null.push_back('b'); |
| 93 |
| 94 std::string expected_null("a"); |
| 95 expected_null.push_back(0); |
| 96 expected_null.push_back('b'); |
| 97 |
| 98 EXPECT_EQ(expected_null, SysWideToNativeMB(wide_null)); |
| 99 } |
| 100 |
| 101 // We assume the test is running in a UTF8 locale. |
| 102 TEST(SysStrings, SysNativeMBToWide) { |
| 103 using base::SysNativeMBToWide; |
| 104 EXPECT_EQ(L"Hello, world", SysNativeMBToWide("Hello, world")); |
| 105 EXPECT_EQ(L"\x4f60\x597d", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); |
| 106 // >16 bits |
| 107 EXPECT_EQ(kSysWideOldItalicLetterA, SysNativeMBToWide("\xF0\x90\x8C\x80")); |
| 108 |
| 109 // Error case. When Windows finds an invalid UTF-8 character, it just skips |
| 110 // it. This seems weird because it's inconsistent with the reverse conversion. |
| 111 // |
| 112 // This is what XP does, but Vista has different behavior, so we don't bother |
| 113 // verifying it: |
| 114 //EXPECT_EQ(L"\x4f60zyxw", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5zyxw")); |
| 115 |
| 116 // Test embedded NULLs. |
| 117 std::string utf8_null("a"); |
| 118 utf8_null.push_back(0); |
| 119 utf8_null.push_back('b'); |
| 120 |
| 121 std::wstring expected_null(L"a"); |
| 122 expected_null.push_back(0); |
| 123 expected_null.push_back('b'); |
| 124 |
| 125 EXPECT_EQ(expected_null, SysNativeMBToWide(utf8_null)); |
| 126 } |
| 127 |
| 128 static const wchar_t* const kConvertRoundtripCases[] = { |
| 129 L"Google Video", |
| 130 // "网页 图片 资讯更多 »" |
| 131 L"\x7f51\x9875\x0020\x56fe\x7247\x0020\x8d44\x8baf\x66f4\x591a\x0020\x00bb", |
| 132 // "Παγκόσμιος Ιστός" |
| 133 L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9" |
| 134 L"\x03bf\x03c2\x0020\x0399\x03c3\x03c4\x03cc\x03c2", |
| 135 // "Поиск страниц на русском" |
| 136 L"\x041f\x043e\x0438\x0441\x043a\x0020\x0441\x0442" |
| 137 L"\x0440\x0430\x043d\x0438\x0446\x0020\x043d\x0430" |
| 138 L"\x0020\x0440\x0443\x0441\x0441\x043a\x043e\x043c", |
| 139 // "전체서비스" |
| 140 L"\xc804\xccb4\xc11c\xbe44\xc2a4", |
| 141 |
| 142 // Test characters that take more than 16 bits. This will depend on whether |
| 143 // wchar_t is 16 or 32 bits. |
| 144 #if defined(WCHAR_T_IS_UTF16) |
| 145 L"\xd800\xdf00", |
| 146 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E) |
| 147 L"\xd807\xdd40\xd807\xdd41\xd807\xdd42\xd807\xdd43\xd807\xdd44", |
| 148 #elif defined(WCHAR_T_IS_UTF32) |
| 149 L"\x10300", |
| 150 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E) |
| 151 L"\x11d40\x11d41\x11d42\x11d43\x11d44", |
| 152 #endif |
| 153 }; |
| 154 |
| 155 |
| 156 TEST(SysStrings, SysNativeMBAndWide) { |
| 157 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { |
| 158 std::wstring wide = kConvertRoundtripCases[i]; |
| 159 std::wstring trip = base::SysNativeMBToWide(base::SysWideToNativeMB(wide)); |
| 160 EXPECT_EQ(wide.size(), trip.size()); |
| 161 EXPECT_EQ(wide, trip); |
| 162 } |
| 163 |
| 164 // We assume our test is running in UTF-8, so double check through ICU. |
| 165 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { |
| 166 std::wstring wide = kConvertRoundtripCases[i]; |
| 167 std::wstring trip = base::SysNativeMBToWide(WideToUTF8(wide)); |
| 168 EXPECT_EQ(wide.size(), trip.size()); |
| 169 EXPECT_EQ(wide, trip); |
| 170 } |
| 171 |
| 172 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { |
| 173 std::wstring wide = kConvertRoundtripCases[i]; |
| 174 std::wstring trip = UTF8ToWide(base::SysWideToNativeMB(wide)); |
| 175 EXPECT_EQ(wide.size(), trip.size()); |
| 176 EXPECT_EQ(wide, trip); |
| 177 } |
| 178 } |
OLD | NEW |