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.uno.AnyConverter;
42 import com.sun.star.uno.Type;
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.uno.XInterface;
45 
46 /**
47 * Test for object which is represented by service
48 * <code>com.sun.star.text.TextCursor</code>. <p>
49 * Object implements the following interfaces :
50 * <ul>
51 *  <li> <code>com::sun::star::text::XTextCursor</code></li>
52 *  <li> <code>com::sun::star::text::XWordCursor</code></li>
53 *  <li> <code>com::sun::star::style::CharacterPropertiesComplex</code></li>
54 *  <li> <code>com::sun::star::text::XTextRange</code></li>
55 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
56 *  <li> <code>com::sun::star::container::XContentEnumerationAccess</code></li>
57 *  <li> <code>com::sun::star::beans::XPropertyState</code></li>
58 *  <li> <code>com::sun::star::style::CharacterProperties</code></li>
59 *  <li> <code>com::sun::star::text::XSentenceCursor</code></li>
60 *  <li> <code>com::sun::star::style::ParagraphProperties</code></li>
61 *  <li> <code>com::sun::star::text::XParagraphCursor</code></li>
62 *  <li> <code>com::sun::star::document::XDocumentInsertable</code></li>
63 *  <li> <code>com::sun::star::util::XSortable</code></li>
64 *  <li> <code>com::sun::star::style::CharacterPropertiesAsian</code></li>
65 * </ul>
66 * @see com.sun.star.text.TextCursor
67 * @see com.sun.star.text.XTextCursor
68 * @see com.sun.star.text.XWordCursor
69 * @see com.sun.star.style.CharacterPropertiesComplex
70 * @see com.sun.star.text.XTextRange
71 * @see com.sun.star.beans.XPropertySet
72 * @see com.sun.star.container.XContentEnumerationAccess
73 * @see com.sun.star.beans.XPropertyState
74 * @see com.sun.star.style.CharacterProperties
75 * @see com.sun.star.text.XSentenceCursor
76 * @see com.sun.star.style.ParagraphProperties
77 * @see com.sun.star.text.XParagraphCursor
78 * @see com.sun.star.document.XDocumentInsertable
79 * @see com.sun.star.util.XSortable
80 * @see com.sun.star.style.CharacterPropertiesAsian
81 * @see ifc.text._XTextCursor
82 * @see ifc.text._XWordCursor
83 * @see ifc.style._CharacterPropertiesComplex
84 * @see ifc.text._XTextRange
85 * @see ifc.beans._XPropertySet
86 * @see ifc.container._XContentEnumerationAccess
87 * @see ifc.beans._XPropertyState
88 * @see ifc.style._CharacterProperties
89 * @see ifc.text._XSentenceCursor
90 * @see ifc.style._ParagraphProperties
91 * @see ifc.text._XParagraphCursor
92 * @see ifc.document._XDocumentInsertable
93 * @see ifc.util._XSortable
94 * @see ifc.style._CharacterPropertiesAsian
95 */
96 public class ScCellTextCursor extends TestCase {
97     static XSpreadsheetDocument xSheetDoc = null;
98 
99     /**
100     * Creates Spreadsheet document.
101     */
initialize( TestParameters tParam, PrintWriter log )102     protected void initialize( TestParameters tParam, PrintWriter log ) {
103         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
104 
105         try {
106             log.println( "creating a Spreadsheet document" );
107             xSheetDoc = SOF.createCalcDoc(null);
108         } catch ( com.sun.star.uno.Exception e ) {
109             // Some exception occures.FAILED
110             e.printStackTrace( log );
111             throw new StatusException( "Couldn't create document", e );
112         }
113 
114     }
115 
116     /**
117     * Disposes Spreadsheet document.
118     */
cleanup( TestParameters tParam, PrintWriter log )119     protected void cleanup( TestParameters tParam, PrintWriter log ) {
120         log.println( "    disposing xSheetDoc " );
121         XComponent oComp = (XComponent)
122             UnoRuntime.queryInterface (XComponent.class, xSheetDoc) ;
123         util.DesktopTools.closeDoc(oComp);
124     }
125 
126     /**
127     * Creating a Testenvironment for the interfaces to be tested.
128     * Retrieves a collection of spreadsheets from a document
129     * and takes one of them. Retrieves the cell from the spreadsheet
130     * and creates text cursor of the cell using the interface
131     * <code>XText</code>. This text cursor is the instance of
132     * the service <code>com.sun.star.text.TextCursor</code>.
133     * Object relations created :
134     * <ul>
135     *  <li> <code>'XTEXT'</code> for
136     *      {@link ifc.text._XTextRange} (type of
137     *      <code>XEnumerationAccess</code> that was retrieved from the
138     *      collection of visible cells)</li>
139     * </ul>
140     * @see com.sun.star.text.XText
141     */
createTestEnvironment(TestParameters tParam, PrintWriter log)142     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
143 
144         XInterface oObj = null;
145         XCell aCell = null;
146 
147         // creation of testobject here
148         // first we write what we are intend to do to log file
149         log.println( "Creating a test environment" );
150 
151         // get a soffice factory object
152         try {
153             log.println("Getting spreadsheet") ;
154             XSpreadsheets oSheets = xSheetDoc.getSheets() ;
155             XIndexAccess oIndexSheets = (XIndexAccess)
156                 UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
157             XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
158                     new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
159 
160             log.println("Getting a cell from sheet") ;
161             aCell = oSheet.getCellByPosition(1,1) ;
162 
163         } catch (com.sun.star.lang.WrappedTargetException e) {
164             e.printStackTrace(log) ;
165             throw new StatusException(
166                 "Error getting cell object from spreadsheet document",e) ;
167         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
168             e.printStackTrace(log) ;
169             throw new StatusException(
170                 "Error getting cell object from spreadsheet document",e) ;
171         } catch (com.sun.star.lang.IllegalArgumentException e) {
172             e.printStackTrace(log) ;
173             throw new StatusException(
174                 "Error getting cell object from spreadsheet document",e) ;
175         }
176 
177         XText aText = (XText) UnoRuntime.queryInterface(XText.class, aCell);
178         aText.setString("ScCellTextCursor");
179         oObj = aText.createTextCursor();
180 
181         log.println( "creating a new environment for ScCellTextCursor object" );
182         TestEnvironment tEnv = new TestEnvironment( oObj );
183 
184         // Object relations for interface tests
185         tEnv.addObjRelation("XTEXT", aText);
186 
187         return tEnv;
188     } // finish method getTestEnvironment
189 
190 }    // finish class ScCellTextCursor
191 
192