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/bootstrapfixture.hxx>
11 : #include <unotest/macros_test.hxx>
12 : #include <comphelper/processfactory.hxx>
13 :
14 : #include <com/sun/star/lang/XComponent.hpp>
15 : #include <com/sun/star/frame/Desktop.hpp>
16 :
17 : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
18 : #include <com/sun/star/sheet/XSpreadsheet.hpp>
19 : #include <com/sun/star/container/XIndexAccess.hpp>
20 : #include <com/sun/star/table/XTableChartsSupplier.hpp>
21 : #include <com/sun/star/table/XTableCharts.hpp>
22 : #include <com/sun/star/table/XTableChart.hpp>
23 : #include <com/sun/star/document/XEmbeddedObjectSupplier.hpp>
24 : #include <com/sun/star/beans/XPropertySet.hpp>
25 : #include <com/sun/star/frame/XStorable.hpp>
26 : #include <com/sun/star/beans/PropertyValue.hpp>
27 :
28 : #include <unotools/tempfile.hxx>
29 :
30 : #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
31 : #include <com/sun/star/chart2/XAnyDescriptionAccess.hpp>
32 : #include <com/sun/star/chart2/XChartDocument.hpp>
33 : #include <com/sun/star/chart2/XDiagram.hpp>
34 : #include <com/sun/star/chart2/XDataSeries.hpp>
35 : #include <com/sun/star/chart2/XChartTypeContainer.hpp>
36 : #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
37 : #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
38 : #include <com/sun/star/chart2/data/XLabeledDataSequence.hpp>
39 : #include <com/sun/star/chart2/data/XDataSource.hpp>
40 : #include <com/sun/star/chart/XChartDataArray.hpp>
41 : #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
42 : #include <com/sun/star/chart/XChartDocument.hpp>
43 : #include <iostream>
44 :
45 : #include <libxml/xmlwriter.h>
46 : #include <libxml/xpath.h>
47 :
48 : #include <boost/shared_ptr.hpp>
49 : #include <boost/make_shared.hpp>
50 :
51 : using namespace com::sun::star;
52 : using namespace com::sun::star::uno;
53 :
54 39 : class ChartTest : public test::BootstrapFixture, public unotest::MacrosTest
55 : {
56 : public:
57 39 : ChartTest():mbSkipValidation(false) {}
58 : void load( const OUString& rDir, const OUString& rFileName );
59 : boost::shared_ptr<utl::TempFile> reload( const OUString& rFileName );
60 : uno::Sequence < OUString > getImpressChartColumnDescriptions( const char* pDir, const char* pName );
61 : OUString getFileExtension( const OUString& rFileName );
62 :
63 : uno::Reference< chart::XChartDocument > getChartDocFromImpress( const char* pDir, const char* pName );
64 :
65 : virtual void setUp() SAL_OVERRIDE;
66 : virtual void tearDown() SAL_OVERRIDE;
67 :
68 : protected:
69 : Reference< lang::XComponent > mxComponent;
70 : OUString maServiceName;
71 : bool mbSkipValidation; // if you set this flag for a new test I'm going to haunt you!
72 : };
73 :
74 37 : OUString ChartTest::getFileExtension( const OUString& aFileName )
75 : {
76 37 : sal_Int32 nDotLocation = aFileName.lastIndexOf('.');
77 37 : CPPUNIT_ASSERT(nDotLocation != -1);
78 37 : return aFileName.copy(nDotLocation);
79 : }
80 :
81 37 : void ChartTest::load( const OUString& aDir, const OUString& aName )
82 : {
83 37 : OUString extension = getFileExtension(aName);
84 37 : if(extension == "ods" || extension == "xlsx")
85 : {
86 0 : maServiceName = "com.sun.star.sheet.SpreadsheetDocument";
87 : }
88 37 : else if(extension == "docx")
89 : {
90 0 : maServiceName = "com.sun.star.text.TextDocument";
91 : }
92 :
93 37 : mxComponent = loadFromDesktop(getURLFromSrc(aDir) + aName, maServiceName);
94 37 : CPPUNIT_ASSERT(mxComponent.is());
95 37 : }
96 :
97 25 : boost::shared_ptr<utl::TempFile> ChartTest::reload(const OUString& rFilterName)
98 : {
99 25 : uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
100 50 : uno::Sequence<beans::PropertyValue> aArgs(1);
101 25 : aArgs[0].Name = "FilterName";
102 25 : aArgs[0].Value <<= rFilterName;
103 25 : boost::shared_ptr<utl::TempFile> pTempFile = boost::make_shared<utl::TempFile>();
104 25 : pTempFile->EnableKillingFile();
105 25 : xStorable->storeToURL(pTempFile->GetURL(), aArgs);
106 25 : mxComponent->dispose();
107 25 : mxComponent = loadFromDesktop(pTempFile->GetURL(), maServiceName);
108 25 : std::cout << pTempFile->GetURL();
109 25 : if(rFilterName == "Calc Office Open XML")
110 : {
111 5 : validate(pTempFile->GetFileName(), test::OOXML);
112 : }
113 20 : else if(rFilterName == "Office Open XML Text")
114 : {
115 : // validate(pTempFile->GetFileName(), test::OOXML);
116 : }
117 4 : else if(rFilterName == "calc8")
118 : {
119 3 : if(!mbSkipValidation)
120 2 : validate(pTempFile->GetFileName(), test::ODF);
121 : }
122 :
123 25 : CPPUNIT_ASSERT(mxComponent.is());
124 50 : return pTempFile;
125 : }
126 :
127 39 : void ChartTest::setUp()
128 : {
129 39 : test::BootstrapFixture::setUp();
130 :
131 39 : mxDesktop.set( com::sun::star::frame::Desktop::create( comphelper::getComponentContext(getMultiServiceFactory()) ) );
132 39 : }
133 :
134 39 : void ChartTest::tearDown()
135 : {
136 39 : if(mxComponent.is())
137 39 : mxComponent->dispose();
138 :
139 39 : test::BootstrapFixture::tearDown();
140 :
141 39 : }
142 :
143 35 : Reference< lang::XComponent > getChartCompFromSheet( sal_Int32 nSheet, uno::Reference< lang::XComponent > xComponent )
144 : {
145 : // let us assume that we only have one chart per sheet
146 :
147 35 : uno::Reference< sheet::XSpreadsheetDocument > xDoc(xComponent, UNO_QUERY_THROW);
148 35 : CPPUNIT_ASSERT(xDoc.is());
149 :
150 70 : uno::Reference< container::XIndexAccess > xIA(xDoc->getSheets(), UNO_QUERY_THROW);
151 35 : CPPUNIT_ASSERT(xIA.is());
152 :
153 70 : uno::Reference< table::XTableChartsSupplier > xChartSupplier( xIA->getByIndex(nSheet), UNO_QUERY_THROW);
154 35 : CPPUNIT_ASSERT(xChartSupplier.is());
155 :
156 70 : uno::Reference< table::XTableCharts > xCharts = xChartSupplier->getCharts();
157 35 : CPPUNIT_ASSERT(xCharts.is());
158 :
159 70 : uno::Reference< container::XIndexAccess > xIACharts(xCharts, UNO_QUERY_THROW);
160 70 : uno::Reference< table::XTableChart > xChart( xIACharts->getByIndex(0), UNO_QUERY_THROW);
161 35 : CPPUNIT_ASSERT(xChart.is());
162 :
163 70 : uno::Reference< document::XEmbeddedObjectSupplier > xEmbObjectSupplier(xChart, UNO_QUERY_THROW);
164 35 : CPPUNIT_ASSERT(xEmbObjectSupplier.is());
165 :
166 35 : uno::Reference< lang::XComponent > xChartComp( xEmbObjectSupplier->getEmbeddedObject(), UNO_QUERY_THROW );
167 35 : CPPUNIT_ASSERT(xChartComp.is());
168 :
169 70 : return xChartComp;
170 :
171 : }
172 :
173 32 : Reference< chart2::XChartDocument > getChartDocFromSheet( sal_Int32 nSheet, uno::Reference< lang::XComponent > xComponent )
174 : {
175 32 : uno::Reference< chart2::XChartDocument > xChartDoc ( getChartCompFromSheet(nSheet, xComponent), UNO_QUERY_THROW );
176 32 : CPPUNIT_ASSERT(xChartDoc.is());
177 32 : return xChartDoc;
178 : }
179 :
180 32 : Reference< chart2::XChartType > getChartTypeFromDoc( Reference< chart2::XChartDocument > xChartDoc,
181 : sal_Int32 nChartType, sal_Int32 nCooSys = 0 )
182 : {
183 32 : CPPUNIT_ASSERT( xChartDoc.is() );
184 :
185 32 : Reference <chart2::XDiagram > xDiagram = xChartDoc->getFirstDiagram();
186 32 : CPPUNIT_ASSERT( xDiagram.is() );
187 :
188 64 : Reference< chart2::XCoordinateSystemContainer > xCooSysContainer( xDiagram, UNO_QUERY_THROW );
189 32 : CPPUNIT_ASSERT( xCooSysContainer.is() );
190 :
191 64 : Sequence< Reference< chart2::XCoordinateSystem > > xCooSysSequence( xCooSysContainer->getCoordinateSystems());
192 32 : CPPUNIT_ASSERT( xCooSysSequence.getLength() > nCooSys );
193 :
194 64 : Reference< chart2::XChartTypeContainer > xChartTypeContainer( xCooSysSequence[nCooSys], UNO_QUERY_THROW );
195 32 : CPPUNIT_ASSERT( xChartTypeContainer.is() );
196 :
197 64 : Sequence< Reference< chart2::XChartType > > xChartTypeSequence( xChartTypeContainer->getChartTypes() );
198 32 : CPPUNIT_ASSERT( xChartTypeSequence.getLength() > nChartType );
199 :
200 64 : return xChartTypeSequence[nChartType];
201 : }
202 :
203 14 : Reference< chart2::XDataSeries > getDataSeriesFromDoc( uno::Reference< chart2::XChartDocument > xChartDoc,
204 : sal_Int32 nDataSeries, sal_Int32 nChartType = 0, sal_Int32 nCooSys = 0 )
205 : {
206 14 : Reference< chart2::XChartType > xChartType = getChartTypeFromDoc( xChartDoc, nChartType, nCooSys );
207 28 : Reference< chart2::XDataSeriesContainer > xDataSeriesContainer( xChartType, UNO_QUERY_THROW );
208 14 : CPPUNIT_ASSERT ( xDataSeriesContainer.is() );
209 :
210 28 : Sequence< Reference< chart2::XDataSeries > > xSeriesSequence( xDataSeriesContainer->getDataSeries() );
211 14 : CPPUNIT_ASSERT( xSeriesSequence.getLength() > nDataSeries );
212 :
213 14 : Reference< chart2::XDataSeries > xSeries = xSeriesSequence[nDataSeries];
214 :
215 28 : return xSeries;
216 : }
217 :
218 2 : Reference< chart2::data::XDataSequence > getLabelDataSequenceFromDoc(
219 : Reference< chart2::XChartDocument > xChartDoc,
220 : sal_Int32 nDataSeries = 0, sal_Int32 nChartType = 0, sal_Int32 nCooSys = 0 )
221 : {
222 : Reference< chart2::XDataSeries > xDataSeries =
223 2 : getDataSeriesFromDoc( xChartDoc, nDataSeries, nChartType, nCooSys );
224 2 : CPPUNIT_ASSERT(xDataSeries.is());
225 4 : Reference< chart2::data::XDataSource > xDataSource( xDataSeries, uno::UNO_QUERY_THROW );
226 : Sequence< Reference< chart2::data::XLabeledDataSequence > > xDataSequences =
227 4 : xDataSource->getDataSequences();
228 4 : for(sal_Int32 i = 0; i < xDataSequences.getLength(); ++i)
229 : {
230 4 : Reference< chart2::data::XDataSequence> xLabelSeq = xDataSequences[i]->getLabel();
231 4 : if(!xLabelSeq.is())
232 2 : continue;
233 :
234 2 : return xLabelSeq;
235 0 : }
236 :
237 0 : CPPUNIT_FAIL("no Label sequence found");
238 2 : return Reference< chart2::data::XDataSequence > ();
239 : }
240 :
241 3 : uno::Sequence < OUString > getWriterChartColumnDescriptions( Reference< lang::XComponent > mxComponent )
242 : {
243 3 : uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier(mxComponent, uno::UNO_QUERY);
244 6 : uno::Reference<drawing::XDrawPage> xDrawPage = xDrawPageSupplier->getDrawPage();
245 6 : uno::Reference<drawing::XShape> xShape(xDrawPage->getByIndex(0), uno::UNO_QUERY);
246 3 : CPPUNIT_ASSERT( xShape.is() );
247 6 : uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY);
248 6 : uno::Reference< chart2::XChartDocument > xChartDoc;
249 3 : xChartDoc.set( xPropertySet->getPropertyValue( "Model" ), uno::UNO_QUERY );
250 3 : CPPUNIT_ASSERT( xChartDoc.is() );
251 3 : CPPUNIT_ASSERT( xChartDoc->getDataProvider().is() );
252 6 : uno::Reference<beans::XPropertySet> xProp(xChartDoc->getDataProvider(), uno::UNO_QUERY );
253 6 : uno::Reference< chart2::XAnyDescriptionAccess > xAnyDescriptionAccess ( xChartDoc->getDataProvider(), uno::UNO_QUERY_THROW );
254 3 : CPPUNIT_ASSERT( xAnyDescriptionAccess.is() );
255 3 : uno::Sequence< OUString > seriesList = xAnyDescriptionAccess->getColumnDescriptions();
256 6 : return seriesList;
257 : }
258 :
259 4 : uno::Reference< chart::XChartDocument > ChartTest::getChartDocFromImpress( const char* pDir, const char* pName )
260 : {
261 4 : mxComponent = loadFromDesktop(getURLFromSrc(pDir) + OUString::createFromAscii(pName), "com.sun.star.comp.Draw.PresentationDocument");
262 4 : uno::Reference< drawing::XDrawPagesSupplier > xDoc(mxComponent, uno::UNO_QUERY_THROW );
263 : uno::Reference< drawing::XDrawPage > xPage(
264 8 : xDoc->getDrawPages()->getByIndex(0), uno::UNO_QUERY_THROW );
265 4 : CPPUNIT_ASSERT(xPage.is());
266 : uno::Reference< beans::XPropertySet > xShapeProps(
267 8 : xPage->getByIndex(0), uno::UNO_QUERY );
268 4 : CPPUNIT_ASSERT(xShapeProps.is());
269 8 : uno::Reference< frame::XModel > xDocModel;
270 4 : xShapeProps->getPropertyValue("Model") >>= xDocModel;
271 4 : CPPUNIT_ASSERT(xDocModel.is());
272 4 : uno::Reference< chart::XChartDocument > xChartDoc( xDocModel, uno::UNO_QUERY_THROW );
273 :
274 8 : return xChartDoc;
275 : }
276 :
277 3 : uno::Sequence < OUString > ChartTest::getImpressChartColumnDescriptions( const char* pDir, const char* pName )
278 : {
279 3 : uno::Reference< chart::XChartDocument > xChartDoc = getChartDocFromImpress( pDir, pName );
280 6 : uno::Reference< chart::XChartDataArray > xChartData ( xChartDoc->getData(), uno::UNO_QUERY_THROW);
281 3 : CPPUNIT_ASSERT(xChartData.is());
282 3 : uno::Sequence < OUString > seriesList = xChartData->getColumnDescriptions();
283 6 : return seriesList;
284 : }
285 :
286 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|