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