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 "config.h"
6
7#include "wtf/text/TextCodecReplacement.h"
8
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>
16
17namespace WTF {
18
19namespace {
20
21// Just one example, others are listed in the codec implementation.
22const char* replacementAlias = "iso-2022-kr";
23
24TEST(TextCodecReplacement, Aliases)
25{
26    // "replacement" is not a valid alias for itself
27    EXPECT_FALSE(TextEncoding("replacement").isValid());
28    EXPECT_FALSE(TextEncoding("rEpLaCeMeNt").isValid());
29
30    EXPECT_TRUE(TextEncoding(replacementAlias).isValid());
31    EXPECT_STREQ("replacement", TextEncoding(replacementAlias).name());
32}
33
34TEST(TextCodecReplacement, DecodesToFFFD)
35{
36    TextEncoding encoding(replacementAlias);
37    OwnPtr<TextCodec> codec(newTextCodec(encoding));
38
39    bool sawError = false;
40    const char testCase[] = "hello world";
41    size_t testCaseSize = sizeof(testCase) - 1;
42
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]);
47}
48
49TEST(TextCodecReplacement, EncodesToUTF8)
50{
51    TextEncoding encoding(replacementAlias);
52    OwnPtr<TextCodec> codec(newTextCodec(encoding));
53
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);
58
59    EXPECT_STREQ("\xE6\xBC\xA2\xE5\xAD\x97", result.data());
60}
61
62} // namespace
63
64} // namespace WTF
65