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.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.AccessibilityTools;
37 import util.SOfficeFactory;
38 import util.utils;
39 
40 import com.sun.star.accessibility.AccessibleRole;
41 import com.sun.star.accessibility.XAccessible;
42 import com.sun.star.accessibility.XAccessibleAction;
43 import com.sun.star.accessibility.XAccessibleContext;
44 import com.sun.star.awt.XExtendedToolkit;
45 import com.sun.star.awt.XWindow;
46 import com.sun.star.beans.PropertyValue;
47 import com.sun.star.lang.XComponent;
48 import com.sun.star.lang.XMultiServiceFactory;
49 import com.sun.star.uno.UnoRuntime;
50 import com.sun.star.uno.XInterface;
51 
52 public class ScAccessibleCsvCell extends TestCase {
53 
54     Thread lThread = null;
55     static XAccessibleAction accAction = null;
56 
57     /**
58      * Called to create an instance of <code>TestEnvironment</code> with an
59      * object to test and related objects. Subclasses should implement this
60      * method to provide the implementation and related objects. The method is
61      * called from <code>getTestEnvironment()</code>.
62      *
63      * @param tParam test parameters
64      * @param log writer to log information while testing
65      *
66      * @see TestEnvironment
67      * @see #getTestEnvironment()
68      */
69     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
70         XInterface oObj = null;
71 
72         shortWait();
73 
74         try {
75             oObj = (XInterface) ((XMultiServiceFactory)tParam.getMSF()).createInstance
76                 ("com.sun.star.awt.Toolkit") ;
77         } catch (com.sun.star.uno.Exception e) {
78             log.println("Couldn't get toolkit");
79             e.printStackTrace(log);
80             throw new StatusException("Couldn't get toolkit", e );
81         }
82 
83 
84         XExtendedToolkit tk = (XExtendedToolkit)
85                         UnoRuntime.queryInterface(XExtendedToolkit.class,oObj);
86 
87         XWindow xWindow = (XWindow)
88                 UnoRuntime.queryInterface(XWindow.class,tk.getActiveTopWindow());
89 
90         XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);
91 
92         AccessibilityTools.printAccessibleTree(log, xRoot, tParam.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
93         oObj = AccessibilityTools.getAccessibleObjectForRole
94             (xRoot, AccessibleRole.PUSH_BUTTON, "Cancel");
95 
96         accAction = (XAccessibleAction) UnoRuntime.queryInterface(XAccessibleAction.class, oObj);
97 
98         oObj = AccessibilityTools.getAccessibleObjectForRole
99             (xRoot, AccessibleRole.TABLE, true);
100 
101         //util.dbg.printInterfaces(oObj);
102 
103         XAccessibleContext cont = (XAccessibleContext)
104                 UnoRuntime.queryInterface(XAccessibleContext.class, oObj);
105 
106         String name = "";
107         try {
108             XAccessible acc = cont.getAccessibleChild(3);
109             name = acc.getAccessibleContext().getAccessibleName();
110             log.println("Child: "+ name);
111             log.println("ImplementationName " + utils.getImplName(acc));
112             oObj = acc;
113         }
114         catch(com.sun.star.lang.IndexOutOfBoundsException e) {}
115 
116         TestEnvironment tEnv = new TestEnvironment(oObj);
117 
118         tEnv.addObjRelation("EditOnly",
119                     "This method isn't supported in this dialog");
120 
121         tEnv.addObjRelation("XAccessibleText.Text", name);
122 
123         return tEnv;
124     }
125 
126     /**
127     * Called while disposing a <code>TestEnvironment</code>.
128     * Disposes calc document.
129     * @param tParam test parameters
130     * @param tEnv the environment to cleanup
131     * @param log writer to log information while testing
132     */
133     protected void cleanup( TestParameters Param, PrintWriter log) {
134         log.println( "    closing Dialog " );
135         try {
136             accAction.doAccessibleAction(0);
137         } catch (com.sun.star.lang.IndexOutOfBoundsException iae) {
138             log.println("Couldn't close dialog");
139         }
140     }
141 
142     /**
143      * Called while the <code>TestCase</code> initialization. In the
144      * implementation does nothing. Subclasses can override to initialize
145      * objects shared among all <code>TestEnvironment</code>s.
146      *
147      * @param tParam test parameters
148      * @param log writer to log information while testing
149      *
150      * @see #initializeTestCase()
151      */
152     protected void initialize(TestParameters Param, PrintWriter log) {
153         // get a soffice factory object
154         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
155 
156         log.println("opening dialog");
157 
158         PropertyValue[] args = new PropertyValue[1];
159         try {
160             args[0] = new PropertyValue();
161             args[0].Name = "InteractionHandler";
162             args[0].Value = ((XMultiServiceFactory)Param.getMSF()).createInstance(
163                 "com.sun.star.comp.uui.UUIInteractionHandler");
164         } catch(com.sun.star.uno.Exception e) {
165         }
166 
167         lThread = new loadThread(SOF, args);
168         lThread.start();
169         shortWait();
170     }
171 
172     /**
173     * Sleeps for 2 sec. to allow StarOffice to react on <code>
174     * reset</code> call.
175     */
176     private void shortWait() {
177         try {
178             Thread.sleep(2000) ;
179         } catch (InterruptedException e) {
180             log.println("While waiting :" + e) ;
181         }
182     }
183 
184     public class loadThread extends Thread {
185 
186         private SOfficeFactory SOF = null ;
187         private PropertyValue[] args = null;
188         public XComponent xSpreadSheedDoc = null;
189 
190         public loadThread(SOfficeFactory SOF, PropertyValue[] Args) {
191             this.SOF = SOF;
192             this.args = Args;
193         }
194 
195         public void run() {
196             try {
197                 String url= utils.getFullTestURL("10test.csv");
198                 log.println("loading "+url);
199                 SOF.loadDocument(url,args);
200             } catch (com.sun.star.uno.Exception e) {
201                 e.printStackTrace();
202                 throw new StatusException( "Couldn't create document ", e );
203             }
204         }
205     }
206 
207 
208 }
209