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 package mod._toolkit;
24 
25 import com.sun.star.accessibility.AccessibleRole;
26 import com.sun.star.accessibility.XAccessible;
27 import com.sun.star.accessibility.XAccessibleAction;
28 import com.sun.star.awt.XWindow;
29 import com.sun.star.frame.XDesktop;
30 import com.sun.star.frame.XModel;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.text.XTextDocument;
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.uno.XInterface;
35 
36 import java.io.PrintWriter;
37 
38 import lib.StatusException;
39 import lib.TestCase;
40 import lib.TestEnvironment;
41 import lib.TestParameters;
42 
43 import util.AccessibilityTools;
44 import util.DesktopTools;
45 import util.SOfficeFactory;
46 
47 /**
48  * Test for object that implements the following interfaces :
49  * <ul>
50  *  <li><code>
51  *  ::com::sun::star::accessibility::XAccessibleContext</code></li>
52  *  <li><code>
53  *  ::com::sun::star::accessibility::XAccessibleEventBroadcaster
54  *  </code></li>
55  *  <li><code>
56  *  ::com::sun::star::accessibility::XAccessibleComponent</code></li>
57  *  <li><code>
58  *  ::com::sun::star::accessibility::XAccessibleExtendedComponent</code></li>
59  * </ul> <p>
60  *
61  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
62  * @see com.sun.star.accessibility.XAccessibleContext
63  * @see com.sun.star.accessibility.XAccessibleComponent
64  * @see com.sun.star.accessibility.XAccessibleExtendedComponent
65  * @see ifc.accessibility._XAccessibleEventBroadcaster
66  * @see ifc.accessibility._XAccessibleContext
67  * @see ifc.accessibility._XAccessibleComponent
68  * @see ifc.accessibility.XAccessibleExtendedComponent
69  */
70 public class AccessibleToolBox extends TestCase {
71     private static XDesktop the_Desk;
72     private static XTextDocument xTextDoc;
73 
74     /**
75      * Creates the Desktop service (<code>com.sun.star.frame.Desktop</code>).
76      */
77     protected void initialize(TestParameters Param, PrintWriter log) {
78         the_Desk = (XDesktop) UnoRuntime.queryInterface(XDesktop.class,
79                 DesktopTools.createDesktop(
80                 (XMultiServiceFactory) Param.getMSF()));
81     }
82 
83     /**
84      * Disposes the document, if exists, created in
85      * <code>createTestEnvironment</code> method.
86      */
87     protected void cleanup(TestParameters Param, PrintWriter log) {
88         log.println("disposing xTextDoc");
89 
90         if (xTextDoc != null) {
91             util.DesktopTools.closeDoc(xTextDoc);
92             ;
93         }
94     }
95 
96     /**
97      * Creates a text document.
98      * Then obtains an accessible object with
99      * the role <code>AccessibleRole.TOOLBAR</code>.
100      * Object relations created :
101      * <ul>
102      *  <li> <code>'EventProducer'</code> for
103      *      {@link ifc.accessibility._XAccessibleEventBroadcaster}:
104      *      grabs focus
105      *   </li>
106      * </ul>
107      *
108      * @param tParam test parameters
109      * @param log writer to log information while testing
110      *
111      * @see com.sun.star.awt.Toolkit
112      * @see com.sun.star.accessibility.AccessibleRole
113      * @see ifc.accessibility._XAccessibleEventBroadcaster
114      * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
115      */
116     protected TestEnvironment createTestEnvironment(TestParameters tParam,
117             PrintWriter log) {
118         log.println("creating a test environment");
119 
120         if (xTextDoc != null) {
121             util.DesktopTools.closeDoc(xTextDoc);
122         }
123 
124         XMultiServiceFactory msf = (XMultiServiceFactory) tParam.getMSF();
125 
126         // get a soffice factory object
127         SOfficeFactory SOF = SOfficeFactory.getFactory(msf);
128 
129         try {
130             log.println("creating a text document");
131             xTextDoc = SOF.createTextDoc(null);
132         } catch (com.sun.star.uno.Exception e) {
133             // Some exception occures.FAILED
134             e.printStackTrace(log);
135             throw new StatusException("Couldn't create document", e);
136         }
137 
138         XInterface oObj = null;
139 
140         XWindow xWindow = UnoRuntime.queryInterface(XModel.class, xTextDoc).
141             getCurrentController().getFrame().getContainerWindow();
142 
143         AccessibilityTools at = new AccessibilityTools();
144 
145         XAccessible xRoot = at.getAccessibleObject(xWindow);
146 
147         at.printAccessibleTree(log, xRoot, tParam.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
148 
149         oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.TOOL_BAR);
150 
151         log.println("ImplementationName: " + util.utils.getImplName(oObj));
152 
153         TestEnvironment tEnv = new TestEnvironment(oObj);
154 
155         tEnv.addObjRelation("LimitedBounds", "yes");
156 
157         XAccessible acc = at.getAccessibleObject(oObj);
158         XAccessible child = null;
159 
160         try {
161             child = acc.getAccessibleContext().getAccessibleChild(0);
162         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
163         }
164 
165         util.dbg.printInterfaces(child);
166 
167         final XAccessibleAction action = (XAccessibleAction) UnoRuntime.queryInterface(
168                 XAccessibleAction.class,
169                 child);
170 
171         tEnv.addObjRelation("EventProducer",
172                 new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
173             public void fireEvent() {
174                 try {
175                     action.doAccessibleAction(0);
176                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
177                     System.out.println("Couldn't fire event");
178                 }
179             }
180         });
181 
182         return tEnv;
183     }
184 }
185