1ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5ef39d40dSAndrew Rist * distributed with this work for additional information 6ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17ef39d40dSAndrew Rist * specific language governing permissions and limitations 18ef39d40dSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20ef39d40dSAndrew Rist *************************************************************/ 21ef39d40dSAndrew Rist 22ef39d40dSAndrew Rist 23cdf0e10cSrcweir package mod._sw; 24cdf0e10cSrcweir 25cdf0e10cSrcweir import java.io.PrintWriter; 26cdf0e10cSrcweir 27cdf0e10cSrcweir import lib.Status; 28cdf0e10cSrcweir import lib.StatusException; 29cdf0e10cSrcweir import lib.TestCase; 30cdf0e10cSrcweir import lib.TestEnvironment; 31cdf0e10cSrcweir import lib.TestParameters; 32cdf0e10cSrcweir import util.AccessibilityTools; 33cdf0e10cSrcweir import util.WriterTools; 34cdf0e10cSrcweir import util.utils; 35cdf0e10cSrcweir 36cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleRole; 37cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible; 38cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleContext; 39cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleValue; 40cdf0e10cSrcweir import com.sun.star.awt.XWindow; 41cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 42cdf0e10cSrcweir import com.sun.star.container.XEnumeration; 43cdf0e10cSrcweir import com.sun.star.container.XEnumerationAccess; 44cdf0e10cSrcweir import com.sun.star.frame.XController; 45cdf0e10cSrcweir import com.sun.star.frame.XDispatch; 46cdf0e10cSrcweir import com.sun.star.frame.XDispatchProvider; 47cdf0e10cSrcweir import com.sun.star.frame.XModel; 48cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 49cdf0e10cSrcweir import com.sun.star.text.ControlCharacter; 50cdf0e10cSrcweir import com.sun.star.text.XText; 51cdf0e10cSrcweir import com.sun.star.text.XTextCursor; 52cdf0e10cSrcweir import com.sun.star.text.XTextDocument; 53cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 54cdf0e10cSrcweir import com.sun.star.uno.Type; 55cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 56cdf0e10cSrcweir import com.sun.star.uno.XInterface; 57cdf0e10cSrcweir import com.sun.star.util.URL; 58cdf0e10cSrcweir import com.sun.star.util.XURLTransformer; 59cdf0e10cSrcweir 60cdf0e10cSrcweir public class SwAccessiblePageView extends TestCase { 61cdf0e10cSrcweir 62cdf0e10cSrcweir XTextDocument xTextDoc = null; 63cdf0e10cSrcweir 64cdf0e10cSrcweir /** 65cdf0e10cSrcweir * Called to create an instance of <code>TestEnvironment</code> 66cdf0e10cSrcweir * with an object to test and related objects. 67*e6b649b5SPedro Giffuni * Switches the document to Print Preview mode. 68*e6b649b5SPedro Giffuni * Obtains accessible object for the page view. 69cdf0e10cSrcweir * 70*e6b649b5SPedro Giffuni * @param Param test parameters 71cdf0e10cSrcweir * @param log writer to log information while testing 72cdf0e10cSrcweir * 73cdf0e10cSrcweir * @see TestEnvironment 745496b966SPedro Giffuni * @see #getTestEnvironment 75cdf0e10cSrcweir */ createTestEnvironment( TestParameters Param, PrintWriter log)76cdf0e10cSrcweir protected TestEnvironment createTestEnvironment( 77cdf0e10cSrcweir TestParameters Param, PrintWriter log) { 78cdf0e10cSrcweir 79cdf0e10cSrcweir XInterface oObj = null; 80cdf0e10cSrcweir XInterface port = null; 81cdf0e10cSrcweir XInterface para = null; 82cdf0e10cSrcweir XPropertySet paraP = null; 83cdf0e10cSrcweir XPropertySet portP = null; 84cdf0e10cSrcweir 85cdf0e10cSrcweir XText oText = xTextDoc.getText(); 86cdf0e10cSrcweir XTextCursor oCursor = oText.createTextCursor(); 87cdf0e10cSrcweir 88cdf0e10cSrcweir log.println( "inserting some lines" ); 89cdf0e10cSrcweir try { 90cdf0e10cSrcweir for (int i=0; i<2; i++){ 91cdf0e10cSrcweir oText.insertString( oCursor,"Paragraph Number: " + i, false); 92cdf0e10cSrcweir oText.insertString( oCursor, 93cdf0e10cSrcweir " The quick brown fox jumps over the lazy Dog: SwXParagraph", 94cdf0e10cSrcweir false); 95cdf0e10cSrcweir oText.insertControlCharacter( 96cdf0e10cSrcweir oCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 97cdf0e10cSrcweir oText.insertString( oCursor, 98cdf0e10cSrcweir "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG: SwXParagraph", 99cdf0e10cSrcweir false); 100cdf0e10cSrcweir oText.insertControlCharacter(oCursor, 101cdf0e10cSrcweir ControlCharacter.PARAGRAPH_BREAK, false ); 102cdf0e10cSrcweir oText.insertControlCharacter( 103cdf0e10cSrcweir oCursor, ControlCharacter.LINE_BREAK, false ); 104cdf0e10cSrcweir } 105cdf0e10cSrcweir } catch ( com.sun.star.lang.IllegalArgumentException e ){ 106cdf0e10cSrcweir e.printStackTrace(log); 107cdf0e10cSrcweir throw new StatusException( "Couldn't insert lines", e ); 108cdf0e10cSrcweir } 109cdf0e10cSrcweir 110cdf0e10cSrcweir // Enumeration 111cdf0e10cSrcweir XEnumerationAccess oEnumA = (XEnumerationAccess) 112cdf0e10cSrcweir UnoRuntime.queryInterface(XEnumerationAccess.class, oText ); 113cdf0e10cSrcweir XEnumeration oEnum = oEnumA.createEnumeration(); 114cdf0e10cSrcweir 115cdf0e10cSrcweir try { 116cdf0e10cSrcweir para = (XInterface) AnyConverter.toObject( 117cdf0e10cSrcweir new Type(XInterface.class),oEnum.nextElement()); 118cdf0e10cSrcweir XEnumerationAccess oEnumB = (XEnumerationAccess) 119cdf0e10cSrcweir UnoRuntime.queryInterface( XEnumerationAccess.class, para ); 120cdf0e10cSrcweir XEnumeration oEnum2 = oEnumB.createEnumeration(); 121cdf0e10cSrcweir port = (XInterface) AnyConverter.toObject( 122cdf0e10cSrcweir new Type(XInterface.class),oEnum2.nextElement()); 123cdf0e10cSrcweir } catch ( com.sun.star.lang.WrappedTargetException e ) { 124cdf0e10cSrcweir e.printStackTrace(log); 125cdf0e10cSrcweir log.println("Error: exception occured..."); 126cdf0e10cSrcweir } catch ( com.sun.star.container.NoSuchElementException e ) { 127cdf0e10cSrcweir e.printStackTrace(log); 128cdf0e10cSrcweir log.println("Error: exception occured..."); 129cdf0e10cSrcweir } catch ( com.sun.star.lang.IllegalArgumentException e ) { 130cdf0e10cSrcweir e.printStackTrace(log); 131cdf0e10cSrcweir log.println("Error: exception occured..."); 132cdf0e10cSrcweir } 133cdf0e10cSrcweir 134cdf0e10cSrcweir try { 135cdf0e10cSrcweir portP = (XPropertySet) 136cdf0e10cSrcweir UnoRuntime.queryInterface(XPropertySet.class, port); 137cdf0e10cSrcweir paraP = (XPropertySet) 138cdf0e10cSrcweir UnoRuntime.queryInterface(XPropertySet.class, para); 139cdf0e10cSrcweir paraP.setPropertyValue("BreakType",com.sun.star.style.BreakType.PAGE_AFTER); 140cdf0e10cSrcweir } catch ( com.sun.star.lang.WrappedTargetException e ) { 141cdf0e10cSrcweir log.println("Error, exception occured..."); 142cdf0e10cSrcweir e.printStackTrace(log); 143cdf0e10cSrcweir throw new StatusException( "Couldn't get Paragraph", e ); 144cdf0e10cSrcweir } catch ( com.sun.star.lang.IllegalArgumentException e ) { 145cdf0e10cSrcweir log.println("Error, exception occured..."); 146cdf0e10cSrcweir e.printStackTrace(log); 147cdf0e10cSrcweir throw new StatusException( "Couldn't get Paragraph", e ); 148cdf0e10cSrcweir } catch ( com.sun.star.beans.UnknownPropertyException e ) { 149cdf0e10cSrcweir log.println("Error, exception occured..."); 150cdf0e10cSrcweir e.printStackTrace(log); 151cdf0e10cSrcweir throw new StatusException( "Couldn't get Paragraph", e ); 152cdf0e10cSrcweir } catch ( com.sun.star.beans.PropertyVetoException e ) { 153cdf0e10cSrcweir log.println("Error, exception occured..."); 154cdf0e10cSrcweir e.printStackTrace(log); 155cdf0e10cSrcweir throw new StatusException( "Couldn't get Paragraph", e ); 156cdf0e10cSrcweir } 157cdf0e10cSrcweir 158cdf0e10cSrcweir shortWait(); 159cdf0e10cSrcweir 160cdf0e10cSrcweir XController xController = xTextDoc.getCurrentController(); 161cdf0e10cSrcweir 162cdf0e10cSrcweir XModel aModel = (XModel) 163cdf0e10cSrcweir UnoRuntime.queryInterface(XModel.class, xTextDoc); 164cdf0e10cSrcweir 165cdf0e10cSrcweir //switch to 'Print Preview' mode 166cdf0e10cSrcweir try { 167cdf0e10cSrcweir XDispatchProvider xDispProv = (XDispatchProvider) 168cdf0e10cSrcweir UnoRuntime.queryInterface(XDispatchProvider.class, xController); 169cdf0e10cSrcweir XURLTransformer xParser = (com.sun.star.util.XURLTransformer) 170cdf0e10cSrcweir UnoRuntime.queryInterface(XURLTransformer.class, 171cdf0e10cSrcweir ((XMultiServiceFactory)Param.getMSF()).createInstance("com.sun.star.util.URLTransformer")); 172cdf0e10cSrcweir // Because it's an in/out parameter we must use an array of URL objects. 173cdf0e10cSrcweir URL[] aParseURL = new URL[1]; 174cdf0e10cSrcweir aParseURL[0] = new URL(); 175cdf0e10cSrcweir aParseURL[0].Complete = ".uno:PrintPreview"; 176cdf0e10cSrcweir xParser.parseStrict(aParseURL); 177cdf0e10cSrcweir URL aURL = aParseURL[0]; 178cdf0e10cSrcweir XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0); 179cdf0e10cSrcweir if(xDispatcher != null) 180cdf0e10cSrcweir xDispatcher.dispatch( aURL, null ); 181cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e) { 182cdf0e10cSrcweir log.println("Couldn't change mode"); 183cdf0e10cSrcweir throw new StatusException(Status.failed("Couldn't change mode")); 184cdf0e10cSrcweir } 185cdf0e10cSrcweir 186cdf0e10cSrcweir shortWait(); 187cdf0e10cSrcweir 188cdf0e10cSrcweir AccessibilityTools at = new AccessibilityTools(); 189cdf0e10cSrcweir 190cdf0e10cSrcweir XWindow xWindow = at.getCurrentWindow((XMultiServiceFactory)Param.getMSF(), aModel); 191cdf0e10cSrcweir XAccessible xRoot = at.getAccessibleObject(xWindow); 192cdf0e10cSrcweir 193cdf0e10cSrcweir at.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE)); 194cdf0e10cSrcweir 195cdf0e10cSrcweir System.out.println("Panel: "+AccessibleRole.PANEL); 196cdf0e10cSrcweir System.out.println("ScrollPane: "+AccessibleRole.SCROLL_PANE); 197cdf0e10cSrcweir oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL,"Page"); 198cdf0e10cSrcweir 199cdf0e10cSrcweir log.println("ImplementationName " + utils.getImplName(oObj)); 200cdf0e10cSrcweir 201cdf0e10cSrcweir 202cdf0e10cSrcweir TestEnvironment tEnv = new TestEnvironment(oObj); 203cdf0e10cSrcweir 204cdf0e10cSrcweir getAccessibleObjectForRole(xRoot, AccessibleRole.SCROLL_BAR); 205cdf0e10cSrcweir final XAccessibleValue xAccVal = (XAccessibleValue) UnoRuntime.queryInterface 206cdf0e10cSrcweir (XAccessibleValue.class, SearchedContext) ; 207cdf0e10cSrcweir 208cdf0e10cSrcweir tEnv.addObjRelation("EventProducer", 209cdf0e10cSrcweir new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() { 210cdf0e10cSrcweir public void fireEvent() { 211cdf0e10cSrcweir Integer old = (Integer) xAccVal.getCurrentValue(); 212cdf0e10cSrcweir Integer newValue = new Integer(old.intValue()+10); 213cdf0e10cSrcweir xAccVal.setCurrentValue(newValue); 214cdf0e10cSrcweir xAccVal.setCurrentValue(old); 215cdf0e10cSrcweir } 216cdf0e10cSrcweir }); 217cdf0e10cSrcweir 218cdf0e10cSrcweir return tEnv; 219cdf0e10cSrcweir 220cdf0e10cSrcweir } 221cdf0e10cSrcweir 222cdf0e10cSrcweir public static boolean first = false; 223cdf0e10cSrcweir public static XAccessibleContext SearchedContext = null; 224cdf0e10cSrcweir getAccessibleObjectForRole(XAccessible xacc,short role)225cdf0e10cSrcweir public static void getAccessibleObjectForRole(XAccessible xacc,short role) { 226cdf0e10cSrcweir XAccessibleContext ac = xacc.getAccessibleContext(); 227cdf0e10cSrcweir if (ac.getAccessibleRole()==role) { 228cdf0e10cSrcweir if (first) SearchedContext = ac; 229cdf0e10cSrcweir else first=true; 230cdf0e10cSrcweir } else { 231cdf0e10cSrcweir int k = ac.getAccessibleChildCount(); 232cdf0e10cSrcweir for (int i=0;i<k;i++) { 233cdf0e10cSrcweir try { 234cdf0e10cSrcweir getAccessibleObjectForRole(ac.getAccessibleChild(i),role); 235cdf0e10cSrcweir if (SearchedContext != null) return ; 236cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 237cdf0e10cSrcweir System.out.println("Couldn't get Child"); 238cdf0e10cSrcweir } 239cdf0e10cSrcweir } 240cdf0e10cSrcweir } 241cdf0e10cSrcweir } 242cdf0e10cSrcweir 243cdf0e10cSrcweir 244cdf0e10cSrcweir /** 245cdf0e10cSrcweir * Called while disposing a <code>TestEnvironment</code>. 246cdf0e10cSrcweir * Disposes text document. 247*e6b649b5SPedro Giffuni * @param Param test parameters 248cdf0e10cSrcweir * @param log writer to log information while testing 249cdf0e10cSrcweir */ cleanup( TestParameters Param, PrintWriter log)250cdf0e10cSrcweir protected void cleanup( TestParameters Param, PrintWriter log) { 251cdf0e10cSrcweir log.println("dispose text document"); 252cdf0e10cSrcweir util.DesktopTools.closeDoc(xTextDoc); 253cdf0e10cSrcweir } 254cdf0e10cSrcweir 255cdf0e10cSrcweir /** 256cdf0e10cSrcweir * Called while the <code>TestCase</code> initialization. In the 257cdf0e10cSrcweir * implementation does nothing. Subclasses can override to initialize 258cdf0e10cSrcweir * objects shared among all <code>TestEnvironment</code>s. 259cdf0e10cSrcweir * 260*e6b649b5SPedro Giffuni * @param Param test parameters 261cdf0e10cSrcweir * @param log writer to log information while testing 262cdf0e10cSrcweir * 2635496b966SPedro Giffuni * @see #initializeTestCase 264cdf0e10cSrcweir */ initialize(TestParameters Param, PrintWriter log)265cdf0e10cSrcweir protected void initialize(TestParameters Param, PrintWriter log) { 266cdf0e10cSrcweir log.println( "creating a text document" ); 267cdf0e10cSrcweir xTextDoc = WriterTools.createTextDoc((XMultiServiceFactory)Param.getMSF()); 268cdf0e10cSrcweir } 269cdf0e10cSrcweir 270cdf0e10cSrcweir /** 271cdf0e10cSrcweir * Sleeps for 0.5 sec. to allow StarOffice to react on <code> 272cdf0e10cSrcweir * reset</code> call. 273cdf0e10cSrcweir */ shortWait()274cdf0e10cSrcweir private void shortWait() { 275cdf0e10cSrcweir try { 276cdf0e10cSrcweir Thread.sleep(500) ; 277cdf0e10cSrcweir } catch (InterruptedException e) { 278cdf0e10cSrcweir log.println("While waiting :" + e) ; 279cdf0e10cSrcweir } 280cdf0e10cSrcweir } 281cdf0e10cSrcweir } 282