xref: /AOO41X/main/qadevOOo/tests/java/mod/_sc/ScAccessiblePreviewHeaderCell.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir package mod._sc;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir import java.io.PrintWriter;
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir import lib.Status;
32*cdf0e10cSrcweir import lib.StatusException;
33*cdf0e10cSrcweir import lib.TestCase;
34*cdf0e10cSrcweir import lib.TestEnvironment;
35*cdf0e10cSrcweir import lib.TestParameters;
36*cdf0e10cSrcweir import util.AccessibilityTools;
37*cdf0e10cSrcweir import util.SOfficeFactory;
38*cdf0e10cSrcweir import util.utils;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleRole;
41*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible;
42*cdf0e10cSrcweir import com.sun.star.awt.XWindow;
43*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
44*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
45*cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
46*cdf0e10cSrcweir import com.sun.star.frame.XController;
47*cdf0e10cSrcweir import com.sun.star.frame.XDispatch;
48*cdf0e10cSrcweir import com.sun.star.frame.XDispatchProvider;
49*cdf0e10cSrcweir import com.sun.star.frame.XModel;
50*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
51*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
52*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet;
53*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
54*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets;
55*cdf0e10cSrcweir import com.sun.star.style.XStyleFamiliesSupplier;
56*cdf0e10cSrcweir import com.sun.star.table.XCell;
57*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
58*cdf0e10cSrcweir import com.sun.star.uno.Type;
59*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
60*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
61*cdf0e10cSrcweir import com.sun.star.util.URL;
62*cdf0e10cSrcweir import com.sun.star.util.XCloseable;
63*cdf0e10cSrcweir import com.sun.star.util.XURLTransformer;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir /**
67*cdf0e10cSrcweir  * Object implements the following interfaces:
68*cdf0e10cSrcweir  * <ul>
69*cdf0e10cSrcweir  *  <li> <code>::com::sun::star::accessibility::XAccessibleComponent</code>
70*cdf0e10cSrcweir  *  </li>
71*cdf0e10cSrcweir  *  <li> <code>::com::sun::star::accessibility::XAccessibleContext</code>
72*cdf0e10cSrcweir  *  </li>
73*cdf0e10cSrcweir  *  <li> <code>::com::sun::star::accessibility::XAccessibleSelection
74*cdf0e10cSrcweir  *  </code></li>
75*cdf0e10cSrcweir  *  <li><code>::com::sun::star::accessibility::XAccessibleValue</code>
76*cdf0e10cSrcweir  *  </li>
77*cdf0e10cSrcweir  *  <li><code>::com::sun::star::accessibility::XAccessibleEventBroadcaster
78*cdf0e10cSrcweir  *  </code></li>
79*cdf0e10cSrcweir  * </ul>
80*cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleComponent
81*cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleContext
82*cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleSelection
83*cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleValue
84*cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
85*cdf0e10cSrcweir  * @see ifc.accessibility._XAccessibleEventBroadcaster
86*cdf0e10cSrcweir  * @see ifc.accessibility._XAccessibleComponent
87*cdf0e10cSrcweir  * @see ifc.accessibility._XAccessibleContext
88*cdf0e10cSrcweir  * @see ifc.accessibility._XAccessibleSelection
89*cdf0e10cSrcweir  * @see ifc.accessibility._XAccessibleTable
90*cdf0e10cSrcweir  */
91*cdf0e10cSrcweir public class ScAccessiblePreviewHeaderCell extends TestCase {
92*cdf0e10cSrcweir     static XSpreadsheetDocument xSheetDoc = null;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir     /**
95*cdf0e10cSrcweir      * Creates a spreadsheet document.
96*cdf0e10cSrcweir      */
97*cdf0e10cSrcweir     protected void initialize(TestParameters tParam, PrintWriter log) {
98*cdf0e10cSrcweir     }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     /**
101*cdf0e10cSrcweir      * Disposes a spreadsheet document.
102*cdf0e10cSrcweir      */
103*cdf0e10cSrcweir     protected void cleanup(TestParameters tParam, PrintWriter log) {
104*cdf0e10cSrcweir         log.println("    disposing xSheetDoc ");
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir         if (xSheetDoc != null) {
107*cdf0e10cSrcweir             try {
108*cdf0e10cSrcweir                 XCloseable oComp = (XCloseable) UnoRuntime.queryInterface(
109*cdf0e10cSrcweir                                            XCloseable.class, xSheetDoc);
110*cdf0e10cSrcweir                 oComp.close(true);
111*cdf0e10cSrcweir                 xSheetDoc = null;
112*cdf0e10cSrcweir             } catch (com.sun.star.util.CloseVetoException e) {
113*cdf0e10cSrcweir                 e.printStackTrace(log);
114*cdf0e10cSrcweir             } catch (com.sun.star.lang.DisposedException e) {
115*cdf0e10cSrcweir                 log.println("document already disposed");
116*cdf0e10cSrcweir                 xSheetDoc = null;
117*cdf0e10cSrcweir             }
118*cdf0e10cSrcweir         }
119*cdf0e10cSrcweir     }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     /**
122*cdf0e10cSrcweir      * Creating a Testenvironment for the interfaces to be tested.
123*cdf0e10cSrcweir      * Sets a value of the cell 'A1'. Sets the property 'PrintHeaders'
124*cdf0e10cSrcweir      * of the style 'Default' of the family 'PageStyles' to true.
125*cdf0e10cSrcweir      * Switchs the document to preview mode and then obtains the
126*cdf0e10cSrcweir      * accessible object for the header cell.
127*cdf0e10cSrcweir      */
128*cdf0e10cSrcweir     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
129*cdf0e10cSrcweir                                                                  PrintWriter log) {
130*cdf0e10cSrcweir         XInterface oObj = null;
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         if (xSheetDoc != null) {
133*cdf0e10cSrcweir             XComponent oComp = (XComponent) UnoRuntime.queryInterface(
134*cdf0e10cSrcweir                                        XComponent.class, xSheetDoc);
135*cdf0e10cSrcweir             util.DesktopTools.closeDoc(oComp);
136*cdf0e10cSrcweir         }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory) Param.getMSF());
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir         try {
141*cdf0e10cSrcweir             log.println("creating a Spreadsheet document");
142*cdf0e10cSrcweir             xSheetDoc = SOF.createCalcDoc(null);
143*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
144*cdf0e10cSrcweir             // Some exception occures.FAILED
145*cdf0e10cSrcweir             e.printStackTrace(log);
146*cdf0e10cSrcweir             throw new StatusException("Couldn't create document", e);
147*cdf0e10cSrcweir         }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class,
150*cdf0e10cSrcweir                                                            xSheetDoc);
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir         XController xController = xModel.getCurrentController();
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir         //setting value of cell A1
155*cdf0e10cSrcweir         XCell xCell = null;
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir         try {
158*cdf0e10cSrcweir             log.println("Getting spreadsheet");
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir             XSpreadsheets oSheets = xSheetDoc.getSheets();
161*cdf0e10cSrcweir             XIndexAccess oIndexSheets = (XIndexAccess) UnoRuntime.queryInterface(
162*cdf0e10cSrcweir                                                 XIndexAccess.class, oSheets);
163*cdf0e10cSrcweir             XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
164*cdf0e10cSrcweir                                           new Type(XSpreadsheet.class),
165*cdf0e10cSrcweir                                           oIndexSheets.getByIndex(0));
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir             log.println("Getting a cell from sheet");
168*cdf0e10cSrcweir             xCell = oSheet.getCellByPosition(0, 0);
169*cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
170*cdf0e10cSrcweir             e.printStackTrace(log);
171*cdf0e10cSrcweir             throw new StatusException(
172*cdf0e10cSrcweir                     "Error getting cell object from spreadsheet document", e);
173*cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
174*cdf0e10cSrcweir             e.printStackTrace(log);
175*cdf0e10cSrcweir             throw new StatusException(
176*cdf0e10cSrcweir                     "Error getting cell object from spreadsheet document", e);
177*cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
178*cdf0e10cSrcweir             e.printStackTrace(log);
179*cdf0e10cSrcweir             throw new StatusException(
180*cdf0e10cSrcweir                     "Error getting cell object from spreadsheet document", e);
181*cdf0e10cSrcweir         }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir         xCell.setFormula("Value");
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir         //setting property 'PrintHeaders' of the style 'Default'
186*cdf0e10cSrcweir         XStyleFamiliesSupplier xSFS = (XStyleFamiliesSupplier) UnoRuntime.queryInterface(
187*cdf0e10cSrcweir                                               XStyleFamiliesSupplier.class,
188*cdf0e10cSrcweir                                               xSheetDoc);
189*cdf0e10cSrcweir         XNameAccess xNA = xSFS.getStyleFamilies();
190*cdf0e10cSrcweir         XPropertySet xPropSet = null;
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir         try {
193*cdf0e10cSrcweir             Object oPageStyles = xNA.getByName("PageStyles");
194*cdf0e10cSrcweir             xNA = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class,
195*cdf0e10cSrcweir                                                           oPageStyles);
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir             Object oDefStyle = xNA.getByName("Default");
198*cdf0e10cSrcweir             xPropSet = (XPropertySet) UnoRuntime.queryInterface(
199*cdf0e10cSrcweir                                XPropertySet.class, oDefStyle);
200*cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
201*cdf0e10cSrcweir             e.printStackTrace(log);
202*cdf0e10cSrcweir             throw new StatusException(Status.failed("Couldn't get element"));
203*cdf0e10cSrcweir         } catch (com.sun.star.container.NoSuchElementException e) {
204*cdf0e10cSrcweir             e.printStackTrace(log);
205*cdf0e10cSrcweir             throw new StatusException(Status.failed("Couldn't get element"));
206*cdf0e10cSrcweir         }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir         try {
209*cdf0e10cSrcweir             xPropSet.setPropertyValue("PrintHeaders", new Boolean(true));
210*cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
211*cdf0e10cSrcweir             e.printStackTrace(log);
212*cdf0e10cSrcweir             throw new StatusException(Status.failed(
213*cdf0e10cSrcweir                                               "Couldn't set property 'PrintHeaders'"));
214*cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
215*cdf0e10cSrcweir             e.printStackTrace(log);
216*cdf0e10cSrcweir             throw new StatusException(Status.failed(
217*cdf0e10cSrcweir                                               "Couldn't set property 'PrintHeaders'"));
218*cdf0e10cSrcweir         } catch (com.sun.star.beans.PropertyVetoException e) {
219*cdf0e10cSrcweir             e.printStackTrace(log);
220*cdf0e10cSrcweir             throw new StatusException(Status.failed(
221*cdf0e10cSrcweir                                               "Couldn't set property 'PrintHeaders'"));
222*cdf0e10cSrcweir         } catch (com.sun.star.beans.UnknownPropertyException e) {
223*cdf0e10cSrcweir             e.printStackTrace(log);
224*cdf0e10cSrcweir             throw new StatusException(Status.failed(
225*cdf0e10cSrcweir                                               "Couldn't set property 'PrintHeaders'"));
226*cdf0e10cSrcweir         }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir         //switching to 'Print Preview' mode
229*cdf0e10cSrcweir         try {
230*cdf0e10cSrcweir             XDispatchProvider xDispProv = (XDispatchProvider) UnoRuntime.queryInterface(
231*cdf0e10cSrcweir                                                   XDispatchProvider.class,
232*cdf0e10cSrcweir                                                   xController);
233*cdf0e10cSrcweir             XURLTransformer xParser = (com.sun.star.util.XURLTransformer) UnoRuntime.queryInterface(
234*cdf0e10cSrcweir                                               XURLTransformer.class,
235*cdf0e10cSrcweir                                               ( (XMultiServiceFactory) Param.getMSF())
236*cdf0e10cSrcweir                                                    .createInstance("com.sun.star.util.URLTransformer"));
237*cdf0e10cSrcweir             URL[] aParseURL = new URL[1];
238*cdf0e10cSrcweir             aParseURL[0] = new URL();
239*cdf0e10cSrcweir             aParseURL[0].Complete = ".uno:PrintPreview";
240*cdf0e10cSrcweir             xParser.parseStrict(aParseURL);
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir             URL aURL = aParseURL[0];
243*cdf0e10cSrcweir             XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0);
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir             if (xDispatcher != null) {
246*cdf0e10cSrcweir                 xDispatcher.dispatch(aURL, null);
247*cdf0e10cSrcweir             }
248*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
249*cdf0e10cSrcweir             log.println("Couldn't change mode");
250*cdf0e10cSrcweir             throw new StatusException(Status.failed("Couldn't change mode"));
251*cdf0e10cSrcweir         }
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir         shortWait();
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir         AccessibilityTools at = new AccessibilityTools();
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir         XWindow xWindow = at.getCurrentWindow( (XMultiServiceFactory) Param.getMSF(), xModel);
258*cdf0e10cSrcweir         XAccessible xRoot = at.getAccessibleObject(xWindow);
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir         oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.TABLE, "A");
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir         if (oObj == null) {
263*cdf0e10cSrcweir             log.println("Version with a fixed #103863#");
264*cdf0e10cSrcweir             oObj = at.getAccessibleObjectForRole(xRoot,
265*cdf0e10cSrcweir                                                  AccessibleRole.TABLE_CELL,
266*cdf0e10cSrcweir                                                  true);
267*cdf0e10cSrcweir         }
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir         log.println("ImplementationName " + utils.getImplName(oObj));
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment(oObj);
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir         tEnv.addObjRelation("EventProducer",
274*cdf0e10cSrcweir                             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
275*cdf0e10cSrcweir             public void fireEvent() {
276*cdf0e10cSrcweir             }
277*cdf0e10cSrcweir         });
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir         return tEnv;
280*cdf0e10cSrcweir     }
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir     /**
283*cdf0e10cSrcweir     * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
284*cdf0e10cSrcweir     * reset</code> call.
285*cdf0e10cSrcweir     */
286*cdf0e10cSrcweir     private void shortWait() {
287*cdf0e10cSrcweir         try {
288*cdf0e10cSrcweir             Thread.currentThread().sleep(500);
289*cdf0e10cSrcweir         } catch (InterruptedException e) {
290*cdf0e10cSrcweir             System.out.println("While waiting :" + e);
291*cdf0e10cSrcweir         }
292*cdf0e10cSrcweir     }
293*cdf0e10cSrcweir }