1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <test/unoapi_test.hxx>
12 #include <com/sun/star/beans/NamedValue.hpp>
13 #include <com/sun/star/frame/Desktop.hpp>
14 #include <com/sun/star/frame/XStorable.hpp>
15 #include <comphelper/processfactory.hxx>
16 #include <comphelper/propertyvalue.hxx>
17 #include <comphelper/sequence.hxx>
19 #include <sfx2/app.hxx>
20 #include <sfx2/objsh.hxx>
21 #include <unotools/mediadescriptor.hxx>
25 using namespace css::uno
;
27 UnoApiTest::UnoApiTest(OUString path
)
28 : mbSkipValidation(false)
29 , m_aBaseString(std::move(path
))
31 maTempFile
.EnableKillingFile();
34 void UnoApiTest::setUp()
36 test::BootstrapFixture::setUp();
39 = css::frame::Desktop::create(comphelper::getComponentContext(getMultiServiceFactory()));
40 CPPUNIT_ASSERT_MESSAGE("no desktop!", mxDesktop
.is());
41 SfxApplication::GetOrCreate();
44 void UnoApiTest::tearDown()
46 if (mxComponent2
.is())
48 mxComponent2
->dispose();
54 mxComponent
->dispose();
58 test::BootstrapFixture::tearDown();
61 OUString
UnoApiTest::createFileURL(std::u16string_view aFileBase
)
63 return m_directories
.getSrcRootURL() + m_aBaseString
+ "/" + aFileBase
;
66 OUString
UnoApiTest::createFilePath(std::u16string_view aFileBase
)
68 return m_directories
.getSrcRootPath() + "/" + m_aBaseString
+ "/" + aFileBase
;
71 void UnoApiTest::setTestInteractionHandler(const char* pPassword
,
72 std::vector
<beans::PropertyValue
>& rFilterOptions
)
74 OUString sPassword
= OUString::createFromAscii(pPassword
);
75 rFilterOptions
.emplace_back();
77 = rtl::Reference
<TestInteractionHandler
>(new TestInteractionHandler(sPassword
));
78 css::uno::Reference
<task::XInteractionHandler2
> const xInteraction(xInteractionHandler
);
79 rFilterOptions
[0].Name
= "InteractionHandler";
80 rFilterOptions
[0].Value
<<= xInteraction
;
83 void UnoApiTest::load(OUString
const& rURL
, const char* pPassword
)
87 mxComponent
->dispose();
91 std::vector
<beans::PropertyValue
> aFilterOptions
;
95 setTestInteractionHandler(pPassword
, aFilterOptions
);
98 if (!maImportFilterOptions
.isEmpty())
100 beans::PropertyValue aValue
;
101 aValue
.Name
= "FilterOptions";
102 aValue
.Value
<<= maImportFilterOptions
;
103 aFilterOptions
.push_back(aValue
);
106 if (!maImportFilterName
.isEmpty())
108 beans::PropertyValue aValue
;
109 aValue
.Name
= "FilterName";
110 aValue
.Value
<<= maImportFilterName
;
111 aFilterOptions
.push_back(aValue
);
115 = loadFromDesktop(rURL
, OUString(), comphelper::containerToSequence(aFilterOptions
));
119 CPPUNIT_ASSERT_MESSAGE("Password set but not requested",
120 xInteractionHandler
->wasPasswordRequested());
124 OUString
UnoApiTest::loadFromURL(std::u16string_view aFileBase
, const char* pPassword
)
126 OUString aFileName
= createFileURL(aFileBase
);
127 load(aFileName
, pPassword
);
131 uno::Any
UnoApiTest::executeMacro(const OUString
& rScriptURL
,
132 const uno::Sequence
<uno::Any
>& rParams
)
135 uno::Sequence
<sal_Int16
> aOutParamIndex
;
136 uno::Sequence
<uno::Any
> aOutParam
;
138 ErrCode result
= SfxObjectShell::CallXScript(mxComponent
, rScriptURL
, rParams
, aRet
,
139 aOutParamIndex
, aOutParam
);
140 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE
, result
);
145 void UnoApiTest::save(const OUString
& rFilter
, const char* pPassword
)
147 utl::MediaDescriptor aMediaDescriptor
;
148 aMediaDescriptor
["FilterName"] <<= rFilter
;
149 if (!maFilterOptions
.isEmpty())
150 aMediaDescriptor
["FilterOptions"] <<= maFilterOptions
;
151 css::uno::Reference
<frame::XStorable
> xStorable(mxComponent
, css::uno::UNO_QUERY_THROW
);
155 if (rFilter
!= "Office Open XML Text" && rFilter
!= "Calc Office Open XML"
156 && rFilter
!= "Impress Office Open XML")
158 aMediaDescriptor
["Password"] <<= OUString::createFromAscii(pPassword
);
162 OUString sPassword
= OUString::createFromAscii(pPassword
);
163 uno::Sequence
<beans::NamedValue
> aEncryptionData
{
164 { "CryptoType", uno::Any(OUString("Standard")) },
165 { "OOXPassword", uno::Any(sPassword
) }
167 aMediaDescriptor
[utl::MediaDescriptor::PROP_ENCRYPTIONDATA
] <<= aEncryptionData
;
169 // validation fails with "zip END header not found"
174 xStorable
->storeToURL(maTempFile
.GetURL(), aMediaDescriptor
.getAsConstPropertyValueList());
176 if (!mbSkipValidation
)
178 if (rFilter
== "Office Open XML Text")
179 validate(maTempFile
.GetFileName(), test::OOXML
);
180 else if (rFilter
== "Calc Office Open XML")
181 validate(maTempFile
.GetFileName(), test::OOXML
);
182 else if (rFilter
== "Impress Office Open XML")
183 validate(maTempFile
.GetFileName(), test::OOXML
);
184 else if (rFilter
== "writer8")
185 validate(maTempFile
.GetFileName(), test::ODF
);
186 else if (rFilter
== "calc8")
187 validate(maTempFile
.GetFileName(), test::ODF
);
188 else if (rFilter
== "impress8")
189 validate(maTempFile
.GetFileName(), test::ODF
);
190 else if (rFilter
== "draw8")
191 validate(maTempFile
.GetFileName(), test::ODF
);
192 else if (rFilter
== "OpenDocument Text Flat XML")
193 validate(maTempFile
.GetFileName(), test::ODF
);
194 else if (rFilter
== "MS Word 97")
195 validate(maTempFile
.GetFileName(), test::MSBINARY
);
196 else if (rFilter
== "MS Excel 97")
197 validate(maTempFile
.GetFileName(), test::MSBINARY
);
198 else if (rFilter
== "MS PowerPoint 97")
199 validate(maTempFile
.GetFileName(), test::MSBINARY
);
203 void UnoApiTest::saveAndReload(const OUString
& rFilter
, const char* pPassword
)
205 save(rFilter
, pPassword
);
207 load(maTempFile
.GetURL(), pPassword
);
210 std::unique_ptr
<vcl::pdf::PDFiumDocument
> UnoApiTest::parsePDFExport(const OString
& rPassword
)
212 SvFileStream
aFile(maTempFile
.GetURL(), StreamMode::READ
);
213 maMemory
.WriteStream(aFile
);
214 std::shared_ptr
<vcl::pdf::PDFium
> pPDFium
= vcl::pdf::PDFiumLibrary::get();
219 std::unique_ptr
<vcl::pdf::PDFiumDocument
> pPdfDocument
220 = pPDFium
->openDocument(maMemory
.GetData(), maMemory
.GetSize(), rPassword
);
221 CPPUNIT_ASSERT(pPdfDocument
);
225 void UnoApiTest::createTempCopy(std::u16string_view fileName
)
227 OUString url
= createFileURL(fileName
);
228 auto const e
= osl::File::copy(url
, maTempFile
.GetURL());
229 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, e
);
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */