bump product version to 5.0.4.1
[LibreOffice.git] / sc / qa / unit / helper / qahelper.hxx
blob829c127014f0abae39c8398b71a2fb3ff429942a
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 INCLUDED_SC_QA_UNIT_HELPER_QAHELPER_HXX
11 #define INCLUDED_SC_QA_UNIT_HELPER_QAHELPER_HXX
13 #include "scdllapi.h"
14 #include "debughelper.hxx"
15 #include "docsh.hxx"
16 #include "address.hxx"
18 #include <test/bootstrapfixture.hxx>
19 #include <comphelper/documentconstants.hxx>
21 #include <osl/detail/android-bootstrap.h>
23 #include <unotools/tempfile.hxx>
24 #include <comphelper/storagehelper.hxx>
25 #include <sfx2/docfilt.hxx>
26 #include <sfx2/docfile.hxx>
27 #include <svl/stritem.hxx>
28 #include <formula/grammar.hxx>
30 #include <string>
31 #include <sstream>
33 #include <sal/types.h>
35 #include <boost/shared_ptr.hpp>
37 #if defined(SCQAHELPER_DLLIMPLEMENTATION)
38 #define SCQAHELPER_DLLPUBLIC SAL_DLLPUBLIC_EXPORT
39 #else
40 #define SCQAHELPER_DLLPUBLIC SAL_DLLPUBLIC_IMPORT
41 #endif
43 #define ODS_FORMAT_TYPE (SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::TEMPLATE | SfxFilterFlags::OWN | SfxFilterFlags::DEFAULT | SfxFilterFlags::ENCRYPTION | SfxFilterFlags::PASSWORDTOMODIFY)
44 #define XLS_FORMAT_TYPE (SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN | SfxFilterFlags::ENCRYPTION | SfxFilterFlags::PASSWORDTOMODIFY | SfxFilterFlags::PREFERED)
45 #define XLSX_FORMAT_TYPE (SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN | SfxFilterFlags::STARONEFILTER | SfxFilterFlags::PREFERED)
46 #define LOTUS123_FORMAT_TYPE (SfxFilterFlags::IMPORT | SfxFilterFlags::ALIEN | SfxFilterFlags::USESOPTIONS | SfxFilterFlags::PREFERED)
47 #define CSV_FORMAT_TYPE (SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN | SfxFilterFlags::USESOPTIONS)
48 #define HTML_FORMAT_TYPE (SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN | SfxFilterFlags::USESOPTIONS)
49 #define DIF_FORMAT_TYPE (SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN | SfxFilterFlags::USESOPTIONS)
50 #define XLS_XML_FORMAT_TYPE (SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN)
51 #define XLSB_XML_FORMAT_TYPE (SfxFilterFlags::IMPORT | SfxFilterFlags::ALIEN | SfxFilterFlags::STARONEFILTER | SfxFilterFlags::PREFERED)
53 #define ODS 0
54 #define XLS 1
55 #define XLSX 2
56 #define XLSM 3
57 #define CSV 4
58 #define HTML 5
59 #define LOTUS123 6
60 #define DIF 7
61 #define XLS_XML 8
62 #define XLSB 9
64 enum StringType { PureString, FormulaValue, StringValue };
66 SCQAHELPER_DLLPUBLIC bool testEqualsWithTolerance( long nVal1, long nVal2, long nTol );
68 #define CHECK_OPTIMAL 0x1
70 class SdrOle2Obj;
71 class ScRangeList;
72 class ScTokenArray;
74 // data format for row height tests
75 struct TestParam
77 struct RowData
79 SCROW nStartRow;
80 SCROW nEndRow;
81 SCTAB nTab;
82 int nExpectedHeight; // -1 for default height
83 int nCheck; // currently only CHECK_OPTIMAL ( we could add CHECK_MANUAL etc.)
84 bool bOptimal;
86 const char* sTestDoc;
87 int nImportType;
88 int nExportType; // -1 for import test, otherwise this is an export test
89 int nRowData;
90 RowData* pData;
93 struct FileFormat {
94 const char* pName; const char* pFilterName; const char* pTypeName; SfxFilterFlags nFormatType;
97 // Printers for the calc data structures. Needed for the EQUAL assertion
98 // macros from CPPUNIT.
100 SCQAHELPER_DLLPUBLIC std::ostream& operator<<(std::ostream& rStrm, const ScAddress& rAddr);
102 SCQAHELPER_DLLPUBLIC std::ostream& operator<<(std::ostream& rStrm, const ScRange& rRange);
104 SCQAHELPER_DLLPUBLIC std::ostream& operator<<(std::ostream& rStrm, const ScRangeList& rList);
106 SCQAHELPER_DLLPUBLIC std::ostream& operator<<(std::ostream& rStrm, const Color& rColor);
108 SCQAHELPER_DLLPUBLIC std::ostream& operator<<(std::ostream& rStrm, const OpCode& rCode);
110 // Why is this here and not in osl, and using the already existing file
111 // handling APIs? Do we really want to add arbitrary new file handling
112 // wrappers here and there (and then having to handle the Android (and
113 // eventually perhaps iOS) special cases here, too)? Please move this to osl,
114 // it sure looks gemerally useful. Or am I missing something?
116 SCQAHELPER_DLLPUBLIC void loadFile(const OUString& aFileName, std::string& aContent);
118 SCQAHELPER_DLLPUBLIC void testFile(OUString& aFileName, ScDocument& rDoc, SCTAB nTab, StringType aStringFormat = StringValue);
120 //need own handler because conditional formatting strings must be generated
121 SCQAHELPER_DLLPUBLIC void testCondFile(OUString& aFileName, ScDocument* pDoc, SCTAB nTab);
123 SCQAHELPER_DLLPUBLIC const SdrOle2Obj* getSingleChartObject(ScDocument& rDoc, sal_uInt16 nPage);
125 SCQAHELPER_DLLPUBLIC std::vector<OUString> getChartRangeRepresentations(const SdrOle2Obj& rChartObj);
127 SCQAHELPER_DLLPUBLIC ScRangeList getChartRanges(ScDocument& rDoc, const SdrOle2Obj& rChartObj);
129 SCQAHELPER_DLLPUBLIC bool checkFormula(ScDocument& rDoc, const ScAddress& rPos, const char* pExpected);
131 SCQAHELPER_DLLPUBLIC bool checkFormulaPosition(ScDocument& rDoc, const ScAddress& rPos);
132 SCQAHELPER_DLLPUBLIC bool checkFormulaPositions(
133 ScDocument& rDoc, SCTAB nTab, SCCOL nCol, const SCROW* pRows, size_t nRowCount);
135 SCQAHELPER_DLLPUBLIC ScTokenArray* compileFormula(
136 ScDocument* pDoc, const OUString& rFormula, const ScAddress* pPos = NULL,
137 formula::FormulaGrammar::Grammar eGram = formula::FormulaGrammar::GRAM_NATIVE );
139 template<size_t _Size>
140 bool checkOutput(ScDocument* pDoc, const ScRange& aOutRange, const char* aOutputCheck[][_Size], const char* pCaption)
142 bool bResult = true;
143 const ScAddress& s = aOutRange.aStart;
144 const ScAddress& e = aOutRange.aEnd;
145 svl::GridPrinter printer(e.Row() - s.Row() + 1, e.Col() - s.Col() + 1, CALC_DEBUG_OUTPUT != 0);
146 SCROW nOutRowSize = e.Row() - s.Row() + 1;
147 SCCOL nOutColSize = e.Col() - s.Col() + 1;
148 for (SCROW nRow = 0; nRow < nOutRowSize; ++nRow)
150 for (SCCOL nCol = 0; nCol < nOutColSize; ++nCol)
152 OUString aVal = pDoc->GetString(nCol + s.Col(), nRow + s.Row(), s.Tab());
153 printer.set(nRow, nCol, aVal);
154 const char* p = aOutputCheck[nRow][nCol];
155 if (p)
157 OUString aCheckVal = OUString::createFromAscii(p);
158 bool bEqual = aCheckVal.equals(aVal);
159 if (!bEqual)
161 cout << "Expected: " << aCheckVal << " Actual: " << aVal << endl;
162 bResult = false;
165 else if (!aVal.isEmpty())
167 cout << "Empty cell expected" << endl;
168 bResult = false;
172 printer.print(pCaption);
173 return bResult;
176 SCQAHELPER_DLLPUBLIC void clearFormulaCellChangedFlag( ScDocument& rDoc, const ScRange& rRange );
179 * Check if the cell at specified position is a formula cell that doesn't
180 * have an error value.
182 SCQAHELPER_DLLPUBLIC bool isFormulaWithoutError(ScDocument& rDoc, const ScAddress& rPos);
185 * Convert formula token array to a formula string.
187 SCQAHELPER_DLLPUBLIC OUString toString(
188 ScDocument& rDoc, const ScAddress& rPos, ScTokenArray& rArray,
189 formula::FormulaGrammar::Grammar eGram = formula::FormulaGrammar::GRAM_NATIVE);
191 inline std::string print(const ScAddress& rAddr)
193 std::ostringstream str;
194 str << "Col: " << rAddr.Col();
195 str << " Row: " << rAddr.Row();
196 str << " Tab: " << rAddr.Tab();
197 return str.str();
200 namespace CppUnit {
202 template<>
203 struct assertion_traits<ScRange>
205 static bool equal( const ScRange& x, const ScRange& y )
207 return x == y;
210 static std::string toString( const ScRange& x )
212 std::stringstream str;
213 str << "Start: " << print(x.aStart);
214 str << "\nEnd: " << print(x.aEnd);
215 return str.str();
221 class SCQAHELPER_DLLPUBLIC ScBootstrapFixture : public test::BootstrapFixture
223 static const FileFormat aFileFormats[];
224 protected:
225 OUString m_aBaseString;
227 ScDocShellRef load(
228 bool bReadWrite, const OUString& rURL, const OUString& rFilter, const OUString &rUserData,
229 const OUString& rTypeName, SfxFilterFlags nFilterFlags, SotClipboardFormatId nClipboardID,
230 sal_uIntPtr nFilterVersion = SOFFICE_FILEFORMAT_CURRENT, const OUString* pPassword = NULL );
232 ScDocShellRef load(
233 const OUString& rURL, const OUString& rFilter, const OUString &rUserData,
234 const OUString& rTypeName, SfxFilterFlags nFilterFlags, SotClipboardFormatId nClipboardID,
235 sal_uIntPtr nFilterVersion = SOFFICE_FILEFORMAT_CURRENT, const OUString* pPassword = NULL );
237 ScDocShellRef loadDoc(const OUString& rFileName, sal_Int32 nFormat, bool bReadWrite = false );
239 public:
240 static const FileFormat* getFileFormats() { return aFileFormats; }
242 ScBootstrapFixture( const OUString& rsBaseString );
243 virtual ~ScBootstrapFixture();
245 void createFileURL(const OUString& aFileBase, const OUString& aFileExtension, OUString& rFilePath);
247 void createCSVPath(const OUString& aFileBase, OUString& rCSVPath);
249 ScDocShellRef saveAndReload(ScDocShell* pShell, const OUString &rFilter,
250 const OUString &rUserData, const OUString& rTypeName, SfxFilterFlags nFormatType);
252 ScDocShellRef saveAndReload( ScDocShell* pShell, sal_Int32 nFormat );
254 static boost::shared_ptr<utl::TempFile> exportTo( ScDocShell* pShell, sal_Int32 nFormat );
256 void miscRowHeightsTest( TestParam* aTestValues, unsigned int numElems );
259 #define ASSERT_DOUBLES_EQUAL( expected, result ) \
260 CPPUNIT_ASSERT_DOUBLES_EQUAL( (expected), (result), 1e-14 )
262 #define ASSERT_DOUBLES_EQUAL_MESSAGE( message, expected, result ) \
263 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( (message), (expected), (result), 1e-14 )
265 #define ASSERT_EQUAL_TYPE( type, expected, result ) \
266 CPPUNIT_ASSERT_EQUAL( static_cast<type>(expected), static_cast<type>(result) );
268 SCQAHELPER_DLLPUBLIC void testFormats(ScBootstrapFixture* pTest, ScDocument* pDoc, sal_Int32 nFormat);
270 #endif
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */