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