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.accessibility.XAccessibleContext;
29 import com.sun.star.accessibility.XAccessibleText;
30 import com.sun.star.awt.XWindow;
31 import com.sun.star.frame.XModel;
32 import com.sun.star.lang.XMultiServiceFactory;
33 import com.sun.star.text.XTextDocument;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.uno.XInterface;
36 
37 import java.io.PrintWriter;
38 
39 import lib.StatusException;
40 import lib.TestCase;
41 import lib.TestEnvironment;
42 import lib.TestParameters;
43 
44 import util.AccessibilityTools;
45 import util.SOfficeFactory;
46 import util.utils;
47 
48 
49 /**
50  * Test for object which is represented accessible component of
51  * main menu of a document. <p>
52  *
53  * Object implements the following interfaces :
54  * <ul>
55  *  <li> <code>::com::sun::star::accessibility::XAccessibleEventBroadcaster</code></li>
56  *  <li> <code>::com::sun::star::accessibility::XAccessibleAction</code></li>
57  *  <li> <code>::com::sun::star::accessibility::XAccessibleSelection</code></li>
58  *  <li> <code>::com::sun::star::accessibility::XAccessibleValue</code></li>
59  *  <li> <code>::com::sun::star::accessibility::XAccessibleExtendedComponent</code></li>
60  *  <li> <code>::com::sun::star::accessibility::XAccessibleComponent</code></li>
61  *  <li> <code>::com::sun::star::accessibility::XAccessibleText</code></li>
62  *  <li> <code>::com::sun::star::accessibility::XAccessibleContext</code></li>
63  * </ul> <p>
64  *
65  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
66  * @see com.sun.star.accessibility.XAccessibleAction
67  * @see com.sun.star.accessibility.XAccessibleSelection
68  * @see com.sun.star.accessibility.XAccessibleValue
69  * @see com.sun.star.accessibility.XAccessibleExtendedComponent
70  * @see com.sun.star.accessibility.XAccessibleComponent
71  * @see com.sun.star.accessibility.XAccessibleText
72  * @see com.sun.star.accessibility.XAccessibleContext
73  * @see ifc.accessibility._XAccessibleEventBroadcaster
74  * @see ifc.accessibility._XAccessibleAction
75  * @see ifc.accessibility._XAccessibleSelection
76  * @see ifc.accessibility._XAccessibleValue
77  * @see ifc.accessibility._XAccessibleExtendedComponent
78  * @see ifc.accessibility._XAccessibleComponent
79  * @see ifc.accessibility._XAccessibleText
80  * @see ifc.accessibility._XAccessibleContext
81  */
82 public class AccessibleMenu extends TestCase {
83     private static XTextDocument xTextDoc = null;
84     private static XAccessibleAction action = null;
85 
86     /**
87      * Finds accessible component with role <code>MENU</code>
88      * walking through the accessible component tree of a document.
89      */
createTestEnvironment(TestParameters Param, PrintWriter log)90     protected TestEnvironment createTestEnvironment(TestParameters Param,
91                                                     PrintWriter log) {
92         XMultiServiceFactory msf = (XMultiServiceFactory) Param.getMSF();
93 
94         try {
95             SOfficeFactory SOF = SOfficeFactory.getFactory(msf);
96             xTextDoc = SOF.createTextDoc(null);
97         } catch (com.sun.star.uno.Exception e) {
98             throw new StatusException("Can't create document", e);
99         }
100 
101         shortWait();
102 
103         XWindow xWindow = UnoRuntime.queryInterface(XModel.class, xTextDoc).
104             getCurrentController().getFrame().getContainerWindow();
105 
106         AccessibilityTools at = new AccessibilityTools();
107 
108         XAccessible xRoot = at.getAccessibleObject(xWindow);
109 
110         at.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
111         XAccessibleContext menubar = at.getAccessibleObjectForRole(xRoot,
112                                                                    AccessibleRole.MENU_BAR);
113         XInterface oObj = null;
114         Object menu2 = null;
115 
116         try {
117             oObj = menubar.getAccessibleChild(2);
118             menu2 = menubar.getAccessibleChild(1);
119         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
120         }
121 
122         log.println("ImplementationName " + utils.getImplName(oObj));
123 
124         TestEnvironment tEnv = new TestEnvironment(oObj);
125 
126         final XAccessibleAction act1 = (XAccessibleAction) UnoRuntime.queryInterface(
127                                                XAccessibleAction.class, oObj);
128         final XAccessibleAction act2 = (XAccessibleAction) UnoRuntime.queryInterface(
129                                                XAccessibleAction.class, menu2);
130 
131         tEnv.addObjRelation("EventProducer",
132                             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
133             public void fireEvent() {
134                 try {
135                     act2.doAccessibleAction(0);
136 
137                     try {
138                         Thread.sleep(500);
139                     } catch (InterruptedException e) {
140                     }
141 
142                     act1.doAccessibleAction(0);
143                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
144                 }
145             }
146         });
147 
148         XAccessibleText text = (XAccessibleText) UnoRuntime.queryInterface(
149                                        XAccessibleText.class, oObj);
150 
151         tEnv.addObjRelation("XAccessibleText.Text", text.getText());
152 
153         tEnv.addObjRelation("EditOnly", "Can't change or select Text in Menu");
154 
155         tEnv.addObjRelation("LimitedBounds", "yes");
156 
157         tEnv.addObjRelation("XAccessibleSelection.OneAlwaysSelected",
158                             new Boolean(false));
159 
160         tEnv.addObjRelation("XAccessibleSelection.multiSelection",
161                             new Boolean(false));
162 
163         return tEnv;
164     }
165 
cleanup(TestParameters Param, PrintWriter log)166     protected void cleanup(TestParameters Param, PrintWriter log) {
167         try {
168             util.DesktopTools.closeDoc(xTextDoc);
169         } catch (com.sun.star.lang.DisposedException e) {
170             log.println("Object already disposed");
171         }
172     }
173 
174     /**
175      * Creates a new writer document
176      */
initialize(TestParameters Param, PrintWriter log)177     protected void initialize(TestParameters Param, PrintWriter log) {
178     }
179 
180     /**
181     * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
182     * reset</code> call.
183     */
shortWait()184     private void shortWait() {
185         try {
186             Thread.sleep(500);
187         } catch (InterruptedException e) {
188             log.println("While waiting :" + e);
189         }
190     }
191 }
192 
193