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