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