2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import com
.sun
.star
.beans
.XPropertySet
;
21 import com
.sun
.star
.container
.XIndexAccess
;
22 import com
.sun
.star
.sheet
.XScenariosSupplier
;
23 import com
.sun
.star
.sheet
.XSpreadsheet
;
24 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
25 import com
.sun
.star
.sheet
.XSpreadsheets
;
26 import com
.sun
.star
.table
.CellAddress
;
27 import com
.sun
.star
.table
.CellRangeAddress
;
28 import com
.sun
.star
.table
.XCell
;
29 import com
.sun
.star
.table
.XCellRange
;
30 import com
.sun
.star
.uno
.AnyConverter
;
31 import com
.sun
.star
.uno
.Type
;
32 import com
.sun
.star
.uno
.UnoRuntime
;
33 import com
.sun
.star
.uno
.XInterface
;
34 import com
.sun
.star
.util
.XCloseable
;
36 import ifc
.sheet
._XCellRangesQuery
;
38 import java
.io
.PrintWriter
;
40 import lib
.StatusException
;
42 import lib
.TestEnvironment
;
43 import lib
.TestParameters
;
45 import util
.SOfficeFactory
;
46 import util
.ValueComparer
;
50 * Test for object which is represented by service
51 * <code>com.sun.star.sheet.Spreadsheet</code>. <p>
52 * Object implements the following interfaces :
54 * <li> <code>com::sun::star::style::ParagraphProperties</code></li>
55 * <li> <code>com::sun::star::table::XTableChartsSupplier</code></li>
56 * <li> <code>com::sun::star::sheet::XSpreadsheet</code></li>
57 * <li> <code>com::sun::star::table::CellProperties</code></li>
58 * <li> <code>com::sun::star::sheet::XSheetAnnotationsSupplier</code></li>
59 * <li> <code>com::sun::star::table::XCellRange</code></li>
60 * <li> <code>com::sun::star::sheet::XCellRangeAddressable</code></li>
61 * <li> <code>com::sun::star::sheet::XDataPilotTablesSupplier</code></li>
62 * <li> <code>com::sun::star::sheet::Spreadsheet</code></li>
63 * <li> <code>com::sun::star::sheet::XCellRangeMovement</code></li>
64 * <li> <code>com::sun::star::style::CharacterProperties</code></li>
65 * <li> <code>com::sun::star::sheet::XSheetOperation</code></li>
66 * <li> <code>com::sun::star::sheet::XScenariosSupplier</code></li>
67 * <li> <code>com::sun::star::sheet::XSheetPageBreak</code></li>
68 * <li> <code>com::sun::star::sheet::XArrayFormulaRange</code></li>
69 * <li> <code>com::sun::star::sheet::XSheetCellRange</code></li>
70 * <li> <code>com::sun::star::container::XNamed</code></li>
71 * <li> <code>com::sun::star::sheet::SheetCellRange</code></li>
72 * <li> <code>com::sun::star::chart::XChartData</code></li>
73 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
74 * <li> <code>com::sun::star::util::XMergeable</code></li>
75 * <li> <code>com::sun::star::table::XColumnRowRange</code></li>
77 * @see com.sun.star.style.ParagraphProperties
78 * @see com.sun.star.table.XTableChartsSupplier
79 * @see com.sun.star.sheet.XSpreadsheet
80 * @see com.sun.star.table.CellProperties
81 * @see com.sun.star.sheet.XSheetAnnotationsSupplier
82 * @see com.sun.star.table.XCellRange
83 * @see com.sun.star.sheet.XCellRangeAddressable
84 * @see com.sun.star.sheet.XDataPilotTablesSupplier
85 * @see com.sun.star.sheet.Spreadsheet
86 * @see com.sun.star.sheet.XCellRangeMovement
87 * @see com.sun.star.style.CharacterProperties
88 * @see com.sun.star.sheet.XSheetOperation
89 * @see com.sun.star.sheet.XScenariosSupplier
90 * @see com.sun.star.sheet.XSheetPageBreak
91 * @see com.sun.star.sheet.XArrayFormulaRange
92 * @see com.sun.star.sheet.XSheetCellRange
93 * @see com.sun.star.container.XNamed
94 * @see com.sun.star.sheet.SheetCellRange
95 * @see com.sun.star.chart.XChartData
96 * @see com.sun.star.beans.XPropertySet
97 * @see com.sun.star.util.XMergeable
98 * @see com.sun.star.table.XColumnRowRange
99 * @see ifc.style._ParagraphProperties
100 * @see ifc.table._XTableChartsSupplier
101 * @see ifc.sheet._XSpreadsheet
102 * @see ifc.table._CellProperties
103 * @see ifc.sheet._XSheetAnnotationsSupplier
104 * @see ifc.table._XCellRange
105 * @see ifc.sheet._XCellRangeAddressable
106 * @see ifc.sheet._XDataPilotTablesSupplier
107 * @see ifc.sheet._Spreadsheet
108 * @see ifc.sheet._XCellRangeMovement
109 * @see ifc.style._CharacterProperties
110 * @see ifc.sheet._XSheetOperation
111 * @see ifc.sheet._XScenariosSupplier
112 * @see ifc.sheet._XSheetPageBreak
113 * @see ifc.sheet._XArrayFormulaRange
114 * @see ifc.sheet._XSheetCellRange
115 * @see ifc.container._XNamed
116 * @see ifc.sheet._SheetCellRange
117 * @see ifc.chart._XChartData
118 * @see ifc.beans._XPropertySet
119 * @see ifc.util._XMergeable
120 * @see ifc.table._XColumnRowRange
122 public class ScTableSheetObj
extends TestCase
{
123 private XSpreadsheetDocument xSheetDoc
= null;
126 * Creates Spreadsheet document.
129 protected void initialize(TestParameters tParam
, PrintWriter log
) {
130 // get a soffice factory object
131 SOfficeFactory SOF
= SOfficeFactory
.getFactory(
135 log
.println("creating a sheetdocument");
136 xSheetDoc
= SOF
.createCalcDoc(null);
137 } catch (com
.sun
.star
.uno
.Exception e
) {
138 // Some exception occurs.FAILED
139 e
.printStackTrace(log
);
140 throw new StatusException("Couldn't create document", e
);
145 * Disposes Spreadsheet document.
148 protected void cleanup(TestParameters tParam
, PrintWriter log
) {
149 log
.println(" disposing xSheetDoc ");
152 XCloseable oCloser
= UnoRuntime
.queryInterface(
153 XCloseable
.class, xSheetDoc
);
155 } catch (com
.sun
.star
.util
.CloseVetoException e
) {
156 log
.println("Couldn't close document");
157 } catch (com
.sun
.star
.lang
.DisposedException e
) {
158 log
.println("Document already disposed");
159 } catch (NullPointerException e
) {
160 log
.println("Couldn't get XCloseable");
163 util
.utils
.pause(500);
165 log
.println("... Done");
169 * Creating a Testenvironment for the interfaces to be tested.
170 * Retrieves a collection of spreadsheets from the document and takes one of
171 * them. Fills some cell in the spreadsheet. The retrieved spreadsheet is the
172 * instance of the service <code>com.sun.star.sheet.Spreadsheet</code>.
173 * Object relations created :
175 * <li> <code>'noArray'</code> for
176 * {@link ifc.sheet._XArrayFormulaRange}(to avoid the test of
177 * the interface <code>XArrayFormulaRange</code>)</li>
179 * @see com.sun.star.sheet.XArrayFormulaRange
182 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
,
184 XInterface oObj
= null;
186 log
.println("getting sheets");
188 XSpreadsheets xSpreadsheets
= xSheetDoc
.getSheets();
190 log
.println("getting a sheet");
192 XSpreadsheet oSheet
= null;
193 XIndexAccess oIndexAccess
= UnoRuntime
.queryInterface(
194 XIndexAccess
.class, xSpreadsheets
);
197 oSheet
= (XSpreadsheet
) AnyConverter
.toObject(
198 new Type(XSpreadsheet
.class),
199 oIndexAccess
.getByIndex(0));
200 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
201 e
.printStackTrace(log
);
202 throw new StatusException("Couldn't get a spreadsheet", e
);
203 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
204 e
.printStackTrace(log
);
205 throw new StatusException("Couldn't get a spreadsheet", e
);
206 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
207 e
.printStackTrace(log
);
208 throw new StatusException("Couldn't get a spreadsheet", e
);
211 log
.println("filling some cells");
214 oSheet
.getCellByPosition(5, 5).setValue(15);
215 oSheet
.getCellByPosition(1, 4).setValue(10);
216 oSheet
.getCellByPosition(2, 0).setValue(-5.15);
217 oSheet
.getCellByPosition(8, 8).setFormula("= B5 + C1");
218 // fill cells for XSheetOtline::autoutline
219 oSheet
.getCellByPosition(6, 6).setValue(3);
220 oSheet
.getCellByPosition(7, 6).setValue(3);
221 oSheet
.getCellByPosition(8, 6).setFormula("= SUM(G7:H7)");
222 oSheet
.getCellByPosition(9, 6).setFormula("= G7*I7");
223 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
224 e
.printStackTrace(log
);
225 throw new StatusException("Exception occurred while filling cells", e
);
228 oObj
= UnoRuntime
.queryInterface(XInterface
.class, oSheet
);
230 log
.println("creating a new environment for object");
232 TestEnvironment tEnv
= new TestEnvironment(oObj
);
234 // do not execute com::sun::star::sheets::XCellSeries::fillAuto()
235 tEnv
.addObjRelation("XCELLSERIES_FILLAUTO", Boolean
.FALSE
);
237 // set the address ranges of the cells (see values set above): for e.g. XSheetOutline test
238 tEnv
.addObjRelation("CellRangeAddress",
239 new CellRangeAddress((short)0, 6, 6, 8, 8));
240 tEnv
.addObjRelation("CellRangeSubAddress",
241 new CellRangeAddress((short)0, 6, 6, 7, 8));
242 // pick a cell with a formula for XSheetAuditing, a dependent cell and a precedent cell
243 tEnv
.addObjRelation("XSheetAuditing.CellAddress", new CellAddress((short)0, 8, 6));
244 tEnv
.addObjRelation("XSheetAuditing.PrecedentCellAddress", new CellAddress((short)0, 7, 6));
245 tEnv
.addObjRelation("XSheetAuditing.DependentCellAddress", new CellAddress((short)0, 9, 6));
247 // add an existing sheet for linking
248 tEnv
.addObjRelation("XSheetLinkable.LinkSheet", "ScSheetLinksObj.ods");
250 //adding Scenario and with that a ScenarioSheet-Relation for Scenario and XScenarioEnhanced
251 XScenariosSupplier scene
= UnoRuntime
.queryInterface(
252 XScenariosSupplier
.class,
253 tEnv
.getTestObject());
255 .addNewByName("Scenario",
256 new CellRangeAddress
[] {
257 new CellRangeAddress((short) 0, 0, 0, 10, 10)
260 XSpreadsheet sSheet
= null;
263 sSheet
= UnoRuntime
.queryInterface(
265 xSpreadsheets
.getByName("Scenario"));
266 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
267 log
.println("Couldn't get Scenario");
268 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
269 log
.println("Couldn't get Scenario");
272 tEnv
.addObjRelation("ScenarioSheet", sSheet
);
274 log
.println("adding ObjRelation 'noArray' to avoid the test" +
275 " 'XArrayFormulaRange'");
276 tEnv
.addObjRelation("noArray", "ScTableSheetObj");
278 XPropertySet PropSet
= UnoRuntime
.queryInterface(
279 XPropertySet
.class, oObj
);
280 tEnv
.addObjRelation("PropSet", PropSet
);
281 tEnv
.addObjRelation("SHEET", oSheet
);
283 // add expected results for the XCellRangesQuery interface test
284 String
[] expectedResults
= new String
[7];
285 expectedResults
[_XCellRangesQuery
.QUERYCOLUMNDIFFERENCES
] = "Sheet1.B5;Sheet1.C1";
286 expectedResults
[_XCellRangesQuery
.QUERYCONTENTCELLS
] = "Sheet1.B5;Sheet1.C1;Sheet1.F6";
287 expectedResults
[_XCellRangesQuery
.QUERYEMPTYCELLS
] = "Sheet1.A1 ... Sheet1.B1 ... Sheet1.B6 ... Sheet1.C2 ... Sheet1.D1 ... Sheet1.F1 ... Sheet1.F7 ... Sheet1.G1";
288 expectedResults
[_XCellRangesQuery
.QUERYFORMULACELLS
] = "Sheet1.I7:J7;Sheet1.I9";
289 expectedResults
[_XCellRangesQuery
.QUERYINTERSECTION
] = "Sheet1.D4";
290 expectedResults
[_XCellRangesQuery
.QUERYROWDIFFERENCES
] = "Sheet1.A5;Sheet1.C1";
291 expectedResults
[_XCellRangesQuery
.QUERYVISIBLECELLS
] = "Sheet1.A2";
292 tEnv
.addObjRelation("XCellRangesQuery.EXPECTEDRESULTS",
295 // for XFormulaQuery interface test
297 tEnv
.addObjRelation("MAKEENTRYINCELL",
298 oSheet
.getCellByPosition(15, 15));
299 tEnv
.addObjRelation("RANGEINDICES", new int[] { 0, 0 });
300 tEnv
.addObjRelation("EXPECTEDDEPENDENTVALUES",
301 new int[] { 0, 255, 0, 65535 });
302 tEnv
.addObjRelation("EXPECTEDPRECEDENTVALUES",
303 new int[] { 0, 255, 0, 65535 });
304 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
306 "Cannot add the necessary object relation for XFormulaQuery test.");
309 // XSearchable interface test
311 tEnv
.addObjRelation("XSearchable.MAKEENTRYINCELL",
313 oSheet
.getCellByPosition(15, 15),
314 oSheet
.getCellByPosition(15, 16)
316 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
317 e
.printStackTrace(log
);
319 "Cannot make required object relation 'XSearchable.MAKEENTRYINCELL'.");
322 //Adding relation for util.XSortable
323 final PrintWriter finalLog
= log
;
324 final XCellRange oTable
= oSheet
;
325 tEnv
.addObjRelation("SORTCHECKER",
326 new ifc
.util
._XSortable
.XSortChecker() {
327 PrintWriter out
= finalLog
;
329 public void setPrintWriter(PrintWriter log
) {
333 public void prepareToSort() {
335 oTable
.getCellByPosition(0, 0).setValue(4);
336 oTable
.getCellByPosition(0, 1).setFormula("b");
337 oTable
.getCellByPosition(0, 2).setValue(3);
338 oTable
.getCellByPosition(0, 3).setValue(23);
339 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
340 out
.println("Exception while checking sort");
344 public boolean checkSort(boolean isSortNumbering
,
345 boolean isSortAscending
) {
346 out
.println("Sort checking...");
349 String
[] value
= new String
[4];
351 for (int i
= 0; i
< 4; i
++) {
353 XCell cell
= oTable
.getCellByPosition(0, i
);
354 value
[i
] = cell
.getFormula();
355 } catch (com
.sun
.star
.lang
.IndexOutOfBoundsException e
) {
356 out
.println("Exception while checking sort");
360 if (isSortNumbering
) {
361 if (isSortAscending
) {
362 out
.println("Sorting ascending");
364 String
[] rightVal
= { "3", "4", "23", "b" };
365 String
[] vals
= { value
[0], value
[1], value
[2], value
[3] };
366 res
= ValueComparer
.equalValue(vals
, rightVal
);
367 out
.println("Expected 3, 4, 23, b");
368 out
.println("getting: " + value
[0] + ", " +
369 value
[1] + ", " + value
[2] + ", " +
372 String
[] rightVal
= { "b", "23", "4", "3" };
373 String
[] vals
= { value
[0], value
[1], value
[2], value
[3] };
374 res
= ValueComparer
.equalValue(vals
, rightVal
);
375 out
.println("Expected b, 23, 4, 3");
376 out
.println("getting: " + value
[0] + ", " +
377 value
[1] + ", " + value
[2] + ", " +
381 if (isSortAscending
) {
382 String
[] rightVal
= { "3", "4", "23", "b" };
383 res
= ValueComparer
.equalValue(value
, rightVal
);
384 out
.println("Expected 3, 4, 23, b");
385 out
.println("getting: " + value
[0] + ", " +
386 value
[1] + ", " + value
[2] + ", " +
389 String
[] rightVal
= { "b", "23", "4", "3" };
390 res
= ValueComparer
.equalValue(value
, rightVal
);
391 out
.println("Expected b, 23, 4, 3");
392 out
.println("getting: " + value
[0] + ", " +
393 value
[1] + ", " + value
[2] + ", " +
399 out
.println("Sorted correctly");
401 out
.println("Sorted uncorrectly");