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