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/sys_string_conversions.h" | 6 #include "base/sys_string_conversions.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 // Apparently Windows doesn't have constants for these. | 9 #ifdef WCHAR_T_IS_UTF32 |
10 static const int kCpLatin1 = 850; | 10 static const std::wstring kSysWideOldItalicLetterA = L"\x10300"; |
11 static const int kCpBig5 = 950; | 11 #else |
| 12 static const std::wstring kSysWideOldItalicLetterA = L"\xd800\xdf00"; |
| 13 #endif |
12 | 14 |
13 TEST(SysStringsWin, SysWideToUTF8) { | 15 TEST(SysStrings, SysWideToUTF8) { |
14 using base::SysWideToUTF8; | 16 using base::SysWideToUTF8; |
15 EXPECT_EQ("Hello, world", SysWideToUTF8(L"Hello, world")); | 17 EXPECT_EQ("Hello, world", SysWideToUTF8(L"Hello, world")); |
16 EXPECT_EQ("\xe4\xbd\xa0\xe5\xa5\xbd", SysWideToUTF8(L"\x4f60\x597d")); | 18 EXPECT_EQ("\xe4\xbd\xa0\xe5\xa5\xbd", SysWideToUTF8(L"\x4f60\x597d")); |
17 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToUTF8(L"\xd800\xdf00")); // >16 bits | 19 |
| 20 // >16 bits |
| 21 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToUTF8(kSysWideOldItalicLetterA)); |
18 | 22 |
19 // Error case. When Windows finds a UTF-16 character going off the end of | 23 // Error case. When Windows finds a UTF-16 character going off the end of |
20 // a string, it just converts that literal value to UTF-8, even though this | 24 // a string, it just converts that literal value to UTF-8, even though this |
21 // is invalid. | 25 // is invalid. |
22 // | 26 // |
23 // This is what XP does, but Vista has different behavior, so we don't bother | 27 // This is what XP does, but Vista has different behavior, so we don't bother |
24 // verifying it: | 28 // verifying it: |
25 //EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw", SysWideToUTF8(L"\x4f60\xd800zyxw")
); | 29 //EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw", SysWideToUTF8(L"\x4f60\xd800zyxw")
); |
26 | 30 |
27 // Test embedded NULLs. | 31 // Test embedded NULLs. |
28 std::wstring wide_null(L"a"); | 32 std::wstring wide_null(L"a"); |
29 wide_null.push_back(0); | 33 wide_null.push_back(0); |
30 wide_null.push_back('b'); | 34 wide_null.push_back('b'); |
31 | 35 |
32 std::string expected_null("a"); | 36 std::string expected_null("a"); |
33 expected_null.push_back(0); | 37 expected_null.push_back(0); |
34 expected_null.push_back('b'); | 38 expected_null.push_back('b'); |
35 | 39 |
36 EXPECT_EQ(expected_null, SysWideToUTF8(wide_null)); | 40 EXPECT_EQ(expected_null, SysWideToUTF8(wide_null)); |
37 } | 41 } |
38 | 42 |
39 TEST(SysStringsWin, SysUTF8ToWide) { | 43 TEST(SysStrings, SysUTF8ToWide) { |
40 using base::SysUTF8ToWide; | 44 using base::SysUTF8ToWide; |
41 EXPECT_EQ(L"Hello, world", SysUTF8ToWide("Hello, world")); | 45 EXPECT_EQ(L"Hello, world", SysUTF8ToWide("Hello, world")); |
42 EXPECT_EQ(L"\x4f60\x597d", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); | 46 EXPECT_EQ(L"\x4f60\x597d", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); |
43 EXPECT_EQ(L"\xd800\xdf00", SysUTF8ToWide("\xF0\x90\x8C\x80")); // >16 bits | 47 // >16 bits |
| 48 EXPECT_EQ(kSysWideOldItalicLetterA, SysUTF8ToWide("\xF0\x90\x8C\x80")); |
44 | 49 |
45 // Error case. When Windows finds an invalid UTF-8 character, it just skips | 50 // Error case. When Windows finds an invalid UTF-8 character, it just skips |
46 // it. This seems weird because it's inconsistent with the reverse conversion. | 51 // it. This seems weird because it's inconsistent with the reverse conversion. |
47 // | 52 // |
48 // This is what XP does, but Vista has different behavior, so we don't bother | 53 // This is what XP does, but Vista has different behavior, so we don't bother |
49 // verifying it: | 54 // verifying it: |
50 //EXPECT_EQ(L"\x4f60zyxw", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5zyxw")); | 55 //EXPECT_EQ(L"\x4f60zyxw", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5zyxw")); |
51 | 56 |
52 // Test embedded NULLs. | 57 // Test embedded NULLs. |
53 std::string utf8_null("a"); | 58 std::string utf8_null("a"); |
54 utf8_null.push_back(0); | 59 utf8_null.push_back(0); |
55 utf8_null.push_back('b'); | 60 utf8_null.push_back('b'); |
56 | 61 |
57 std::wstring expected_null(L"a"); | 62 std::wstring expected_null(L"a"); |
58 expected_null.push_back(0); | 63 expected_null.push_back(0); |
59 expected_null.push_back('b'); | 64 expected_null.push_back('b'); |
60 | 65 |
61 EXPECT_EQ(expected_null, SysUTF8ToWide(utf8_null)); | 66 EXPECT_EQ(expected_null, SysUTF8ToWide(utf8_null)); |
62 } | 67 } |
63 | |
OLD | NEW |