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._sc;
24 
25 import java.io.PrintWriter;
26 
27 import lib.StatusException;
28 import lib.TestCase;
29 import lib.TestEnvironment;
30 import lib.TestParameters;
31 import util.AccessibilityTools;
32 import util.SOfficeFactory;
33 import util.utils;
34 
35 import com.sun.star.accessibility.AccessibleRole;
36 import com.sun.star.accessibility.XAccessible;
37 import com.sun.star.accessibility.XAccessibleAction;
38 import com.sun.star.accessibility.XAccessibleContext;
39 import com.sun.star.accessibility.XAccessibleEditableText;
40 import com.sun.star.accessibility.XAccessibleText;
41 import com.sun.star.awt.XExtendedToolkit;
42 import com.sun.star.awt.XWindow;
43 import com.sun.star.frame.XController;
44 import com.sun.star.frame.XDispatch;
45 import com.sun.star.frame.XDispatchProvider;
46 import com.sun.star.frame.XModel;
47 import com.sun.star.lang.XComponent;
48 import com.sun.star.lang.XMultiServiceFactory;
49 import com.sun.star.uno.UnoRuntime;
50 import com.sun.star.util.URL;
51 import com.sun.star.util.XURLTransformer;
52 
53 
54 public class AccessibleEditableTextPara_HeaderFooter extends TestCase {
55     protected static XComponent xCalcDoc = null;
56     protected static XMultiServiceFactory msf = null;
57     protected static XAccessibleAction action = null;
58 
createTestEnvironment(TestParameters Param, PrintWriter log)59     protected TestEnvironment createTestEnvironment(TestParameters Param,
60                                                     PrintWriter log) {
61         XAccessibleContext oObj = null;
62         Object toolkit = null;
63 
64         try {
65             toolkit = msf.createInstance("com.sun.star.awt.Toolkit");
66         } catch (com.sun.star.uno.Exception e) {
67             log.println("Couldn't get toolkit");
68             e.printStackTrace(log);
69             throw new StatusException("Couldn't get toolkit", e);
70         }
71 
72         XExtendedToolkit tk = (XExtendedToolkit) UnoRuntime.queryInterface(
73                                       XExtendedToolkit.class, toolkit);
74 
75         util.utils.shortWait(Param.getInt("ShortWait"));
76 
77         DiagThread psDiag = new DiagThread(xCalcDoc, msf);
78         psDiag.start();
79 
80         util.utils.shortWait(Param.getInt("ShortWait")*5);
81 
82         Object atw = tk.getActiveTopWindow();
83 
84         XWindow xWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class,
85                                                               atw);
86 
87         XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);
88 
89         util.utils.shortWait(Param.getInt("ShortWait"));
90 
91         AccessibilityTools.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
92         XAccessibleContext ok_button = AccessibilityTools.getAccessibleObjectForRole(xRoot,
93                                                                      AccessibleRole.PUSH_BUTTON,
94                                                                      "Cancel");
95 
96         oObj = AccessibilityTools.getAccessibleObjectForRole(xRoot, AccessibleRole.PARAGRAPH);
97 
98         log.println("ImplementationName " + utils.getImplName(oObj));
99         log.println("AccessibleName " + oObj.getAccessibleName());
100         log.println("ParentDescription " +
101                     oObj.getAccessibleParent().getAccessibleContext()
102                         .getAccessibleDescription());
103 
104         TestEnvironment tEnv = new TestEnvironment(oObj);
105 
106         action = (XAccessibleAction) UnoRuntime.queryInterface(
107                          XAccessibleAction.class, ok_button);
108 
109         XAccessibleText text = (XAccessibleText) UnoRuntime.queryInterface(
110                                        XAccessibleText.class, oObj);
111 
112         XAccessibleEditableText eText = (XAccessibleEditableText) UnoRuntime.queryInterface(
113                                                 XAccessibleEditableText.class,
114                                                 oObj);
115 
116         eText.setText("LEFT");
117 
118         tEnv.addObjRelation("XAccessibleText.Text", text.getText());
119 
120         tEnv.addObjRelation("Destroy", "AccessibleEditableTextPara_PreviewCell");
121 
122         final XAccessibleEditableText editText = eText;
123 
124         tEnv.addObjRelation("EventProducer",
125                             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
126             public void fireEvent() {
127                 editText.setText("LEFT_EVENT");
128             }
129         });
130 
131         return tEnv;
132     }
133 
134     /**
135      * Closes the dialog using accessible button 'OK' found in
136      * <code>createTestEnvironment()</code>.
137      */
cleanup(TestParameters Param, PrintWriter log)138     protected void cleanup(TestParameters Param, PrintWriter log) {
139         log.println("    disposing xCalcDoc ");
140 
141         try {
142             action.doAccessibleAction(0);
143         } catch (com.sun.star.lang.IndexOutOfBoundsException ioe) {
144             log.println("Couldn't close dialog");
145         } catch (com.sun.star.lang.DisposedException de) {
146             log.println("Dialog already disposed");
147         }
148 
149         util.DesktopTools.closeDoc(xCalcDoc);
150     }
151 
152     /**
153      * Opens new writer document.
154      */
initialize(TestParameters Param, PrintWriter log)155     protected void initialize(TestParameters Param, PrintWriter log) {
156         msf = (XMultiServiceFactory) Param.getMSF();
157 
158         try {
159             SOfficeFactory SOF = SOfficeFactory.getFactory(msf);
160             xCalcDoc = (XComponent) UnoRuntime.queryInterface(XComponent.class,
161                                                               SOF.createCalcDoc(
162                                                                       null));
163         } catch (com.sun.star.uno.Exception e) {
164             throw new StatusException("Can't create document", e);
165         }
166     }
167 
168 
169     /**
170      * Thread for opening modal dialog 'Print Settings'.
171      */
172     public class DiagThread extends Thread {
173         public XComponent xCalcDoc = null;
174         public XMultiServiceFactory msf = null;
175 
DiagThread(XComponent xCalcDoc, XMultiServiceFactory msf)176         public DiagThread(XComponent xCalcDoc, XMultiServiceFactory msf) {
177             this.xCalcDoc = xCalcDoc;
178             this.msf = msf;
179         }
180 
run()181         public void run() {
182             XModel aModel = (XModel) UnoRuntime.queryInterface(XModel.class,
183                                                                xCalcDoc);
184 
185             XController xController = aModel.getCurrentController();
186 
187             //Opening HeaderFooterDialog
188             try {
189                 String aSlotID = ".uno:EditHeaderAndFooter";
190                 XDispatchProvider xDispProv = (XDispatchProvider) UnoRuntime.queryInterface(
191                                                       XDispatchProvider.class,
192                                                       xController);
193                 XURLTransformer xParser = (com.sun.star.util.XURLTransformer) UnoRuntime.queryInterface(
194                                                   XURLTransformer.class,
195                                                   msf.createInstance(
196                                                           "com.sun.star.util.URLTransformer"));
197 
198                 // Because it's an in/out parameter
199                 // we must use an array of URL objects.
200                 URL[] aParseURL = new URL[1];
201                 aParseURL[0] = new URL();
202                 aParseURL[0].Complete = aSlotID;
203                 xParser.parseStrict(aParseURL);
204 
205                 URL aURL = aParseURL[0];
206                 XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0);
207 
208                 if (xDispatcher != null) {
209                     xDispatcher.dispatch(aURL, null);
210                 }
211             } catch (com.sun.star.uno.Exception e) {
212                 log.println("Couldn't open dialog");
213             }
214         }
215     }
216 }