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> | 5 #include <locale.h> |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/string_piece.h" | 10 #include "base/string_piece.h" |
11 #include "base/string_util.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 |
21 TEST(SysStrings, SysWideToUTF8) { | 21 TEST(SysStrings, SysWideToUTF8) { |
22 using base::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; | 51 using base::SysUTF8ToWide; |
52 EXPECT_EQ(L"Hello, world", SysUTF8ToWide("Hello, world")); | 52 EXPECT_EQ(L"Hello, world", SysUTF8ToWide("Hello, world")); |
53 EXPECT_EQ(L"\x4f60\x597d", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); | 53 EXPECT_EQ(L"\x4f60\x597d", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); |
54 // >16 bits | 54 // >16 bits |
55 EXPECT_EQ(kSysWideOldItalicLetterA, SysUTF8ToWide("\xF0\x90\x8C\x80")); | 55 EXPECT_EQ(kSysWideOldItalicLetterA, SysUTF8ToWide("\xF0\x90\x8C\x80")); |
56 | 56 |
57 // Error case. When Windows finds an invalid UTF-8 character, it just skips | 57 // 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. | 58 // it. This seems weird because it's inconsistent with the reverse conversion. |
59 // | 59 // |
60 // This is what XP does, but Vista has different behavior, so we don't bother | 60 // This is what XP does, but Vista has different behavior, so we don't bother |
61 // verifying it: | 61 // verifying it: |
62 //EXPECT_EQ(L"\x4f60zyxw", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5zyxw")); | 62 // EXPECT_EQ(L"\x4f60zyxw", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5zyxw")); |
63 | 63 |
64 // Test embedded NULLs. | 64 // Test embedded NULLs. |
65 std::string utf8_null("a"); | 65 std::string utf8_null("a"); |
66 utf8_null.push_back(0); | 66 utf8_null.push_back(0); |
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 |
(...skipping 27 matching lines...) Expand all Loading... |
100 | 100 |
101 // >16 bits | 101 // >16 bits |
102 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToNativeMB(kSysWideOldItalicLetterA)); | 102 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToNativeMB(kSysWideOldItalicLetterA)); |
103 | 103 |
104 // Error case. When Windows finds a UTF-16 character going off the end of | 104 // 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 | 105 // a string, it just converts that literal value to UTF-8, even though this |
106 // is invalid. | 106 // is invalid. |
107 // | 107 // |
108 // This is what XP does, but Vista has different behavior, so we don't bother | 108 // This is what XP does, but Vista has different behavior, so we don't bother |
109 // verifying it: | 109 // verifying it: |
110 //EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw", | 110 // EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw", |
111 // SysWideToNativeMB(L"\x4f60\xd800zyxw")); | 111 // SysWideToNativeMB(L"\x4f60\xd800zyxw")); |
112 | 112 |
113 // Test embedded NULLs. | 113 // Test embedded NULLs. |
114 std::wstring wide_null(L"a"); | 114 std::wstring wide_null(L"a"); |
115 wide_null.push_back(0); | 115 wide_null.push_back(0); |
116 wide_null.push_back('b'); | 116 wide_null.push_back('b'); |
117 | 117 |
118 std::string expected_null("a"); | 118 std::string expected_null("a"); |
119 expected_null.push_back(0); | 119 expected_null.push_back(0); |
120 expected_null.push_back('b'); | 120 expected_null.push_back('b'); |
121 | 121 |
122 EXPECT_EQ(expected_null, SysWideToNativeMB(wide_null)); | 122 EXPECT_EQ(expected_null, SysWideToNativeMB(wide_null)); |
123 } | 123 } |
124 | 124 |
125 // We assume the test is running in a UTF8 locale. | 125 // We assume the test is running in a UTF8 locale. |
126 TEST(SysStrings, SysNativeMBToWide) { | 126 TEST(SysStrings, SysNativeMBToWide) { |
127 using base::SysNativeMBToWide; | 127 using base::SysNativeMBToWide; |
128 ScopedSetLocale locale("en_US.utf-8"); | 128 ScopedSetLocale locale("en_US.utf-8"); |
129 EXPECT_EQ(L"Hello, world", SysNativeMBToWide("Hello, world")); | 129 EXPECT_EQ(L"Hello, world", SysNativeMBToWide("Hello, world")); |
130 EXPECT_EQ(L"\x4f60\x597d", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); | 130 EXPECT_EQ(L"\x4f60\x597d", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); |
131 // >16 bits | 131 // >16 bits |
132 EXPECT_EQ(kSysWideOldItalicLetterA, SysNativeMBToWide("\xF0\x90\x8C\x80")); | 132 EXPECT_EQ(kSysWideOldItalicLetterA, SysNativeMBToWide("\xF0\x90\x8C\x80")); |
133 | 133 |
134 // Error case. When Windows finds an invalid UTF-8 character, it just skips | 134 // 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. | 135 // it. This seems weird because it's inconsistent with the reverse conversion. |
136 // | 136 // |
137 // This is what XP does, but Vista has different behavior, so we don't bother | 137 // This is what XP does, but Vista has different behavior, so we don't bother |
138 // verifying it: | 138 // verifying it: |
139 //EXPECT_EQ(L"\x4f60zyxw", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5zyxw")); | 139 // EXPECT_EQ(L"\x4f60zyxw", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5zyxw")); |
140 | 140 |
141 // Test embedded NULLs. | 141 // Test embedded NULLs. |
142 std::string utf8_null("a"); | 142 std::string utf8_null("a"); |
143 utf8_null.push_back(0); | 143 utf8_null.push_back(0); |
144 utf8_null.push_back('b'); | 144 utf8_null.push_back('b'); |
145 | 145 |
146 std::wstring expected_null(L"a"); | 146 std::wstring expected_null(L"a"); |
147 expected_null.push_back(0); | 147 expected_null.push_back(0); |
148 expected_null.push_back('b'); | 148 expected_null.push_back('b'); |
149 | 149 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 } | 196 } |
197 | 197 |
198 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { | 198 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { |
199 std::wstring wide = kConvertRoundtripCases[i]; | 199 std::wstring wide = kConvertRoundtripCases[i]; |
200 std::wstring trip = UTF8ToWide(base::SysWideToNativeMB(wide)); | 200 std::wstring trip = UTF8ToWide(base::SysWideToNativeMB(wide)); |
201 EXPECT_EQ(wide.size(), trip.size()); | 201 EXPECT_EQ(wide.size(), trip.size()); |
202 EXPECT_EQ(wide, trip); | 202 EXPECT_EQ(wide, trip); |
203 } | 203 } |
204 } | 204 } |
205 #endif // OS_LINUX | 205 #endif // OS_LINUX |
OLD | NEW |