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     */
cleanup(TestParameters tParam, PrintWriter log)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     */
createTestEnvironment(TestParameters Param, PrintWriter log)122     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
123                                                                  PrintWriter log) {
124         // creation of the testobject here
125         // first we write what we are intend to do to log file
126         log.println("craeting a test environment");
127 
128         // get a soffice factory object
129         SOfficeFactory SOF = SOfficeFactory.getFactory(
130                                      (XMultiServiceFactory) Param.getMSF());
131 
132         if (xSpreadsheetDoc != null) {
133             XComponent oComp = (XComponent) UnoRuntime.queryInterface(
134                                        XComponent.class, xSpreadsheetDoc);
135             util.DesktopTools.closeDoc(oComp);
136         }
137 
138         if (xSecondsheetDoc != null) {
139             XComponent oComp = (XComponent) UnoRuntime.queryInterface(
140                                        XComponent.class, xSecondsheetDoc);
141             util.DesktopTools.closeDoc(oComp);
142         }
143 
144         try {
145             log.println("creating two spreadsheet documents");
146             xSpreadsheetDoc = SOF.createCalcDoc(null);
147             xSecondsheetDoc = SOF.createCalcDoc(null);
148         } catch (com.sun.star.uno.Exception e) {
149             e.printStackTrace(log);
150             throw new StatusException("Couldn't create document ", e);
151         }
152 
153         XModel model1 = (XModel) UnoRuntime.queryInterface(XModel.class,
154                                                            xSpreadsheetDoc);
155         XModel model2 = (XModel) UnoRuntime.queryInterface(XModel.class,
156                                                            xSecondsheetDoc);
157         XInterface oObj = model1;
158 
159         TestEnvironment tEnv = new TestEnvironment(oObj);
160 
161         XController cont1 = model1.getCurrentController();
162         XController cont2 = model2.getCurrentController();
163         cont1.getFrame().setName("cont1");
164         cont2.getFrame().setName("cont2");
165 
166         XSelectionSupplier sel = (XSelectionSupplier) UnoRuntime.queryInterface(
167                                          XSelectionSupplier.class, cont1);
168 
169         XCell toSel = null;
170         XCell[] xCalculatableCells = null;
171         try {
172             log.println("Getting spreadsheet");
173 
174             XSpreadsheets oSheets = xSpreadsheetDoc.getSheets();
175             XIndexAccess oIndexSheets = (XIndexAccess) UnoRuntime.queryInterface(
176                                                 XIndexAccess.class, oSheets);
177             XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
178                                           new Type(XSpreadsheet.class),
179                                           oIndexSheets.getByIndex(0));
180 
181             log.println("Getting a cell from sheet");
182             toSel = oSheet.getCellByPosition(2, 3);
183             // create a simple formula for XCalculatable
184             oSheet.getCellByPosition(4, 5).setValue(15);
185             oSheet.getCellByPosition(5, 5).setValue(10);
186             oSheet.getCellByPosition(6, 5).setFormula("= E6 * F6");
187             xCalculatableCells = new XCell[]{
188                 oSheet.getCellByPosition(4, 5),
189                 oSheet.getCellByPosition(5, 5),
190                 oSheet.getCellByPosition(6, 5)
191             };
192         } catch (com.sun.star.lang.WrappedTargetException e) {
193             e.printStackTrace(log);
194             throw new StatusException(
195                     "Error getting cell object from spreadsheet document", e);
196         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
197             e.printStackTrace(log);
198             throw new StatusException(
199                     "Error getting cell object from spreadsheet document", e);
200         } catch (com.sun.star.lang.IllegalArgumentException e) {
201             e.printStackTrace(log);
202             throw new StatusException(
203                     "Error getting cell object from spreadsheet document", e);
204         }
205 
206         log.println("Adding SelectionSupplier and Shape to select for XModel");
207         tEnv.addObjRelation("SELSUPP", sel);
208         tEnv.addObjRelation("TOSELECT", toSel);
209 
210         log.println("Adding cells for XCalculatable");
211         tEnv.addObjRelation("XCalculatable.Cells", xCalculatableCells);
212         log.println("adding Controller as ObjRelation for XModel");
213         tEnv.addObjRelation("CONT2", cont2);
214 
215         // create object relation for XPrintJobBroadcaster
216         String fileName = utils.getOfficeTempDirSys((XMultiServiceFactory) Param.getMSF())+"printfile.prt" ;
217         File f = new File(fileName);
218         if (f.exists()) {
219             f.delete();
220         }
221         _XPrintJobBroadcaster.MyPrintJobListener listener = new _XPrintJobBroadcaster.MyPrintJobListener(oObj, fileName);
222         tEnv.addObjRelation("XPrintJobBroadcaster.XPrintJobListener", listener);
223 
224         return tEnv;
225     }
226 
227 } // finish class ScModelObj
228