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 java.io.PrintWriter;
27 
28 import lib.Status;
29 import lib.StatusException;
30 import lib.TestCase;
31 import lib.TestEnvironment;
32 import lib.TestParameters;
33 import util.AccessibilityTools;
34 import util.SOfficeFactory;
35 import util.utils;
36 
37 import com.sun.star.accessibility.AccessibleRole;
38 import com.sun.star.accessibility.XAccessible;
39 import com.sun.star.awt.XWindow;
40 import com.sun.star.container.XIndexAccess;
41 import com.sun.star.frame.XController;
42 import com.sun.star.frame.XDispatch;
43 import com.sun.star.frame.XDispatchProvider;
44 import com.sun.star.frame.XModel;
45 import com.sun.star.lang.XComponent;
46 import com.sun.star.lang.XMultiServiceFactory;
47 import com.sun.star.sheet.XSpreadsheet;
48 import com.sun.star.sheet.XSpreadsheetDocument;
49 import com.sun.star.sheet.XSpreadsheets;
50 import com.sun.star.table.XCell;
51 import com.sun.star.uno.AnyConverter;
52 import com.sun.star.uno.Type;
53 import com.sun.star.uno.UnoRuntime;
54 import com.sun.star.uno.XInterface;
55 import com.sun.star.util.URL;
56 import com.sun.star.util.XURLTransformer;
57 
58 /**
59  * Object implements the following interfaces:
60  * <ul>
61  *  <li> <code>::com::sun::star::accessibility::XAccessibleComponent</code>
62  *  </li>
63  *  <li> <code>::com::sun::star::accessibility::XAccessibleContext</code>
64  *  </li>
65  *  <li> <code>::com::sun::star::accessibility::XAccessibleSelection
66  *  </code></li>
67  *  <li><code>::com::sun::star::accessibility::XAccessibleValue</code>
68  *  </li>
69  *  <li><code>::com::sun::star::accessibility::XAccessibleEventBroadcaster
70  *  </code></li>
71  * </ul>
72  * @see com.sun.star.accessibility.XAccessibleComponent
73  * @see com.sun.star.accessibility.XAccessibleContext
74  * @see com.sun.star.accessibility.XAccessibleSelection
75  * @see com.sun.star.accessibility.XAccessibleValue
76  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
77  * @see ifc.accessibility._XAccessibleEventBroadcaster
78  * @see ifc.accessibility._XAccessibleComponent
79  * @see ifc.accessibility._XAccessibleContext
80  * @see ifc.accessibility._XAccessibleSelection
81  * @see ifc.accessibility._XAccessibleTable
82  */
83 public class ScAccessiblePreviewCell extends TestCase {
84     static XSpreadsheetDocument xSheetDoc = null;
85 
86     /**
87      * Creates a spreadsheet document.
88      */
initialize( TestParameters tParam, PrintWriter log )89     protected void initialize( TestParameters tParam, PrintWriter log ) {
90         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
91         try {
92             log.println( "creating a Spreadsheet document" );
93             xSheetDoc = SOF.createCalcDoc(null);
94         } catch ( com.sun.star.uno.Exception e ) {
95             // Some exception occures.FAILED
96             e.printStackTrace( log );
97             throw new StatusException( "Couldn't create document", e );
98         }
99     }
100 
101     /**
102      * Disposes a spreadsheet document.
103      */
cleanup( TestParameters tParam, PrintWriter log )104     protected void cleanup( TestParameters tParam, PrintWriter log ) {
105         log.println( "    disposing xSheetDoc " );
106         XComponent oComp = (XComponent)UnoRuntime.queryInterface
107             (XComponent.class, xSheetDoc);
108         util.DesktopTools.closeDoc(oComp);
109     }
110 
111 
112     /**
113      * Creating a Testenvironment for the interfaces to be tested.
114      * Obtains the accessible object for a one of cell in preview mode.
115      */
createTestEnvironment(TestParameters Param, PrintWriter log)116     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
117 
118         XInterface oObj = null;
119         XCell xCell = null;
120 
121         try {
122             log.println("Getting spreadsheet") ;
123             XSpreadsheets oSheets = xSheetDoc.getSheets() ;
124             XIndexAccess oIndexSheets = (XIndexAccess)
125             UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
126             XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
127                     new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
128 
129             log.println("Getting a cell from sheet") ;
130             xCell = oSheet.getCellByPosition(0, 0);
131         } catch (com.sun.star.lang.WrappedTargetException e) {
132             e.printStackTrace(log);
133             throw new StatusException(
134                 "Error getting cell object from spreadsheet document", e);
135         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
136             e.printStackTrace(log);
137             throw new StatusException(
138                 "Error getting cell object from spreadsheet document", e);
139         } catch (com.sun.star.lang.IllegalArgumentException e) {
140             e.printStackTrace(log);
141             throw new StatusException(
142                 "Error getting cell object from spreadsheet document", e);
143         }
144 
145         xCell.setFormula("Value");
146 
147         XModel xModel = (XModel)
148             UnoRuntime.queryInterface(XModel.class, xSheetDoc);
149 
150         XController xController = xModel.getCurrentController();
151 
152         //switch to 'Print Preview' mode
153         try {
154             XDispatchProvider xDispProv = (XDispatchProvider)
155                 UnoRuntime.queryInterface(XDispatchProvider.class, xController);
156             XURLTransformer xParser = (com.sun.star.util.XURLTransformer)
157                 UnoRuntime.queryInterface(XURLTransformer.class,
158             ((XMultiServiceFactory)Param.getMSF()).createInstance("com.sun.star.util.URLTransformer"));
159             URL[] aParseURL = new URL[1];
160             aParseURL[0] = new URL();
161             aParseURL[0].Complete = ".uno:PrintPreview";
162             xParser.parseStrict(aParseURL);
163             URL aURL = aParseURL[0];
164             XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0);
165             if(xDispatcher != null)
166                 xDispatcher.dispatch( aURL, null );
167         } catch (com.sun.star.uno.Exception e) {
168             log.println("Couldn't change mode");
169             throw new StatusException(Status.failed("Couldn't change mode"));
170         }
171 
172         shortWait();
173 
174         AccessibilityTools at = new AccessibilityTools();
175 
176         XWindow xWindow = at.getCurrentWindow((XMultiServiceFactory)Param.getMSF(), xModel);
177         XAccessible xRoot = at.getAccessibleObject(xWindow);
178 
179         at.getAccessibleObjectForRole(xRoot, AccessibleRole.TABLE_CELL, true);
180 
181         oObj = AccessibilityTools.SearchedContext;
182 
183         log.println("ImplementationName " + utils.getImplName(oObj));
184 
185         TestEnvironment tEnv = new TestEnvironment( oObj );
186 
187         tEnv.addObjRelation("EventProducer",
188             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer(){
189                 public void fireEvent() {
190                     System.out.println("Fire Event");
191                 }
192             });
193 
194         return tEnv;
195     }
196 
shortWait()197     protected void shortWait() {
198         try {
199             Thread.sleep(1000) ;
200         } catch (InterruptedException e) {
201             System.out.println("While waiting :" + e);
202         }
203     }
204 }
205