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