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