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