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/sheet/xdatapilottable.hxx>
25 #include <test/sheet/xdatapilotdescriptor.hxx>
26 #include <test/beans/xpropertyset.hxx>
27 //check the DataPilot of Calc.
28 
29 using namespace css;
30 using namespace css::lang;
31 
32 namespace sc_apitest {
33 
34 class CheckDataPilotTable : public CalcUnoApiTest, public apitest::XNamed, public apitest::XDataPilotTable, public apitest::XPropertySet, public apitest::XDataPilotDescriptor
35 {
36 public:
37     CheckDataPilotTable();
38 
39     virtual void setUp() override;
40     virtual void tearDown() override;
41 
42     uno::Reference< uno::XInterface > init() override;
43 
44     CPPUNIT_TEST_SUITE(CheckDataPilotTable);
45     // _XNamed
46     CPPUNIT_TEST(testGetName);
47     CPPUNIT_TEST(testSetName);
48 
49     // _XDataPilotTable
50     CPPUNIT_TEST(testGetOutputRange);
51 
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);
61 
62     CPPUNIT_TEST_SUITE_END();
63 
64 private:
65     uno::Reference<lang::XComponent> mxComponent;
66     uno::Reference<uno::XInterface> mxObject;
67     static constexpr int MAX_FIELD_INDEX = 6;
68 };
69 
CheckDataPilotTable()70 CheckDataPilotTable::CheckDataPilotTable()
71      : CalcUnoApiTest("/sc/qa/extras/testdocuments"),
72        apitest::XNamed("DataPilotTable")
73 {
74 }
75 
init()76 uno::Reference< uno::XInterface > CheckDataPilotTable::init()
77 {
78     // create a calc document
79     if (!mxComponent.is())
80         // Load an empty document.
81         mxComponent = loadFromDesktop("private:factory/scalc");
82     else
83         return mxObject;
84 
85     uno::Reference< sheet::XSpreadsheetDocument > xSheetDoc(mxComponent, uno::UNO_QUERY_THROW);
86 
87     // the cell range
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;
94 
95     // position of the data pilot table
96     table::CellAddress sCellAdress;
97     sCellAdress.Sheet = 0;
98     sCellAdress.Column = 7;
99     sCellAdress.Row = 8;
100     // Getting spreadsheet
101     uno::Reference< sheet::XSpreadsheets > xSpreadsheets = xSheetDoc->getSheets();
102     uno::Reference< container::XIndexAccess > oIndexAccess(xSpreadsheets, uno::UNO_QUERY_THROW);
103 
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);
109 
110     uno::Any aAny2 = oIndexAccess->getByIndex(1);
111     uno::Reference< sheet::XSpreadsheet > oSheet2;
112     CPPUNIT_ASSERT(aAny2 >>= oSheet2);
113 
114     //Filling a table
115     for (int i = 1; i < MAX_FIELD_INDEX; i++)
116     {
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));
121     }
122 
123     for (int i = 1; i < MAX_FIELD_INDEX; i++)
124     {
125         for (int j = 1; j < MAX_FIELD_INDEX; j++)
126         {
127             oSheet->getCellByPosition(i, j)->setValue(i * (j + 1));
128             oSheet2->getCellByPosition(i, j)->setValue(i * (j + 2));
129          }
130     }
131 
132     // change a value of a cell and check the change in the data pilot
133     // cell of data
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
139     uno::Any oCheckCell;
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);
146 
147     uno::Any oDataPilotField = DPDsc->getDataPilotFields()->getByIndex(0);
148     uno::Reference<beans::XPropertySet> fieldPropSet(oDataPilotField,  uno::UNO_QUERY_THROW);
149 
150     uno::Any sum;
151     sum<<= sheet::GeneralFunction_SUM;
152     fieldPropSet->setPropertyValue("Function", sum );
153 
154     uno::Any data;
155     data<<= sheet::DataPilotFieldOrientation_DATA;
156     fieldPropSet->setPropertyValue("Orientation", data);
157 
158     //Insert the DataPilotTable
159     if (DPT->hasByName("DataPilotTable"))
160         DPT->removeByName("DataPilotTable");
161     DPT->insertNewByName("DataPilotTable", sCellAdress, DPDsc);
162 
163     uno::Reference<uno::XInterface> xDataPilotTableObject;
164     data = DPT->getByName( DPT->getElementNames()[0] );
165     CPPUNIT_ASSERT(data >>= xDataPilotTableObject);
166     mxObject = xDataPilotTableObject;
167 
168     return xDataPilotTableObject;
169 }
170 
setUp()171 void CheckDataPilotTable::setUp()
172 {
173     CalcUnoApiTest::setUp();
174     init();
175 }
176 
tearDown()177 void CheckDataPilotTable::tearDown()
178 {
179     closeDocument(mxComponent);
180     mxComponent.clear();
181     CalcUnoApiTest::tearDown();
182 }
183 
184 CPPUNIT_TEST_SUITE_REGISTRATION(CheckDataPilotTable);
185 
186 }
187 
188 CPPUNIT_PLUGIN_IMPLEMENT();
189