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