Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / SwAccessiblePageView.java
blob84fa8139cf68843390f11bdfa69deba146613e3f
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.text.ControlCharacter;
44 import com.sun.star.text.XText;
45 import com.sun.star.text.XTextCursor;
46 import com.sun.star.text.XTextDocument;
47 import com.sun.star.uno.AnyConverter;
48 import com.sun.star.uno.Type;
49 import com.sun.star.uno.UnoRuntime;
50 import com.sun.star.uno.XInterface;
51 import com.sun.star.util.URL;
52 import com.sun.star.util.XURLTransformer;
54 public class SwAccessiblePageView extends TestCase {
56 XTextDocument xTextDoc = null;
58 /**
59 * Called to create an instance of <code>TestEnvironment</code>
60 * with an object to test and related objects.
61 * Switches the document to Print Preview mode.
62 * Obtains accessible object for the page view.
64 * @param Param test parameters
65 * @param log writer to log information while testing
67 * @see TestEnvironment
68 * @see #getTestEnvironment
70 @Override
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 util.utils.pause(500);
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 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 throw new StatusException(e, Status.failed("Couldn't change mode"));
171 util.utils.pause(500);
173 XWindow xWindow = AccessibilityTools.getCurrentWindow(aModel);
174 XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);
176 AccessibilityTools.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
178 System.out.println("Panel: "+AccessibleRole.PANEL);
179 System.out.println("ScrollPane: "+AccessibleRole.SCROLL_PANE);
180 oObj = AccessibilityTools.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL,"Page");
182 log.println("ImplementationName " + utils.getImplName(oObj));
185 TestEnvironment tEnv = new TestEnvironment(oObj);
187 getAccessibleObjectForRole(xRoot, AccessibleRole.SCROLL_BAR);
188 final XAccessibleValue xAccVal = UnoRuntime.queryInterface
189 (XAccessibleValue.class, SearchedContext) ;
191 tEnv.addObjRelation("EventProducer",
192 new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
193 public void fireEvent() {
194 Integer old = (Integer) xAccVal.getCurrentValue();
195 Integer newValue = Integer.valueOf(old.intValue()+10);
196 xAccVal.setCurrentValue(newValue);
197 xAccVal.setCurrentValue(old);
201 return tEnv;
205 public static boolean first = false;
206 public static XAccessibleContext SearchedContext = null;
208 public static void getAccessibleObjectForRole(XAccessible xacc,short role) {
209 XAccessibleContext ac = xacc.getAccessibleContext();
210 if (ac.getAccessibleRole()==role) {
211 if (first) SearchedContext = ac;
212 else first=true;
213 } else {
214 int k = ac.getAccessibleChildCount();
215 for (int i=0;i<k;i++) {
216 try {
217 getAccessibleObjectForRole(ac.getAccessibleChild(i),role);
218 if (SearchedContext != null) return ;
219 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
220 System.out.println("Couldn't get Child");
228 * Called while disposing a <code>TestEnvironment</code>.
229 * Disposes text document.
230 * @param Param test parameters
231 * @param log writer to log information while testing
233 @Override
234 protected void cleanup( TestParameters Param, PrintWriter log) {
235 log.println("dispose text document");
236 util.DesktopTools.closeDoc(xTextDoc);
240 * Called while the <code>TestCase</code> initialization. In the
241 * implementation does nothing. Subclasses can override to initialize
242 * objects shared among all <code>TestEnvironment</code>s.
244 * @param Param test parameters
245 * @param log writer to log information while testing
247 * @see #initializeTestCase
249 @Override
250 protected void initialize(TestParameters Param, PrintWriter log) {
251 log.println( "creating a text document" );
252 xTextDoc = WriterTools.createTextDoc(Param.getMSF());