Branch data 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 : 32 : XDataPilotTable2::~XDataPilotTable2()
50 : : {
51 [ - + ]: 32 : }
52 : :
53 : 2 : void XDataPilotTable2::testGetPositionData()
54 : : {
55 [ + - ][ + - ]: 2 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
56 [ + - ]: 2 : getOutputRanges(xDPTable);
57 : 2 : table::CellAddress aAddr;
58 : :
59 : 2 : aAddr.Sheet = maRangeTable.Sheet;
60 [ + + ]: 18 : for (sal_Int32 x = maRangeTable.StartColumn; x <= maRangeTable.EndColumn; ++x)
61 : : {
62 [ + + ]: 160 : for (sal_Int32 y = maRangeTable.StartRow; y <= maRangeTable.EndRow; ++y)
63 : : {
64 : 144 : aAddr.Column = x;
65 : 144 : aAddr.Row = y;
66 : :
67 [ + - ][ + - ]: 144 : sheet::DataPilotTablePositionData aPosData = xDPTable->getPositionData(aAddr);
68 [ - + ]: 144 : if (aPosData.PositionType == sheet::DataPilotTablePositionType::NOT_IN_TABLE)
69 : : {
70 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT(false);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
71 : : }
72 : 144 : }
73 : 2 : }
74 : 2 : }
75 : :
76 : 2 : void XDataPilotTable2::testGetDrillDownData()
77 : : {
78 [ + - ][ + - ]: 2 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
79 : :
80 [ + - ]: 2 : getOutputRanges(xDPTable);
81 [ + - ]: 2 : buildDataFields(xDPTable);
82 [ + - ]: 2 : buildResultCells(xDPTable);
83 : :
84 [ + + ][ + - ]: 104 : for (std::vector<table::CellAddress>::iterator itr = maResultCells.begin();
85 : 52 : itr != maResultCells.end(); ++itr)
86 : : {
87 [ + - ][ + - ]: 50 : sheet::DataPilotTablePositionData aPosData = xDPTable->getPositionData(*itr);
88 : 50 : Any aTempAny = aPosData.PositionData;
89 [ + - ]: 50 : sheet::DataPilotTableResultData aResData;
90 [ + - ][ + - ]: 50 : CPPUNIT_ASSERT(aTempAny >>= aResData);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
91 [ + - ]: 50 : sal_Int32 nDim = maDataFieldDims[aResData.DataFieldIndex];
92 : 50 : sheet::DataResult aRes = aResData.Result;
93 : 50 : double nVal = aRes.Value;
94 : :
95 [ + - ][ + - ]: 50 : Sequence< Sequence<Any> > aData = xDPTable->getDrillDownData(*itr);
96 : 50 : double sum = 0;
97 : :
98 [ + + ]: 50 : if( aData.getLength() > 1 )
99 : : {
100 [ + + ]: 20 : for ( sal_Int32 row = 1; row < aData.getLength(); ++row)
101 : : {
102 [ + - ][ + - ]: 10 : Any aAny = aData[row][nDim];
103 : 10 : double nValue = 0;
104 [ + - ]: 10 : if (aAny >>= nValue)
105 : 10 : sum += nValue;
106 : 10 : }
107 : : }
108 : :
109 [ + - ][ + - ]: 50 : std::cout << "Sum: " << sum << "; nVal: " << nVal << std::endl;
[ + - ][ + - ]
[ + - ]
110 [ + - ][ + - ]: 50 : CPPUNIT_ASSERT(sum == nVal);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
111 [ + - ][ + - ]: 52 : }
112 : :
113 : :
114 : 2 : }
115 : :
116 : 2 : void XDataPilotTable2::testGetOutputRangeByType()
117 : : {
118 [ + - ][ + - ]: 2 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
119 [ - + ][ + - ]: 4 : getOutputRanges(xDPTable);
120 : :
121 : : // check for wrong arguments
122 : 2 : bool bCaught = false;
123 : : try
124 : : {
125 [ - + ][ + - ]: 2 : xDPTable->getOutputRangeByType(-1);
126 : : }
127 [ + - ]: 2 : catch ( const lang::IllegalArgumentException& )
128 : : {
129 : 2 : bCaught = true;
130 : : }
131 [ + - ][ + - ]: 4 : CPPUNIT_ASSERT(bCaught);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ]
132 : :
133 : 2 : bCaught = false;
134 : : try
135 : : {
136 [ - + ][ + - ]: 2 : xDPTable->getOutputRangeByType(100);
137 : : }
138 [ + - ]: 2 : catch ( const lang::IllegalArgumentException& )
139 : : {
140 : 2 : bCaught = true;
141 : : }
142 [ + - ][ + - ]: 2 : CPPUNIT_ASSERT(bCaught);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
143 : :
144 : : // make sure the whole range is not empty
145 [ + - ][ + - ]: 2 : CPPUNIT_ASSERT( maRangeWhole.EndColumn - maRangeWhole.StartColumn > 0);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
146 [ + - ][ + - ]: 2 : 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 [ + - ][ + - ]: 2 : CPPUNIT_ASSERT( maRangeTable.Sheet == maRangeWhole.Sheet );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
150 [ + - ][ + - ]: 2 : CPPUNIT_ASSERT( maRangeTable.EndRow == maRangeWhole.EndRow );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
151 [ + - ][ + - ]: 2 : CPPUNIT_ASSERT( maRangeTable.StartColumn == maRangeWhole.StartColumn );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
152 [ + - ][ + - ]: 2 : 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 [ + - ][ + - ]: 2 : CPPUNIT_ASSERT( maRangeResult.Sheet == maRangeTable.Sheet );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
156 [ + - ][ + - ]: 2 : CPPUNIT_ASSERT( maRangeResult.StartColumn >= maRangeTable.StartColumn );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
157 [ + - ][ + - ]: 2 : CPPUNIT_ASSERT( maRangeResult.StartRow >= maRangeTable.StartRow );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
158 [ + - ][ + - ]: 2 : CPPUNIT_ASSERT( maRangeResult.EndRow == maRangeTable.EndRow );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
159 [ + - ][ + - ]: 2 : CPPUNIT_ASSERT( maRangeResult.EndColumn == maRangeTable.EndColumn );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
160 : :
161 : 2 : }
162 : :
163 : 2 : void XDataPilotTable2::testInsertDrillDownSheet()
164 : : {
165 [ + - ][ + - ]: 2 : uno::Reference< sheet::XDataPilotTable2 > xDPTable(initDP2(), UNO_QUERY_THROW);
166 : 2 : sal_Int32 nCellCount = maResultCells.size();
167 : :
168 [ + - ][ + - ]: 2 : uno::Reference< sheet::XSpreadsheets > xSheets(getSheets(), UNO_QUERY_THROW);
169 [ + - ]: 2 : uno::Reference< container::XIndexAccess > xIA(xSheets, UNO_QUERY_THROW);
170 [ + - ][ + - ]: 2 : sal_Int32 nSheetCount = xIA->getCount();
171 : :
172 [ - + ]: 2 : 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 [ # # ]: 2 : }
204 : 2 : }
205 : :
206 : 2 : void XDataPilotTable2::buildResultCells( uno::Reference< sheet::XDataPilotTable2 > xDPTable)
207 : : {
208 [ + - ]: 2 : getOutputRanges(xDPTable);
209 : 2 : maResultCells.clear();
210 : :
211 [ + + ]: 12 : for ( sal_Int32 x = maRangeResult.StartColumn; x < maRangeResult.EndColumn; ++x)
212 : : {
213 [ + + ]: 60 : for( sal_Int32 y = maRangeResult.StartRow; y < maRangeResult.EndRow; ++y)
214 : : {
215 : 50 : table::CellAddress aAddr;
216 : 50 : aAddr.Sheet = maRangeResult.Sheet;
217 : 50 : aAddr.Column = x;
218 : 50 : aAddr.Row = y;
219 [ + - ][ + - ]: 50 : sheet::DataPilotTablePositionData aPosData = xDPTable->getPositionData(aAddr);
220 [ - + ]: 50 : if (aPosData.PositionType != sheet::DataPilotTablePositionType::RESULT)
221 : : {
222 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT(false);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
223 : : }
224 [ + - ]: 50 : maResultCells.push_back(aAddr);
225 : 50 : }
226 : : }
227 : 2 : }
228 : :
229 : 8 : void XDataPilotTable2::getOutputRanges( uno::Reference< sheet::XDataPilotTable2 > xDPTable)
230 : : {
231 : 8 : maRangeWhole = xDPTable->getOutputRangeByType(sheet::DataPilotOutputRangeType::WHOLE);
232 : 8 : maRangeTable = xDPTable->getOutputRangeByType(sheet::DataPilotOutputRangeType::TABLE);
233 : 8 : maRangeResult = xDPTable->getOutputRangeByType(sheet::DataPilotOutputRangeType::RESULT);
234 : 8 : }
235 : :
236 : 2 : void XDataPilotTable2::buildDataFields( uno::Reference< sheet::XDataPilotTable2 > xDPTable )
237 : : {
238 [ + - ]: 2 : uno::Reference< sheet::XDataPilotDescriptor > xDesc(xDPTable, UNO_QUERY_THROW);
239 [ + - ][ + - ]: 2 : uno::Reference< container::XIndexAccess > xIndex(xDesc->getDataPilotFields(), UNO_QUERY_THROW);
[ + - ]
240 : :
241 [ + - ][ + - ]: 2 : sal_Int32 nFieldCount = xIndex->getCount();
242 [ + + ]: 14 : for( sal_Int32 i = 0; i < nFieldCount; ++i)
243 : : {
244 [ + - ][ + - ]: 12 : uno::Reference< beans::XPropertySet > xPropSet(xIndex->getByIndex(i), UNO_QUERY_THROW);
[ + - ]
245 [ + - ][ + - ]: 12 : Any aAny = xPropSet->getPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Orientation")));
[ + - ]
246 : : sheet::DataPilotFieldOrientation aOrientation;
247 [ + - ][ + - ]: 12 : CPPUNIT_ASSERT( aAny >>= aOrientation );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
248 : :
249 [ + + ]: 12 : if ( aOrientation == sheet::DataPilotFieldOrientation_DATA )
250 : : {
251 [ + - ]: 2 : maDataFieldDims.push_back( i );
252 : : }
253 : 14 : }
254 : 2 : }
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 [ + - ][ + - ]: 66 : }
298 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|