Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / ifc / awt / _XUserInputInterception.java
blob3425ebe095ea2f1c065cf80e6eef0d17ab7d139a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XUserInputInterception.java,v $
10 * $Revision: 1.4.8.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc.awt;
33 import com.sun.star.accessibility.AccessibleRole;
34 import com.sun.star.accessibility.XAccessible;
35 import com.sun.star.accessibility.XAccessibleComponent;
36 import com.sun.star.accessibility.XAccessibleContext;
37 import com.sun.star.awt.KeyEvent;
38 import com.sun.star.awt.MouseEvent;
39 import com.sun.star.awt.Point;
40 import com.sun.star.awt.Rectangle;
41 import com.sun.star.awt.XKeyHandler;
42 import com.sun.star.awt.XMouseClickHandler;
43 import com.sun.star.awt.XUserInputInterception;
44 import com.sun.star.awt.XWindow;
45 import com.sun.star.frame.XModel;
46 import com.sun.star.lang.EventObject;
47 import com.sun.star.lang.XMultiServiceFactory;
48 import com.sun.star.uno.UnoRuntime;
49 import java.awt.Robot;
50 import java.awt.event.InputEvent;
51 import lib.MultiMethodTest;
52 import util.AccessibilityTools;
54 /**
55 * Testing <code>com.sun.star.awt.XUserInputInterception</code>
56 * interface methods:
57 * <ul>
58 * <li><code> addKeyHandler() </code></li>
59 * <li><code> removeKeyHandler() </code></li>
60 * <li><code> addMouseClickHandler() </code></li>
61 * <li><code> removeMouseClickHandler() </code></li>
62 * </ul><p>
63 * This test needs the following object relations :
64 * <ul>
65 * <li> <code>'XUserInputInterception.XModel'</code> (of type <code>XModel</code>):
66 * used as model where a mouse click or a key press could be done </li>
67 * </ul> <p>
68 * Test is <b> NOT </b> multithread compilant. <p>
69 * @see com.sun.star.awt.XUserInputInterception
71 public class _XUserInputInterception extends MultiMethodTest {
72 public XUserInputInterception oObj = null;
74 private XModel m_XModel = null;
76 /** the listener 1 for the mouse click test */
77 private MyMouseClickHandler1 m_MouseListener1 = null;
78 /** the listener 2 for the mouse click test */
79 private MyMouseClickHandler2 m_MouseListener2 = null;
81 /** the listener 1 for the key event test */
82 private MyKeyHandler1 m_KeyListener1 = null;
83 /** the listener 2 for the key event test */
84 private MyKeyHandler2 m_KeyListener2 = null;
86 /** indicates if the mousePressed event was called*/
87 private boolean m_mousePressed1 = false;
88 /** indicates if the mouseReleased event was called*/
89 private boolean m_mouseReleased1 = false;
91 /** indicates if the mousePressed event was called*/
92 private boolean m_mousePressed2 = false;
93 /** indicates if the mouseReleased event was called*/
94 private boolean m_mouseReleased2 = false;
96 /** indicates if the mousePressed event was called*/
97 private boolean m_keyPressed1 = false;
98 /** indicates if the mouseReleased event was called*/
99 private boolean m_keyReleased1 = false;
101 /** indicates if the mousePressed event was called*/
102 private boolean m_keyPressed2 = false;
103 /** indicates if the mouseReleased event was called*/
104 private boolean m_keyReleased2 = false;
106 /** get the object rlation XUserInputInterception.XModel from the
107 * test environment
109 protected void before() {
110 log.print("try to get object relation 'XUserInputInterception.XModel': ");
111 m_XModel = (XModel)tEnv.getObjRelation("XUserInputInterception.XModel");
112 if (m_XModel == null) log.println("failed => null");
113 else log.println("OK");
118 * This test adds two different key listener to the object. <p>
120 * Has <b> OK </b> if no exception is thrown.
122 public void _addKeyHandler() {
124 log.println("creating key listener 1");
125 m_KeyListener1 = new MyKeyHandler1();
127 log.println("creating key listener 2");
128 m_KeyListener2 = new MyKeyHandler2();
131 log.println("adding key listener 1");
132 oObj.addKeyHandler(m_KeyListener1);
135 log.println("adding key listener 2");
136 oObj.addKeyHandler(m_KeyListener2);
138 tRes.tested("addKeyHandler()", true);
142 * The test requires <CODE>addKeyHandler()</CODE> which adds two key listener.
143 * Then one of them will be removed. In a second thread a key event is released
144 * by the <CODE>robot</CODE> class.<p>
145 * Has <b> OK </b> status if only one of the listener are triggered. <p>
146 * The following method tests are to be completed successfully before :
147 * <ul>
148 * <li> <code> addKeyHandler() </code> : adds two key listener </li>
149 * </ul>
151 public void _removeKeyHandler() {
152 requiredMethod("addKeyHandler()");
154 log.println("remove key listener 2");
156 oObj.removeKeyHandler(m_KeyListener2);
158 log.println("starting thread to check the key listener...");
159 EventTrigger et = new EventTrigger(m_XModel, EventTriggerType.KEY_TEXT_INTO_DOC);
161 et.run();
163 util.utils.shortWait(tParam.getInt(util.PropertyName.SHORT_WAIT) * 2);
164 log.println("key listener thread should be finished.");
167 boolean bOK = m_keyPressed1 & m_keyReleased1 &
168 ! m_keyPressed2 & ! m_keyReleased2;
170 if (! bOK){
171 log.println("The key listener has not the expectd status:");
172 log.println("listener\texpected\tgot");
173 log.println("keyPressed1\ttrue\t"+m_keyPressed1);
174 log.println("keyReleased1\ttrue\t"+m_keyReleased1);
175 log.println("keyPressed2\tfalse\t"+m_keyPressed2);
176 log.println("keyReleased2\tfalse\t"+m_keyReleased2);
179 log.println("remove Key listener 1");
180 oObj.removeKeyHandler(m_KeyListener1);
182 tRes.tested("removeKeyHandler()", bOK);
186 * This test adds two different mouse klick listener to the object. <p>
188 * Has <b> OK </b> if no exception is thrown.
190 public void _addMouseClickHandler() {
191 log.println("creating mouse listener 1");
192 m_MouseListener1 = new MyMouseClickHandler1();
193 log.println("creating mouse listener 2");
194 m_MouseListener2 = new MyMouseClickHandler2();
196 log.println("adding mouse listener 1");
197 oObj.addMouseClickHandler(m_MouseListener1);
198 log.println("adding mouse listener 2");
199 oObj.addMouseClickHandler(m_MouseListener2);
201 tRes.tested("addMouseClickHandler()", true);
205 * The test requires <CODE>addMouseClickHandler()</CODE> which adds two key listener.
206 * Then one of them will be removed. In a second thread a mouse klick event is released
207 * by the <CODE>robot</CODE> class.<p>
208 * Has <b> OK </b> status if only one of the listener are triggered. <p>
209 * The following method tests are to be completed successfully before :
210 * <ul>
211 * <li> <code> addMouseKlickHandler() </code> : adds two key listener </li>
212 * </ul>
214 public void _removeMouseClickHandler() {
215 requiredMethod("addMouseClickHandler");
217 log.println("remove mouse listener 2");
219 oObj.removeMouseClickHandler(m_MouseListener2);
221 log.println("starting thread to check the mouse listener...");
222 EventTrigger et = new EventTrigger(m_XModel, EventTriggerType.MOUSE_KLICK_INTO_DOC);
224 et.run();
226 util.utils.shortWait(tParam.getInt(util.PropertyName.SHORT_WAIT) * 2);
227 log.println("mouse listener thread should be finished.");
229 boolean bOK = m_mousePressed1 & m_mouseReleased1 &
230 ! m_mousePressed2 & ! m_mouseReleased2;
232 if (! bOK){
233 log.println("The mouse listener has not the expectd status:");
234 log.println("listener\t\texpected\tgot");
235 log.println("mousePressed1\ttrue\t\t"+m_mousePressed1);
236 log.println("mouseReleased1\ttrue\t\t"+m_mouseReleased1);
237 log.println("mousePressed2\tfalse\t\t"+m_mousePressed2);
238 log.println("mouseReleased2\tfalse\t\t"+m_mouseReleased2);
241 log.println("remove mouse listener 1");
242 oObj.removeMouseClickHandler(m_MouseListener1);
244 tRes.tested("removeMouseClickHandler()", bOK);
249 * Forces environment recreation.
251 protected void after() {
252 disposeEnvironment();
256 * Listener which added and its method must be called
257 * on <code>keyPressed</code> and <code>keyReleased</code> call.
259 public class MyKeyHandler1 implements XKeyHandler {
261 * This event sets the member <code>m_keyPressed</coed> to
262 * <code>true</code>
263 * @param oEvent The key event informs about the pressed key.
264 * @return returns <CODE>TRUE</CODE> in erery case
266 public boolean keyPressed( KeyEvent oEvent ){
267 log.println("XKeyHandler 1: keyPressed-Event");
268 m_keyPressed1 = true;
269 return true;
272 * This event sets the member <code>m_keyReleased</coed> to
273 * <code>true</code>
274 * @param oEvent The key event informs about the pressed key.
275 * @return returns <CODE>TRUE</CODE> in erery case
277 public boolean keyReleased( KeyEvent oEvent ){
278 log.println("XKeyHandler 1: keyReleased-Event");
279 m_keyReleased1 = true;
280 return true;
283 * This event does nothing usefull
284 * @param oEvent refers to the object that fired the event.
286 public void disposing( EventObject oEvent ){
287 log.println("XKeyHandler 1: disposing-Event");
291 * Listener which added and its method must be called
292 * on <code>keyPressed</code> and <code>keyReleased</code> call.
294 public class MyKeyHandler2 implements XKeyHandler {
296 * This event sets the member <code>m_keyPressed</coed> to
297 * <code>true</code>
298 * @param oEvent The key event informs about the pressed key.
299 * @return returns <CODE>TRUE</CODE> in erery case
301 public boolean keyPressed( KeyEvent oEvent ){
302 log.println("XKeyHandler 2: keyPressed-Event: " +
303 "This should not be happen because listener is removed!");
304 m_keyPressed2 = true;
305 return true;
308 * This event sets the member <code>m_keyReleased</coed> to
309 * <code>true</code>
310 * @param oEvent The key event informs about the pressed key.
311 * @return returns <CODE>TRUE</CODE> in erery case
313 public boolean keyReleased( KeyEvent oEvent ){
314 log.println("XKeyHandler 2: keyReleased-Event: " +
315 "This should not be happen because listener is removed!");
316 m_keyReleased2 = true;
317 return true;
320 * This event does nothing usefull
321 * @param oEvent refers to the object that fired the event.
323 public void disposing( EventObject oEvent ){
324 log.println("XKeyHandler 2: disposing-Event: " +
325 "This should not be happen because listener is removed!");
330 * Listener which added and its method must be called
331 * on <code>mousePressed</code> and <code>mouseReleased</code> call.
333 public class MyMouseClickHandler1 implements XMouseClickHandler {
335 * This event sets the member <code>m_mousePressed</coed> to
336 * <code>true</code>
337 * @param oEvent The mouse event informs about the kind of mouse event.
338 * @return returns <CODE>TRUE</CODE> in erery case
340 public boolean mousePressed( MouseEvent oEvent ){
341 log.println("XMouseClickHandler 1: mousePressed-Event");
342 m_mousePressed1 = true;
343 return true;
346 * This event sets the member <code>m_mouseReleased</coed> to
347 * <code>true</code>
348 * @param oEvent The mouse event informs about the kind of mouse event.
349 * @return returns <CODE>TRUE</CODE> in erery case
351 public boolean mouseReleased( MouseEvent oEvent ){
352 log.println("XMouseClickHandler 1: mouseReleased-Event");
353 m_mouseReleased1 = true;
354 return true;
357 * This event does nothing usefull
358 * @param oEvent refers to the object that fired the event.
360 public void disposing( EventObject oEvent ){
361 log.println("XMouseClickHandler 1: disposing-Event");
366 * Listener which added and removed. Its method must NOT be called
367 * on <code>mousePressed</code> and <code>mouseReleased</code> call.
369 public class MyMouseClickHandler2 implements XMouseClickHandler {
371 * This event sets the member <code>m_mousePressed</coed> to
372 * <code>true</code>
373 * @param oEvent The mouse event informs about the kind of mouse event.
374 * @return returns <CODE>TRUE</CODE> in erery case
376 public boolean mousePressed( MouseEvent oEvent ){
377 log.println("XMouseClickHandler 2: mousePressed-Event: " +
378 "This should not be happen because listener is removed!");
379 m_mousePressed2 = true;
380 return true;
383 * This event sets the member <code>m_mouseReleased</coed> to
384 * <code>true</code>
385 * @param oEvent The mouse event informs about the kind of mouse event.
386 * @return returns <CODE>TRUE</CODE> in erery case
388 public boolean mouseReleased( MouseEvent oEvent ){
389 log.println("XMouseClickHandler 2: mouseReleased-Event: " +
390 "This should not be happen because listener is removed!");
391 m_mouseReleased2 = true;
392 return true;
395 * This event does nothing usefull
396 * @param oEvent refers to the object that fired the event.
398 public void disposing( EventObject oEvent ){
399 log.println("XMouseClickHandler 2: disposing-Event: " +
400 " This should not be happen because listener is removed!");
405 * To check the events this class is a thread which click a mouse button and
406 * press a key with the <CODE>Robot</CODE> class
407 * @see java.awt.Robot
409 private class EventTrigger extends Thread{
412 * represents a <CODE>AccessibilityTools</CODE>
414 private final AccessibilityTools at = new AccessibilityTools();
416 * represents an <CODE>EventType</CODE>
417 * @see EventTest.EventTriggerType
419 private int eventType = 0;
421 * represents a <CODE>XModel</CODE> of a document
423 private XModel xModel = null;
426 * Creates an instacne of this class. The parameter <CODE>eType</CODE> represents
427 * the kind of event wich will be triggert at <CODE>run()</CODE>
428 * @param model the model of a document
429 * @param eType the kind of event which should be trigger
431 public EventTrigger(XModel model, int eType)
433 this.xModel = model;
434 this.eventType = eType;
438 * Triggers the event wich is represented by <CODE>eventType</CODE>
439 * The scenarios are:
440 * <ul>
441 * <li>EventTest.EventTriggerType.MOUSE_KLICK_INTO_DOC
442 * which calls
443 * <li><CODE>clickIntoDoc</CODE></LI>
444 * </LI>
445 * <li>EventTest.EventTriggerType.KEY_TEXT_INTO_DOC
446 * which calls
447 * <li><CODE>clickIntodoc</CODE></LI>
448 * <li><CODE>keyIntoDoc</CODE></LI>
449 * </LI>
450 * </UL>
452 public void run(){
454 switch (this.eventType){
456 case EventTriggerType.MOUSE_KLICK_INTO_DOC:
457 clickIntoDoc();
458 break;
459 case EventTriggerType.KEY_TEXT_INTO_DOC:
460 clickIntoDoc();
461 keyIntoDoc();
462 break;
467 * This method cklicks into the middel of a document. It uses Accessibility
468 * to get the document and query for its position and its range to calculate
469 * the middle. This values was used for <CODE>Robot</CODE> Class. This
470 * Robot class is able to move the mouse and to cklick a mouse button
471 * @see java.awt.Robot
473 private void clickIntoDoc(){
474 try{
476 util.DesktopTools.bringWindowToFront(xModel);
478 XWindow xWindow = at.getCurrentWindow(
479 (XMultiServiceFactory) tParam.getMSF(),
480 xModel);
482 XAccessible xRoot = at.getAccessibleObject(xWindow);
486 XAccessibleContext xPanel = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL);
487 XAccessibleComponent xPanelCont = (XAccessibleComponent) UnoRuntime.queryInterface(XAccessibleComponent.class, xPanel);
489 // the position of the panel
490 Point point = xPanelCont.getLocationOnScreen();
492 // the range of the panel
493 Rectangle rect = xPanelCont.getBounds();
495 try {
496 Robot rob = new Robot();
497 int x = point.X + (rect.Width / 2);
498 int y = point.Y + (rect.Height / 2);
499 log.println("try to klick into the middle of the document");
500 rob.mouseMove(x, y);
501 rob.mousePress(InputEvent.BUTTON1_MASK);
502 rob.mouseRelease(InputEvent.BUTTON1_MASK);
503 } catch (java.awt.AWTException e) {
504 log.println("couldn't press mouse button");
506 } catch (java.lang.Exception e){
507 log.println("could not click into the scroll bar: " + e.toString());
512 * This method press the "A" key. Therefore it uses the <CODE>Robot</CODE>
513 * class.
514 * @see java.awt.Robot
516 private void keyIntoDoc(){
517 try {
518 Robot rob = new Robot();
519 log.println("try to press 'A'");
520 rob.keyPress(java.awt.event.KeyEvent.VK_A);
521 rob.keyRelease(java.awt.event.KeyEvent.VK_A);
522 } catch (java.awt.AWTException e) {
523 log.println("couldn't press key");
529 /** This interface represents all possible actions which could be used
530 * in the <CODE>EventTrigger</CODE> class.
531 * @see EventTest.EventTrigger
533 private interface EventTriggerType{
535 /** klick the mouse into the scroll bar*/
536 final public static int MOUSE_KLICK_INTO_DOC = 1;
538 /** write some text into a spread sheet*/
539 final public static int KEY_TEXT_INTO_DOC = 2;