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