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