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