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 
29 import java.io.PrintWriter;
30 
31 import lib.StatusException;
32 import lib.TestCase;
33 import lib.TestEnvironment;
34 import lib.TestParameters;
35 import util.DefaultDsc;
36 import util.InstCreator;
37 import util.SOfficeFactory;
38 
39 import com.sun.star.beans.XPropertySet;
40 import com.sun.star.container.XIndexAccess;
41 import com.sun.star.lang.XComponent;
42 import com.sun.star.lang.XMultiServiceFactory;
43 import com.sun.star.sheet.XSpreadsheet;
44 import com.sun.star.sheet.XSpreadsheetDocument;
45 import com.sun.star.sheet.XSpreadsheets;
46 import com.sun.star.table.XCell;
47 import com.sun.star.uno.AnyConverter;
48 import com.sun.star.uno.Type;
49 import com.sun.star.uno.UnoRuntime;
50 import com.sun.star.uno.XInterface;
51 import ifc.sheet._XCellRangesQuery;
52 
53 /**
54 * Test for object which is represented by service
55 * <code>com.sun.star.sheet.SheetCell</code>. <p>
56 * Object implements the following interfaces :
57 * <ul>
58 *  <li> <code>com::sun::star::table::CellProperties</code></li>
59 *  <li> <code>com::sun::star::text::XSimpleText</code></li>
60 *  <li> <code>com::sun::star::table::XCell</code></li>
61 *  <li> <code>com::sun::star::text::XTextRange</code></li>
62 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
63 *  <li> <code>com::sun::star::style::CharacterProperties</code></li>
64 *  <li> <code>com::sun::star::document::XActionLockable</code></li>
65 *  <li> <code>com::sun::star::style::ParagraphProperties</code></li>
66 *  <li> <code>com::sun::star::text::XText</code></li>
67 *  <li> <code>com::sun::star::sheet::XCellAddressable</code></li>
68 * </ul>
69 * @see com.sun.star.sheet.SheetCell
70 * @see com.sun.star.table.CellProperties
71 * @see com.sun.star.text.XSimpleText
72 * @see com.sun.star.table.XCell
73 * @see com.sun.star.text.XTextRange
74 * @see com.sun.star.beans.XPropertySet
75 * @see com.sun.star.style.CharacterProperties
76 * @see com.sun.star.document.XActionLockable
77 * @see com.sun.star.style.ParagraphProperties
78 * @see com.sun.star.text.XText
79 * @see com.sun.star.sheet.XCellAddressable
80 * @see ifc.table._CellProperties
81 * @see ifc.text._XSimpleText
82 * @see ifc.table._XCell
83 * @see ifc.text._XTextRange
84 * @see ifc.beans._XPropertySet
85 * @see ifc.style._CharacterProperties
86 * @see ifc.document._XActionLockable
87 * @see ifc.style._ParagraphProperties
88 * @see ifc.text._XText
89 * @see ifc.sheet._XCellAddressable
90 */
91 public class ScCellObj extends TestCase {
92     static XSpreadsheetDocument xSheetDoc = null;
93 
94 
95     /**
96     * Creates Spreadsheet document.
97     */
98     protected void initialize( TestParameters tParam, PrintWriter log ) {
99         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
100 
101         try {
102             log.println( "Creating a Spreadsheet document" );
103             xSheetDoc = SOF.createCalcDoc(null);
104         } catch ( com.sun.star.uno.Exception e ) {
105             // Some exception occures.FAILED
106             e.printStackTrace( log );
107             throw new StatusException( "Couldn't create document", e );
108         }
109     }
110 
111     /**
112     * Disposes Spreadsheet document.
113     */
114     protected void cleanup( TestParameters tParam, PrintWriter log ) {
115         log.println( "    disposing xSheetDoc " );
116         XComponent oComp = (XComponent)
117             UnoRuntime.queryInterface (XComponent.class, xSheetDoc);
118         util.DesktopTools.closeDoc(oComp);
119     }
120 
121     /**
122     * Creating a Testenvironment for the interfaces to be tested.
123     * Retrieves a collection of spreadsheets from a document,
124     * and takes one of them. Retrieves some cell from the spreadsheet
125     * that is instance of the service <code>com.sun.star.sheet.SheetCell</code>.
126     * Object relations created :
127     * <ul>
128     *  <li> <code>'XTEXTINFO',</code> for
129     *      {@link ifc.text._XText} </li>
130     * </ul>
131     * @see com.sun.star.sheet.SheetCell
132     */
133     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
134 
135         XInterface oObj = null;
136 
137         // creation of testobject here
138         // first we write what we are intend to do to log file
139         log.println( "Creating a test environment" );
140 
141         XSpreadsheet oSheet = null;
142         XCell cell = null;
143         try {
144             log.println("Getting spreadsheet") ;
145             XSpreadsheets oSheets = xSheetDoc.getSheets() ;
146             XIndexAccess oIndexSheets = (XIndexAccess)
147             UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
148             oSheet = (XSpreadsheet) AnyConverter.toObject(
149                     new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
150 
151             log.println("Getting a cell from sheet") ;
152             oObj = oSheet.getCellByPosition(2, 3) ;
153             cell = (XCell)UnoRuntime.queryInterface(XCell.class, oObj);
154 
155         } catch (com.sun.star.lang.WrappedTargetException e) {
156             e.printStackTrace(log);
157             throw new StatusException(
158                 "Error getting cell object from spreadsheet document", e);
159         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
160             e.printStackTrace(log);
161             throw new StatusException(
162                 "Error getting cell object from spreadsheet document", e);
163         } catch (com.sun.star.lang.IllegalArgumentException e) {
164             e.printStackTrace(log);
165             throw new StatusException(
166                 "Error getting cell object from spreadsheet document", e);
167         }
168 
169         log.println( "creating a new environment for ScCellObj object" );
170         TestEnvironment tEnv = new TestEnvironment( oObj );
171 
172         // Object relations for interface tests
173         DefaultDsc tDsc = new DefaultDsc(
174             "com.sun.star.text.XTextContent", "com.sun.star.text.TextField.URL");
175         log.println( "    adding InstCreator object" );
176         tEnv.addObjRelation( "XTEXTINFO", new InstCreator( xSheetDoc, tDsc ) );
177         // add the sheet
178         tEnv.addObjRelation("SHEET", oSheet);
179         // add expected results for the XCellRangesQuery interface test
180         String[]expectedResults = new String[7];
181 
182         expectedResults[_XCellRangesQuery.QUERYCOLUMNDIFFERENCES] = "Sheet1.C4";
183         expectedResults[_XCellRangesQuery.QUERYCONTENTCELLS] = "";
184         expectedResults[_XCellRangesQuery.QUERYEMPTYCELLS] = "Sheet1.C4";
185         expectedResults[_XCellRangesQuery.QUERYFORMULACELLS] = "";
186         expectedResults[_XCellRangesQuery.QUERYINTERSECTION] = "";
187         expectedResults[_XCellRangesQuery.QUERYROWDIFFERENCES] = "Sheet1.C4";
188         expectedResults[_XCellRangesQuery.QUERYVISIBLECELLS] = "Sheet1.C4";
189         tEnv.addObjRelation("XCellRangesQuery.EXPECTEDRESULTS", expectedResults);
190         tEnv.addObjRelation("XCellRangesQuery.CREATEENTRIES", Boolean.TRUE);
191 
192         // make entries in this cell at the interface test
193         tEnv.addObjRelation("XTextFieldsSupplier.MAKEENTRY", Boolean.TRUE);
194         tEnv.addObjRelation("MAKEENTRYINCELL", cell);
195 
196         // for XSearchable amd XReplaceable interface test
197         tEnv.addObjRelation("XSearchable.MAKEENTRYINCELL", cell);
198         tEnv.addObjRelation("EXCLUDEFINDNEXT", Boolean.TRUE);
199 
200         // for XFormulaQuery interface test
201         tEnv.addObjRelation("EXPECTEDDEPENDENTVALUES", new int[]{2,2,3,3});
202         tEnv.addObjRelation("EXPECTEDPRECEDENTVALUES", new int[]{0,3,0,0});
203         tEnv.addObjRelation("RANGEINDICES", new int[]{0,0});
204 
205         // XTextFieldsSupplier
206         tEnv.addObjRelation("SPREADSHEET", xSheetDoc);
207 
208         XPropertySet PropSet = (XPropertySet)
209                     UnoRuntime.queryInterface(XPropertySet.class, oObj);
210         tEnv.addObjRelation("PropSet",PropSet);
211 
212         return tEnv;
213     } // finish method getTestEnvironment
214 
215 }    // finish class ScCellObj
216 
217