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 com.sun.star.container.XIndexAccess;
26 import com.sun.star.frame.XController;
27 import com.sun.star.frame.XModel;
28 import com.sun.star.lang.XComponent;
29 import com.sun.star.lang.XMultiServiceFactory;
30 import com.sun.star.sheet.XSpreadsheet;
31 import com.sun.star.sheet.XSpreadsheetDocument;
32 import com.sun.star.sheet.XSpreadsheets;
33 import com.sun.star.table.XCell;
34 import com.sun.star.uno.AnyConverter;
35 import com.sun.star.uno.Type;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.uno.XInterface;
38 import com.sun.star.view.XSelectionSupplier;
39 import ifc.view._XPrintJobBroadcaster;
40 import java.io.File;
41 
42 import java.io.PrintWriter;
43 
44 import lib.StatusException;
45 import lib.TestCase;
46 import lib.TestEnvironment;
47 import lib.TestParameters;
48 
49 import util.SOfficeFactory;
50 import util.utils;
51 
52 
53 /**
54 * Test for object which is represented by service
55 * <code>com.sun.star.sheet.SpreadsheetDocument</code>. <p>
56 * Object implements the following interfaces :
57 * <ul>
58 *  <li> <code>com::sun::star::sheet::XSpreadsheetDocument</code></li>
59 *  <li> <code>com::sun::star::sheet::SpreadsheetDocumentSettings</code></li>
60 *  <li> <code>com::sun::star::lang::XComponent</code></li>
61 *  <li> <code>com::sun::star::frame::XModel</code></li>
62 *  <li> <code>com::sun::star::sheet::SpreadsheetDocument</code></li>
63 *  <li> <code>com::sun::star::util::XNumberFormatsSupplier</code></li>
64 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
65 * </ul>
66 * @see com.sun.star.sheet.SpreadsheetDocument
67 * @see com.sun.star.sheet.XSpreadsheetDocument
68 * @see com.sun.star.sheet.SpreadsheetDocumentSettings
69 * @see com.sun.star.lang.XComponent
70 * @see com.sun.star.frame.XModel
71 * @see com.sun.star.sheet.SpreadsheetDocument
72 * @see com.sun.star.util.XNumberFormatsSupplier
73 * @see com.sun.star.beans.XPropertySet
74 * @see ifc.sheet._XSpreadsheetDocument
75 * @see ifc.sheet._SpreadsheetDocumentSettings
76 * @see ifc.lang._XComponent
77 * @see ifc.frame._XModel
78 * @see ifc.sheet._SpreadsheetDocument
79 * @see ifc.util._XNumberFormatsSupplier
80 * @see ifc.beans._XPropertySet
81 */
82 public class ScModelObj extends TestCase {
83     public static XSpreadsheetDocument xSpreadsheetDoc;
84     public static XSpreadsheetDocument xSecondsheetDoc;
85 
86     /**
87     * Disposes Spreadsheet documents.
88     */
89     protected void cleanup(TestParameters tParam, PrintWriter log) {
90         log.println("    disposing xSheetDoc ");
91 
92         XComponent oComp = (XComponent) UnoRuntime.queryInterface(
93                                    XComponent.class, xSpreadsheetDoc);
94         util.DesktopTools.closeDoc(oComp);
95 
96         oComp = (XComponent) UnoRuntime.queryInterface(XComponent.class,
97                                                        xSecondsheetDoc);
98         util.DesktopTools.closeDoc(oComp);
99     }
100 
101     /**
102     * Creating a Testenvironment for the interfaces to be tested.
103     * Disposes the spreadsheet documents if they was created already.
104     * Creates two spreadsheet documents. Rertieves the current controller for
105     * each of them. Obtains the collection of spreadsheets, takes one of them
106     * and takes some cell from the spreadsheet. The created documents are the instances
107     * of the service <code>com.sun.star.sheet.SpreadsheetDocument</code>.
108     * Object relations created :
109     * <ul>
110     *  <li> <code>'SELSUPP'</code> for
111     *      {@link ifc.frame._XModel}( the interface
112     *      <code>XSelectionSupplier</code> that was queried from the current
113     *      controller of the spreadsheet)</li>
114     *  <li> <code>'TOSELECT'</code> for
115     *      {@link ifc.frame._XModel}( the cell that was retrieved from the
116     *      spreadsheet)</li>
117     *  <li> <code>'CONT2'</code> for
118     *      {@link ifc.frame._XModel}( the current controller of the second
119     *      spreadsheet)</li>
120     * </ul>
121     * @see
122     */
123     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
124                                                                  PrintWriter log) {
125         // creation of the testobject here
126         // first we write what we are intend to do to log file
127         log.println("craeting a test environment");
128 
129         // get a soffice factory object
130         SOfficeFactory SOF = SOfficeFactory.getFactory(
131                                      (XMultiServiceFactory) Param.getMSF());
132 
133         if (xSpreadsheetDoc != null) {
134             XComponent oComp = (XComponent) UnoRuntime.queryInterface(
135                                        XComponent.class, xSpreadsheetDoc);
136             util.DesktopTools.closeDoc(oComp);
137         }
138 
139         if (xSecondsheetDoc != null) {
140             XComponent oComp = (XComponent) UnoRuntime.queryInterface(
141                                        XComponent.class, xSecondsheetDoc);
142             util.DesktopTools.closeDoc(oComp);
143         }
144 
145         try {
146             log.println("creating two spreadsheet documents");
147             xSpreadsheetDoc = SOF.createCalcDoc(null);
148             xSecondsheetDoc = SOF.createCalcDoc(null);
149         } catch (com.sun.star.uno.Exception e) {
150             e.printStackTrace(log);
151             throw new StatusException("Couldn't create document ", e);
152         }
153 
154         XModel model1 = (XModel) UnoRuntime.queryInterface(XModel.class,
155                                                            xSpreadsheetDoc);
156         XModel model2 = (XModel) UnoRuntime.queryInterface(XModel.class,
157                                                            xSecondsheetDoc);
158         XInterface oObj = model1;
159 
160         TestEnvironment tEnv = new TestEnvironment(oObj);
161 
162         XController cont1 = model1.getCurrentController();
163         XController cont2 = model2.getCurrentController();
164         cont1.getFrame().setName("cont1");
165         cont2.getFrame().setName("cont2");
166 
167         XSelectionSupplier sel = (XSelectionSupplier) UnoRuntime.queryInterface(
168                                          XSelectionSupplier.class, cont1);
169 
170         XCell toSel = null;
171         XCell[] xCalculatableCells = null;
172         try {
173             log.println("Getting spreadsheet");
174 
175             XSpreadsheets oSheets = xSpreadsheetDoc.getSheets();
176             XIndexAccess oIndexSheets = (XIndexAccess) UnoRuntime.queryInterface(
177                                                 XIndexAccess.class, oSheets);
178             XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
179                                           new Type(XSpreadsheet.class),
180                                           oIndexSheets.getByIndex(0));
181 
182             log.println("Getting a cell from sheet");
183             toSel = oSheet.getCellByPosition(2, 3);
184             // create a simple formula for XCalculatable
185             oSheet.getCellByPosition(4, 5).setValue(15);
186             oSheet.getCellByPosition(5, 5).setValue(10);
187             oSheet.getCellByPosition(6, 5).setFormula("= E6 * F6");
188             xCalculatableCells = new XCell[]{
189                 oSheet.getCellByPosition(4, 5),
190                 oSheet.getCellByPosition(5, 5),
191                 oSheet.getCellByPosition(6, 5)
192             };
193         } catch (com.sun.star.lang.WrappedTargetException e) {
194             e.printStackTrace(log);
195             throw new StatusException(
196                     "Error getting cell object from spreadsheet document", e);
197         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
198             e.printStackTrace(log);
199             throw new StatusException(
200                     "Error getting cell object from spreadsheet document", e);
201         } catch (com.sun.star.lang.IllegalArgumentException e) {
202             e.printStackTrace(log);
203             throw new StatusException(
204                     "Error getting cell object from spreadsheet document", e);
205         }
206 
207         log.println("Adding SelectionSupplier and Shape to select for XModel");
208         tEnv.addObjRelation("SELSUPP", sel);
209         tEnv.addObjRelation("TOSELECT", toSel);
210 
211         log.println("Adding cells for XCalculatable");
212         tEnv.addObjRelation("XCalculatable.Cells", xCalculatableCells);
213         log.println("adding Controller as ObjRelation for XModel");
214         tEnv.addObjRelation("CONT2", cont2);
215 
216         // create object relation for XPrintJobBroadcaster
217         String fileName = utils.getOfficeTempDirSys((XMultiServiceFactory) Param.getMSF())+"printfile.prt" ;
218         File f = new File(fileName);
219         if (f.exists()) {
220             f.delete();
221         }
222         _XPrintJobBroadcaster.MyPrintJobListener listener = new _XPrintJobBroadcaster.MyPrintJobListener(oObj, fileName);
223         tEnv.addObjRelation("XPrintJobBroadcaster.XPrintJobListener", listener);
224 
225         return tEnv;
226     }
227 
228 } // finish class ScModelObj
229