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.container.XNameAccess;
35cdf0e10cSrcweir import com.sun.star.lang.XComponent;
36cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
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.SheetCellRange</code>. <p>
52cdf0e10cSrcweir * Object implements the following interfaces :
53cdf0e10cSrcweir * <ul>
54cdf0e10cSrcweir *  <li> <code>com::sun::star::table::CellProperties</code></li>
55cdf0e10cSrcweir *  <li> <code>com::sun::star::util::XMergeable</code></li>
56cdf0e10cSrcweir *  <li> <code>com::sun::star::sheet::XCellRangeAddressable</code></li>
57cdf0e10cSrcweir *  <li> <code>com::sun::star::sheet::XArrayFormulaRange</code></li>
58cdf0e10cSrcweir *  <li> <code>com::sun::star::chart::XChartData</code></li>
59cdf0e10cSrcweir *  <li> <code>com::sun::star::table::XColumnRowRange</code></li>
60cdf0e10cSrcweir *  <li> <code>com::sun::star::beans::XPropertySet</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::table::XCellRange</code></li>
64cdf0e10cSrcweir *  <li> <code>com::sun::star::style::ParagraphProperties</code></li>
65cdf0e10cSrcweir *  <li> <code>com::sun::star::sheet::SheetCellRange</code></li>
66cdf0e10cSrcweir * </ul>
67cdf0e10cSrcweir * @see com.sun.star.sheet.SheetCellRange
68cdf0e10cSrcweir * @see com.sun.star.table.CellProperties
69cdf0e10cSrcweir * @see com.sun.star.util.XMergeable
70cdf0e10cSrcweir * @see com.sun.star.sheet.XCellRangeAddressable
71cdf0e10cSrcweir * @see com.sun.star.sheet.XArrayFormulaRange
72cdf0e10cSrcweir * @see com.sun.star.chart.XChartData
73cdf0e10cSrcweir * @see com.sun.star.table.XColumnRowRange
74cdf0e10cSrcweir * @see com.sun.star.beans.XPropertySet
75cdf0e10cSrcweir * @see com.sun.star.style.CharacterProperties
76cdf0e10cSrcweir * @see com.sun.star.sheet.XSheetOperation
77cdf0e10cSrcweir * @see com.sun.star.table.XCellRange
78cdf0e10cSrcweir * @see com.sun.star.style.ParagraphProperties
79cdf0e10cSrcweir * @see com.sun.star.sheet.SheetCellRange
80cdf0e10cSrcweir * @see ifc.table._CellProperties
81cdf0e10cSrcweir * @see ifc.util._XMergeable
82cdf0e10cSrcweir * @see ifc.sheet._XCellRangeAddressable
83cdf0e10cSrcweir * @see ifc.sheet._XArrayFormulaRange
84cdf0e10cSrcweir * @see ifc.chart._XChartData
85cdf0e10cSrcweir * @see ifc.table._XColumnRowRange
86cdf0e10cSrcweir * @see ifc.beans._XPropertySet
87cdf0e10cSrcweir * @see ifc.style._CharacterProperties
88cdf0e10cSrcweir * @see ifc.sheet._XSheetOperation
89cdf0e10cSrcweir * @see ifc.table._XCellRange
90cdf0e10cSrcweir * @see ifc.style._ParagraphProperties
91cdf0e10cSrcweir * @see ifc.sheet._SheetCellRange
92cdf0e10cSrcweir */
93cdf0e10cSrcweir public class ScCellRangeObj extends TestCase {
94cdf0e10cSrcweir     XSpreadsheetDocument xSheetDoc = null;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     /**
97cdf0e10cSrcweir     * Creates Spreadsheet document.
98cdf0e10cSrcweir     */
initialize(TestParameters tParam, PrintWriter log)99cdf0e10cSrcweir     protected void initialize(TestParameters tParam, PrintWriter log) {
100cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory) tParam.getMSF());
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         try {
103cdf0e10cSrcweir             log.println("creating a Spreadsheet document");
104cdf0e10cSrcweir             xSheetDoc = SOF.createCalcDoc(null);
105cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
106cdf0e10cSrcweir             // Some exception occures.FAILED
107cdf0e10cSrcweir             e.printStackTrace(log);
108cdf0e10cSrcweir             throw new StatusException("Couldn't create document", e);
109cdf0e10cSrcweir         }
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir     /**
113cdf0e10cSrcweir     * Disposes Spreadsheet document.
114cdf0e10cSrcweir     */
cleanup(TestParameters tParam, PrintWriter log)115cdf0e10cSrcweir     protected void cleanup(TestParameters tParam, PrintWriter log) {
116cdf0e10cSrcweir         log.println("    disposing xSheetDoc ");
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         XComponent oComp = (XComponent) UnoRuntime.queryInterface(
119cdf0e10cSrcweir                                    XComponent.class, xSheetDoc);
120cdf0e10cSrcweir         util.DesktopTools.closeDoc(oComp);
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     /**
124cdf0e10cSrcweir     * Creating a Testenvironment for the interfaces to be tested.
125cdf0e10cSrcweir     * Retrieves a collection of spreadsheets from a document,
126cdf0e10cSrcweir     * and takes one of them. Retrieves some cell range from the spreadsheet.
127cdf0e10cSrcweir     * The retrieved cell range is instance of the service
128cdf0e10cSrcweir     * <code>com.sun.star.sheet.SheetCellRange</code>.
129cdf0e10cSrcweir     * Object relations created :
130cdf0e10cSrcweir     * <ul>
131cdf0e10cSrcweir     *  <li> <code>'SHEET'</code> for
132cdf0e10cSrcweir     *      {@link ifc.sheet._XArrayFormulaRange} (the spreadsheet which the cell
133cdf0e10cSrcweir     *      range was retrieved from)</li>
134cdf0e10cSrcweir     * </ul>
135cdf0e10cSrcweir     * @see com.sun.star.sheet.XSpreadsheet
136cdf0e10cSrcweir     */
createTestEnvironment(TestParameters Param, PrintWriter log)137cdf0e10cSrcweir     protected TestEnvironment createTestEnvironment(TestParameters Param,
138cdf0e10cSrcweir                                                     PrintWriter log) {
139cdf0e10cSrcweir         XInterface oObj = null;
140cdf0e10cSrcweir         XCellRange testRange;
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 
143cdf0e10cSrcweir         // creation of testobject here
144cdf0e10cSrcweir         // first we write what we are intend to do to log file
145cdf0e10cSrcweir         log.println("Creating a test environment");
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         XSpreadsheets oSpreadsheets = ((XSpreadsheetDocument) UnoRuntime.queryInterface(
148cdf0e10cSrcweir                                                XSpreadsheetDocument.class,
149cdf0e10cSrcweir                                                xSheetDoc)).getSheets();
150cdf0e10cSrcweir         XNameAccess oNames = (XNameAccess) UnoRuntime.queryInterface(
151cdf0e10cSrcweir                                      XNameAccess.class, oSpreadsheets);
152cdf0e10cSrcweir 
153cdf0e10cSrcweir         XSpreadsheet oSheet = null;
154cdf0e10cSrcweir 
155cdf0e10cSrcweir         try {
156cdf0e10cSrcweir             oSheet = (XSpreadsheet) AnyConverter.toObject(
157cdf0e10cSrcweir                              new Type(XSpreadsheet.class),
158cdf0e10cSrcweir                              oNames.getByName(oNames.getElementNames()[0]));
159cdf0e10cSrcweir 
160cdf0e10cSrcweir             oObj = oSheet.getCellRangeByPosition(0, 0, 3, 4);
161cdf0e10cSrcweir             testRange = (XCellRange) UnoRuntime.queryInterface(
162cdf0e10cSrcweir                                 XCellRange.class, oObj);
163cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
164cdf0e10cSrcweir             e.printStackTrace(log);
165cdf0e10cSrcweir             throw new StatusException(
166cdf0e10cSrcweir                     "Error getting cell object from spreadsheet document", e);
167cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
168cdf0e10cSrcweir             e.printStackTrace(log);
169cdf0e10cSrcweir             throw new StatusException(
170cdf0e10cSrcweir                     "Error getting cell object from spreadsheet document", e);
171cdf0e10cSrcweir         } catch (com.sun.star.container.NoSuchElementException e) {
172cdf0e10cSrcweir             e.printStackTrace(log);
173cdf0e10cSrcweir             throw new StatusException(
174cdf0e10cSrcweir                     "Error getting cell object from spreadsheet document", e);
175cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
176cdf0e10cSrcweir             e.printStackTrace(log);
177cdf0e10cSrcweir             throw new StatusException(
178cdf0e10cSrcweir                     "Error getting cell object from spreadsheet document", e);
179cdf0e10cSrcweir         }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 
182cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment(oObj);
183cdf0e10cSrcweir 
184cdf0e10cSrcweir         tEnv.addObjRelation("SHEET", oSheet);
185cdf0e10cSrcweir         // add expected results for the XCellRangesQuery interface test
186cdf0e10cSrcweir         String[]expectedResults = new String[7];
187cdf0e10cSrcweir         expectedResults[_XCellRangesQuery.QUERYCOLUMNDIFFERENCES] = "Sheet1.B1:C1;Sheet1.B3";
188cdf0e10cSrcweir         expectedResults[_XCellRangesQuery.QUERYCONTENTCELLS] = "Sheet1.B2:B3";
189cdf0e10cSrcweir         expectedResults[_XCellRangesQuery.QUERYEMPTYCELLS] = "Sheet1.A1 ... Sheet1.B1 ... Sheet1.B5 ... Sheet1.C3 ... Sheet1.D1";
190cdf0e10cSrcweir         expectedResults[_XCellRangesQuery.QUERYFORMULACELLS] = "Sheet1.C2";
191cdf0e10cSrcweir         expectedResults[_XCellRangesQuery.QUERYINTERSECTION] = "Sheet1.D4";
192cdf0e10cSrcweir         expectedResults[_XCellRangesQuery.QUERYROWDIFFERENCES] = "Sheet1.A2:A4;Sheet1.C2:D4";
193cdf0e10cSrcweir         expectedResults[_XCellRangesQuery.QUERYVISIBLECELLS] = "Sheet1.A2";
194cdf0e10cSrcweir         tEnv.addObjRelation("XCellRangesQuery.EXPECTEDRESULTS", expectedResults);
195cdf0e10cSrcweir         tEnv.addObjRelation("XCellRangesQuery.CREATEENTRIES", Boolean.TRUE);
196cdf0e10cSrcweir 
197cdf0e10cSrcweir         XPropertySet PropSet = (XPropertySet) UnoRuntime.queryInterface(
198cdf0e10cSrcweir                                        XPropertySet.class, oObj);
199cdf0e10cSrcweir         tEnv.addObjRelation("PropSet", PropSet);
200cdf0e10cSrcweir 
201cdf0e10cSrcweir         // XSearchable: Add a cell to make a seacrchable entry
202cdf0e10cSrcweir         try {
203cdf0e10cSrcweir             tEnv.addObjRelation("XSearchable.MAKEENTRYINCELL", new XCell[] {
204cdf0e10cSrcweir                         testRange.getCellByPosition(0,0), testRange.getCellByPosition(0,1)});
205cdf0e10cSrcweir         }
206cdf0e10cSrcweir         catch(com.sun.star.lang.IndexOutOfBoundsException e){
207cdf0e10cSrcweir             e.printStackTrace((PrintWriter)log);
208cdf0e10cSrcweir             log.println("Cannot make required object relation 'XSearchable.MAKEENTRYINCELL'.");
209cdf0e10cSrcweir         }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir         // XCellRangeData
212cdf0e10cSrcweir /*        Object[][] newData = new Object[5][4];
213cdf0e10cSrcweir         for (int i=0; i<newData.length; i++) {
214cdf0e10cSrcweir             for (int j=0; j<newData[i].length; j++) {
215cdf0e10cSrcweir                 newData[i][j] = new Double(i*10+j);
216cdf0e10cSrcweir             }
217cdf0e10cSrcweir         }
218cdf0e10cSrcweir         tEnv.addObjRelation("NewData", newData); */
219cdf0e10cSrcweir 
220cdf0e10cSrcweir         //Adding relation for util.XSortable
221cdf0e10cSrcweir         final PrintWriter finalLog = log;
222cdf0e10cSrcweir         final XCellRange oTable = testRange;
223cdf0e10cSrcweir         tEnv.addObjRelation("SORTCHECKER",
224cdf0e10cSrcweir                             new ifc.util._XSortable.XSortChecker() {
225cdf0e10cSrcweir             PrintWriter out = finalLog;
226cdf0e10cSrcweir 
227cdf0e10cSrcweir             public void setPrintWriter(PrintWriter log) {
228cdf0e10cSrcweir                 out = log;
229cdf0e10cSrcweir             }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir             public void prepareToSort() {
232cdf0e10cSrcweir                 try {
233cdf0e10cSrcweir                     oTable.getCellByPosition(0, 0).setValue(4);
234cdf0e10cSrcweir                     oTable.getCellByPosition(0, 1).setFormula("b");
235cdf0e10cSrcweir                     oTable.getCellByPosition(0, 2).setValue(3);
236cdf0e10cSrcweir                     oTable.getCellByPosition(0, 3).setValue(23);
237cdf0e10cSrcweir                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
238cdf0e10cSrcweir                     out.println("Exception while checking sort");
239cdf0e10cSrcweir                 }
240cdf0e10cSrcweir             }
241cdf0e10cSrcweir 
242cdf0e10cSrcweir             public boolean checkSort(boolean isSortNumbering,
243cdf0e10cSrcweir                                      boolean isSortAscending) {
244cdf0e10cSrcweir                 out.println("Sort checking...");
245cdf0e10cSrcweir 
246cdf0e10cSrcweir                 boolean res = false;
247cdf0e10cSrcweir                 String[] value = new String[4];
248cdf0e10cSrcweir 
249cdf0e10cSrcweir                 for (int i = 0; i < 4; i++) {
250cdf0e10cSrcweir                     try {
251cdf0e10cSrcweir                         XCell cell = oTable.getCellByPosition(0, i);
252cdf0e10cSrcweir                         value[i] = cell.getFormula();
253cdf0e10cSrcweir                     } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
254cdf0e10cSrcweir                         out.println("Exception while checking sort");
255cdf0e10cSrcweir                     }
256cdf0e10cSrcweir                 }
257cdf0e10cSrcweir 
258cdf0e10cSrcweir                 if (isSortNumbering) {
259cdf0e10cSrcweir                     if (isSortAscending) {
260cdf0e10cSrcweir                         out.println("Sorting ascending");
261cdf0e10cSrcweir 
262cdf0e10cSrcweir                         String[] rightVal = { "3", "4", "23", "b" };
263cdf0e10cSrcweir                         String[] vals = { value[0], value[1], value[2], value[3] };
264cdf0e10cSrcweir                         res = ValueComparer.equalValue(vals, rightVal);
265cdf0e10cSrcweir                         out.println("Expected 3, 4, 23, b");
266cdf0e10cSrcweir                         out.println("getting: " + value[0] + ", " +
267cdf0e10cSrcweir                                         value[1] + ", " + value[2] + ", " +
268cdf0e10cSrcweir                                         value[3]);
269cdf0e10cSrcweir                     } else {
270cdf0e10cSrcweir                         String[] rightVal = { "b", "23", "4", "3" };
271cdf0e10cSrcweir                         String[] vals = { value[0], value[1], value[2], value[3] };
272cdf0e10cSrcweir                         res = ValueComparer.equalValue(vals, rightVal);
273cdf0e10cSrcweir                         out.println("Expected b, 23, 4, 3");
274cdf0e10cSrcweir                         out.println("getting: " + value[0] + ", " +
275cdf0e10cSrcweir                                         value[1] + ", " + value[2] + ", " +
276cdf0e10cSrcweir                                         value[3]);
277cdf0e10cSrcweir                     }
278cdf0e10cSrcweir                 } else {
279cdf0e10cSrcweir                     if (isSortAscending) {
280cdf0e10cSrcweir                         String[] rightVal = { "3", "4", "23", "b" };
281cdf0e10cSrcweir                         res = ValueComparer.equalValue(value, rightVal);
282cdf0e10cSrcweir                         out.println("Expected 3, 4, 23, b");
283cdf0e10cSrcweir                         out.println("getting: " + value[0] + ", " +
284cdf0e10cSrcweir                                         value[1] + ", " + value[2] + ", " +
285cdf0e10cSrcweir                                         value[3]);
286cdf0e10cSrcweir                     } else {
287cdf0e10cSrcweir                         String[] rightVal = { "b", "23", "4", "3" };
288cdf0e10cSrcweir                         res = ValueComparer.equalValue(value, rightVal);
289cdf0e10cSrcweir                         out.println("Expected b, 23, 4, 3");
290cdf0e10cSrcweir                         out.println("getting: " + value[0] + ", " +
291cdf0e10cSrcweir                                         value[1] + ", " + value[2] + ", " +
292cdf0e10cSrcweir                                         value[3]);
293cdf0e10cSrcweir                     }
294cdf0e10cSrcweir                 }
295cdf0e10cSrcweir 
296cdf0e10cSrcweir                 if (res) {
297cdf0e10cSrcweir                     out.println("Sorted correctly");
298cdf0e10cSrcweir                 } else {
299cdf0e10cSrcweir                     out.println("Sorted uncorrectly");
300cdf0e10cSrcweir                 }
301cdf0e10cSrcweir 
302cdf0e10cSrcweir                 return res;
303cdf0e10cSrcweir             }
304cdf0e10cSrcweir         });
305cdf0e10cSrcweir 
306cdf0e10cSrcweir         return tEnv;
307cdf0e10cSrcweir     }
308cdf0e10cSrcweir } // finish class ScCellRangeObj
309