xref: /AOO41X/main/sw/qa/complex/accessibility/AccessibleRelationSet.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package complex.accessibility;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleRole;
31*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible;
32*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleText;
33*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleContext;
34*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleRelationSet;
35*cdf0e10cSrcweir import com.sun.star.awt.XWindow;
36*cdf0e10cSrcweir import com.sun.star.frame.XModel;
37*cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException;
38*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
39*cdf0e10cSrcweir import com.sun.star.text.ControlCharacter;
40*cdf0e10cSrcweir import com.sun.star.text.XText;
41*cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
42*cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
43*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
44*cdf0e10cSrcweir import org.junit.After;
45*cdf0e10cSrcweir import org.junit.AfterClass;
46*cdf0e10cSrcweir import org.junit.Before;
47*cdf0e10cSrcweir import org.junit.BeforeClass;
48*cdf0e10cSrcweir import org.junit.Test;
49*cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
50*cdf0e10cSrcweir import util.AccessibilityTools;
51*cdf0e10cSrcweir import util.WriterTools;
52*cdf0e10cSrcweir import static org.junit.Assert.*;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir public class AccessibleRelationSet {
55*cdf0e10cSrcweir     private XAccessible para1 = null;
56*cdf0e10cSrcweir     private XAccessible para2 = null;
57*cdf0e10cSrcweir     private XAccessible para3 = null;
58*cdf0e10cSrcweir     private XTextDocument xTextDoc = null;
59*cdf0e10cSrcweir     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"};
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir     @Test public void contents_flows_to() {
62*cdf0e10cSrcweir         XAccessibleRelationSet set = getAccessibleRelation(para1);
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir         short firstrelation=-1;
65*cdf0e10cSrcweir         XAccessibleText atarget=null;
66*cdf0e10cSrcweir         if (set != null) {
67*cdf0e10cSrcweir             assertEquals(
68*cdf0e10cSrcweir                 "didn't gain correct count of relations", 1,
69*cdf0e10cSrcweir                 set.getRelationCount());
70*cdf0e10cSrcweir             try {
71*cdf0e10cSrcweir                 firstrelation = set.getRelation(0).RelationType;
72*cdf0e10cSrcweir                 Object oTmp = set.getRelation(0).TargetSet[0];
73*cdf0e10cSrcweir                 atarget = (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, oTmp);
74*cdf0e10cSrcweir             } catch (IndexOutOfBoundsException e) {
75*cdf0e10cSrcweir                 fail("Exception when getting relations "+e);
76*cdf0e10cSrcweir             }
77*cdf0e10cSrcweir         }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir         assertEquals(
80*cdf0e10cSrcweir             "didn't gain correct relation type for paragraph 0", types[2],
81*cdf0e10cSrcweir             types[firstrelation]);
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir         XAccessibleText paraTxt2 =
84*cdf0e10cSrcweir           (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, para2);
85*cdf0e10cSrcweir         assertEquals(
86*cdf0e10cSrcweir             "didn't gain correct target paragraph", atarget.getText(),
87*cdf0e10cSrcweir             paraTxt2.getText());
88*cdf0e10cSrcweir     }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     @Test public void contents_flows_from() {
91*cdf0e10cSrcweir         XAccessibleRelationSet set = getAccessibleRelation(para2);
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir         short[] relationtypes = new short[2];
94*cdf0e10cSrcweir         XAccessibleText[] atargets = new XAccessibleText[2];
95*cdf0e10cSrcweir         if (set != null) {
96*cdf0e10cSrcweir             assertEquals(
97*cdf0e10cSrcweir                 "didn't gain correct count of relations", 2,
98*cdf0e10cSrcweir                 set.getRelationCount());
99*cdf0e10cSrcweir             try {
100*cdf0e10cSrcweir                 short tmprelation = set.getRelation(0).RelationType;
101*cdf0e10cSrcweir                 if ( tmprelation == 1 )
102*cdf0e10cSrcweir                 {
103*cdf0e10cSrcweir                   Object oTmp = set.getRelation(0).TargetSet[0];
104*cdf0e10cSrcweir                   atargets[0] = (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, oTmp);
105*cdf0e10cSrcweir                   relationtypes[0] = tmprelation;
106*cdf0e10cSrcweir                 }
107*cdf0e10cSrcweir                 else if ( tmprelation == 2 )
108*cdf0e10cSrcweir                 {
109*cdf0e10cSrcweir                   Object oTmp = set.getRelation(0).TargetSet[0];
110*cdf0e10cSrcweir                   atargets[1] = (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, oTmp);
111*cdf0e10cSrcweir                   relationtypes[1] = tmprelation;
112*cdf0e10cSrcweir                 }
113*cdf0e10cSrcweir                 else
114*cdf0e10cSrcweir                 {
115*cdf0e10cSrcweir                     fail("didn't gain correct relation type");
116*cdf0e10cSrcweir                 }
117*cdf0e10cSrcweir                 tmprelation = set.getRelation(1).RelationType;
118*cdf0e10cSrcweir                 if ( tmprelation == 1 )
119*cdf0e10cSrcweir                 {
120*cdf0e10cSrcweir                   Object oTmp = set.getRelation(1).TargetSet[0];
121*cdf0e10cSrcweir                   atargets[0] = (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, oTmp);
122*cdf0e10cSrcweir                   relationtypes[0] = tmprelation;
123*cdf0e10cSrcweir                 }
124*cdf0e10cSrcweir                 else if ( tmprelation == 2 )
125*cdf0e10cSrcweir                 {
126*cdf0e10cSrcweir                   Object oTmp = set.getRelation(1).TargetSet[0];
127*cdf0e10cSrcweir                   atargets[1] = (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, oTmp);
128*cdf0e10cSrcweir                   relationtypes[1] = tmprelation;
129*cdf0e10cSrcweir                 }
130*cdf0e10cSrcweir                 else
131*cdf0e10cSrcweir                 {
132*cdf0e10cSrcweir                     fail("didn't gain correct relation type");
133*cdf0e10cSrcweir                 }
134*cdf0e10cSrcweir             } catch (IndexOutOfBoundsException e) {
135*cdf0e10cSrcweir                 fail("Exception when getting relations "+e);
136*cdf0e10cSrcweir             }
137*cdf0e10cSrcweir         }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir         assertEquals(
140*cdf0e10cSrcweir             "didn't gain correct relation type for paragraph 1", types[1],
141*cdf0e10cSrcweir             types[relationtypes[0]]);
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir         XAccessibleText paraTxt1 =
144*cdf0e10cSrcweir           (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, para1);
145*cdf0e10cSrcweir         assertEquals(
146*cdf0e10cSrcweir             "didn't gain correct target paragraph", atargets[0].getText(),
147*cdf0e10cSrcweir             paraTxt1.getText());
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         assertEquals(
150*cdf0e10cSrcweir             "didn't gain correct relation type for paragraph 3", types[2],
151*cdf0e10cSrcweir             types[relationtypes[1]]);
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir         XAccessibleText paraTxt3 =
154*cdf0e10cSrcweir           (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, para3);
155*cdf0e10cSrcweir         assertEquals(
156*cdf0e10cSrcweir             "didn't gain correct target paragraph", atargets[1].getText(),
157*cdf0e10cSrcweir             paraTxt3.getText());
158*cdf0e10cSrcweir     }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     @Before public void before()
161*cdf0e10cSrcweir         throws com.sun.star.lang.IllegalArgumentException,
162*cdf0e10cSrcweir         IndexOutOfBoundsException
163*cdf0e10cSrcweir     {
164*cdf0e10cSrcweir         XMultiServiceFactory factory = UnoRuntime.queryInterface(
165*cdf0e10cSrcweir             XMultiServiceFactory.class,
166*cdf0e10cSrcweir             connection.getComponentContext().getServiceManager());
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir         xTextDoc = WriterTools.createTextDoc(factory);
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir         XText oText = xTextDoc.getText();
171*cdf0e10cSrcweir         XTextCursor oCursor = oText.createTextCursor();
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir         for (int i=0; i<5; i++){
174*cdf0e10cSrcweir             oText.insertString( oCursor,"Paragraph Number: " + i, false);
175*cdf0e10cSrcweir             oText.insertControlCharacter(
176*cdf0e10cSrcweir                 oCursor, ControlCharacter.PARAGRAPH_BREAK, false );
177*cdf0e10cSrcweir         }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir         XModel aModel = (XModel)
180*cdf0e10cSrcweir         UnoRuntime.queryInterface(XModel.class, xTextDoc);
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir         AccessibilityTools at = new AccessibilityTools();
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir         XWindow xWindow = at.getCurrentWindow(factory, aModel);
185*cdf0e10cSrcweir         XAccessible xRoot = at.getAccessibleObject(xWindow);
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir         at.getAccessibleObjectForRole(xRoot, AccessibleRole.DOCUMENT);
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir         para1 = at.SearchedContext.getAccessibleChild(0);
190*cdf0e10cSrcweir         para2 = at.SearchedContext.getAccessibleChild(1);
191*cdf0e10cSrcweir         para3 = at.SearchedContext.getAccessibleChild(2);
192*cdf0e10cSrcweir     }
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir     @After public void after() {
195*cdf0e10cSrcweir         util.DesktopTools.closeDoc(xTextDoc);
196*cdf0e10cSrcweir     }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir     public XAccessibleRelationSet getAccessibleRelation(XAccessible xAcc) {
199*cdf0e10cSrcweir         XAccessibleContext oObj = (XAccessibleContext)
200*cdf0e10cSrcweir         UnoRuntime.queryInterface(XAccessibleContext.class, xAcc);
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir         XAccessibleRelationSet set = oObj.getAccessibleRelationSet();
203*cdf0e10cSrcweir         return set;
204*cdf0e10cSrcweir     }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
207*cdf0e10cSrcweir         connection.setUp();
208*cdf0e10cSrcweir     }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
211*cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
212*cdf0e10cSrcweir     {
213*cdf0e10cSrcweir         connection.tearDown();
214*cdf0e10cSrcweir     }
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
217*cdf0e10cSrcweir }
218