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/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/beans/xpropertyset.hxx>
25 //check the DataPilot of Calc.
28 using namespace css::lang
;
32 class CheckDataPilotField
: public UnoApiTest
, public apitest::XNamed
, public apitest::XPropertySet
35 CheckDataPilotField();
37 virtual void setUp() override
;
39 uno::Reference
<uno::XInterface
> init() override
;
41 CPPUNIT_TEST_SUITE(CheckDataPilotField
);
44 CPPUNIT_TEST(testGetName
);
45 CPPUNIT_TEST(testSetName
);
48 CPPUNIT_TEST(testGetPropertySetInfo
);
49 CPPUNIT_TEST(testSetPropertyValue
);
50 CPPUNIT_TEST(testGetPropertyValue
);
52 CPPUNIT_TEST_SUITE_END();
55 virtual bool isPropertyIgnored(const OUString
& rName
) override
;
58 uno::Reference
<uno::XInterface
> mxObject
;
59 int mMaxFieldIndex
= 6;
62 bool CheckDataPilotField::isPropertyIgnored(const OUString
& rName
)
64 return rName
== "Function" || rName
== "Subtotals" || rName
== "Function2"
65 || rName
== "Subtotals2";
68 CheckDataPilotField::CheckDataPilotField()
69 : UnoApiTest("/sc/qa/extras/testdocuments")
70 , apitest::XNamed("Col1")
74 uno::Reference
<uno::XInterface
> CheckDataPilotField::init()
76 // create a calc document
77 if (!mxComponent
.is())
78 // Load an empty document.
79 mxComponent
= loadFromDesktop("private:factory/scalc");
83 uno::Reference
<sheet::XSpreadsheetDocument
> xSheetDoc(mxComponent
, uno::UNO_QUERY_THROW
);
86 table::CellRangeAddress sCellRangeAddress
;
87 sCellRangeAddress
.Sheet
= 0;
88 sCellRangeAddress
.StartColumn
= 1;
89 sCellRangeAddress
.StartRow
= 0;
90 sCellRangeAddress
.EndColumn
= mMaxFieldIndex
- 1;
91 sCellRangeAddress
.EndRow
= mMaxFieldIndex
- 1;
93 // position of the data pilot table
94 table::CellAddress sCellAddress
;
95 sCellAddress
.Sheet
= 0;
96 sCellAddress
.Column
= 7;
98 // Getting spreadsheet
99 uno::Reference
<sheet::XSpreadsheets
> xSpreadsheets
= xSheetDoc
->getSheets();
100 uno::Reference
<container::XIndexAccess
> oIndexAccess(xSpreadsheets
, uno::UNO_QUERY_THROW
);
102 // Per default there's now just one sheet, make sure we have at least two, then
103 xSpreadsheets
->insertNewByName("Some Sheet", 0);
104 uno::Any aAny
= oIndexAccess
->getByIndex(0);
105 uno::Reference
<sheet::XSpreadsheet
> oSheet
;
106 CPPUNIT_ASSERT(aAny
>>= oSheet
);
108 uno::Any aAny2
= oIndexAccess
->getByIndex(1);
109 uno::Reference
<sheet::XSpreadsheet
> oSheet2
;
110 CPPUNIT_ASSERT(aAny2
>>= oSheet2
);
113 for (int i
= 1; i
< mMaxFieldIndex
; i
++)
115 oSheet
->getCellByPosition(i
, 0)->setFormula("Col" + OUString::number(i
));
116 oSheet
->getCellByPosition(0, i
)->setFormula("Row" + OUString::number(i
));
117 oSheet2
->getCellByPosition(i
, 0)->setFormula("Col" + OUString::number(i
));
118 oSheet2
->getCellByPosition(0, i
)->setFormula("Row" + OUString::number(i
));
121 for (int i
= 1; i
< mMaxFieldIndex
; i
++)
123 for (int j
= 1; j
< mMaxFieldIndex
; j
++)
125 oSheet
->getCellByPosition(i
, j
)->setValue(i
* (j
+ 1));
126 oSheet2
->getCellByPosition(i
, j
)->setValue(i
* (j
+ 2));
130 // change a value of a cell and check the change in the data pilot
132 uno::Any oChangeCell
;
133 oChangeCell
<<= oSheet
->getCellByPosition(1, 5);
134 int x
= sCellAddress
.Column
;
135 int y
= sCellAddress
.Row
+ 3;
136 // cell of the data pilot output
138 oCheckCell
<<= oSheet
->getCellByPosition(x
, y
);
139 // create the test objects
140 uno::Reference
<sheet::XDataPilotTablesSupplier
> DPTS(oSheet
, uno::UNO_QUERY_THROW
);
141 uno::Reference
<sheet::XDataPilotTables
> DPT
= DPTS
->getDataPilotTables();
142 uno::Reference
<sheet::XDataPilotDescriptor
> DPDsc
= DPT
->createDataPilotDescriptor();
143 DPDsc
->setSourceRange(sCellRangeAddress
);
145 uno::Any oDataPilotField
= DPDsc
->getDataPilotFields()->getByIndex(0);
146 uno::Reference
<beans::XPropertySet
> fieldPropSet(oDataPilotField
, uno::UNO_QUERY_THROW
);
149 sum
<<= sheet::GeneralFunction_SUM
;
150 fieldPropSet
->setPropertyValue("Function", sum
);
153 data
<<= sheet::DataPilotFieldOrientation_DATA
;
154 fieldPropSet
->setPropertyValue("Orientation", data
);
156 //Insert the DataPilotTable
157 if (DPT
->hasByName("DataPilotField"))
158 DPT
->removeByName("DataPilotField");
159 DPT
->insertNewByName("DataPilotTField", sCellAddress
, DPDsc
);
161 uno::Reference
<container::XIndexAccess
> IA
= DPDsc
->getDataPilotFields();
162 uno::Reference
<uno::XInterface
> xDataPilotFieldObject
;
163 data
= IA
->getByIndex(0);
164 CPPUNIT_ASSERT(data
>>= xDataPilotFieldObject
);
165 mxObject
= xDataPilotFieldObject
;
167 return xDataPilotFieldObject
;
170 void CheckDataPilotField::setUp()
176 CPPUNIT_TEST_SUITE_REGISTRATION(CheckDataPilotField
);
179 CPPUNIT_PLUGIN_IMPLEMENT();