summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/wtf/text/text_codec_replacement_test.cc
blob: b5d94405b6290c1e488a46dce624e337430fa5d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/wtf/text/text_codec_replacement.h"

#include <memory>
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/wtf/text/cstring.h"
#include "third_party/blink/renderer/platform/wtf/text/text_codec.h"
#include "third_party/blink/renderer/platform/wtf/text/text_encoding.h"
#include "third_party/blink/renderer/platform/wtf/text/text_encoding_registry.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace WTF {

namespace {

// Just one example, others are listed in the codec implementation.
const char* g_replacement_alias = "iso-2022-kr";

TEST(TextCodecReplacement, Aliases) {
  EXPECT_TRUE(TextEncoding("replacement").IsValid());
  EXPECT_TRUE(TextEncoding("rEpLaCeMeNt").IsValid());

  EXPECT_TRUE(TextEncoding(g_replacement_alias).IsValid());
  EXPECT_STREQ("replacement", TextEncoding(g_replacement_alias).GetName());
}

TEST(TextCodecReplacement, DecodesToFFFD) {
  TextEncoding encoding(g_replacement_alias);
  std::unique_ptr<TextCodec> codec(NewTextCodec(encoding));

  bool saw_error = false;
  const char kTestCase[] = "hello world";
  size_t test_case_size = sizeof(kTestCase) - 1;

  const String result =
      codec->Decode(kTestCase, test_case_size, kDataEOF, false, saw_error);
  EXPECT_TRUE(saw_error);
  ASSERT_EQ(1u, result.length());
  EXPECT_EQ(0xFFFDU, result[0]);
}

TEST(TextCodecReplacement, EncodesToUTF8) {
  TextEncoding encoding(g_replacement_alias);
  std::unique_ptr<TextCodec> codec(NewTextCodec(encoding));

  // "Kanji" in Chinese characters.
  const UChar kTestCase[] = {0x6F22, 0x5B57};
  size_t test_case_size = WTF_ARRAY_LENGTH(kTestCase);
  CString result =
      codec->Encode(kTestCase, test_case_size, kEntitiesForUnencodables);

  EXPECT_STREQ("\xE6\xBC\xA2\xE5\xAD\x97", result.data());
}

}  // namespace

}  // namespace WTF