Line data Source code
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/sheet/xdatapilottable2.hxx>
11 : #include <com/sun/star/sheet/XDataPilotTable2.hpp>
12 : #include <com/sun/star/sheet/XDataPilotTable.hpp>
13 : #include <com/sun/star/sheet/DataPilotTableResultData.hpp>
14 : #include <com/sun/star/sheet/XDataPilotDescriptor.hpp>
15 : #include <com/sun/star/sheet/DataPilotFieldOrientation.hpp>
16 : #include <com/sun/star/sheet/DataPilotTablePositionType.hpp>
17 : #include <com/sun/star/sheet/DataPilotOutputRangeType.hpp>
18 : #include <com/sun/star/sheet/XSpreadsheets.hpp>
19 : #include <com/sun/star/sheet/XCellAddressable.hpp>
20 : #include <com/sun/star/table/XCellCursor.hpp>
21 : #include <com/sun/star/sheet/XCellRangeData.hpp>
22 : #include <com/sun/star/sheet/DataResult.hpp>
23 : #include <com/sun/star/beans/XPropertySet.hpp>
24 : #include "cppunit/extensions/HelperMacros.h"
25 :
26 : using namespace css;
27 : using namespace css::uno;
28 :
29 : namespace apitest {
30 :
31 16 : XDataPilotTable2::~XDataPilotTable2()
32 : {
33 16 : }
34 :
35 1 : void XDataPilotTable2::testGetPositionData()
36 : {
37 1 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
38 1 : getOutputRanges(xDPTable);
39 1 : table::CellAddress aAddr;
40 :
41 1 : aAddr.Sheet = maRangeTable.Sheet;
42 9 : for (sal_Int32 x = maRangeTable.StartColumn; x <= maRangeTable.EndColumn; ++x)
43 : {
44 80 : for (sal_Int32 y = maRangeTable.StartRow; y <= maRangeTable.EndRow; ++y)
45 : {
46 72 : aAddr.Column = x;
47 72 : aAddr.Row = y;
48 :
49 72 : sheet::DataPilotTablePositionData aPosData = xDPTable->getPositionData(aAddr);
50 72 : if (aPosData.PositionType == sheet::DataPilotTablePositionType::NOT_IN_TABLE)
51 : {
52 0 : CPPUNIT_ASSERT(false);
53 : }
54 72 : }
55 1 : }
56 1 : }
57 :
58 1 : void XDataPilotTable2::testGetDrillDownData()
59 : {
60 1 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
61 :
62 1 : getOutputRanges(xDPTable);
63 1 : buildDataFields(xDPTable);
64 1 : buildResultCells(xDPTable);
65 :
66 78 : for (std::vector<table::CellAddress>::iterator itr = maResultCells.begin();
67 52 : itr != maResultCells.end(); ++itr)
68 : {
69 25 : sheet::DataPilotTablePositionData aPosData = xDPTable->getPositionData(*itr);
70 50 : Any aTempAny = aPosData.PositionData;
71 50 : sheet::DataPilotTableResultData aResData;
72 25 : CPPUNIT_ASSERT(aTempAny >>= aResData);
73 25 : sal_Int32 nDim = maDataFieldDims[aResData.DataFieldIndex];
74 25 : sheet::DataResult aRes = aResData.Result;
75 25 : double nVal = aRes.Value;
76 :
77 50 : Sequence< Sequence<Any> > aData = xDPTable->getDrillDownData(*itr);
78 25 : double sum = 0;
79 :
80 25 : if( aData.getLength() > 1 )
81 : {
82 10 : for ( sal_Int32 row = 1; row < aData.getLength(); ++row)
83 : {
84 5 : Any aAny = aData[row][nDim];
85 5 : double nValue = 0;
86 5 : if (aAny >>= nValue)
87 5 : sum += nValue;
88 5 : }
89 : }
90 :
91 25 : std::cout << "Sum: " << sum << "; nVal: " << nVal << std::endl;
92 25 : CPPUNIT_ASSERT(sum == nVal);
93 26 : }
94 :
95 :
96 1 : }
97 :
98 1 : void XDataPilotTable2::testGetOutputRangeByType()
99 : {
100 1 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
101 1 : getOutputRanges(xDPTable);
102 :
103 : // check for wrong arguments
104 1 : bool bCaught = false;
105 : try
106 : {
107 1 : xDPTable->getOutputRangeByType(-1);
108 : }
109 2 : catch ( const lang::IllegalArgumentException& )
110 : {
111 1 : bCaught = true;
112 : }
113 1 : CPPUNIT_ASSERT(bCaught);
114 :
115 1 : bCaught = false;
116 : try
117 : {
118 1 : xDPTable->getOutputRangeByType(100);
119 : }
120 2 : catch ( const lang::IllegalArgumentException& )
121 : {
122 1 : bCaught = true;
123 : }
124 1 : CPPUNIT_ASSERT(bCaught);
125 :
126 : // make sure the whole range is not empty
127 1 : CPPUNIT_ASSERT( maRangeWhole.EndColumn - maRangeWhole.StartColumn > 0);
128 1 : CPPUNIT_ASSERT( maRangeWhole.EndRow - maRangeWhole.StartRow > 0);
129 :
130 : //table range must be of equal width with the whole range, and the same bottom
131 1 : CPPUNIT_ASSERT( maRangeTable.Sheet == maRangeWhole.Sheet );
132 1 : CPPUNIT_ASSERT( maRangeTable.EndRow == maRangeWhole.EndRow );
133 1 : CPPUNIT_ASSERT( maRangeTable.StartColumn == maRangeWhole.StartColumn );
134 1 : CPPUNIT_ASSERT( maRangeTable.EndColumn == maRangeWhole.EndColumn );
135 :
136 : //result range must be smaller than the table range, and must share the same lower-right corner
137 1 : CPPUNIT_ASSERT( maRangeResult.Sheet == maRangeTable.Sheet );
138 1 : CPPUNIT_ASSERT( maRangeResult.StartColumn >= maRangeTable.StartColumn );
139 1 : CPPUNIT_ASSERT( maRangeResult.StartRow >= maRangeTable.StartRow );
140 1 : CPPUNIT_ASSERT( maRangeResult.EndRow == maRangeTable.EndRow );
141 1 : CPPUNIT_ASSERT( maRangeResult.EndColumn == maRangeTable.EndColumn );
142 :
143 1 : }
144 :
145 1 : void XDataPilotTable2::testInsertDrillDownSheet()
146 : {
147 1 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
148 1 : sal_Int32 nCellCount = maResultCells.size();
149 :
150 2 : uno::Reference< sheet::XSpreadsheets > xSheets(getSheets(), UNO_QUERY_THROW);
151 2 : uno::Reference< container::XIndexAccess > xIA(xSheets, UNO_QUERY_THROW);
152 1 : sal_Int32 nSheetCount = xIA->getCount();
153 :
154 1 : for (sal_Int32 i = 0; i < nCellCount; ++i)
155 : {
156 0 : table::CellAddress aAddr = maResultCells[i];
157 0 : uno::Sequence< uno::Sequence< Any > > aData = xDPTable->getDrillDownData(aAddr);
158 0 : xDPTable->insertDrillDownSheet(aAddr);
159 :
160 0 : sal_Int32 nNewSheetCount= xIA->getCount();
161 0 : if (nNewSheetCount == nSheetCount + 1)
162 : {
163 0 : CPPUNIT_ASSERT(aData.getLength() >= 2);
164 0 : uno::Reference< sheet::XSpreadsheet > xSheet(xIA->getByIndex(aAddr.Sheet),UNO_QUERY_THROW);
165 0 : CPPUNIT_ASSERT(xSheet.is());
166 :
167 0 : checkDrillDownSheetContent(xSheet, aData);
168 :
169 0 : uno::Reference< container::XNamed > xNamed(xSheet, UNO_QUERY_THROW);
170 0 : OUString aName = xNamed->getName();
171 0 : xSheets->removeByName(aName);
172 : }
173 0 : else if (nNewSheetCount == nSheetCount)
174 : {
175 0 : if (aData.getLength() > 1)
176 : {
177 0 : CPPUNIT_ASSERT(false);
178 : }
179 : }
180 : else
181 : {
182 0 : CPPUNIT_ASSERT(false);
183 : }
184 :
185 1 : }
186 1 : }
187 :
188 1 : void XDataPilotTable2::buildResultCells( uno::Reference< sheet::XDataPilotTable2 > xDPTable)
189 : {
190 1 : getOutputRanges(xDPTable);
191 1 : maResultCells.clear();
192 :
193 6 : for ( sal_Int32 x = maRangeResult.StartColumn; x < maRangeResult.EndColumn; ++x)
194 : {
195 30 : for( sal_Int32 y = maRangeResult.StartRow; y < maRangeResult.EndRow; ++y)
196 : {
197 25 : table::CellAddress aAddr;
198 25 : aAddr.Sheet = maRangeResult.Sheet;
199 25 : aAddr.Column = x;
200 25 : aAddr.Row = y;
201 25 : sheet::DataPilotTablePositionData aPosData = xDPTable->getPositionData(aAddr);
202 25 : if (aPosData.PositionType != sheet::DataPilotTablePositionType::RESULT)
203 : {
204 0 : CPPUNIT_ASSERT(false);
205 : }
206 25 : maResultCells.push_back(aAddr);
207 25 : }
208 : }
209 1 : }
210 :
211 4 : void XDataPilotTable2::getOutputRanges( uno::Reference< sheet::XDataPilotTable2 > xDPTable)
212 : {
213 4 : maRangeWhole = xDPTable->getOutputRangeByType(sheet::DataPilotOutputRangeType::WHOLE);
214 4 : maRangeTable = xDPTable->getOutputRangeByType(sheet::DataPilotOutputRangeType::TABLE);
215 4 : maRangeResult = xDPTable->getOutputRangeByType(sheet::DataPilotOutputRangeType::RESULT);
216 4 : }
217 :
218 1 : void XDataPilotTable2::buildDataFields( uno::Reference< sheet::XDataPilotTable2 > xDPTable )
219 : {
220 1 : uno::Reference< sheet::XDataPilotDescriptor > xDesc(xDPTable, UNO_QUERY_THROW);
221 2 : uno::Reference< container::XIndexAccess > xIndex(xDesc->getDataPilotFields(), UNO_QUERY_THROW);
222 :
223 1 : sal_Int32 nFieldCount = xIndex->getCount();
224 7 : for( sal_Int32 i = 0; i < nFieldCount; ++i)
225 : {
226 6 : uno::Reference< beans::XPropertySet > xPropSet(xIndex->getByIndex(i), UNO_QUERY_THROW);
227 12 : Any aAny = xPropSet->getPropertyValue("Orientation");
228 : sheet::DataPilotFieldOrientation aOrientation;
229 6 : CPPUNIT_ASSERT( aAny >>= aOrientation );
230 :
231 6 : if ( aOrientation == sheet::DataPilotFieldOrientation_DATA )
232 : {
233 1 : maDataFieldDims.push_back( i );
234 : }
235 7 : }
236 1 : }
237 :
238 : namespace {
239 :
240 0 : table::CellAddress getLastUsedCellAddress( uno::Reference< sheet::XSpreadsheet > xSheet, sal_Int32 nCol, sal_Int32 nRow )
241 : {
242 0 : uno::Reference< sheet::XSheetCellRange > xSheetRange( xSheet->getCellRangeByPosition(nCol, nRow, nCol, nRow), UNO_QUERY_THROW);
243 0 : uno::Reference< sheet::XSheetCellCursor > xCursor = xSheet->createCursorByRange(xSheetRange);
244 0 : uno::Reference< table::XCellCursor > xCellCursor(xCursor, UNO_QUERY_THROW);
245 0 : xCellCursor->gotoEnd();
246 :
247 0 : uno::Reference< sheet::XCellAddressable > xCellAddr(xCursor->getCellByPosition(0, 0), UNO_QUERY_THROW);
248 0 : return xCellAddr->getCellAddress();
249 : }
250 :
251 : }
252 :
253 0 : bool XDataPilotTable2::checkDrillDownSheetContent(uno::Reference< sheet::XSpreadsheet > xSheet, const uno::Sequence< uno::Sequence< Any > >& aData)
254 : {
255 0 : table::CellAddress aLastCell = getLastUsedCellAddress(xSheet, 0, 0);
256 0 : CPPUNIT_ASSERT(aData.getLength() > 0);
257 0 : CPPUNIT_ASSERT(aLastCell.Row);
258 0 : CPPUNIT_ASSERT(aLastCell.Column);
259 :
260 0 : CPPUNIT_ASSERT_EQUAL(aData.getLength(), aLastCell.Row + 1);
261 0 : CPPUNIT_ASSERT_EQUAL(aData[0].getLength(), aLastCell.Column + 1);
262 :
263 0 : uno::Reference< table::XCellRange > xCellRange = xSheet->getCellRangeByPosition(0, 0, aLastCell.Column, aLastCell.Row);
264 0 : uno::Reference< sheet::XCellRangeData > xCellRangeData(xCellRange, UNO_QUERY_THROW);
265 :
266 0 : uno::Sequence< uno::Sequence< Any > > aSheetData = xCellRangeData->getDataArray();
267 0 : for (sal_Int32 x = 0; x < aSheetData.getLength(); ++x)
268 : {
269 0 : for(sal_Int32 y = 0; y < aSheetData[x].getLength(); ++y)
270 : {
271 0 : Any& aCell1 = aSheetData[x][y];
272 0 : const Any& aCell2 = aData[x][y];
273 0 : CPPUNIT_ASSERT(aCell1 == aCell2);
274 : }
275 : }
276 0 : return true;
277 : }
278 :
279 90 : }
280 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|