merge the formfield patch from ooo-build
[ooovba.git] / sw / qa / complex / accessibility / AccessibleRelationSet.java
blob17e7a635bb90f36542f8ef8c55fafb82d5c2af1f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: AccessibleRelationSet.java,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package complex.accessibility;
33 import com.sun.star.accessibility.AccessibleRelation;
34 import com.sun.star.accessibility.AccessibleRole;
35 import com.sun.star.accessibility.XAccessible;
36 import com.sun.star.accessibility.XAccessibleText;
37 import com.sun.star.accessibility.XAccessibleContext;
38 import com.sun.star.accessibility.XAccessibleRelationSet;
39 import com.sun.star.awt.XWindow;
40 import com.sun.star.frame.XModel;
41 import com.sun.star.lang.IndexOutOfBoundsException;
42 import com.sun.star.lang.XMultiServiceFactory;
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.UnoRuntime;
48 import com.sun.star.uno.XInterface;
49 import complexlib.ComplexTestCase;
50 import java.io.PrintWriter;
51 import lib.StatusException;
52 import util.AccessibilityTools;
53 import util.WriterTools;
54 import util.utils;
56 public class AccessibleRelationSet extends ComplexTestCase {
58 private static XAccessible para1 = null;
59 private static XAccessible para2 = null;
60 private static XAccessible para3 = null;
61 private static XTextDocument xTextDoc = null;
62 private final static String[] types = {"INVALID","CONTENT_FLOWS_FROM","CONTENT_FLOWS_TO","CONTROLLED_BY","CONTROLLER_FOR","LABEL_FOR","LABELED_BY","MEMBER_OF","SUB_WINDOW_OF"};
64 public String[] getTestMethodNames() {
65 return new String[]{"contents_flows_to","contents_flows_from"};
68 public void contents_flows_to() {
69 XAccessibleRelationSet set = getAccessibleRelation(para1);
71 boolean res = true;
72 short firstrelation=-1;
73 XAccessibleText atarget=null;
74 if (set != null) {
75 log.println("Count of relations "+set.getRelationCount());
76 assure("didn't gain correct count of relations",
77 set.getRelationCount() == 1);
78 try {
79 firstrelation = set.getRelation(0).RelationType;
80 Object oTmp = set.getRelation(0).TargetSet[0];
81 atarget = (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, oTmp);
82 } catch (IndexOutOfBoundsException e) {
83 log.println("Exception when getting relations "+e);
84 res = false;
88 log.println("Expected for paragraph 0 "+types[2]);
89 log.println("gained for paragraph 0 "+types[firstrelation]);
90 res = types[2].equals(types[firstrelation]);
91 assure("didn't gain correct relation type",res);
93 log.println("Text of target paragraph "+atarget.getText());
94 XAccessibleText paraTxt2 =
95 (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, para2);
96 assure("didn't gain correct target paragraph",
97 atarget.getText().equals(paraTxt2.getText()) );
100 public void contents_flows_from() {
101 XAccessibleRelationSet set = getAccessibleRelation(para2);
103 boolean res = true;
104 short[] relationtypes = new short[2];
105 XAccessibleText[] atargets = new XAccessibleText[2];
106 if (set != null) {
107 log.println("Count of relations "+set.getRelationCount());
108 assure("didn't gain correct count of relations",
109 set.getRelationCount() == 2);
110 try {
111 short tmprelation = set.getRelation(0).RelationType;
112 if ( tmprelation == 1 )
114 Object oTmp = set.getRelation(0).TargetSet[0];
115 atargets[0] = (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, oTmp);
116 relationtypes[0] = tmprelation;
118 else if ( tmprelation == 2 )
120 Object oTmp = set.getRelation(0).TargetSet[0];
121 atargets[1] = (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, oTmp);
122 relationtypes[1] = tmprelation;
124 else
126 assure("didn't gain correct relation type", false);
128 tmprelation = set.getRelation(1).RelationType;
129 if ( tmprelation == 1 )
131 Object oTmp = set.getRelation(1).TargetSet[0];
132 atargets[0] = (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, oTmp);
133 relationtypes[0] = tmprelation;
135 else if ( tmprelation == 2 )
137 Object oTmp = set.getRelation(1).TargetSet[0];
138 atargets[1] = (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, oTmp);
139 relationtypes[1] = tmprelation;
141 else
143 assure("didn't gain correct relation type", false);
145 } catch (IndexOutOfBoundsException e) {
146 log.println("Exception when getting relations "+e);
147 res = false;
151 log.println("### Checking "+types[1]+" for paragraph 1");
152 log.println("Expected for paragraph 1 "+types[1]);
153 log.println("gained for paragraph 1 "+types[relationtypes[0]]);
154 res = types[1].equals(types[relationtypes[0]]);
155 assure("didn't gain correct relation type",res);
157 log.println("Text of target paragraph "+atargets[0].getText());
158 XAccessibleText paraTxt1 =
159 (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, para1);
160 assure("didn't gain correct target paragraph",
161 atargets[0].getText().equals(paraTxt1.getText()) );
163 log.println("### Checking "+types[2]+" for paragraph 1");
164 log.println("Expected for paragraph 1 "+types[2]);
165 log.println("gained for paragraph 1 "+types[relationtypes[1]]);
166 res = types[2].equals(types[relationtypes[1]]);
167 assure("didn't gain correct relation type",res);
169 log.println("Text of target paragraph "+atargets[1].getText());
170 XAccessibleText paraTxt3 =
171 (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, para3);
172 assure("didn't gain correct target paragraph",
173 atargets[1].getText().equals(paraTxt3.getText()) );
176 private boolean getResult(XAccessible aPara, short index, int nr) {
177 XAccessibleRelationSet set = getAccessibleRelation(aPara);
179 boolean res = true;
180 short firstrelation=-1;
181 if (set != null) {
182 log.println("Count of relations "+set.getRelationCount());
183 try {
184 firstrelation = set.getRelation(0).RelationType;
185 } catch (IndexOutOfBoundsException e) {
186 log.println("Exception when getting relations "+e);
187 res = false;
192 log.println("Expected for paragraph "+nr+" "+types[index]);
193 log.println("gained for paragraph "+nr+" "+types[firstrelation]);
194 res = types[index].equals(types[firstrelation]);
195 return res;
199 public void before() {
200 log.println( "creating a text document" );
201 xTextDoc = WriterTools.createTextDoc( (XMultiServiceFactory) param.getMSF());
203 XText oText = xTextDoc.getText();
204 XTextCursor oCursor = oText.createTextCursor();
206 log.println( "inserting some lines" );
207 try {
208 for (int i=0; i<5; i++){
209 oText.insertString( oCursor,"Paragraph Number: " + i, false);
210 oText.insertControlCharacter(
211 oCursor, ControlCharacter.PARAGRAPH_BREAK, false );
213 } catch ( com.sun.star.lang.IllegalArgumentException e ){
214 e.printStackTrace((PrintWriter)log);
215 throw new StatusException( "Couldn't insert lines", e );
218 XModel aModel = (XModel)
219 UnoRuntime.queryInterface(XModel.class, xTextDoc);
221 AccessibilityTools at = new AccessibilityTools();
223 XWindow xWindow = at.getCurrentWindow( (XMultiServiceFactory) param.getMSF(), aModel);
224 XAccessible xRoot = at.getAccessibleObject(xWindow);
226 at.getAccessibleObjectForRole(xRoot, AccessibleRole.DOCUMENT);
228 try {
229 para1 = at.SearchedContext.getAccessibleChild(0);
230 para2 = at.SearchedContext.getAccessibleChild(1);
231 para3 = at.SearchedContext.getAccessibleChild(2);
232 } catch(IndexOutOfBoundsException e) {
233 e.printStackTrace((PrintWriter)log);
234 throw new StatusException( "Couldn't insert lines", e );
237 log.println("ImplementationName (para1)" + utils.getImplName(para1));
238 log.println("ImplementationName (para2)" + utils.getImplName(para2));
241 public void after() {
242 log.println("close text document");
243 util.DesktopTools.closeDoc(xTextDoc);
246 public XAccessibleRelationSet getAccessibleRelation(XAccessible xAcc) {
247 XAccessibleContext oObj = (XAccessibleContext)
248 UnoRuntime.queryInterface(XAccessibleContext.class, xAcc);
250 XAccessibleRelationSet set = oObj.getAccessibleRelationSet();
251 return set;