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