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