Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / qa / extras / check_xcell_ranges_query.cxx
blobe2d216dc22a0bbd79314be7747001ca7d69e6604
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>
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>
19 using namespace css;
20 using namespace css::lang;
22 namespace sc_apitest {
24 class CheckXCellRangesQuery : public CalcUnoApiTest
26 public:
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();
45 private:
46 uno::Reference<lang::XComponent> mxComponent;
47 uno::Reference< sheet::XCellRangesQuery > m_xCell;
48 OUString sSheetName;
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();
73 // get the cell
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");
84 return xSpreadSheet;
87 /**
88 * Perform some tests on an empty cell:
89 * <ol>
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>
93 * <ol>
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:
105 * <ol>
106 * <li>compare a cell with value 5 with a cell with value 15 in the same
107 * column</li>
108 * <li>compare a cell with value 5 with a cell with value 15 in the same
109 * row</li>
110 * <li>query for an empty cell.</li>
111 * <ol>
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)
149 //Query empty cells
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();
172 init();
175 void CheckXCellRangesQuery::tearDown()
177 closeDocument(mxComponent);
178 mxComponent.clear();
179 CalcUnoApiTest::tearDown();
182 CPPUNIT_TEST_SUITE_REGISTRATION(CheckXCellRangesQuery);
186 CPPUNIT_PLUGIN_IMPLEMENT();