Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / qa / unit / SparklineImportExportTest.cxx
blob67b139052c82e1de3a1720ed4ff89221fc698ae8
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 #include <test/unoapixml_test.hxx>
12 #include <com/sun/star/lang/XComponent.hpp>
13 #include <comphelper/servicehelper.hxx>
14 #include <docsh.hxx>
15 #include <Sparkline.hxx>
16 #include <SparklineGroup.hxx>
18 using namespace css;
20 /** Test import, export or roundtrip of sparklines for ODF and OOXML */
21 class SparklineImportExportTest : public UnoApiXmlTest
23 public:
24 SparklineImportExportTest()
25 : UnoApiXmlTest("sc/qa/unit/data")
29 void testSparklinesRoundtripXLSX();
30 void testSparklinesExportODS();
31 void testSparklinesRoundtripODS();
32 void testNoSparklinesInDocumentXLSX();
33 void testSparklinesRoundtripThemeColorsODS();
34 void testSparklinesRoundtripThemeColorsOOXML();
36 CPPUNIT_TEST_SUITE(SparklineImportExportTest);
37 CPPUNIT_TEST(testSparklinesRoundtripXLSX);
38 CPPUNIT_TEST(testSparklinesExportODS);
39 CPPUNIT_TEST(testSparklinesRoundtripODS);
40 CPPUNIT_TEST(testNoSparklinesInDocumentXLSX);
41 CPPUNIT_TEST(testSparklinesRoundtripThemeColorsODS);
42 CPPUNIT_TEST(testSparklinesRoundtripThemeColorsOOXML);
43 CPPUNIT_TEST_SUITE_END();
46 namespace
48 void checkSparklines(ScDocument& rDocument)
50 // Sparkline at Sheet1:A2
52 auto pSparkline = rDocument.GetSparkline(ScAddress(0, 1, 0)); // A2
53 CPPUNIT_ASSERT(pSparkline);
54 CPPUNIT_ASSERT_EQUAL("{1C5C5DE0-3C09-4CB3-A3EC-9E763301EC82}"_ostr,
55 pSparkline->getSparklineGroup()->getID().getString());
57 auto& rAttributes = pSparkline->getSparklineGroup()->getAttributes();
58 CPPUNIT_ASSERT_EQUAL(sc::SparklineType::Line, rAttributes.getType());
60 CPPUNIT_ASSERT_EQUAL(Color(0x376092), rAttributes.getColorSeries().getFinalColor());
61 CPPUNIT_ASSERT_EQUAL(Color(0x00b050), rAttributes.getColorNegative().getFinalColor());
62 CPPUNIT_ASSERT_EQUAL(COL_BLACK, rAttributes.getColorAxis().getFinalColor());
63 CPPUNIT_ASSERT_EQUAL(COL_BLACK, rAttributes.getColorMarkers().getFinalColor());
64 CPPUNIT_ASSERT_EQUAL(Color(0x7030a0), rAttributes.getColorFirst().getFinalColor());
65 CPPUNIT_ASSERT_EQUAL(Color(0xff0000), rAttributes.getColorLast().getFinalColor());
66 CPPUNIT_ASSERT_EQUAL(Color(0x92d050), rAttributes.getColorHigh().getFinalColor());
67 CPPUNIT_ASSERT_EQUAL(Color(0x00b0f0), rAttributes.getColorLow().getFinalColor());
69 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, rAttributes.getLineWeight(), 1E-2);
70 CPPUNIT_ASSERT_EQUAL(false, rAttributes.isDateAxis());
71 CPPUNIT_ASSERT_EQUAL(sc::DisplayEmptyCellsAs::Gap, rAttributes.getDisplayEmptyCellsAs());
73 CPPUNIT_ASSERT_EQUAL(true, rAttributes.isMarkers());
74 CPPUNIT_ASSERT_EQUAL(true, rAttributes.isHigh());
75 CPPUNIT_ASSERT_EQUAL(true, rAttributes.isLow());
76 CPPUNIT_ASSERT_EQUAL(true, rAttributes.isFirst());
77 CPPUNIT_ASSERT_EQUAL(true, rAttributes.isLast());
78 CPPUNIT_ASSERT_EQUAL(true, rAttributes.isNegative());
79 CPPUNIT_ASSERT_EQUAL(true, rAttributes.shouldDisplayXAxis());
80 CPPUNIT_ASSERT_EQUAL(false, rAttributes.shouldDisplayHidden());
81 CPPUNIT_ASSERT_EQUAL(false, rAttributes.isRightToLeft());
83 CPPUNIT_ASSERT_EQUAL(false, bool(rAttributes.getManualMax()));
84 CPPUNIT_ASSERT_EQUAL(false, bool(rAttributes.getManualMin()));
86 // Sparkline at Sheet1:A3
88 auto pSparkline = rDocument.GetSparkline(ScAddress(0, 2, 0)); // A3
89 CPPUNIT_ASSERT(pSparkline);
90 auto& rAttributes = pSparkline->getSparklineGroup()->getAttributes();
91 CPPUNIT_ASSERT_EQUAL(sc::SparklineType::Column, rAttributes.getType());
93 CPPUNIT_ASSERT_EQUAL(Color(0x376092), rAttributes.getColorSeries().getFinalColor());
94 CPPUNIT_ASSERT_EQUAL(Color(0xff0000), rAttributes.getColorNegative().getFinalColor());
95 CPPUNIT_ASSERT_EQUAL(COL_BLACK, rAttributes.getColorAxis().getFinalColor());
96 CPPUNIT_ASSERT_EQUAL(Color(0xd00000), rAttributes.getColorMarkers().getFinalColor());
97 CPPUNIT_ASSERT_EQUAL(Color(0x92d050), rAttributes.getColorFirst().getFinalColor());
98 CPPUNIT_ASSERT_EQUAL(Color(0x00b0f0), rAttributes.getColorLast().getFinalColor());
99 CPPUNIT_ASSERT_EQUAL(Color(0x7030a0), rAttributes.getColorHigh().getFinalColor());
100 CPPUNIT_ASSERT_EQUAL(Color(0xffc000), rAttributes.getColorLow().getFinalColor());
102 CPPUNIT_ASSERT_EQUAL(0.75, rAttributes.getLineWeight());
103 CPPUNIT_ASSERT_EQUAL(false, rAttributes.isDateAxis());
104 CPPUNIT_ASSERT_EQUAL(sc::DisplayEmptyCellsAs::Gap, rAttributes.getDisplayEmptyCellsAs());
106 CPPUNIT_ASSERT_EQUAL(false, rAttributes.isMarkers());
107 CPPUNIT_ASSERT_EQUAL(true, rAttributes.isHigh());
108 CPPUNIT_ASSERT_EQUAL(true, rAttributes.isLow());
109 CPPUNIT_ASSERT_EQUAL(true, rAttributes.isFirst());
110 CPPUNIT_ASSERT_EQUAL(true, rAttributes.isLast());
111 CPPUNIT_ASSERT_EQUAL(true, rAttributes.isNegative());
112 CPPUNIT_ASSERT_EQUAL(false, rAttributes.shouldDisplayXAxis());
113 CPPUNIT_ASSERT_EQUAL(false, rAttributes.shouldDisplayHidden());
114 CPPUNIT_ASSERT_EQUAL(false, rAttributes.isRightToLeft());
116 CPPUNIT_ASSERT_EQUAL(false, bool(rAttributes.getManualMax()));
117 CPPUNIT_ASSERT_EQUAL(false, bool(rAttributes.getManualMin()));
119 // Sparkline at Sheet2:B1
121 auto pSparkline = rDocument.GetSparkline(ScAddress(1, 0, 1)); //B1
122 CPPUNIT_ASSERT(pSparkline);
123 auto& rAttributes = pSparkline->getSparklineGroup()->getAttributes();
124 CPPUNIT_ASSERT_EQUAL(sc::SparklineType::Column, rAttributes.getType());
126 // Sparkline at Sheet2:B2
128 auto pSparkline = rDocument.GetSparkline(ScAddress(1, 1, 1)); //B2
129 CPPUNIT_ASSERT(pSparkline);
130 auto& rAttributes = pSparkline->getSparklineGroup()->getAttributes();
131 CPPUNIT_ASSERT_EQUAL(sc::SparklineType::Line, rAttributes.getType());
133 // Sparkline at Sheet2:B2
135 auto pSparkline = rDocument.GetSparkline(ScAddress(1, 1, 1)); //B2
136 CPPUNIT_ASSERT(pSparkline);
137 auto& rAttributes = pSparkline->getSparklineGroup()->getAttributes();
138 CPPUNIT_ASSERT_EQUAL(sc::SparklineType::Line, rAttributes.getType());
140 // Sparkline doesn't exists at A4
142 auto pSparkline = rDocument.GetSparkline(ScAddress(0, 3, 0)); //A4
143 CPPUNIT_ASSERT(!pSparkline);
146 } // end anonymous namespace
148 void SparklineImportExportTest::testSparklinesRoundtripXLSX()
150 loadFromFile(u"xlsx/Sparklines.xlsx");
151 ScModelObj* pModelObj = comphelper::getFromUnoTunnel<ScModelObj>(mxComponent);
152 CPPUNIT_ASSERT(pModelObj);
154 checkSparklines(*pModelObj->GetDocument());
156 saveAndReload("Calc Office Open XML");
157 pModelObj = comphelper::getFromUnoTunnel<ScModelObj>(mxComponent);
158 CPPUNIT_ASSERT(pModelObj);
160 checkSparklines(*pModelObj->GetDocument());
163 void SparklineImportExportTest::testSparklinesExportODS()
165 // Load the document containing sparklines
166 loadFromFile(u"xlsx/Sparklines.xlsx");
168 // Save as ODS and check content.xml with XPath
169 save("calc8");
170 xmlDocUniquePtr pXmlDoc = parseExport("content.xml");
172 // We have 3 sparkline groups = 3 tables that contain sparklines
173 assertXPath(pXmlDoc, "//table:table/calcext:sparkline-groups"_ostr, 3);
175 // Check the number of sparkline groups in table[1]
176 assertXPath(pXmlDoc, "//table:table[1]/calcext:sparkline-groups/calcext:sparkline-group"_ostr,
178 // Check the number of sparkline groups in table[2]
179 assertXPath(pXmlDoc, "//table:table[2]/calcext:sparkline-groups/calcext:sparkline-group"_ostr,
181 // Check the number of sparkline groups in table[3]
182 assertXPath(pXmlDoc, "//table:table[3]/calcext:sparkline-groups/calcext:sparkline-group"_ostr,
185 // Check table[1] - sparkline-group[1]
186 OString aSparklineGroupPath
187 = "//table:table[1]/calcext:sparkline-groups/calcext:sparkline-group[1]"_ostr;
188 assertXPath(pXmlDoc, aSparklineGroupPath, "type"_ostr, "line");
189 assertXPath(pXmlDoc, aSparklineGroupPath, "line-width"_ostr, "1pt");
190 assertXPath(pXmlDoc, aSparklineGroupPath, "display-empty-cells-as"_ostr, "gap");
191 assertXPath(pXmlDoc, aSparklineGroupPath, "markers"_ostr, "true");
192 assertXPath(pXmlDoc, aSparklineGroupPath, "high"_ostr, "true");
193 assertXPath(pXmlDoc, aSparklineGroupPath, "low"_ostr, "true");
194 assertXPath(pXmlDoc, aSparklineGroupPath, "first"_ostr, "true");
195 assertXPath(pXmlDoc, aSparklineGroupPath, "last"_ostr, "true");
196 assertXPath(pXmlDoc, aSparklineGroupPath, "negative"_ostr, "true");
197 assertXPath(pXmlDoc, aSparklineGroupPath, "display-x-axis"_ostr, "true");
198 assertXPath(pXmlDoc, aSparklineGroupPath, "min-axis-type"_ostr, "individual");
199 assertXPath(pXmlDoc, aSparklineGroupPath, "max-axis-type"_ostr, "individual");
200 assertXPath(pXmlDoc, aSparklineGroupPath, "color-series"_ostr, "#376092");
201 assertXPath(pXmlDoc, aSparklineGroupPath, "color-negative"_ostr, "#00b050");
202 assertXPath(pXmlDoc, aSparklineGroupPath, "color-axis"_ostr, "#000000");
203 assertXPath(pXmlDoc, aSparklineGroupPath, "color-markers"_ostr, "#000000");
204 assertXPath(pXmlDoc, aSparklineGroupPath, "color-first"_ostr, "#7030a0");
205 assertXPath(pXmlDoc, aSparklineGroupPath, "color-last"_ostr, "#ff0000");
206 assertXPath(pXmlDoc, aSparklineGroupPath, "color-high"_ostr, "#92d050");
207 assertXPath(pXmlDoc, aSparklineGroupPath, "color-low"_ostr, "#00b0f0");
209 assertXPath(pXmlDoc, aSparklineGroupPath + "/calcext:sparklines/calcext:sparkline", 1);
210 assertXPath(pXmlDoc, aSparklineGroupPath + "/calcext:sparklines/calcext:sparkline[1]",
211 "cell-address"_ostr, "Sheet1.A2");
214 void SparklineImportExportTest::testSparklinesRoundtripODS()
216 loadFromFile(u"xlsx/Sparklines.xlsx");
217 ScModelObj* pModelObj = comphelper::getFromUnoTunnel<ScModelObj>(mxComponent);
218 CPPUNIT_ASSERT(pModelObj);
220 checkSparklines(*pModelObj->GetDocument());
222 // Trigger export and import of sparklines
223 saveAndReload("calc8");
224 pModelObj = comphelper::getFromUnoTunnel<ScModelObj>(mxComponent);
225 CPPUNIT_ASSERT(pModelObj);
227 checkSparklines(*pModelObj->GetDocument());
230 void SparklineImportExportTest::testNoSparklinesInDocumentXLSX()
232 // tdf#148835
233 // Check no sparkline elements are written when there are none in the document
235 // Load the document containing NO sparklines
236 loadFromFile(u"xlsx/empty.xlsx");
238 save("Calc Office Open XML");
239 xmlDocUniquePtr pXmlDoc = parseExport("xl/worksheets/sheet1.xml");
240 CPPUNIT_ASSERT(pXmlDoc);
242 assertXPath(pXmlDoc, "/x:worksheet"_ostr, 1);
243 assertXPath(pXmlDoc, "/x:worksheet/x:extLst/x:ext/x14:sparklineGroups"_ostr, 0);
244 assertXPath(pXmlDoc, "/x:worksheet/x:extLst/x:ext"_ostr, 0);
245 assertXPath(pXmlDoc, "/x:worksheet/x:extLst"_ostr, 0);
248 namespace
250 void checkSparklineThemeColors(ScDocument& rDocument)
252 auto pSparkline = rDocument.GetSparkline(ScAddress(0, 1, 0)); // A2
253 CPPUNIT_ASSERT(pSparkline);
254 CPPUNIT_ASSERT_EQUAL("{1C5C5DE0-3C09-4CB3-A3EC-9E763301EC82}"_ostr,
255 pSparkline->getSparklineGroup()->getID().getString());
257 auto& rAttributes = pSparkline->getSparklineGroup()->getAttributes();
258 CPPUNIT_ASSERT_EQUAL(sc::SparklineType::Column, rAttributes.getType());
260 CPPUNIT_ASSERT_EQUAL(model::ThemeColorType::Accent3,
261 rAttributes.getColorSeries().getThemeColorType());
262 CPPUNIT_ASSERT_EQUAL(model::ThemeColorType::Accent6,
263 rAttributes.getColorNegative().getThemeColorType());
264 CPPUNIT_ASSERT_EQUAL(model::ColorType::Unused, rAttributes.getColorAxis().getType());
265 CPPUNIT_ASSERT_EQUAL(model::ThemeColorType::Light1,
266 rAttributes.getColorMarkers().getThemeColorType());
267 CPPUNIT_ASSERT_EQUAL(model::ThemeColorType::Accent5,
268 rAttributes.getColorFirst().getThemeColorType());
269 CPPUNIT_ASSERT_EQUAL(model::ThemeColorType::Accent2,
270 rAttributes.getColorLast().getThemeColorType());
271 CPPUNIT_ASSERT_EQUAL(model::ThemeColorType::Accent1,
272 rAttributes.getColorHigh().getThemeColorType());
273 CPPUNIT_ASSERT_EQUAL(model::ThemeColorType::Accent4,
274 rAttributes.getColorLow().getThemeColorType());
276 } // end anonymous namespace
278 void SparklineImportExportTest::testSparklinesRoundtripThemeColorsODS()
280 loadFromFile(u"fods/Sparklines.fods");
282 ScModelObj* pModelObj = comphelper::getFromUnoTunnel<ScModelObj>(mxComponent);
283 CPPUNIT_ASSERT(pModelObj);
284 checkSparklineThemeColors(*pModelObj->GetDocument());
286 saveAndReload("calc8");
288 pModelObj = comphelper::getFromUnoTunnel<ScModelObj>(mxComponent);
289 CPPUNIT_ASSERT(pModelObj);
290 checkSparklineThemeColors(*pModelObj->GetDocument());
293 void SparklineImportExportTest::testSparklinesRoundtripThemeColorsOOXML()
295 loadFromFile(u"fods/Sparklines.fods");
296 saveAndReload("Calc Office Open XML");
298 ScModelObj* pModelObj = comphelper::getFromUnoTunnel<ScModelObj>(mxComponent);
299 CPPUNIT_ASSERT(pModelObj);
300 checkSparklineThemeColors(*pModelObj->GetDocument());
303 CPPUNIT_TEST_SUITE_REGISTRATION(SparklineImportExportTest);
305 CPPUNIT_PLUGIN_IMPLEMENT();
307 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */