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