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