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