1*86250549SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*86250549SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*86250549SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*86250549SAndrew Rist  * distributed with this work for additional information
6*86250549SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*86250549SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*86250549SAndrew Rist  * "License"); you may not use this file except in compliance
9*86250549SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*86250549SAndrew Rist  *
11*86250549SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*86250549SAndrew Rist  *
13*86250549SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*86250549SAndrew Rist  * software distributed under the License is distributed on an
15*86250549SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*86250549SAndrew Rist  * KIND, either express or implied.  See the License for the
17*86250549SAndrew Rist  * specific language governing permissions and limitations
18*86250549SAndrew Rist  * under the License.
19*86250549SAndrew Rist  *
20*86250549SAndrew Rist  *************************************************************/
21*86250549SAndrew Rist 
22*86250549SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package complex.accessibility;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleRole;
27cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible;
28cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleText;
29cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleContext;
30cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleRelationSet;
31cdf0e10cSrcweir import com.sun.star.awt.XWindow;
32cdf0e10cSrcweir import com.sun.star.frame.XModel;
33cdf0e10cSrcweir import com.sun.star.lang.IndexOutOfBoundsException;
34cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
35cdf0e10cSrcweir import com.sun.star.text.ControlCharacter;
36cdf0e10cSrcweir import com.sun.star.text.XText;
37cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
38cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
39cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
40cdf0e10cSrcweir import org.junit.After;
41cdf0e10cSrcweir import org.junit.AfterClass;
42cdf0e10cSrcweir import org.junit.Before;
43cdf0e10cSrcweir import org.junit.BeforeClass;
44cdf0e10cSrcweir import org.junit.Test;
45cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
46cdf0e10cSrcweir import util.AccessibilityTools;
47cdf0e10cSrcweir import util.WriterTools;
48cdf0e10cSrcweir import static org.junit.Assert.*;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir public class AccessibleRelationSet {
51cdf0e10cSrcweir     private XAccessible para1 = null;
52cdf0e10cSrcweir     private XAccessible para2 = null;
53cdf0e10cSrcweir     private XAccessible para3 = null;
54cdf0e10cSrcweir     private XTextDocument xTextDoc = null;
55cdf0e10cSrcweir     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"};
56cdf0e10cSrcweir 
contents_flows_to()57cdf0e10cSrcweir     @Test public void contents_flows_to() {
58cdf0e10cSrcweir         XAccessibleRelationSet set = getAccessibleRelation(para1);
59cdf0e10cSrcweir 
60cdf0e10cSrcweir         short firstrelation=-1;
61cdf0e10cSrcweir         XAccessibleText atarget=null;
62cdf0e10cSrcweir         if (set != null) {
63cdf0e10cSrcweir             assertEquals(
64cdf0e10cSrcweir                 "didn't gain correct count of relations", 1,
65cdf0e10cSrcweir                 set.getRelationCount());
66cdf0e10cSrcweir             try {
67cdf0e10cSrcweir                 firstrelation = set.getRelation(0).RelationType;
68cdf0e10cSrcweir                 Object oTmp = set.getRelation(0).TargetSet[0];
69cdf0e10cSrcweir                 atarget = (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, oTmp);
70cdf0e10cSrcweir             } catch (IndexOutOfBoundsException e) {
71cdf0e10cSrcweir                 fail("Exception when getting relations "+e);
72cdf0e10cSrcweir             }
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         assertEquals(
76cdf0e10cSrcweir             "didn't gain correct relation type for paragraph 0", types[2],
77cdf0e10cSrcweir             types[firstrelation]);
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         XAccessibleText paraTxt2 =
80cdf0e10cSrcweir           (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, para2);
81cdf0e10cSrcweir         assertEquals(
82cdf0e10cSrcweir             "didn't gain correct target paragraph", atarget.getText(),
83cdf0e10cSrcweir             paraTxt2.getText());
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir 
contents_flows_from()86cdf0e10cSrcweir     @Test public void contents_flows_from() {
87cdf0e10cSrcweir         XAccessibleRelationSet set = getAccessibleRelation(para2);
88cdf0e10cSrcweir 
89cdf0e10cSrcweir         short[] relationtypes = new short[2];
90cdf0e10cSrcweir         XAccessibleText[] atargets = new XAccessibleText[2];
91cdf0e10cSrcweir         if (set != null) {
92cdf0e10cSrcweir             assertEquals(
93cdf0e10cSrcweir                 "didn't gain correct count of relations", 2,
94cdf0e10cSrcweir                 set.getRelationCount());
95cdf0e10cSrcweir             try {
96cdf0e10cSrcweir                 short tmprelation = set.getRelation(0).RelationType;
97cdf0e10cSrcweir                 if ( tmprelation == 1 )
98cdf0e10cSrcweir                 {
99cdf0e10cSrcweir                   Object oTmp = set.getRelation(0).TargetSet[0];
100cdf0e10cSrcweir                   atargets[0] = (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, oTmp);
101cdf0e10cSrcweir                   relationtypes[0] = tmprelation;
102cdf0e10cSrcweir                 }
103cdf0e10cSrcweir                 else if ( tmprelation == 2 )
104cdf0e10cSrcweir                 {
105cdf0e10cSrcweir                   Object oTmp = set.getRelation(0).TargetSet[0];
106cdf0e10cSrcweir                   atargets[1] = (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, oTmp);
107cdf0e10cSrcweir                   relationtypes[1] = tmprelation;
108cdf0e10cSrcweir                 }
109cdf0e10cSrcweir                 else
110cdf0e10cSrcweir                 {
111cdf0e10cSrcweir                     fail("didn't gain correct relation type");
112cdf0e10cSrcweir                 }
113cdf0e10cSrcweir                 tmprelation = set.getRelation(1).RelationType;
114cdf0e10cSrcweir                 if ( tmprelation == 1 )
115cdf0e10cSrcweir                 {
116cdf0e10cSrcweir                   Object oTmp = set.getRelation(1).TargetSet[0];
117cdf0e10cSrcweir                   atargets[0] = (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, oTmp);
118cdf0e10cSrcweir                   relationtypes[0] = tmprelation;
119cdf0e10cSrcweir                 }
120cdf0e10cSrcweir                 else if ( tmprelation == 2 )
121cdf0e10cSrcweir                 {
122cdf0e10cSrcweir                   Object oTmp = set.getRelation(1).TargetSet[0];
123cdf0e10cSrcweir                   atargets[1] = (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, oTmp);
124cdf0e10cSrcweir                   relationtypes[1] = tmprelation;
125cdf0e10cSrcweir                 }
126cdf0e10cSrcweir                 else
127cdf0e10cSrcweir                 {
128cdf0e10cSrcweir                     fail("didn't gain correct relation type");
129cdf0e10cSrcweir                 }
130cdf0e10cSrcweir             } catch (IndexOutOfBoundsException e) {
131cdf0e10cSrcweir                 fail("Exception when getting relations "+e);
132cdf0e10cSrcweir             }
133cdf0e10cSrcweir         }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         assertEquals(
136cdf0e10cSrcweir             "didn't gain correct relation type for paragraph 1", types[1],
137cdf0e10cSrcweir             types[relationtypes[0]]);
138cdf0e10cSrcweir 
139cdf0e10cSrcweir         XAccessibleText paraTxt1 =
140cdf0e10cSrcweir           (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, para1);
141cdf0e10cSrcweir         assertEquals(
142cdf0e10cSrcweir             "didn't gain correct target paragraph", atargets[0].getText(),
143cdf0e10cSrcweir             paraTxt1.getText());
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         assertEquals(
146cdf0e10cSrcweir             "didn't gain correct relation type for paragraph 3", types[2],
147cdf0e10cSrcweir             types[relationtypes[1]]);
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         XAccessibleText paraTxt3 =
150cdf0e10cSrcweir           (XAccessibleText) UnoRuntime.queryInterface(XAccessibleText.class, para3);
151cdf0e10cSrcweir         assertEquals(
152cdf0e10cSrcweir             "didn't gain correct target paragraph", atargets[1].getText(),
153cdf0e10cSrcweir             paraTxt3.getText());
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir 
before()156cdf0e10cSrcweir     @Before public void before()
157cdf0e10cSrcweir         throws com.sun.star.lang.IllegalArgumentException,
158cdf0e10cSrcweir         IndexOutOfBoundsException
159cdf0e10cSrcweir     {
160cdf0e10cSrcweir         XMultiServiceFactory factory = UnoRuntime.queryInterface(
161cdf0e10cSrcweir             XMultiServiceFactory.class,
162cdf0e10cSrcweir             connection.getComponentContext().getServiceManager());
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         xTextDoc = WriterTools.createTextDoc(factory);
165cdf0e10cSrcweir 
166cdf0e10cSrcweir         XText oText = xTextDoc.getText();
167cdf0e10cSrcweir         XTextCursor oCursor = oText.createTextCursor();
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         for (int i=0; i<5; i++){
170cdf0e10cSrcweir             oText.insertString( oCursor,"Paragraph Number: " + i, false);
171cdf0e10cSrcweir             oText.insertControlCharacter(
172cdf0e10cSrcweir                 oCursor, ControlCharacter.PARAGRAPH_BREAK, false );
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir         XModel aModel = (XModel)
176cdf0e10cSrcweir         UnoRuntime.queryInterface(XModel.class, xTextDoc);
177cdf0e10cSrcweir 
178cdf0e10cSrcweir         AccessibilityTools at = new AccessibilityTools();
179cdf0e10cSrcweir 
180cdf0e10cSrcweir         XWindow xWindow = at.getCurrentWindow(factory, aModel);
181cdf0e10cSrcweir         XAccessible xRoot = at.getAccessibleObject(xWindow);
182cdf0e10cSrcweir 
183cdf0e10cSrcweir         at.getAccessibleObjectForRole(xRoot, AccessibleRole.DOCUMENT);
184cdf0e10cSrcweir 
185cdf0e10cSrcweir         para1 = at.SearchedContext.getAccessibleChild(0);
186cdf0e10cSrcweir         para2 = at.SearchedContext.getAccessibleChild(1);
187cdf0e10cSrcweir         para3 = at.SearchedContext.getAccessibleChild(2);
188cdf0e10cSrcweir     }
189cdf0e10cSrcweir 
after()190cdf0e10cSrcweir     @After public void after() {
191cdf0e10cSrcweir         util.DesktopTools.closeDoc(xTextDoc);
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
getAccessibleRelation(XAccessible xAcc)194cdf0e10cSrcweir     public XAccessibleRelationSet getAccessibleRelation(XAccessible xAcc) {
195cdf0e10cSrcweir         XAccessibleContext oObj = (XAccessibleContext)
196cdf0e10cSrcweir         UnoRuntime.queryInterface(XAccessibleContext.class, xAcc);
197cdf0e10cSrcweir 
198cdf0e10cSrcweir         XAccessibleRelationSet set = oObj.getAccessibleRelationSet();
199cdf0e10cSrcweir         return set;
200cdf0e10cSrcweir     }
201cdf0e10cSrcweir 
setUpConnection()202cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
203cdf0e10cSrcweir         connection.setUp();
204cdf0e10cSrcweir     }
205cdf0e10cSrcweir 
tearDownConnection()206cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
207cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
208cdf0e10cSrcweir     {
209cdf0e10cSrcweir         connection.tearDown();
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
213cdf0e10cSrcweir }
214