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 23 package complex.sc; 24 25 import com.sun.star.beans.Property; 26 import com.sun.star.beans.PropertyAttribute; 27 import com.sun.star.beans.PropertyVetoException; 28 import com.sun.star.beans.UnknownPropertyException; 29 import com.sun.star.beans.XPropertySet; 30 import com.sun.star.beans.XPropertySetInfo; 31 import com.sun.star.container.XIndexAccess; 32 import com.sun.star.drawing.XDrawPage; 33 import com.sun.star.drawing.XDrawPages; 34 import com.sun.star.drawing.XDrawPagesSupplier; 35 import com.sun.star.drawing.XShape; 36 import com.sun.star.lang.WrappedTargetException; 37 import com.sun.star.lang.XComponent; 38 import com.sun.star.lang.XMultiServiceFactory; 39 import com.sun.star.sheet.XCellRangeAddressable; 40 import com.sun.star.sheet.XSpreadsheet; 41 import com.sun.star.sheet.XSpreadsheetDocument; 42 import com.sun.star.sheet.XSpreadsheets; 43 // import com.sun.star.table.XCell; 44 import com.sun.star.uno.Any; 45 import com.sun.star.uno.AnyConverter; 46 import com.sun.star.uno.Type; 47 import com.sun.star.uno.UnoRuntime; 48 import com.sun.star.util.XCloseable; 49 50 // import complexlib.ComplexTestCase; 51 52 // import java.io.PrintWriter; 53 54 import util.DrawTools; 55 import util.SOfficeFactory; 56 import util.ValueComparer; 57 58 // import org.junit.After; 59 import org.junit.AfterClass; 60 // import org.junit.Before; 61 import org.junit.BeforeClass; 62 import org.junit.Test; 63 import org.openoffice.test.OfficeConnection; 64 import static org.junit.Assert.*; 65 66 67 public class CalcRTL /* extends ComplexTestCase */ 68 { 69 XSpreadsheetDocument xSheetDoc = null; 70 71 /* 72 * This testcase checks the calcrtl feature added to cws_calcrtl in three steps 73 * <br> 74 * (<b>REMARK: </b> the testcases expects the TableLayout to be LR_TB) 75 * <br> 76 * 1. A calc document is opened and the properties in css.sheet.Spreadsheet are checked 77 * <br> 78 * 2. A calc document is opened and a shape inserted, afterwards the properties in css.sheet.Shape are checked 79 * <br> 80 * 3. A calc document is opened and a shape inserted, afterwards the effect of changing the TableLayout to the shape is checked 81 */ 82 // public String[] getTestMethodNames() { 83 // return new String[] { 84 // "checkSpreadsheetProperties", "checkShapeProperties", 85 // "checkInfluenceOfSpreadsheetChange" 86 // }; 87 // } 88 89 /* 90 * In this method a spreadsheet document is opened<br> 91 * afterwards all properties of the Spreadsheet are checked.<br> 92 * <p> 93 * These are 94 * <ul> 95 *<li> IsVisible 96 *<li> PageStyle 97 *<li> TableLayout 98 *</ul> 99 */ checkSpreadsheetProperties()100 @Test public void checkSpreadsheetProperties() { 101 assertTrue("Couldn't open document", openSpreadsheetDocument()); 102 103 XPropertySet set = UnoRuntime.queryInterface( 104 XPropertySet.class, getSpreadsheet()); 105 assertTrue("Problems when setting property 'IsVisible'", 106 changeProperty(set, "IsVisible", Boolean.FALSE)); 107 assertTrue("Problems when setting property 'IsVisible'", 108 changeProperty(set, "IsVisible", Boolean.TRUE)); 109 assertTrue("Problems when setting property 'PageStyle'", 110 changeProperty(set, "PageStyle", "Report")); 111 assertTrue("Problems when setting property 'PageStyle'", 112 changeProperty(set, "PageStyle", "Default")); 113 assertTrue("Problems when setting property 'TableLayout'", 114 changeProperty(set, "TableLayout", 115 new Short(com.sun.star.text.WritingMode2.RL_TB))); 116 assertTrue("Problems when setting property 'TableLayout'", 117 changeProperty(set, "TableLayout", 118 new Short(com.sun.star.text.WritingMode2.LR_TB))); 119 assertTrue("Couldn't close document", closeSpreadsheetDocument()); 120 } 121 122 /* 123 * In this method a spreadsheet document is opened and a shape inserted<br> 124 * afterwards all calc specific properties of the Shape are checked.<br> 125 * <p> 126 * These are 127 * <ul> 128 *<li> Anchor 129 *<li> HoriOrientPosition 130 *<li> VertOrientPosition 131 *</ul> 132 */ checkShapeProperties()133 @Test public void checkShapeProperties() { 134 assertTrue("Couldn't open document", openSpreadsheetDocument()); 135 136 XPropertySet set = UnoRuntime.queryInterface( 137 XPropertySet.class, getInsertedShape()); 138 139 try { 140 assertTrue("Problems when setting property 'Anchor'", 141 changeProperty(set, "Anchor", 142 getSpreadsheet().getCellByPosition(5, 5))); 143 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 144 // assure("Problems when setting property 'Anchor'", false); 145 fail("Problems when setting property 'Anchor'"); 146 } 147 148 assertTrue("Problems when setting property 'HoriOrientPosition'", 149 changeProperty(set, "HoriOrientPosition", new Integer(1000))); 150 151 assertTrue("Problems when setting property 'VertOrientPosition'", 152 changeProperty(set, "VertOrientPosition", new Integer(1000))); 153 154 assertTrue("Couldn't close document", closeSpreadsheetDocument()); 155 } 156 checkInfluenceOfSpreadsheetChange()157 @Test public void checkInfluenceOfSpreadsheetChange() { 158 assertTrue("Couldn't open document", openSpreadsheetDocument()); 159 160 XShape oShape = getInsertedShape(); 161 XPropertySet sheetProps = UnoRuntime.queryInterface( 162 XPropertySet.class, getSpreadsheet()); 163 XPropertySet shapeProps = UnoRuntime.queryInterface( 164 XPropertySet.class, oShape); 165 166 String[] previous = getShapeProps(shapeProps, oShape); 167 assertTrue("Problems when setting property 'TableLayout'", 168 changeProperty(sheetProps, "TableLayout", 169 new Short(com.sun.star.text.WritingMode2.RL_TB))); 170 171 String[] RL_TB = getShapeProps(shapeProps, oShape); 172 assertTrue("Problems when setting property 'TableLayout'", 173 changeProperty(sheetProps, "TableLayout", 174 new Short(com.sun.star.text.WritingMode2.LR_TB))); 175 176 String[] LR_TB = getShapeProps(shapeProps, oShape); 177 assertTrue("Anchor has changed", 178 (previous[0].equals(RL_TB[0]) && previous[0].equals(LR_TB[0]))); 179 assertEquals("HoriOrientPosition has changed", 180 2100, (Integer.valueOf(previous[1]).intValue() + Integer.valueOf( 181 RL_TB[1]) 182 .intValue() + 183 Integer.valueOf(LR_TB[1]).intValue())); 184 assertEquals("VertOrientPosition has changed", 185 3*Integer.valueOf(previous[2]).intValue(), 186 (Integer.valueOf(previous[2]).intValue() + Integer.valueOf( 187 RL_TB[2]) 188 .intValue() + 189 Integer.valueOf(LR_TB[2]).intValue())); 190 assertTrue("x-position hasn't changed", 191 (previous[3].equals(LR_TB[3]) && 192 ((Integer.valueOf(previous[3]).intValue() * (-1)) - 193 oShape.getSize().Width != Integer.valueOf(LR_TB[2]) 194 .intValue()))); 195 assertTrue("Couldn't close document", closeSpreadsheetDocument()); 196 } 197 getShapeProps(XPropertySet set, XShape oShape)198 protected String[] getShapeProps(XPropertySet set, XShape oShape) { 199 String[] reValue = new String[4]; 200 201 try { 202 reValue[0] = toString(getRealValue(set.getPropertyValue("Anchor"))); 203 reValue[1] = toString(set.getPropertyValue("HoriOrientPosition")); 204 reValue[2] = toString(set.getPropertyValue("VertOrientPosition")); 205 reValue[3] = toString(new Integer(oShape.getPosition().X)); 206 } catch (com.sun.star.beans.UnknownPropertyException e) { 207 } catch (com.sun.star.lang.WrappedTargetException e) { 208 } 209 210 System.out.println("\t Anchor :" + reValue[0]); 211 System.out.println("\t HoriOrientPosition :" + reValue[1]); 212 System.out.println("\t VertOrientPosition :" + reValue[2]); 213 System.out.println("\t Shape Position (x,y) : (" + oShape.getPosition().X + 214 "," + oShape.getPosition().Y + ")"); 215 216 return reValue; 217 } 218 219 /* 220 * this method opens a calc document and sets the corresponding class variable xSheetDoc 221 */ openSpreadsheetDocument()222 protected boolean openSpreadsheetDocument() { 223 // SOfficeFactory SOF = SOfficeFactory.getFactory( 224 // (XMultiServiceFactory) param.getMSF()); 225 226 final XMultiServiceFactory xMsf = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); 227 SOfficeFactory SOF = SOfficeFactory.getFactory(xMsf); 228 229 boolean worked = true; 230 231 try { 232 System.out.println("creating a sheetdocument"); 233 xSheetDoc = SOF.createCalcDoc(null); 234 } catch (com.sun.star.uno.Exception e) { 235 // Some exception occures.FAILED 236 worked = false; 237 e.printStackTrace(/*(PrintWriter) log*/ System.out); 238 } 239 240 return worked; 241 } 242 243 /* 244 * this method closes a calc document and resets the corresponding class variable xSheetDoc 245 */ closeSpreadsheetDocument()246 protected boolean closeSpreadsheetDocument() { 247 boolean worked = true; 248 249 System.out.println(" disposing xSheetDoc "); 250 251 try { 252 XCloseable oCloser = UnoRuntime.queryInterface( 253 XCloseable.class, xSheetDoc); 254 oCloser.close(true); 255 } catch (com.sun.star.util.CloseVetoException e) { 256 worked = false; 257 System.out.println("Couldn't close document"); 258 } catch (com.sun.star.lang.DisposedException e) { 259 worked = false; 260 System.out.println("Document already disposed"); 261 } catch (java.lang.NullPointerException e) { 262 worked = false; 263 System.out.println("Couldn't get XCloseable"); 264 } 265 266 xSheetDoc = null; 267 268 return worked; 269 } 270 271 /* 272 * This method gets the first Sheet of the SpreadsheetDocument 273 * 274 */ getSpreadsheet()275 protected XSpreadsheet getSpreadsheet() { 276 XSpreadsheet oSheet = null; 277 278 System.out.println("getting sheets"); 279 280 XSpreadsheets xSpreadsheets = xSheetDoc.getSheets(); 281 282 System.out.println("getting a sheet"); 283 284 XIndexAccess oIndexAccess = UnoRuntime.queryInterface( 285 XIndexAccess.class, xSpreadsheets); 286 287 try { 288 oSheet = UnoRuntime.queryInterface( 289 XSpreadsheet.class, oIndexAccess.getByIndex(0)); 290 } catch (com.sun.star.lang.WrappedTargetException e) { 291 e.printStackTrace(System.out); 292 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 293 e.printStackTrace(System.out); 294 } 295 296 return oSheet; 297 } 298 299 /* 300 * This method changes a given property to a given value 301 * Parameters: 302 * @param set : The XPropertySet that contains the properties 303 * @param pName : The name of the Property that has to be changed 304 * @param pValue : The value, the property should be changed too. 305 */ changeProperty(XPropertySet set, String pName, Object pValue)306 protected boolean changeProperty(XPropertySet set, String pName, 307 Object pValue) { 308 boolean worked = true; 309 310 try { 311 Object oldValue = set.getPropertyValue(pName); 312 313 // for an exception thrown during setting new value 314 // to pass it to checkResult method 315 Exception exception = null; 316 317 try { 318 set.setPropertyValue(pName, pValue); 319 } catch (IllegalArgumentException e) { 320 exception = e; 321 } catch (PropertyVetoException e) { 322 exception = e; 323 } catch (WrappedTargetException e) { 324 exception = e; 325 } catch (UnknownPropertyException e) { 326 exception = e; 327 } catch (RuntimeException e) { 328 exception = e; 329 } 330 331 // getting result value 332 Object resValue = set.getPropertyValue(pName); 333 334 335 // checking results 336 worked = checkResult(set, pName, oldValue, pValue, resValue, 337 exception); 338 } catch (Exception e) { 339 System.out.println("Exception occured while testing property '" + 340 pName + "'"); 341 e.printStackTrace(); 342 worked = false; 343 } 344 345 return worked; 346 } 347 348 /* 349 * This methods checks if a property has changed as expected 350 * Parameters: 351 * @param set : the given XPropertySet 352 * @param propName : the name of the property that has been changed 353 * @param oldValue : the value of the property before it has been changed 354 * @param newValue : the value the property has been set to 355 * @param resValue : the value getPropertyValue returned for the property 356 * @param exception : the exeption thrown during the change of the property 357 */ checkResult(XPropertySet set, String propName, Object oldValue, Object newValue, Object resValue, Exception exception)358 protected boolean checkResult(XPropertySet set, String propName, 359 Object oldValue, Object newValue, 360 Object resValue, Exception exception) 361 throws Exception { 362 XPropertySetInfo info = set.getPropertySetInfo(); 363 Property prop = info.getPropertyByName(propName); 364 365 oldValue = getRealValue(oldValue); 366 newValue = getRealValue(newValue); 367 resValue = getRealValue(resValue); 368 369 short attr = prop.Attributes; 370 boolean readOnly = (prop.Attributes & PropertyAttribute.READONLY) != 0; 371 boolean maybeVoid = (prop.Attributes & PropertyAttribute.MAYBEVOID) != 0; 372 373 //check get-set methods 374 if (maybeVoid) { 375 System.out.println("Property " + propName + " is void"); 376 } 377 378 if (readOnly) { 379 System.out.println("Property " + propName + " is readOnly"); 380 } 381 382 if (util.utils.isVoid(oldValue) && !maybeVoid) { 383 System.out.println(propName + " is void, but it's not MAYBEVOID"); 384 385 return false; 386 } else if (oldValue == null) { 387 System.out.println(propName + 388 " has null value, and therefore can't be changed"); 389 390 return true; 391 } else if (readOnly) { 392 // check if exception was thrown 393 if (exception != null) { 394 if (exception instanceof PropertyVetoException) { 395 // the change of read only prohibited - OK 396 System.out.println("Property is ReadOnly and wasn't changed"); 397 System.out.println("Property '" + propName + "' OK"); 398 399 return true; 400 } else if (exception instanceof IllegalArgumentException) { 401 // the change of read only prohibited - OK 402 System.out.println("Property is ReadOnly and wasn't changed"); 403 System.out.println("Property '" + propName + "' OK"); 404 405 return true; 406 } else if (exception instanceof UnknownPropertyException) { 407 // the change of read only prohibited - OK 408 System.out.println("Property is ReadOnly and wasn't changed"); 409 System.out.println("Property '" + propName + "' OK"); 410 411 return true; 412 } else if (exception instanceof RuntimeException) { 413 // the change of read only prohibited - OK 414 System.out.println("Property is ReadOnly and wasn't changed"); 415 System.out.println("Property '" + propName + "' OK"); 416 417 return true; 418 } else { 419 throw exception; 420 } 421 } else { 422 // if no exception - check that value 423 // has not changed 424 if (!ValueComparer.equalValue(resValue, oldValue)) { 425 System.out.println("Read only property '" + propName + 426 "' has changed"); 427 428 try { 429 if (!util.utils.isVoid(oldValue) && 430 oldValue instanceof Any) { 431 oldValue = AnyConverter.toObject( 432 new Type(((Any) oldValue).getClass()), 433 oldValue); 434 } 435 436 System.out.println("old = " + toString(oldValue)); 437 System.out.println("new = " + toString(newValue)); 438 System.out.println("result = " + toString(resValue)); 439 } catch (com.sun.star.lang.IllegalArgumentException iae) { 440 } 441 442 return false; 443 } else { 444 System.out.println("Read only property '" + propName + 445 "' hasn't changed"); 446 System.out.println("Property '" + propName + "' OK"); 447 448 return true; 449 } 450 } 451 } else { 452 if (exception == null) { 453 // if no exception thrown 454 // check that the new value is set 455 if ((!ValueComparer.equalValue(resValue, newValue)) || 456 (ValueComparer.equalValue(resValue, oldValue))) { 457 System.out.println("Value for '" + propName + 458 "' hasn't changed as expected"); 459 460 try { 461 if (!util.utils.isVoid(oldValue) && 462 oldValue instanceof Any) { 463 oldValue = AnyConverter.toObject( 464 new Type(((Any) oldValue).getClass()), 465 oldValue); 466 } 467 468 System.out.println("old = " + toString(oldValue)); 469 System.out.println("new = " + toString(newValue)); 470 System.out.println("result = " + toString(resValue)); 471 } catch (com.sun.star.lang.IllegalArgumentException iae) { 472 } 473 474 if (resValue != null) { 475 if ((!ValueComparer.equalValue(resValue, oldValue)) || 476 (!resValue.equals(oldValue))) { 477 System.out.println("But it has changed."); 478 479 return true; 480 } else { 481 return false; 482 } 483 } else { 484 return false; 485 } 486 487 //tRes.tested(propName, false); 488 } else { 489 System.out.println("Property '" + propName + "' OK"); 490 491 try { 492 if (!util.utils.isVoid(oldValue) && 493 oldValue instanceof Any) { 494 oldValue = AnyConverter.toObject( 495 new Type(((Any) oldValue).getClass()), 496 oldValue); 497 } 498 499 System.out.println("old = " + toString(oldValue)); 500 System.out.println("new = " + toString(newValue)); 501 System.out.println("result = " + toString(resValue)); 502 } catch (com.sun.star.lang.IllegalArgumentException iae) { 503 } 504 505 return true; 506 } 507 } else { 508 throw exception; 509 } 510 } 511 } 512 513 /* 514 * Returns a String representation of a given object, returns "null" in case of a NullObject 515 * @param obj : the given object 516 */ toString(Object obj)517 protected String toString(Object obj) { 518 return (obj == null) ? "null" : obj.toString(); 519 } 520 521 /* 522 * Returns a human readable String representation for CellRangeAddressable 523 */ getRealValue(Object obj)524 protected Object getRealValue(Object obj) { 525 Object value = null; 526 527 if (UnoRuntime.queryInterface(XCellRangeAddressable.class, obj) != null) { 528 XCellRangeAddressable aCell = UnoRuntime.queryInterface( 529 XCellRangeAddressable.class, 530 obj); 531 value = "Cell in Column " + aCell.getRangeAddress().StartColumn + 532 " and Row " + aCell.getRangeAddress().StartRow; 533 } else { 534 return obj; 535 } 536 537 return value; 538 } 539 540 /* 541 * This method inserts a RectangleShape into the calc document xSheetDoc and returns the resultung XShape-object 542 */ getInsertedShape()543 protected XShape getInsertedShape() { 544 XShape insertedShape = null; 545 546 try { 547 System.out.println("getting Drawpages"); 548 549 XDrawPagesSupplier oDPS = UnoRuntime.queryInterface( 550 XDrawPagesSupplier.class, 551 xSheetDoc); 552 XDrawPages oDP = oDPS.getDrawPages(); 553 XDrawPage firstDrawPage = UnoRuntime.queryInterface( 554 XDrawPage.class, 555 oDP.getByIndex(0)); 556 557 final XMultiServiceFactory xMsf = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); 558 SOfficeFactory SOF = SOfficeFactory.getFactory(xMsf); 559 560 // SOfficeFactory SOF = SOfficeFactory.getFactory( 561 // (XMultiServiceFactory) param.getMSF()); 562 XComponent xComp = UnoRuntime.queryInterface( 563 XComponent.class, xSheetDoc); 564 insertedShape = SOF.createShape(xComp, 5000, 3500, 700, 500, 565 "Rectangle"); 566 DrawTools.getShapes(firstDrawPage).add(insertedShape); 567 } catch (com.sun.star.lang.WrappedTargetException e) { 568 System.out.println("Couldn't create instance"); 569 e.printStackTrace(); 570 571 return null; 572 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 573 System.out.println("Couldn't create instance"); 574 e.printStackTrace(); 575 576 return null; 577 } 578 579 return insertedShape; 580 } 581 582 583 584 setUpConnection()585 @BeforeClass public static void setUpConnection() throws Exception { 586 connection.setUp(); 587 } 588 tearDownConnection()589 @AfterClass public static void tearDownConnection() 590 throws InterruptedException, com.sun.star.uno.Exception 591 { 592 connection.tearDown(); 593 } 594 595 private static final OfficeConnection connection = new OfficeConnection(); 596 597 } 598