xref: /trunk/main/chart2/qa/complex/chart2/TestCaseOldAPI.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // package name: as default, start with complex
23 package complex.chart2;
24 
25 // imports
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.uno.XInterface;
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.uno.Type;
30 import com.sun.star.uno.XComponentContext;
31 
32 import java.io.PrintWriter;
33 import java.util.Hashtable;
34 
35 import com.sun.star.lang.*;
36 import com.sun.star.beans.*;
37 import com.sun.star.frame.*;
38 import com.sun.star.chart.*;
39 import com.sun.star.drawing.*;
40 import com.sun.star.awt.*;
41 import com.sun.star.container.*;
42 import com.sun.star.util.XCloseable;
43 import com.sun.star.util.CloseVetoException;
44 
45 import com.sun.star.uno.AnyConverter;
46 import com.sun.star.comp.helper.ComponentContext;
47 
48 import org.junit.After;
49 import org.junit.AfterClass;
50 import org.junit.Before;
51 import org.junit.BeforeClass;
52 import org.junit.Test;
53 import org.openoffice.test.OfficeConnection;
54 import static org.junit.Assert.*;
55 
56 /**
57  * The following Complex Test will test the
58  * com.sun.star.document.IndexedPropertyValues
59  * service
60  */
61 
62 public class TestCaseOldAPI {
63 
64     private static final OfficeConnection officeConnection = new OfficeConnection();
65     private XMultiServiceFactory xMSF = null;
66     private XModel                    mxChartModel;
67     private XChartDocument            mxOldDoc;
68     private boolean                   mbCreateView;
69 
70 
71     @BeforeClass
beforeClass()72     public static void beforeClass() throws Exception
73     {
74         officeConnection.setUp();
75     }
76 
77     @AfterClass
afterClass()78     public static void afterClass() throws Exception
79     {
80         officeConnection.tearDown();
81     }
82 
83     // ____________
84 
85     @Before
before()86     public void before()
87     {
88         xMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, officeConnection.getComponentContext().getServiceManager());
89 
90         // set to "true" to get a view
91         mbCreateView = true;
92 
93         if( mbCreateView )
94             mxChartModel = createDocument( "schart" );
95         else
96             mxChartModel = createChartModel();
97 
98         mxOldDoc = (XChartDocument) UnoRuntime.queryInterface(
99             XChartDocument.class, mxChartModel );
100     }
101 
102     // ____________
103 
after()104     public void after()
105     {
106         XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(
107             XCloseable.class, mxChartModel );
108         assertTrue( "document is no XCloseable", xCloseable != null );
109 
110         // do not close document if there exists a view
111         if( ! mbCreateView )
112         {
113             try
114             {
115                 xCloseable.close( true );
116             }
117             catch( CloseVetoException ex )
118             {
119                 fail( ex.getMessage() );
120                 ex.printStackTrace( System.err );
121             }
122         }
123     }
124 
125     // ____________
126     @Test
testTitle()127     public void testTitle()
128     {
129         try
130         {
131             XPropertySet xDocProp = (XPropertySet) UnoRuntime.queryInterface(
132                 XPropertySet.class, mxOldDoc );
133             assertTrue( "Chart Document is no XPropertySet", xDocProp != null );
134             xDocProp.setPropertyValue( "HasMainTitle", new Boolean( true ));
135             assertTrue( "Property HasMainTitle", AnyConverter.toBoolean(
136                         xDocProp.getPropertyValue( "HasMainTitle" )));
137 
138             XShape xTitleShape = mxOldDoc.getTitle();
139             XPropertySet xTitleProp = (XPropertySet) UnoRuntime.queryInterface(
140                 XPropertySet.class, xTitleShape );
141 
142             // set property via old API
143             if( xTitleProp != null )
144             {
145                 String aTitle = " Overwritten by Old API ";
146                 float fHeight = (float)17.0;
147 
148                 xTitleProp.setPropertyValue( "String", aTitle );
149                 xTitleProp.setPropertyValue( "CharHeight", new Float( fHeight ) );
150 
151                 float fNewHeight = AnyConverter.toFloat( xTitleProp.getPropertyValue( "CharHeight" ) );
152                 assertTrue( "Changing CharHeight via old API failed", fNewHeight == fHeight );
153 
154                 String aNewTitle = AnyConverter.toString( xTitleProp.getPropertyValue( "String" ) );
155                 assertTrue( "Property \"String\" failed", aNewTitle.equals( aTitle ));
156             }
157 
158             // move title
159             Point aSetPos = new Point();
160             aSetPos.X = 1000;
161             aSetPos.Y = 200;
162             xTitleShape.setPosition( aSetPos );
163 
164             Point aNewPos = xTitleShape.getPosition();
165             assertTrue( "Title Position X", approxEqual( aNewPos.X, aSetPos.X, 1 ));
166             assertTrue( "Title Position Y", approxEqual( aNewPos.Y, aSetPos.Y, 1 ));
167         }
168         catch( Exception ex )
169         {
170             fail( ex.getMessage() );
171             ex.printStackTrace( System.err );
172         }
173     }
174 
175     // ____________
176     @Test
testSubTitle()177     public void testSubTitle()
178     {
179         try
180         {
181             XPropertySet xDocProp = (XPropertySet) UnoRuntime.queryInterface(
182                 XPropertySet.class, mxOldDoc );
183             assertTrue( "Chart Document is no XPropertySet", xDocProp != null );
184             xDocProp.setPropertyValue( "HasSubTitle", new Boolean( true ));
185             assertTrue( "Property HasSubTitle", AnyConverter.toBoolean(
186                         xDocProp.getPropertyValue( "HasSubTitle" )));
187 
188             XShape xTitleShape = mxOldDoc.getSubTitle();
189             XPropertySet xTitleProp = (XPropertySet) UnoRuntime.queryInterface(
190                 XPropertySet.class, xTitleShape );
191 
192             // set Property via old API
193             if( xTitleProp != null )
194             {
195                 int nColor = 0x009acd; // DeepSkyBlue3
196                 float fWeight = FontWeight.BOLD;
197                 float fHeight = (float)14.0;
198 
199                 xTitleProp.setPropertyValue( "CharColor", new Integer( nColor ) );
200                 xTitleProp.setPropertyValue( "CharWeight", new Float( fWeight ));
201                 xTitleProp.setPropertyValue( "CharHeight", new Float( fHeight ) );
202 
203                 int nNewColor = AnyConverter.toInt( xTitleProp.getPropertyValue( "CharColor" ) );
204                 assertTrue( "Changing CharColor via old API failed", nNewColor == nColor );
205 
206                 float fNewWeight = AnyConverter.toFloat( xTitleProp.getPropertyValue( "CharWeight" ) );
207                 assertTrue( "Changing CharWeight via old API failed", fNewWeight == fWeight );
208 
209                 float fNewHeight = AnyConverter.toFloat( xTitleProp.getPropertyValue( "CharHeight" ) );
210                 assertTrue( "Changing CharHeight via old API failed", fNewHeight == fHeight );
211             }
212         }
213         catch( Exception ex )
214         {
215             fail( ex.getMessage() );
216             ex.printStackTrace( System.err );
217         }
218     }
219 
220     // ------------
221     @Test
testDiagram()222     public void testDiagram()
223     {
224         try
225         {
226             // testing wall
227             XDiagram xDia = mxOldDoc.getDiagram();
228             if( xDia != null )
229             {
230                 X3DDisplay xDisp = (X3DDisplay) UnoRuntime.queryInterface(
231                     X3DDisplay.class, xDia );
232                 assertTrue( "X3DDisplay not supported", xDisp != null );
233 
234                 // Wall
235                 XPropertySet xProp = xDisp.getWall();
236                 if( xProp != null )
237                 {
238                     int nColor = 0xffe1ff; // thistle1
239                     xProp.setPropertyValue( "FillColor", new Integer( nColor ) );
240                     int nNewColor = AnyConverter.toInt( xProp.getPropertyValue( "FillColor" ) );
241                     assertTrue( "Changing FillColor via old API failed", nNewColor == nColor );
242                 }
243 
244                 assertTrue( "Wrong Diagram Type", xDia.getDiagramType().equals(
245                             "com.sun.star.chart.BarDiagram" ));
246 
247                 // Diagram properties
248                 xProp = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xDia );
249                 assertTrue( "Diagram is no property set", xProp != null );
250 
251                 // y-axis
252                 boolean bFirstYAxisText = false;
253                 xProp.setPropertyValue( "HasYAxisDescription", new Boolean( bFirstYAxisText ));
254                 boolean bNewFirstYAxisText = AnyConverter.toBoolean(
255                     xProp.getPropertyValue( "HasYAxisDescription" ));
256                 assertTrue( "Removing description of first y-axis", bNewFirstYAxisText == bFirstYAxisText );
257 
258 //                 boolean bYAxisTitle = true;
259 //                 xProp.setPropertyValue( "HasYAxisTitle", new Boolean( bYAxisTitle ));
260 //                 boolean bNewYAxisTitle = AnyConverter.toBoolean(
261 //                     xProp.getPropertyValue( "HasYAxisTitle" ));
262 //                 assertTrue( "Adding y-axis title", bNewYAxisTitle == bYAxisTitle );
263 
264                 // set title text
265 //                 XAxisYSupplier xYAxisSuppl = (XAxisYSupplier) UnoRuntime.queryInterface(
266 //                     XAxisYSupplier.class, mxOldDoc.getDiagram() );
267 //                 assertTrue( "Diagram is no y-axis supplier", xYAxisSuppl != null );
268 //                 XPropertySet xAxisTitleProp = (XPropertySet) UnoRuntime.queryInterface(
269 //                     XPropertySet.class, xYAxisSuppl.getYAxisTitle() );
270 //                 assertTrue( "Y-Axis Title is no XPropertySet", xAxisTitleProp != null );
271 //                 xAxisTitleProp.setPropertyValue( "String", "New y axis title" );
272 
273                 // second y-axis
274                 boolean bSecondaryYAxis = true;
275                 xProp.setPropertyValue( "HasSecondaryYAxis", new Boolean( bSecondaryYAxis ));
276                 boolean bNewSecYAxisValue = AnyConverter.toBoolean(
277                     xProp.getPropertyValue( "HasSecondaryYAxis" ));
278                 assertTrue( "Adding a second y-axis does not work", bNewSecYAxisValue == bSecondaryYAxis );
279 
280                 XTwoAxisYSupplier xSecYAxisSuppl = (XTwoAxisYSupplier) UnoRuntime.queryInterface(
281                     XTwoAxisYSupplier.class, xDia );
282                 assertTrue( "XTwoAxisYSupplier not implemented", xSecYAxisSuppl != null );
283                 assertTrue( "No second y-axis found", xSecYAxisSuppl.getSecondaryYAxis() != null );
284             }
285 
286             // move diagram
287             {
288                 XShape xDiagramShape = (XShape) UnoRuntime.queryInterface(
289                     XShape.class, xDia );
290 
291                 Point aOldPos = xDiagramShape.getPosition();
292                 int xDiff = 20;
293                 int yDiff = 20;
294                 Point aSetPos = new Point();
295                 aSetPos.X = aOldPos.X + xDiff;
296                 aSetPos.Y = aOldPos.Y + yDiff;
297                 xDiagramShape.setPosition( aSetPos );
298 
299                 Point aNewPos = xDiagramShape.getPosition();
300                 //System.out.println( "set X = " + aSetPos.X + ", new X = " + aNewPos.X );
301                 //System.out.println( "set Y = " + aSetPos.Y + ", new Y = " + aNewPos.Y );
302                 assertTrue( "Diagram Position X", approxEqual( aNewPos.X, aSetPos.X, 1 ));
303                 assertTrue( "Diagram Position Y", approxEqual( aNewPos.Y, aSetPos.Y, 1 ));
304             }
305 
306             // size diagram
307             {
308                 XShape xDiagramShape = (XShape) UnoRuntime.queryInterface(
309                     XShape.class, xDia );
310 
311                 Size aOldSize = xDiagramShape.getSize();
312                 int xDiff = aOldSize.Width/2+2;
313                 int yDiff = aOldSize.Height/2+2;
314                 Size aSetSize = new Size();
315                 aSetSize.Width = aOldSize.Width - xDiff;
316                 aSetSize.Height = aOldSize.Height - yDiff;
317                 xDiagramShape.setSize( aSetSize );
318 
319                 Size aNewSize = xDiagramShape.getSize();
320                 //System.out.println( "set width = " + aSetSize.Width + ", new width = " + aNewSize.Width );
321                 //System.out.println( "set height = " + aSetSize.Height + ", new height = " + aNewSize.Height );
322                 assertTrue( "Diagram Width", approxEqual( aNewSize.Width, aSetSize.Width, 2 ));
323                 assertTrue( "Diagram Height", approxEqual( aNewSize.Height, aSetSize.Height, 2 ));
324             }
325         }
326         catch( Exception ex )
327         {
328             fail( ex.getMessage() );
329             ex.printStackTrace( System.err );
330         }
331     }
332 
333     // ------------
334     @Test
testAxis()335     public void testAxis()
336     {
337         try
338         {
339             XAxisYSupplier xYAxisSuppl = (XAxisYSupplier) UnoRuntime.queryInterface(
340                 XAxisYSupplier.class, mxOldDoc.getDiagram() );
341             assertTrue( "Diagram is no y-axis supplier", xYAxisSuppl != null );
342 
343             XPropertySet xProp = xYAxisSuppl.getYAxis();
344             assertTrue( "No y-axis found", xProp != null );
345 
346             double fMax1, fMax2;
347             Object oMax = xProp.getPropertyValue( "Max" );
348             assertTrue( "No Maximum set", AnyConverter.isDouble( oMax ));
349             fMax1 = AnyConverter.toDouble( oMax );
350             System.out.println( "Maximum retrieved: " + fMax1 );
351             //todo: the view has to be built before there is an explicit value
352 //             assertTrue( "Max is 0.0", fMax1 > 0.0 );
353             xProp.setPropertyValue( "AutoMax", new Boolean( false ));
354             oMax = xProp.getPropertyValue( "Max" );
355             assertTrue( "No Maximum set", AnyConverter.isDouble( oMax ));
356             fMax2 = AnyConverter.toDouble( oMax );
357             System.out.println( "Maximum with AutoMax off: " + fMax2 );
358             assertTrue( "maxima differ", fMax1 == fMax2 );
359 
360             double nNewMax = 12.3;
361             double nNewOrigin = 2.7;
362 
363             xProp.setPropertyValue( "Max", new Double( nNewMax ));
364             assertTrue( "AutoMax is on", ! AnyConverter.toBoolean( xProp.getPropertyValue( "AutoMax" )) );
365 
366             assertTrue( "Maximum value invalid",
367                     approxEqual(
368                         AnyConverter.toDouble( xProp.getPropertyValue( "Max" )),
369                         nNewMax ));
370 
371             xProp.setPropertyValue( "AutoMin", new Boolean( true ));
372             assertTrue( "AutoMin is off", AnyConverter.toBoolean( xProp.getPropertyValue( "AutoMin" )) );
373 
374             xProp.setPropertyValue( "Origin", new Double( nNewOrigin ));
375             assertTrue( "Origin invalid",
376                     approxEqual(
377                         AnyConverter.toDouble( xProp.getPropertyValue( "Origin" )),
378                         nNewOrigin ));
379             xProp.setPropertyValue( "AutoOrigin", new Boolean( true ));
380             assertTrue( "AutoOrigin is off", AnyConverter.toBoolean( xProp.getPropertyValue( "AutoOrigin" )) );
381             Object oOrigin = xProp.getPropertyValue( "Origin" );
382             assertTrue( "No Origin set", AnyConverter.isDouble( oOrigin ));
383             System.out.println( "Origin retrieved: " + AnyConverter.toDouble( oOrigin ));
384 
385             xProp.setPropertyValue( "Logarithmic", new Boolean( true ));
386             assertTrue( "Scaling is not logarithmic",
387                     AnyConverter.toBoolean( xProp.getPropertyValue( "Logarithmic" )) );
388             xProp.setPropertyValue( "Logarithmic", new Boolean( false ));
389             assertTrue( "Scaling is not logarithmic",
390                     ! AnyConverter.toBoolean( xProp.getPropertyValue( "Logarithmic" )) );
391 
392             int nNewColor =  0xcd853f; // peru
393             xProp.setPropertyValue( "LineColor", new Integer( nNewColor ));
394             assertTrue( "Property LineColor",
395                     AnyConverter.toInt( xProp.getPropertyValue( "LineColor" )) == nNewColor );
396             float fNewCharHeight = (float)(16.0);
397             xProp.setPropertyValue( "CharHeight", new Float( fNewCharHeight ));
398             assertTrue( "Property CharHeight",
399                     AnyConverter.toFloat( xProp.getPropertyValue( "CharHeight" )) == fNewCharHeight );
400 
401             int nNewTextRotation = 700; // in 1/100 degrees
402             xProp.setPropertyValue( "TextRotation", new Integer( nNewTextRotation ));
403             assertTrue( "Property TextRotation",
404                     AnyConverter.toInt( xProp.getPropertyValue( "TextRotation" )) == nNewTextRotation );
405 
406             double fStepMain = 10.0;
407             xProp.setPropertyValue( "StepMain", new Double( fStepMain ));
408             assertTrue( "Property StepMain",
409                     AnyConverter.toDouble( xProp.getPropertyValue( "StepMain" )) == fStepMain );
410 
411             // note: fStepHelp must be a divider of fStepMain, because
412             // internally, the help-step is stored as an integer number of
413             // substeps
414             double fStepHelp = 5.0;
415             xProp.setPropertyValue( "StepHelp", new Double( fStepHelp ));
416             assertTrue( "Property StepHelp",
417                     AnyConverter.toDouble( xProp.getPropertyValue( "StepHelp" )) == fStepHelp );
418 
419             xProp.setPropertyValue( "DisplayLabels", new Boolean( false ));
420             assertTrue( "Property DisplayLabels", ! AnyConverter.toBoolean(
421                         xProp.getPropertyValue( "DisplayLabels" )));
422         }
423         catch( Exception ex )
424         {
425             fail( ex.getMessage() );
426             ex.printStackTrace( System.err );
427         }
428     }
429 
430     // ------------
431     @Test
testLegend()432     public void testLegend()
433     {
434         XShape xLegend = mxOldDoc.getLegend();
435         assertTrue( "No Legend returned", xLegend != null );
436 
437         XPropertySet xLegendProp = (XPropertySet) UnoRuntime.queryInterface(
438             XPropertySet.class, xLegend );
439         assertTrue( "Legend is no property set", xLegendProp != null );
440 
441         try
442         {
443             ChartLegendPosition eNewPos = ChartLegendPosition.BOTTOM;
444             xLegendProp.setPropertyValue( "Alignment", eNewPos );
445             assertTrue( "Property Alignment",
446                     AnyConverter.toObject(
447                         new Type( ChartLegendPosition.class ),
448                         xLegendProp.getPropertyValue( "Alignment" )) == eNewPos );
449 
450             float fNewCharHeight = (float)(11.0);
451             xLegendProp.setPropertyValue( "CharHeight", new Float( fNewCharHeight ));
452             assertTrue( "Property CharHeight",
453                     AnyConverter.toFloat( xLegendProp.getPropertyValue( "CharHeight" )) == fNewCharHeight );
454 
455             // move legend
456             {
457                 Point aOldPos = xLegend.getPosition();
458                 int xDiff = 20;
459                 int yDiff = 20;
460                 Point aSetPos = new Point();
461                 aSetPos.X = aOldPos.X + xDiff;
462                 aSetPos.Y = aOldPos.Y + yDiff;
463                 xLegend.setPosition( aSetPos );
464 
465                 Point aNewPos = xLegend.getPosition();
466                 assertTrue( "Legend Position X", approxEqual( aNewPos.X, aSetPos.X, 1 ));
467                 assertTrue( "Legend Position Y", approxEqual( aNewPos.Y, aSetPos.Y, 1 ));
468             }
469         }
470         catch( Exception ex )
471         {
472             fail( ex.getMessage() );
473             ex.printStackTrace( System.err );
474         }
475     }
476 
477     // ------------
478     @Test
testArea()479     public void testArea()
480     {
481         XPropertySet xArea = mxOldDoc.getArea();
482         assertTrue( "No Area", xArea != null );
483 
484         try
485         {
486             int nColor = 0xf5fffa; // mint cream
487             xArea.setPropertyValue( "FillColor", new Integer( nColor ) );
488             xArea.setPropertyValue( "FillStyle", FillStyle.SOLID );
489 //             XPropertySetInfo xInfo = xArea.getPropertySetInfo();
490 //             assertTrue( "Area does not support ChartUserDefinedAttributes",
491 //                     xInfo.hasPropertyByName( "ChartUserDefinedAttributes" ));
492 
493 //             String aTestAttributeName = "test:foo";
494 //             String aTestAttributeValue = "content";
495 //             XNameContainer xUserDefAttributes = (XNameContainer) AnyConverter.toObject(
496 //                 new Type( XNameContainer.class ), xArea.getPropertyValue( "ChartUserDefinedAttributes" ));
497 //             xUserDefAttributes.insertByName( aTestAttributeName, aTestAttributeValue );
498 
499 //             String aContent = AnyConverter.toString( xUserDefAttributes.getByName( aTestAttributeName ));
500 //             assertTrue( "Wrong content in UserDefinedAttributes container",
501 //                     aContent.equals( aTestAttributeValue ));
502 
503             int nNewColor = AnyConverter.toInt( xArea.getPropertyValue( "FillColor" ) );
504             assertTrue( "Changing FillColor of Area failed", nNewColor == nColor );
505         }
506         catch( Exception ex )
507         {
508             fail( ex.getMessage() );
509             ex.printStackTrace( System.err );
510         }
511     }
512 
513     // ------------
514     @Test
testChartType()515     public void testChartType()
516     {
517         XMultiServiceFactory xFact = (XMultiServiceFactory) UnoRuntime.queryInterface(
518             XMultiServiceFactory.class, mxOldDoc );
519         assertTrue( "document is no factory", xFact != null );
520 
521         try
522         {
523             String aMyServiceName = new String( "com.sun.star.chart.BarDiagram" );
524             String aServices[] = xFact.getAvailableServiceNames();
525             boolean bServiceFound = false;
526             for( int i = 0; i < aServices.length; ++i )
527             {
528                 if( aServices[ i ].equals( aMyServiceName ))
529                 {
530                     bServiceFound = true;
531                     break;
532                 }
533             }
534             assertTrue( "getAvailableServiceNames did not return " + aMyServiceName, bServiceFound );
535 
536             if( bServiceFound )
537             {
538                 XDiagram xDia = (XDiagram) UnoRuntime.queryInterface(
539                     XDiagram.class, xFact.createInstance( aMyServiceName ));
540                 assertTrue( aMyServiceName + " could not be created", xDia != null );
541 
542                 mxOldDoc.setDiagram( xDia );
543 
544                 XPropertySet xDiaProp = (XPropertySet) UnoRuntime.queryInterface(
545                     XPropertySet.class, xDia );
546                 assertTrue( "Diagram is no XPropertySet", xDiaProp != null );
547 
548                 xDiaProp.setPropertyValue( "Stacked", new Boolean( true ));
549                 assertTrue( "StackMode could not be set correctly",
550                         AnyConverter.toBoolean(
551                             xDiaProp.getPropertyValue( "Stacked" )));
552 
553                 xDiaProp.setPropertyValue( "Dim3D", new Boolean( false ));
554                 assertTrue( "Dim3D could not be set correctly",
555                         ! AnyConverter.toBoolean(
556                             xDiaProp.getPropertyValue( "Dim3D" )));
557 
558                 xDiaProp.setPropertyValue( "Vertical", new Boolean( true ));
559                 assertTrue( "Vertical could not be set correctly",
560                         AnyConverter.toBoolean(
561                             xDiaProp.getPropertyValue( "Vertical" )));
562             }
563 
564             // reset to bar-chart
565 //             aMyServiceName = new String( "com.sun.star.chart.BarDiagram" );
566 //             XDiagram xDia = (XDiagram) UnoRuntime.queryInterface(
567 //                 XDiagram.class, xFact.createInstance( aMyServiceName ));
568 //             assertTrue( aMyServiceName + " could not be created", xDia != null );
569 
570 //             mxOldDoc.setDiagram( xDia );
571         }
572         catch( Exception ex )
573         {
574             fail( ex.getMessage() );
575             ex.printStackTrace( System.err );
576         }
577     }
578 
579     // ------------
580     @Test
testAggregation()581     public void testAggregation()
582     {
583         // query to new type
584         XChartDocument xDiaProv = (XChartDocument) UnoRuntime.queryInterface(
585             XChartDocument.class, mxOldDoc );
586         assertTrue( "query to new interface failed", xDiaProv != null );
587 
588         com.sun.star.chart.XChartDocument xDoc = (com.sun.star.chart.XChartDocument) UnoRuntime.queryInterface(
589             com.sun.star.chart.XChartDocument.class, xDiaProv );
590         assertTrue( "querying back to old interface failed", xDoc != null );
591     }
592 
593     // ------------
594     @Test
testDataSeriesAndPoints()595     public void testDataSeriesAndPoints()
596     {
597         try
598         {
599             XDiagram xDia = mxOldDoc.getDiagram();
600             assertTrue( "Invalid Diagram", xDia != null );
601             XMultiServiceFactory xFact = (XMultiServiceFactory) UnoRuntime.queryInterface(
602                 XMultiServiceFactory.class, mxOldDoc );
603             assertTrue( "document is no factory", xFact != null );
604 
605             // FillColor
606             XPropertySet xProp = xDia.getDataRowProperties( 0 );
607             int nColor = 0xffd700; // gold
608             xProp.setPropertyValue( "FillColor", new Integer( nColor ));
609             int nNewColor = AnyConverter.toInt( xProp.getPropertyValue( "FillColor" ) );
610             assertTrue( "Changing FillColor of Data Series failed", nNewColor == nColor );
611 
612             // Gradient
613             assertTrue( "No DataRowProperties for series 0", xProp != null );
614 
615             // note: the FillGradient property is optional, however it was
616             // supported in the old chart's API
617             XNameContainer xGradientTable = (XNameContainer) UnoRuntime.queryInterface(
618                 XNameContainer.class,
619                 xFact.createInstance( "com.sun.star.drawing.GradientTable" ));
620             assertTrue( "no gradient table", xGradientTable != null );
621             String aGradientName = "NewAPITestGradient";
622             Gradient aGradient = new Gradient();
623             aGradient.Style = GradientStyle.LINEAR;
624             aGradient.StartColor = 0xe0ffff; // light cyan
625             aGradient.EndColor  = 0xff8c00; // dark orange
626             aGradient.Angle = 300;          // 30 degrees
627             aGradient.Border = 15;
628             aGradient.XOffset = 0;
629             aGradient.YOffset = 0;
630             aGradient.StartIntensity = 100;
631             aGradient.EndIntensity = 80;
632             aGradient.StepCount = 23;
633 
634             xGradientTable.insertByName( aGradientName, aGradient );
635             xProp.setPropertyValue( "FillStyle", FillStyle.GRADIENT );
636             xProp.setPropertyValue( "FillGradientName", aGradientName );
637             String aNewGradientName = AnyConverter.toString( xProp.getPropertyValue( "FillGradientName" ));
638             assertTrue( "GradientName", aNewGradientName.equals( aGradientName ));
639             Gradient aNewGradient = (Gradient) AnyConverter.toObject(
640                 new Type( Gradient.class ),
641                 xGradientTable.getByName( aNewGradientName ));
642             assertTrue( "Gradient Style", aNewGradient.Style == aGradient.Style );
643             assertTrue( "Gradient StartColor", aNewGradient.StartColor == aGradient.StartColor );
644             assertTrue( "Gradient EndColor", aNewGradient.EndColor == aGradient.EndColor );
645             assertTrue( "Gradient Angle", aNewGradient.Angle == aGradient.Angle );
646             assertTrue( "Gradient Border", aNewGradient.Border == aGradient.Border );
647             assertTrue( "Gradient XOffset", aNewGradient.XOffset == aGradient.XOffset );
648             assertTrue( "Gradient YOffset", aNewGradient.YOffset == aGradient.YOffset );
649             assertTrue( "Gradient StartIntensity", aNewGradient.StartIntensity == aGradient.StartIntensity );
650             assertTrue( "Gradient EndIntensity", aNewGradient.EndIntensity == aGradient.EndIntensity );
651             assertTrue( "Gradient StepCount", aNewGradient.StepCount == aGradient.StepCount );
652 
653             // Hatch
654             xProp = xDia.getDataPointProperties( 1, 0 );
655             assertTrue( "No DataPointProperties for (1,0)", xProp != null );
656 
657             // note: the FillHatch property is optional, however it was
658             // supported in the old chart's API
659             XNameContainer xHatchTable = (XNameContainer) UnoRuntime.queryInterface(
660                 XNameContainer.class,
661                 xFact.createInstance( "com.sun.star.drawing.HatchTable" ));
662             assertTrue( "no hatch table", xHatchTable != null );
663             String aHatchName = "NewAPITestHatch";
664             Hatch aHatch = new Hatch();
665             aHatch.Style = HatchStyle.DOUBLE;
666             aHatch.Color = 0xd2691e; // chocolate
667             aHatch.Distance = 200;   // 2 mm (?)
668             aHatch.Angle = 230;      // 23 degrees
669 
670             xHatchTable.insertByName( aHatchName, aHatch );
671             xProp.setPropertyValue( "FillHatchName", aHatchName );
672             xProp.setPropertyValue( "FillStyle", FillStyle.HATCH );
673             xProp.setPropertyValue( "FillBackground", new Boolean( true ));
674             String aNewHatchName = AnyConverter.toString( xProp.getPropertyValue( "FillHatchName" ));
675             assertTrue( "HatchName", aNewHatchName.equals( aHatchName ));
676             Hatch aNewHatch = (Hatch) AnyConverter.toObject(
677                 new Type( Hatch.class ),
678                 xHatchTable.getByName( aNewHatchName ));
679             assertTrue( "Hatch Style", aNewHatch.Style == aHatch.Style );
680             assertTrue( "Hatch Color", aNewHatch.Color == aHatch.Color );
681             assertTrue( "Hatch Distance", aNewHatch.Distance == aHatch.Distance );
682             assertTrue( "Hatch Angle", aNewHatch.Angle == aHatch.Angle );
683             assertTrue( "FillBackground", AnyConverter.toBoolean( xProp.getPropertyValue( "FillBackground" )) );
684         }
685         catch( Exception ex )
686         {
687             fail( ex.getMessage() );
688             ex.printStackTrace( System.err );
689         }
690     }
691 
692     // ------------
693     @Test
testStatistics()694     public void testStatistics()
695     {
696         try
697         {
698             XDiagram xDia = mxOldDoc.getDiagram();
699             assertTrue( "Invalid Diagram", xDia != null );
700 
701             XPropertySet xProp = xDia.getDataRowProperties( 0 );
702             assertTrue( "No DataRowProperties for first series", xProp != null );
703 
704             xProp.setPropertyValue( "MeanValue", new Boolean( true ));
705             assertTrue( "No MeanValue", AnyConverter.toBoolean( xProp.getPropertyValue( "MeanValue" )) );
706         }
707         catch( Exception ex )
708         {
709             fail( ex.getMessage() );
710             ex.printStackTrace( System.err );
711         }
712     }
713 
714     // ------------
715 
setStockData_Type4()716     public void setStockData_Type4()
717     {
718         try
719         {
720             XPropertySet xDiaProp = (XPropertySet) UnoRuntime.queryInterface(
721                 XPropertySet.class, mxOldDoc.getDiagram() );
722 
723             ChartDataRowSource eNewSource = ChartDataRowSource.ROWS;
724             xDiaProp.setPropertyValue( "DataRowSource", eNewSource );
725             assertTrue( "Couldn't set \"DataRowSource\" property at Diagram",
726                     AnyConverter.toObject(
727                         new Type( ChartDataRowSource.class ),
728                         xDiaProp.getPropertyValue( "DataRowSource" )) == eNewSource );
729 
730             double aData[][] =
731                 {
732                     { 100.0, 200.0, 300.0, 250.0, 300.0 },
733                     { 6.5, 4.5, 6.0, 5.5, 3.5 },
734                     { 1.0, 1.5, 2.0, 2.5, 3.0 },
735                     { 6.0, 6.5, 7.0, 6.5, 5.0 },
736                     { 6.0, 5.5, 4.0, 4.5, 4.0 }
737                 };
738 
739             String[] aRowDescriptions =
740                 {
741                     "Volume", "Open", "Min", "Max", "Close"
742                 };
743 
744              String[] aColumnDescriptions =
745              {
746                  "First Row", "Second Row", "Third Row", "Fourth Row", "Fifth Row"
747              };
748 
749 
750             XChartData xData = mxOldDoc.getData();
751             XChartDataArray xDataArray = (XChartDataArray) UnoRuntime.queryInterface(
752                 XChartDataArray.class, xData );
753             assertTrue( "document has no XChartDataArray", xDataArray != null );
754 
755             xDataArray.setData( aData );
756             xDataArray.setRowDescriptions( aRowDescriptions );
757             xDataArray.setColumnDescriptions( aColumnDescriptions );
758 
759             mxOldDoc.attachData( xData );
760         }
761         catch( Exception ex )
762         {
763             fail( ex.getMessage() );
764             ex.printStackTrace( System.err );
765         }
766     }
767 
768     // ------------
769     @Test
testStockProperties()770     public void testStockProperties()
771     {
772         try
773         {
774             setStockData_Type4();
775 
776             XMultiServiceFactory xFact = (XMultiServiceFactory) UnoRuntime.queryInterface(
777                 XMultiServiceFactory.class, mxOldDoc );
778             assertTrue( "document is no factory", xFact != null );
779 
780             String aMyServiceName = new String( "com.sun.star.chart.StockDiagram" );
781             XDiagram xDia = (XDiagram) UnoRuntime.queryInterface(
782                 XDiagram.class, xFact.createInstance( aMyServiceName ));
783             assertTrue( aMyServiceName + " could not be created", xDia != null );
784 
785             mxOldDoc.setDiagram( xDia );
786 
787             XPropertySet xDiaProp = (XPropertySet) UnoRuntime.queryInterface(
788                 XPropertySet.class, xDia );
789             assertTrue( "Diagram is no XPropertySet", xDiaProp != null );
790 
791             xDiaProp.setPropertyValue( "Volume", new Boolean( true ));
792             assertTrue( "Has Volume", AnyConverter.toBoolean( xDiaProp.getPropertyValue( "Volume" )));
793 
794             xDiaProp.setPropertyValue( "UpDown", new Boolean( true ));
795             assertTrue( "Has UpDown", AnyConverter.toBoolean( xDiaProp.getPropertyValue( "UpDown" )));
796 
797             // MinMaxLine
798             XStatisticDisplay xMinMaxProvider = (XStatisticDisplay) UnoRuntime.queryInterface(
799                 XStatisticDisplay.class, xDia );
800             assertTrue( "Diagram is no XStatisticDisplay", xMinMaxProvider != null );
801             XPropertySet xMinMaxProp = xMinMaxProvider.getMinMaxLine();
802             assertTrue( "No MinMaxLine", xMinMaxProp != null );
803 
804             int nLineColor = 0x458b00; // chartreuse4
805             xMinMaxProp.setPropertyValue( "LineColor", new Integer( nLineColor ));
806             int nNewColor = AnyConverter.toInt( xMinMaxProp.getPropertyValue( "LineColor" ) );
807             assertTrue( "Changing LineColor of MinMax Line", nNewColor == nLineColor );
808         }
809         catch( Exception ex )
810         {
811             fail( ex.getMessage() );
812             ex.printStackTrace( System.err );
813         }
814     }
815 
816     // ------------
817     @Test
testFactory()818     public void testFactory()
819     {
820         try
821         {
822             XMultiServiceFactory xFact = (XMultiServiceFactory) UnoRuntime.queryInterface(
823                 XMultiServiceFactory.class, mxOldDoc );
824             assertTrue( "document is no factory", xFact != null );
825 
826             Object aTestTable = xFact.createInstance( "com.sun.star.drawing.GradientTable" );
827             assertTrue( "Couldn't create gradient table via factory", aTestTable != null );
828         }
829         catch( Exception ex )
830         {
831             fail( ex.getMessage() );
832             ex.printStackTrace( System.err );
833         }
834     }
835 
836     // ------------
837     @Test
testData()838     public void testData()
839     {
840         try
841         {
842             // set data
843             double aData[][] = {
844                 { 1.0, 1.5, 2.0, 2.5, 3.0 },
845                 { 2.0, 2.5, 3.0, 3.5, 4.0 },
846                 { 3.0, 3.5, 4.0, 4.5, 5.0 }
847             };
848 
849             String[] aColumnDescriptions = {
850                 "First Column", "Second Column", "Third Column",
851                 "Fourth Column", "Fifth Column"
852             };
853 
854             String[] aRowDescriptions = {
855                 "First Row", "Second Row", "Third Row"
856             };
857 
858             XPropertySet xDiaProp = (XPropertySet) UnoRuntime.queryInterface(
859                     XPropertySet.class, mxOldDoc.getDiagram() );
860             ChartDataRowSource eNewSource = ChartDataRowSource.ROWS;
861             xDiaProp.setPropertyValue( "DataRowSource", eNewSource );
862             assertTrue( "Couldn't set \"DataRowSource\" property at Diagram",
863                     AnyConverter.toObject(
864                         new Type( ChartDataRowSource.class ),
865                         xDiaProp.getPropertyValue( "DataRowSource" )) == eNewSource );
866 
867             XChartData xData = mxOldDoc.getData();
868             XChartDataArray xDataArray = (XChartDataArray) UnoRuntime.queryInterface(
869                 XChartDataArray.class, xData );
870             assertTrue( "document has no XChartDataArray", xDataArray != null );
871 
872             xDataArray.setData( aData );
873             xDataArray.setRowDescriptions( aRowDescriptions );
874             xDataArray.setColumnDescriptions( aColumnDescriptions );
875 
876             mxOldDoc.attachData( xData );
877 
878             // get data
879             double aReadData[][];
880             String[] aReadColumnDescriptions;
881             String[] aReadRowDescriptions;
882 
883             // refetch data
884             xData = mxOldDoc.getData();
885             xDataArray = (XChartDataArray) UnoRuntime.queryInterface(
886                 XChartDataArray.class, xData );
887             assertTrue( "document has no XChartDataArray", xDataArray != null );
888 
889             aReadData = xDataArray.getData();
890             aReadRowDescriptions = xDataArray.getRowDescriptions();
891             aReadColumnDescriptions = xDataArray.getColumnDescriptions();
892 
893             // compare to values set before
894             assertTrue( "Data size differs", aData.length == aReadData.length );
895             for( int i=0; i<aReadData.length; ++i )
896             {
897                 assertTrue( "Data size differs", aData[i].length == aReadData[i].length );
898                 for( int j=0; j<aReadData[i].length; ++j )
899                     assertTrue( "Data differs", aData[i][j] == aReadData[i][j] );
900             }
901 
902             assertTrue( "Column Description size differs", aColumnDescriptions.length == aReadColumnDescriptions.length );
903             for( int i=0; i<aReadColumnDescriptions.length; ++i )
904                 assertTrue( "Column Descriptions differ", aColumnDescriptions[i].equals( aReadColumnDescriptions[i] ));
905 
906             assertTrue( "Row Description size differs", aRowDescriptions.length == aReadRowDescriptions.length );
907             for( int i=0; i<aReadRowDescriptions.length; ++i )
908                 assertTrue( "Row Descriptions differ", aRowDescriptions[i].equals( aReadRowDescriptions[i] ));
909         }
910         catch( Exception ex )
911         {
912             fail( ex.getMessage() );
913             ex.printStackTrace( System.err );
914         }
915     }
916 
917     // ================================================================================
918 
919     // --------------------------------------------------------------------------------
920 
createDocument( String sDocType )921     private XModel createDocument( String sDocType )
922     {
923         XModel aResult = null;
924         try
925         {
926             XComponentLoader aLoader = (XComponentLoader) UnoRuntime.queryInterface(
927                 XComponentLoader.class,
928                 xMSF.createInstance( "com.sun.star.frame.Desktop" ) );
929 
930             aResult = (XModel) UnoRuntime.queryInterface(
931                 XModel.class,
932                 aLoader.loadComponentFromURL( "private:factory/" + sDocType,
933                                               "_blank",
934                                               0,
935                                               new PropertyValue[ 0 ] ) );
936         }
937         catch( Exception ex )
938         {
939             fail( ex.getMessage() );
940             ex.printStackTrace( System.err );
941         }
942 
943         return aResult;
944     }
945 
946     // ------------
947 
createChartModel()948     public XModel createChartModel()
949     {
950         XModel aResult = null;
951         try
952         {
953             aResult = (XModel) UnoRuntime.queryInterface(
954                 XModel.class,
955                 xMSF.createInstance( "com.sun.star.comp.chart2.ChartModel" ) );
956         }
957         catch( Exception ex )
958         {
959             fail( ex.getMessage() );
960             ex.printStackTrace( System.err );
961         }
962 
963         return aResult;
964     }
965 
966     // ------------
967 
getComponentContext( XMultiServiceFactory xFact )968     private XComponentContext getComponentContext( XMultiServiceFactory xFact )
969     {
970         XComponentContext xResult = null;
971 
972         XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(
973             XPropertySet.class, xFact );
974         if( xProp != null )
975             try
976             {
977                 xResult = (XComponentContext)
978                     AnyConverter.toObject(
979                         new Type( XComponentContext.class ),
980                         xProp.getPropertyValue( "DefaultContext" ) );
981             }
982             catch( Exception ex )
983             {
984                 fail( ex.getMessage() );
985                 ex.printStackTrace( System.err );
986             }
987 
988         return xResult;
989     }
990 
991     // ------------
992 
printInterfacesAndServices( Object oObj )993     private void printInterfacesAndServices( Object oObj )
994     {
995         System.out.println( "Services:" );
996         util.dbg.getSuppServices( oObj );
997         System.out.println( "Interfaces:" );
998         util.dbg.printInterfaces( (XInterface)oObj, true );
999     }
1000 
1001     // ------------
1002 
1003     /// see rtl/math.hxx
approxEqual( double a, double b )1004     private boolean approxEqual( double a, double b )
1005     {
1006         if( a == b )
1007             return true;
1008         double x = a - b;
1009         return (x < 0.0 ? -x : x)
1010             < ((a < 0.0 ? -a : a) * (1.0 / (16777216.0 * 16777216.0)));
1011     }
1012 
1013     // ------------
1014     /** returns true if a and b differ no more than tolerance.
1015 
1016         @param tolerance
1017             must be non-negative
1018      */
approxEqual( int a, int b, int tolerance )1019     private boolean approxEqual( int a, int b, int tolerance )
1020     {
1021         if( a != b )
1022             System.out.println( "Integer values differ by " + java.lang.Math.abs( a-b ));
1023         return ( ( a - tolerance <= b ) ||
1024                  ( a + tolerance >= b ));
1025     }
1026 }
1027