1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._sc;
29 
30 import java.io.PrintWriter;
31 
32 import lib.Status;
33 import lib.StatusException;
34 import lib.TestCase;
35 import lib.TestEnvironment;
36 import lib.TestParameters;
37 import util.AccessibilityTools;
38 import util.SOfficeFactory;
39 import util.utils;
40 
41 import com.sun.star.accessibility.AccessibleRole;
42 import com.sun.star.accessibility.XAccessible;
43 import com.sun.star.accessibility.XAccessibleAction;
44 import com.sun.star.accessibility.XAccessibleContext;
45 import com.sun.star.awt.XWindow;
46 import com.sun.star.container.XIndexAccess;
47 import com.sun.star.frame.XController;
48 import com.sun.star.frame.XDispatch;
49 import com.sun.star.frame.XDispatchProvider;
50 import com.sun.star.frame.XModel;
51 import com.sun.star.lang.XComponent;
52 import com.sun.star.lang.XMultiServiceFactory;
53 import com.sun.star.sheet.XSpreadsheet;
54 import com.sun.star.sheet.XSpreadsheetDocument;
55 import com.sun.star.sheet.XSpreadsheets;
56 import com.sun.star.table.XCell;
57 import com.sun.star.uno.AnyConverter;
58 import com.sun.star.uno.Type;
59 import com.sun.star.uno.UnoRuntime;
60 import com.sun.star.uno.XInterface;
61 import com.sun.star.util.URL;
62 import com.sun.star.util.XURLTransformer;
63 import util.PropertyName;
64 
65 /**
66  * Object implements the following interfaces:
67  * <ul>
68  *  <li> <code>::com::sun::star::accessibility::XAccessibleComponent</code>
69  *  </li>
70  *  <li> <code>::com::sun::star::accessibility::XAccessibleContext</code>
71  *  </li>
72  *  <li> <code>::com::sun::star::accessibility::XAccessibleSelection
73  *  </code></li>
74  *  <li><code>::com::sun::star::accessibility::XAccessibleTable</code>
75  *  </li>
76  *  <li><code>::com::sun::star::accessibility::XAccessibleEventBroadcaster
77  *  </code></li>
78  * </ul>
79  * @see com.sun.star.accessibility.XAccessibleComponent
80  * @see com.sun.star.accessibility.XAccessibleContext
81  * @see com.sun.star.accessibility.XAccessibleSelection
82  * @see com.sun.star.accessibility.XAccessibleTable
83  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
84  * @see ifc.accessibility._XAccessibleEventBroadcaster
85  * @see ifc.accessibility._XAccessibleComponent
86  * @see ifc.accessibility._XAccessibleContext
87  * @see ifc.accessibility._XAccessibleSelection
88  * @see ifc.accessibility._XAccessibleTable
89  */
90 public class ScAccessiblePreviewTable extends TestCase {
91     static XSpreadsheetDocument xSheetDoc = null;
92 
93     /**
94      * Creates a spreadsheet document.
95      */
96     protected void initialize( TestParameters tParam, PrintWriter log ) {
97         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
98         try {
99             log.println( "creating a Spreadsheet document" );
100             xSheetDoc = SOF.createCalcDoc(null);
101         } catch ( com.sun.star.uno.Exception e ) {
102             // Some exception occures.FAILED
103             e.printStackTrace( log );
104             throw new StatusException( "Couldn't create document", e );
105         }
106     }
107 
108     /**
109      * Disposes a spreadsheet document.
110      */
111     protected void cleanup( TestParameters tParam, PrintWriter log ) {
112         log.println( "    disposing xSheetDoc " );
113         XComponent oComp = (XComponent)UnoRuntime.queryInterface
114             (XComponent.class, xSheetDoc);
115         util.DesktopTools.closeDoc(oComp);
116     }
117 
118 
119     /**
120      * Creating a Testenvironment for the interfaces to be tested.
121      * Obtains the accessible object for a table in preview mode.
122      */
123     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
124 
125         XInterface oObj = null;
126         XCell xCell = null;
127 
128         try {
129             log.println("Getting spreadsheet") ;
130             XSpreadsheets oSheets = xSheetDoc.getSheets() ;
131             XIndexAccess oIndexSheets = (XIndexAccess)
132             UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
133             XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
134                     new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
135 
136             log.println("Getting a cell from sheet") ;
137             xCell = oSheet.getCellByPosition(0, 0);
138         } catch (com.sun.star.lang.WrappedTargetException e) {
139             e.printStackTrace(log);
140             throw new StatusException(
141                 "Error getting cell object from spreadsheet document", e);
142         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
143             e.printStackTrace(log);
144             throw new StatusException(
145                 "Error getting cell object from spreadsheet document", e);
146         } catch (com.sun.star.lang.IllegalArgumentException e) {
147             e.printStackTrace(log);
148             throw new StatusException(
149                 "Error getting cell object from spreadsheet document", e);
150         }
151 
152         xCell.setFormula("Value");
153 
154         XModel xModel = (XModel)
155             UnoRuntime.queryInterface(XModel.class, xSheetDoc);
156 
157         XController xController = xModel.getCurrentController();
158 
159         //switch to 'Print Preview' mode
160         try {
161             XDispatchProvider xDispProv = (XDispatchProvider)
162                 UnoRuntime.queryInterface(XDispatchProvider.class, xController);
163             XURLTransformer xParser = (com.sun.star.util.XURLTransformer)
164                 UnoRuntime.queryInterface(XURLTransformer.class,
165             ((XMultiServiceFactory)Param.getMSF()).createInstance("com.sun.star.util.URLTransformer"));
166             URL[] aParseURL = new URL[1];
167             aParseURL[0] = new URL();
168             aParseURL[0].Complete = ".uno:PrintPreview";
169             xParser.parseStrict(aParseURL);
170             URL aURL = aParseURL[0];
171             XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0);
172             if(xDispatcher != null)
173                 xDispatcher.dispatch( aURL, null );
174         } catch (com.sun.star.uno.Exception e) {
175             log.println("Couldn't change mode");
176             throw new StatusException(Status.failed("Couldn't change mode"));
177         }
178 
179         shortWait();
180 
181         AccessibilityTools at = new AccessibilityTools();
182 
183         XWindow xWindow = at.getCurrentContainerWindow((XMultiServiceFactory)Param.getMSF(), xModel);
184         XAccessible xRoot = at.getAccessibleObject(xWindow);
185 
186         at.printAccessibleTree(log,xRoot, Param.getBool(PropertyName.DEBUG_IS_ACTIVE));
187 
188         at.getAccessibleObjectForRole(xRoot, AccessibleRole.TABLE);
189 
190         oObj = AccessibilityTools.SearchedContext;
191 
192         log.println("ImplementationName " + utils.getImplName(oObj));
193 
194         TestEnvironment tEnv = new TestEnvironment( oObj );
195 
196         XAccessibleContext zoomIn =
197             at.getAccessibleObjectForRole(xRoot,AccessibleRole.PUSH_BUTTON, "Zoom In");
198 
199         log.println("Getting "+ zoomIn.getAccessibleName());
200 
201         final XAccessibleAction pressZoom = (XAccessibleAction)
202                     UnoRuntime.queryInterface(XAccessibleAction.class, zoomIn);
203         tEnv.addObjRelation("EventProducer",
204             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
205                 public void fireEvent() {
206                         try {
207                             pressZoom.doAccessibleAction(0);
208                         } catch (com.sun.star.lang.IndexOutOfBoundsException ibe) {}
209                 }
210             });
211 
212         return tEnv;
213     }
214 
215     protected void shortWait() {
216         try {
217             Thread.sleep(1000) ;
218         } catch (InterruptedException e) {
219             System.out.println("While waiting :" + e);
220         }
221     }
222 }
223