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