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