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 
24 package mod._sc;
25 
26 import java.io.PrintWriter;
27 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.SOfficeFactory;
33 
34 import com.sun.star.container.XIndexAccess;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.sheet.XCellRangesQuery;
38 import com.sun.star.sheet.XSheetCellRanges;
39 import com.sun.star.sheet.XSpreadsheetDocument;
40 import com.sun.star.sheet.XSpreadsheets;
41 import com.sun.star.table.XCell;
42 import com.sun.star.table.XCellRange;
43 import com.sun.star.text.XTextRange;
44 import com.sun.star.uno.AnyConverter;
45 import com.sun.star.uno.Type;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.uno.XInterface;
48 
49 /**
50 * Test for object which is represented by service
51 * <code>com.sun.star.sheet.Cells</code>. <p>
52 * Object implements the following interfaces :
53 * <ul>
54 *  <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
55 *  <li> <code>com::sun::star::container::XElementAccess</code></li>
56 * </ul>
57 * @see com.sun.star.sheet.Cells
58 * @see com.sun.star.container.XEnumerationAccess
59 * @see com.sun.star.container.XElementAccess
60 * @see ifc.container._XEnumerationAccess
61 * @see ifc.container._XElementAccess
62 */
63 public class ScCellsObj extends TestCase {
64     static XSpreadsheetDocument xSheetDoc = null;
65 
66     /**
67     * Creates Spreadsheet document.
68     */
initialize( TestParameters tParam, PrintWriter log )69     protected void initialize( TestParameters tParam, PrintWriter log ) {
70 
71         // get a soffice factory object
72         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
73 
74         try {
75             log.println( "creating a sheetdocument" );
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     * Disposes Spreadsheet document.
86     */
cleanup( TestParameters tParam, PrintWriter log )87     protected void cleanup( TestParameters tParam, PrintWriter log ) {
88         log.println( "    disposing xSheetDoc " );
89         XComponent oComp = (XComponent)
90             UnoRuntime.queryInterface (XComponent.class, xSheetDoc) ;
91         util.DesktopTools.closeDoc(oComp);
92     }
93 
94     /**
95     * Creating a Testenvironment for the interfaces to be tested.
96     * Retrieves a collection of spreadsheets from a document,
97     * and takes one of them. Replaces text of some cells.
98     * Retrives a cell range of the visible cells using the interface
99     * <code>XCellRangesQuery</code>. Retrieves a collection of cells from
100     * this cell range, this collection is instance of the service
101     * <code>com.sun.star.sheet.Cells</code>.
102     * @see com.sun.star.sheet.XCellRangesQuery
103     */
createTestEnvironment(TestParameters Param, PrintWriter log)104     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
105 
106         XInterface oObj = null;
107         Object cellArr[] = new Object[3];
108 
109         // creation of testobject here
110         XSpreadsheets oSheets = (XSpreadsheets)xSheetDoc.getSheets();
111         XIndexAccess oIndexAccess = (XIndexAccess)
112             UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
113         XCellRange oSheet = null;
114         try {
115             oSheet = (XCellRange) AnyConverter.toObject(
116                     new Type(XCellRange.class),oIndexAccess.getByIndex(0));
117 
118             XCell oCell_1 = (XCell)oSheet.getCellByPosition(0, 0);
119             XTextRange oTextRange = (XTextRange)
120                 UnoRuntime.queryInterface(XTextRange.class, oCell_1);
121 
122             oTextRange.setString("ScCellsObj test 1");
123 
124             XCell oCell_2 = (XCell)oSheet.getCellByPosition(5, 1);
125             oCell_2.setValue(15);
126 
127             XCell oCell_3 = (XCell)oSheet.getCellByPosition(3, 9);
128             oTextRange = (XTextRange)
129                 UnoRuntime.queryInterface(XTextRange.class, oCell_3);
130 
131             oTextRange.setString("ScCellsObj test 2");
132 
133             cellArr[0] = oCell_1;
134             cellArr[2] = oCell_2;
135             cellArr[1] = oCell_3;
136         } catch(com.sun.star.lang.WrappedTargetException e) {
137             log.println ("Exception occured while creating test Object.");
138             e.printStackTrace(log);
139             throw new StatusException("Couldn't create test object", e);
140         } catch(com.sun.star.lang.IndexOutOfBoundsException 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.IllegalArgumentException 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         }
149 
150         XCellRangesQuery oCellRangesQuery = (XCellRangesQuery)
151             UnoRuntime.queryInterface(XCellRangesQuery.class, oSheet);
152         XSheetCellRanges oSheetCellRanges = oCellRangesQuery.queryVisibleCells();
153 
154         oObj = oSheetCellRanges.getCells();
155 
156         TestEnvironment tEnv = new TestEnvironment(oObj) ;
157         log.println ("Object created.") ;
158 
159         return tEnv;
160     }
161 
162 }    // finish class ScCellsObj
163 
164