1 /*
2  * This file is part of the LibreOffice project.
3  *
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/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
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 .
17  */
18 package mod._sc;
19 
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;
35 
36 import java.io.PrintWriter;
37 
38 import lib.TestCase;
39 import lib.TestEnvironment;
40 import lib.TestParameters;
41 
42 import util.SOfficeFactory;
43 import util.utils;
44 
45 
46 /**
47 * Test for object which is represented by service
48 * <code>com.sun.star.sheet.SpreadsheetDocument</code>. <p>
49 * Object implements the following interfaces :
50 * <ul>
51 *  <li> <code>com::sun::star::sheet::XSpreadsheetDocument</code></li>
52 *  <li> <code>com::sun::star::sheet::SpreadsheetDocumentSettings</code></li>
53 *  <li> <code>com::sun::star::lang::XComponent</code></li>
54 *  <li> <code>com::sun::star::frame::XModel</code></li>
55 *  <li> <code>com::sun::star::sheet::SpreadsheetDocument</code></li>
56 *  <li> <code>com::sun::star::util::XNumberFormatsSupplier</code></li>
57 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
58 * </ul>
59 * @see com.sun.star.sheet.SpreadsheetDocument
60 * @see com.sun.star.sheet.XSpreadsheetDocument
61 * @see com.sun.star.sheet.SpreadsheetDocumentSettings
62 * @see com.sun.star.lang.XComponent
63 * @see com.sun.star.frame.XModel
64 * @see com.sun.star.sheet.SpreadsheetDocument
65 * @see com.sun.star.util.XNumberFormatsSupplier
66 * @see com.sun.star.beans.XPropertySet
67 * @see ifc.sheet._XSpreadsheetDocument
68 * @see ifc.sheet._SpreadsheetDocumentSettings
69 * @see ifc.lang._XComponent
70 * @see ifc.frame._XModel
71 * @see ifc.sheet._SpreadsheetDocument
72 * @see ifc.util._XNumberFormatsSupplier
73 * @see ifc.beans._XPropertySet
74 */
75 public class ScModelObj extends TestCase {
76     private XSpreadsheetDocument xSpreadsheetDoc;
77     private XSpreadsheetDocument xSecondsheetDoc;
78 
79     /**
80     * Disposes Spreadsheet documents.
81     */
82     @Override
cleanup(TestParameters tParam, PrintWriter log)83     protected void cleanup(TestParameters tParam, PrintWriter log) {
84         log.println("    disposing xSheetDoc ");
85 
86         XComponent oComp = UnoRuntime.queryInterface(
87                                    XComponent.class, xSpreadsheetDoc);
88         util.DesktopTools.closeDoc(oComp);
89 
90         oComp = UnoRuntime.queryInterface(XComponent.class,
91                                                        xSecondsheetDoc);
92         util.DesktopTools.closeDoc(oComp);
93     }
94 
95     /**
96     * Creating a TestEnvironment for the interfaces to be tested.
97     * Disposes the spreadsheet documents if they were created already.
98     * Creates two spreadsheet documents. Retrieves the current controller for
99     * each of them. Obtains the collection of spreadsheets, takes one of them
100     * and takes some cell from the spreadsheet. The created documents are the instances
101     * of the service <code>com.sun.star.sheet.SpreadsheetDocument</code>.
102     * Object relations created :
103     * <ul>
104     *  <li> <code>'SELSUPP'</code> for
105     *      {@link ifc.frame._XModel}( the interface
106     *      <code>XSelectionSupplier</code> that was queried from the current
107     *      controller of the spreadsheet)</li>
108     *  <li> <code>'TOSELECT'</code> for
109     *      {@link ifc.frame._XModel}( the cell that was retrieved from the
110     *      spreadsheet)</li>
111     *  <li> <code>'CONT2'</code> for
112     *      {@link ifc.frame._XModel}( the current controller of the second
113     *      spreadsheet)</li>
114     * </ul>
115     */
116     @Override
createTestEnvironment(TestParameters Param, PrintWriter log)117     protected TestEnvironment createTestEnvironment(TestParameters Param,
118                                                                  PrintWriter log) throws Exception {
119         // creation of the testobject here
120         // first we write what we are intend to do to log file
121         log.println("creating a test environment");
122 
123         // get a soffice factory object
124         SOfficeFactory SOF = SOfficeFactory.getFactory(
125                                      Param.getMSF());
126 
127         if (xSpreadsheetDoc != null) {
128             XComponent oComp = UnoRuntime.queryInterface(
129                                        XComponent.class, xSpreadsheetDoc);
130             util.DesktopTools.closeDoc(oComp);
131         }
132 
133         if (xSecondsheetDoc != null) {
134             XComponent oComp = UnoRuntime.queryInterface(
135                                        XComponent.class, xSecondsheetDoc);
136             util.DesktopTools.closeDoc(oComp);
137         }
138 
139         log.println("creating two spreadsheet documents");
140         xSpreadsheetDoc = SOF.createCalcDoc(null);
141         xSecondsheetDoc = SOF.createCalcDoc(null);
142 
143         XModel model1 = UnoRuntime.queryInterface(XModel.class,
144                                                            xSpreadsheetDoc);
145         XModel model2 = UnoRuntime.queryInterface(XModel.class,
146                                                            xSecondsheetDoc);
147         XInterface oObj = model1;
148 
149         TestEnvironment tEnv = new TestEnvironment(oObj);
150 
151         XController cont1 = model1.getCurrentController();
152         XController cont2 = model2.getCurrentController();
153         cont1.getFrame().setName("cont1");
154         cont2.getFrame().setName("cont2");
155 
156         XSelectionSupplier sel = UnoRuntime.queryInterface(
157                                          XSelectionSupplier.class, cont1);
158 
159         XCell toSel = null;
160         XCell[] xCalculatableCells = null;
161         log.println("Getting spreadsheet");
162 
163         XSpreadsheets oSheets = xSpreadsheetDoc.getSheets();
164         XIndexAccess oIndexSheets = UnoRuntime.queryInterface(
165                                             XIndexAccess.class, oSheets);
166         XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
167                                       new Type(XSpreadsheet.class),
168                                       oIndexSheets.getByIndex(0));
169 
170         log.println("Getting a cell from sheet");
171         toSel = oSheet.getCellByPosition(2, 3);
172         // create a simple formula for XCalculatable
173         oSheet.getCellByPosition(4, 5).setValue(15);
174         oSheet.getCellByPosition(5, 5).setValue(10);
175         oSheet.getCellByPosition(6, 5).setFormula("= E6 * F6");
176         xCalculatableCells = new XCell[]{
177             oSheet.getCellByPosition(4, 5),
178             oSheet.getCellByPosition(5, 5),
179             oSheet.getCellByPosition(6, 5)
180         };
181 
182         // Make sure there are at least two sheets:
183         oSheets.insertNewByName("Some Sheet", (short) 1);
184 
185         log.println("Adding SelectionSupplier and Shape to select for XModel");
186         tEnv.addObjRelation("SELSUPP", sel);
187         tEnv.addObjRelation("TOSELECT", toSel);
188 
189         log.println("Adding cells for XCalculatable");
190         tEnv.addObjRelation("XCalculatable.Cells", xCalculatableCells);
191         log.println("adding Controller as ObjRelation for XModel");
192         tEnv.addObjRelation("CONT2", cont2);
193 
194         // create object relation for XPrintJobBroadcaster
195         String fileName = utils.getOfficeTempDirSys(Param.getMSF())+"printfile.prt" ;
196         File f = new File(fileName);
197         if (f.exists()) {
198             boolean bDeleteOk = f.delete();
199             if (!bDeleteOk) {
200                 System.out.println("delete failed");
201             }
202         }
203         _XPrintJobBroadcaster.MyPrintJobListener listener = new _XPrintJobBroadcaster.MyPrintJobListener(oObj, fileName);
204         tEnv.addObjRelation("XPrintJobBroadcaster.XPrintJobListener", listener);
205 
206         return tEnv;
207     }
208 
209 } // finish class ScModelObj
210