From 19fad57424146f0987265c701a58450c43f09008 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Mon, 6 Mar 2017 11:34:03 -0500 Subject: Adding tests for CFX_RTFBreak MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This CL starts adding tests to the CFX_RTFBreak class. In doing so, the code to append char has been simplified to make it clearer. Change-Id: I5a2c8c114b52e1efa0c41a18670c6542b733baae Reviewed-on: https://pdfium-review.googlesource.com/2900 Reviewed-by: Nicolás Peña Commit-Queue: dsinclair --- xfa/fgas/layout/fgas_rtfbreak.h | 3 ++ xfa/fgas/layout/fgas_rtfbreak_unittest.cpp | 87 ++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 xfa/fgas/layout/fgas_rtfbreak_unittest.cpp (limited to 'xfa/fgas/layout') diff --git a/xfa/fgas/layout/fgas_rtfbreak.h b/xfa/fgas/layout/fgas_rtfbreak.h index 4d999ec682..2dfc0248a1 100644 --- a/xfa/fgas/layout/fgas_rtfbreak.h +++ b/xfa/fgas/layout/fgas_rtfbreak.h @@ -169,6 +169,9 @@ class CFX_RTFBreak { bool bCharCode) const; CFX_RTFBreakType AppendChar(FX_WCHAR wch); + + CFX_RTFLine* GetCurrentLineForTesting() const { return m_pCurLine; } + CFX_RTFBreakType AppendChar_Combination(CFX_RTFChar* pCurChar); CFX_RTFBreakType AppendChar_Tab(CFX_RTFChar* pCurChar); CFX_RTFBreakType AppendChar_Control(CFX_RTFChar* pCurChar); diff --git a/xfa/fgas/layout/fgas_rtfbreak_unittest.cpp b/xfa/fgas/layout/fgas_rtfbreak_unittest.cpp new file mode 100644 index 0000000000..aba440aac7 --- /dev/null +++ b/xfa/fgas/layout/fgas_rtfbreak_unittest.cpp @@ -0,0 +1,87 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "xfa/fgas/layout/fgas_rtfbreak.h" + +#include + +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/test_support.h" +#include "third_party/base/ptr_util.h" +#include "xfa/fgas/font/cfgas_fontmgr.h" +#include "xfa/fgas/font/cfgas_gefont.h" + +class RTFBreakTest : public testing::Test { + public: + void SetUp() override { + CFX_GEModule::Get()->GetFontMgr()->SetSystemFontInfo( + IFX_SystemFontInfo::CreateDefault(nullptr)); + +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ + font_mgr_ = CFGAS_FontMgr::Create(FX_GetDefFontEnumerator()); +#else + font_source_ = pdfium::MakeUnique(); + font_mgr_ = CFGAS_FontMgr::Create(font_source_.get()); +#endif + + font_ = CFGAS_GEFont::LoadFont(L"Arial Black", 0, 0, font_mgr_.get()); + ASSERT(font_.Get() != nullptr); + } + + std::unique_ptr CreateBreak(int32_t args) { + auto b = pdfium::MakeUnique(args); + b->SetFont(font_); + return b; + } + + private: + std::unique_ptr font_mgr_; + CFX_RetainPtr font_; + +#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ + std::unique_ptr font_source_; +#endif +}; + +// As soon as you get one of the control characters the break is complete +// and must be consumed before you get any more characters .... + +TEST_F(RTFBreakTest, AddChars) { + auto b = CreateBreak(FX_RTFLAYOUTSTYLE_ExpandTab); + + CFX_WideString str(L"Input String."); + for (int32_t i = 0; i < str.GetLength(); i++) + EXPECT_EQ(CFX_RTFBreakType::None, b->AppendChar(str.GetAt(i))); + + EXPECT_EQ(CFX_RTFBreakType::Paragraph, b->AppendChar(L'\n')); + ASSERT_EQ(1, b->CountBreakPieces()); + EXPECT_EQ(str + L"\n", b->GetBreakPiece(0)->GetString()); + + b->ClearBreakPieces(); + b->Reset(); + EXPECT_EQ(0, b->GetCurrentLineForTesting()->GetLineEnd()); + + str = L"Second str."; + for (int32_t i = 0; i < str.GetLength(); i++) + EXPECT_EQ(CFX_RTFBreakType::None, b->AppendChar(str.GetAt(i))); + + // Force the end of the break at the end of the string. + b->EndBreak(CFX_RTFBreakType::Paragraph); + ASSERT_EQ(1, b->CountBreakPieces()); + EXPECT_EQ(str, b->GetBreakPiece(0)->GetString()); +} + +TEST_F(RTFBreakTest, ControlCharacters) { + auto b = CreateBreak(FX_RTFLAYOUTSTYLE_ExpandTab); + EXPECT_EQ(CFX_RTFBreakType::Line, b->AppendChar(L'\v')); + EXPECT_EQ(CFX_RTFBreakType::Page, b->AppendChar(L'\f')); + // 0x2029 is the Paragraph Separator unicode character. + EXPECT_EQ(CFX_RTFBreakType::Paragraph, b->AppendChar(0x2029)); + EXPECT_EQ(CFX_RTFBreakType::Paragraph, b->AppendChar(L'\n')); + + ASSERT_EQ(1, b->CountBreakPieces()); + EXPECT_EQ(L"\v", b->GetBreakPiece(0)->GetString()); +} -- cgit v1.2.3