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 complex.toolkit;
25 
26 import complex.toolkit.accessibility._XAccessibleEventBroadcaster;
27 import complex.toolkit.accessibility._XAccessibleExtendedComponent;
28 import complex.toolkit.accessibility._XAccessibleComponent;
29 import complex.toolkit.accessibility._XAccessibleContext;
30 import java.util.logging.Logger;
31 import java.util.logging.Level;
32 import util.SOfficeFactory;
33 import util.AccessibilityTools;
34 import com.sun.star.awt.XWindow;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XServiceInfo;
38 import com.sun.star.sheet.XSpreadsheetDocument;
39 import com.sun.star.text.XTextDocument;
40 import com.sun.star.uno.XInterface;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.util.XCloseable;
43 import com.sun.star.accessibility.AccessibleRole;
44 import com.sun.star.accessibility.XAccessible;
45 import com.sun.star.accessibility.XAccessibleContext;
46 import com.sun.star.awt.XExtendedToolkit;
47 
48 import org.junit.AfterClass;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
51 import org.openoffice.test.OfficeConnection;
52 import static org.junit.Assert.*;
53 
54 /**
55  *
56  */
57 public class AccessibleStatusBar {
58 
59     XInterface testObject = null;
60     XMultiServiceFactory xMSF = null;
61     XWindow xWindow = null;
62 
getMSF()63     private XMultiServiceFactory getMSF()
64     {
65         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
66         return xMSF1;
67     }
68 
69 
70     /**
71     * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
72     * reset</code> call.
73     */
shortWait()74     private void shortWait() {
75         try {
76             Thread.sleep(500) ;
77         } catch (InterruptedException e) {
78             System.out.println("While waiting :" + e) ;
79         }
80     }
81 
82     /**
83      * Check document types
84      */
85     @Test
checkDocs()86     public void checkDocs() {
87         checkWriterDoc();
88         checkMathDoc();
89         checkDrawDoc();
90         checkImpressDoc();
91         checkCalcDoc();
92     }
93 
94     /**
95      * Test the interfaces on a writer document
96      */
checkWriterDoc()97     public void checkWriterDoc() {
98         xMSF = getMSF();
99         SOfficeFactory xSOF = SOfficeFactory.getFactory(xMSF);
100         XTextDocument xTextDoc = null;
101         try {
102             System.out.println("****** Open a new writer document");
103             xTextDoc = xSOF.createTextDoc("_blank");
104             getTestObject();
105         }
106         catch(com.sun.star.uno.Exception e) {
107             Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, "caught an exception", e );
108         }
109         runAllInterfaceTests();
110 
111         if (xTextDoc != null) {
112             XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xTextDoc);
113             try {
114                 xClose.close(false);
115             }
116             catch(com.sun.star.util.CloseVetoException e) {
117                 Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, "caught an exception", e );
118             }
119         }
120     }
121 
122     /**
123      * Test the interfaces on a math document
124      */
checkMathDoc()125     public void checkMathDoc() {
126         xMSF = getMSF();
127         SOfficeFactory xSOF = SOfficeFactory.getFactory(xMSF);
128         XComponent xMathDoc = null;
129         try {
130             System.out.println("****** Open a new math document");
131             xMathDoc = xSOF.createMathDoc("_blank");
132             getTestObject();
133         }
134         catch(com.sun.star.uno.Exception e) {
135             Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, "caught an exception", e );
136         }
137         runAllInterfaceTests();
138 
139         if (xMathDoc != null) {
140             XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xMathDoc);
141             try {
142                 xClose.close(false);
143             }
144             catch(com.sun.star.util.CloseVetoException e) {
145                 Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, "caught an exception", e );
146             }
147         }
148     }
149 
150     /**
151      * Test the interfaces on a draw document
152      */
checkDrawDoc()153     public void checkDrawDoc() {
154         xMSF = getMSF();
155         SOfficeFactory xSOF = SOfficeFactory.getFactory(xMSF);
156         XComponent xDrawDoc = null;
157         try {
158             System.out.println("****** Open a new draw document");
159             xDrawDoc = xSOF.createDrawDoc("_blank");
160             getTestObject();
161         }
162         catch(com.sun.star.uno.Exception e) {
163             Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, "caught an exception", e );
164         }
165         runAllInterfaceTests();
166 
167         if (xDrawDoc != null) {
168             XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xDrawDoc);
169             try {
170                 xClose.close(false);
171             }
172             catch(com.sun.star.util.CloseVetoException e) {
173                 Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, "caught an exception", e );
174             }
175         }
176     }
177 
178     /**
179      * Test the interfaces on an impress document
180      */
checkImpressDoc()181     public void checkImpressDoc() {
182         xMSF = getMSF();
183         SOfficeFactory xSOF = SOfficeFactory.getFactory(xMSF);
184         XComponent xImpressDoc = null;
185         try {
186             System.out.println("****** Open a new impress document");
187             xImpressDoc = xSOF.createImpressDoc("_blank");
188             getTestObject();
189         }
190         catch(com.sun.star.uno.Exception e) {
191             Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, "caught an exception", e );
192         }
193         runAllInterfaceTests();
194 
195         if (xImpressDoc != null) {
196             XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xImpressDoc);
197             try {
198                 xClose.close(false);
199             }
200             catch(com.sun.star.util.CloseVetoException e) {
201                 Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, "caught an exception", e );
202             }
203         }
204     }
205     /**
206      * Test the interfaces on an calc document
207      */
checkCalcDoc()208     public void checkCalcDoc() {
209         xMSF = getMSF();
210         SOfficeFactory xSOF = SOfficeFactory.getFactory(xMSF);
211         XSpreadsheetDocument xSpreadsheetDoc = null;
212         try {
213             System.out.println("****** Open a new calc document");
214             xSpreadsheetDoc = xSOF.createCalcDoc("_blank");
215             shortWait();
216             getTestObject();
217         }
218         catch(com.sun.star.uno.Exception e) {
219             Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, "caught an exception", e );
220         }
221         runAllInterfaceTests();
222 
223         if (xSpreadsheetDoc != null) {
224             XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xSpreadsheetDoc);
225             try {
226                 xClose.close(false);
227             }
228             catch(com.sun.star.util.CloseVetoException e) {
229                 Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, "caught an exception", e );
230             }
231         }
232     }
233 
getTestObject()234     public void getTestObject() {
235         try {
236             XInterface xIfc = (XInterface) xMSF.createInstance(
237                                             "com.sun.star.awt.Toolkit") ;
238             XExtendedToolkit tk = UnoRuntime.queryInterface(XExtendedToolkit.class, xIfc);
239 
240             shortWait();
241             xWindow = UnoRuntime.queryInterface(XWindow.class, tk.getActiveTopWindow());
242 
243             shortWait();
244             XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);
245             XAccessibleContext parentContext = null;
246 
247             System.out.println("Get the accessible status bar.");
248             parentContext = AccessibilityTools.getAccessibleObjectForRole(
249                                         xRoot, AccessibleRole.STATUS_BAR, "");
250             shortWait();
251             System.out.println("...OK.");
252 
253             XServiceInfo xSI = UnoRuntime.queryInterface(XServiceInfo.class, parentContext);
254             String[] services = xSI.getSupportedServiceNames();
255             System.out.println("*****");
256             System.out.println("* Implementation Name: " + xSI.getImplementationName());
257             for (int i=0; i<services.length; i++)
258             {
259                 System.out.println("* ServiceName " + i + ": " + services[i]);
260             }
261             System.out.println("*****");
262             testObject=parentContext;
263         }
264         catch(com.sun.star.uno.Exception e) {
265             Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, "caught an exception", e );
266         }
267         catch(Throwable t) {
268             System.out.println("Got throwable:");
269             Logger.getLogger( this.getClass().getName() ).log( Level.SEVERE, "caught an exception", t );
270         }
271     }
272 
runAllInterfaceTests()273     public void runAllInterfaceTests() {
274         System.out.println("*** Now testing XAccessibleComponent ***");
275         _XAccessibleComponent _xAccCompTest =
276                                 new _XAccessibleComponent(testObject);
277         assertTrue("failed: XAccessibleComponent::getBounds", _xAccCompTest._getBounds());
278         assertTrue("failed: XAccessibleComponent::contains", _xAccCompTest._containsPoint());
279         assertTrue("failed: XAccessibleComponent::getAccessibleAt", _xAccCompTest._getAccessibleAtPoint());
280         assertTrue("failed: XAccessibleComponent::getBackground", _xAccCompTest._getBackground());
281         assertTrue("failed: XAccessibleComponent::getForeground", _xAccCompTest._getForeground());
282         assertTrue("failed: XAccessibleComponent::getLocation", _xAccCompTest._getLocation());
283         assertTrue("failed: XAccessibleComponent::getLocationOnScreen", _xAccCompTest._getLocationOnScreen());
284         assertTrue("failed: XAccessibleComponent::getSize", _xAccCompTest._getSize());
285         assertTrue("failed: XAccessibleComponent::grabFocus", _xAccCompTest._grabFocus());
286 
287         System.out.println("*** Now testing XAccessibleContext ***");
288         _XAccessibleContext _xAccContext =
289                                 new _XAccessibleContext(testObject);
290         assertTrue("failed: XAccessibleContext::getAccessibleChildCount", _xAccContext._getAccessibleChildCount());
291         assertTrue("failed: XAccessibleContext::getAccessibleChild", _xAccContext._getAccessibleChild());
292         assertTrue("failed: XAccessibleContext::getAccessibleDescription", _xAccContext._getAccessibleDescription());
293         assertTrue("failed: XAccessibleContext::getAccessibleName", _xAccContext._getAccessibleName());
294         assertTrue("failed: XAccessibleContext::getAccessibleParent", _xAccContext._getAccessibleParent());
295         assertTrue("failed: XAccessibleContext::getAccessibleIndexInParent", _xAccContext._getAccessibleIndexInParent());
296         assertTrue("failed: XAccessibleContext::getAccessibleRelationSet", _xAccContext._getAccessibleRelationSet());
297         assertTrue("failed: XAccessibleContext::getAccessibleRole", _xAccContext._getAccessibleRole());
298         assertTrue("failed: XAccessibleContext::getAccessibleStateSet", _xAccContext._getAccessibleStateSet());
299         assertTrue("failed: XAccessibleContext::getLocale", _xAccContext._getLocale());
300 
301         System.out.println("*** Now testing XAccessibleExtendedComponent ***");
302         _XAccessibleExtendedComponent _xAccExtComp =
303                                 new _XAccessibleExtendedComponent(testObject);
304         assertTrue("failed: XAccessibleExtendedComponent::getFont", _xAccExtComp._getFont());
305         assertTrue("failed: XAccessibleExtendedComponent::getTitledBorderText", _xAccExtComp._getTitledBorderText());
306         assertTrue("failed: XAccessibleExtendedComponent::getToolTipText", _xAccExtComp._getToolTipText());
307 
308         System.out.println("*** Now testing XAccessibleEventBroadcaster ***");
309         _XAccessibleEventBroadcaster _xAccEvBcast =
310                                 new _XAccessibleEventBroadcaster(testObject, xWindow);
311         assertTrue("failed: XAccessibleEventBroadcaster::addEventListener", _xAccEvBcast._addEventListener());
312         assertTrue("failed: XAccessibleEventBroadcaster::removeEventListener", _xAccEvBcast._removeEventListener());
313     }
314 
315 
316 
317 
318     @BeforeClass
setUpConnection()319     public static void setUpConnection() throws Exception {
320         System.out.println("setUpConnection()");
321         connection.setUp();
322     }
323 
324     @AfterClass
tearDownConnection()325     public static void tearDownConnection()
326         throws InterruptedException, com.sun.star.uno.Exception
327     {
328         System.out.println("tearDownConnection()");
329         connection.tearDown();
330     }
331 
332     private static final OfficeConnection connection = new OfficeConnection();
333 
334 }
335