fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sd / qa / unit / sdmodeltestbase.hxx
blob037ba3ea5950647f7904715d209f4b9949c0b8ff
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 #ifndef INCLUDED_SD_QA_UNIT_SDMODELTESTBASE_HXX
11 #define INCLUDED_SD_QA_UNIT_SDMODELTESTBASE_HXX
13 #include <test/bootstrapfixture.hxx>
14 #include <test/xmldiff.hxx>
16 #include <unotest/filters-test.hxx>
17 #include <unotest/macros_test.hxx>
19 #include "drawdoc.hxx"
20 #include "../source/ui/inc/DrawDocShell.hxx"
21 #include "unotools/tempfile.hxx"
23 #include <rtl/strbuf.hxx>
24 #include <sfx2/docfile.hxx>
25 #include <sfx2/docfilt.hxx>
26 #include <svl/itemset.hxx>
28 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
29 #include <drawinglayer/XShapeDumper.hxx>
31 using namespace ::com::sun::star;
33 struct FileFormat
35 const char* pName;
36 const char* pFilterName;
37 const char* pTypeName;
38 const char* pUserData;
39 SfxFilterFlags nFormatType;
42 // These values are taken from "Flags" in filter/source/config/fragments/filters/*
43 // You need to turn value of oor:name="Flags" to SfxFilterFlags::*, see
44 // include/comphelper/documentconstants.hxx for the possible values.
45 // Note: 3RDPARTYFILTER == SfxFilterFlags::STARONEFILTER
46 #define ODP_FORMAT_TYPE ( SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::TEMPLATE | SfxFilterFlags::OWN | SfxFilterFlags::DEFAULT | SfxFilterFlags::ENCRYPTION | SfxFilterFlags::PREFERED )
47 #define PPT_FORMAT_TYPE ( SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN )
48 #define PPTX_FORMAT_TYPE ( SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN | SfxFilterFlags::STARONEFILTER | SfxFilterFlags::PREFERED )
49 #define HTML_FORMAT_TYPE ( SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN )
50 #define PDF_FORMAT_TYPE ( SfxFilterFlags::STARONEFILTER | SfxFilterFlags::ALIEN | SfxFilterFlags::IMPORT | SfxFilterFlags::PREFERED )
51 #define FODG_FORMAT_TYPE (SfxFilterFlags::STARONEFILTER | SfxFilterFlags::OWN | SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT)
53 /** List of file formats we support in Impress unit tests.
55 Taken from filter/source/config/fragments/filters/ too:
56 pName: The file extension.
57 pFilterName: <node oor:Name="...">
58 pTypeName: <prop oor:Name="UIName">...</prop>
59 nFormatType: <prop oor:name="Flags">...</prop>
61 FileFormat aFileFormats[] =
63 { "odp", "impress8", "impress8", "", ODP_FORMAT_TYPE },
64 { "ppt", "MS PowerPoint 97", "Microsoft PowerPoint 97/2000/XP/2003", "sdfilt", PPT_FORMAT_TYPE },
65 { "pptx", "Impress Office Open XML", "Office Open XML Presentation", "", PPTX_FORMAT_TYPE },
66 { "html", "graphic_HTML", "graphic_HTML", "", HTML_FORMAT_TYPE },
67 { "pdf", "draw_pdf_import", "pdf_Portable_Document_Format", "", PDF_FORMAT_TYPE },
68 { "fodg", "OpenDocument Drawing Flat XML", "Flat XML ODF Drawing", "", FODG_FORMAT_TYPE },
69 { 0, 0, 0, 0, SfxFilterFlags::NONE }
72 #define ODP 0
73 #define PPT 1
74 #define PPTX 2
75 #define HTML 3
76 #define PDF 4
77 #define FODG 5
79 /// Base class for filter tests loading or roundtriping a document, and asserting the document model.
80 class SdModelTestBase : public test::BootstrapFixture, public unotest::MacrosTest
82 private:
83 uno::Reference<uno::XInterface> mxDrawComponent;
85 public:
86 SdModelTestBase()
89 virtual void setUp() SAL_OVERRIDE
91 test::BootstrapFixture::setUp();
93 // This is a bit of a fudge, we do this to ensure that ScGlobals::ensure,
94 // which is a private symbol to us, gets called
95 mxDrawComponent = getMultiServiceFactory()->createInstance("com.sun.star.comp.Draw.PresentationDocument");
96 CPPUNIT_ASSERT_MESSAGE("no impress component!", mxDrawComponent.is());
99 virtual void tearDown() SAL_OVERRIDE
101 uno::Reference<lang::XComponent>(mxDrawComponent, uno::UNO_QUERY_THROW)->dispose();
102 test::BootstrapFixture::tearDown();
105 protected:
106 /// Load the document.
107 sd::DrawDocShellRef loadURL( const OUString &rURL, sal_Int32 nFormat, SfxAllItemSet *pParams = 0 )
109 FileFormat *pFmt = getFormat(nFormat);
110 CPPUNIT_ASSERT_MESSAGE( "missing filter info", pFmt->pName != NULL );
112 SotClipboardFormatId nOptions = SotClipboardFormatId::NONE;
113 if (pFmt->nFormatType != SfxFilterFlags::NONE)
114 nOptions = SotClipboardFormatId::STARCALC_8;
115 SfxFilter* aFilter = new SfxFilter(
116 OUString::createFromAscii( pFmt->pFilterName ),
117 OUString(), pFmt->nFormatType, nOptions,
118 OUString::createFromAscii( pFmt->pTypeName ),
119 0, OUString(),
120 OUString::createFromAscii( pFmt->pUserData ),
121 OUString("private:factory/simpress*") );
122 aFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
124 ::sd::DrawDocShellRef xDocShRef = new ::sd::DrawDocShell();
125 SfxMedium* pSrcMed = new SfxMedium(rURL, STREAM_STD_READ, aFilter, pParams);
126 if ( !xDocShRef->DoLoad(pSrcMed) || !xDocShRef.Is() )
128 if (xDocShRef.Is())
129 xDocShRef->DoClose();
130 CPPUNIT_ASSERT_MESSAGE( OUStringToOString( "failed to load " + rURL, RTL_TEXTENCODING_UTF8 ).getStr(), false );
132 CPPUNIT_ASSERT_MESSAGE( "not in destruction", !xDocShRef->IsInDestruction() );
134 return xDocShRef;
137 FileFormat* getFormat(sal_Int32 nExportType)
139 FileFormat* pFormat = &aFileFormats[0];
140 if (((sal_uInt32) nExportType) < SAL_N_ELEMENTS(aFileFormats))
141 pFormat = &aFileFormats[nExportType];
142 return pFormat;
145 void exportTo(sd::DrawDocShell* pShell, FileFormat* pFormat, utl::TempFile& rTempFile)
147 SfxMedium aStoreMedium(rTempFile.GetURL(), STREAM_STD_WRITE);
148 SotClipboardFormatId nExportFormat = SotClipboardFormatId::NONE;
149 if (pFormat->nFormatType == ODP_FORMAT_TYPE)
150 nExportFormat = SotClipboardFormatId::STARCALC_8;
151 SfxFilter* pExportFilter = new SfxFilter(
152 OUString::createFromAscii(pFormat->pFilterName),
153 OUString(), pFormat->nFormatType, nExportFormat,
154 OUString::createFromAscii(pFormat->pTypeName),
155 0, OUString(),
156 OUString::createFromAscii(pFormat->pUserData),
157 OUString("private:factory/simpress*") );
158 pExportFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
159 aStoreMedium.SetFilter(pExportFilter);
160 pShell->ConvertTo(aStoreMedium);
161 pShell->DoClose();
164 void save(sd::DrawDocShell* pShell, FileFormat* pFormat, utl::TempFile& rTempFile)
166 SfxMedium aStoreMedium(rTempFile.GetURL(), STREAM_STD_WRITE);
167 SotClipboardFormatId nExportFormat = SotClipboardFormatId::NONE;
168 if (pFormat->nFormatType == ODP_FORMAT_TYPE)
169 nExportFormat = SotClipboardFormatId::STARCHART_8;
170 SfxFilter* pExportFilter = new SfxFilter(
171 OUString::createFromAscii(pFormat->pFilterName),
172 OUString(), pFormat->nFormatType, nExportFormat,
173 OUString::createFromAscii(pFormat->pTypeName),
174 0, OUString(),
175 OUString::createFromAscii(pFormat->pUserData),
176 OUString("private:factory/simpress*") );
177 pExportFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
178 aStoreMedium.SetFilter(pExportFilter);
179 pShell->DoSaveAs(aStoreMedium);
180 pShell->DoClose();
183 sd::DrawDocShellRef saveAndReload(sd::DrawDocShell *pShell, sal_Int32 nExportType)
185 FileFormat* pFormat = getFormat(nExportType);
186 utl::TempFile aTempFile;
187 save(pShell, pFormat, aTempFile);
188 if(nExportType == ODP)
190 // BootstrapFixture::validate(aTempFile.GetFileName(), test::ODF);
192 else if(nExportType == PPTX)
194 BootstrapFixture::validate(aTempFile.GetFileName(), test::OOXML);
196 aTempFile.EnableKillingFile();
197 return loadURL(aTempFile.GetURL(), nExportType);
200 /** Dump shapes in xDocShRef, and compare the dump against content of pShapesDumpFileNameBase<number>.xml.
202 @param bCreate Instead of comparing to the reference file(s), create it/them.
204 void compareWithShapesDump( ::sd::DrawDocShellRef xDocShRef, const OUString &rShapesDumpFileNameBase, bool bCreate = false )
206 CPPUNIT_ASSERT_MESSAGE( "failed to load", xDocShRef.Is() );
207 CPPUNIT_ASSERT_MESSAGE( "not in destruction", !xDocShRef->IsInDestruction() );
209 uno::Reference<frame::XModel> xTempModel(xDocShRef->GetDoc()->getUnoModel(), uno::UNO_QUERY_THROW);
210 CPPUNIT_ASSERT(xTempModel.is());
211 uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSupplier (xTempModel, uno::UNO_QUERY_THROW);
212 CPPUNIT_ASSERT(xDrawPagesSupplier.is());
213 uno::Reference< drawing::XDrawPages > xDrawPages = xDrawPagesSupplier->getDrawPages();
214 CPPUNIT_ASSERT(xDrawPages.is());
216 XShapeDumper xShapeDumper;
217 sal_Int32 nLength = xDrawPages->getCount();
218 for (sal_Int32 i = 0; i < nLength; ++i)
220 uno::Reference<drawing::XDrawPage> xDrawPage;
221 uno::Any aAny = xDrawPages->getByIndex(i);
222 aAny >>= xDrawPage;
223 uno::Reference< drawing::XShapes > xShapes(xDrawPage, uno::UNO_QUERY_THROW);
224 OUString aString = XShapeDumper::dump(xShapes);
226 OStringBuffer aFileNameBuf( OUStringToOString( rShapesDumpFileNameBase, RTL_TEXTENCODING_UTF8 ) );
227 aFileNameBuf.append(i);
228 aFileNameBuf.append(".xml");
230 OString aFileName = aFileNameBuf.makeStringAndClear();
232 if ( bCreate )
234 std::ofstream aStream( aFileName.getStr(), std::ofstream::out );
235 aStream << aString;
236 aStream.close();
238 else
240 doXMLDiff(aFileName.getStr(),
241 OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr(),
242 static_cast<int>(aString.getLength()),
243 OUStringToOString(
244 getPathFromSrc("/sd/qa/unit/data/tolerance.xml"),
245 RTL_TEXTENCODING_UTF8).getStr());
248 xDocShRef->DoClose();
252 #endif
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */