xref: /aosp_15_r20/external/pdfium/xfa/fgas/layout/cfgas_rtfbreak_unittest.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2017 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "xfa/fgas/layout/cfgas_rtfbreak.h"
8 
9 #include <memory>
10 #include <utility>
11 
12 #include "core/fxcrt/fx_codepage.h"
13 #include "core/fxge/cfx_font.h"
14 #include "core/fxge/cfx_gemodule.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "xfa/fgas/font/cfgas_fontmgr.h"
17 #include "xfa/fgas/font/cfgas_gefont.h"
18 #include "xfa/fgas/layout/cfgas_char.h"
19 
20 class CFGAS_RTFBreakTest : public testing::Test {
21  public:
SetUp()22   void SetUp() override {
23     const wchar_t kFontFamily[] = L"Arimo Bold";
24     font_ = CFGAS_GEFont::LoadFont(kFontFamily, 0, FX_CodePage::kDefANSI);
25     ASSERT_TRUE(font_);
26   }
27 
CreateBreak(Mask<CFGAS_Break::LayoutStyle> layout_styles)28   std::unique_ptr<CFGAS_RTFBreak> CreateBreak(
29       Mask<CFGAS_Break::LayoutStyle> layout_styles) {
30     auto rtf_break = std::make_unique<CFGAS_RTFBreak>(layout_styles);
31     rtf_break->SetFont(font_);
32     return rtf_break;
33   }
34 
35  private:
36   RetainPtr<CFGAS_GEFont> font_;
37 };
38 
39 // As soon as you get one of the control characters the break is complete
40 // and must be consumed before you get any more characters ....
41 
TEST_F(CFGAS_RTFBreakTest,AddChars)42 TEST_F(CFGAS_RTFBreakTest, AddChars) {
43   auto rtf_break = CreateBreak(CFGAS_Break::LayoutStyle::kExpandTab);
44   WideString str(L"Input String.");
45   for (wchar_t ch : str)
46     EXPECT_EQ(CFGAS_Char::BreakType::kNone, rtf_break->AppendChar(ch));
47 
48   EXPECT_EQ(CFGAS_Char::BreakType::kParagraph, rtf_break->AppendChar(L'\n'));
49   ASSERT_EQ(1, rtf_break->CountBreakPieces());
50   EXPECT_EQ(str + L"\n", rtf_break->GetBreakPieceUnstable(0)->GetString());
51 
52   rtf_break->ClearBreakPieces();
53   rtf_break->Reset();
54   EXPECT_EQ(0, rtf_break->GetCurrentLineForTesting()->GetLineEnd());
55 
56   str = L"Second str.";
57   for (wchar_t ch : str)
58     EXPECT_EQ(CFGAS_Char::BreakType::kNone, rtf_break->AppendChar(ch));
59 
60   // Force the end of the break at the end of the string.
61   rtf_break->EndBreak(CFGAS_Char::BreakType::kParagraph);
62   ASSERT_EQ(1, rtf_break->CountBreakPieces());
63   EXPECT_EQ(str, rtf_break->GetBreakPieceUnstable(0)->GetString());
64 }
65 
TEST_F(CFGAS_RTFBreakTest,ControlCharacters)66 TEST_F(CFGAS_RTFBreakTest, ControlCharacters) {
67   auto rtf_break = CreateBreak(CFGAS_Break::LayoutStyle::kExpandTab);
68   EXPECT_EQ(CFGAS_Char::BreakType::kLine, rtf_break->AppendChar(L'\v'));
69   EXPECT_EQ(CFGAS_Char::BreakType::kPage, rtf_break->AppendChar(L'\f'));
70   EXPECT_EQ(CFGAS_Char::BreakType::kParagraph,
71             rtf_break->AppendChar(pdfium::unicode::kParagraphSeparator));
72   EXPECT_EQ(CFGAS_Char::BreakType::kParagraph, rtf_break->AppendChar(L'\n'));
73 
74   ASSERT_EQ(1, rtf_break->CountBreakPieces());
75   EXPECT_EQ(L"\v", rtf_break->GetBreakPieceUnstable(0)->GetString());
76 }
77 
TEST_F(CFGAS_RTFBreakTest,BidiLine)78 TEST_F(CFGAS_RTFBreakTest, BidiLine) {
79   auto rtf_break = CreateBreak(CFGAS_Break::LayoutStyle::kExpandTab);
80   rtf_break->SetLineBreakTolerance(1);
81   rtf_break->SetFontSize(12);
82 
83   WideString input = WideString::FromUTF8(ByteStringView("\xa\x0\xa\xa", 4));
84   for (wchar_t ch : input)
85     rtf_break->AppendChar(ch);
86 
87   std::vector<CFGAS_Char> chars =
88       rtf_break->GetCurrentLineForTesting()->m_LineChars;
89   CFGAS_Char::BidiLine(&chars, chars.size());
90   EXPECT_EQ(3u, chars.size());
91 }
92