Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScAccessiblePreviewHeaderCell.java
blob388249d8c7d49eae64e051e2f5cba04b0fe700ba
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package mod._sc;
20 import java.io.PrintWriter;
22 import lib.Status;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.AccessibilityTools;
28 import util.SOfficeFactory;
29 import util.utils;
31 import com.sun.star.accessibility.AccessibleRole;
32 import com.sun.star.accessibility.XAccessible;
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.container.XIndexAccess;
35 import com.sun.star.container.XNameAccess;
36 import com.sun.star.frame.XController;
37 import com.sun.star.frame.XDispatch;
38 import com.sun.star.frame.XDispatchProvider;
39 import com.sun.star.frame.XModel;
40 import com.sun.star.lang.DisposedException;
41 import com.sun.star.lang.XComponent;
42 import com.sun.star.sheet.XSpreadsheet;
43 import com.sun.star.sheet.XSpreadsheetDocument;
44 import com.sun.star.sheet.XSpreadsheets;
45 import com.sun.star.style.XStyleFamiliesSupplier;
46 import com.sun.star.table.XCell;
47 import com.sun.star.uno.AnyConverter;
48 import com.sun.star.uno.Type;
49 import com.sun.star.uno.UnoRuntime;
50 import com.sun.star.uno.XInterface;
51 import com.sun.star.util.URL;
52 import com.sun.star.util.XCloseable;
53 import com.sun.star.util.XURLTransformer;
56 /**
57 * Object implements the following interfaces:
58 * <ul>
59 * <li> <code>::com::sun::star::accessibility::XAccessibleComponent</code>
60 * </li>
61 * <li> <code>::com::sun::star::accessibility::XAccessibleContext</code>
62 * </li>
63 * <li> <code>::com::sun::star::accessibility::XAccessibleSelection
64 * </code></li>
65 * <li><code>::com::sun::star::accessibility::XAccessibleValue</code>
66 * </li>
67 * <li><code>::com::sun::star::accessibility::XAccessibleEventBroadcaster
68 * </code></li>
69 * </ul>
70 * @see com.sun.star.accessibility.XAccessibleComponent
71 * @see com.sun.star.accessibility.XAccessibleContext
72 * @see com.sun.star.accessibility.XAccessibleSelection
73 * @see com.sun.star.accessibility.XAccessibleValue
74 * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
75 * @see ifc.accessibility._XAccessibleEventBroadcaster
76 * @see ifc.accessibility._XAccessibleComponent
77 * @see ifc.accessibility._XAccessibleContext
78 * @see ifc.accessibility._XAccessibleSelection
79 * @see ifc.accessibility._XAccessibleTable
81 public class ScAccessiblePreviewHeaderCell extends TestCase {
82 private XSpreadsheetDocument xSheetDoc = null;
84 /**
85 * Creates a spreadsheet document.
87 @Override
88 protected void initialize(TestParameters tParam, PrintWriter log) {
91 /**
92 * Disposes a spreadsheet document.
94 @Override
95 protected void cleanup(TestParameters tParam, PrintWriter log) {
96 log.println(" disposing xSheetDoc ");
98 if (xSheetDoc != null) {
99 try {
100 XCloseable oComp = UnoRuntime.queryInterface(
101 XCloseable.class, xSheetDoc);
102 oComp.close(true);
103 xSheetDoc = null;
104 } catch (com.sun.star.util.CloseVetoException e) {
105 e.printStackTrace(log);
106 } catch (com.sun.star.lang.DisposedException e) {
107 log.println("document already disposed");
108 xSheetDoc = null;
114 * Creating a Testenvironment for the interfaces to be tested.
115 * Sets a value of the cell 'A1'. Sets the property 'PrintHeaders'
116 * of the style 'Default' of the family 'PageStyles' to true.
117 * Switchs the document to preview mode and then obtains the
118 * accessible object for the header cell.
120 @Override
121 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
122 PrintWriter log) {
123 if (xSheetDoc != null) {
124 XComponent oComp = UnoRuntime.queryInterface(
125 XComponent.class, xSheetDoc);
126 util.DesktopTools.closeDoc(oComp);
129 SOfficeFactory SOF = SOfficeFactory.getFactory( Param.getMSF());
131 try {
132 log.println("creating a Spreadsheet document");
133 xSheetDoc = SOF.createCalcDoc(null);
134 } catch (com.sun.star.uno.Exception e) {
135 // Some exception occurs.FAILED
136 e.printStackTrace(log);
137 throw new StatusException("Couldn't create document", e);
140 XModel xModel = UnoRuntime.queryInterface(XModel.class,
141 xSheetDoc);
143 XController xController = xModel.getCurrentController();
145 //setting value of cell A1
146 XCell xCell = null;
148 try {
149 log.println("Getting spreadsheet");
151 XSpreadsheets oSheets = xSheetDoc.getSheets();
152 XIndexAccess oIndexSheets = UnoRuntime.queryInterface(
153 XIndexAccess.class, oSheets);
154 XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
155 new Type(XSpreadsheet.class),
156 oIndexSheets.getByIndex(0));
158 log.println("Getting a cell from sheet");
159 xCell = oSheet.getCellByPosition(0, 0);
160 } catch (com.sun.star.lang.WrappedTargetException e) {
161 e.printStackTrace(log);
162 throw new StatusException(
163 "Error getting cell object from spreadsheet document", e);
164 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
165 e.printStackTrace(log);
166 throw new StatusException(
167 "Error getting cell object from spreadsheet document", e);
168 } catch (com.sun.star.lang.IllegalArgumentException e) {
169 e.printStackTrace(log);
170 throw new StatusException(
171 "Error getting cell object from spreadsheet document", e);
174 xCell.setFormula("Value");
176 //setting property 'PrintHeaders' of the style 'Default'
177 XStyleFamiliesSupplier xSFS = UnoRuntime.queryInterface(
178 XStyleFamiliesSupplier.class,
179 xSheetDoc);
180 XNameAccess xNA = xSFS.getStyleFamilies();
181 XPropertySet xPropSet = null;
183 try {
184 Object oPageStyles = xNA.getByName("PageStyles");
185 xNA = UnoRuntime.queryInterface(XNameAccess.class,
186 oPageStyles);
188 Object oDefStyle = xNA.getByName("Default");
189 xPropSet = UnoRuntime.queryInterface(
190 XPropertySet.class, oDefStyle);
191 } catch (com.sun.star.lang.WrappedTargetException e) {
192 throw new StatusException(e, Status.failed("Couldn't get element"));
193 } catch (com.sun.star.container.NoSuchElementException e) {
194 throw new StatusException(e, Status.failed("Couldn't get element"));
197 try {
198 xPropSet.setPropertyValue("PrintHeaders", Boolean.TRUE);
199 } catch (com.sun.star.lang.WrappedTargetException e) {
200 throw new StatusException(e, Status.failed(
201 "Couldn't set property 'PrintHeaders'"));
202 } catch (com.sun.star.lang.IllegalArgumentException e) {
203 throw new StatusException(e, Status.failed(
204 "Couldn't set property 'PrintHeaders'"));
205 } catch (com.sun.star.beans.PropertyVetoException e) {
206 throw new StatusException(e, Status.failed(
207 "Couldn't set property 'PrintHeaders'"));
208 } catch (com.sun.star.beans.UnknownPropertyException e) {
209 throw new StatusException(e, Status.failed(
210 "Couldn't set property 'PrintHeaders'"));
213 //switching to 'Print Preview' mode
214 try {
215 XDispatchProvider xDispProv = UnoRuntime.queryInterface(
216 XDispatchProvider.class,
217 xController);
218 XURLTransformer xParser = UnoRuntime.queryInterface(
219 XURLTransformer.class,
220 Param.getMSF()
221 .createInstance("com.sun.star.util.URLTransformer"));
222 URL[] aParseURL = new URL[1];
223 aParseURL[0] = new URL();
224 aParseURL[0].Complete = ".uno:PrintPreview";
225 xParser.parseStrict(aParseURL);
227 URL aURL = aParseURL[0];
228 XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0);
230 if (xDispatcher != null) {
231 xDispatcher.dispatch(aURL, null);
233 } catch (com.sun.star.uno.Exception e) {
234 throw new StatusException(e, Status.failed("Couldn't change mode"));
237 XInterface oObj = null;
238 for (int i = 0;; ++i) {
239 try {
240 Thread.sleep(500);
241 } catch (InterruptedException e) {
242 throw new RuntimeException(e);
244 try {
245 XAccessible xRoot = AccessibilityTools.getAccessibleObject(
246 AccessibilityTools.getCurrentWindow(
247 xModel));
248 if (xRoot != null) {
249 oObj = AccessibilityTools.getAccessibleObjectForRole(
250 xRoot, AccessibleRole.TABLE, "A");
251 if (oObj == null) {
252 log.println("Version with a fixed #103863#?");
253 oObj = AccessibilityTools.getAccessibleObjectForRole(
254 xRoot, AccessibleRole.TABLE_CELL, true);
256 if (oObj != null) {
257 break;
260 } catch (DisposedException e) {
261 log.println("Ignoring DisposedException");
263 if (i == 20) { // give up after 10 sec
264 throw new RuntimeException(
265 "Couldn't get AccessibleRolte.TABLE/TABLE_CELL object");
267 log.println("No TABLE/TABLE_CELL found yet, retrying");
270 log.println("ImplementationName " + utils.getImplName(oObj));
272 TestEnvironment tEnv = new TestEnvironment(oObj);
274 tEnv.addObjRelation("EventProducer",
275 new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
276 public void fireEvent() {
280 return tEnv;