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