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