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 tParam test parameters
59      * @param log writer to log information while testing
60      *
61      * @see TestEnvironment
62      * @see #getTestEnvironment()
63      */
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 tParam test parameters
120     * @param tEnv the environment to cleanup
121     * @param log writer to log information while testing
122     */
123     protected void cleanup( TestParameters Param, PrintWriter log) {
124         log.println( "    closing Dialog " );
125         try {
126             accAction.doAccessibleAction(0);
127         } catch (com.sun.star.lang.IndexOutOfBoundsException iae) {
128             log.println("Couldn't close dialog");
129         }
130     }
131 
132     /**
133      * Called while the <code>TestCase</code> initialization. In the
134      * implementation does nothing. Subclasses can override to initialize
135      * objects shared among all <code>TestEnvironment</code>s.
136      *
137      * @param tParam test parameters
138      * @param log writer to log information while testing
139      *
140      * @see #initializeTestCase()
141      */
142     protected void initialize(TestParameters Param, PrintWriter log) {
143         // get a soffice factory object
144         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF());
145 
146         log.println("opening dialog");
147 
148         PropertyValue[] args = new PropertyValue[1];
149         try {
150             args[0] = new PropertyValue();
151             args[0].Name = "InteractionHandler";
152             args[0].Value = ((XMultiServiceFactory)Param.getMSF()).createInstance(
153                 "com.sun.star.comp.uui.UUIInteractionHandler");
154         } catch(com.sun.star.uno.Exception e) {
155         }
156 
157         lThread = new loadThread(SOF, args);
158         lThread.start();
159         shortWait();
160 
161     }
162 
163     /**
164     * Sleeps for 2 sec. to allow StarOffice to react on <code>
165     * reset</code> call.
166     */
167     private void shortWait() {
168         try {
169             Thread.sleep(2000) ;
170         } catch (InterruptedException e) {
171             log.println("While waiting :" + e) ;
172         }
173     }
174 
175     public class loadThread extends Thread {
176 
177         private SOfficeFactory SOF = null ;
178         private PropertyValue[] args = null;
179         public XComponent xSpreadSheedDoc = null;
180 
181         public loadThread(SOfficeFactory SOF, PropertyValue[] Args) {
182             this.SOF = SOF;
183             this.args = Args;
184         }
185 
186         public void run() {
187             try {
188                 String url= utils.getFullTestURL("10test.csv");
189                 log.println("loading "+url);
190                 SOF.loadDocument(url,args);
191             } catch (com.sun.star.uno.Exception e) {
192                 e.printStackTrace();
193                 throw new StatusException( "Couldn't create document ", e );
194             }
195         }
196     }
197 
198 }
199