1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package mod._sc;
28 import java.io.PrintWriter;
29 
30 import lib.StatusException;
31 import lib.TestCase;
32 import lib.TestEnvironment;
33 import lib.TestParameters;
34 import util.SOfficeFactory;
35 import util.ValueComparer;
36 
37 import com.sun.star.beans.XPropertySet;
38 import com.sun.star.lang.XComponent;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.sheet.XSheetCellRange;
41 import com.sun.star.sheet.XSpreadsheet;
42 import com.sun.star.sheet.XSpreadsheetDocument;
43 import com.sun.star.sheet.XSpreadsheets;
44 import com.sun.star.table.XCell;
45 import com.sun.star.table.XCellRange;
46 import com.sun.star.uno.AnyConverter;
47 import com.sun.star.uno.Type;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XInterface;
50 import ifc.sheet._XCellRangesQuery;
51 
52 
53 /**
54 * Test for object which is represented by service
55 * <code>com.sun.star.sheet.SheetCellCursor</code>. <p>
56 * Object implements the following interfaces :
57 * <ul>
58 *  <li> <code>com::sun::star::style::ParagraphProperties</code></li>
59 *  <li> <code>com::sun::star::sheet::XUsedAreaCursor</code></li>
60 *  <li> <code>com::sun::star::table::CellProperties</code></li>
61 *  <li> <code>com::sun::star::table::XCellRange</code></li>
62 *  <li> <code>com::sun::star::sheet::XCellRangeAddressable</code></li>
63 *  <li> <code>com::sun::star::table::XCellCursor</code></li>
64 *  <li> <code>com::sun::star::sheet::XSheetCellCursor</code></li>
65 *  <li> <code>com::sun::star::style::CharacterProperties</code></li>
66 *  <li> <code>com::sun::star::sheet::XSheetOperation</code></li>
67 *  <li> <code>com::sun::star::sheet::XArrayFormulaRange</code></li>
68 *  <li> <code>com::sun::star::sheet::XSheetCellRange</code></li>
69 *  <li> <code>com::sun::star::sheet::SheetCellRange</code></li>
70 *  <li> <code>com::sun::star::chart::XChartData</code></li>
71 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
72 *  <li> <code>com::sun::star::util::XMergeable</code></li>
73 *  <li> <code>com::sun::star::table::XColumnRowRange</code></li>
74 * </ul>
75 * @see com.sun.star.sheet.SheetCellCursor
76 * @see com.sun.star.style.ParagraphProperties
77 * @see com.sun.star.sheet.XUsedAreaCursor
78 * @see com.sun.star.table.CellProperties
79 * @see com.sun.star.table.XCellRange
80 * @see com.sun.star.sheet.XCellRangeAddressable
81 * @see com.sun.star.table.XCellCursor
82 * @see com.sun.star.sheet.XSheetCellCursor
83 * @see com.sun.star.style.CharacterProperties
84 * @see com.sun.star.sheet.XSheetOperation
85 * @see com.sun.star.sheet.XArrayFormulaRange
86 * @see com.sun.star.sheet.XSheetCellRange
87 * @see com.sun.star.sheet.SheetCellRange
88 * @see com.sun.star.chart.XChartData
89 * @see com.sun.star.beans.XPropertySet
90 * @see com.sun.star.util.XMergeable
91 * @see com.sun.star.table.XColumnRowRange
92 * @see ifc.style._ParagraphProperties
93 * @see ifc.sheet._XUsedAreaCursor
94 * @see ifc.table._CellProperties
95 * @see ifc.table._XCellRange
96 * @see ifc.sheet._XCellRangeAddressable
97 * @see ifc.table._XCellCursor
98 * @see ifc.sheet._XSheetCellCursor
99 * @see ifc.style._CharacterProperties
100 * @see ifc.sheet._XSheetOperation
101 * @see ifc.sheet._XArrayFormulaRange
102 * @see ifc.sheet._XSheetCellRange
103 * @see ifc.sheet._SheetCellRange
104 * @see ifc.chart._XChartData
105 * @see ifc.beans._XPropertySet
106 * @see ifc.util._XMergeable
107 * @see ifc.table._XColumnRowRange
108 */
109 public class ScCellCursorObj extends TestCase {
110     static XSpreadsheetDocument xSheetDoc = null;
111 
112     /**
113     * Creates Spreadsheet document.
114     */
115     protected void initialize(TestParameters tParam, PrintWriter log) {
116         SOfficeFactory SOF = SOfficeFactory.getFactory(
117                                      (XMultiServiceFactory) tParam.getMSF());
118 
119         try {
120             log.println("creating a Spreadsheet document");
121             xSheetDoc = SOF.createCalcDoc(null);
122         } catch (com.sun.star.uno.Exception e) {
123             // Some exception occures.FAILED
124             e.printStackTrace(log);
125             throw new StatusException("Couldn't create document", e);
126         }
127     }
128 
129     /**
130     * Disposes Spreadsheet document.
131     */
132     protected void cleanup(TestParameters tParam, PrintWriter log) {
133         log.println("    disposing xSheetDoc ");
134 
135         XComponent oComp = (XComponent) UnoRuntime.queryInterface(
136                                    XComponent.class, xSheetDoc);
137 
138         util.DesktopTools.closeDoc(oComp);
139     }
140 
141     /**
142     * Creating a Testenvironment for the interfaces to be tested.
143     * Retrieves a collection of spreadsheets from a document,
144     * and takes one of them, retrieves some cell range and creates cursor range
145     * that is instance of the service
146     * <code>com.sun.star.sheet.SheetCellCursor</code>,
147     * fills some cells in the cell range.
148     * Object relations created :
149     * <ul>
150     *  <li> <code>'SHEET'</code> for
151     *      {@link ifc.table._XCellCursor},
152     *      {@link ifc.sheet._XArrayFormulaRange} (the spreadsheet that was
153     *      retieved from collection)</li>
154     * </ul>
155     */
156     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
157                                                                  PrintWriter log) {
158         XInterface oObj = null;
159         TestEnvironment tEnv = null;
160         XSpreadsheet oSheet = null;
161         XCellRange testRange = null;
162 
163 
164         // creation of testobject here
165         // first we write what we are intend to do to log file
166         log.println("Creating a test environment");
167 
168         XSpreadsheets oSpreadsheets = ((XSpreadsheetDocument) UnoRuntime.queryInterface(
169                                                XSpreadsheetDocument.class,
170                                                xSheetDoc)).getSheets();
171 
172         try {
173             oSheet = (XSpreadsheet) AnyConverter.toObject(
174                              new Type(XSpreadsheet.class),
175                              oSpreadsheets.getByName(
176                                      oSpreadsheets.getElementNames()[0]));
177 
178             testRange = oSheet.getCellRangeByName("$A$1:$D$4");
179 
180             XSheetCellRange testSheetRange = (XSheetCellRange) UnoRuntime.queryInterface(
181                                                      XSheetCellRange.class,
182                                                      testRange);
183             oObj = oSheet.createCursorByRange(testSheetRange);
184             oSheet.getCellByPosition(1, 1).setValue(1);
185             oSheet.getCellByPosition(4, 5).setValue(1);
186             oSheet.getCellByPosition(3, 2).setFormula("xTextDoc");
187             oSheet.getCellByPosition(3, 3).setFormula("xTextDoc");
188         } catch (com.sun.star.lang.WrappedTargetException e) {
189             log.println("Exception occured while creating test object:");
190             e.printStackTrace(log);
191             throw new StatusException("Couldn't create test object", e);
192         } catch (com.sun.star.container.NoSuchElementException e) {
193             log.println("Exception occured while creating test object:");
194             e.printStackTrace(log);
195             throw new StatusException("Couldn't create test object", e);
196         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
197             log.println("Exception occured while creating test object:");
198             e.printStackTrace(log);
199             throw new StatusException("Couldn't create test object", e);
200         } catch (com.sun.star.lang.IllegalArgumentException e) {
201             log.println("Exception occured while creating test object:");
202             e.printStackTrace(log);
203             throw new StatusException("Couldn't create test object", e);
204         }
205 
206         log.println("Test object successfully created.");
207 
208         tEnv = new TestEnvironment(oObj);
209 
210         tEnv.addObjRelation("CRDESC",
211                             "Column and RowDescriptions can't be changed for this Object");
212 
213         tEnv.addObjRelation("SHEET", oSheet);
214         // add expected results for the XCellRangesQuery interface test
215         String[]expectedResults = new String[7];
216         expectedResults[_XCellRangesQuery.QUERYCOLUMNDIFFERENCES] = "Sheet1.B1";
217         expectedResults[_XCellRangesQuery.QUERYCONTENTCELLS] = "Sheet1.B2";
218         expectedResults[_XCellRangesQuery.QUERYEMPTYCELLS] = "Sheet1.A1 ... Sheet1.B1 ... Sheet1.B3 ... Sheet1.C1 ... Sheet1.D1";
219         expectedResults[_XCellRangesQuery.QUERYFORMULACELLS] = "";
220         expectedResults[_XCellRangesQuery.QUERYINTERSECTION] = "Sheet1.D4";
221         expectedResults[_XCellRangesQuery.QUERYROWDIFFERENCES] = "Sheet1.A2;Sheet1.C2";
222         expectedResults[_XCellRangesQuery.QUERYVISIBLECELLS] = "Sheet1.A2";
223         tEnv.addObjRelation("XCellRangesQuery.EXPECTEDRESULTS", expectedResults);
224 
225         tEnv.addObjRelation("NewData",
226                             new Object[][]
227         {
228             { "", "", "", "" },
229             { "", "2", "3", "4" },
230             { "", "2", "3", "4" },
231             { "", "2", "3", "4" }
232         });
233 
234         XPropertySet PropSet = (XPropertySet) UnoRuntime.queryInterface(
235                                        XPropertySet.class, oObj);
236         tEnv.addObjRelation("PropSet", PropSet);
237 
238         //Adding relation for util.XSortable
239         final PrintWriter finalLog = log;
240         final XCellRange oTable = testRange;
241         tEnv.addObjRelation("SORTCHECKER",
242                             new ifc.util._XSortable.XSortChecker() {
243             PrintWriter out = finalLog;
244 
245             public void setPrintWriter(PrintWriter log) {
246                 out = log;
247             }
248 
249             public void prepareToSort() {
250                 try {
251                     oTable.getCellByPosition(0, 0).setValue(4);
252                     oTable.getCellByPosition(0, 1).setFormula("b");
253                     oTable.getCellByPosition(0, 2).setValue(3);
254                     oTable.getCellByPosition(0, 3).setValue(23);
255                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
256                     out.println("Exception while checking sort");
257                 }
258             }
259 
260             public boolean checkSort(boolean isSortNumbering,
261                                      boolean isSortAscending) {
262                 out.println("Sort checking...");
263 
264                 boolean res = false;
265                 String[] value = new String[4];
266 
267                 for (int i = 0; i < 4; i++) {
268                     try {
269                         XCell cell = oTable.getCellByPosition(0, i);
270                         value[i] = cell.getFormula();
271                     } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
272                         out.println("Exception while checking sort");
273                     }
274                 }
275 
276                 if (isSortNumbering) {
277                     if (isSortAscending) {
278                         out.println("Sorting ascending");
279 
280                         String[] rightVal = { "3", "4", "23", "b" };
281                         String[] vals = { value[0], value[1], value[2], value[3] };
282                         res = ValueComparer.equalValue(vals, rightVal);
283                         out.println("Expected 3, 4, 23, b");
284                         out.println("getting: " + value[0] + ", " +
285                                         value[1] + ", " + value[2] + ", " +
286                                         value[3]);
287                     } else {
288                         String[] rightVal = { "b", "23", "4", "3" };
289                         String[] vals = { value[0], value[1], value[2], value[3] };
290                         res = ValueComparer.equalValue(vals, rightVal);
291                         out.println("Expected b, 23, 4, 3");
292                         out.println("getting: " + value[0] + ", " +
293                                         value[1] + ", " + value[2] + ", " +
294                                         value[3]);
295                     }
296                 } else {
297                     if (isSortAscending) {
298                         String[] rightVal = { "3", "4", "23", "b" };
299                         res = ValueComparer.equalValue(value, rightVal);
300                         out.println("Expected 3, 4, 23, b");
301                         out.println("getting: " + value[0] + ", " +
302                                         value[1] + ", " + value[2] + ", " +
303                                         value[3]);
304                     } else {
305                         String[] rightVal = { "b", "23", "4", "3" };
306                         res = ValueComparer.equalValue(value, rightVal);
307                         out.println("Expected b, 23, 4, 3");
308                         out.println("getting: " + value[0] + ", " +
309                                         value[1] + ", " + value[2] + ", " +
310                                         value[3]);
311                     }
312                 }
313 
314                 if (res) {
315                     out.println("Sorted correctly");
316                 } else {
317                     out.println("Sorted uncorrectly");
318                 }
319 
320                 return res;
321             }
322         });
323 
324         return tEnv;
325     }
326 } // finish class ScCellCursorObj
327