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