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