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