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