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