1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <test/calc_unoapi_test.hxx>
11 #include <com/sun/star/container/XIndexAccess.hpp>
12 #include <com/sun/star/sheet/XCellRangesQuery.hpp>
13 #include <com/sun/star/sheet/XSheetCellRanges.hpp>
14 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
15 #include <com/sun/star/sheet/XSpreadsheet.hpp>
16 #include <com/sun/star/table/CellAddress.hpp>
17 #include <com/sun/star/container/XNamed.hpp>
20 using namespace css::lang
;
22 namespace sc_apitest
{
24 class CheckXCellRangesQuery
: public CalcUnoApiTest
27 CheckXCellRangesQuery();
29 virtual void setUp() override
;
30 virtual void tearDown() override
;
32 uno::Reference
< uno::XInterface
> init();
33 void checkEmptyCell();
34 void checkFilledCell();
36 void _queryColumnDifferences(const OUString
& expected
);
37 void _queryRowDifferences(const OUString
& expected
);
38 void _queryEmptyCells(const OUString
& expected
);
40 CPPUNIT_TEST_SUITE(CheckXCellRangesQuery
);
41 CPPUNIT_TEST(checkEmptyCell
);
42 CPPUNIT_TEST(checkFilledCell
);
43 CPPUNIT_TEST_SUITE_END();
46 uno::Reference
<lang::XComponent
> mxComponent
;
47 uno::Reference
< sheet::XCellRangesQuery
> m_xCell
;
51 CheckXCellRangesQuery::CheckXCellRangesQuery()
52 : CalcUnoApiTest("/sc/qa/extras/testdocuments")
56 uno::Reference
< uno::XInterface
> CheckXCellRangesQuery::init()
58 // create a calc document
59 if (!mxComponent
.is())
60 // Load an empty document.
61 mxComponent
= loadFromDesktop("private:factory/scalc");
63 uno::Reference
< sheet::XSpreadsheetDocument
> xSheetDoc(mxComponent
, uno::UNO_QUERY_THROW
);
65 // Getting spreadsheet
66 uno::Reference
< sheet::XSpreadsheets
> oSheets
= xSheetDoc
->getSheets();
67 uno::Reference
< container::XIndexAccess
> oIndexSheets(oSheets
, uno::UNO_QUERY_THROW
);
68 uno::Any aAny
= oIndexSheets
->getByIndex(0);
69 uno::Reference
<container::XNamed
> xNamed
;
70 CPPUNIT_ASSERT(aAny
>>= xNamed
);
71 sSheetName
= xNamed
->getName();
74 uno::Reference
< sheet::XSpreadsheet
> xSpreadSheet
;
75 CPPUNIT_ASSERT(aAny
>>= xSpreadSheet
);
76 uno::Reference
<uno::XInterface
> oObj
= xSpreadSheet
->getCellByPosition(2, 3);
77 m_xCell
= uno::Reference
<sheet::XCellRangesQuery
>(oObj
, uno::UNO_QUERY_THROW
);
79 // set one value for comparison.
80 xSpreadSheet
->getCellByPosition(1, 1)->setValue(15);
81 xSpreadSheet
->getCellByPosition(1, 3)->setValue(5);
82 xSpreadSheet
->getCellByPosition(2, 1)->setFormula("=B2+B4");
88 * Perform some tests on an empty cell:
90 * <li>compare an empty cell with a cell with a value in the same column</li>
91 * <li>compare an empty cell with a cell with a value in the same row</li>
92 * <li>query for empty cells</li>
95 void CheckXCellRangesQuery::checkEmptyCell()
97 // compare an empty cell with a cell with a value
98 _queryColumnDifferences(sSheetName
+ ".C4");
99 // compare an empty cell with a cell with a value
100 _queryRowDifferences(sSheetName
+ ".C4");
104 * Perform some tests on a filled cell:
106 * <li>compare a cell with value 5 with a cell with value 15 in the same
108 * <li>compare a cell with value 5 with a cell with value 15 in the same
110 * <li>query for an empty cell.</li>
114 void CheckXCellRangesQuery::checkFilledCell()
116 uno::Reference
< sheet::XSpreadsheet
> xSpreadSheet(init(), uno::UNO_QUERY_THROW
);
117 // fill the cell with a value
118 xSpreadSheet
->getCellByPosition(2, 3)->setValue(15);
120 // compare a cell with value 5 with a cell with value 15
121 _queryColumnDifferences(sSheetName
+ ".C4");
122 // compare a cell with value 5 with a cell with value 15
123 _queryRowDifferences(sSheetName
+ ".C4");
124 // try to get nothing
125 _queryEmptyCells("");
129 * Query column differences between my cell(2,3) and (1,1).
131 * @param expected The expected outcome value.
133 void CheckXCellRangesQuery::_queryColumnDifferences(const OUString
& expected
)
135 //Query column differences
136 uno::Reference
<sheet::XSheetCellRanges
> ranges
= m_xCell
->queryColumnDifferences(table::CellAddress(0, 1, 1));
137 OUString getting
= ranges
->getRangeAddressesAsString();
139 CPPUNIT_ASSERT_EQUAL(expected
, getting
);
143 * Query for an empty cell.
145 * @param expected The expected outcome value.
147 void CheckXCellRangesQuery::_queryEmptyCells(const OUString
& expected
)
150 uno::Reference
<sheet::XSheetCellRanges
> ranges
= m_xCell
->queryEmptyCells();
151 OUString getting
= ranges
->getRangeAddressesAsString();
153 CPPUNIT_ASSERT_EQUAL(expected
, getting
);
157 * Query row differences between my cell(2,3) and (1,1).
159 * @param expected The expected outcome value.
161 void CheckXCellRangesQuery::_queryRowDifferences(const OUString
& expected
) {
162 //Query row differences
163 uno::Reference
<sheet::XSheetCellRanges
> ranges
= m_xCell
->queryRowDifferences(table::CellAddress(0, 1, 1));
164 OUString getting
= ranges
->getRangeAddressesAsString();
166 CPPUNIT_ASSERT_EQUAL(expected
, getting
);
169 void CheckXCellRangesQuery::setUp()
171 CalcUnoApiTest::setUp();
175 void CheckXCellRangesQuery::tearDown()
177 closeDocument(mxComponent
);
179 CalcUnoApiTest::tearDown();
182 CPPUNIT_TEST_SUITE_REGISTRATION(CheckXCellRangesQuery
);
186 CPPUNIT_PLUGIN_IMPLEMENT();