Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / qa / unit / cond_format_merge.cxx
blobed6a938bb472440f60c5d15115d0ae92d89ea275
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 <sal/config.h>
12 #include <com/sun/star/beans/XPropertySet.hpp>
13 #include <com/sun/star/sheet/XConditionalFormats.hpp>
14 #include <com/sun/star/sheet/XSpreadsheet.hpp>
15 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
16 #include <test/unoapi_test.hxx>
18 namespace com::sun::star::lang
20 class XComponent;
23 using namespace css;
25 class ScCondFormatMergeTest : public UnoApiTest
27 public:
28 ScCondFormatMergeTest();
30 void testCondFormatMerge();
32 CPPUNIT_TEST_SUITE(ScCondFormatMergeTest);
33 CPPUNIT_TEST(testCondFormatMerge);
34 CPPUNIT_TEST_SUITE_END();
37 ScCondFormatMergeTest::ScCondFormatMergeTest()
38 : UnoApiTest("sc/qa/extras/testdocuments/")
42 void ScCondFormatMergeTest::testCondFormatMerge()
44 loadFromFile(u"cond_format_merge.ods");
46 // get the first sheet
47 uno::Reference<sheet::XSpreadsheetDocument> xDoc(mxComponent, uno::UNO_QUERY_THROW);
48 uno::Reference<container::XIndexAccess> xIndex(xDoc->getSheets(), uno::UNO_QUERY_THROW);
49 uno::Reference<sheet::XSpreadsheet> xSheet(xIndex->getByIndex(0), uno::UNO_QUERY_THROW);
51 uno::Reference<beans::XPropertySet> xProps(xSheet, uno::UNO_QUERY_THROW);
52 uno::Any aAny = xProps->getPropertyValue("ConditionalFormats");
53 uno::Reference<sheet::XConditionalFormats> xCondFormats;
55 CPPUNIT_ASSERT(aAny >>= xCondFormats);
56 CPPUNIT_ASSERT(xCondFormats.is());
58 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), xCondFormats->getLength());
60 uno::Sequence<uno::Reference<sheet::XConditionalFormat>> xCondFormatSeq
61 = xCondFormats->getConditionalFormats();
62 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), xCondFormatSeq.getLength());
64 int nRanges = 0;
65 for (sal_Int32 i = 0, n = xCondFormatSeq.getLength(); i < n; ++i)
67 CPPUNIT_ASSERT(xCondFormatSeq[i].is());
69 uno::Reference<sheet::XConditionalFormat> xCondFormat = xCondFormatSeq[i];
70 CPPUNIT_ASSERT(xCondFormat.is());
72 uno::Reference<beans::XPropertySet> xPropSet(xCondFormat, uno::UNO_QUERY_THROW);
74 aAny = xPropSet->getPropertyValue("Range");
75 uno::Reference<sheet::XSheetCellRanges> xCellRanges;
76 CPPUNIT_ASSERT(aAny >>= xCellRanges);
77 CPPUNIT_ASSERT(xCellRanges.is());
79 uno::Sequence<table::CellRangeAddress> aRanges = xCellRanges->getRangeAddresses();
80 CPPUNIT_ASSERT_GREATEREQUAL(sal_Int32(1), aRanges.getLength());
82 table::CellRangeAddress aRange0 = aRanges[0];
83 CPPUNIT_ASSERT_EQUAL(sal_Int16(0), aRange0.Sheet);
84 CPPUNIT_ASSERT_EQUAL(aRange0.StartColumn, aRange0.EndColumn);
86 table::CellRangeAddress aRange1;
88 switch (aRange0.StartColumn)
90 case 3:
91 switch (aRange0.StartRow)
93 case 0: // D1:D2,D5::D8
94 nRanges++;
95 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aRange0.EndRow);
96 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aRanges.getLength());
97 aRange1 = aRanges[1];
98 CPPUNIT_ASSERT_EQUAL(sal_Int16(0), aRange1.Sheet);
99 CPPUNIT_ASSERT_EQUAL(aRange1.StartColumn, aRange1.EndColumn);
100 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), aRange1.StartColumn);
101 CPPUNIT_ASSERT_EQUAL(sal_Int32(4), aRange1.StartRow);
102 CPPUNIT_ASSERT_EQUAL(sal_Int32(7), aRange1.EndRow);
103 break;
104 default:
105 CPPUNIT_FAIL("Unexpected range in column D");
107 break;
108 case 5:
109 switch (aRange0.StartRow)
111 case 0: // F1:F2
112 nRanges++;
113 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aRange0.EndRow);
114 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aRanges.getLength());
115 break;
116 case 2: // F3
117 nRanges++;
118 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aRange0.EndRow);
119 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aRanges.getLength());
120 break;
121 case 3: // F4
122 nRanges++;
123 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), aRange0.EndRow);
124 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aRanges.getLength());
125 break;
126 case 4: // F5
127 nRanges++;
128 CPPUNIT_ASSERT_EQUAL(sal_Int32(4), aRange0.EndRow);
129 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aRanges.getLength());
130 break;
131 default:
132 CPPUNIT_FAIL("Unexpected range in column F");
134 break;
135 default:
136 CPPUNIT_FAIL("Unexpected range");
140 CPPUNIT_ASSERT_EQUAL(5, nRanges);
143 CPPUNIT_TEST_SUITE_REGISTRATION(ScCondFormatMergeTest);
145 CPPUNIT_PLUGIN_IMPLEMENT();
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */