summaryrefslogtreecommitdiffstats
path: root/sd/qa/unit/sdmodeltestbase.hxx
blob: 4f0d15c56474e8e980c5bce7643a4c2e07d50b1a (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
/* -*- 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/.
 */

#pragma once

#include <memory>
#include <string_view>

#include <test/unoapixml_test.hxx>
#include <test/xmldiff.hxx>

#include <drawdoc.hxx>
#include <DrawDocShell.hxx>
#include <GraphicDocShell.hxx>
#include <unotools/tempfile.hxx>
#include <unotools/ucbstreamhelper.hxx>
#include <tools/color.hxx>
#include <comphelper/fileformat.h>
#include <comphelper/processfactory.hxx>
#include <o3tl/safeint.hxx>
#include <rtl/strbuf.hxx>
#include <sfx2/docfile.hxx>
#include <sfx2/docfilt.hxx>
#include <svl/itemset.hxx>
#include <unomodel.hxx>

#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
#include <com/sun/star/packages/zip/ZipFileAccess.hpp>
#include <drawinglayer/XShapeDumper.hxx>
#include <com/sun/star/text/XTextField.hpp>

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

class SdModelTestBase : public UnoApiXmlTest
{
public:
    SdModelTestBase(OUString path)
        : UnoApiXmlTest(path)
    {
    }

    void createSdImpressDoc(const char* pName = nullptr, const char* pPassword = nullptr)
    {
        if (!pName)
            load("private:factory/simpress");
        else
            loadFromFile(OUString::createFromAscii(pName), pPassword);

        uno::Reference<lang::XServiceInfo> xServiceInfo(mxComponent, uno::UNO_QUERY_THROW);
        CPPUNIT_ASSERT(
            xServiceInfo->supportsService("com.sun.star.presentation.PresentationDocument"));

        CPPUNIT_ASSERT(!getSdDocShell()->GetMedium()->GetWarningError());
    }

    void createSdDrawDoc(const char* pName = nullptr, const char* pPassword = nullptr)
    {
        if (!pName)
            load("private:factory/sdraw");
        else
            loadFromFile(OUString::createFromAscii(pName), pPassword);

        uno::Reference<lang::XServiceInfo> xServiceInfo(mxComponent, uno::UNO_QUERY_THROW);
        CPPUNIT_ASSERT(xServiceInfo->supportsService("com.sun.star.drawing.DrawingDocument"));

        CPPUNIT_ASSERT(!getSdDocShell()->GetMedium()->GetWarningError());
    }

    sd::DrawDocShell* getSdDocShell()
    {
        SdXImpressDocument* pImpressDocument = dynamic_cast<SdXImpressDocument*>(mxComponent.get());
        CPPUNIT_ASSERT(pImpressDocument);
        return pImpressDocument->GetDocShell();
    }

    uno::Reference<drawing::XDrawPage> getPage(int nPage)
    {
        uno::Reference<drawing::XDrawPagesSupplier> xDoc(mxComponent, uno::UNO_QUERY);
        CPPUNIT_ASSERT(xDoc.is());
        uno::Reference<drawing::XDrawPage> xPage(xDoc->getDrawPages()->getByIndex(nPage),
                                                 uno::UNO_QUERY_THROW);
        return xPage;
    }

    uno::Reference<beans::XPropertySet> getShapeFromPage(int nShape, int nPage)
    {
        uno::Reference<drawing::XDrawPage> xPage(getPage(nPage));
        uno::Reference<beans::XPropertySet> xShape(getShape(nShape, xPage));
        CPPUNIT_ASSERT_MESSAGE("Failed to load shape", xShape.is());

        return xShape;
    }

    // very confusing ... UNO index-based access to pages is 0-based. This one is 1-based
    const SdrPage* GetPage(int nPage)
    {
        SdXImpressDocument* pXImpressDocument
            = dynamic_cast<SdXImpressDocument*>(mxComponent.get());
        CPPUNIT_ASSERT(pXImpressDocument);
        SdDrawDocument* pDoc = pXImpressDocument->GetDoc();
        CPPUNIT_ASSERT_MESSAGE("no document", pDoc != nullptr);

        const SdrPage* pPage = pDoc->GetPage(nPage);
        CPPUNIT_ASSERT_MESSAGE("no page", pPage != nullptr);
        return pPage;
    }

    uno::Reference<beans::XPropertySet> getShape(int nShape,
                                                 uno::Reference<drawing::XDrawPage> const& xPage)
    {
        uno::Reference<beans::XPropertySet> xShape(xPage->getByIndex(nShape), uno::UNO_QUERY);
        CPPUNIT_ASSERT_MESSAGE("Failed to load shape", xShape.is());
        return xShape;
    }

    uno::Reference<text::XTextRange>
    getParagraphFromShape(int nPara, uno::Reference<beans::XPropertySet> const& xShape)
    {
        uno::Reference<text::XText> xText
            = uno::Reference<text::XTextRange>(xShape, uno::UNO_QUERY_THROW)->getText();
        CPPUNIT_ASSERT_MESSAGE("Not a text shape", xText.is());

        uno::Reference<container::XEnumerationAccess> paraEnumAccess(xText, uno::UNO_QUERY);
        uno::Reference<container::XEnumeration> paraEnum(paraEnumAccess->createEnumeration());

        for (int i = 0; i < nPara; ++i)
            paraEnum->nextElement();

        uno::Reference<text::XTextRange> xParagraph(paraEnum->nextElement(), uno::UNO_QUERY_THROW);

        return xParagraph;
    }

    uno::Reference<text::XTextRange>
    getRunFromParagraph(int nRun, uno::Reference<text::XTextRange> const& xParagraph)
    {
        uno::Reference<container::XEnumerationAccess> runEnumAccess(xParagraph, uno::UNO_QUERY);
        uno::Reference<container::XEnumeration> runEnum = runEnumAccess->createEnumeration();

        for (int i = 0; i < nRun; ++i)
            runEnum->nextElement();

        uno::Reference<text::XTextRange> xRun(runEnum->nextElement(), uno::UNO_QUERY);

        return xRun;
    }

    uno::Reference<text::XTextField> getTextFieldFromPage(int nRun, int nPara, int nShape,
                                                          int nPage)
    {
        // get TextShape 1 from the first page
        uno::Reference<beans::XPropertySet> xShape(getShapeFromPage(nShape, nPage));

        // Get first paragraph
        uno::Reference<text::XTextRange> xParagraph(getParagraphFromShape(nPara, xShape));

        // first chunk of text
        uno::Reference<text::XTextRange> xRun(getRunFromParagraph(nRun, xParagraph));

        uno::Reference<beans::XPropertySet> xPropSet(xRun, uno::UNO_QUERY_THROW);

        uno::Reference<text::XTextField> xField;
        xPropSet->getPropertyValue("TextField") >>= xField;
        return xField;
    }
};

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