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.toolkit.accessibility;
25 
26 import com.sun.star.lang.Locale;
27 import com.sun.star.uno.XInterface;
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.accessibility.IllegalAccessibleComponentStateException;
30 import com.sun.star.accessibility.XAccessible;
31 import com.sun.star.accessibility.XAccessibleContext;
32 import com.sun.star.accessibility.XAccessibleRelationSet;
33 import com.sun.star.accessibility.XAccessibleStateSet;
34 // import com.sun.star.accessibility.AccessibleRelationType;
35 import util.AccessibilityTools;
36 // import share.LogWriter;
37 
38 /**
39  * Testing <code>com.sun.star.accessibility.XAccessibleContext</code>
40  * interface methods :
41  * <ul>
42  *  <li><code> getAccessibleChildCount()</code></li>
43  *  <li><code> getAccessibleChild()</code></li>
44  *  <li><code> getAccessibleParent()</code></li>
45  *  <li><code> getAccessibleIndexInParent()</code></li>
46  *  <li><code> getAccessibleRole()</code></li>
47  *  <li><code> getAccessibleDescription()</code></li>
48  *  <li><code> getAccessibleName()</code></li>
49  *  <li><code> getAccessibleRelationSet()</code></li>
50  *  <li><code> getAccessibleStateSet()</code></li>
51  *  <li><code> getLocale()</code></li>
52  * </ul> <p>
53  *
54  * @see com.sun.star.accessibility.XAccessibleContext
55  */
56 public class _XAccessibleContext {
57 
58     // private LogWriter log;
59 
60     private static final String className =
61         "com.sun.star.accessibility.XAccessibleContext" ;
62 
63     public XAccessibleContext oObj = null;
64 
65     private int childCount = 0;
66     private XAccessible parent = null ;
67 
_XAccessibleContext(XInterface object)68     public _XAccessibleContext(XInterface object) {
69         oObj = UnoRuntime.queryInterface(XAccessibleContext.class, object);
70         // this.log = log;
71     }
72 
73     /**
74      * Calls the method and stores the number of children. <p>
75      * Has <b> OK </b> status if non-negative number rutrned.
76      * @return
77      */
_getAccessibleChildCount()78     public boolean _getAccessibleChildCount() {
79         childCount = oObj.getAccessibleChildCount();
80         System.out.println("" + childCount + " children found.");
81         return childCount > -1;
82     }
83 
84     /**
85      * Tries to get every child and checks its parent. <p>
86      *
87      * Has <b> OK </b> status if parent of every child
88      * and the tested component are the same objects.
89      *
90      * The following method tests are to be completed successfully before :
91      * <ul>
92      *  <li> <code> getAccessibleChildCount() </code> : to have a number of
93      *     children </li>
94      * </ul>
95      * @return
96      */
_getAccessibleChild()97     public boolean _getAccessibleChild() {
98         boolean bOK = true;
99         int counter = childCount;
100         if (childCount > 500)
101         {
102             counter = 500;
103         }
104         for (int i = 0; i < counter; i++) {
105             try {
106                 XAccessible ch = oObj.getAccessibleChild(i) ;
107                 XAccessibleContext chAC = ch.getAccessibleContext();
108 
109                 System.out.println("  Child " + i + ": " +
110                     chAC.getAccessibleDescription()) ;
111 
112                 if (!AccessibilityTools.equals
113                     (chAC.getAccessibleParent().getAccessibleContext(), oObj)){
114 
115                     System.out.println("Role:");
116                     System.out.println("Getting: "+chAC.getAccessibleParent().getAccessibleContext().getAccessibleRole());
117                     System.out.println("Expected: "+oObj.getAccessibleRole());
118 
119                     System.out.println("ImplementationName:");
120                     System.out.println("Getting: "+util.utils.getImplName(chAC.getAccessibleParent().getAccessibleContext()));
121                     System.out.println("Expected: "+util.utils.getImplName(oObj));
122 
123                     System.out.println("The parent of child and component "+
124                         "itself differ.");
125                     System.out.println("Getting(Description): "
126                             +chAC.getAccessibleParent().getAccessibleContext().getAccessibleDescription());
127                     System.out.println("Expected(Description): "
128                             +oObj.getAccessibleDescription());
129 
130                     bOK = false;
131                 } else {
132                     System.out.println("Getting the expected Child -- OK");
133                 }
134             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
135                 e.printStackTrace();
136                 bOK = false;
137             }
138         }
139 
140         return bOK;
141     }
142 
143     /**
144      * Just gets the parent. <p>
145      *
146      * Has <b> OK </b> status if parent is not null.
147      * @return
148      */
_getAccessibleParent()149     public boolean _getAccessibleParent() {
150         // assume that the component is not ROOT
151         parent = oObj.getAccessibleParent();
152         return parent != null;
153     }
154 
155     /**
156      * Retrieves the index of tested component in its parent.
157      * Then gets the parent's child by this index and compares
158      * it with tested component.<p>
159      *
160      * Has <b> OK </b> status if the parent's child and the tested
161      * component are the same objects.
162      *
163      * The following method tests are to be completed successfully before :
164      * <ul>
165      *  <li> <code> getAccessibleParent() </code> : to have a parent </li>
166      * </ul>
167      * @return
168      */
_getAccessibleIndexInParent()169     public boolean _getAccessibleIndexInParent() {
170 
171         boolean bOK = true;
172         int idx = oObj.getAccessibleIndexInParent();
173 
174         XAccessibleContext parentAC = parent.getAccessibleContext() ;
175         try {
176             bOK &= AccessibilityTools.equals(
177                 parentAC.getAccessibleChild(idx).getAccessibleContext(),oObj);
178             if (!bOK) {
179                 System.out.println("Expected: "+util.utils.getImplName(oObj));
180                 System.out.println("Getting: "+util.utils.getImplName(
181                     parentAC.getAccessibleChild(idx).getAccessibleContext()));
182             }
183         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
184             e.printStackTrace();
185             bOK = false;
186         }
187         return bOK;
188     }
189 
190     /**
191      * Get the accessible role of component. <p>
192      *
193      * Has <b> OK </b> status if non-negative number rutrned.
194      * @return
195      */
_getAccessibleRole()196     public boolean _getAccessibleRole() {
197         short role = oObj.getAccessibleRole();
198         System.out.println("The role is " + role);
199         return role > -1;
200     }
201 
202     /**
203      * Get the accessible name of the component. <p>
204      *
205      * Has <b> OK </b> status if the name has non-zero length.
206      * @return
207      */
_getAccessibleName()208     public boolean _getAccessibleName() {
209         String name = oObj.getAccessibleName();
210         System.out.println("The name is '" + name + "'");
211         return name != null;
212     }
213 
214     /**
215      * Get the accessible description of the component. <p>
216      *
217      * Has <b> OK </b> status if the description has non-zero length.
218      * @return
219      */
_getAccessibleDescription()220     public boolean _getAccessibleDescription() {
221         String descr = oObj.getAccessibleDescription();
222         System.out.println("The description is '" + descr + "'");
223         return descr != null;
224     }
225 
226     /**
227      * Just gets the set. <p>
228      *
229      * Has <b> OK </b> status if the set is not null.
230      * @return
231      */
_getAccessibleRelationSet()232     public boolean _getAccessibleRelationSet() {
233         XAccessibleRelationSet set = oObj.getAccessibleRelationSet();
234         return set != null;
235     }
236 
237     /**
238      * Just gets the set. <p>
239      *
240      * Has <b> OK </b> status if the set is not null.
241      * @return
242      */
_getAccessibleStateSet()243     public boolean _getAccessibleStateSet() {
244         XAccessibleStateSet set = oObj.getAccessibleStateSet();
245         return set != null;
246     }
247 
248     /**
249      * Gets the locale. <p>
250      *
251      * Has <b> OK </b> status if <code>Country</code> and
252      * <code>Language</code> fields of locale structure
253      * are not empty.
254      * @return
255      */
_getLocale()256     public boolean _getLocale() {
257         Locale loc = null ;
258         try {
259             loc = oObj.getLocale();
260             System.out.println("The locale is " + loc.Language + "," + loc.Country);
261         } catch (IllegalAccessibleComponentStateException e) {
262             e.printStackTrace();
263         }
264 
265         return loc != null && loc.Language.length() > 0 &&
266                                             loc.Country.length() > 0;
267     }
268 }
269 
270