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