1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._sc;
29 
30 import com.sun.star.container.NoSuchElementException;
31 import com.sun.star.container.XNameContainer;
32 import com.sun.star.drawing.XDrawPage;
33 import com.sun.star.drawing.XDrawPages;
34 import com.sun.star.drawing.XDrawPagesSupplier;
35 import com.sun.star.drawing.XShape;
36 import com.sun.star.form.XForm;
37 import com.sun.star.lang.WrappedTargetException;
38 import java.io.PrintWriter;
39 import java.util.Comparator;
40 
41 import lib.StatusException;
42 import lib.TestCase;
43 import lib.TestEnvironment;
44 import lib.TestParameters;
45 import util.FormTools;
46 import util.SOfficeFactory;
47 import util.ValueComparer;
48 
49 import com.sun.star.container.XIndexAccess;
50 import com.sun.star.frame.XModel;
51 import com.sun.star.lang.XComponent;
52 import com.sun.star.lang.XMultiServiceFactory;
53 import com.sun.star.sheet.XCellRangeAddressable;
54 import com.sun.star.sheet.XSpreadsheet;
55 import com.sun.star.sheet.XSpreadsheetDocument;
56 import com.sun.star.sheet.XSpreadsheets;
57 import com.sun.star.table.CellRangeAddress;
58 import com.sun.star.table.XCell;
59 import com.sun.star.uno.AnyConverter;
60 import com.sun.star.uno.Type;
61 import com.sun.star.uno.UnoRuntime;
62 import com.sun.star.uno.XInterface;
63 
64 /**
65  * Test for object which is represented by service
66  * <code>com.sun.star.sheet.SpreadsheetView</code>. <p>
67  * Object implements the following interfaces :
68  * <ul>
69  *  <li> <code>com::sun::star::sheet::XViewSplitable</code></li>
70  *  <li> <code>com::sun::star::sheet::XViewFreezable</code></li>
71  *  <li> <code>com::sun::star::sheet::SpreadsheetViewSettings</code></li>
72  *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
73  *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
74  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
75  *  <li> <code>com::sun::star::sheet::XSpreadsheetView</code></li>
76  *  <li> <code>com::sun::star::view::XSelectionSupplier</code></li>
77  * </ul>
78  * @see com.sun.star.sheet.XViewSplitable
79  * @see com.sun.star.sheet.XViewFreezable
80  * @see com.sun.star.sheet.SpreadsheetViewSettings
81  * @see com.sun.star.beans.XPropertySet
82  * @see com.sun.star.container.XIndexAccess
83  * @see com.sun.star.container.XElementAccess
84  * @see com.sun.star.sheet.XSpreadsheetView
85  * @see com.sun.star.view.XSelectionSupplier
86  * @see ifc.sheet._XViewSplitable
87  * @see ifc.sheet._XViewFreezable
88  * @see ifc.sheet._SpreadsheetViewSettings
89  * @see ifc.beans._XPropertySet
90  * @see ifc.container._XIndexAccess
91  * @see ifc.container._XElementAccess
92  * @see ifc.sheet._XSpreadsheetView
93  * @see ifc.view._XSelectionSupplier
94  */
95 public class ScTabViewObj extends TestCase {
96     public static XSpreadsheetDocument xSpreadsheetDoc;
97     public static XSpreadsheetDocument xSpreadsheetDoc2;
98 
99     /**
100      * Creates Spreadsheet document.
101      */
102     public void initialize( TestParameters Param, PrintWriter log ) {
103         // get a soffice factory object
104 
105         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
106 
107         try {
108             log.println("creating two spreadsheet documents");
109             xSpreadsheetDoc = SOF.createCalcDoc(null);
110             try {
111                 Thread.sleep(1000);
112             } catch (java.lang.InterruptedException e) {}
113             xSpreadsheetDoc2 = SOF.createCalcDoc(null);
114         } catch (com.sun.star.uno.Exception e) {
115             e.printStackTrace( log );
116             throw new StatusException( "Couldn't create document ", e );
117         }
118     }
119 
120     /**
121      * Disposes Spreadsheet document.
122      */
123     protected void cleanup( TestParameters tParam, PrintWriter log ) {
124         log.println( "    disposing xSheetDoc " );
125         XComponent oComp = (XComponent)
126         UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc) ;
127         util.DesktopTools.closeDoc(oComp);
128         XComponent oComp2 = (XComponent)
129         UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc2) ;
130         util.DesktopTools.closeDoc(oComp2);
131     }
132 
133     /**
134      * Creating a Testenvironment for the interfaces to be tested.
135      * Retieves the current controller of the spreadsheet document using the
136      * interface <code>XModel</code>. The controller is the instance of the
137      * service <code>com.sun.star.sheet.SpreadsheetView</code>. Retrieves
138      * a collection of spreadsheets from the document and takes one of  them.
139      * Fills two cells in the spreadsheet.
140      * Object relations created :
141      * <ul>
142      *  <li> <code>'Sheet'</code> for
143      *      {@link ifc.sheet._XSpreadsheetView}(the retrieved spreadsheet)</li>
144      * </ul>
145      * @see com.sun.star.frame.XModel
146      * @see com.sun.star.sheet.SpreadsheetView
147      */
148     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
149         XDrawPage oDrawPage = null;
150 
151         XModel aModel = (XModel)
152         UnoRuntime.queryInterface(XModel.class, xSpreadsheetDoc);
153 
154         XModel aSecondModel = (XModel)
155         UnoRuntime.queryInterface(XModel.class, xSpreadsheetDoc2);
156 
157         XInterface oObj = aModel.getCurrentController();
158 
159         log.println("getting sheets");
160         XSpreadsheets xSpreadsheets = (XSpreadsheets)xSpreadsheetDoc.getSheets();
161 
162         log.println("getting a sheet");
163         XSpreadsheet oSheet = null;
164         XIndexAccess oIndexAccess = (XIndexAccess)
165         UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
166         try {
167             oSheet = (XSpreadsheet) AnyConverter.toObject(
168                 new Type(XSpreadsheet.class), oIndexAccess.getByIndex(1));
169         } catch (com.sun.star.lang.WrappedTargetException e) {
170             e.printStackTrace(log);
171             throw new StatusException( "Couldn't get a spreadsheet", e);
172         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
173             e.printStackTrace(log);
174             throw new StatusException( "Couldn't get a spreadsheet", e);
175         } catch (com.sun.star.lang.IllegalArgumentException e) {
176             e.printStackTrace(log);
177             throw new StatusException( "Couldn't get a spreadsheet", e);
178         }
179 
180         TestEnvironment tEnv = new TestEnvironment(oObj);
181 
182         tEnv.addObjRelation("XDispatchProvider.URL", ".uno:Copy") ;
183 
184         log.println("adding 'Sheet' as ObjRelation");
185         tEnv.addObjRelation("Sheet", oSheet);
186         tEnv.addObjRelation("Frame",aModel.getCurrentController().getFrame());
187         tEnv.addObjRelation("SecondModel",aSecondModel);
188         tEnv.addObjRelation("FirstModel",aModel);
189 
190         //Relation for XControlAccess
191         tEnv.addObjRelation("DOCUMENT", UnoRuntime.queryInterface(XComponent.class,xSpreadsheetDoc));
192         tEnv.addObjRelation("XControlAccess.isSheet", Boolean.TRUE);
193         //Relations for XSelectionSupplier
194         XCell cell_1 = null;
195         XCell cell_2 = null;
196         Object cellRange = null;
197         try {
198             cellRange = (Object)oSheet.getCellRangeByPosition(0, 0, 3, 3);
199             cell_1 = oSheet.getCellByPosition(5,5);
200             cell_2 = oSheet.getCellByPosition(7,7);
201             cell_2.setValue(17.5);
202             cell_1.setValue(5.5);
203         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
204             e.printStackTrace(log);
205             throw new StatusException("Couldn't get some cell", e);
206         }
207 
208         Object[] selections = {oSheet, cellRange, cell_1, cell_2};
209         tEnv.addObjRelation("Selections", selections);
210 
211         tEnv.addObjRelation("Comparer", new Comparator() {
212             public int compare(Object o1, Object o2) {
213                 XCellRangeAddressable adr1 = (XCellRangeAddressable)
214                 UnoRuntime.queryInterface(XCellRangeAddressable.class, o1);
215                 XCellRangeAddressable adr2 = (XCellRangeAddressable)
216                 UnoRuntime.queryInterface(XCellRangeAddressable.class, o2);
217                 if (adr1 == null || adr2 == null) return -1;
218                 CellRangeAddress range1 = adr1.getRangeAddress();
219                 CellRangeAddress range2 = adr2.getRangeAddress();
220                 return ValueComparer.equalValue(range1, range2) ? 0 : 1 ;
221             }
222             public boolean equals(Object obj) {
223                 return compare(this, obj) == 0;
224             } });
225 
226             tEnv.addObjRelation("XUserInputInterception.XModel", aModel);
227 
228             // XForm for com.sun.star.view.XFormLayerAccess
229 
230             XForm myForm = null;
231             String kindOfControl="CommandButton";
232             XShape aShape = null;
233             try{
234                 log.println("adding contol shape '" + kindOfControl + "'");
235                 XComponent oComp = (XComponent) UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc) ;
236 
237                 aShape = FormTools.createControlShape(oComp, 3000, 4500, 15000, 10000, kindOfControl);
238 
239             } catch (Exception e){
240                 e.printStackTrace(log);
241                 throw new StatusException("Couldn't create following control shape : '" +
242                     kindOfControl + "': ", e);
243             }
244 
245             log.println("adding relation for com.sun.star.view.XFormLayerAccess: XForm");
246             try {
247                 log.println( "getting Drawpages" );
248                 XDrawPagesSupplier oDPS = (XDrawPagesSupplier)
249                 UnoRuntime.queryInterface(XDrawPagesSupplier.class,xSpreadsheetDoc);
250                 XDrawPages oDP = (XDrawPages) oDPS.getDrawPages();
251                 oDP.insertNewByIndex(1);
252                 oDP.insertNewByIndex(2);
253                 oDrawPage = (XDrawPage) AnyConverter.toObject(
254                     new Type(XDrawPage.class),oDP.getByIndex(0));
255                 if (oDrawPage == null)
256                     log.println("ERROR: could not get DrawPage: null");
257 
258                 oDrawPage.add(aShape);
259                 log.println("getting XForm");
260                 XNameContainer xForm = FormTools.getForms(oDrawPage);
261                 try {
262                     myForm = (XForm) AnyConverter.toObject(new Type(XForm.class),xForm.getByName("Standard"));
263                 } catch (WrappedTargetException ex) {
264                     log.println("ERROR: could not XFormm 'Standard': " + ex.toString());
265                 } catch (com.sun.star.lang.IllegalArgumentException ex) {
266                     log.println("ERROR: could not XFormm 'Standard': " + ex.toString());
267                 } catch (NoSuchElementException ex) {
268                     log.println("ERROR: could not XFormm 'Standard': " + ex.toString());
269                 }
270                 if (myForm == null)
271                     log.println("ERROR: could not get XForm: null");
272                 tEnv.addObjRelation("XFormLayerAccess.XForm", myForm);
273             } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
274                 log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString());
275             } catch (WrappedTargetException ex) {
276                 log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString());
277             } catch (com.sun.star.lang.IllegalArgumentException ex) {
278                 log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString());
279             }
280             return tEnv;
281     }
282 
283 }    // finish class ScTabViewObj
284