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