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