Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : */
9 :
10 : #include "rtl/ustring.hxx"
11 : #include "chpfld.hxx"
12 : #include "ndtxt.hxx"
13 : #include "tox.hxx"
14 : #include "txmsrt.hxx"
15 : #include "ToxTextGenerator.hxx"
16 : #include "ToxTabStopTokenHandler.hxx"
17 :
18 : #include <boost/make_shared.hpp>
19 :
20 : #include <cppunit/TestAssert.h>
21 : #include <cppunit/TestFixture.h>
22 : #include <cppunit/extensions/HelperMacros.h>
23 : #include <cppunit/plugin/TestPlugIn.h>
24 :
25 : using namespace sw;
26 :
27 42 : class ToxTextGeneratorTest : public CppUnit::TestFixture {
28 : public:
29 : void EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems();
30 : void OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem();
31 : void TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem();
32 : void EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet();
33 : void EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty();
34 : void ChapterNumberWithoutTextIsGeneratedForNoprepstTitle();
35 : void ChapterNumberWithTitleIsGeneratedForNumberNoPrepst();
36 :
37 4 : CPPUNIT_TEST_SUITE(ToxTextGeneratorTest);
38 2 : CPPUNIT_TEST(EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems);
39 2 : CPPUNIT_TEST(OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem);
40 2 : CPPUNIT_TEST(TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem);
41 2 : CPPUNIT_TEST(EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet);
42 2 : CPPUNIT_TEST(EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty);
43 2 : CPPUNIT_TEST(ChapterNumberWithoutTextIsGeneratedForNoprepstTitle);
44 2 : CPPUNIT_TEST(ChapterNumberWithTitleIsGeneratedForNumberNoPrepst);
45 4 : CPPUNIT_TEST_SUITE_END();
46 :
47 : };
48 :
49 4 : struct MockedSortTab : public SwTOXSortTabBase {
50 4 : MockedSortTab()
51 4 : : SwTOXSortTabBase(TOX_SORT_INDEX,0,0,0) {;}
52 :
53 0 : virtual TextAndReading GetText_Impl() const SAL_OVERRIDE {
54 0 : return TextAndReading();
55 : }
56 0 : virtual sal_uInt16 GetLevel() const SAL_OVERRIDE {
57 0 : return 0;
58 : }
59 : };
60 :
61 : void
62 2 : ToxTextGeneratorTest::EmptyStringIsReturnedForPageNumberPlaceholderOfZeroItems()
63 : {
64 2 : OUString expected("");
65 4 : OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(0);
66 4 : CPPUNIT_ASSERT_EQUAL(expected, actual);
67 2 : }
68 :
69 : void
70 2 : ToxTextGeneratorTest::OneAtSignIsReturnedForPageNumberPlaceholderOfOneItem()
71 : {
72 2 : OUString expected("@~");
73 4 : OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(1);
74 4 : CPPUNIT_ASSERT_EQUAL(expected, actual);
75 2 : }
76 :
77 : void
78 2 : ToxTextGeneratorTest::TwoAtSignsAreReturnedForPageNumberPlaceholderOfOneItem()
79 : {
80 2 : OUString expected("@, @~");
81 4 : OUString actual = ToxTextGenerator::ConstructPageNumberPlaceholder(2);
82 4 : CPPUNIT_ASSERT_EQUAL(expected, actual);
83 2 : }
84 :
85 : void
86 2 : ToxTextGeneratorTest::EmptyStringIsReturnedAsNumStringIfNoTextMarkIsSet()
87 : {
88 2 : MockedSortTab sortTab;
89 2 : sortTab.pTxtMark = NULL;
90 :
91 4 : OUString expected("");
92 4 : OUString actual = ToxTextGenerator::GetNumStringOfFirstNode(sortTab, false, 0);
93 4 : CPPUNIT_ASSERT_EQUAL(expected, actual);
94 2 : }
95 :
96 : void
97 2 : ToxTextGeneratorTest::EmptyStringIsReturnedAsNumStringIfToxSourcesIsEmpty()
98 : {
99 2 : MockedSortTab sortTab;
100 2 : sortTab.pTxtMark = reinterpret_cast<SwTxtTOXMark*>(1);
101 :
102 4 : OUString expected("");
103 4 : OUString actual = ToxTextGenerator::GetNumStringOfFirstNode(sortTab, false, 0);
104 4 : CPPUNIT_ASSERT_EQUAL(expected, actual);
105 2 : }
106 :
107 8 : class MockedToxTabStopTokenHandler : public ToxTabStopTokenHandler {
108 : public:
109 : virtual HandledTabStopToken
110 0 : HandleTabStopToken(const SwFormToken& aToken, const SwTxtNode& targetNode,
111 : const SwRootFrm *currentLayout) const SAL_OVERRIDE {
112 : (void)(aToken); (void)(targetNode); (void)(currentLayout); // avoid unused warnings.
113 0 : return HandledTabStopToken();
114 : }
115 : };
116 :
117 4 : class ToxTextGeneratorWithMockedChapterField : public ToxTextGenerator {
118 : public:
119 4 : ToxTextGeneratorWithMockedChapterField(SwForm &form)
120 : : ToxTextGenerator(form, boost::make_shared<MockedToxTabStopTokenHandler>()),
121 4 : mChapterFieldType(), mChapterField(&mChapterFieldType) {;}
122 :
123 : SwChapterField&
124 16 : GetChapterField() {
125 16 : return mChapterField;
126 : }
127 :
128 : private:
129 : SwChapterField
130 8 : ObtainChapterField(SwChapterFieldType* chapterFieldType, const SwFormToken* chapterToken,
131 : const SwCntntFrm* contentFrame, const SwCntntNode *contentNode) const SAL_OVERRIDE {
132 : // get rid of 'unused-parameters' warnings
133 : (void)(chapterFieldType);(void)(chapterToken);(void)(contentFrame);(void)(contentNode);
134 8 : return mChapterField;
135 : }
136 :
137 : SwChapterFieldType mChapterFieldType;
138 : SwChapterField mChapterField;
139 : };
140 :
141 : void
142 2 : ToxTextGeneratorTest::ChapterNumberWithoutTextIsGeneratedForNoprepstTitle()
143 : {
144 2 : SwForm form;
145 4 : ToxTextGeneratorWithMockedChapterField ttg(form);
146 : // set all values to make sure they are not used
147 2 : ttg.GetChapterField().sNumber = "1";
148 2 : ttg.GetChapterField().sPre = "PRE";
149 2 : ttg.GetChapterField().sPost = "POST";
150 2 : ttg.GetChapterField().sTitle = "TITLE";
151 :
152 4 : SwFormToken token(TOKEN_CHAPTER_INFO);
153 2 : token.nChapterFormat = CF_NUM_NOPREPST_TITLE;
154 :
155 4 : OUString expected("1");
156 4 : OUString actual = ttg.GenerateTextForChapterToken(token, NULL, NULL);
157 2 : CPPUNIT_ASSERT_EQUAL(expected, actual);
158 :
159 : // we cannot mock the pre- and suffix generation in the chapterfield. We just test that sNumber and
160 : // sTitle are used and hope that the pre- and suffix addition works.
161 2 : token.nChapterFormat = CF_NUMBER;
162 2 : expected = ttg.GenerateTextForChapterToken(token, NULL, NULL);
163 4 : CPPUNIT_ASSERT_EQUAL(expected, actual);
164 2 : }
165 :
166 :
167 : void
168 2 : ToxTextGeneratorTest::ChapterNumberWithTitleIsGeneratedForNumberNoPrepst()
169 : {
170 2 : SwForm form;
171 4 : ToxTextGeneratorWithMockedChapterField ttg(form);
172 : // set all values to make sure they are not used
173 2 : ttg.GetChapterField().sNumber = "5";
174 2 : ttg.GetChapterField().sPre = "PRE";
175 2 : ttg.GetChapterField().sPost = "POST";
176 2 : ttg.GetChapterField().sTitle = "myTitle";
177 :
178 4 : SwFormToken token(TOKEN_CHAPTER_INFO);
179 2 : token.nChapterFormat = CF_NUMBER_NOPREPST;
180 :
181 4 : OUString expected("5 myTitle");
182 4 : OUString actual = ttg.GenerateTextForChapterToken(token, NULL, NULL);
183 2 : CPPUNIT_ASSERT_EQUAL(expected, actual);
184 :
185 : // we cannot mock the pre- and suffix generation in the chapterfield. We just test that sNumber and
186 : // sTitle are used and hope that the pre- and suffix addition works.
187 2 : token.nChapterFormat = CF_NUM_TITLE;
188 2 : expected = ttg.GenerateTextForChapterToken(token, NULL, NULL);
189 4 : CPPUNIT_ASSERT_EQUAL(expected, actual);
190 2 : }
191 :
192 : // Put the test suite in the registry
193 6 : CPPUNIT_TEST_SUITE_REGISTRATION(ToxTextGeneratorTest);
194 :
195 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|