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 32 : XDataPilotTable2::~XDataPilotTable2()
32 : {
33 32 : }
34 :
35 2 : void XDataPilotTable2::testGetPositionData()
36 : {
37 2 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
38 2 : getOutputRanges(xDPTable);
39 2 : table::CellAddress aAddr;
40 :
41 2 : aAddr.Sheet = maRangeTable.Sheet;
42 18 : for (sal_Int32 x = maRangeTable.StartColumn; x <= maRangeTable.EndColumn; ++x)
43 : {
44 160 : for (sal_Int32 y = maRangeTable.StartRow; y <= maRangeTable.EndRow; ++y)
45 : {
46 144 : aAddr.Column = x;
47 144 : aAddr.Row = y;
48 :
49 144 : sheet::DataPilotTablePositionData aPosData = xDPTable->getPositionData(aAddr);
50 144 : if (aPosData.PositionType == sheet::DataPilotTablePositionType::NOT_IN_TABLE)
51 : {
52 0 : CPPUNIT_ASSERT(false);
53 : }
54 144 : }
55 2 : }
56 2 : }
57 :
58 2 : void XDataPilotTable2::testGetDrillDownData()
59 : {
60 2 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
61 :
62 2 : getOutputRanges(xDPTable);
63 2 : buildDataFields(xDPTable);
64 2 : buildResultCells(xDPTable);
65 :
66 156 : for (std::vector<table::CellAddress>::iterator itr = maResultCells.begin();
67 104 : itr != maResultCells.end(); ++itr)
68 : {
69 50 : sheet::DataPilotTablePositionData aPosData = xDPTable->getPositionData(*itr);
70 100 : Any aTempAny = aPosData.PositionData;
71 100 : sheet::DataPilotTableResultData aResData;
72 50 : CPPUNIT_ASSERT(aTempAny >>= aResData);
73 50 : sal_Int32 nDim = maDataFieldDims[aResData.DataFieldIndex];
74 50 : sheet::DataResult aRes = aResData.Result;
75 50 : double nVal = aRes.Value;
76 :
77 100 : Sequence< Sequence<Any> > aData = xDPTable->getDrillDownData(*itr);
78 50 : double sum = 0;
79 :
80 50 : if( aData.getLength() > 1 )
81 : {
82 20 : for ( sal_Int32 row = 1; row < aData.getLength(); ++row)
83 : {
84 10 : Any aAny = aData[row][nDim];
85 10 : double nValue = 0;
86 10 : if (aAny >>= nValue)
87 10 : sum += nValue;
88 10 : }
89 : }
90 :
91 50 : std::cout << "Sum: " << sum << "; nVal: " << nVal << std::endl;
92 50 : CPPUNIT_ASSERT(sum == nVal);
93 52 : }
94 :
95 :
96 2 : }
97 :
98 2 : void XDataPilotTable2::testGetOutputRangeByType()
99 : {
100 2 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
101 2 : getOutputRanges(xDPTable);
102 :
103 : // check for wrong arguments
104 2 : bool bCaught = false;
105 : try
106 : {
107 2 : xDPTable->getOutputRangeByType(-1);
108 : }
109 4 : catch ( const lang::IllegalArgumentException& )
110 : {
111 2 : bCaught = true;
112 : }
113 2 : CPPUNIT_ASSERT(bCaught);
114 :
115 2 : bCaught = false;
116 : try
117 : {
118 2 : xDPTable->getOutputRangeByType(100);
119 : }
120 4 : catch ( const lang::IllegalArgumentException& )
121 : {
122 2 : bCaught = true;
123 : }
124 2 : CPPUNIT_ASSERT(bCaught);
125 :
126 : // make sure the whole range is not empty
127 2 : CPPUNIT_ASSERT( maRangeWhole.EndColumn - maRangeWhole.StartColumn > 0);
128 2 : 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 2 : CPPUNIT_ASSERT( maRangeTable.Sheet == maRangeWhole.Sheet );
132 2 : CPPUNIT_ASSERT( maRangeTable.EndRow == maRangeWhole.EndRow );
133 2 : CPPUNIT_ASSERT( maRangeTable.StartColumn == maRangeWhole.StartColumn );
134 2 : 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 2 : CPPUNIT_ASSERT( maRangeResult.Sheet == maRangeTable.Sheet );
138 2 : CPPUNIT_ASSERT( maRangeResult.StartColumn >= maRangeTable.StartColumn );
139 2 : CPPUNIT_ASSERT( maRangeResult.StartRow >= maRangeTable.StartRow );
140 2 : CPPUNIT_ASSERT( maRangeResult.EndRow == maRangeTable.EndRow );
141 2 : CPPUNIT_ASSERT( maRangeResult.EndColumn == maRangeTable.EndColumn );
142 :
143 2 : }
144 :
145 2 : void XDataPilotTable2::testInsertDrillDownSheet()
146 : {
147 2 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
148 2 : sal_Int32 nCellCount = maResultCells.size();
149 :
150 4 : uno::Reference< sheet::XSpreadsheets > xSheets(getSheets(), UNO_QUERY_THROW);
151 4 : uno::Reference< container::XIndexAccess > xIA(xSheets, UNO_QUERY_THROW);
152 2 : sal_Int32 nSheetCount = xIA->getCount();
153 :
154 2 : 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 2 : }
186 2 : }
187 :
188 2 : void XDataPilotTable2::buildResultCells( uno::Reference< sheet::XDataPilotTable2 > xDPTable)
189 : {
190 2 : getOutputRanges(xDPTable);
191 2 : maResultCells.clear();
192 :
193 12 : for ( sal_Int32 x = maRangeResult.StartColumn; x < maRangeResult.EndColumn; ++x)
194 : {
195 60 : for( sal_Int32 y = maRangeResult.StartRow; y < maRangeResult.EndRow; ++y)
196 : {
197 50 : table::CellAddress aAddr;
198 50 : aAddr.Sheet = maRangeResult.Sheet;
199 50 : aAddr.Column = x;
200 50 : aAddr.Row = y;
201 50 : sheet::DataPilotTablePositionData aPosData = xDPTable->getPositionData(aAddr);
202 50 : if (aPosData.PositionType != sheet::DataPilotTablePositionType::RESULT)
203 : {
204 0 : CPPUNIT_ASSERT(false);
205 : }
206 50 : maResultCells.push_back(aAddr);
207 50 : }
208 : }
209 2 : }
210 :
211 8 : void XDataPilotTable2::getOutputRanges( uno::Reference< sheet::XDataPilotTable2 > xDPTable)
212 : {
213 8 : maRangeWhole = xDPTable->getOutputRangeByType(sheet::DataPilotOutputRangeType::WHOLE);
214 8 : maRangeTable = xDPTable->getOutputRangeByType(sheet::DataPilotOutputRangeType::TABLE);
215 8 : maRangeResult = xDPTable->getOutputRangeByType(sheet::DataPilotOutputRangeType::RESULT);
216 8 : }
217 :
218 2 : void XDataPilotTable2::buildDataFields( uno::Reference< sheet::XDataPilotTable2 > xDPTable )
219 : {
220 2 : uno::Reference< sheet::XDataPilotDescriptor > xDesc(xDPTable, UNO_QUERY_THROW);
221 4 : uno::Reference< container::XIndexAccess > xIndex(xDesc->getDataPilotFields(), UNO_QUERY_THROW);
222 :
223 2 : sal_Int32 nFieldCount = xIndex->getCount();
224 14 : for( sal_Int32 i = 0; i < nFieldCount; ++i)
225 : {
226 12 : uno::Reference< beans::XPropertySet > xPropSet(xIndex->getByIndex(i), UNO_QUERY_THROW);
227 24 : Any aAny = xPropSet->getPropertyValue("Orientation");
228 : sheet::DataPilotFieldOrientation aOrientation;
229 12 : CPPUNIT_ASSERT( aAny >>= aOrientation );
230 :
231 12 : if ( aOrientation == sheet::DataPilotFieldOrientation_DATA )
232 : {
233 2 : maDataFieldDims.push_back( i );
234 : }
235 14 : }
236 2 : }
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 144 : }
280 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|