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.XExtendedToolkit;
31 import com.sun.star.awt.XWindow;
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 by accessible component
51  * of a menu item in main menu of a document. <p>
52  *
53  * Object implements the following interfaces :
54  * <ul>
55  *  <li> <code>::com::sun::star::accessibility::XAccessibleExtendedComponent</code></li>
56  *  <li> <code>::com::sun::star::accessibility::XAccessibleEventBroadcaster</code></li>
57  *  <li> <code>::com::sun::star::accessibility::XAccessibleComponent</code></li>
58  *  <li> <code>::com::sun::star::accessibility::XAccessibleValue</code></li>
59  *  <li> <code>::com::sun::star::accessibility::XAccessibleAction</code></li>
60  *  <li> <code>::com::sun::star::accessibility::XAccessibleContext</code></li>
61  *  <li> <code>::com::sun::star::accessibility::XAccessibleText</code></li>
62  * </ul> <p>
63  *
64  * @see com.sun.star.accessibility.XAccessibleExtendedComponent
65  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
66  * @see com.sun.star.accessibility.XAccessibleComponent
67  * @see com.sun.star.accessibility.XAccessibleValue
68  * @see com.sun.star.accessibility.XAccessibleAction
69  * @see com.sun.star.accessibility.XAccessibleContext
70  * @see com.sun.star.accessibility.XAccessibleText
71  * @see ifc.accessibility._XAccessibleExtendedComponent
72  * @see ifc.accessibility._XAccessibleEventBroadcaster
73  * @see ifc.accessibility._XAccessibleComponent
74  * @see ifc.accessibility._XAccessibleValue
75  * @see ifc.accessibility._XAccessibleAction
76  * @see ifc.accessibility._XAccessibleContext
77  * @see ifc.accessibility._XAccessibleText
78  */
79 public class AccessibleMenuItem extends TestCase {
80     private static XTextDocument xTextDoc = null;
81     private static XAccessibleAction action = null;
82     private static XMultiServiceFactory msf = null;
83 
84     /**
85      * Finds first accessible component with role <code>MENUITEM</code>
86      * walking through the accessible component tree of a document.
87      */
createTestEnvironment(TestParameters Param, PrintWriter log)88     protected TestEnvironment createTestEnvironment(TestParameters Param,
89                                                     PrintWriter log) {
90         XInterface oObj = null;
91 
92         try {
93             oObj = (XInterface) msf.createInstance("com.sun.star.awt.Toolkit");
94         } catch (com.sun.star.uno.Exception e) {
95             log.println("Couldn't get toolkit");
96             e.printStackTrace(log);
97             throw new StatusException("Couldn't get toolkit", e);
98         }
99 
100         XExtendedToolkit tk = (XExtendedToolkit) UnoRuntime.queryInterface(
101                                       XExtendedToolkit.class, oObj);
102 
103         shortWait();
104 
105         AccessibilityTools at = new AccessibilityTools();
106 
107         Object atw = tk.getActiveTopWindow();
108 
109         XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
110                                                               atw);
111 
112         XAccessible xRoot = at.getAccessibleObject(xWindow);
113 
114         at.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
115         XAccessibleContext MenuBar = at.getAccessibleObjectForRole(xRoot,
116                                                                    AccessibleRole.MENU_BAR);
117 
118         try {
119             //activate Edit-Menu
120             XAccessible Menu = MenuBar.getAccessibleChild(1);
121             XAccessibleAction act = (XAccessibleAction) UnoRuntime.queryInterface(
122                                             XAccessibleAction.class, Menu);
123             act.doAccessibleAction(0);
124             shortWait();
125 
126 
127             //get a menue-item
128             oObj = Menu.getAccessibleContext().getAccessibleChild(11);
129         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
130         }
131 
132 
133         //oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.MENUITEM);
134         log.println("ImplementationName " + utils.getImplName(oObj));
135 
136         TestEnvironment tEnv = new TestEnvironment(oObj);
137 
138         final XAccessibleAction action = (XAccessibleAction) UnoRuntime.queryInterface(
139                                                  XAccessibleAction.class, oObj);
140 
141         tEnv.addObjRelation("EventProducer",
142                             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
143             public void fireEvent() {
144                 try {
145                     action.doAccessibleAction(0);
146                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
147                 }
148             }
149         });
150 
151         XAccessibleText text = (XAccessibleText) UnoRuntime.queryInterface(
152                                        XAccessibleText.class, oObj);
153 
154         tEnv.addObjRelation("XAccessibleText.Text", text.getText());
155 
156         tEnv.addObjRelation("EditOnly",
157                             "Can't change or select Text in MenuBarItem");
158 
159         tEnv.addObjRelation("Destroy", new Boolean(true));
160 
161         tEnv.addObjRelation("LimitedBounds", "yes");
162 
163         return tEnv;
164     }
165 
166     /**
167      * Creates writer document.
168      */
initialize(TestParameters Param, PrintWriter log)169     protected void initialize(TestParameters Param, PrintWriter log) {
170         try {
171             msf = (XMultiServiceFactory) Param.getMSF();
172 
173             SOfficeFactory SOF = SOfficeFactory.getFactory(msf);
174             xTextDoc = SOF.createTextDoc(null);
175         } catch (com.sun.star.uno.Exception e) {
176             throw new StatusException("Can't create document", e);
177         }
178     }
179 
180     /**
181      * Disposes document.
182      */
cleanup(TestParameters Param, PrintWriter log)183     protected void cleanup(TestParameters Param, PrintWriter log) {
184         util.DesktopTools.closeDoc(xTextDoc);
185     }
186 
187     /**
188     * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
189     * reset</code> call.
190     */
shortWait()191     private void shortWait() {
192         try {
193             Thread.sleep(500);
194         } catch (InterruptedException e) {
195             log.println("While waiting :" + e);
196         }
197     }
198 }
199