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