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