summaryrefslogtreecommitdiffstats
path: root/writerfilter/qa/cppunittests/rtftok/rtfdocumentimpl.cxx
blob: f33f0f0e58be9cc5a0e7369533a5ccc9a24b6201 (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
/* -*- 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 <test/bootstrapfixture.hxx>
#include <unotest/macros_test.hxx>

#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/drawing/XDrawPageSupplier.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/graphic/XGraphic.hpp>
#include <com/sun/star/text/XTextTablesSupplier.hpp>
#include <com/sun/star/text/XTextTable.hpp>
#include <com/sun/star/text/XTextDocument.hpp>

#include <vcl/graph.hxx>

using namespace ::com::sun::star;

namespace
{
/// Tests for writerfilter/source/rtftok/rtfdocumentimpl.cxx.
class Test : public test::BootstrapFixture, public unotest::MacrosTest
{
private:
    uno::Reference<lang::XComponent> mxComponent;

public:
    void setUp() override;
    void tearDown() override;
    uno::Reference<lang::XComponent>& getComponent() { return mxComponent; }
};

void Test::setUp()
{
    test::BootstrapFixture::setUp();

    mxDesktop.set(frame::Desktop::create(mxComponentContext));
}

void Test::tearDown()
{
    if (mxComponent.is())
        mxComponent->dispose();

    test::BootstrapFixture::tearDown();
}

constexpr OUStringLiteral DATA_DIRECTORY = u"/writerfilter/qa/cppunittests/rtftok/data/";

CPPUNIT_TEST_FIXTURE(Test, testPicwPich)
{
    // Given a document with a WMF file where picwgoal and picscalex is provided, so picw is not
    // relevant:
    OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "picw-pich.rtf";

    // When loading that document:
    getComponent() = loadFromDesktop(aURL);

    // Then make sure the graphic's preferred size is correct:
    uno::Reference<drawing::XDrawPageSupplier> xTextDocument(getComponent(), uno::UNO_QUERY);
    uno::Reference<drawing::XDrawPage> xDrawPage = xTextDocument->getDrawPage();
    uno::Reference<beans::XPropertySet> xShape(xDrawPage->getByIndex(0), uno::UNO_QUERY);
    uno::Reference<graphic::XGraphic> xGraphic;
    xShape->getPropertyValue("Graphic") >>= xGraphic;
    Graphic aGraphic(xGraphic);
    Size aPrefSize = aGraphic.GetPrefSize();
    // Without the accompanying fix in place, this test would have failed with:
    // - Expected: 2619
    // - Actual  : 132
    // i.e. the graphic width didn't match 2.62 cm from the Word UI.
    CPPUNIT_ASSERT_EQUAL(static_cast<tools::Long>(2619), aPrefSize.Width());
}

CPPUNIT_TEST_FIXTURE(Test, testCharHiddenInTable)
{
    // Given a document with a table, and a hidden \line in it:
    OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "char-hidden-intbl.rtf";

    // When loading that document:
    getComponent() = loadFromDesktop(aURL);

    // Then make sure that line is indeed hidden:
    uno::Reference<text::XTextTablesSupplier> xTextDocument(getComponent(), uno::UNO_QUERY);
    uno::Reference<container::XIndexAccess> xTables(xTextDocument->getTextTables(), uno::UNO_QUERY);
    uno::Reference<text::XTextTable> xTable(xTables->getByIndex(0), uno::UNO_QUERY);
    uno::Reference<container::XEnumerationAccess> xCell(xTable->getCellByName("B1"),
                                                        uno::UNO_QUERY);
    uno::Reference<container::XEnumeration> xParagraphs = xCell->createEnumeration();
    uno::Reference<container::XEnumerationAccess> xParagraph(xParagraphs->nextElement(),
                                                             uno::UNO_QUERY);
    uno::Reference<container::XEnumeration> xPortions = xParagraph->createEnumeration();
    uno::Reference<beans::XPropertySet> xPortion(xPortions->nextElement(), uno::UNO_QUERY);
    bool bCharHidden{};
    xPortion->getPropertyValue("CharHidden") >>= bCharHidden;
    // Without the accompanying fix in place, this test would have failed, the newline was not
    // hidden.
    CPPUNIT_ASSERT(bCharHidden);
}

CPPUNIT_TEST_FIXTURE(Test, testDuplicatedImage)
{
    // Given a document with 2 images:
    OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "duplicated-image.rtf";

    // When importing that document:
    getComponent() = loadFromDesktop(aURL);

    // Then make sure no duplicated images are created:
    uno::Reference<drawing::XDrawPageSupplier> xTextDocument(getComponent(), uno::UNO_QUERY);
    uno::Reference<drawing::XDrawPage> xDrawPage = xTextDocument->getDrawPage();
    // Without the accompanying fix in place, this test would have failed with:
    // - Expected: 2
    // - Actual  : 3
    // i.e. there was a 3rd, duplicated image.
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), xDrawPage->getCount());
}

CPPUNIT_TEST_FIXTURE(Test, testOldParaNumLeftMargin)
{
    // Given a document with 3 paragraphs, the third one with a left indent:
    OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "old-para-num-left-margin.rtf";

    // When importing that document:
    getComponent() = loadFromDesktop(aURL);

    // Then make sure that the third paragraph has a left indent:
    uno::Reference<text::XTextDocument> xTextDocument(getComponent(), uno::UNO_QUERY);
    uno::Reference<container::XEnumerationAccess> xText(xTextDocument->getText(), uno::UNO_QUERY);
    uno::Reference<container::XEnumeration> xParagraphs = xText->createEnumeration();
    xParagraphs->nextElement();
    xParagraphs->nextElement();
    uno::Reference<beans::XPropertySet> xParagraph(xParagraphs->nextElement(), uno::UNO_QUERY);
    sal_Int32 nParaLeftMargin{};
    xParagraph->getPropertyValue("ParaLeftMargin") >>= nParaLeftMargin;
    // Without the accompanying fix in place, this test would have failed with:
    // - Expected: 2101
    // - Actual  : 0
    // i.e. the left indent was 0, not 1191 twips (from the file) in mm100.
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2101), nParaLeftMargin);
}
}

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