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