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 INCLUDED_SC_QA_UNIT_HELPER_QAHELPER_HXX
11 #define INCLUDED_SC_QA_UNIT_HELPER_QAHELPER_HXX
12 
13 #include <docsh.hxx>
14 #include <address.hxx>
15 
16 #include <cppunit/SourceLine.h>
17 
18 #include <test/bootstrapfixture.hxx>
19 #include <comphelper/documentconstants.hxx>
20 
21 #include <comphelper/fileformat.h>
22 #include <formula/grammar.hxx>
23 
24 #include <string>
25 #include <sstream>
26 
27 #include <sal/types.h>
28 
29 #include <memory>
30 
31 namespace utl { class TempFile; }
32 
33 #if defined(SCQAHELPER_DLLIMPLEMENTATION)
34 #define SCQAHELPER_DLLPUBLIC  SAL_DLLPUBLIC_EXPORT
35 #else
36 #define SCQAHELPER_DLLPUBLIC  SAL_DLLPUBLIC_IMPORT
37 #endif
38 
39 #define ODS_FORMAT_TYPE      (SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::TEMPLATE | SfxFilterFlags::OWN | SfxFilterFlags::DEFAULT | SfxFilterFlags::ENCRYPTION | SfxFilterFlags::PASSWORDTOMODIFY)
40 #define XLS_FORMAT_TYPE      (SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN | SfxFilterFlags::ENCRYPTION | SfxFilterFlags::PASSWORDTOMODIFY | SfxFilterFlags::PREFERED)
41 #define XLSX_FORMAT_TYPE     (SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN | SfxFilterFlags::STARONEFILTER | SfxFilterFlags::PREFERED)
42 #define LOTUS123_FORMAT_TYPE (SfxFilterFlags::IMPORT |                          SfxFilterFlags::ALIEN | SfxFilterFlags::PREFERED)
43 #define CSV_FORMAT_TYPE      (SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN )
44 #define HTML_FORMAT_TYPE     (SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN )
45 #define DIF_FORMAT_TYPE      (SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::ALIEN )
46 #define XLS_XML_FORMAT_TYPE  (SfxFilterFlags::IMPORT | SfxFilterFlags::ALIEN | SfxFilterFlags::PREFERED)
47 #define XLSB_XML_FORMAT_TYPE (SfxFilterFlags::IMPORT |                          SfxFilterFlags::ALIEN | SfxFilterFlags::STARONEFILTER | SfxFilterFlags::PREFERED)
48 #define FODS_FORMAT_TYPE     (SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::OWN | SfxFilterFlags::STARONEFILTER )
49 #define GNUMERIC_FORMAT_TYPE (SfxFilterFlags::IMPORT | SfxFilterFlags::ALIEN | SfxFilterFlags::PREFERED )
50 #define XLTX_FORMAT_TYPE     (SfxFilterFlags::IMPORT | SfxFilterFlags::EXPORT | SfxFilterFlags::TEMPLATE |SfxFilterFlags::ALIEN | SfxFilterFlags::STARONEFILTER | SfxFilterFlags::PREFERED)
51 
52 #define FORMAT_ODS      0
53 #define FORMAT_XLS      1
54 #define FORMAT_XLSX     2
55 #define FORMAT_XLSM     3
56 #define FORMAT_CSV      4
57 #define FORMAT_HTML     5
58 #define FORMAT_LOTUS123 6
59 #define FORMAT_DIF      7
60 #define FORMAT_XLS_XML  8
61 #define FORMAT_XLSB     9
62 #define FORMAT_FODS     10
63 #define FORMAT_GNUMERIC 11
64 #define FORMAT_XLTX     12
65 
66 enum class StringType { PureString, StringValue };
67 
68 SCQAHELPER_DLLPUBLIC bool testEqualsWithTolerance( long nVal1, long nVal2, long nTol );
69 
70 #define CHECK_OPTIMAL 0x1
71 
72 class SdrOle2Obj;
73 class ScRangeList;
74 class ScTokenArray;
75 
76 // data format for row height tests
77 struct TestParam
78 {
79     struct RowData
80     {
81         SCROW const nStartRow;
82         SCROW const nEndRow;
83         SCTAB const nTab;
84         int const nExpectedHeight; // -1 for default height
85         int const nCheck; // currently only CHECK_OPTIMAL ( we could add CHECK_MANUAL etc.)
86         bool const bOptimal;
87     };
88     const char* sTestDoc;
89     int nImportType;
90     int nExportType; // -1 for import test, otherwise this is an export test
91     int nRowData;
92     RowData const * pData;
93 };
94 
95 struct FileFormat {
96     const char* pName; const char* pFilterName; const char* pTypeName; SfxFilterFlags nFormatType;
97 };
98 
99 // Printers for the calc data structures. Needed for the EQUAL assertion
100 // macros from CPPUNIT.
101 
102 SCQAHELPER_DLLPUBLIC std::ostream& operator<<(std::ostream& rStrm, const ScAddress& rAddr);
103 
104 SCQAHELPER_DLLPUBLIC std::ostream& operator<<(std::ostream& rStrm, const ScRange& rRange);
105 
106 SCQAHELPER_DLLPUBLIC std::ostream& operator<<(std::ostream& rStrm, const ScRangeList& rList);
107 
108 SCQAHELPER_DLLPUBLIC std::ostream& operator<<(std::ostream& rStrm, const Color& rColor);
109 
110 SCQAHELPER_DLLPUBLIC std::ostream& operator<<(std::ostream& rStrm, const OpCode& rCode);
111 
112 // Why is this here and not in osl, and using the already existing file
113 // handling APIs? Do we really want to add arbitrary new file handling
114 // wrappers here and there (and then having to handle the Android (and
115 // eventually perhaps iOS) special cases here, too)?  Please move this to osl,
116 // it sure looks generally useful. Or am I missing something?
117 
118 SCQAHELPER_DLLPUBLIC void loadFile(const OUString& aFileName, std::string& aContent);
119 
120 SCQAHELPER_DLLPUBLIC void testFile(const OUString& aFileName, ScDocument& rDoc, SCTAB nTab, StringType aStringFormat = StringType::StringValue);
121 
122 //need own handler because conditional formatting strings must be generated
123 SCQAHELPER_DLLPUBLIC void testCondFile(const OUString& aFileName, ScDocument* pDoc, SCTAB nTab);
124 
125 SCQAHELPER_DLLPUBLIC const SdrOle2Obj* getSingleOleObject(ScDocument& rDoc, sal_uInt16 nPage);
126 
127 SCQAHELPER_DLLPUBLIC const SdrOle2Obj* getSingleChartObject(ScDocument& rDoc, sal_uInt16 nPage);
128 
129 SCQAHELPER_DLLPUBLIC std::vector<OUString> getChartRangeRepresentations(const SdrOle2Obj& rChartObj);
130 
131 SCQAHELPER_DLLPUBLIC ScRangeList getChartRanges(ScDocument& rDoc, const SdrOle2Obj& rChartObj);
132 
133 SCQAHELPER_DLLPUBLIC bool checkFormula(ScDocument& rDoc, const ScAddress& rPos, const char* pExpected);
134 
135 SCQAHELPER_DLLPUBLIC bool checkFormulaPosition(ScDocument& rDoc, const ScAddress& rPos);
136 SCQAHELPER_DLLPUBLIC bool checkFormulaPositions(
137     ScDocument& rDoc, SCTAB nTab, SCCOL nCol, const SCROW* pRows, size_t nRowCount);
138 
139 SCQAHELPER_DLLPUBLIC std::unique_ptr<ScTokenArray> compileFormula(
140     ScDocument* pDoc, const OUString& rFormula,
141     formula::FormulaGrammar::Grammar eGram = formula::FormulaGrammar::GRAM_NATIVE );
142 
143 SCQAHELPER_DLLPUBLIC bool checkOutput(
144     const ScDocument* pDoc, const ScRange& aOutRange,
145     const std::vector<std::vector<const char*>>& aCheck, const char* pCaption );
146 
147 SCQAHELPER_DLLPUBLIC void clearFormulaCellChangedFlag( ScDocument& rDoc, const ScRange& rRange );
148 
149 /**
150  * Check if the cell at specified position is a formula cell that doesn't
151  * have an error value.
152  */
153 SCQAHELPER_DLLPUBLIC bool isFormulaWithoutError(ScDocument& rDoc, const ScAddress& rPos);
154 
155 /**
156  * Convert formula token array to a formula string.
157  */
158 SCQAHELPER_DLLPUBLIC OUString toString(
159     ScDocument& rDoc, const ScAddress& rPos, ScTokenArray& rArray,
160     formula::FormulaGrammar::Grammar eGram);
161 
print(const ScAddress & rAddr)162 inline std::string print(const ScAddress& rAddr)
163 {
164     std::ostringstream str;
165     str << "Col: " << rAddr.Col();
166     str << " Row: " << rAddr.Row();
167     str << " Tab: " << rAddr.Tab();
168     return str.str();
169 }
170 
171 class SCQAHELPER_DLLPUBLIC ScBootstrapFixture : public test::BootstrapFixture
172 {
173     static const FileFormat aFileFormats[];
174 protected:
175     OUString const m_aBaseString;
176 
177     ScDocShellRef load(
178         bool bReadWrite, const OUString& rURL, const OUString& rFilter, const OUString &rUserData,
179         const OUString& rTypeName, SfxFilterFlags nFilterFlags, SotClipboardFormatId nClipboardID,
180         sal_uIntPtr nFilterVersion = SOFFICE_FILEFORMAT_CURRENT, const OUString* pPassword = nullptr );
181 
182     ScDocShellRef load(
183         const OUString& rURL, const OUString& rFilter, const OUString &rUserData,
184         const OUString& rTypeName, SfxFilterFlags nFilterFlags, SotClipboardFormatId nClipboardID,
185         sal_uIntPtr nFilterVersion = SOFFICE_FILEFORMAT_CURRENT, const OUString* pPassword = nullptr );
186 
187     ScDocShellRef loadDoc(const OUString& rFileName, sal_Int32 nFormat, bool bReadWrite = false );
188 
189 public:
getFileFormats()190     static const FileFormat* getFileFormats() { return aFileFormats; }
191 
192     explicit ScBootstrapFixture( const OUString& rsBaseString );
193     virtual ~ScBootstrapFixture() override;
194 
195     void createFileURL(const OUString& aFileBase, const OUString& aFileExtension, OUString& rFilePath);
196 
197     void createCSVPath(const OUString& aFileBase, OUString& rCSVPath);
198 
199     ScDocShellRef saveAndReload(ScDocShell* pShell, const OUString &rFilter,
200     const OUString &rUserData, const OUString& rTypeName, SfxFilterFlags nFormatType);
201 
202     ScDocShellRef saveAndReload( ScDocShell* pShell, sal_Int32 nFormat );
203 
204     std::shared_ptr<utl::TempFile> saveAs(ScDocShell* pShell, sal_Int32 nFormat);
205     std::shared_ptr<utl::TempFile> exportTo(ScDocShell* pShell, sal_Int32 nFormat);
206 
207     void miscRowHeightsTest( TestParam const * aTestValues, unsigned int numElems );
208 };
209 
210 #define ASSERT_DOUBLES_EQUAL( expected, result )    \
211     CPPUNIT_ASSERT_DOUBLES_EQUAL( (expected), (result), 1e-14 )
212 
213 #define ASSERT_DOUBLES_EQUAL_MESSAGE( message, expected, result )   \
214     CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( (message), (expected), (result), 1e-14 )
215 
216 SCQAHELPER_DLLPUBLIC void checkFormula(ScDocument& rDoc, const ScAddress& rPos,
217         const char* expected, const char* msg, CppUnit::SourceLine const & sourceLine);
218 
219 #define ASSERT_FORMULA_EQUAL(doc, pos, expected, msg) \
220     checkFormula(doc, pos, expected, msg, CPPUNIT_SOURCELINE())
221 
222 SCQAHELPER_DLLPUBLIC void testFormats(ScBootstrapFixture* pTest, ScDocument* pDoc, sal_Int32 nFormat);
223 
224 SCQAHELPER_DLLPUBLIC ScTokenArray* getTokens(ScDocument& rDoc, const ScAddress& rPos);
225 
226 SCQAHELPER_DLLPUBLIC std::string to_std_string(const OUString& rStr);
227 
228 #endif
229 
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
231