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