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