1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
package complex.sc;
import com.sun.star.beans.Property;
import com.sun.star.beans.PropertyAttribute;
import com.sun.star.beans.PropertyVetoException;
import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.beans.XPropertySetInfo;
import com.sun.star.container.XIndexAccess;
import com.sun.star.drawing.XDrawPage;
import com.sun.star.drawing.XDrawPages;
import com.sun.star.drawing.XDrawPagesSupplier;
import com.sun.star.drawing.XShape;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.sheet.XCellRangeAddressable;
import com.sun.star.sheet.XSpreadsheet;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.sheet.XSpreadsheets;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.util.XCloseable;
import util.DrawTools;
import util.SOfficeFactory;
import util.ValueComparer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openoffice.test.OfficeConnection;
import static org.junit.Assert.*;
public class CalcRTL
{
XSpreadsheetDocument xSheetDoc = null;
/*
* This testcase checks the calcrtl feature added to cws_calcrtl in three steps
* <br>
* (<b>REMARK: </b> the testcases expects the TableLayout to be LR_TB)
* <br>
* 1. A calc document is opened and the properties in css.sheet.Spreadsheet are checked
* <br>
* 2. A calc document is opened and a shape inserted, afterwards the properties in css.sheet.Shape are checked
* <br>
* 3. A calc document is opened and a shape inserted, afterwards the effect of changing the TableLayout to the shape is checked
*/
/*
* In this method a spreadsheet document is opened and a shape inserted<br>
* afterwards all calc specific properties of the Shape are checked.<br>
* <p>
* These are
* <ul>
*<li> Anchor
*<li> HoriOrientPosition
*<li> VertOrientPosition
*</ul>
*/
@Test public void checkShapeProperties() {
assertTrue("Couldn't open document", openSpreadsheetDocument());
XPropertySet set = UnoRuntime.queryInterface(
XPropertySet.class, getInsertedShape());
try {
assertTrue("Problems when setting property 'Anchor'",
changeProperty(set, "Anchor",
getSpreadsheet().getCellByPosition(5, 5)));
} catch (com.sun.star.lang.IndexOutOfBoundsException e) {
// assure("Problems when setting property 'Anchor'", false);
fail("Problems when setting property 'Anchor'");
}
assertTrue("Problems when setting property 'HoriOrientPosition'",
changeProperty(set, "HoriOrientPosition", Integer.valueOf(1000)));
assertTrue("Problems when setting property 'VertOrientPosition'",
changeProperty(set, "VertOrientPosition", Integer.valueOf(1000)));
assertTrue("Couldn't close document", closeSpreadsheetDocument());
}
@Test public void checkInfluenceOfSpreadsheetChange() {
assertTrue("Couldn't open document", openSpreadsheetDocument());
XShape oShape = getInsertedShape();
XPropertySet sheetProps = UnoRuntime.queryInterface(
XPropertySet.class, getSpreadsheet());
XPropertySet shapeProps = UnoRuntime.queryInterface(
XPropertySet.class, oShape);
String[] previous = getShapeProps(shapeProps, oShape);
assertTrue("Problems when setting property 'TableLayout'",
changeProperty(sheetProps, "TableLayout",
Short.valueOf(com.sun.star.text.WritingMode2.RL_TB)));
String[] RL_TB = getShapeProps(shapeProps, oShape);
assertTrue("Problems when setting property 'TableLayout'",
changeProperty(sheetProps, "TableLayout",
Short.valueOf(com.sun.star.text.WritingMode2.LR_TB)));
String[] LR_TB = getShapeProps(shapeProps, oShape);
assertTrue("Anchor has changed",
(previous[0].equals(RL_TB[0]) && previous[0].equals(LR_TB[0])));
assertEquals("HoriOrientPosition has changed",
2100, Integer.parseInt(previous[1]) + Integer.parseInt(RL_TB[1]) +
Integer.parseInt(LR_TB[1]));
assertEquals("VertOrientPosition has changed",
3*Integer.parseInt(previous[2]),
(Integer.parseInt(previous[2]) + Integer.parseInt(RL_TB[2]) +
Integer.parseInt(LR_TB[2])));
assertTrue("x-position hasn't changed",
(previous[3].equals(LR_TB[3]) &&
((Integer.parseInt(previous[3]) * (-1)) -
oShape.getSize().Width != Integer.parseInt(LR_TB[2]))));
assertTrue("Couldn't close document", closeSpreadsheetDocument());
}
protected String[] getShapeProps(XPropertySet set, XShape oShape) {
String[] reValue = new String[4];
try {
reValue[0] = toString(getRealValue(set.getPropertyValue("Anchor")));
reValue[1] = toString(set.getPropertyValue("HoriOrientPosition"));
reValue[2] = toString(set.getPropertyValue("VertOrientPosition"));
reValue[3] = toString(Integer.valueOf(oShape.getPosition().X));
} catch (com.sun.star.beans.UnknownPropertyException e) {
} catch (com.sun.star.lang.WrappedTargetException e) {
}
System.out.println("\t Anchor :" + reValue[0]);
System.out.println("\t HoriOrientPosition :" + reValue[1]);
System.out.println("\t VertOrientPosition :" + reValue[2]);
System.out.println("\t Shape Position (x,y) : (" + oShape.getPosition().X +
"," + oShape.getPosition().Y + ")");
return reValue;
}
/*
* this method opens a calc document and sets the corresponding class variable xSheetDoc
*/
protected boolean openSpreadsheetDocument() {
final XMultiServiceFactory xMsf = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
SOfficeFactory SOF = SOfficeFactory.getFactory(xMsf);
boolean worked = true;
try {
System.out.println("creating a sheetdocument");
xSheetDoc = SOF.createCalcDoc(null);
} catch (com.sun.star.uno.Exception e) {
// Some exception occurs.FAILED
worked = false;
e.printStackTrace(/*(PrintWriter) log*/ System.err);
}
return worked;
}
/*
* this method closes a calc document and resets the corresponding class variable xSheetDoc
*/
protected boolean closeSpreadsheetDocument() {
boolean worked = true;
System.out.println(" disposing xSheetDoc ");
try {
XCloseable oCloser = UnoRuntime.queryInterface(
XCloseable.class, xSheetDoc);
oCloser.close(true);
} catch (com.sun.star.util.CloseVetoException e) {
worked = false;
System.out.println("Couldn't close document");
} catch (com.sun.star.lang.DisposedException e) {
worked = false;
System.out.println("Document already disposed");
} catch (NullPointerException e) {
worked = false;
System.out.println("Couldn't get XCloseable");
}
xSheetDoc = null;
return worked;
}
/*
* This method gets the first Sheet of the SpreadsheetDocument
*
*/
protected XSpreadsheet getSpreadsheet() {
XSpreadsheet oSheet = null;
System.out.println("getting sheets");
XSpreadsheets xSpreadsheets = xSheetDoc.getSheets();
System.out.println("getting a sheet");
XIndexAccess oIndexAccess = UnoRuntime.queryInterface(
XIndexAccess.class, xSpreadsheets);
try {
oSheet = UnoRuntime.queryInterface(
XSpreadsheet.class, oIndexAccess.getByIndex(0));
} catch (com.sun.star.lang.WrappedTargetException e) {
e.printStackTrace(System.err);
} catch (com.sun.star.lang.IndexOutOfBoundsException e) {
e.printStackTrace(System.err);
}
return oSheet;
}
/*
* This method changes a given property to a given value
* Parameters:
* @param set : The XPropertySet that contains the properties
* @param pName : The name of the Property that has to be changed
* @param pValue : The value, the property should be changed too.
*/
protected boolean changeProperty(XPropertySet set, String pName,
Object pValue) {
boolean worked = true;
try {
Object oldValue = set.getPropertyValue(pName);
// for an exception thrown during setting new value
// to pass it to checkResult method
Exception exception = null;
try {
set.setPropertyValue(pName, pValue);
} catch (IllegalArgumentException e) {
exception = e;
} catch (PropertyVetoException e) {
exception = e;
} catch (WrappedTargetException e) {
exception = e;
} catch (UnknownPropertyException e) {
exception = e;
} catch (RuntimeException e) {
exception = e;
}
// getting result value
Object resValue = set.getPropertyValue(pName);
// checking results
worked = checkResult(set, pName, oldValue, pValue, resValue,
exception);
} catch (Exception e) {
System.out.println("Exception occurred while testing property '" +
pName + "'");
e.printStackTrace();
worked = false;
}
return worked;
}
/*
* This methods checks if a property has changed as expected
* Parameters:
* @param set : the given XPropertySet
* @param propName : the name of the property that has been changed
* @param oldValue : the value of the property before it has been changed
* @param newValue : the value the property has been set to
* @param resValue : the value getPropertyValue returned for the property
* @param exception : the exception thrown during the change of the property
*/
protected boolean checkResult(XPropertySet set, String propName,
Object oldValue, Object newValue,
Object resValue, Exception exception)
throws Exception {
XPropertySetInfo info = set.getPropertySetInfo();
Property prop = info.getPropertyByName(propName);
oldValue = getRealValue(oldValue);
newValue = getRealValue(newValue);
resValue = getRealValue(resValue);
boolean readOnly = (prop.Attributes & PropertyAttribute.READONLY) != 0;
boolean maybeVoid = (prop.Attributes & PropertyAttribute.MAYBEVOID) != 0;
//check get-set methods
if (maybeVoid) {
System.out.println("Property " + propName + " is void");
}
if (readOnly) {
System.out.println("Property " + propName + " is readOnly");
}
if (util.utils.isVoid(oldValue) && !maybeVoid) {
System.out.println(propName + " is void, but it's not MAYBEVOID");
return false;
} else if (oldValue == null) {
System.out.println(propName +
" has null value, and therefore can't be changed");
return true;
} else if (readOnly) {
// check if exception was thrown
if (exception != null) {
if (exception instanceof PropertyVetoException) {
// the change of read only prohibited - OK
System.out.println("Property is ReadOnly and wasn't changed");
System.out.println("Property '" + propName + "' OK");
return true;
} else if (exception instanceof IllegalArgumentException) {
// the change of read only prohibited - OK
System.out.println("Property is ReadOnly and wasn't changed");
System.out.println("Property '" + propName + "' OK");
return true;
} else if (exception instanceof UnknownPropertyException) {
// the change of read only prohibited - OK
System.out.println("Property is ReadOnly and wasn't changed");
System.out.println("Property '" + propName + "' OK");
return true;
} else if (exception instanceof RuntimeException) {
// the change of read only prohibited - OK
System.out.println("Property is ReadOnly and wasn't changed");
System.out.println("Property '" + propName + "' OK");
return true;
} else {
throw exception;
}
} else {
// if no exception - check that value
// has not changed
if (!ValueComparer.equalValue(resValue, oldValue)) {
System.out.println("Read only property '" + propName +
"' has changed");
System.out.println("old = " + toString(oldValue));
System.out.println("new = " + toString(newValue));
System.out.println("result = " + toString(resValue));
return false;
} else {
System.out.println("Read only property '" + propName +
"' hasn't changed");
System.out.println("Property '" + propName + "' OK");
return true;
}
}
} else {
if (exception == null) {
// if no exception thrown
// check that the new value is set
if (!ValueComparer.equalValue(resValue, newValue)) {
System.out.println("Value for '" + propName +
"' hasn't changed as expected");
System.out.println("old = " + toString(oldValue));
System.out.println("new = " + toString(newValue));
System.out.println("result = " + toString(resValue));
if (!ValueComparer.equalValue(resValue, oldValue)) {
System.out.println("But it has changed.");
return true;
} else {
return false;
}
} else {
System.out.println("Property '" + propName + "' OK");
System.out.println("old = " + toString(oldValue));
System.out.println("new = " + toString(newValue));
System.out.println("result = " + toString(resValue));
return true;
}
} else {
throw exception;
}
}
}
/*
* Returns a String representation of a given object, returns "null" in case of a NullObject
* @param obj : the given object
*/
protected String toString(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
/*
* Returns a human readable String representation for CellRangeAddressable
*/
protected Object getRealValue(Object obj) {
Object value = null;
if (UnoRuntime.queryInterface(XCellRangeAddressable.class, obj) != null) {
XCellRangeAddressable aCell = UnoRuntime.queryInterface(
XCellRangeAddressable.class,
obj);
value = "Cell in Column " + aCell.getRangeAddress().StartColumn +
" and Row " + aCell.getRangeAddress().StartRow;
} else {
return obj;
}
return value;
}
/*
* This method inserts a RectangleShape into the calc document xSheetDoc and returns the resulting XShape-object
*/
protected XShape getInsertedShape() {
XShape insertedShape = null;
try {
System.out.println("getting Drawpages");
XDrawPagesSupplier oDPS = UnoRuntime.queryInterface(
XDrawPagesSupplier.class,
xSheetDoc);
XDrawPages oDP = oDPS.getDrawPages();
XDrawPage firstDrawPage = UnoRuntime.queryInterface(
XDrawPage.class,
oDP.getByIndex(0));
final XMultiServiceFactory xMsf = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
SOfficeFactory SOF = SOfficeFactory.getFactory(xMsf);
XComponent xComp = UnoRuntime.queryInterface(
XComponent.class, xSheetDoc);
insertedShape = SOF.createShape(xComp, 5000, 3500, 700, 500,
"Rectangle");
DrawTools.getShapes(firstDrawPage).add(insertedShape);
} catch (com.sun.star.lang.WrappedTargetException e) {
System.out.println("Couldn't create instance");
e.printStackTrace();
return null;
} catch (com.sun.star.lang.IndexOutOfBoundsException e) {
System.out.println("Couldn't create instance");
e.printStackTrace();
return null;
}
return insertedShape;
}
@BeforeClass public static void setUpConnection() throws Exception {
connection.setUp();
}
@AfterClass public static void tearDownConnection()
throws InterruptedException, com.sun.star.uno.Exception
{
connection.tearDown();
}
private static final OfficeConnection connection = new OfficeConnection();
}
|