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.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.AccessibilityTools;
33 import util.SOfficeFactory;
34 import util.utils;
35 
36 import com.sun.star.accessibility.AccessibleRole;
37 import com.sun.star.accessibility.XAccessible;
38 import com.sun.star.awt.XWindow;
39 import com.sun.star.beans.XPropertySet;
40 import com.sun.star.container.XIndexAccess;
41 import com.sun.star.frame.XModel;
42 import com.sun.star.lang.XComponent;
43 import com.sun.star.lang.XMultiServiceFactory;
44 import com.sun.star.sheet.XSpreadsheet;
45 import com.sun.star.sheet.XSpreadsheetDocument;
46 import com.sun.star.sheet.XSpreadsheets;
47 import com.sun.star.table.XCell;
48 import com.sun.star.table.XColumnRowRange;
49 import com.sun.star.table.XTableColumns;
50 import com.sun.star.uno.AnyConverter;
51 import com.sun.star.uno.Type;
52 import com.sun.star.uno.UnoRuntime;
53 import com.sun.star.uno.XInterface;
54 
55 /**
56  * Test for object which is represented by accessible component of
57  * a cell in the spreadsheet. <p>
58  * Object implements the following interfaces :
59  * <ul>
60  *  <li> <code>::com::sun::star::accessibility::XAccessibleSelection</code></li>
61  *  <li> <code>::com::sun::star::accessibility::XAccessibleEventBroadcaster</code></li>
62  *  <li> <code>::com::sun::star::accessibility::XAccessibleComponent</code></li>
63  *  <li> <code>::com::sun::star::accessibility::XAccessibleTable</code></li>
64  *  <li> <code>::com::sun::star::accessibility::XAccessibleContext</code></li>
65  * </ul> <p>
66  *
67  * @see com.sun.star.accessibility.XAccessibleSelection
68  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
69  * @see com.sun.star.accessibility.XAccessibleComponent
70  * @see com.sun.star.accessibility.XAccessibleTable
71  * @see com.sun.star.accessibility.XAccessibleContext
72  * @see ifc.accessibility._XAccessibleSelection
73  * @see ifc.accessibility._XAccessibleEventBroadcaster
74  * @see ifc.accessibility._XAccessibleComponent
75  * @see ifc.accessibility._XAccessibleTable
76  * @see ifc.accessibility._XAccessibleContext
77  */
78 public class ScAccessibleCell extends TestCase {
79 
80     static XSpreadsheetDocument xSpreadsheetDoc = null;
81 
82     /**
83     * Called to create an instance of <code>TestEnvironment</code>
84     * with an object to test and related objects.
85     * Switches the document to Print Preview mode.
86     * Obtains accessible object for the page view.
87     *
88     * @param Param test parameters
89     * @param log writer to log information while testing
90     *
91     * @see TestEnvironment
92     * @see #getTestEnvironment
93     */
createTestEnvironment( TestParameters Param, PrintWriter log)94     protected TestEnvironment createTestEnvironment(
95         TestParameters Param, PrintWriter log) {
96 
97         // get a soffice factory object
98         SOfficeFactory SOF = SOfficeFactory.getFactory(  (XMultiServiceFactory) Param.getMSF());
99 
100         try {
101             log.println("creating a spreadsheetdocument");
102             xSpreadsheetDoc = SOF.createCalcDoc(null);
103         } catch (com.sun.star.uno.Exception e) {
104             e.printStackTrace( log );
105             throw new StatusException( "Couldn't create document ", e );
106         }
107 
108         XInterface oObj = null;
109 
110         XModel aModel = (XModel)
111             UnoRuntime.queryInterface(XModel.class, xSpreadsheetDoc);
112 
113         XWindow xWindow = AccessibilityTools.getCurrentWindow( (XMultiServiceFactory) Param.getMSF(), aModel);
114         XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);
115 
116         oObj = AccessibilityTools.getAccessibleObjectForRole
117             (xRoot, AccessibleRole.TABLE_CELL, "B1");
118 
119         log.println("ImplementationName " + utils.getImplName(oObj));
120 
121         TestEnvironment tEnv = new TestEnvironment(oObj);
122 
123         // relation for XAccessibleEventBroadcaster
124         XCell xCell = null;
125         final String text = "XAccessibleText";
126         try {
127             XSpreadsheets oSheets = xSpreadsheetDoc.getSheets() ;
128             XIndexAccess oIndexSheets = (XIndexAccess)
129                 UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
130             XSpreadsheet oSheet = null;
131             try {
132                 oSheet = (XSpreadsheet) AnyConverter.toObject(
133                         new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
134             } catch (com.sun.star.lang.IllegalArgumentException iae) {
135                 throw new StatusException("couldn't get sheet",iae);
136             }
137             xCell = oSheet.getCellByPosition(1, 0) ;
138             xCell.setFormula(text);
139             XColumnRowRange oColumnRowRange = (XColumnRowRange)
140                 UnoRuntime.queryInterface(XColumnRowRange.class, oSheet);
141             XTableColumns oColumns = (XTableColumns) oColumnRowRange.getColumns();
142             XIndexAccess oIndexAccess = (XIndexAccess)
143                 UnoRuntime.queryInterface(XIndexAccess.class, oColumns);
144             XPropertySet column = (XPropertySet) UnoRuntime.queryInterface(
145                                 XPropertySet.class,oIndexAccess.getByIndex(1));
146             column.setPropertyValue("OptimalWidth", new Boolean(true));
147         } catch(com.sun.star.lang.WrappedTargetException e) {
148             log.println("Exception ceating relation :");
149             e.printStackTrace(log);
150         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
151             log.println("Exception ceating relation :");
152             e.printStackTrace(log);
153         } catch(com.sun.star.beans.UnknownPropertyException e) {
154             log.println("Exception ceating relation :");
155             e.printStackTrace(log);
156         } catch(com.sun.star.beans.PropertyVetoException e) {
157             log.println("Exception ceating relation :");
158             e.printStackTrace(log);
159         } catch(com.sun.star.lang.IllegalArgumentException e) {
160             log.println("Exception ceating relation :");
161             e.printStackTrace(log);
162         }
163 
164         tEnv.addObjRelation("EditOnly",
165                     "This method is only supported if the Cell is in edit mode");
166 
167         final XCell fCell = xCell ;
168 
169         tEnv.addObjRelation("EventProducer",
170             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer(){
171                 public void fireEvent() {
172                     fCell.setFormula("firing event");
173                     fCell.setFormula(text);
174                 }
175             });
176 
177         tEnv.addObjRelation("XAccessibleText.Text", text);
178 
179         return tEnv;
180 
181     }
182 
183     /**
184     * Called while disposing a <code>TestEnvironment</code>.
185     * Disposes calc document.
186     * @param Param test parameters
187     * @param log writer to log information while testing
188     */
cleanup( TestParameters Param, PrintWriter log)189     protected void cleanup( TestParameters Param, PrintWriter log) {
190         log.println( "    disposing xSheetDoc " );
191         XComponent oComp = (XComponent)
192             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc) ;
193         util.DesktopTools.closeDoc(oComp);
194     }
195 }
196