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/beans/XPropertySet.hpp>
12 #include <com/sun/star/container/XIndexAccess.hpp>
13 #include <com/sun/star/sheet/XDataPilotDescriptor.hpp>
14 #include <com/sun/star/sheet/XDataPilotTables.hpp>
15 #include <com/sun/star/sheet/XDataPilotTablesSupplier.hpp>
16 #include <com/sun/star/sheet/XSpreadsheet.hpp>
17 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
18 #include <com/sun/star/sheet/XSpreadsheets.hpp>
19 #include <com/sun/star/table/CellAddress.hpp>
20 #include <com/sun/star/table/CellRangeAddress.hpp>
21 #include <com/sun/star/sheet/GeneralFunction.hpp>
22 #include <com/sun/star/sheet/DataPilotFieldOrientation.hpp>
23 #include <test/container/xnamed.hxx>
24 #include <test/sheet/xdatapilottable.hxx>
25 #include <test/sheet/xdatapilotdescriptor.hxx>
26 #include <test/beans/xpropertyset.hxx>
27 //check the DataPilot of Calc.
30 using namespace css::lang
;
32 namespace sc_apitest
{
34 class CheckDataPilotTable
: public CalcUnoApiTest
, public apitest::XNamed
, public apitest::XDataPilotTable
, public apitest::XPropertySet
, public apitest::XDataPilotDescriptor
37 CheckDataPilotTable();
39 virtual void setUp() override
;
40 virtual void tearDown() override
;
42 uno::Reference
< uno::XInterface
> init() override
;
44 CPPUNIT_TEST_SUITE(CheckDataPilotTable
);
46 CPPUNIT_TEST(testGetName
);
47 CPPUNIT_TEST(testSetName
);
50 CPPUNIT_TEST(testGetOutputRange
);
52 // _XDataPilotDescriptor
53 CPPUNIT_TEST(testTag
);
54 CPPUNIT_TEST(testGetFilterDescriptor
);
55 CPPUNIT_TEST(testGetDataPilotFields
);
56 CPPUNIT_TEST(testGetColumnFields
);
57 CPPUNIT_TEST(testGetRowFields
);
58 // CPPUNIT_TEST(testGetDataFields);
59 // CPPUNIT_TEST(testGetHiddenFields);
60 CPPUNIT_TEST(testGetPageFields
);
62 CPPUNIT_TEST_SUITE_END();
65 uno::Reference
<lang::XComponent
> mxComponent
;
66 uno::Reference
<uno::XInterface
> mxObject
;
67 static constexpr int MAX_FIELD_INDEX
= 6;
70 CheckDataPilotTable::CheckDataPilotTable()
71 : CalcUnoApiTest("/sc/qa/extras/testdocuments"),
72 apitest::XNamed("DataPilotTable")
76 uno::Reference
< uno::XInterface
> CheckDataPilotTable::init()
78 // create a calc document
79 if (!mxComponent
.is())
80 // Load an empty document.
81 mxComponent
= loadFromDesktop("private:factory/scalc");
85 uno::Reference
< sheet::XSpreadsheetDocument
> xSheetDoc(mxComponent
, uno::UNO_QUERY_THROW
);
88 table::CellRangeAddress sCellRangeAdress
;
89 sCellRangeAdress
.Sheet
= 0;
90 sCellRangeAdress
.StartColumn
= 1;
91 sCellRangeAdress
.StartRow
= 0;
92 sCellRangeAdress
.EndColumn
= MAX_FIELD_INDEX
-1;
93 sCellRangeAdress
.EndRow
= MAX_FIELD_INDEX
- 1;
95 // position of the data pilot table
96 table::CellAddress sCellAdress
;
97 sCellAdress
.Sheet
= 0;
98 sCellAdress
.Column
= 7;
100 // Getting spreadsheet
101 uno::Reference
< sheet::XSpreadsheets
> xSpreadsheets
= xSheetDoc
->getSheets();
102 uno::Reference
< container::XIndexAccess
> oIndexAccess(xSpreadsheets
, uno::UNO_QUERY_THROW
);
104 // Per default there's now just one sheet, make sure we have at least two, then
105 xSpreadsheets
->insertNewByName("Some Sheet", 0);
106 uno::Any aAny
= oIndexAccess
->getByIndex(0);
107 uno::Reference
< sheet::XSpreadsheet
> oSheet
;
108 CPPUNIT_ASSERT(aAny
>>= oSheet
);
110 uno::Any aAny2
= oIndexAccess
->getByIndex(1);
111 uno::Reference
< sheet::XSpreadsheet
> oSheet2
;
112 CPPUNIT_ASSERT(aAny2
>>= oSheet2
);
115 for (int i
= 1; i
< MAX_FIELD_INDEX
; i
++)
117 oSheet
->getCellByPosition(i
, 0)->setFormula("Col" + OUString::number(i
));
118 oSheet
->getCellByPosition(0, i
)->setFormula("Row" + OUString::number(i
));
119 oSheet2
->getCellByPosition(i
, 0)->setFormula("Col" + OUString::number(i
));
120 oSheet2
->getCellByPosition(0, i
)->setFormula("Row" + OUString::number(i
));
123 for (int i
= 1; i
< MAX_FIELD_INDEX
; i
++)
125 for (int j
= 1; j
< MAX_FIELD_INDEX
; j
++)
127 oSheet
->getCellByPosition(i
, j
)->setValue(i
* (j
+ 1));
128 oSheet2
->getCellByPosition(i
, j
)->setValue(i
* (j
+ 2));
132 // change a value of a cell and check the change in the data pilot
134 uno::Any oChangeCell
;
135 oChangeCell
<<= oSheet
->getCellByPosition(1, 5);
136 int x
= sCellAdress
.Column
;
137 int y
= sCellAdress
.Row
+ 3;
138 // cell of the data pilot output
140 oCheckCell
<<= oSheet
->getCellByPosition(x
, y
);
141 // create the test objects
142 uno::Reference
< sheet::XDataPilotTablesSupplier
> DPTS(oSheet
, uno::UNO_QUERY_THROW
);
143 uno::Reference
< sheet::XDataPilotTables
> DPT
= DPTS
->getDataPilotTables();
144 uno::Reference
< sheet::XDataPilotDescriptor
> DPDsc
= DPT
->createDataPilotDescriptor();
145 DPDsc
->setSourceRange(sCellRangeAdress
);
147 uno::Any oDataPilotField
= DPDsc
->getDataPilotFields()->getByIndex(0);
148 uno::Reference
<beans::XPropertySet
> fieldPropSet(oDataPilotField
, uno::UNO_QUERY_THROW
);
151 sum
<<= sheet::GeneralFunction_SUM
;
152 fieldPropSet
->setPropertyValue("Function", sum
);
155 data
<<= sheet::DataPilotFieldOrientation_DATA
;
156 fieldPropSet
->setPropertyValue("Orientation", data
);
158 //Insert the DataPilotTable
159 if (DPT
->hasByName("DataPilotTable"))
160 DPT
->removeByName("DataPilotTable");
161 DPT
->insertNewByName("DataPilotTable", sCellAdress
, DPDsc
);
163 uno::Reference
<uno::XInterface
> xDataPilotTableObject
;
164 data
= DPT
->getByName( DPT
->getElementNames()[0] );
165 CPPUNIT_ASSERT(data
>>= xDataPilotTableObject
);
166 mxObject
= xDataPilotTableObject
;
168 return xDataPilotTableObject
;
171 void CheckDataPilotTable::setUp()
173 CalcUnoApiTest::setUp();
177 void CheckDataPilotTable::tearDown()
179 closeDocument(mxComponent
);
181 CalcUnoApiTest::tearDown();
184 CPPUNIT_TEST_SUITE_REGISTRATION(CheckDataPilotTable
);
188 CPPUNIT_PLUGIN_IMPLEMENT();