update credits
[LibreOffice.git] / sd / qa / unit / sdmodeltestbase.hxx
blob853f089d0c26b86ba18b05559c95cc7724ed76ca
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 SD_QA_UNIT_SDMODELTESTBASE_HXX
11 #define 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"
22 #include <rtl/strbuf.hxx>
23 #include <sfx2/docfile.hxx>
24 #include <sfx2/docfilt.hxx>
26 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
27 #include <drawinglayer/XShapeDumper.hxx>
29 using namespace ::com::sun::star;
31 struct FileFormat {
32 const char* pName; const char* pFilterName; const char* pTypeName; const char* pUserData; sal_uLong nFormatType;
35 // These values are taken from "Flags" in filter/source/config/fragments/filters/*
36 #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 )
37 #define PPT_FORMAT_TYPE ( SFX_FILTER_IMPORT | SFX_FILTER_EXPORT | SFX_FILTER_ALIEN )
38 #define PPTX_FORMAT_TYPE ( SFX_FILTER_IMPORT | SFX_FILTER_EXPORT | SFX_FILTER_ALIEN | SFX_FILTER_STARONEFILTER | SFX_FILTER_PREFERED )
40 /** List of file formats we support in Impress unit tests.
42 Taken from filter/source/config/fragments/filters/ too:
43 pName: The file extension.
44 pFilterName: <node oor:Name="...">
45 pTypeName: <prop oor:Name="UIName">...</prop>
46 nFormatType: <prop oor:name="Flags">...</prop>
48 FileFormat aFileFormats[] = {
49 { "odp", "impress8", "impress8", "", ODP_FORMAT_TYPE },
50 { "ppt", "MS PowerPoint 97", "Microsoft PowerPoint 97/2000/XP/2003", "sdfilt", PPT_FORMAT_TYPE },
51 { "pptx", "Impress MS PowerPoint 2007 XML", "MS PowerPoint 2007 XML", "", PPTX_FORMAT_TYPE },
52 { 0, 0, 0, 0, 0 }
55 /// Base class for filter tests loading or roundtriping a document, and asserting the document model.
56 class SdModelTestBase : public test::BootstrapFixture, public unotest::MacrosTest
58 public:
59 SdModelTestBase()
63 virtual void setUp()
65 test::BootstrapFixture::setUp();
67 // This is a bit of a fudge, we do this to ensure that ScGlobals::ensure,
68 // which is a private symbol to us, gets called
69 m_xDrawComponent = getMultiServiceFactory()->createInstance(OUString("com.sun.star.comp.Draw.PresentationDocument"));
70 CPPUNIT_ASSERT_MESSAGE("no impress component!", m_xDrawComponent.is());
73 virtual void tearDown()
75 uno::Reference< lang::XComponent >( m_xDrawComponent, uno::UNO_QUERY_THROW )->dispose();
76 test::BootstrapFixture::tearDown();
79 protected:
80 /// Load the document.
81 ::sd::DrawDocShellRef loadURL( const OUString &rURL )
83 FileFormat *pFmt(0);
85 for (size_t i = 0; i < SAL_N_ELEMENTS (aFileFormats); i++)
87 pFmt = aFileFormats + i;
88 if (pFmt->pName && rURL.endsWithIgnoreAsciiCaseAsciiL (pFmt->pName, strlen (pFmt->pName)))
89 break;
91 CPPUNIT_ASSERT_MESSAGE( "missing filter info", pFmt->pName != NULL );
93 sal_uInt32 nFormat = 0;
94 if (pFmt->nFormatType)
95 nFormat = SFX_FILTER_IMPORT | SFX_FILTER_USESOPTIONS;
96 SfxFilter* aFilter = new SfxFilter(
97 OUString::createFromAscii( pFmt->pFilterName ),
98 OUString(), pFmt->nFormatType, nFormat,
99 OUString::createFromAscii( pFmt->pTypeName ),
100 0, OUString(),
101 OUString::createFromAscii( pFmt->pUserData ),
102 OUString("private:factory/simpress*") );
103 aFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
105 ::sd::DrawDocShellRef xDocShRef = new ::sd::DrawDocShell();
106 SfxMedium* pSrcMed = new SfxMedium(rURL, STREAM_STD_READ);
107 pSrcMed->SetFilter(aFilter);
108 if ( !xDocShRef->DoLoad(pSrcMed) )
110 if (xDocShRef.Is())
111 xDocShRef->DoClose();
112 CPPUNIT_ASSERT_MESSAGE( OUStringToOString( "failed to load " + rURL, RTL_TEXTENCODING_UTF8 ).getStr(), false );
115 return xDocShRef;
118 /** Dump shapes in xDocShRef, and compare the dump against content of pShapesDumpFileNameBase<number>.xml.
120 @param bCreate Instead of comparing to the reference file(s), create it/them.
122 void compareWithShapesDump( ::sd::DrawDocShellRef xDocShRef, const OUString &rShapesDumpFileNameBase, bool bCreate = false )
124 CPPUNIT_ASSERT_MESSAGE( "failed to load", xDocShRef.Is() );
125 CPPUNIT_ASSERT_MESSAGE( "not in destruction", !xDocShRef->IsInDestruction() );
127 uno::Reference<frame::XModel> xTempModel(xDocShRef->GetDoc()->getUnoModel(), uno::UNO_QUERY_THROW);
128 CPPUNIT_ASSERT(xTempModel.is());
129 uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSupplier (xTempModel, uno::UNO_QUERY_THROW);
130 CPPUNIT_ASSERT(xDrawPagesSupplier.is());
131 uno::Reference< drawing::XDrawPages > xDrawPages = xDrawPagesSupplier->getDrawPages();
132 CPPUNIT_ASSERT(xDrawPages.is());
134 XShapeDumper xShapeDumper;
135 sal_Int32 nLength = xDrawPages->getCount();
136 for (sal_Int32 i = 0; i < nLength; ++i)
138 uno::Reference<drawing::XDrawPage> xDrawPage;
139 uno::Any aAny = xDrawPages->getByIndex(i);
140 aAny >>= xDrawPage;
141 uno::Reference< drawing::XShapes > xShapes(xDrawPage, uno::UNO_QUERY_THROW);
142 OUString aString = xShapeDumper.dump(xShapes);
144 OStringBuffer aFileNameBuf( OUStringToOString( rShapesDumpFileNameBase, RTL_TEXTENCODING_UTF8 ) );
145 aFileNameBuf.append(i);
146 aFileNameBuf.append(".xml");
148 OString aFileName = aFileNameBuf.makeStringAndClear();
150 if ( bCreate )
152 std::ofstream aStream( aFileName.getStr(), std::ofstream::out );
153 aStream << aString;
154 aStream.close();
156 else
158 doXMLDiff(aFileName.getStr(),
159 OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr(),
160 static_cast<int>(aString.getLength()),
161 OUStringToOString(
162 getPathFromSrc("/sd/qa/unit/data/tolerance.xml"),
163 RTL_TEXTENCODING_UTF8).getStr());
166 xDocShRef->DoClose();
170 private:
171 uno::Reference<uno::XInterface> m_xDrawComponent;
174 #endif
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */