OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/sync/util/character_set_converters.h" | 5 #include "chrome/browser/sync/util/character_set_converters.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "build/build_config.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 using browser_sync::ToPathString; | 13 using browser_sync::ToPathString; |
13 using browser_sync::ToUTF8; | 14 using browser_sync::ToUTF8; |
14 using browser_sync::AppendPathStringToUTF8; | 15 using browser_sync::AppendPathStringToUTF8; |
15 using browser_sync::AppendUTF8ToPathString; | 16 using browser_sync::AppendUTF8ToPathString; |
16 using browser_sync::PathStringToUTF8; | 17 using browser_sync::PathStringToUTF8; |
17 using browser_sync::UTF8ToPathString; | 18 using browser_sync::UTF8ToPathString; |
18 using std::string; | 19 using std::string; |
19 | 20 |
20 class CharacterSetConverterTest : public testing::Test { | 21 class CharacterSetConverterTest : public testing::Test { |
21 }; | 22 }; |
22 | 23 |
23 TEST_F(CharacterSetConverterTest, ASCIIConversionTest) { | 24 TEST_F(CharacterSetConverterTest, ASCIIConversionTest) { |
24 string ascii = "Test String"; | 25 string ascii = "Test String"; |
25 PathString wide = PSTR("Test String"); | 26 PathString wide = PSTR("Test String"); |
26 ToPathString to_wide(ascii); | 27 ToPathString to_wide(ascii); |
27 ASSERT_TRUE(to_wide.good()); | 28 ASSERT_TRUE(to_wide.good()); |
28 ToUTF8 to_utf8(wide); | 29 ToUTF8 to_utf8(wide); |
29 | 30 |
30 // Using == as gunit doesn't handle PathString equality tests correctly (it | 31 // Using == as gunit doesn't handle PathString equality tests correctly (it |
31 // tries to print the PathString and fails). | 32 // tries to print the PathString and fails). |
32 ASSERT_TRUE(PathString(wide) == to_wide.get_string16()); | 33 ASSERT_TRUE(PathString(wide) == to_wide.get_string16()); |
33 ASSERT_EQ(string(ascii), to_utf8.get_string()); | 34 ASSERT_EQ(string(ascii), to_utf8.get_string()); |
34 ToPathString to_16(ascii); | 35 ToPathString to_16(ascii); |
35 ASSERT_TRUE(to_16.good()); | 36 ASSERT_TRUE(to_16.good()); |
36 ASSERT_TRUE(PathString(wide) == to_16.get_string16()); | 37 ASSERT_TRUE(PathString(wide) == to_16.get_string16()); |
37 #ifdef OS_WINDOWS | 38 #ifdef OS_WIN |
38 // On Linux, PathString is already UTF8 | 39 // On Linux, PathString is already UTF8 |
39 ASSERT_EQ(string(ascii), static_cast<string>(ToUTF8(wide))); | 40 ASSERT_EQ(string(ascii), static_cast<string>(ToUTF8(wide))); |
40 #endif | 41 #endif |
41 // The next line fails the good_checked_ test. It would be a good death test | 42 // The next line fails the good_checked_ test. It would be a good death test |
42 // but they don't work on Windows. | 43 // but they don't work on Windows. |
43 // ASSERT_TRUE(wide == ToPathString(utf8).get_string16()); | 44 // ASSERT_TRUE(wide == ToPathString(utf8).get_string16()); |
44 } | 45 } |
45 | 46 |
46 #ifdef OS_WINDOWS | 47 #ifdef OS_WIN |
47 // On Linux, PathString is already UTF8 | 48 // On Linux, PathString is already UTF8 |
48 TEST_F(CharacterSetConverterTest, UnicodeConversionText) { | 49 TEST_F(CharacterSetConverterTest, UnicodeConversionText) { |
49 // Source data obtained by running od -b on files saved in utf-8 and unicode | 50 // Source data obtained by running od -b on files saved in utf-8 and unicode |
50 // from a text editor. | 51 // from a text editor. |
51 const char* utf8 = "\357\273\277\150\145\154\154\157\040\303\250\303\251" | 52 const char* utf8 = "\357\273\277\150\145\154\154\157\040\303\250\303\251" |
52 "\302\251\342\202\254\302\243\302\245\302\256\342\204\242"; | 53 "\302\251\342\202\254\302\243\302\245\302\256\342\204\242"; |
53 // #ifdef IS_LITTLE_ENDIAN | 54 // #ifdef IS_LITTLE_ENDIAN |
54 const PathChar* wide = reinterpret_cast<const PathChar*>("\377\376\150\000" | 55 const PathChar* wide = reinterpret_cast<const PathChar*>("\377\376\150\000" |
55 "\145\000\154\000\154\000\157\000\040\000\350\000\351\000\251\000\254\040" | 56 "\145\000\154\000\154\000\157\000\040\000\350\000\351\000\251\000\254\040" |
56 "\243\000\245\000\256\000\042\041"); | 57 "\243\000\245\000\256\000\042\041"); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 PathString out; | 101 PathString out; |
101 AppendUTF8ToPathString(one.data(), one.length(), &out); | 102 AppendUTF8ToPathString(one.data(), one.length(), &out); |
102 AppendUTF8ToPathString(two.data(), two.length(), &out); | 103 AppendUTF8ToPathString(two.data(), two.length(), &out); |
103 AppendUTF8ToPathString(three.data(), three.length(), &out); | 104 AppendUTF8ToPathString(three.data(), three.length(), &out); |
104 ASSERT_TRUE(out == PathString(PSTR("onetwothree"))); | 105 ASSERT_TRUE(out == PathString(PSTR("onetwothree"))); |
105 string onetwothree = "onetwothree"; | 106 string onetwothree = "onetwothree"; |
106 UTF8ToPathString(onetwothree.data(), onetwothree.length(), &out); | 107 UTF8ToPathString(onetwothree.data(), onetwothree.length(), &out); |
107 ASSERT_TRUE(out == PathString(PSTR("onetwothree"))); | 108 ASSERT_TRUE(out == PathString(PSTR("onetwothree"))); |
108 } | 109 } |
109 | 110 |
110 #ifdef OS_WINDOWS | 111 #ifdef OS_WIN |
111 namespace { | 112 namespace { |
112 // See http://en.wikipedia.org/wiki/UTF-16 for an explanation of UTF16. | 113 // See http://en.wikipedia.org/wiki/UTF-16 for an explanation of UTF16. |
113 // For a test case we use the UTF-8 and UTF-16 encoding of char 119070 | 114 // For a test case we use the UTF-8 and UTF-16 encoding of char 119070 |
114 // (hex 1D11E), which is musical G clef. | 115 // (hex 1D11E), which is musical G clef. |
115 const unsigned char utf8_test_string[] = { | 116 const unsigned char utf8_test_string[] = { |
116 0xEF, 0xBB, 0xBF, // BOM | 117 0xEF, 0xBB, 0xBF, // BOM |
117 0xE6, 0xB0, 0xB4, // water, Chinese (0x6C34) | 118 0xE6, 0xB0, 0xB4, // water, Chinese (0x6C34) |
118 0x7A, // lower case z | 119 0x7A, // lower case z |
119 0xF0, 0x9D, 0x84, 0x9E, // musical G clef (0x1D11E) | 120 0xF0, 0x9D, 0x84, 0x9E, // musical G clef (0x1D11E) |
120 0x00, | 121 0x00, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 message.resize(message.length() - 1); | 160 message.resize(message.length() - 1); |
160 old_length = message.length(); | 161 old_length = message.length(); |
161 } | 162 } |
162 TrimPathStringToValidCharacter(&message); | 163 TrimPathStringToValidCharacter(&message); |
163 } | 164 } |
164 #else | 165 #else |
165 | 166 |
166 // TODO(zork): Add unittests here once we're running these tests on linux. | 167 // TODO(zork): Add unittests here once we're running these tests on linux. |
167 | 168 |
168 #endif | 169 #endif |
OLD | NEW |