nss: upgrade to release 3.73
[LibreOffice.git] / sc / qa / extras / new_cond_format.cxx
blobcaebb4b4a3dbe670f4a7b8477b298e958939d3b0
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/calc_unoapi_test.hxx>
12 #include <com/sun/star/beans/XPropertySet.hpp>
13 #include <com/sun/star/sheet/XConditionalFormats.hpp>
14 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
15 #include <com/sun/star/sheet/XSpreadsheet.hpp>
16 #include <com/sun/star/sheet/DataBarAxis.hpp>
17 #include <com/sun/star/sheet/XDataBarEntry.hpp>
18 #include <com/sun/star/sheet/DataBarEntryType.hpp>
19 #include <com/sun/star/sheet/ColorScaleEntryType.hpp>
20 #include <com/sun/star/sheet/XColorScaleEntry.hpp>
22 using namespace css;
24 static std::ostream& operator<<(std::ostream& rStrm, const Color& rColor)
26 rStrm << "Color: R:" << static_cast<int>(rColor.GetRed()) << " G:" << static_cast<int>(rColor.GetGreen()) << " B: " << static_cast<int>(rColor.GetBlue());
27 return rStrm;
30 namespace sc_apitest {
32 class ScConditionalFormatTest : public CalcUnoApiTest
34 public:
35 ScConditionalFormatTest();
37 virtual void setUp() override;
38 virtual void tearDown() override;
40 uno::Reference< uno::XInterface > init(sal_Int32 nIndex = 0);
41 void testRequestCondFormatListFromSheet();
42 void testCondFormatListProperties();
43 void testCondFormatListFormats();
44 void testCondFormatProperties();
45 void testCondFormatXIndex();
46 void testDataBarProperties();
47 void testColorScaleProperties();
49 CPPUNIT_TEST_SUITE(ScConditionalFormatTest);
50 CPPUNIT_TEST(testRequestCondFormatListFromSheet);
51 CPPUNIT_TEST(testCondFormatListProperties);
52 CPPUNIT_TEST(testCondFormatListFormats);
53 CPPUNIT_TEST(testCondFormatProperties);
54 CPPUNIT_TEST(testCondFormatXIndex);
55 CPPUNIT_TEST(testDataBarProperties);
56 CPPUNIT_TEST(testColorScaleProperties);
57 CPPUNIT_TEST_SUITE_END();
58 private:
60 uno::Reference< lang::XComponent > mxComponent;
63 ScConditionalFormatTest::ScConditionalFormatTest()
64 : CalcUnoApiTest("sc/qa/extras/testdocuments/")
68 uno::Reference< uno::XInterface > ScConditionalFormatTest::init(sal_Int32 nIndex)
70 // get the first sheet
71 uno::Reference< sheet::XSpreadsheetDocument > xDoc(mxComponent, uno::UNO_QUERY_THROW);
72 uno::Reference< container::XIndexAccess > xIndex (xDoc->getSheets(), uno::UNO_QUERY_THROW);
73 uno::Reference< sheet::XSpreadsheet > xSheet( xIndex->getByIndex(nIndex), uno::UNO_QUERY_THROW);
75 return xSheet;
78 void ScConditionalFormatTest::testRequestCondFormatListFromSheet()
80 uno::Reference<sheet::XSpreadsheet> xSheet(init(), uno::UNO_QUERY_THROW);
81 uno::Reference<beans::XPropertySet> xProps(xSheet, uno::UNO_QUERY_THROW);
82 uno::Any aAny = xProps->getPropertyValue("ConditionalFormats");
83 uno::Reference<sheet::XConditionalFormats> xCondFormats;
84 CPPUNIT_ASSERT(aAny >>= xCondFormats);
85 CPPUNIT_ASSERT(xCondFormats.is());
88 namespace {
90 uno::Reference<sheet::XConditionalFormats> getConditionalFormatList(uno::Reference<uno::XInterface> const & xInterface)
92 uno::Reference<sheet::XSpreadsheet> xSheet(xInterface, uno::UNO_QUERY_THROW);
93 uno::Reference<beans::XPropertySet> xProps(xSheet, uno::UNO_QUERY_THROW);
94 uno::Any aAny = xProps->getPropertyValue("ConditionalFormats");
95 uno::Reference<sheet::XConditionalFormats> xCondFormats;
96 CPPUNIT_ASSERT(aAny >>= xCondFormats);
97 CPPUNIT_ASSERT(xCondFormats.is());
99 return xCondFormats;
104 void ScConditionalFormatTest::testCondFormatListProperties()
106 uno::Reference<sheet::XConditionalFormats> xCondFormat =
107 getConditionalFormatList(init());
108 CPPUNIT_ASSERT_EQUAL(sal_Int32(4), xCondFormat->getLength());
111 void ScConditionalFormatTest::testCondFormatListFormats()
113 uno::Reference<sheet::XConditionalFormats> xCondFormatList =
114 getConditionalFormatList(init());
116 const uno::Sequence<uno::Reference<sheet::XConditionalFormat> > xCondFormats =
117 xCondFormatList->getConditionalFormats();
118 CPPUNIT_ASSERT_EQUAL(sal_Int32(4), xCondFormats.getLength());
119 for (auto const & cf : xCondFormats)
121 CPPUNIT_ASSERT(cf.is());
125 void ScConditionalFormatTest::testCondFormatProperties()
127 uno::Reference<sheet::XConditionalFormats> xCondFormatList =
128 getConditionalFormatList(init(1));
130 uno::Sequence<uno::Reference<sheet::XConditionalFormat> > xCondFormats =
131 xCondFormatList->getConditionalFormats();
132 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xCondFormats.getLength());
134 uno::Reference<sheet::XConditionalFormat> xCondFormat = xCondFormats[0];
135 CPPUNIT_ASSERT(xCondFormat.is());
136 uno::Reference<beans::XPropertySet> xPropSet(xCondFormat, uno::UNO_QUERY_THROW);
137 uno::Any aAny = xPropSet->getPropertyValue("Range");
138 uno::Reference<sheet::XSheetCellRanges> xCellRanges;
139 CPPUNIT_ASSERT(aAny >>= xCellRanges);
140 CPPUNIT_ASSERT(xCellRanges.is());
141 uno::Sequence<table::CellRangeAddress> aRanges = xCellRanges->getRangeAddresses();
142 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aRanges.getLength());
143 table::CellRangeAddress aRange = aRanges[0];
144 CPPUNIT_ASSERT_EQUAL(sal_Int16(1), aRange.Sheet);
145 CPPUNIT_ASSERT_EQUAL(sal_Int32(4), aRange.StartColumn);
146 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), aRange.StartRow);
147 CPPUNIT_ASSERT_EQUAL(sal_Int32(7), aRange.EndColumn);
148 CPPUNIT_ASSERT_EQUAL(sal_Int32(16), aRange.EndRow);
151 void ScConditionalFormatTest::testCondFormatXIndex()
153 uno::Reference<sheet::XConditionalFormats> xCondFormatList =
154 getConditionalFormatList(init(1));
156 uno::Sequence<uno::Reference<sheet::XConditionalFormat> > xCondFormats =
157 xCondFormatList->getConditionalFormats();
158 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xCondFormats.getLength());
160 uno::Reference<sheet::XConditionalFormat> xCondFormat = xCondFormats[0];
161 CPPUNIT_ASSERT(xCondFormat.is());
163 uno::Type aType = xCondFormat->getElementType();
164 CPPUNIT_ASSERT_EQUAL(OUString("com.sun.star.beans.XPropertySet"), aType.getTypeName());
166 CPPUNIT_ASSERT(xCondFormat->hasElements());
167 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), xCondFormat->getCount());
169 uno::Any aAny = xCondFormat->getByIndex(0);
170 CPPUNIT_ASSERT(aAny.hasValue());
173 namespace {
175 void testAxisPosition(uno::Reference<beans::XPropertySet> const & xPropSet, sal_Int32 ePos)
177 sal_Int32 eAxisPos;
178 uno::Any aAny = xPropSet->getPropertyValue("AxisPosition");
179 CPPUNIT_ASSERT(aAny >>= eAxisPos);
180 CPPUNIT_ASSERT_EQUAL(ePos, eAxisPos);
183 void testShowValue(uno::Reference<beans::XPropertySet> const & xPropSet, bool bShowVal)
185 bool bShow;
186 uno::Any aAny = xPropSet->getPropertyValue("ShowValue");
187 CPPUNIT_ASSERT(aAny >>= bShow);
188 CPPUNIT_ASSERT_EQUAL(bShowVal, bShow);
191 void testUseGradient(uno::Reference<beans::XPropertySet> const & xPropSet, bool bUseGradient)
193 bool bGradient;
194 uno::Any aAny = xPropSet->getPropertyValue("UseGradient");
195 CPPUNIT_ASSERT(aAny >>= bGradient);
196 CPPUNIT_ASSERT_EQUAL(bUseGradient, bGradient);
199 void testPositiveColor(uno::Reference<beans::XPropertySet> const & xPropSet, Color aColor)
201 ::Color nColor;
202 uno::Any aAny = xPropSet->getPropertyValue("Color");
203 CPPUNIT_ASSERT(aAny >>= nColor);
204 CPPUNIT_ASSERT_EQUAL(aColor, nColor);
207 void testNegativeColor(uno::Reference<beans::XPropertySet> const & xPropSet, Color aColor)
209 ::Color nColor;
210 uno::Any aAny = xPropSet->getPropertyValue("NegativeColor");
211 CPPUNIT_ASSERT(aAny >>= nColor);
212 CPPUNIT_ASSERT_EQUAL(aColor, nColor);
215 void testAxisColor(uno::Reference<beans::XPropertySet> const & xPropSet, Color aColor)
217 ::Color nColor;
218 uno::Any aAny = xPropSet->getPropertyValue("AxisColor");
219 CPPUNIT_ASSERT(aAny >>= nColor);
220 CPPUNIT_ASSERT_EQUAL(aColor, nColor);
223 void testDataBarEntryValue(uno::Reference<sheet::XDataBarEntry> const & xEntry,
224 const OUString& rExpectedValue, sal_Int32 nType)
226 switch (nType)
228 case sheet::DataBarEntryType::DATABAR_VALUE:
229 case sheet::DataBarEntryType::DATABAR_PERCENT:
230 case sheet::DataBarEntryType::DATABAR_PERCENTILE:
231 case sheet::DataBarEntryType::DATABAR_FORMULA:
233 OUString aString = xEntry->getFormula();
234 CPPUNIT_ASSERT_EQUAL(rExpectedValue, aString);
236 break;
237 default:
238 break;
242 void testDataBarEntries(uno::Reference<beans::XPropertySet> const & xPropSet,
243 const OUString& rExpectedMinString, sal_Int32 nExpectedMinType,
244 const OUString& rExpectedMaxString, sal_Int32 nExpectedMaxType)
246 uno::Any aAny = xPropSet->getPropertyValue("DataBarEntries");
247 uno::Sequence<uno::Reference<sheet::XDataBarEntry> > aEntries;
248 CPPUNIT_ASSERT(aAny >>= aEntries);
250 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aEntries.getLength());
252 sal_Int32 nMinType = aEntries[0]->getType();
253 CPPUNIT_ASSERT_EQUAL(nExpectedMinType, nMinType);
255 sal_Int32 nMaxType = aEntries[1]->getType();
256 CPPUNIT_ASSERT_EQUAL(nExpectedMaxType, nMaxType);
258 testDataBarEntryValue(aEntries[0], rExpectedMinString, nMinType);
259 testDataBarEntryValue(aEntries[1], rExpectedMaxString, nMaxType);
264 void ScConditionalFormatTest::testDataBarProperties()
266 uno::Reference<sheet::XConditionalFormats> xCondFormatList =
267 getConditionalFormatList(init(2));
269 uno::Sequence<uno::Reference<sheet::XConditionalFormat> > xCondFormats =
270 xCondFormatList->getConditionalFormats();
271 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xCondFormats.getLength());
273 uno::Reference<sheet::XConditionalFormat> xCondFormat = xCondFormats[0];
274 CPPUNIT_ASSERT(xCondFormat.is());
276 uno::Type aType = xCondFormat->getElementType();
277 CPPUNIT_ASSERT_EQUAL(OUString("com.sun.star.beans.XPropertySet"), aType.getTypeName());
279 CPPUNIT_ASSERT(xCondFormat->hasElements());
280 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), xCondFormat->getCount());
282 uno::Reference<beans::XPropertySet> xPropSet;
284 uno::Any aAny = xCondFormat->getByIndex(0);
285 CPPUNIT_ASSERT(aAny.hasValue());
286 CPPUNIT_ASSERT(aAny >>= xPropSet);
287 testAxisPosition(xPropSet, sheet::DataBarAxis::AXIS_AUTOMATIC);
288 testShowValue(xPropSet, true);
289 testUseGradient(xPropSet, true);
290 testPositiveColor(xPropSet, COL_LIGHTBLUE);
291 testNegativeColor(xPropSet, COL_LIGHTRED);
292 testAxisColor(xPropSet, COL_BLACK);
293 testDataBarEntries(xPropSet, "", sheet::DataBarEntryType::DATABAR_AUTO,
294 "", sheet::DataBarEntryType::DATABAR_MAX);
297 uno::Any aAny = xCondFormat->getByIndex(1);
298 CPPUNIT_ASSERT(aAny.hasValue());
299 CPPUNIT_ASSERT(aAny >>= xPropSet);
300 testAxisPosition(xPropSet, sheet::DataBarAxis::AXIS_AUTOMATIC);
301 testShowValue(xPropSet, true);
302 testUseGradient(xPropSet, true);
303 testPositiveColor(xPropSet, COL_LIGHTBLUE);
304 testNegativeColor(xPropSet, COL_LIGHTRED);
305 testAxisColor(xPropSet, COL_BLACK);
306 testDataBarEntries(xPropSet, "", sheet::DataBarEntryType::DATABAR_MIN,
307 "90", sheet::DataBarEntryType::DATABAR_PERCENTILE);
310 uno::Any aAny = xCondFormat->getByIndex(2);
311 CPPUNIT_ASSERT(aAny.hasValue());
312 CPPUNIT_ASSERT(aAny >>= xPropSet);
313 testAxisPosition(xPropSet, sheet::DataBarAxis::AXIS_AUTOMATIC);
314 testShowValue(xPropSet, true);
315 testUseGradient(xPropSet, true);
316 testPositiveColor(xPropSet, COL_LIGHTBLUE);
317 testNegativeColor(xPropSet, COL_LIGHTRED);
318 testAxisColor(xPropSet, COL_BLACK);
319 testDataBarEntries(xPropSet, "2", sheet::DataBarEntryType::DATABAR_VALUE,
320 "80", sheet::DataBarEntryType::DATABAR_PERCENT);
323 uno::Any aAny = xCondFormat->getByIndex(3);
324 CPPUNIT_ASSERT(aAny.hasValue());
325 CPPUNIT_ASSERT(aAny >>= xPropSet);
326 testAxisPosition(xPropSet, sheet::DataBarAxis::AXIS_AUTOMATIC);
327 testShowValue(xPropSet, true);
328 testUseGradient(xPropSet, true);
329 testPositiveColor(xPropSet, COL_LIGHTBLUE);
330 testNegativeColor(xPropSet, COL_LIGHTRED);
331 testAxisColor(xPropSet, COL_BLACK);
333 * TODO: implement FORMULA
334 testDataBarEntries(xPropSet, "=A1", sheet::DataBarEntryType::DATABAR_FORMULA,
335 "", sheet::DataBarEntryType::DATABAR_AUTO);
339 uno::Any aAny = xCondFormat->getByIndex(4);
340 CPPUNIT_ASSERT(aAny.hasValue());
341 CPPUNIT_ASSERT(aAny >>= xPropSet);
342 testAxisPosition(xPropSet, sheet::DataBarAxis::AXIS_MIDDLE);
343 testShowValue(xPropSet, true);
344 testUseGradient(xPropSet, false);
345 testPositiveColor(xPropSet, sal_uInt32(10092390));
346 testNegativeColor(xPropSet, sal_uInt32(52428));
347 testAxisColor(xPropSet, sal_uInt32(16777113));
348 testDataBarEntries(xPropSet, "", sheet::DataBarEntryType::DATABAR_AUTO,
349 "", sheet::DataBarEntryType::DATABAR_AUTO);
353 namespace {
355 void testColorScaleEntry(uno::Reference<sheet::XColorScaleEntry> const & xEntry,
356 sal_Int32 nType, const OUString& rString, Color nColor)
358 CPPUNIT_ASSERT_EQUAL(nType, xEntry->getType());
359 CPPUNIT_ASSERT_EQUAL(nColor, Color(xEntry->getColor()));
360 switch (nType)
362 case sheet::ColorScaleEntryType::COLORSCALE_VALUE:
363 case sheet::ColorScaleEntryType::COLORSCALE_PERCENT:
364 case sheet::ColorScaleEntryType::COLORSCALE_PERCENTILE:
365 // case sheet::ColorScaleEntryType::COLORSCALE_FORMULA:
367 CPPUNIT_ASSERT_EQUAL(rString, xEntry->getFormula());
369 break;
370 default:
371 break;
375 void testColorScaleEntries(uno::Reference<beans::XPropertySet> const & xPropSet, sal_Int32 nEntries,
376 sal_Int32 nMinType, const OUString& rMinString, Color nMinColor,
377 sal_Int32 nMediumType, const OUString& rMediumString, Color nMediumColor,
378 sal_Int32 nMaxType, const OUString& rMaxString, Color nMaxColor)
380 uno::Any aAny = xPropSet->getPropertyValue("ColorScaleEntries");
381 CPPUNIT_ASSERT(aAny.hasValue());
382 uno::Sequence<uno::Reference<sheet::XColorScaleEntry> > aEntries;
383 CPPUNIT_ASSERT(aAny >>= aEntries);
385 CPPUNIT_ASSERT_EQUAL(nEntries, aEntries.getLength());
386 testColorScaleEntry(aEntries[0], nMinType, rMinString, nMinColor);
387 size_t nMaxEntry = 1;
388 if (nEntries == 3)
390 nMaxEntry = 2;
391 testColorScaleEntry(aEntries[1], nMediumType, rMediumString, nMediumColor);
393 testColorScaleEntry(aEntries[nMaxEntry], nMaxType, rMaxString, nMaxColor);
398 void ScConditionalFormatTest::testColorScaleProperties()
400 uno::Reference<sheet::XConditionalFormats> xCondFormatList =
401 getConditionalFormatList(init(3));
403 uno::Sequence<uno::Reference<sheet::XConditionalFormat> > xCondFormats =
404 xCondFormatList->getConditionalFormats();
405 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xCondFormats.getLength());
407 uno::Reference<sheet::XConditionalFormat> xCondFormat = xCondFormats[0];
408 CPPUNIT_ASSERT(xCondFormat.is());
410 uno::Type aType = xCondFormat->getElementType();
411 CPPUNIT_ASSERT_EQUAL(OUString("com.sun.star.beans.XPropertySet"), aType.getTypeName());
413 CPPUNIT_ASSERT(xCondFormat->hasElements());
414 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), xCondFormat->getCount());
416 uno::Reference<beans::XPropertySet> xPropSet;
418 uno::Any aAny = xCondFormat->getByIndex(0);
419 CPPUNIT_ASSERT(aAny.hasValue());
420 CPPUNIT_ASSERT(aAny >>= xPropSet);
421 testColorScaleEntries(xPropSet, 3, sheet::ColorScaleEntryType::COLORSCALE_MIN, "", sal_uInt32(16777113),
422 sheet::ColorScaleEntryType::COLORSCALE_PERCENTILE, "50", sal_uInt32(16737792),
423 sheet::ColorScaleEntryType::COLORSCALE_MAX, "", sal_uInt32(16724787));
426 uno::Any aAny = xCondFormat->getByIndex(1);
427 CPPUNIT_ASSERT(aAny.hasValue());
428 CPPUNIT_ASSERT(aAny >>= xPropSet);
429 testColorScaleEntries(xPropSet, 3, sheet::ColorScaleEntryType::COLORSCALE_VALUE, "0", sal_uInt32(16711680),
430 sheet::ColorScaleEntryType::COLORSCALE_PERCENTILE, "50", sal_uInt32(10092390),
431 sheet::ColorScaleEntryType::COLORSCALE_PERCENT, "90", sal_uInt32(255));
434 uno::Any aAny = xCondFormat->getByIndex(2);
435 CPPUNIT_ASSERT(aAny.hasValue());
436 CPPUNIT_ASSERT(aAny >>= xPropSet);
437 testColorScaleEntries(xPropSet, 2, sheet::ColorScaleEntryType::COLORSCALE_FORMULA, "=A1", COL_WHITE,
438 sheet::ColorScaleEntryType::COLORSCALE_PERCENTILE, "not used", sal_uInt32(1),
439 sheet::ColorScaleEntryType::COLORSCALE_VALUE, "10", COL_BLACK);
443 void ScConditionalFormatTest::setUp()
445 CalcUnoApiTest::setUp();
446 // get the test file
447 OUString aFileURL;
448 createFileURL("new_cond_format_api.ods", aFileURL);
449 mxComponent = loadFromDesktop(aFileURL);
452 void ScConditionalFormatTest::tearDown()
454 closeDocument(mxComponent);
455 CalcUnoApiTest::tearDown();
458 CPPUNIT_TEST_SUITE_REGISTRATION(ScConditionalFormatTest);
462 CPPUNIT_PLUGIN_IMPLEMENT();
465 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */