1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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 */
9
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/beans/xpropertyset.hxx>
25 //check the DataPilot of Calc.
26
27 using namespace css;
28 using namespace css::lang;
29
30 namespace sc_apitest {
31
32 class CheckDataPilotField : public CalcUnoApiTest, public apitest::XNamed, public apitest::XPropertySet
33 {
34 public:
35 CheckDataPilotField();
36
37 virtual void setUp() override;
38 virtual void tearDown() override;
39
40 uno::Reference< uno::XInterface > init() override;
41
42 CPPUNIT_TEST_SUITE(CheckDataPilotField);
43
44 // _XNamed
45 CPPUNIT_TEST(testGetName);
46 CPPUNIT_TEST(testSetName);
47
48 // XPropertySet
49 CPPUNIT_TEST(testGetPropertySetInfo);
50 CPPUNIT_TEST(testSetPropertyValue);
51 CPPUNIT_TEST(testGetPropertyValue);
52
53 CPPUNIT_TEST_SUITE_END();
54
55 protected:
56
57 virtual bool isPropertyIgnored(const OUString& rName) override;
58
59 private:
60 uno::Reference<lang::XComponent> mxComponent;
61 uno::Reference<uno::XInterface> mxObject;
62 int mMaxFieldIndex = 6;
63 };
64
isPropertyIgnored(const OUString & rName)65 bool CheckDataPilotField::isPropertyIgnored(const OUString& rName)
66 {
67 return rName == "Function"
68 || rName == "Subtotals"
69 || rName == "Function2"
70 || rName == "Subtotals2";
71 }
72
CheckDataPilotField()73 CheckDataPilotField::CheckDataPilotField()
74 : CalcUnoApiTest("/sc/qa/extras/testdocuments"),
75 apitest::XNamed("Col1")
76 {
77 }
78
init()79 uno::Reference< uno::XInterface > CheckDataPilotField::init()
80 {
81 // create a calc document
82 if (!mxComponent.is())
83 // Load an empty document.
84 mxComponent = loadFromDesktop("private:factory/scalc");
85 else
86 return mxObject;
87
88 uno::Reference< sheet::XSpreadsheetDocument > xSheetDoc(mxComponent, uno::UNO_QUERY_THROW);
89
90 // the cell range
91 table::CellRangeAddress sCellRangeAdress;
92 sCellRangeAdress.Sheet = 0;
93 sCellRangeAdress.StartColumn = 1;
94 sCellRangeAdress.StartRow = 0;
95 sCellRangeAdress.EndColumn = mMaxFieldIndex-1;
96 sCellRangeAdress.EndRow = mMaxFieldIndex - 1;
97
98 // position of the data pilot table
99 table::CellAddress sCellAdress;
100 sCellAdress.Sheet = 0;
101 sCellAdress.Column = 7;
102 sCellAdress.Row = 8;
103 // Getting spreadsheet
104 uno::Reference< sheet::XSpreadsheets > xSpreadsheets = xSheetDoc->getSheets();
105 uno::Reference< container::XIndexAccess > oIndexAccess(xSpreadsheets, uno::UNO_QUERY_THROW);
106
107 // Per default there's now just one sheet, make sure we have at least two, then
108 xSpreadsheets->insertNewByName("Some Sheet", 0);
109 uno::Any aAny = oIndexAccess->getByIndex(0);
110 uno::Reference< sheet::XSpreadsheet > oSheet;
111 CPPUNIT_ASSERT(aAny >>= oSheet);
112
113 uno::Any aAny2 = oIndexAccess->getByIndex(1);
114 uno::Reference< sheet::XSpreadsheet > oSheet2;
115 CPPUNIT_ASSERT(aAny2 >>= oSheet2);
116
117 //Filling a table
118 for (int i = 1; i < mMaxFieldIndex; i++)
119 {
120 oSheet->getCellByPosition(i, 0)->setFormula("Col" + OUString::number(i));
121 oSheet->getCellByPosition(0, i)->setFormula("Row" + OUString::number(i));
122 oSheet2->getCellByPosition(i, 0)->setFormula("Col" + OUString::number(i));
123 oSheet2->getCellByPosition(0, i)->setFormula("Row" + OUString::number(i));
124 }
125
126 for (int i = 1; i < mMaxFieldIndex; i++)
127 {
128 for (int j = 1; j < mMaxFieldIndex; j++)
129 {
130 oSheet->getCellByPosition(i, j)->setValue(i * (j + 1));
131 oSheet2->getCellByPosition(i, j)->setValue(i * (j + 2));
132 }
133 }
134
135 // change a value of a cell and check the change in the data pilot
136 // cell of data
137 uno::Any oChangeCell;
138 oChangeCell<<= oSheet->getCellByPosition(1, 5);
139 int x = sCellAdress.Column;
140 int y = sCellAdress.Row + 3;
141 // cell of the data pilot output
142 uno::Any oCheckCell;
143 oCheckCell<<= oSheet->getCellByPosition(x, y);
144 // create the test objects
145 uno::Reference< sheet::XDataPilotTablesSupplier> DPTS(oSheet, uno::UNO_QUERY_THROW);
146 uno::Reference< sheet::XDataPilotTables> DPT = DPTS->getDataPilotTables();
147 uno::Reference< sheet::XDataPilotDescriptor> DPDsc = DPT->createDataPilotDescriptor();
148 DPDsc->setSourceRange(sCellRangeAdress);
149
150 uno::Any oDataPilotField = DPDsc->getDataPilotFields()->getByIndex(0);
151 uno::Reference<beans::XPropertySet> fieldPropSet(oDataPilotField, uno::UNO_QUERY_THROW);
152
153 uno::Any sum;
154 sum<<= sheet::GeneralFunction_SUM;
155 fieldPropSet->setPropertyValue("Function", sum );
156
157 uno::Any data;
158 data<<= sheet::DataPilotFieldOrientation_DATA;
159 fieldPropSet->setPropertyValue("Orientation", data);
160
161 //Insert the DataPilotTable
162 if (DPT->hasByName("DataPilotField"))
163 DPT->removeByName("DataPilotField");
164 DPT->insertNewByName("DataPilotTField", sCellAdress, DPDsc);
165
166 uno::Reference< container::XIndexAccess > IA = DPDsc->getDataPilotFields();
167 uno::Reference<uno::XInterface> xDataPilotFieldObject;
168 data = IA->getByIndex(0);
169 CPPUNIT_ASSERT(data >>= xDataPilotFieldObject);
170 mxObject = xDataPilotFieldObject;
171
172 return xDataPilotFieldObject;
173 }
174
175
setUp()176 void CheckDataPilotField::setUp()
177 {
178 CalcUnoApiTest::setUp();
179 init();
180 }
181
tearDown()182 void CheckDataPilotField::tearDown()
183 {
184 closeDocument(mxComponent);
185 mxComponent.clear();
186 CalcUnoApiTest::tearDown();
187 }
188
189 CPPUNIT_TEST_SUITE_REGISTRATION(CheckDataPilotField);
190
191 }
192
193 CPPUNIT_PLUGIN_IMPLEMENT();
194