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.XEnumerationAccess;
35 import com.sun.star.container.XIndexAccess;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sheet.XCellFormatRangesSupplier;
39 import com.sun.star.sheet.XSpreadsheet;
40 import com.sun.star.sheet.XSpreadsheetDocument;
41 import com.sun.star.sheet.XSpreadsheets;
42 import com.sun.star.uno.AnyConverter;
43 import com.sun.star.uno.Type;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.uno.XInterface;
46 
47 /**
48 * Test for object which is represented by service
49 * <code>com.sun.star.sheet.CellFormatRangesEnumeration</code>. <p>
50 * Object implements the following interfaces :
51 * <ul>
52 *  <li> <code>com::sun::star::container::XEnumeration</code></li>
53 * </ul>
54 * @see com.sun.star.sheet.CellFormatRangesEnumeration
55 * @see com.sun.star.container.XEnumeration
56 * @see ifc.container._XEnumeration
57 */
58 public class ScCellFormatsEnumeration extends TestCase {
59     static XSpreadsheetDocument xSheetDoc = null;
60 
61     /**
62     * Creates Spreadsheet document.
63     */
initialize( TestParameters tParam, PrintWriter log )64     protected void initialize( TestParameters tParam, PrintWriter log ) {
65         // get a soffice factory object
66         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
67 
68         try {
69             log.println( "creating a sheetdocument" );
70             xSheetDoc = SOF.createCalcDoc(null);
71         } catch (com.sun.star.uno.Exception e) {
72             // Some exception occured.FAILED
73             e.printStackTrace( log );
74             throw new StatusException( "Couldn't create document", e);
75         }
76     }
77 
78     /**
79     * Disposes Spreadsheet document.
80     */
cleanup( TestParameters tParam, PrintWriter log )81     protected void cleanup( TestParameters tParam, PrintWriter log ) {
82         log.println( "    disposing xSheetDoc " );
83         XComponent oComp = (XComponent)
84             UnoRuntime.queryInterface (XComponent.class, xSheetDoc);
85         util.DesktopTools.closeDoc(oComp);
86     }
87 
88     /**
89     * Creating a Testenvironment for the interfaces to be tested.
90     * Retrieves a collection of spreadsheets from a document,
91     * and takes one of them. Then retrieves a collection of cell format range
92     * using the interface <code>XCellFormatRangesSupplier</code>, creates the
93     * enumeration of this collection using interface <code>XEnumerationAccess</code>.
94     * This enumeration is the instance of the service
95     * <code>com.sun.star.sheet.CellFormatRangesEnumeration</code>.
96     * Object relations created :
97     * <ul>
98     *  <li> <code>'ENUM'</code> for
99     *      {@link ifc.container._XEnumeration} (type of
100     *      <code>XEnumerationAccess</code> that was queried from the collection
101     *      of cell format range)</li>
102     * </ul>
103     * @see com.sun.star.sheet.CellFormatRangesEnumeration
104     * @see com.sun.star.sheet.XCellFormatRangesSupplier
105     * @see com.sun.star.container.XEnumerationAccess
106     */
createTestEnvironment(TestParameters Param, PrintWriter log)107     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
108 
109         XInterface oObj = null;
110 
111         log.println("getting sheets");
112         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSheetDoc.getSheets();
113 
114         log.println("getting a sheet");
115         XSpreadsheet oSheet = null;
116         XIndexAccess oIndexAccess = (XIndexAccess)
117             UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
118 
119         try {
120             oSheet = (XSpreadsheet) AnyConverter.toObject(
121                     new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0));
122         } catch (com.sun.star.lang.WrappedTargetException e) {
123             e.printStackTrace(log);
124             throw new StatusException( "Couldn't get a spreadsheet", e);
125         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
126             e.printStackTrace(log);
127             throw new StatusException( "Couldn't get a spreadsheet", e);
128         } catch (com.sun.star.lang.IllegalArgumentException e) {
129             e.printStackTrace(log);
130             throw new StatusException( "Couldn't get a spreadsheet", e);
131         }
132 
133         log.println("getting CellFormats");
134 
135         XCellFormatRangesSupplier xCFRS = (XCellFormatRangesSupplier)
136             UnoRuntime.queryInterface(XCellFormatRangesSupplier.class,oSheet);
137         XIndexAccess formats = xCFRS.getCellFormatRanges();
138 
139         log.println("getting Enumeration");
140         XEnumerationAccess oEnum = (XEnumerationAccess)
141             UnoRuntime.queryInterface(XEnumerationAccess.class,formats);
142         oObj = oEnum.createEnumeration();
143 
144         log.println("creating a new environment for object");
145         TestEnvironment tEnv = new TestEnvironment(oObj);
146 
147         tEnv.addObjRelation("ENUM", oEnum);
148 
149         return tEnv;
150     }
151 
152 }    // finish class ScCellFormatsEnumeration
153 
154