LibreOffice Module test (master) 1
unoapi_test.cxx
Go to the documentation of this file.
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#include <test/unoapi_test.hxx>
11
12#include <com/sun/star/beans/NamedValue.hpp>
13#include <com/sun/star/frame/Desktop.hpp>
14#include <com/sun/star/frame/XStorable.hpp>
18
19#include <sfx2/app.hxx>
20#include <sfx2/objsh.hxx>
22#include <utility>
23
24using namespace css;
25using namespace css::uno;
26
28 : mbSkipValidation(false)
29 , m_aBaseString(std::move(path))
30{
32}
33
35{
37
39 = css::frame::Desktop::create(comphelper::getComponentContext(getMultiServiceFactory()));
40 CPPUNIT_ASSERT_MESSAGE("no desktop!", mxDesktop.is());
42}
43
45{
46 if (mxComponent2.is())
47 {
48 mxComponent2->dispose();
49 mxComponent2.clear();
50 }
51
52 if (mxComponent.is())
53 {
54 mxComponent->dispose();
55 mxComponent.clear();
56 }
57
59}
60
61OUString UnoApiTest::createFileURL(std::u16string_view aFileBase)
62{
63 return m_directories.getSrcRootURL() + m_aBaseString + "/" + aFileBase;
64}
65
66OUString UnoApiTest::createFilePath(std::u16string_view aFileBase)
67{
68 return m_directories.getSrcRootPath() + "/" + m_aBaseString + "/" + aFileBase;
69}
70
71void UnoApiTest::setTestInteractionHandler(const char* pPassword,
72 std::vector<beans::PropertyValue>& rFilterOptions)
73{
74 OUString sPassword = OUString::createFromAscii(pPassword);
75 rFilterOptions.emplace_back();
78 css::uno::Reference<task::XInteractionHandler2> const xInteraction(xInteractionHandler);
79 rFilterOptions[0].Name = "InteractionHandler";
80 rFilterOptions[0].Value <<= xInteraction;
81}
82
83void UnoApiTest::load(OUString const& rURL, const char* pPassword)
84{
85 if (mxComponent.is())
86 {
87 mxComponent->dispose();
88 mxComponent.clear();
89 }
90
91 std::vector<beans::PropertyValue> aFilterOptions;
92
93 if (pPassword)
94 {
95 setTestInteractionHandler(pPassword, aFilterOptions);
96 }
97
98 if (!maImportFilterOptions.isEmpty())
99 {
100 beans::PropertyValue aValue;
101 aValue.Name = "FilterOptions";
102 aValue.Value <<= maImportFilterOptions;
103 aFilterOptions.push_back(aValue);
104 }
105
106 if (!maImportFilterName.isEmpty())
107 {
108 beans::PropertyValue aValue;
109 aValue.Name = "FilterName";
110 aValue.Value <<= maImportFilterName;
111 aFilterOptions.push_back(aValue);
112 }
113
115 = loadFromDesktop(rURL, OUString(), comphelper::containerToSequence(aFilterOptions));
116
117 if (pPassword)
118 {
119 CPPUNIT_ASSERT_MESSAGE("Password set but not requested",
120 xInteractionHandler->wasPasswordRequested());
121 }
122}
123
124OUString UnoApiTest::loadFromURL(std::u16string_view aFileBase, const char* pPassword)
125{
126 OUString aFileName = createFileURL(aFileBase);
127 load(aFileName, pPassword);
128 return aFileName;
129}
130
131uno::Any UnoApiTest::executeMacro(const OUString& rScriptURL,
132 const uno::Sequence<uno::Any>& rParams)
133{
134 uno::Any aRet;
135 uno::Sequence<sal_Int16> aOutParamIndex;
136 uno::Sequence<uno::Any> aOutParam;
137
138 ErrCode result = SfxObjectShell::CallXScript(mxComponent, rScriptURL, rParams, aRet,
139 aOutParamIndex, aOutParam);
140 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, result);
141
142 return aRet;
143}
144
145void UnoApiTest::save(const OUString& rFilter, const char* pPassword)
146{
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);
152
153 if (pPassword)
154 {
155 if (rFilter != "Office Open XML Text" && rFilter != "Calc Office Open XML"
156 && rFilter != "Impress Office Open XML")
157 {
158 aMediaDescriptor["Password"] <<= OUString::createFromAscii(pPassword);
159 }
160 else
161 {
162 OUString sPassword = OUString::createFromAscii(pPassword);
163 uno::Sequence<beans::NamedValue> aEncryptionData{
164 { "CryptoType", uno::Any(OUString("Standard")) },
165 { "OOXPassword", uno::Any(sPassword) }
166 };
167 aMediaDescriptor[utl::MediaDescriptor::PROP_ENCRYPTIONDATA] <<= aEncryptionData;
168
169 // validation fails with "zip END header not found"
171 }
172 }
173
174 xStorable->storeToURL(maTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
175
176 if (!mbSkipValidation)
177 {
178 if (rFilter == "Calc Office Open XML")
180 /*
181 // too many validation errors right now
182 else if (rFilter == "Office Open XML Text")
183 validate(maTempFile.GetFileName(), test::OOXML);
184 */
185 else if (rFilter == "Impress Office Open XML")
187 else if (rFilter == "writer8")
189 else if (rFilter == "calc8")
191 else if (rFilter == "impress8")
193 else if (rFilter == "draw8")
195 else if (rFilter == "OpenDocument Text Flat XML")
197 else if (rFilter == "MS Word 97")
199 else if (rFilter == "MS Excel 97")
201 else if (rFilter == "MS PowerPoint 97")
203 }
204}
205
206void UnoApiTest::saveAndReload(const OUString& rFilter, const char* pPassword)
207{
208 save(rFilter, pPassword);
209
210 load(maTempFile.GetURL(), pPassword);
211}
212
213std::unique_ptr<vcl::pdf::PDFiumDocument> UnoApiTest::parsePDFExport(const OString& rPassword)
214{
215 SvFileStream aFile(maTempFile.GetURL(), StreamMode::READ);
216 maMemory.WriteStream(aFile);
217 std::shared_ptr<vcl::pdf::PDFium> pPDFium = vcl::pdf::PDFiumLibrary::get();
218 if (!pPDFium)
219 {
220 return nullptr;
221 }
222 std::unique_ptr<vcl::pdf::PDFiumDocument> pPdfDocument
223 = pPDFium->openDocument(maMemory.GetData(), maMemory.GetSize(), rPassword);
224 CPPUNIT_ASSERT(pPdfDocument);
225 return pPdfDocument;
226}
227
228void UnoApiTest::createTempCopy(std::u16string_view fileName)
229{
230 OUString url = createFileURL(fileName);
231 auto const e = osl::File::copy(url, maTempFile.GetURL());
232 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e);
233}
234
235/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static SfxApplication * GetOrCreate()
ErrCode CallXScript(const OUString &rScriptURL, const css::uno::Sequence< css::uno::Any > &aParams, css::uno::Any &aRet, css::uno::Sequence< sal_Int16 > &aOutParamIndex, css::uno::Sequence< css::uno::Any > &aOutParam, bool bRaiseError=true, const css::uno::Any *aCaller=nullptr)
const void * GetData()
sal_uInt64 GetSize()
SvStream & WriteStream(SvStream &rStream)
bool mbSkipValidation
Definition: unoapi_test.hxx:79
virtual void tearDown() override
Definition: unoapi_test.cxx:44
css::uno::Reference< css::lang::XComponent > mxComponent2
Definition: unoapi_test.hxx:66
OUString maFilterOptions
Definition: unoapi_test.hxx:81
OUString createFilePath(std::u16string_view aFileBase)
Definition: unoapi_test.cxx:66
css::uno::Any executeMacro(const OUString &rScriptURL, const css::uno::Sequence< css::uno::Any > &rParams={})
rtl::Reference< TestInteractionHandler > xInteractionHandler
Definition: unoapi_test.hxx:72
void saveAndReload(const OUString &rFilter, const char *pPassword=nullptr)
void createTempCopy(std::u16string_view fileName)
OUString loadFromURL(std::u16string_view aFileBase, const char *pPassword=nullptr)
OUString maImportFilterName
Definition: unoapi_test.hxx:84
OUString m_aBaseString
Definition: unoapi_test.hxx:80
UnoApiTest(OUString path)
Definition: unoapi_test.cxx:27
void skipValidation()
Definition: unoapi_test.hxx:51
void load(const OUString &rURL, const char *pPassword=nullptr)
Definition: unoapi_test.cxx:83
OUString createFileURL(std::u16string_view aFileBase)
Definition: unoapi_test.cxx:61
virtual void setUp() override
Definition: unoapi_test.cxx:34
std::unique_ptr< vcl::pdf::PDFiumDocument > parsePDFExport(const OString &rPassword=OString())
utl::TempFileNamed maTempFile
Definition: unoapi_test.hxx:68
void setTestInteractionHandler(const char *pPassword, std::vector< com::sun::star::beans::PropertyValue > &rFilterOptions)
Definition: unoapi_test.cxx:71
css::uno::Reference< css::lang::XComponent > mxComponent
Definition: unoapi_test.hxx:63
OUString maImportFilterOptions
Definition: unoapi_test.hxx:83
SvMemoryStream maMemory
Definition: unoapi_test.hxx:70
void save(const OUString &rFilter, const char *pPassword=nullptr)
const css::uno::Reference< css::lang::XMultiServiceFactory > & getMultiServiceFactory() const
virtual void tearDown() override
virtual void setUp() override
void validate(const OUString &rURL, ValidationFormat) const
const OUString & getSrcRootPath() const
const OUString & getSrcRootURL() const
css::uno::Reference< css::frame::XDesktop2 > mxDesktop
css::uno::Reference< css::lang::XComponent > loadFromDesktop(const OUString &rURL, const OUString &rDocService=OUString(), const css::uno::Sequence< css::beans::PropertyValue > &rExtra_args=css::uno::Sequence< css::beans::PropertyValue >())
static constexpr OUStringLiteral PROP_ENCRYPTIONDATA
void EnableKillingFile(bool bEnable=true)
OUString const & GetURL() const
OUString GetFileName() const
#define ERRCODE_NONE
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Reference< XComponentContext > getComponentContext(Reference< XMultiServiceFactory > const &factory)
static std::shared_ptr< PDFium > & get()
Any result