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