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.XWindow;
44 import com.sun.star.container.XIndexAccess;
45 import com.sun.star.frame.XController;
46 import com.sun.star.frame.XDispatch;
47 import com.sun.star.frame.XDispatchProvider;
48 import com.sun.star.frame.XModel;
49 import com.sun.star.lang.XComponent;
50 import com.sun.star.lang.XMultiServiceFactory;
51 import com.sun.star.sheet.XSpreadsheet;
52 import com.sun.star.sheet.XSpreadsheetDocument;
53 import com.sun.star.sheet.XSpreadsheets;
54 import com.sun.star.table.XCell;
55 import com.sun.star.uno.AnyConverter;
56 import com.sun.star.uno.Type;
57 import com.sun.star.uno.UnoRuntime;
58 import com.sun.star.uno.XInterface;
59 import com.sun.star.util.URL;
60 import com.sun.star.util.XURLTransformer;
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 ScAccessiblePreviewCell extends TestCase {
88     static XSpreadsheetDocument xSheetDoc = null;
89 
90     /**
91      * Creates a spreadsheet document.
92      */
93     protected void initialize( TestParameters tParam, PrintWriter log ) {
94         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
95         try {
96             log.println( "creating a Spreadsheet document" );
97             xSheetDoc = SOF.createCalcDoc(null);
98         } catch ( com.sun.star.uno.Exception e ) {
99             // Some exception occures.FAILED
100             e.printStackTrace( log );
101             throw new StatusException( "Couldn't create document", e );
102         }
103     }
104 
105     /**
106      * Disposes a spreadsheet document.
107      */
108     protected void cleanup( TestParameters tParam, PrintWriter log ) {
109         log.println( "    disposing xSheetDoc " );
110         XComponent oComp = (XComponent)UnoRuntime.queryInterface
111             (XComponent.class, xSheetDoc);
112         util.DesktopTools.closeDoc(oComp);
113     }
114 
115 
116     /**
117      * Creating a Testenvironment for the interfaces to be tested.
118      * Obtains the accessible object for a one of cell in preview mode.
119      */
120     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
121 
122         XInterface oObj = null;
123         XCell xCell = null;
124 
125         try {
126             log.println("Getting spreadsheet") ;
127             XSpreadsheets oSheets = xSheetDoc.getSheets() ;
128             XIndexAccess oIndexSheets = (XIndexAccess)
129             UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
130             XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
131                     new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
132 
133             log.println("Getting a cell from sheet") ;
134             xCell = oSheet.getCellByPosition(0, 0);
135         } catch (com.sun.star.lang.WrappedTargetException e) {
136             e.printStackTrace(log);
137             throw new StatusException(
138                 "Error getting cell object from spreadsheet document", e);
139         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
140             e.printStackTrace(log);
141             throw new StatusException(
142                 "Error getting cell object from spreadsheet document", e);
143         } catch (com.sun.star.lang.IllegalArgumentException e) {
144             e.printStackTrace(log);
145             throw new StatusException(
146                 "Error getting cell object from spreadsheet document", e);
147         }
148 
149         xCell.setFormula("Value");
150 
151         XModel xModel = (XModel)
152             UnoRuntime.queryInterface(XModel.class, xSheetDoc);
153 
154         XController xController = xModel.getCurrentController();
155 
156         //switch to 'Print Preview' mode
157         try {
158             XDispatchProvider xDispProv = (XDispatchProvider)
159                 UnoRuntime.queryInterface(XDispatchProvider.class, xController);
160             XURLTransformer xParser = (com.sun.star.util.XURLTransformer)
161                 UnoRuntime.queryInterface(XURLTransformer.class,
162             ((XMultiServiceFactory)Param.getMSF()).createInstance("com.sun.star.util.URLTransformer"));
163             URL[] aParseURL = new URL[1];
164             aParseURL[0] = new URL();
165             aParseURL[0].Complete = ".uno:PrintPreview";
166             xParser.parseStrict(aParseURL);
167             URL aURL = aParseURL[0];
168             XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0);
169             if(xDispatcher != null)
170                 xDispatcher.dispatch( aURL, null );
171         } catch (com.sun.star.uno.Exception e) {
172             log.println("Couldn't change mode");
173             throw new StatusException(Status.failed("Couldn't change mode"));
174         }
175 
176         shortWait();
177 
178         AccessibilityTools at = new AccessibilityTools();
179 
180         XWindow xWindow = at.getCurrentWindow((XMultiServiceFactory)Param.getMSF(), xModel);
181         XAccessible xRoot = at.getAccessibleObject(xWindow);
182 
183         at.getAccessibleObjectForRole(xRoot, AccessibleRole.TABLE_CELL, true);
184 
185         oObj = AccessibilityTools.SearchedContext;
186 
187         log.println("ImplementationName " + utils.getImplName(oObj));
188 
189         TestEnvironment tEnv = new TestEnvironment( oObj );
190 
191         tEnv.addObjRelation("EventProducer",
192             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer(){
193                 public void fireEvent() {
194                     System.out.println("Fire Event");
195                 }
196             });
197 
198         return tEnv;
199     }
200 
201     protected void shortWait() {
202         try {
203             Thread.sleep(1000) ;
204         } catch (InterruptedException e) {
205             System.out.println("While waiting :" + e);
206         }
207     }
208 }
209