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