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