1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
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"
27 using namespace css::uno
;
31 XDataPilotTable2::~XDataPilotTable2()
35 void XDataPilotTable2::testGetPositionData()
37 uno::Reference
< sheet::XDataPilotTable2
> xDPTable(initDP2(), UNO_QUERY_THROW
);
38 getOutputRanges(xDPTable
);
39 table::CellAddress aAddr
;
41 aAddr
.Sheet
= maRangeTable
.Sheet
;
42 for (sal_Int32 x
= maRangeTable
.StartColumn
; x
<= maRangeTable
.EndColumn
; ++x
)
44 for (sal_Int32 y
= maRangeTable
.StartRow
; y
<= maRangeTable
.EndRow
; ++y
)
49 sheet::DataPilotTablePositionData aPosData
= xDPTable
->getPositionData(aAddr
);
50 if (aPosData
.PositionType
== sheet::DataPilotTablePositionType::NOT_IN_TABLE
)
52 CPPUNIT_ASSERT(false);
58 void XDataPilotTable2::testGetDrillDownData()
60 uno::Reference
< sheet::XDataPilotTable2
> xDPTable(initDP2(), UNO_QUERY_THROW
);
62 getOutputRanges(xDPTable
);
63 buildDataFields(xDPTable
);
64 buildResultCells(xDPTable
);
66 for (std::vector
<table::CellAddress
>::iterator itr
= maResultCells
.begin();
67 itr
!= maResultCells
.end(); ++itr
)
69 sheet::DataPilotTablePositionData aPosData
= xDPTable
->getPositionData(*itr
);
70 Any aTempAny
= aPosData
.PositionData
;
71 sheet::DataPilotTableResultData aResData
;
72 CPPUNIT_ASSERT(aTempAny
>>= aResData
);
73 sal_Int32 nDim
= maDataFieldDims
[aResData
.DataFieldIndex
];
74 sheet::DataResult aRes
= aResData
.Result
;
75 double nVal
= aRes
.Value
;
77 Sequence
< Sequence
<Any
> > aData
= xDPTable
->getDrillDownData(*itr
);
80 if( aData
.getLength() > 1 )
82 for ( sal_Int32 row
= 1; row
< aData
.getLength(); ++row
)
84 Any aAny
= aData
[row
][nDim
];
91 std::cout
<< "Sum: " << sum
<< "; nVal: " << nVal
<< std::endl
;
92 CPPUNIT_ASSERT(sum
== nVal
);
98 void XDataPilotTable2::testGetOutputRangeByType()
100 uno::Reference
< sheet::XDataPilotTable2
> xDPTable(initDP2(), UNO_QUERY_THROW
);
101 getOutputRanges(xDPTable
);
103 // check for wrong arguments
104 bool bCaught
= false;
107 xDPTable
->getOutputRangeByType(-1);
109 catch ( const lang::IllegalArgumentException
& )
113 CPPUNIT_ASSERT(bCaught
);
118 xDPTable
->getOutputRangeByType(100);
120 catch ( const lang::IllegalArgumentException
& )
124 CPPUNIT_ASSERT(bCaught
);
126 // make sure the whole range is not empty
127 CPPUNIT_ASSERT( maRangeWhole
.EndColumn
- maRangeWhole
.StartColumn
> 0);
128 CPPUNIT_ASSERT( maRangeWhole
.EndRow
- maRangeWhole
.StartRow
> 0);
130 //table range must be of equal width with the whole range, and the same bottom
131 CPPUNIT_ASSERT( maRangeTable
.Sheet
== maRangeWhole
.Sheet
);
132 CPPUNIT_ASSERT( maRangeTable
.EndRow
== maRangeWhole
.EndRow
);
133 CPPUNIT_ASSERT( maRangeTable
.StartColumn
== maRangeWhole
.StartColumn
);
134 CPPUNIT_ASSERT( maRangeTable
.EndColumn
== maRangeWhole
.EndColumn
);
136 //result range must be smaller than the table range, and must share the same lower-right corner
137 CPPUNIT_ASSERT( maRangeResult
.Sheet
== maRangeTable
.Sheet
);
138 CPPUNIT_ASSERT( maRangeResult
.StartColumn
>= maRangeTable
.StartColumn
);
139 CPPUNIT_ASSERT( maRangeResult
.StartRow
>= maRangeTable
.StartRow
);
140 CPPUNIT_ASSERT( maRangeResult
.EndRow
== maRangeTable
.EndRow
);
141 CPPUNIT_ASSERT( maRangeResult
.EndColumn
== maRangeTable
.EndColumn
);
145 void XDataPilotTable2::testInsertDrillDownSheet()
147 uno::Reference
< sheet::XDataPilotTable2
> xDPTable(initDP2(), UNO_QUERY_THROW
);
148 sal_Int32 nCellCount
= maResultCells
.size();
150 uno::Reference
< sheet::XSpreadsheets
> xSheets(getSheets(), UNO_QUERY_THROW
);
151 uno::Reference
< container::XIndexAccess
> xIA(xSheets
, UNO_QUERY_THROW
);
152 sal_Int32 nSheetCount
= xIA
->getCount();
154 for (sal_Int32 i
= 0; i
< nCellCount
; ++i
)
156 table::CellAddress aAddr
= maResultCells
[i
];
157 uno::Sequence
< uno::Sequence
< Any
> > aData
= xDPTable
->getDrillDownData(aAddr
);
158 xDPTable
->insertDrillDownSheet(aAddr
);
160 sal_Int32 nNewSheetCount
= xIA
->getCount();
161 if (nNewSheetCount
== nSheetCount
+ 1)
163 CPPUNIT_ASSERT(aData
.getLength() >= 2);
164 uno::Reference
< sheet::XSpreadsheet
> xSheet(xIA
->getByIndex(aAddr
.Sheet
),UNO_QUERY_THROW
);
165 CPPUNIT_ASSERT(xSheet
.is());
167 checkDrillDownSheetContent(xSheet
, aData
);
169 uno::Reference
< container::XNamed
> xNamed(xSheet
, UNO_QUERY_THROW
);
170 OUString aName
= xNamed
->getName();
171 xSheets
->removeByName(aName
);
173 else if (nNewSheetCount
== nSheetCount
)
175 if (aData
.getLength() > 1)
177 CPPUNIT_ASSERT(false);
182 CPPUNIT_ASSERT(false);
188 void XDataPilotTable2::buildResultCells( uno::Reference
< sheet::XDataPilotTable2
> xDPTable
)
190 getOutputRanges(xDPTable
);
191 maResultCells
.clear();
193 for ( sal_Int32 x
= maRangeResult
.StartColumn
; x
< maRangeResult
.EndColumn
; ++x
)
195 for( sal_Int32 y
= maRangeResult
.StartRow
; y
< maRangeResult
.EndRow
; ++y
)
197 table::CellAddress aAddr
;
198 aAddr
.Sheet
= maRangeResult
.Sheet
;
201 sheet::DataPilotTablePositionData aPosData
= xDPTable
->getPositionData(aAddr
);
202 if (aPosData
.PositionType
!= sheet::DataPilotTablePositionType::RESULT
)
204 CPPUNIT_ASSERT(false);
206 maResultCells
.push_back(aAddr
);
211 void XDataPilotTable2::getOutputRanges( uno::Reference
< sheet::XDataPilotTable2
> xDPTable
)
213 maRangeWhole
= xDPTable
->getOutputRangeByType(sheet::DataPilotOutputRangeType::WHOLE
);
214 maRangeTable
= xDPTable
->getOutputRangeByType(sheet::DataPilotOutputRangeType::TABLE
);
215 maRangeResult
= xDPTable
->getOutputRangeByType(sheet::DataPilotOutputRangeType::RESULT
);
218 void XDataPilotTable2::buildDataFields( uno::Reference
< sheet::XDataPilotTable2
> xDPTable
)
220 uno::Reference
< sheet::XDataPilotDescriptor
> xDesc(xDPTable
, UNO_QUERY_THROW
);
221 uno::Reference
< container::XIndexAccess
> xIndex(xDesc
->getDataPilotFields(), UNO_QUERY_THROW
);
223 sal_Int32 nFieldCount
= xIndex
->getCount();
224 for( sal_Int32 i
= 0; i
< nFieldCount
; ++i
)
226 uno::Reference
< beans::XPropertySet
> xPropSet(xIndex
->getByIndex(i
), UNO_QUERY_THROW
);
227 Any aAny
= xPropSet
->getPropertyValue("Orientation");
228 sheet::DataPilotFieldOrientation aOrientation
;
229 CPPUNIT_ASSERT( aAny
>>= aOrientation
);
231 if ( aOrientation
== sheet::DataPilotFieldOrientation_DATA
)
233 maDataFieldDims
.push_back( i
);
240 table::CellAddress
getLastUsedCellAddress( uno::Reference
< sheet::XSpreadsheet
> xSheet
, sal_Int32 nCol
, sal_Int32 nRow
)
242 uno::Reference
< sheet::XSheetCellRange
> xSheetRange( xSheet
->getCellRangeByPosition(nCol
, nRow
, nCol
, nRow
), UNO_QUERY_THROW
);
243 uno::Reference
< sheet::XSheetCellCursor
> xCursor
= xSheet
->createCursorByRange(xSheetRange
);
244 uno::Reference
< table::XCellCursor
> xCellCursor(xCursor
, UNO_QUERY_THROW
);
245 xCellCursor
->gotoEnd();
247 uno::Reference
< sheet::XCellAddressable
> xCellAddr(xCursor
->getCellByPosition(0, 0), UNO_QUERY_THROW
);
248 return xCellAddr
->getCellAddress();
253 bool XDataPilotTable2::checkDrillDownSheetContent(uno::Reference
< sheet::XSpreadsheet
> xSheet
, const uno::Sequence
< uno::Sequence
< Any
> >& aData
)
255 table::CellAddress aLastCell
= getLastUsedCellAddress(xSheet
, 0, 0);
256 CPPUNIT_ASSERT(aData
.getLength() > 0);
257 CPPUNIT_ASSERT(aLastCell
.Row
);
258 CPPUNIT_ASSERT(aLastCell
.Column
);
260 CPPUNIT_ASSERT_EQUAL(aData
.getLength(), aLastCell
.Row
+ 1);
261 CPPUNIT_ASSERT_EQUAL(aData
[0].getLength(), aLastCell
.Column
+ 1);
263 uno::Reference
< table::XCellRange
> xCellRange
= xSheet
->getCellRangeByPosition(0, 0, aLastCell
.Column
, aLastCell
.Row
);
264 uno::Reference
< sheet::XCellRangeData
> xCellRangeData(xCellRange
, UNO_QUERY_THROW
);
266 uno::Sequence
< uno::Sequence
< Any
> > aSheetData
= xCellRangeData
->getDataArray();
267 for (sal_Int32 x
= 0; x
< aSheetData
.getLength(); ++x
)
269 for(sal_Int32 y
= 0; y
< aSheetData
[x
].getLength(); ++y
)
271 Any
& aCell1
= aSheetData
[x
][y
];
272 const Any
& aCell2
= aData
[x
][y
];
273 CPPUNIT_ASSERT(aCell1
== aCell2
);
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */