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.text.XTextFieldsSupplier;
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 
48 /**
49  * Test for object that represents a colection of text fields
50  * in a cell of a spreadsheet. <p>
51  *
52  * Object implements the following interfaces :
53  * <ul>
54  *  <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
55  *  <li> <code>com::sun::star::util::XRefreshable</code></li>
56  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
57  * </ul> <p>
58  *
59  * @see com.sun.star.container.XEnumerationAccess
60  * @see com.sun.star.util.XRefreshable
61  * @see com.sun.star.container.XElementAccess
62  * @see ifc.container._XEnumerationAccess
63  * @see ifc.util._XRefreshable
64  * @see ifc.container._XElementAccess
65  */
66 public class ScCellFieldsObj extends TestCase {
67     static XSpreadsheetDocument xSheetDoc = null;
68 
69     /**
70     * Creates Spreadsheet document.
71     */
initialize( TestParameters tParam, PrintWriter log )72     protected void initialize( TestParameters tParam, PrintWriter log ) {
73         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
74 
75         try {
76             log.println( "creating a Spreadsheet document" );
77             xSheetDoc = SOF.createCalcDoc(null);
78         } catch ( com.sun.star.uno.Exception e ) {
79             // Some exception occures.FAILED
80             e.printStackTrace( log );
81             throw new StatusException( "Couldn't create document", e );
82         }
83 
84     }
85 
86     /**
87     * Disposes Spreadsheet document.
88     */
cleanup( TestParameters tParam, PrintWriter log )89     protected void cleanup( TestParameters tParam, PrintWriter log ) {
90         log.println( "    disposing xSheetDoc " );
91         XComponent oComp = (XComponent)
92             UnoRuntime.queryInterface (XComponent.class, xSheetDoc);
93         util.DesktopTools.closeDoc(oComp);
94     }
95 
96     /**
97     * Creating a Testenvironment for the interfaces to be tested.
98     * Creates an instance of the service
99     * <code>com.sun.star.text.TextField.URL</code>, inserts it to the content
100     * of the cell in the spreadsheet. Then the component is obtained
101     * by <code>XTextFieldsSupplier</code> interface  of a cell.<p>
102     */
createTestEnvironment( TestParameters Param, PrintWriter log)103     protected synchronized TestEnvironment createTestEnvironment(
104         TestParameters Param, PrintWriter log) {
105 
106         XInterface oObj = null;
107         XText oText = null;
108         XTextContent oContent = null;
109         XInterface aField = null;
110 
111         try {
112             // we want to create an instance of ScCellFieldObj.
113             // to do this we must get an MultiServiceFactory.
114 
115             XMultiServiceFactory _oMSF = (XMultiServiceFactory)
116                 UnoRuntime.queryInterface(XMultiServiceFactory.class, xSheetDoc);
117 
118             aField = (XInterface)
119                 _oMSF.createInstance("com.sun.star.text.TextField.URL");
120             oContent = (XTextContent)
121                 UnoRuntime.queryInterface(XTextContent.class, aField);
122 
123             XSpreadsheets oSheets = xSheetDoc.getSheets() ;
124             XIndexAccess oIndexSheets = (XIndexAccess)
125                 UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
126             XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
127                         new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
128 
129             XCell oCell = oSheet.getCellByPosition(2,3);
130             oText = (XText)UnoRuntime.queryInterface(XText.class, oCell);
131 
132             oText.insertTextContent(
133                 oText.createTextCursor(), oContent, true);
134 
135             XTextFieldsSupplier xTextFieldsSupp = (XTextFieldsSupplier)
136                 UnoRuntime.queryInterface(XTextFieldsSupplier.class, oCell);
137 
138             oObj = xTextFieldsSupp.getTextFields();
139         } catch (com.sun.star.lang.WrappedTargetException e) {
140             log.println("Exception occured while creating test Object.");
141             e.printStackTrace(log);
142             throw new StatusException("Couldn't create test object", e);
143         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
144             log.println("Exception occured while creating test Object.");
145             e.printStackTrace(log);
146             throw new StatusException("Couldn't create test object", e);
147         } catch (com.sun.star.lang.IllegalArgumentException e) {
148             log.println("Exception occured while creating test Object.");
149             e.printStackTrace(log);
150             throw new StatusException("Couldn't create test object", e);
151         } catch (com.sun.star.uno.Exception e) {
152             log.println("Exception occured while creating test Object.");
153             e.printStackTrace(log);
154             throw new StatusException("Couldn't create test object", e);
155         }
156 
157         TestEnvironment tEnv = new TestEnvironment(oObj) ;
158 
159         return tEnv;
160     }
161 
162 }
163 
164