tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / sd / qa / unit / sdmodeltestbase.hxx
blob1cd4fc626342ed18defaf3ae138c83a5f4d8a46e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #pragma once
12 #include <memory>
13 #include <string_view>
15 #include <test/unoapixml_test.hxx>
16 #include <test/xmldiff.hxx>
18 #include <drawdoc.hxx>
19 #include <DrawDocShell.hxx>
20 #include <GraphicDocShell.hxx>
21 #include <unotools/tempfile.hxx>
22 #include <unotools/ucbstreamhelper.hxx>
23 #include <tools/color.hxx>
24 #include <comphelper/fileformat.h>
25 #include <comphelper/processfactory.hxx>
26 #include <o3tl/safeint.hxx>
27 #include <rtl/strbuf.hxx>
28 #include <sfx2/docfile.hxx>
29 #include <sfx2/docfilt.hxx>
30 #include <svl/itemset.hxx>
31 #include <unomodel.hxx>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
35 #include <com/sun/star/packages/zip/ZipFileAccess.hpp>
36 #include <drawinglayer/XShapeDumper.hxx>
37 #include <com/sun/star/text/XTextField.hpp>
39 using namespace ::com::sun::star;
41 class SdModelTestBase : public UnoApiXmlTest
43 public:
44 SdModelTestBase(const OUString& path)
45 : UnoApiXmlTest(path)
49 void createSdImpressDoc(const char* pName = nullptr, const char* pPassword = nullptr)
51 if (!pName)
52 loadFromURL(u"private:factory/simpress"_ustr);
53 else
54 loadFromFile(OUString::createFromAscii(pName), pPassword);
56 uno::Reference<lang::XServiceInfo> xServiceInfo(mxComponent, uno::UNO_QUERY_THROW);
57 CPPUNIT_ASSERT(
58 xServiceInfo->supportsService(u"com.sun.star.presentation.PresentationDocument"_ustr));
60 CPPUNIT_ASSERT(!getSdDocShell()->GetMedium()->GetWarningError());
63 void createSdDrawDoc(const char* pName = nullptr, const char* pPassword = nullptr)
65 if (!pName)
66 loadFromURL(u"private:factory/sdraw"_ustr);
67 else
68 loadFromFile(OUString::createFromAscii(pName), pPassword);
70 uno::Reference<lang::XServiceInfo> xServiceInfo(mxComponent, uno::UNO_QUERY_THROW);
71 CPPUNIT_ASSERT(xServiceInfo->supportsService(u"com.sun.star.drawing.DrawingDocument"_ustr));
73 CPPUNIT_ASSERT(!getSdDocShell()->GetMedium()->GetWarningError());
76 sd::DrawDocShell* getSdDocShell()
78 SdXImpressDocument* pImpressDocument = dynamic_cast<SdXImpressDocument*>(mxComponent.get());
79 CPPUNIT_ASSERT(pImpressDocument);
80 return pImpressDocument->GetDocShell();
83 uno::Reference<drawing::XDrawPage> getPage(int nPage)
85 uno::Reference<drawing::XDrawPagesSupplier> xDoc(mxComponent, uno::UNO_QUERY);
86 CPPUNIT_ASSERT(xDoc.is());
87 uno::Reference<drawing::XDrawPage> xPage(xDoc->getDrawPages()->getByIndex(nPage),
88 uno::UNO_QUERY_THROW);
89 return xPage;
92 uno::Reference<beans::XPropertySet> getShapeFromPage(int nShape, int nPage)
94 uno::Reference<drawing::XDrawPage> xPage(getPage(nPage));
95 uno::Reference<beans::XPropertySet> xShape(getShape(nShape, xPage));
96 CPPUNIT_ASSERT_MESSAGE("Failed to load shape", xShape.is());
98 return xShape;
101 // very confusing ... UNO index-based access to pages is 0-based. This one is 1-based
102 const SdrPage* GetPage(int nPage)
104 SdXImpressDocument* pXImpressDocument
105 = dynamic_cast<SdXImpressDocument*>(mxComponent.get());
106 CPPUNIT_ASSERT(pXImpressDocument);
107 SdDrawDocument* pDoc = pXImpressDocument->GetDoc();
108 CPPUNIT_ASSERT_MESSAGE("no document", pDoc != nullptr);
110 const SdrPage* pPage = pDoc->GetPage(nPage);
111 CPPUNIT_ASSERT_MESSAGE("no page", pPage != nullptr);
112 return pPage;
115 uno::Reference<beans::XPropertySet> getShape(int nShape,
116 uno::Reference<drawing::XDrawPage> const& xPage)
118 uno::Reference<beans::XPropertySet> xShape(xPage->getByIndex(nShape), uno::UNO_QUERY);
119 CPPUNIT_ASSERT_MESSAGE("Failed to load shape", xShape.is());
120 return xShape;
123 uno::Reference<text::XTextRange>
124 getParagraphFromShape(int nPara, uno::Reference<beans::XPropertySet> const& xShape)
126 uno::Reference<text::XText> xText
127 = uno::Reference<text::XTextRange>(xShape, uno::UNO_QUERY_THROW)->getText();
128 CPPUNIT_ASSERT_MESSAGE("Not a text shape", xText.is());
130 uno::Reference<container::XEnumerationAccess> paraEnumAccess(xText, uno::UNO_QUERY);
131 uno::Reference<container::XEnumeration> paraEnum(paraEnumAccess->createEnumeration());
133 for (int i = 0; i < nPara; ++i)
134 paraEnum->nextElement();
136 uno::Reference<text::XTextRange> xParagraph(paraEnum->nextElement(), uno::UNO_QUERY_THROW);
138 return xParagraph;
141 uno::Reference<text::XTextRange>
142 getRunFromParagraph(int nRun, uno::Reference<text::XTextRange> const& xParagraph)
144 uno::Reference<container::XEnumerationAccess> runEnumAccess(xParagraph, uno::UNO_QUERY);
145 uno::Reference<container::XEnumeration> runEnum = runEnumAccess->createEnumeration();
147 for (int i = 0; i < nRun; ++i)
148 runEnum->nextElement();
150 uno::Reference<text::XTextRange> xRun(runEnum->nextElement(), uno::UNO_QUERY);
152 return xRun;
155 uno::Reference<text::XTextField> getTextFieldFromPage(int nRun, int nPara, int nShape,
156 int nPage)
158 // get TextShape 1 from the first page
159 uno::Reference<beans::XPropertySet> xShape(getShapeFromPage(nShape, nPage));
161 // Get first paragraph
162 uno::Reference<text::XTextRange> xParagraph(getParagraphFromShape(nPara, xShape));
164 // first chunk of text
165 uno::Reference<text::XTextRange> xRun(getRunFromParagraph(nRun, xParagraph));
167 uno::Reference<beans::XPropertySet> xPropSet(xRun, uno::UNO_QUERY_THROW);
169 uno::Reference<text::XTextField> xField;
170 xPropSet->getPropertyValue(u"TextField"_ustr) >>= xField;
171 return xField;
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */