• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 PDFium 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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "xfa/fgas/layout/cfx_rtfbreak.h"
8 
9 #include <memory>
10 
11 #include "core/fxge/cfx_gemodule.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/test_support.h"
14 #include "third_party/base/ptr_util.h"
15 #include "xfa/fgas/font/cfgas_fontmgr.h"
16 #include "xfa/fgas/font/cfgas_gefont.h"
17 
18 class CFX_RTFBreakTest : public testing::Test {
19  public:
SetUp()20   void SetUp() override {
21     font_ =
22         CFGAS_GEFont::LoadFont(L"Arial Black", 0, 0, GetGlobalFontManager());
23     ASSERT(font_.Get() != nullptr);
24   }
25 
CreateBreak(int32_t args)26   std::unique_ptr<CFX_RTFBreak> CreateBreak(int32_t args) {
27     auto b = pdfium::MakeUnique<CFX_RTFBreak>(args);
28     b->SetFont(font_);
29     return b;
30   }
31 
32  private:
33   RetainPtr<CFGAS_GEFont> font_;
34 };
35 
36 // As soon as you get one of the control characters the break is complete
37 // and must be consumed before you get any more characters ....
38 
TEST_F(CFX_RTFBreakTest,AddChars)39 TEST_F(CFX_RTFBreakTest, AddChars) {
40   auto b = CreateBreak(FX_LAYOUTSTYLE_ExpandTab);
41 
42   WideString str(L"Input String.");
43   for (const auto& c : str)
44     EXPECT_EQ(CFX_BreakType::None, b->AppendChar(c));
45 
46   EXPECT_EQ(CFX_BreakType::Paragraph, b->AppendChar(L'\n'));
47   ASSERT_EQ(1, b->CountBreakPieces());
48   EXPECT_EQ(str + L"\n", b->GetBreakPieceUnstable(0)->GetString());
49 
50   b->ClearBreakPieces();
51   b->Reset();
52   EXPECT_EQ(0, b->GetCurrentLineForTesting()->GetLineEnd());
53 
54   str = L"Second str.";
55   for (const auto& c : str)
56     EXPECT_EQ(CFX_BreakType::None, b->AppendChar(c));
57 
58   // Force the end of the break at the end of the string.
59   b->EndBreak(CFX_BreakType::Paragraph);
60   ASSERT_EQ(1, b->CountBreakPieces());
61   EXPECT_EQ(str, b->GetBreakPieceUnstable(0)->GetString());
62 }
63 
TEST_F(CFX_RTFBreakTest,ControlCharacters)64 TEST_F(CFX_RTFBreakTest, ControlCharacters) {
65   auto b = CreateBreak(FX_LAYOUTSTYLE_ExpandTab);
66   EXPECT_EQ(CFX_BreakType::Line, b->AppendChar(L'\v'));
67   EXPECT_EQ(CFX_BreakType::Page, b->AppendChar(L'\f'));
68   // 0x2029 is the Paragraph Separator unicode character.
69   EXPECT_EQ(CFX_BreakType::Paragraph, b->AppendChar(0x2029));
70   EXPECT_EQ(CFX_BreakType::Paragraph, b->AppendChar(L'\n'));
71 
72   ASSERT_EQ(1, b->CountBreakPieces());
73   EXPECT_EQ(L"\v", b->GetBreakPieceUnstable(0)->GetString());
74 }
75