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 <sal/config.h>
11 #include <rtl/strbuf.hxx>
12 #include <osl/file.hxx>
14 #include <sfx2/app.hxx>
15 #include <sfx2/docfile.hxx>
16 #include <sfx2/frame.hxx>
17 #include <sfx2/sfxmodelfactory.hxx>
18 #include <svl/stritem.hxx>
21 #define CALC_DEBUG_OUTPUT 0
23 #include "helper/qahelper.hxx"
24 #include "helper/shared_test_impl.hxx"
28 #include "patattr.hxx"
29 #include "scitems.hxx"
30 #include "document.hxx"
31 #include "cellform.hxx"
32 #include "tabprotection.hxx"
34 using namespace ::com::sun::star
;
35 using namespace ::com::sun::star::uno
;
37 class ScExportTest
: public ScBootstrapFixture
43 virtual void tearDown();
45 ScDocShellRef
saveAndReloadPassword( ScDocShell
*, const OUString
&, const OUString
&, const OUString
&, sal_uLong
);
48 void testPasswordExport();
49 void testConditionalFormatExportODS();
50 void testConditionalFormatExportXLSX();
51 void testColorScaleExportODS();
52 void testColorScaleExportXLSX();
53 void testDataBarExportODS();
54 void testDataBarExportXLSX();
55 void testMiscRowHeightExport();
56 void testNamedRangeBugfdo62729();
57 void testSheetProtectionXLSX();
59 CPPUNIT_TEST_SUITE(ScExportTest
);
61 #if !defined(MACOSX) && !defined(DRAGONFLY)
62 CPPUNIT_TEST(testPasswordExport
);
64 CPPUNIT_TEST(testConditionalFormatExportODS
);
65 CPPUNIT_TEST(testConditionalFormatExportXLSX
);
66 CPPUNIT_TEST(testColorScaleExportODS
);
67 CPPUNIT_TEST(testColorScaleExportXLSX
);
68 CPPUNIT_TEST(testMiscRowHeightExport
);
69 CPPUNIT_TEST(testNamedRangeBugfdo62729
);
70 CPPUNIT_TEST(testSheetProtectionXLSX
);
71 CPPUNIT_TEST_SUITE_END();
74 uno::Reference
<uno::XInterface
> m_xCalcComponent
;
78 ScDocShellRef
ScExportTest::saveAndReloadPassword(ScDocShell
* pShell
, const OUString
&rFilter
,
79 const OUString
&rUserData
, const OUString
& rTypeName
, sal_uLong nFormatType
)
81 utl::TempFile aTempFile
;
82 aTempFile
.EnableKillingFile();
83 SfxMedium
aStoreMedium( aTempFile
.GetURL(), STREAM_STD_WRITE
);
84 sal_uInt32 nExportFormat
= 0;
85 if (nFormatType
== ODS_FORMAT_TYPE
)
86 nExportFormat
= SFX_FILTER_EXPORT
| SFX_FILTER_USESOPTIONS
;
87 SfxFilter
* pExportFilter
= new SfxFilter(
89 OUString(), nFormatType
, nExportFormat
, rTypeName
, 0, OUString(),
90 rUserData
, OUString("private:factory/scalc*") );
91 pExportFilter
->SetVersion(SOFFICE_FILEFORMAT_CURRENT
);
92 aStoreMedium
.SetFilter(pExportFilter
);
93 SfxItemSet
* pExportSet
= aStoreMedium
.GetItemSet();
94 uno::Sequence
< beans::NamedValue
> aEncryptionData
= comphelper::OStorageHelper::CreatePackageEncryptionData( OUString("test") );
95 uno::Any xEncryptionData
;
96 xEncryptionData
<<= aEncryptionData
;
97 pExportSet
->Put(SfxUnoAnyItem(SID_ENCRYPTIONDATA
, xEncryptionData
));
99 uno::Reference
< embed::XStorage
> xMedStorage
= aStoreMedium
.GetStorage();
100 ::comphelper::OStorageHelper::SetCommonStorageEncryptionData( xMedStorage
, aEncryptionData
);
102 pShell
->DoSaveAs( aStoreMedium
);
105 //std::cout << "File: " << aTempFile.GetURL() << std::endl;
107 sal_uInt32 nFormat
= 0;
108 if (nFormatType
== ODS_FORMAT_TYPE
)
109 nFormat
= SFX_FILTER_IMPORT
| SFX_FILTER_USESOPTIONS
;
111 OUString
aPass("test");
112 return load(aTempFile
.GetURL(), rFilter
, rUserData
, rTypeName
, nFormatType
, nFormat
, SOFFICE_FILEFORMAT_CURRENT
, &aPass
);
115 void ScExportTest::test()
117 ScDocShell
* pShell
= new ScDocShell(
119 SFXMODEL_DISABLE_EMBEDDED_SCRIPTS
|
120 SFXMODEL_DISABLE_DOCUMENT_RECOVERY
);
123 ScDocument
* pDoc
= pShell
->GetDocument();
125 pDoc
->SetValue(0,0,0, 1.0);
126 CPPUNIT_ASSERT(pDoc
);
128 ScDocShellRef xDocSh
= saveAndReload( pShell
, ODS
);
130 CPPUNIT_ASSERT(xDocSh
.Is());
131 ScDocument
* pLoadedDoc
= xDocSh
->GetDocument();
132 double aVal
= pLoadedDoc
->GetValue(0,0,0);
133 ASSERT_DOUBLES_EQUAL(aVal
, 1.0);
136 void ScExportTest::testPasswordExport()
138 ScDocShell
* pShell
= new ScDocShell(
140 SFXMODEL_DISABLE_EMBEDDED_SCRIPTS
|
141 SFXMODEL_DISABLE_DOCUMENT_RECOVERY
);
144 ScDocument
* pDoc
= pShell
->GetDocument();
146 pDoc
->SetValue(0,0,0, 1.0);
147 CPPUNIT_ASSERT(pDoc
);
149 sal_Int32 nFormat
= ODS
;
150 OUString
aFilterName(aFileFormats
[nFormat
].pFilterName
, strlen(aFileFormats
[nFormat
].pFilterName
), RTL_TEXTENCODING_UTF8
) ;
151 OUString
aFilterType(aFileFormats
[nFormat
].pTypeName
, strlen(aFileFormats
[nFormat
].pTypeName
), RTL_TEXTENCODING_UTF8
);
152 ScDocShellRef xDocSh
= saveAndReloadPassword(pShell
, aFilterName
, OUString(), aFilterType
, aFileFormats
[nFormat
].nFormatType
);
154 CPPUNIT_ASSERT(xDocSh
.Is());
155 ScDocument
* pLoadedDoc
= xDocSh
->GetDocument();
156 double aVal
= pLoadedDoc
->GetValue(0,0,0);
157 ASSERT_DOUBLES_EQUAL(aVal
, 1.0);
162 void ScExportTest::testConditionalFormatExportODS()
164 ScDocShellRef xShell
= loadDoc("new_cond_format_test.", ODS
);
165 CPPUNIT_ASSERT(xShell
.Is());
167 ScDocShellRef xDocSh
= saveAndReload(&(*xShell
), ODS
);
168 CPPUNIT_ASSERT(xDocSh
.Is());
169 ScDocument
* pDoc
= xDocSh
->GetDocument();
170 OUString
aCSVFile("new_cond_format_test.");
172 createCSVPath( aCSVFile
, aCSVPath
);
173 testCondFile(aCSVPath
, pDoc
, 0);
178 void ScExportTest::testConditionalFormatExportXLSX()
180 ScDocShellRef xShell
= loadDoc("new_cond_format_test.", XLSX
);
181 CPPUNIT_ASSERT(xShell
.Is());
183 ScDocShellRef xDocSh
= saveAndReload(&(*xShell
), XLSX
);
184 CPPUNIT_ASSERT(xDocSh
.Is());
185 ScDocument
* pDoc
= xDocSh
->GetDocument();
186 OUString
aCSVFile("new_cond_format_test.");
188 createCSVPath( aCSVFile
, aCSVPath
);
189 testCondFile(aCSVPath
, pDoc
, 0);
194 void ScExportTest::testColorScaleExportODS()
196 ScDocShellRef xShell
= loadDoc("colorscale.", ODS
);
197 CPPUNIT_ASSERT(xShell
.Is());
199 ScDocShellRef xDocSh
= saveAndReload(xShell
, ODS
);
200 CPPUNIT_ASSERT(xDocSh
.Is());
202 ScDocument
* pDoc
= xDocSh
->GetDocument();
203 CPPUNIT_ASSERT(pDoc
);
205 testColorScale2Entry_Impl(pDoc
);
206 testColorScale3Entry_Impl(pDoc
);
211 void ScExportTest::testColorScaleExportXLSX()
213 ScDocShellRef xShell
= loadDoc("colorscale.", XLSX
);
214 CPPUNIT_ASSERT(xShell
.Is());
216 ScDocShellRef xDocSh
= saveAndReload(xShell
, XLSX
);
217 CPPUNIT_ASSERT(xDocSh
.Is());
219 ScDocument
* pDoc
= xDocSh
->GetDocument();
220 CPPUNIT_ASSERT(pDoc
);
222 testColorScale2Entry_Impl(pDoc
);
223 testColorScale3Entry_Impl(pDoc
);
228 void ScExportTest::testDataBarExportODS()
230 ScDocShellRef xShell
= loadDoc("databar.", ODS
);
231 CPPUNIT_ASSERT(xShell
.Is());
233 ScDocShellRef xDocSh
= saveAndReload(xShell
, ODS
);
234 CPPUNIT_ASSERT(xDocSh
.Is());
236 ScDocument
* pDoc
= xDocSh
->GetDocument();
237 CPPUNIT_ASSERT(pDoc
);
239 testDataBar_Impl(pDoc
);
244 void ScExportTest::testDataBarExportXLSX()
246 ScDocShellRef xShell
= loadDoc("databar.", XLSX
);
247 CPPUNIT_ASSERT(xShell
.Is());
249 ScDocShellRef xDocSh
= saveAndReload(xShell
, XLSX
);
250 CPPUNIT_ASSERT(xDocSh
.Is());
252 ScDocument
* pDoc
= xDocSh
->GetDocument();
253 CPPUNIT_ASSERT(pDoc
);
255 testDataBar_Impl(pDoc
);
260 void ScExportTest::testMiscRowHeightExport()
262 TestParam::RowData DfltRowData
[] =
264 { 0, 4, 0, 529, 0, false },
265 { 5, 10, 0, 1058, 0, false },
266 { 17, 20, 0, 1767, 0, false },
267 // check last couple of row in document to ensure
268 // they are 5.29mm ( effective default row xlsx height )
269 { 1048573, 1048575, 0, 529, 0, false },
272 TestParam::RowData EmptyRepeatRowData
[] =
274 // rows 0-4, 5-10, 17-20 are all set at various
275 // heights, there is no content in the rows, there
276 // was a bug where only the first row ( of repeated rows )
277 // was set after export
278 { 0, 4, 0, 529, 0, false },
279 { 5, 10, 0, 1058, 0, false },
280 { 17, 20, 0, 1767, 0, false },
283 TestParam aTestValues
[] =
285 // Checks that some distributed ( non-empty ) heights remain set after export (roundtrip)
286 // additionally there is effectively a default row height ( 5.29 mm ). So we test the
287 // unset rows at the end of the document to ensure the effective xlsx default height
289 { "miscrowheights.", XLSX
, XLSX
, SAL_N_ELEMENTS(DfltRowData
), DfltRowData
},
290 // Checks that some distributed ( non-empty ) heights remain set after export (to xls)
291 { "miscrowheights.", XLSX
, XLS
, SAL_N_ELEMENTS(DfltRowData
), DfltRowData
},
292 // Checks that repreated rows ( of various heights ) remain set after export ( to xlsx )
293 { "miscemptyrepeatedrowheights.", ODS
, XLSX
, SAL_N_ELEMENTS(EmptyRepeatRowData
), EmptyRepeatRowData
},
294 // Checks that repreated rows ( of various heights ) remain set after export ( to xls )
295 { "miscemptyrepeatedrowheights.", ODS
, XLS
, SAL_N_ELEMENTS(EmptyRepeatRowData
), EmptyRepeatRowData
},
297 miscRowHeightsTest( aTestValues
, SAL_N_ELEMENTS(aTestValues
) );
301 void ScExportTest::testNamedRangeBugfdo62729()
303 ScDocShellRef xShell
= loadDoc("fdo62729.", ODS
);
304 CPPUNIT_ASSERT(xShell
.Is());
305 ScDocument
* pDoc
= xShell
->GetDocument();
306 CPPUNIT_ASSERT(pDoc
);
308 ScRangeName
* pNames
= pDoc
->GetRangeName();
309 //should be just a single named range
310 CPPUNIT_ASSERT(pNames
->size() == 1 );
312 //should be still a single named range
313 CPPUNIT_ASSERT(pNames
->size() == 1 );
314 ScDocShellRef xDocSh
= saveAndReload(xShell
, ODS
);
317 CPPUNIT_ASSERT(xDocSh
.Is());
318 pDoc
= xDocSh
->GetDocument();
319 CPPUNIT_ASSERT(pDoc
);
321 pNames
= pDoc
->GetRangeName();
322 //after reload should still have a named range
323 CPPUNIT_ASSERT(pNames
->size() == 1 );
328 void ScExportTest::testSheetProtectionXLSX()
330 ScDocShellRef xShell
= loadDoc("ProtecteSheet1234Pass.", XLSX
);
331 CPPUNIT_ASSERT(xShell
.Is());
333 ScDocShellRef xDocSh
= saveAndReload(xShell
, XLSX
);
334 CPPUNIT_ASSERT(xDocSh
.Is());
336 ScDocument
* pDoc
= xDocSh
->GetDocument();
337 CPPUNIT_ASSERT(pDoc
);
338 const ScTableProtection
* pTabProtect
= pDoc
->GetTabProtection(0);
339 CPPUNIT_ASSERT(pTabProtect
);
342 Sequence
<sal_Int8
> aHash
= pTabProtect
->getPasswordHash(PASSHASH_XL
);
344 if (aHash
.getLength() >= 2)
346 CPPUNIT_ASSERT( (sal_uInt8
)aHash
[0] == 204 );
347 CPPUNIT_ASSERT( (sal_uInt8
)aHash
[1] == 61 );
349 // we could flesh out this check I guess
350 CPPUNIT_ASSERT ( !pTabProtect
->isOptionEnabled( ScTableProtection::OBJECTS
) );
351 CPPUNIT_ASSERT ( !pTabProtect
->isOptionEnabled( ScTableProtection::SCENARIOS
) );
356 ScExportTest::ScExportTest()
357 : ScBootstrapFixture("/sc/qa/unit/data")
361 void ScExportTest::setUp()
363 test::BootstrapFixture::setUp();
365 // This is a bit of a fudge, we do this to ensure that ScGlobals::ensure,
366 // which is a private symbol to us, gets called
368 getMultiServiceFactory()->createInstance(OUString("com.sun.star.comp.Calc.SpreadsheetDocument"));
369 CPPUNIT_ASSERT_MESSAGE("no calc component!", m_xCalcComponent
.is());
372 void ScExportTest::tearDown()
374 uno::Reference
< lang::XComponent
>( m_xCalcComponent
, UNO_QUERY_THROW
)->dispose();
375 test::BootstrapFixture::tearDown();
378 CPPUNIT_TEST_SUITE_REGISTRATION(ScExportTest
);
380 CPPUNIT_PLUGIN_IMPLEMENT();
382 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */