OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "wtf/text/TextCodecReplacement.h" | 5 #include "wtf/text/TextCodecReplacement.h" |
6 | 6 |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "wtf/OwnPtr.h" |
8 #include "wtf/text/CString.h" | 9 #include "wtf/text/CString.h" |
9 #include "wtf/text/TextCodec.h" | 10 #include "wtf/text/TextCodec.h" |
10 #include "wtf/text/TextEncoding.h" | 11 #include "wtf/text/TextEncoding.h" |
11 #include "wtf/text/TextEncodingRegistry.h" | 12 #include "wtf/text/TextEncodingRegistry.h" |
12 #include "wtf/text/WTFString.h" | 13 #include "wtf/text/WTFString.h" |
13 #include <memory> | |
14 | 14 |
15 namespace WTF { | 15 namespace WTF { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // Just one example, others are listed in the codec implementation. | 19 // Just one example, others are listed in the codec implementation. |
20 const char* replacementAlias = "iso-2022-kr"; | 20 const char* replacementAlias = "iso-2022-kr"; |
21 | 21 |
22 TEST(TextCodecReplacement, Aliases) | 22 TEST(TextCodecReplacement, Aliases) |
23 { | 23 { |
24 // "replacement" is not a valid alias for itself | 24 // "replacement" is not a valid alias for itself |
25 EXPECT_FALSE(TextEncoding("replacement").isValid()); | 25 EXPECT_FALSE(TextEncoding("replacement").isValid()); |
26 EXPECT_FALSE(TextEncoding("rEpLaCeMeNt").isValid()); | 26 EXPECT_FALSE(TextEncoding("rEpLaCeMeNt").isValid()); |
27 | 27 |
28 EXPECT_TRUE(TextEncoding(replacementAlias).isValid()); | 28 EXPECT_TRUE(TextEncoding(replacementAlias).isValid()); |
29 EXPECT_STREQ("replacement", TextEncoding(replacementAlias).name()); | 29 EXPECT_STREQ("replacement", TextEncoding(replacementAlias).name()); |
30 } | 30 } |
31 | 31 |
32 TEST(TextCodecReplacement, DecodesToFFFD) | 32 TEST(TextCodecReplacement, DecodesToFFFD) |
33 { | 33 { |
34 TextEncoding encoding(replacementAlias); | 34 TextEncoding encoding(replacementAlias); |
35 std::unique_ptr<TextCodec> codec(newTextCodec(encoding)); | 35 OwnPtr<TextCodec> codec(newTextCodec(encoding)); |
36 | 36 |
37 bool sawError = false; | 37 bool sawError = false; |
38 const char testCase[] = "hello world"; | 38 const char testCase[] = "hello world"; |
39 size_t testCaseSize = sizeof(testCase) - 1; | 39 size_t testCaseSize = sizeof(testCase) - 1; |
40 | 40 |
41 const String result = codec->decode(testCase, testCaseSize, DataEOF, false,
sawError); | 41 const String result = codec->decode(testCase, testCaseSize, DataEOF, false,
sawError); |
42 EXPECT_TRUE(sawError); | 42 EXPECT_TRUE(sawError); |
43 ASSERT_EQ(1u, result.length()); | 43 ASSERT_EQ(1u, result.length()); |
44 EXPECT_EQ(0xFFFDU, result[0]); | 44 EXPECT_EQ(0xFFFDU, result[0]); |
45 } | 45 } |
46 | 46 |
47 TEST(TextCodecReplacement, EncodesToUTF8) | 47 TEST(TextCodecReplacement, EncodesToUTF8) |
48 { | 48 { |
49 TextEncoding encoding(replacementAlias); | 49 TextEncoding encoding(replacementAlias); |
50 std::unique_ptr<TextCodec> codec(newTextCodec(encoding)); | 50 OwnPtr<TextCodec> codec(newTextCodec(encoding)); |
51 | 51 |
52 // "Kanji" in Chinese characters. | 52 // "Kanji" in Chinese characters. |
53 const UChar testCase[] = { 0x6F22, 0x5B57 }; | 53 const UChar testCase[] = { 0x6F22, 0x5B57 }; |
54 size_t testCaseSize = WTF_ARRAY_LENGTH(testCase); | 54 size_t testCaseSize = WTF_ARRAY_LENGTH(testCase); |
55 CString result = codec->encode(testCase, testCaseSize, QuestionMarksForUnenc
odables); | 55 CString result = codec->encode(testCase, testCaseSize, QuestionMarksForUnenc
odables); |
56 | 56 |
57 EXPECT_STREQ("\xE6\xBC\xA2\xE5\xAD\x97", result.data()); | 57 EXPECT_STREQ("\xE6\xBC\xA2\xE5\xAD\x97", result.data()); |
58 } | 58 } |
59 | 59 |
60 } // namespace | 60 } // namespace |
61 | 61 |
62 } // namespace WTF | 62 } // namespace WTF |
OLD | NEW |