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