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 java.io.PrintWriter;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import lib.StatusException;
30cdf0e10cSrcweir import lib.TestCase;
31cdf0e10cSrcweir import lib.TestEnvironment;
32cdf0e10cSrcweir import lib.TestParameters;
33cdf0e10cSrcweir import util.SOfficeFactory;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
36cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
37cdf0e10cSrcweir import com.sun.star.drawing.XDrawPage;
38cdf0e10cSrcweir import com.sun.star.drawing.XDrawPages;
39cdf0e10cSrcweir import com.sun.star.drawing.XDrawPagesSupplier;
40cdf0e10cSrcweir import com.sun.star.drawing.XShape;
41cdf0e10cSrcweir import com.sun.star.form.XForm;
42cdf0e10cSrcweir import com.sun.star.frame.XController;
43cdf0e10cSrcweir import com.sun.star.frame.XModel;
44cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException;
45cdf0e10cSrcweir import com.sun.star.lang.XComponent;
46cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
47cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
48cdf0e10cSrcweir import com.sun.star.sheet.XViewPane;
49cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress;
50cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
51cdf0e10cSrcweir import com.sun.star.uno.Type;
52cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
53cdf0e10cSrcweir import com.sun.star.uno.XInterface;
54cdf0e10cSrcweir import util.FormTools;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir /**
57cdf0e10cSrcweir  * Test for object which is represented by service
58cdf0e10cSrcweir  * <code>com.sun.star.sheet.SpreadsheetViewPane</code>. <p>
59cdf0e10cSrcweir  * Object implements the following interfaces :
60cdf0e10cSrcweir  * <ul>
61cdf0e10cSrcweir  *  <li> <code>com::sun::star::sheet::XViewPane</code></li>
62cdf0e10cSrcweir  *  <li> <code>com::sun::star::sheet::XCellRangeReferrer</code></li>
63cdf0e10cSrcweir  * </ul>
64cdf0e10cSrcweir  * @see com.sun.star.sheet.SpreadsheetViewPane
65cdf0e10cSrcweir  * @see com.sun.star.sheet.XViewPane
66cdf0e10cSrcweir  * @see com.sun.star.sheet.XCellRangeReferrer
67cdf0e10cSrcweir  * @see ifc.sheet._XViewPane
68cdf0e10cSrcweir  * @see ifc.sheet._XCellRangeReferrer
69cdf0e10cSrcweir  */
70cdf0e10cSrcweir public class ScViewPaneObj extends TestCase {
71cdf0e10cSrcweir     static private XSpreadsheetDocument xSpreadsheetDoc;
72cdf0e10cSrcweir     static private SOfficeFactory SOF;
73cdf0e10cSrcweir     static private XInterface oObj;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     /**
76cdf0e10cSrcweir      * Creates Spreadsheet document.
77cdf0e10cSrcweir      */
initialize( TestParameters Param, PrintWriter log )78cdf0e10cSrcweir     public void initialize( TestParameters Param, PrintWriter log ) {
79cdf0e10cSrcweir         // get a soffice factory object
80cdf0e10cSrcweir         SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
81cdf0e10cSrcweir 
82cdf0e10cSrcweir         try {
83cdf0e10cSrcweir             log.println("creating a spreadsheetdocument");
84cdf0e10cSrcweir             xSpreadsheetDoc = SOF.createCalcDoc(null);
85cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
86cdf0e10cSrcweir             e.printStackTrace( log );
87cdf0e10cSrcweir             throw new StatusException( "Couldn't create document ", e );
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir     }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir     /**
92cdf0e10cSrcweir      * Disposes Spreadsheet document.
93cdf0e10cSrcweir      */
cleanup( TestParameters tParam, PrintWriter log )94cdf0e10cSrcweir     protected void cleanup( TestParameters tParam, PrintWriter log ) {
95cdf0e10cSrcweir         log.println("disposing xSpreadsheetDocument");
96cdf0e10cSrcweir         XComponent oComp = (XComponent)
97cdf0e10cSrcweir         UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc);
98cdf0e10cSrcweir         util.DesktopTools.closeDoc(oComp);
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     /**
102cdf0e10cSrcweir      * Creating a Testenvironment for the interfaces to be tested.
103cdf0e10cSrcweir      * Retieves the current controller of the spreadsheet document using the
104cdf0e10cSrcweir      * interface <code>XModel</code>. The controller contains the collection
105cdf0e10cSrcweir      * of the view panes so retrieves the view pane with index 0 from
106cdf0e10cSrcweir      * the collection. The retrived view pane is the instance of the service
107cdf0e10cSrcweir      * <code>com.sun.star.sheet.SpreadsheetViewPane</code>. Retrieves the address
108cdf0e10cSrcweir      * of the cell range that consists of the cells which are visible in the pane.
109cdf0e10cSrcweir      * Object relations created :
110cdf0e10cSrcweir      * <ul>
111cdf0e10cSrcweir      *  <li> <code>'DATAAREA'</code> for
112cdf0e10cSrcweir      *      {@link ifc.sheet._XViewPane}(the retrieved address)</li>
113cdf0e10cSrcweir      * </ul>
114cdf0e10cSrcweir      * @see com.sun.star.frame.XModel
115cdf0e10cSrcweir      */
createTestEnvironment(TestParameters Param, PrintWriter log)116cdf0e10cSrcweir     protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
117cdf0e10cSrcweir         XDrawPage oDrawPage;
118cdf0e10cSrcweir 
119cdf0e10cSrcweir         XModel xm = (XModel)
120cdf0e10cSrcweir         UnoRuntime.queryInterface(XModel.class, xSpreadsheetDoc);
121cdf0e10cSrcweir         XController xc = xm.getCurrentController();
122cdf0e10cSrcweir         XIndexAccess xIA = (XIndexAccess)
123cdf0e10cSrcweir         UnoRuntime.queryInterface(XIndexAccess.class, xc);
124cdf0e10cSrcweir         try {
125cdf0e10cSrcweir             oObj = (XInterface) AnyConverter.toObject(
126cdf0e10cSrcweir                 new Type(XInterface.class),xIA.getByIndex(0));
127cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
128cdf0e10cSrcweir             e.printStackTrace(log);
129cdf0e10cSrcweir             throw new StatusException("Couldn't get by index", e);
130cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
131cdf0e10cSrcweir             e.printStackTrace(log);
132cdf0e10cSrcweir             throw new StatusException("Couldn't get by index", e);
133cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
134cdf0e10cSrcweir             e.printStackTrace(log);
135cdf0e10cSrcweir             throw new StatusException("Couldn't get by index", e);
136cdf0e10cSrcweir         }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment(oObj);
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         //Relation for XControlAccess
141cdf0e10cSrcweir         tEnv.addObjRelation("DOCUMENT", UnoRuntime.queryInterface(XComponent.class,xSpreadsheetDoc));
142cdf0e10cSrcweir         tEnv.addObjRelation("XControlAccess.isSheet", Boolean.TRUE);
143cdf0e10cSrcweir 
144cdf0e10cSrcweir         XViewPane VP = (XViewPane)
145cdf0e10cSrcweir         UnoRuntime.queryInterface(XViewPane.class, oObj);
146cdf0e10cSrcweir         CellRangeAddress dataArea = VP.getVisibleRange();
147cdf0e10cSrcweir         tEnv.addObjRelation("DATAAREA", dataArea);
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         // XForm for com.sun.star.view.XFormLayerAccess
150cdf0e10cSrcweir         log.println("adding relation for com.sun.star.view.XFormLayerAccess: XForm");
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         XForm myForm = null;
153cdf0e10cSrcweir         String kindOfControl="CommandButton";
154cdf0e10cSrcweir         XShape aShape = null;
155cdf0e10cSrcweir         try{
156cdf0e10cSrcweir             log.println("adding contol shape '" + kindOfControl + "'");
157cdf0e10cSrcweir             XComponent oComp = (XComponent) UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc) ;
158cdf0e10cSrcweir 
159cdf0e10cSrcweir             aShape = FormTools.createControlShape(oComp, 3000, 4500, 15000, 10000, kindOfControl);
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         } catch (Exception e){
162cdf0e10cSrcweir             e.printStackTrace(log);
163cdf0e10cSrcweir             throw new StatusException("Couldn't create following control shape : '" +
164cdf0e10cSrcweir                 kindOfControl + "': ", e);
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir         try {
168cdf0e10cSrcweir             log.println( "getting Drawpages" );
169cdf0e10cSrcweir             XDrawPagesSupplier oDPS = (XDrawPagesSupplier)
170cdf0e10cSrcweir             UnoRuntime.queryInterface(XDrawPagesSupplier.class,xSpreadsheetDoc);
171cdf0e10cSrcweir             XDrawPages oDP = (XDrawPages) oDPS.getDrawPages();
172cdf0e10cSrcweir             oDP.insertNewByIndex(1);
173cdf0e10cSrcweir             oDP.insertNewByIndex(2);
174cdf0e10cSrcweir             oDrawPage = (XDrawPage) AnyConverter.toObject(
175cdf0e10cSrcweir                 new Type(XDrawPage.class),oDP.getByIndex(0));
176cdf0e10cSrcweir             if (oDrawPage == null)
177cdf0e10cSrcweir                 log.println("ERROR: could not get DrawPage: null");
178cdf0e10cSrcweir 
179cdf0e10cSrcweir             oDrawPage.add(aShape);
180cdf0e10cSrcweir             log.println("getting XForm");
181cdf0e10cSrcweir             XNameContainer xForm = FormTools.getForms(oDrawPage);
182cdf0e10cSrcweir             try {
183cdf0e10cSrcweir                 myForm = (XForm) AnyConverter.toObject(new Type(XForm.class),xForm.getByName("Standard"));
184cdf0e10cSrcweir             } catch (WrappedTargetException ex) {
185cdf0e10cSrcweir                 log.println("ERROR: could not XFormm 'Standard': " + ex.toString());
186cdf0e10cSrcweir             } catch (com.sun.star.lang.IllegalArgumentException ex) {
187cdf0e10cSrcweir                 log.println("ERROR: could not XFormm 'Standard': " + ex.toString());
188cdf0e10cSrcweir             } catch (NoSuchElementException ex) {
189cdf0e10cSrcweir                 log.println("ERROR: could not XFormm 'Standard': " + ex.toString());
190cdf0e10cSrcweir             }
191cdf0e10cSrcweir             if (myForm == null)
192cdf0e10cSrcweir                 log.println("ERROR: could not get XForm: null");
193cdf0e10cSrcweir             tEnv.addObjRelation("XFormLayerAccess.XForm", myForm);
194cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException ex) {
195cdf0e10cSrcweir             log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString());
196cdf0e10cSrcweir         } catch (WrappedTargetException ex) {
197cdf0e10cSrcweir             log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString());
198cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException ex) {
199cdf0e10cSrcweir             log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString());
200cdf0e10cSrcweir         }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir         return tEnv;
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir }
205cdf0e10cSrcweir 
206