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.SOfficeFactory;
32 
33 import com.sun.star.container.XIndexAccess;
34 import com.sun.star.lang.XComponent;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.sheet.XSpreadsheet;
37 import com.sun.star.sheet.XSpreadsheetDocument;
38 import com.sun.star.sheet.XSpreadsheets;
39 import com.sun.star.table.XCell;
40 import com.sun.star.text.XText;
41 import com.sun.star.text.XTextContent;
42 import com.sun.star.uno.AnyConverter;
43 import com.sun.star.uno.Type;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.uno.XInterface;
46 
47 /**
48 * Test for object that represents a text field (implements
49 * <code>com.sun.star.text.TextField</code>) which inserted in a cell of
50 * the spreadsheet. <p>
51 * Object implements the following interfaces :
52 * <ul>
53 *  <li> <code>com::sun::star::lang::XComponent</code></li>
54 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
55 *  <li> <code>com::sun::star::text::XTextField</code></li>
56 *  <li> <code>com::sun::star::text::XTextContent</code></li>
57 *  <li> <code>com::sun::star::text::TextContent</code></li>
58 * </ul>
59 * @see com.sun.star.text.TextField
60 * @see com.sun.star.lang.XComponent
61 * @see com.sun.star.beans.XPropertySet
62 * @see com.sun.star.text.XTextField
63 * @see com.sun.star.text.XTextContent
64 * @see com.sun.star.text.TextContent
65 * @see ifc.lang._XComponent
66 * @see ifc.beans._XPropertySet
67 * @see ifc.text._XTextField
68 * @see ifc.text._XTextContent
69 * @see ifc.text._TextContent
70 */
71 public class ScCellFieldObj extends TestCase {
72     static XSpreadsheetDocument xSheetDoc = null;
73 
74     /**
75     * Creates Spreadsheet document.
76     */
initialize( TestParameters tParam, PrintWriter log )77     protected void initialize( TestParameters tParam, PrintWriter log ) {
78         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
79 
80         try {
81             log.println( "creating a Spreadsheet document" );
82             xSheetDoc = SOF.createCalcDoc(null);
83         } catch ( com.sun.star.uno.Exception e ) {
84             // Some exception occures.FAILED
85             e.printStackTrace( log );
86             throw new StatusException( "Couldn't create document", e );
87         }
88 
89     }
90 
91     /**
92     * Disposes Spreadsheet document.
93     */
cleanup( TestParameters tParam, PrintWriter log )94     protected void cleanup( TestParameters tParam, PrintWriter log ) {
95         log.println( "    disposing xSheetDoc " );
96         XComponent oComp = (XComponent)
97             UnoRuntime.queryInterface (XComponent.class, xSheetDoc);
98         util.DesktopTools.closeDoc(oComp);
99     }
100 
101     /**
102     * Creating a Testenvironment for the interfaces to be tested.
103     * Creates an instance of the service
104     * <code>com.sun.star.text.TextField.URL</code>, inserts it to the content
105     * of the cell in the spreadsheet, retrieves a text content
106     * <code>com.sun.star.text.XTextContent</code> from the cell.<p>
107     * Object relations created :
108     * <ul>
109     *  <li> <code>'TRO'</code> for
110     *      {@link ifc.text._TextContent} </li>
111     *  <li> <code>'CONTENT'</code> for
112     *      {@link ifc.text._XTextContent} (type of
113     *      <code>com.sun.star.text.XTextContent</code> that was queried from
114     *      the newly created service <code>com.sun.star.text.TextField.URL</code>)</li>
115     *  <li> <code>'TEXT'</code> for
116     *      {@link ifc.text._XTextContent} (the text of the cell)</li>
117     * </ul>
118     */
createTestEnvironment( TestParameters Param, PrintWriter log)119     protected synchronized TestEnvironment createTestEnvironment(
120         TestParameters Param, PrintWriter log) {
121 
122         XInterface oObj = null;
123         XText oText = null;
124         XTextContent oContent = null;
125         XInterface aField = null;
126 
127         try {
128             // we want to create an instance of ScCellFieldObj.
129             // to do this we must get an MultiServiceFactory.
130 
131             XMultiServiceFactory _oMSF = (XMultiServiceFactory)
132                 UnoRuntime.queryInterface(XMultiServiceFactory.class, xSheetDoc);
133 
134             // Now create the instance of com.sun.star.text.TextField.
135             // This object has type ScCellFieldObj.
136 
137             oObj = (XInterface)
138                 _oMSF.createInstance("com.sun.star.text.TextField.URL");
139 
140             aField = (XInterface)
141                 _oMSF.createInstance("com.sun.star.text.TextField.URL");
142             oContent = (XTextContent)
143                 UnoRuntime.queryInterface(XTextContent.class, aField);
144 
145             XSpreadsheets oSheets = xSheetDoc.getSheets() ;
146             XIndexAccess oIndexSheets = (XIndexAccess)
147                 UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
148             XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
149                     new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
150 
151             XCell oCell = oSheet.getCellByPosition(2,3);
152             oText = (XText)UnoRuntime.queryInterface(XText.class, oCell);
153 
154             XTextContent oTextContent = (XTextContent)
155                 UnoRuntime.queryInterface(XTextContent.class, oObj);
156 
157             oText.insertTextContent(
158                 oText.createTextCursor(), oTextContent, true);
159 
160             oCell = oSheet.getCellByPosition(1,4);
161             oText = (XText)UnoRuntime.queryInterface(XText.class, oCell);
162         } catch (com.sun.star.lang.WrappedTargetException e) {
163             log.println("Exception occured while creating test Object.");
164             e.printStackTrace(log);
165             throw new StatusException("Couldn't create test object", e);
166         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
167             log.println("Exception occured while creating test Object.");
168             e.printStackTrace(log);
169             throw new StatusException("Couldn't create test object", e);
170         } catch (com.sun.star.lang.IllegalArgumentException e) {
171             log.println("Exception occured while creating test Object.");
172             e.printStackTrace(log);
173             throw new StatusException("Couldn't create test object", e);
174         } catch (com.sun.star.uno.Exception e) {
175             log.println("Exception occured while creating test Object.");
176             e.printStackTrace(log);
177             throw new StatusException("Couldn't create test object", e);
178         }
179 
180         TestEnvironment tEnv = new TestEnvironment(oObj) ;
181 
182         log.println ("Object created.") ;
183         tEnv.addObjRelation("TRO", new Boolean(true));
184 
185         tEnv.addObjRelation("CONTENT",oContent);
186         tEnv.addObjRelation("TEXT",oText);
187 
188         return tEnv;
189     }
190 
191 }    // finish class ScCellFieldObj
192 
193