summaryrefslogtreecommitdiff
path: root/sw/qa/extras/indexing/IndexingExportTest.cxx
blob: f714b50f042a2fef959c12fbe68c6f95a473a7e6 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <memory>
#include <string_view>
#include <swmodeltestbase.hxx>
#include <docsh.hxx>
#include <unotxdoc.hxx>

#include <IndexingExport.hxx>

namespace
{
constexpr OUStringLiteral DATA_DIRECTORY = u"sw/qa/extras/indexing/data/";
}

class IndexingExportTest : public SwModelTestBase
{
private:
    SwDoc* createDoc(const char* pName = nullptr);

public:
    void testIndexingExport_Paragraphs();
    void testIndexingExport_Images();
    void testIndexingExport_OLE();
    void testIndexingExport_Shapes();
    void testIndexingExport_Tables();
    void testIndexingExport_Sections();

    CPPUNIT_TEST_SUITE(IndexingExportTest);
    CPPUNIT_TEST(testIndexingExport_Paragraphs);
    CPPUNIT_TEST(testIndexingExport_Images);
    CPPUNIT_TEST(testIndexingExport_OLE);
    CPPUNIT_TEST(testIndexingExport_Shapes);
    CPPUNIT_TEST(testIndexingExport_Tables);
    CPPUNIT_TEST(testIndexingExport_Sections);
    CPPUNIT_TEST_SUITE_END();
};

SwDoc* IndexingExportTest::createDoc(const char* pName)
{
    if (!pName)
        loadURL("private:factory/swriter", nullptr);
    else
        load(DATA_DIRECTORY, pName);

    SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());
    CPPUNIT_ASSERT(pTextDoc);
    return pTextDoc->GetDocShell()->GetDoc();
}

void IndexingExportTest::testIndexingExport_Paragraphs()
{
    SwDoc* pDoc = createDoc("IndexingExport_VariousParagraphs.odt");
    CPPUNIT_ASSERT(pDoc);

    SvMemoryStream aMemoryStream;
    sw::IndexingExport aIndexingExport(aMemoryStream, pDoc);
    aIndexingExport.runExport();
    aMemoryStream.Seek(0);

    xmlDocUniquePtr pXmlDoc = parseXmlStream(&aMemoryStream);
    CPPUNIT_ASSERT(pXmlDoc);

    assertXPath(pXmlDoc, "/indexing");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[1]", "Title");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[2]", "Heading 1");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[3]", "Heading 2");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[4]", "Paragraph 1");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[5]", "Paragraph 2");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[6]", "Bullet 1");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[7]", "Bullet 2");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[8]", "Bullet 3");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[9]", "Paragraph 3");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[10]", "Paragraph 4");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[11]", "List 1");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[12]", "List 2");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[13]", "List 3");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[14]", "Left");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[15]", "Center");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[16]", "Right");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[17]", "Bold Italic Underline Strikeout");
}

void IndexingExportTest::testIndexingExport_Images()
{
    SwDoc* pDoc = createDoc("IndexingExport_Images.odt");
    CPPUNIT_ASSERT(pDoc);

    SvMemoryStream aMemoryStream;
    sw::IndexingExport aIndexingExport(aMemoryStream, pDoc);
    aIndexingExport.runExport();
    aMemoryStream.Seek(0);

    xmlDocUniquePtr pXmlDoc = parseXmlStream(&aMemoryStream);
    CPPUNIT_ASSERT(pXmlDoc);

    assertXPath(pXmlDoc, "/indexing");
    assertXPath(pXmlDoc, "/indexing/graphic[1]", "alt", "Image_NonCaption - Alternative text");
    assertXPath(pXmlDoc, "/indexing/graphic[1]", "name", "Image_NonCaption");
    assertXPath(pXmlDoc, "/indexing/graphic[2]", "alt", "Image_InCaption - Alternative text");
    assertXPath(pXmlDoc, "/indexing/graphic[2]", "name", "Image_InCaption");
}

void IndexingExportTest::testIndexingExport_OLE()
{
    SwDoc* pDoc = createDoc("IndexingExport_OLE.odt");
    CPPUNIT_ASSERT(pDoc);

    SvMemoryStream aMemoryStream;
    sw::IndexingExport aIndexingExport(aMemoryStream, pDoc);
    aIndexingExport.runExport();
    aMemoryStream.Seek(0);

    xmlDocUniquePtr pXmlDoc = parseXmlStream(&aMemoryStream);
    CPPUNIT_ASSERT(pXmlDoc);

    assertXPath(pXmlDoc, "/indexing");
    assertXPath(pXmlDoc, "/indexing/ole[1]", "name", "Object - Chart");
    assertXPath(pXmlDoc, "/indexing/ole[1]", "alt", "Alt Text");
}

void IndexingExportTest::testIndexingExport_Shapes()
{
    SwDoc* pDoc = createDoc("IndexingExport_Shapes.odt");
    CPPUNIT_ASSERT(pDoc);

    SvMemoryStream aMemoryStream;
    sw::IndexingExport aIndexingExport(aMemoryStream, pDoc);
    aIndexingExport.runExport();
    aMemoryStream.Seek(0);

    xmlDocUniquePtr pXmlDoc = parseXmlStream(&aMemoryStream);
    CPPUNIT_ASSERT(pXmlDoc);

    assertXPath(pXmlDoc, "/indexing");

    assertXPath(pXmlDoc, "/indexing/shape[1]", "name", "Circle");
    assertXPathContent(pXmlDoc, "/indexing/shape[1]/paragraph[1]", "This is a circle");
    assertXPathContent(pXmlDoc, "/indexing/shape[1]/paragraph[2]", "This is a second paragraph");

    assertXPath(pXmlDoc, "/indexing/shape[2]", "name", "Diamond");
    assertXPathContent(pXmlDoc, "/indexing/shape[2]/paragraph[1]", "This is a diamond");

    assertXPath(pXmlDoc, "/indexing/shape[3]", "name", "Text Frame 1");
    assertXPathContent(pXmlDoc, "/indexing/shape[3]/paragraph[1]", "This is a TextBox - Para1");
    assertXPathContent(pXmlDoc, "/indexing/shape[3]/paragraph[2]", "Para2");
    assertXPathContent(pXmlDoc, "/indexing/shape[3]/paragraph[3]", "Para3");
}

void IndexingExportTest::testIndexingExport_Tables()
{
    SwDoc* pDoc = createDoc("IndexingExport_Tables.odt");
    CPPUNIT_ASSERT(pDoc);

    SvMemoryStream aMemoryStream;
    sw::IndexingExport aIndexingExport(aMemoryStream, pDoc);
    aIndexingExport.runExport();
    aMemoryStream.Seek(0);

    xmlDocUniquePtr pXmlDoc = parseXmlStream(&aMemoryStream);
    CPPUNIT_ASSERT(pXmlDoc);

    assertXPath(pXmlDoc, "/indexing");

    assertXPath(pXmlDoc, "/indexing/table[1]", "name", "Table1");
    assertXPathContent(pXmlDoc, "/indexing/table[1]/paragraph[1]", "A");
    assertXPathContent(pXmlDoc, "/indexing/table[1]/paragraph[2]", "B");
    assertXPathContent(pXmlDoc, "/indexing/table[1]/paragraph[3]", "1");
    assertXPathContent(pXmlDoc, "/indexing/table[1]/paragraph[4]", "2");

    assertXPath(pXmlDoc, "/indexing/table[2]", "name", "Table2");
    assertXPathContent(pXmlDoc, "/indexing/table[2]/paragraph[1]", "A");
    assertXPathContent(pXmlDoc, "/indexing/table[2]/paragraph[2]", "B");
    assertXPathContent(pXmlDoc, "/indexing/table[2]/paragraph[3]", "C");
    assertXPathContent(pXmlDoc, "/indexing/table[2]/paragraph[4]", "1");
    assertXPathContent(pXmlDoc, "/indexing/table[2]/paragraph[5]", "10");
    assertXPathContent(pXmlDoc, "/indexing/table[2]/paragraph[6]", "100");
    assertXPathContent(pXmlDoc, "/indexing/table[2]/paragraph[7]", "2");
    assertXPathContent(pXmlDoc, "/indexing/table[2]/paragraph[8]", "20");
    assertXPathContent(pXmlDoc, "/indexing/table[2]/paragraph[9]", "200");
    assertXPathContent(pXmlDoc, "/indexing/table[2]/paragraph[10]", "3");
    assertXPathContent(pXmlDoc, "/indexing/table[2]/paragraph[11]", "30");
    assertXPathContent(pXmlDoc, "/indexing/table[2]/paragraph[12]", "300");
    assertXPathContent(pXmlDoc, "/indexing/table[2]/paragraph[13]", "4");
    assertXPathContent(pXmlDoc, "/indexing/table[2]/paragraph[14]", "40");
    assertXPathContent(pXmlDoc, "/indexing/table[2]/paragraph[15]", "400");

    assertXPath(pXmlDoc, "/indexing/table[3]", "name", "WeirdTable");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[1]", "A1");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[2]", "B1");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[3]", "C1");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[4]", "D1");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[5]", "A2B2");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[6]", "C2D2");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[7]", "A3B3C3D3");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[8]", "A4-1");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[9]", "A4-2");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[10]", "B4-1");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[11]", "C4-1");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[12]", "D4-1");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[13]", "D4-2");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[14]", "");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[15]", "");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[16]", "B4-2");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[17]", "C4-2");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[18]", "");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[19]", "");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[20]", "A5B5C5");
    assertXPathContent(pXmlDoc, "/indexing/table[3]/paragraph[21]", "D5");
}

void IndexingExportTest::testIndexingExport_Sections()
{
    SwDoc* pDoc = createDoc("IndexingExport_Sections.odt");
    CPPUNIT_ASSERT(pDoc);

    SvMemoryStream aMemoryStream;
    sw::IndexingExport aIndexingExport(aMemoryStream, pDoc);
    aIndexingExport.runExport();
    aMemoryStream.Seek(0);

    xmlDocUniquePtr pXmlDoc = parseXmlStream(&aMemoryStream);
    CPPUNIT_ASSERT(pXmlDoc);

    assertXPath(pXmlDoc, "/indexing");

    assertXPath(pXmlDoc, "/indexing/section[1]", "name", "Section1");
    assertXPathContent(pXmlDoc, "/indexing/section[1]/paragraph[1]",
                       "This is a paragraph in a Section1");
    assertXPathContent(pXmlDoc, "/indexing/section[1]/paragraph[2]", "Section1 - Paragraph 2");
    assertXPathContent(pXmlDoc, "/indexing/section[1]/paragraph[3]", "Section1 - Paragraph 3");

    assertXPath(pXmlDoc, "/indexing/section[2]", "name", "Section2");
    assertXPathContent(pXmlDoc, "/indexing/section[2]/paragraph[1]", "Section2 - Paragraph 1");
    assertXPathContent(pXmlDoc, "/indexing/section[2]/paragraph[2]", "Section2 - Paragraph 2");

    assertXPathContent(pXmlDoc, "/indexing/paragraph[1]", "This is a paragraph outside sections");
    assertXPathContent(pXmlDoc, "/indexing/paragraph[2]", "This is a paragraph outside sections");
}

CPPUNIT_TEST_SUITE_REGISTRATION(IndexingExportTest);
CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */