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 
24 package mod._sc;
25 
26 import java.io.PrintWriter;
27 
28 import lib.Status;
29 import lib.StatusException;
30 import lib.TestCase;
31 import lib.TestEnvironment;
32 import lib.TestParameters;
33 import util.AccessibilityTools;
34 import util.SOfficeFactory;
35 import util.utils;
36 
37 import com.sun.star.accessibility.AccessibleRole;
38 import com.sun.star.accessibility.XAccessible;
39 import com.sun.star.accessibility.XAccessibleAction;
40 import com.sun.star.accessibility.XAccessibleContext;
41 import com.sun.star.accessibility.XAccessibleStateSet;
42 import com.sun.star.awt.XWindow;
43 import com.sun.star.container.XIndexAccess;
44 import com.sun.star.frame.XController;
45 import com.sun.star.frame.XDispatch;
46 import com.sun.star.frame.XDispatchProvider;
47 import com.sun.star.frame.XModel;
48 import com.sun.star.lang.XMultiServiceFactory;
49 import com.sun.star.sheet.XSpreadsheet;
50 import com.sun.star.sheet.XSpreadsheetDocument;
51 import com.sun.star.sheet.XSpreadsheets;
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  * Test for object which is represented by accessible component of
63  * a printed header in 'Page Preview' mode.
64  *
65  * Object implements the following interfaces :
66  * <ul>
67  *  <li> <code>::com::sun::star::accessibility::XAccessibleComponent</code></li>
68  *  <li> <code>::com::sun::star::accessibility::XAccessibleContext</code></li>
69  * </ul> <p>
70  *
71  * @see com.sun.star.accessibility.XAccessibleComponent
72  * @see com.sun.star.accessibility.XAccessibleContext
73  * @see ifc.accessibility._XAccessibleComponent
74  * @see ifc.accessibility._XAccessibleContext
75  */
76 public class ScAccessiblePageHeaderArea extends TestCase {
77 
78     static XSpreadsheetDocument xSpreadsheetDoc = null;
79 
80     /**
81      * Called to create an instance of <code>TestEnvironment</code>
82      * with an object to test and related objects.
83      * Switches the document to Print Preview mode.
84      * Obtains accessible object for the page view.
85      *
86      * @param Param test parameters
87      * @param log writer to log information while testing
88      *
89      * @see TestEnvironment
90      * @see #getTestEnvironment
91      */
createTestEnvironment( TestParameters Param, PrintWriter log)92     protected TestEnvironment createTestEnvironment(
93         TestParameters Param, PrintWriter log) {
94 
95         XInterface oObj = null;
96 
97         // inserting some content to have non-empty page preview
98         XCell xCell = null;
99         try {
100             XSpreadsheets oSheets = xSpreadsheetDoc.getSheets() ;
101             XIndexAccess oIndexSheets = (XIndexAccess)
102                 UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
103             XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
104                             new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
105             xCell = oSheet.getCellByPosition(0, 0) ;
106             xCell.setFormula("ScAccessiblePageHeaderArea");
107         } catch(com.sun.star.lang.WrappedTargetException e) {
108             log.println("Exception ceating relation :");
109             e.printStackTrace(log);
110         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
111             log.println("Exception ceating relation :");
112             e.printStackTrace(log);
113         } catch(com.sun.star.lang.IllegalArgumentException e) {
114             log.println("Exception ceating relation :");
115             e.printStackTrace(log);
116         }
117 
118         XModel aModel = (XModel)
119             UnoRuntime.queryInterface(XModel.class, xSpreadsheetDoc);
120 
121         XController xController = aModel.getCurrentController();
122 
123         // switching to 'Page Preview' mode
124         try {
125             XDispatchProvider xDispProv = (XDispatchProvider)
126                 UnoRuntime.queryInterface(XDispatchProvider.class, xController);
127             XURLTransformer xParser = (com.sun.star.util.XURLTransformer)
128                 UnoRuntime.queryInterface(XURLTransformer.class,
129             ( (XMultiServiceFactory) Param.getMSF()).createInstance("com.sun.star.util.URLTransformer"));
130             // Because it's an in/out parameter we must use an array of URL objects.
131             URL[] aParseURL = new URL[1];
132             aParseURL[0] = new URL();
133             aParseURL[0].Complete = ".uno:PrintPreview";
134             xParser.parseStrict(aParseURL);
135             URL aURL = aParseURL[0];
136             XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0);
137             if(xDispatcher != null)
138                 xDispatcher.dispatch( aURL, null );
139         } catch (com.sun.star.uno.Exception e) {
140             log.println("Couldn't change mode");
141             throw new StatusException(Status.failed("Couldn't change mode"));
142         }
143 
144         try {
145             Thread.sleep(500);
146         } catch (InterruptedException ex) {}
147 
148         AccessibilityTools at = new AccessibilityTools();
149 
150         XWindow xWindow = at.getCurrentContainerWindow( (XMultiServiceFactory) Param.getMSF(), aModel);
151         XAccessible xRoot = at.getAccessibleObject(xWindow);
152 
153         try {
154             oObj = at.getAccessibleObjectForRole
155                 (xRoot, AccessibleRole.HEADER, "").getAccessibleChild(0);
156             XAccessibleContext cont = (XAccessibleContext)
157                     UnoRuntime.queryInterface(XAccessibleContext.class, oObj);
158             XAccessibleStateSet StateSet = cont.getAccessibleStateSet();
159             if (StateSet.contains((short)27)) {
160                 log.println("Object is transient");
161             }
162         } catch (com.sun.star.lang.IndexOutOfBoundsException iabe) {
163             throw new StatusException("Couldn't find needed Child",iabe);
164         }
165 
166         log.println("ImplementationName " + utils.getImplName(oObj));
167         at.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
168 
169         TestEnvironment tEnv = new TestEnvironment(oObj);
170 
171         XAccessibleContext zoomIn =
172             at.getAccessibleObjectForRole(xRoot,AccessibleRole.PUSH_BUTTON, "Zoom In");
173 
174         final XAccessibleAction pressZoom = (XAccessibleAction)
175                     UnoRuntime.queryInterface(XAccessibleAction.class, zoomIn);
176         tEnv.addObjRelation("EventProducer",
177             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
178                 public void fireEvent() {
179                         try {
180                             pressZoom.doAccessibleAction(0);
181                         } catch (com.sun.star.lang.IndexOutOfBoundsException ibe) {}
182                 }
183             });
184 
185         return tEnv;
186 
187     }
188 
189     /**
190      * Called while disposing a <code>TestEnvironment</code>.
191      * Disposes calc document.
192      * @param Param test parameters
193      * @param log writer to log information while testing
194      */
cleanup( TestParameters Param, PrintWriter log)195     protected void cleanup( TestParameters Param, PrintWriter log) {
196         log.println( "    disposing xSheetDoc " );
197         try {
198         XCloseable oComp = (XCloseable)
199             UnoRuntime.queryInterface (XCloseable.class, xSpreadsheetDoc) ;
200         oComp.close(true);
201         }catch(com.sun.star.util.CloseVetoException e) {
202             log.println("Couldn't close document: "+e.getMessage());
203         }
204     }
205 
206     /**
207      * Called while the <code>TestCase</code> initialization. In the
208      * implementation does nothing. Subclasses can override to initialize
209      * objects shared among all <code>TestEnvironment</code>s.
210      *
211      * @param Param test parameters
212      * @param log writer to log information while testing
213      *
214      * @see #initializeTestCase
215      */
initialize(TestParameters Param, PrintWriter log)216     protected void initialize(TestParameters Param, PrintWriter log) {
217         // get a soffice factory object
218         SOfficeFactory SOF = SOfficeFactory.getFactory(  (XMultiServiceFactory) Param.getMSF());
219 
220         try {
221             log.println("creating a spreadsheetdocument");
222             xSpreadsheetDoc = SOF.createCalcDoc(null);
223         } catch (com.sun.star.uno.Exception e) {
224             e.printStackTrace( log );
225             throw new StatusException( "Couldn't create document ", e );
226         }
227     }
228 }
229