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