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 SD_QA_UNIT_SDMODELTESTBASE_HXX
11 : #define 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 :
22 : #include <rtl/strbuf.hxx>
23 : #include <sfx2/docfile.hxx>
24 : #include <sfx2/docfilt.hxx>
25 :
26 : #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
27 : #include <drawinglayer/XShapeDumper.hxx>
28 :
29 : using namespace ::com::sun::star;
30 :
31 : struct FileFormat {
32 : const char* pName; const char* pFilterName; const char* pTypeName; const char* pUserData; sal_uLong nFormatType;
33 : };
34 :
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 )
39 :
40 : /** List of file formats we support in Impress unit tests.
41 :
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>
47 : */
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 }
53 : };
54 :
55 : /// Base class for filter tests loading or roundtriping a document, and asserting the document model.
56 5 : class SdModelTestBase : public test::BootstrapFixture, public unotest::MacrosTest
57 : {
58 : public:
59 5 : SdModelTestBase()
60 5 : {
61 5 : }
62 :
63 5 : virtual void setUp()
64 : {
65 5 : test::BootstrapFixture::setUp();
66 :
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 5 : m_xDrawComponent = getMultiServiceFactory()->createInstance("com.sun.star.comp.Draw.PresentationDocument");
70 5 : CPPUNIT_ASSERT_MESSAGE("no impress component!", m_xDrawComponent.is());
71 5 : }
72 :
73 5 : virtual void tearDown()
74 : {
75 5 : uno::Reference< lang::XComponent >( m_xDrawComponent, uno::UNO_QUERY_THROW )->dispose();
76 5 : test::BootstrapFixture::tearDown();
77 5 : }
78 :
79 : protected:
80 : /// Load the document.
81 11 : ::sd::DrawDocShellRef loadURL( const OUString &rURL )
82 : {
83 11 : FileFormat *pFmt(0);
84 :
85 27 : for (size_t i = 0; i < SAL_N_ELEMENTS (aFileFormats); i++)
86 : {
87 27 : pFmt = aFileFormats + i;
88 27 : if (pFmt->pName && rURL.endsWithIgnoreAsciiCaseAsciiL (pFmt->pName, strlen (pFmt->pName)))
89 11 : break;
90 : }
91 11 : CPPUNIT_ASSERT_MESSAGE( "missing filter info", pFmt->pName != NULL );
92 :
93 11 : sal_uInt32 nFormat = 0;
94 11 : if (pFmt->nFormatType)
95 11 : 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 11 : OUString("private:factory/simpress*") );
103 11 : aFilter->SetVersion(SOFFICE_FILEFORMAT_CURRENT);
104 :
105 11 : ::sd::DrawDocShellRef xDocShRef = new ::sd::DrawDocShell();
106 11 : SfxMedium* pSrcMed = new SfxMedium(rURL, STREAM_STD_READ);
107 11 : pSrcMed->SetFilter(aFilter);
108 11 : if ( !xDocShRef->DoLoad(pSrcMed) )
109 : {
110 0 : if (xDocShRef.Is())
111 0 : xDocShRef->DoClose();
112 0 : CPPUNIT_ASSERT_MESSAGE( OUStringToOString( "failed to load " + rURL, RTL_TEXTENCODING_UTF8 ).getStr(), false );
113 : }
114 :
115 11 : return xDocShRef;
116 : }
117 :
118 : /** Dump shapes in xDocShRef, and compare the dump against content of pShapesDumpFileNameBase<number>.xml.
119 :
120 : @param bCreate Instead of comparing to the reference file(s), create it/them.
121 : */
122 7 : void compareWithShapesDump( ::sd::DrawDocShellRef xDocShRef, const OUString &rShapesDumpFileNameBase, bool bCreate = false )
123 : {
124 7 : CPPUNIT_ASSERT_MESSAGE( "failed to load", xDocShRef.Is() );
125 7 : CPPUNIT_ASSERT_MESSAGE( "not in destruction", !xDocShRef->IsInDestruction() );
126 :
127 7 : uno::Reference<frame::XModel> xTempModel(xDocShRef->GetDoc()->getUnoModel(), uno::UNO_QUERY_THROW);
128 7 : CPPUNIT_ASSERT(xTempModel.is());
129 14 : uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSupplier (xTempModel, uno::UNO_QUERY_THROW);
130 7 : CPPUNIT_ASSERT(xDrawPagesSupplier.is());
131 14 : uno::Reference< drawing::XDrawPages > xDrawPages = xDrawPagesSupplier->getDrawPages();
132 7 : CPPUNIT_ASSERT(xDrawPages.is());
133 :
134 7 : XShapeDumper xShapeDumper;
135 7 : sal_Int32 nLength = xDrawPages->getCount();
136 23 : for (sal_Int32 i = 0; i < nLength; ++i)
137 : {
138 16 : uno::Reference<drawing::XDrawPage> xDrawPage;
139 32 : uno::Any aAny = xDrawPages->getByIndex(i);
140 16 : aAny >>= xDrawPage;
141 32 : uno::Reference< drawing::XShapes > xShapes(xDrawPage, uno::UNO_QUERY_THROW);
142 32 : OUString aString = xShapeDumper.dump(xShapes);
143 :
144 32 : OStringBuffer aFileNameBuf( OUStringToOString( rShapesDumpFileNameBase, RTL_TEXTENCODING_UTF8 ) );
145 16 : aFileNameBuf.append(i);
146 16 : aFileNameBuf.append(".xml");
147 :
148 32 : OString aFileName = aFileNameBuf.makeStringAndClear();
149 :
150 16 : if ( bCreate )
151 : {
152 0 : std::ofstream aStream( aFileName.getStr(), std::ofstream::out );
153 0 : aStream << aString;
154 0 : aStream.close();
155 : }
156 : else
157 : {
158 : doXMLDiff(aFileName.getStr(),
159 : OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr(),
160 16 : static_cast<int>(aString.getLength()),
161 : OUStringToOString(
162 : getPathFromSrc("/sd/qa/unit/data/tolerance.xml"),
163 32 : RTL_TEXTENCODING_UTF8).getStr());
164 : }
165 16 : }
166 14 : xDocShRef->DoClose();
167 7 : }
168 :
169 :
170 : private:
171 : uno::Reference<uno::XInterface> m_xDrawComponent;
172 : };
173 :
174 : #endif
175 :
176 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|