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.
7 #include "wtf/text/TextCodecReplacement.h"
9 #include "wtf/OwnPtr.h"
10 #include "wtf/text/CString.h"
11 #include "wtf/text/TextCodec.h"
12 #include "wtf/text/TextEncoding.h"
13 #include "wtf/text/TextEncodingRegistry.h"
14 #include "wtf/text/WTFString.h"
15 #include <gtest/gtest.h>
21 // Just one example, others are listed in the codec implementation.
22 const char* replacementAlias
= "iso-2022-kr";
24 TEST(TextCodecReplacement
, Aliases
)
26 // "replacement" is not a valid alias for itself
27 EXPECT_FALSE(TextEncoding("replacement").isValid());
28 EXPECT_FALSE(TextEncoding("rEpLaCeMeNt").isValid());
30 EXPECT_TRUE(TextEncoding(replacementAlias
).isValid());
31 EXPECT_STREQ("replacement", TextEncoding(replacementAlias
).name());
34 TEST(TextCodecReplacement
, DecodesToFFFD
)
36 TextEncoding
encoding(replacementAlias
);
37 OwnPtr
<TextCodec
> codec(newTextCodec(encoding
));
39 bool sawError
= false;
40 const char testCase
[] = "hello world";
41 size_t testCaseSize
= sizeof(testCase
) - 1;
43 const String result
= codec
->decode(testCase
, testCaseSize
, DataEOF
, false, sawError
);
44 EXPECT_TRUE(sawError
);
45 ASSERT_EQ(1u, result
.length());
46 EXPECT_EQ(0xFFFDU
, result
[0]);
49 TEST(TextCodecReplacement
, EncodesToUTF8
)
51 TextEncoding
encoding(replacementAlias
);
52 OwnPtr
<TextCodec
> codec(newTextCodec(encoding
));
54 // "Kanji" in Chinese characters.
55 const UChar testCase
[] = { 0x6F22, 0x5B57 };
56 size_t testCaseSize
= WTF_ARRAY_LENGTH(testCase
);
57 CString result
= codec
->encode(testCase
, testCaseSize
, QuestionMarksForUnencodables
);
59 EXPECT_STREQ("\xE6\xBC\xA2\xE5\xAD\x97", result
.data());