Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #ifndef INCLUDED_SD_QA_UNIT_SDMODELTESTBASE_HXX
11 : #define INCLUDED_SD_QA_UNIT_SDMODELTESTBASE_HXX
12 :
13 : #include <test/bootstrapfixture.hxx>
14 : #include <test/xmldiff.hxx>
15 :
16 : #include <unotest/filters-test.hxx>
17 : #include <unotest/macros_test.hxx>
18 :
19 : #include "drawdoc.hxx"
20 : #include "../source/ui/inc/DrawDocShell.hxx"
21 : #include "unotools/tempfile.hxx"
22 :
23 : #include <rtl/strbuf.hxx>
24 : #include <sfx2/docfile.hxx>
25 : #include <sfx2/docfilt.hxx>
26 :
27 : #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
28 : #include <drawinglayer/XShapeDumper.hxx>
29 :
30 : using namespace ::com::sun::star;
31 :
32 : struct FileFormat
33 : {
34 : const char* pName;
35 : const char* pFilterName;
36 : const char* pTypeName;
37 : const char* pUserData;
38 : sal_uLong nFormatType;
39 : };
40 :
41 : // These values are taken from "Flags" in filter/source/config/fragments/filters/*
42 : #define ODP_FORMAT_TYPE ( SFX_FILTER_IMPORT | SFX_FILTER_EXPORT | SFX_FILTER_TEMPLATE | SFX_FILTER_OWN | SFX_FILTER_DEFAULT | SFX_FILTER_ENCRYPTION | SFX_FILTER_PREFERED )
43 : #define PPT_FORMAT_TYPE ( SFX_FILTER_IMPORT | SFX_FILTER_EXPORT | SFX_FILTER_ALIEN )
44 : #define PPTX_FORMAT_TYPE ( SFX_FILTER_IMPORT | SFX_FILTER_EXPORT | SFX_FILTER_ALIEN | SFX_FILTER_STARONEFILTER | SFX_FILTER_PREFERED )
45 : #define HTML_FORMAT_TYPE ( SFX_FILTER_EXPORT | SFX_FILTER_ALIEN )
46 :
47 : /** List of file formats we support in Impress unit tests.
48 :
49 : Taken from filter/source/config/fragments/filters/ too:
50 : pName: The file extension.
51 : pFilterName: <node oor:Name="...">
52 : pTypeName: <prop oor:Name="UIName">...</prop>
53 : nFormatType: <prop oor:name="Flags">...</prop>
54 : */
55 : FileFormat aFileFormats[] =
56 : {
57 : { "odp", "impress8", "impress8", "", ODP_FORMAT_TYPE },
58 : { "ppt", "MS PowerPoint 97", "Microsoft PowerPoint 97/2000/XP/2003", "sdfilt", PPT_FORMAT_TYPE },
59 : { "pptx", "Impress Office Open XML", "Office Open XML Presentation", "", PPTX_FORMAT_TYPE },
60 : { "html", "graphic_HTML", "graphic_HTML", "", HTML_FORMAT_TYPE },
61 : { 0, 0, 0, 0, 0 }
62 : };
63 :
64 : #define ODP 0
65 : #define PPT 1
66 : #define PPTX 2
67 : #define HTML 3
68 :
69 : /// Base class for filter tests loading or roundtriping a document, and asserting the document model.
70 76 : class SdModelTestBase : public test::BootstrapFixture, public unotest::MacrosTest
71 : {
72 : private:
73 : uno::Reference<uno::XInterface> mxDrawComponent;
74 :
75 : public:
76 76 : SdModelTestBase()
77 76 : {}
78 :
79 76 : virtual void setUp() SAL_OVERRIDE
80 : {
81 76 : test::BootstrapFixture::setUp();
82 :
83 : // This is a bit of a fudge, we do this to ensure that ScGlobals::ensure,
84 : // which is a private symbol to us, gets called
85 76 : mxDrawComponent = getMultiServiceFactory()->createInstance("com.sun.star.comp.Draw.PresentationDocument");
86 76 : CPPUNIT_ASSERT_MESSAGE("no impress component!", mxDrawComponent.is());
87 76 : }
88 :
89 76 : virtual void tearDown() SAL_OVERRIDE
90 : {
91 76 : uno::Reference<lang::XComponent>(mxDrawComponent, uno::UNO_QUERY_THROW)->dispose();
92 76 : test::BootstrapFixture::tearDown();
93 76 : }
94 :
95 : protected:
96 : /// Load the document.
97 116 : sd::DrawDocShellRef loadURL( const OUString &rURL, sal_Int32 nFormat )
98 : {
99 116 : FileFormat *pFmt = getFormat(nFormat);
100 116 : CPPUNIT_ASSERT_MESSAGE( "missing filter info", pFmt->pName != NULL );
101 :
102 116 : sal_uInt32 nOptions = 0;
103 116 : if (pFmt->nFormatType)
104 116 : nOptions = SFX_FILTER_IMPORT | SFX_FILTER_USESOPTIONS;
105 : SfxFilter* aFilter = new SfxFilter(
106 : OUString::createFromAscii( pFmt->pFilterName ),
107 : OUString(), pFmt->nFormatType, nOptions,
108 : OUString::createFromAscii( pFmt->pTypeName ),
109 : 0, OUString(),
110 : OUString::createFromAscii( pFmt->pUserData ),
111 116 : OUString("private:factory/simpress*") );
112 116 : aFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
113 :
114 116 : ::sd::DrawDocShellRef xDocShRef = new ::sd::DrawDocShell();
115 116 : SfxMedium* pSrcMed = new SfxMedium(rURL, STREAM_STD_READ);
116 116 : pSrcMed->SetFilter(aFilter);
117 116 : if ( !xDocShRef->DoLoad(pSrcMed) || !xDocShRef.Is() )
118 : {
119 0 : if (xDocShRef.Is())
120 0 : xDocShRef->DoClose();
121 0 : CPPUNIT_ASSERT_MESSAGE( OUStringToOString( "failed to load " + rURL, RTL_TEXTENCODING_UTF8 ).getStr(), false );
122 : }
123 116 : CPPUNIT_ASSERT_MESSAGE( "not in destruction", !xDocShRef->IsInDestruction() );
124 :
125 116 : return xDocShRef;
126 : }
127 :
128 142 : FileFormat* getFormat(sal_Int32 nExportType)
129 : {
130 142 : FileFormat* pFormat = &aFileFormats[0];
131 142 : if (((sal_uInt32) nExportType) < SAL_N_ELEMENTS(aFileFormats))
132 142 : pFormat = &aFileFormats[nExportType];
133 142 : return pFormat;
134 : }
135 :
136 2 : void exportTo(sd::DrawDocShell* pShell, FileFormat* pFormat, utl::TempFile& rTempFile)
137 : {
138 2 : SfxMedium aStoreMedium(rTempFile.GetURL(), STREAM_STD_WRITE);
139 2 : sal_uInt32 nExportFormat = 0;
140 2 : if (pFormat->nFormatType == ODP_FORMAT_TYPE)
141 0 : nExportFormat = SFX_FILTER_EXPORT | SFX_FILTER_USESOPTIONS;
142 : SfxFilter* pExportFilter = new SfxFilter(
143 : OUString::createFromAscii(pFormat->pFilterName),
144 : OUString(), pFormat->nFormatType, nExportFormat,
145 : OUString::createFromAscii(pFormat->pTypeName),
146 : 0, OUString(),
147 : OUString::createFromAscii(pFormat->pUserData),
148 2 : OUString("private:factory/simpress*") );
149 2 : pExportFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
150 2 : aStoreMedium.SetFilter(pExportFilter);
151 2 : pShell->ConvertTo(aStoreMedium);
152 2 : pShell->DoClose();
153 2 : }
154 :
155 24 : void save(sd::DrawDocShell* pShell, FileFormat* pFormat, utl::TempFile& rTempFile)
156 : {
157 24 : SfxMedium aStoreMedium(rTempFile.GetURL(), STREAM_STD_WRITE);
158 24 : sal_uInt32 nExportFormat = 0;
159 24 : if (pFormat->nFormatType == ODP_FORMAT_TYPE)
160 4 : nExportFormat = SFX_FILTER_EXPORT | SFX_FILTER_USESOPTIONS;
161 : SfxFilter* pExportFilter = new SfxFilter(
162 : OUString::createFromAscii(pFormat->pFilterName),
163 : OUString(), pFormat->nFormatType, nExportFormat,
164 : OUString::createFromAscii(pFormat->pTypeName),
165 : 0, OUString(),
166 : OUString::createFromAscii(pFormat->pUserData),
167 24 : OUString("private:factory/simpress*") );
168 24 : pExportFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
169 24 : aStoreMedium.SetFilter(pExportFilter);
170 24 : pShell->DoSaveAs(aStoreMedium);
171 24 : pShell->DoClose();
172 24 : }
173 :
174 24 : sd::DrawDocShellRef saveAndReload(sd::DrawDocShell *pShell, sal_Int32 nExportType)
175 : {
176 24 : FileFormat* pFormat = getFormat(nExportType);
177 24 : utl::TempFile aTempFile;
178 24 : save(pShell, pFormat, aTempFile);
179 24 : if(nExportType == ODP)
180 : {
181 : // BootstrapFixture::validate(aTempFile.GetFileName(), test::ODF);
182 : }
183 20 : else if(nExportType == PPTX)
184 : {
185 20 : BootstrapFixture::validate(aTempFile.GetFileName(), test::OOXML);
186 : }
187 24 : aTempFile.EnableKillingFile();
188 24 : return loadURL(aTempFile.GetURL(), nExportType);
189 : }
190 :
191 : /** Dump shapes in xDocShRef, and compare the dump against content of pShapesDumpFileNameBase<number>.xml.
192 :
193 : @param bCreate Instead of comparing to the reference file(s), create it/them.
194 : */
195 18 : void compareWithShapesDump( ::sd::DrawDocShellRef xDocShRef, const OUString &rShapesDumpFileNameBase, bool bCreate = false )
196 : {
197 18 : CPPUNIT_ASSERT_MESSAGE( "failed to load", xDocShRef.Is() );
198 18 : CPPUNIT_ASSERT_MESSAGE( "not in destruction", !xDocShRef->IsInDestruction() );
199 :
200 18 : uno::Reference<frame::XModel> xTempModel(xDocShRef->GetDoc()->getUnoModel(), uno::UNO_QUERY_THROW);
201 18 : CPPUNIT_ASSERT(xTempModel.is());
202 36 : uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSupplier (xTempModel, uno::UNO_QUERY_THROW);
203 18 : CPPUNIT_ASSERT(xDrawPagesSupplier.is());
204 36 : uno::Reference< drawing::XDrawPages > xDrawPages = xDrawPagesSupplier->getDrawPages();
205 18 : CPPUNIT_ASSERT(xDrawPages.is());
206 :
207 18 : XShapeDumper xShapeDumper;
208 18 : sal_Int32 nLength = xDrawPages->getCount();
209 54 : for (sal_Int32 i = 0; i < nLength; ++i)
210 : {
211 36 : uno::Reference<drawing::XDrawPage> xDrawPage;
212 72 : uno::Any aAny = xDrawPages->getByIndex(i);
213 36 : aAny >>= xDrawPage;
214 72 : uno::Reference< drawing::XShapes > xShapes(xDrawPage, uno::UNO_QUERY_THROW);
215 72 : OUString aString = xShapeDumper.dump(xShapes);
216 :
217 72 : OStringBuffer aFileNameBuf( OUStringToOString( rShapesDumpFileNameBase, RTL_TEXTENCODING_UTF8 ) );
218 36 : aFileNameBuf.append(i);
219 36 : aFileNameBuf.append(".xml");
220 :
221 72 : OString aFileName = aFileNameBuf.makeStringAndClear();
222 :
223 36 : if ( bCreate )
224 : {
225 0 : std::ofstream aStream( aFileName.getStr(), std::ofstream::out );
226 0 : aStream << aString;
227 0 : aStream.close();
228 : }
229 : else
230 : {
231 : doXMLDiff(aFileName.getStr(),
232 : OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr(),
233 : static_cast<int>(aString.getLength()),
234 : OUStringToOString(
235 : getPathFromSrc("/sd/qa/unit/data/tolerance.xml"),
236 36 : RTL_TEXTENCODING_UTF8).getStr());
237 : }
238 36 : }
239 36 : xDocShRef->DoClose();
240 18 : }
241 : };
242 :
243 : #endif
244 :
245 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|