Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * Copyright (C) 2012 Markus Mohrhard <markus.mohrhard@googlemail.com> (initial developer)
17 : *
18 : * All Rights Reserved.
19 : *
20 : * For minor contributions see the git repository.
21 : *
22 : * Alternatively, the contents of this file may be used under the terms of
23 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26 : * instead of those above.
27 : */
28 :
29 : #include <test/sheet/xdatapilottable2.hxx>
30 : #include <com/sun/star/sheet/XDataPilotTable2.hpp>
31 : #include <com/sun/star/sheet/XDataPilotTable.hpp>
32 : #include <com/sun/star/sheet/DataPilotTableResultData.hpp>
33 : #include <com/sun/star/sheet/XDataPilotDescriptor.hpp>
34 : #include <com/sun/star/sheet/DataPilotFieldOrientation.hpp>
35 : #include <com/sun/star/sheet/DataPilotTablePositionType.hpp>
36 : #include <com/sun/star/sheet/DataPilotOutputRangeType.hpp>
37 : #include <com/sun/star/sheet/XSpreadsheets.hpp>
38 : #include <com/sun/star/sheet/XCellAddressable.hpp>
39 : #include <com/sun/star/table/XCellCursor.hpp>
40 : #include <com/sun/star/sheet/XCellRangeData.hpp>
41 : #include <com/sun/star/sheet/DataResult.hpp>
42 : #include <com/sun/star/beans/XPropertySet.hpp>
43 : #include "cppunit/extensions/HelperMacros.h"
44 :
45 : using namespace com::sun::star::uno;
46 :
47 : namespace apitest {
48 :
49 0 : XDataPilotTable2::~XDataPilotTable2()
50 : {
51 0 : }
52 :
53 0 : void XDataPilotTable2::testGetPositionData()
54 : {
55 0 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
56 0 : getOutputRanges(xDPTable);
57 0 : table::CellAddress aAddr;
58 :
59 0 : aAddr.Sheet = maRangeTable.Sheet;
60 0 : for (sal_Int32 x = maRangeTable.StartColumn; x <= maRangeTable.EndColumn; ++x)
61 : {
62 0 : for (sal_Int32 y = maRangeTable.StartRow; y <= maRangeTable.EndRow; ++y)
63 : {
64 0 : aAddr.Column = x;
65 0 : aAddr.Row = y;
66 :
67 0 : sheet::DataPilotTablePositionData aPosData = xDPTable->getPositionData(aAddr);
68 0 : if (aPosData.PositionType == sheet::DataPilotTablePositionType::NOT_IN_TABLE)
69 : {
70 0 : CPPUNIT_ASSERT(false);
71 : }
72 0 : }
73 0 : }
74 0 : }
75 :
76 0 : void XDataPilotTable2::testGetDrillDownData()
77 : {
78 0 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
79 :
80 0 : getOutputRanges(xDPTable);
81 0 : buildDataFields(xDPTable);
82 0 : buildResultCells(xDPTable);
83 :
84 0 : for (std::vector<table::CellAddress>::iterator itr = maResultCells.begin();
85 0 : itr != maResultCells.end(); ++itr)
86 : {
87 0 : sheet::DataPilotTablePositionData aPosData = xDPTable->getPositionData(*itr);
88 0 : Any aTempAny = aPosData.PositionData;
89 0 : sheet::DataPilotTableResultData aResData;
90 0 : CPPUNIT_ASSERT(aTempAny >>= aResData);
91 0 : sal_Int32 nDim = maDataFieldDims[aResData.DataFieldIndex];
92 0 : sheet::DataResult aRes = aResData.Result;
93 0 : double nVal = aRes.Value;
94 :
95 0 : Sequence< Sequence<Any> > aData = xDPTable->getDrillDownData(*itr);
96 0 : double sum = 0;
97 :
98 0 : if( aData.getLength() > 1 )
99 : {
100 0 : for ( sal_Int32 row = 1; row < aData.getLength(); ++row)
101 : {
102 0 : Any aAny = aData[row][nDim];
103 0 : double nValue = 0;
104 0 : if (aAny >>= nValue)
105 0 : sum += nValue;
106 0 : }
107 : }
108 :
109 0 : std::cout << "Sum: " << sum << "; nVal: " << nVal << std::endl;
110 0 : CPPUNIT_ASSERT(sum == nVal);
111 0 : }
112 :
113 :
114 0 : }
115 :
116 0 : void XDataPilotTable2::testGetOutputRangeByType()
117 : {
118 0 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
119 0 : getOutputRanges(xDPTable);
120 :
121 : // check for wrong arguments
122 0 : bool bCaught = false;
123 : try
124 : {
125 0 : xDPTable->getOutputRangeByType(-1);
126 : }
127 0 : catch ( const lang::IllegalArgumentException& )
128 : {
129 0 : bCaught = true;
130 : }
131 0 : CPPUNIT_ASSERT(bCaught);
132 :
133 0 : bCaught = false;
134 : try
135 : {
136 0 : xDPTable->getOutputRangeByType(100);
137 : }
138 0 : catch ( const lang::IllegalArgumentException& )
139 : {
140 0 : bCaught = true;
141 : }
142 0 : CPPUNIT_ASSERT(bCaught);
143 :
144 : // make sure the whole range is not empty
145 0 : CPPUNIT_ASSERT( maRangeWhole.EndColumn - maRangeWhole.StartColumn > 0);
146 0 : CPPUNIT_ASSERT( maRangeWhole.EndRow - maRangeWhole.StartRow > 0);
147 :
148 : //table range must be of equal width with the whole range, and the same bottom
149 0 : CPPUNIT_ASSERT( maRangeTable.Sheet == maRangeWhole.Sheet );
150 0 : CPPUNIT_ASSERT( maRangeTable.EndRow == maRangeWhole.EndRow );
151 0 : CPPUNIT_ASSERT( maRangeTable.StartColumn == maRangeWhole.StartColumn );
152 0 : CPPUNIT_ASSERT( maRangeTable.EndColumn == maRangeWhole.EndColumn );
153 :
154 : //result range must be smaller than the table range, and must share the same lower-right corner
155 0 : CPPUNIT_ASSERT( maRangeResult.Sheet == maRangeTable.Sheet );
156 0 : CPPUNIT_ASSERT( maRangeResult.StartColumn >= maRangeTable.StartColumn );
157 0 : CPPUNIT_ASSERT( maRangeResult.StartRow >= maRangeTable.StartRow );
158 0 : CPPUNIT_ASSERT( maRangeResult.EndRow == maRangeTable.EndRow );
159 0 : CPPUNIT_ASSERT( maRangeResult.EndColumn == maRangeTable.EndColumn );
160 :
161 0 : }
162 :
163 0 : void XDataPilotTable2::testInsertDrillDownSheet()
164 : {
165 0 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
166 0 : sal_Int32 nCellCount = maResultCells.size();
167 :
168 0 : uno::Reference< sheet::XSpreadsheets > xSheets(getSheets(), UNO_QUERY_THROW);
169 0 : uno::Reference< container::XIndexAccess > xIA(xSheets, UNO_QUERY_THROW);
170 0 : sal_Int32 nSheetCount = xIA->getCount();
171 :
172 0 : for (sal_Int32 i = 0; i < nCellCount; ++i)
173 : {
174 0 : table::CellAddress aAddr = maResultCells[i];
175 0 : uno::Sequence< uno::Sequence< Any > > aData = xDPTable->getDrillDownData(aAddr);
176 0 : xDPTable->insertDrillDownSheet(aAddr);
177 :
178 0 : sal_Int32 nNewSheetCount= xIA->getCount();
179 0 : if (nNewSheetCount == nSheetCount + 1)
180 : {
181 0 : CPPUNIT_ASSERT(aData.getLength() >= 2);
182 0 : uno::Reference< sheet::XSpreadsheet > xSheet(xIA->getByIndex(aAddr.Sheet),UNO_QUERY_THROW);
183 0 : CPPUNIT_ASSERT(xSheet.is());
184 :
185 0 : checkDrillDownSheetContent(xSheet, aData);
186 :
187 0 : uno::Reference< container::XNamed > xNamed(xSheet, UNO_QUERY_THROW);
188 0 : rtl::OUString aName = xNamed->getName();
189 0 : xSheets->removeByName(aName);
190 : }
191 0 : else if (nNewSheetCount == nSheetCount)
192 : {
193 0 : if (aData.getLength() > 1)
194 : {
195 0 : CPPUNIT_ASSERT(false);
196 : }
197 : }
198 : else
199 : {
200 0 : CPPUNIT_ASSERT(false);
201 : }
202 :
203 0 : }
204 0 : }
205 :
206 0 : void XDataPilotTable2::buildResultCells( uno::Reference< sheet::XDataPilotTable2 > xDPTable)
207 : {
208 0 : getOutputRanges(xDPTable);
209 0 : maResultCells.clear();
210 :
211 0 : for ( sal_Int32 x = maRangeResult.StartColumn; x < maRangeResult.EndColumn; ++x)
212 : {
213 0 : for( sal_Int32 y = maRangeResult.StartRow; y < maRangeResult.EndRow; ++y)
214 : {
215 0 : table::CellAddress aAddr;
216 0 : aAddr.Sheet = maRangeResult.Sheet;
217 0 : aAddr.Column = x;
218 0 : aAddr.Row = y;
219 0 : sheet::DataPilotTablePositionData aPosData = xDPTable->getPositionData(aAddr);
220 0 : if (aPosData.PositionType != sheet::DataPilotTablePositionType::RESULT)
221 : {
222 0 : CPPUNIT_ASSERT(false);
223 : }
224 0 : maResultCells.push_back(aAddr);
225 0 : }
226 : }
227 0 : }
228 :
229 0 : void XDataPilotTable2::getOutputRanges( uno::Reference< sheet::XDataPilotTable2 > xDPTable)
230 : {
231 0 : maRangeWhole = xDPTable->getOutputRangeByType(sheet::DataPilotOutputRangeType::WHOLE);
232 0 : maRangeTable = xDPTable->getOutputRangeByType(sheet::DataPilotOutputRangeType::TABLE);
233 0 : maRangeResult = xDPTable->getOutputRangeByType(sheet::DataPilotOutputRangeType::RESULT);
234 0 : }
235 :
236 0 : void XDataPilotTable2::buildDataFields( uno::Reference< sheet::XDataPilotTable2 > xDPTable )
237 : {
238 0 : uno::Reference< sheet::XDataPilotDescriptor > xDesc(xDPTable, UNO_QUERY_THROW);
239 0 : uno::Reference< container::XIndexAccess > xIndex(xDesc->getDataPilotFields(), UNO_QUERY_THROW);
240 :
241 0 : sal_Int32 nFieldCount = xIndex->getCount();
242 0 : for( sal_Int32 i = 0; i < nFieldCount; ++i)
243 : {
244 0 : uno::Reference< beans::XPropertySet > xPropSet(xIndex->getByIndex(i), UNO_QUERY_THROW);
245 0 : Any aAny = xPropSet->getPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Orientation")));
246 : sheet::DataPilotFieldOrientation aOrientation;
247 0 : CPPUNIT_ASSERT( aAny >>= aOrientation );
248 :
249 0 : if ( aOrientation == sheet::DataPilotFieldOrientation_DATA )
250 : {
251 0 : maDataFieldDims.push_back( i );
252 : }
253 0 : }
254 0 : }
255 :
256 : namespace {
257 :
258 0 : table::CellAddress getLastUsedCellAddress( uno::Reference< sheet::XSpreadsheet > xSheet, sal_Int32 nCol, sal_Int32 nRow )
259 : {
260 0 : uno::Reference< sheet::XSheetCellRange > xSheetRange( xSheet->getCellRangeByPosition(nCol, nRow, nCol, nRow), UNO_QUERY_THROW);
261 0 : uno::Reference< sheet::XSheetCellCursor > xCursor = xSheet->createCursorByRange(xSheetRange);
262 0 : uno::Reference< table::XCellCursor > xCellCursor(xCursor, UNO_QUERY_THROW);
263 0 : xCellCursor->gotoEnd();
264 :
265 0 : uno::Reference< sheet::XCellAddressable > xCellAddr(xCursor->getCellByPosition(0, 0), UNO_QUERY_THROW);
266 0 : return xCellAddr->getCellAddress();
267 : }
268 :
269 : }
270 :
271 0 : bool XDataPilotTable2::checkDrillDownSheetContent(uno::Reference< sheet::XSpreadsheet > xSheet, uno::Sequence< uno::Sequence< Any > > aData)
272 : {
273 0 : table::CellAddress aLastCell = getLastUsedCellAddress(xSheet, 0, 0);
274 0 : CPPUNIT_ASSERT(aData.getLength() > 0);
275 0 : CPPUNIT_ASSERT(aLastCell.Row);
276 0 : CPPUNIT_ASSERT(aLastCell.Column);
277 :
278 0 : CPPUNIT_ASSERT_EQUAL(aData.getLength(), aLastCell.Row + 1);
279 0 : CPPUNIT_ASSERT_EQUAL(aData[0].getLength(), aLastCell.Column + 1);
280 :
281 0 : uno::Reference< table::XCellRange > xCellRange = xSheet->getCellRangeByPosition(0, 0, aLastCell.Column, aLastCell.Row);
282 0 : uno::Reference< sheet::XCellRangeData > xCellRangeData(xCellRange, UNO_QUERY_THROW);
283 :
284 0 : uno::Sequence< uno::Sequence< Any > > aSheetData = xCellRangeData->getDataArray();
285 0 : for (sal_Int32 x = 0; x < aSheetData.getLength(); ++x)
286 : {
287 0 : for(sal_Int32 y = 0; y < aSheetData[x].getLength(); ++y)
288 : {
289 0 : Any& aCell1 = aSheetData[x][y];
290 0 : Any& aCell2 = aData[x][y];
291 0 : CPPUNIT_ASSERT(aCell1 == aCell2);
292 : }
293 : }
294 0 : return true;
295 : }
296 :
297 0 : }
298 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|