bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / ifc / awt / _XUserInputInterception.java
blobc8221d06e8f1b0c8653c5845840362d17fb3d813
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc.awt;
21 import com.sun.star.accessibility.AccessibleRole;
22 import com.sun.star.accessibility.XAccessible;
23 import com.sun.star.accessibility.XAccessibleComponent;
24 import com.sun.star.accessibility.XAccessibleContext;
25 import com.sun.star.awt.KeyEvent;
26 import com.sun.star.awt.MouseEvent;
27 import com.sun.star.awt.Point;
28 import com.sun.star.awt.Rectangle;
29 import com.sun.star.awt.XKeyHandler;
30 import com.sun.star.awt.XMouseClickHandler;
31 import com.sun.star.awt.XUserInputInterception;
32 import com.sun.star.awt.XWindow;
33 import com.sun.star.frame.XModel;
34 import com.sun.star.lang.EventObject;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.uno.UnoRuntime;
37 import java.awt.Robot;
38 import java.awt.event.InputEvent;
39 import lib.MultiMethodTest;
40 import util.AccessibilityTools;
42 /**
43 * Testing <code>com.sun.star.awt.XUserInputInterception</code>
44 * interface methods:
45 * <ul>
46 * <li><code> addKeyHandler() </code></li>
47 * <li><code> removeKeyHandler() </code></li>
48 * <li><code> addMouseClickHandler() </code></li>
49 * <li><code> removeMouseClickHandler() </code></li>
50 * </ul><p>
51 * This test needs the following object relations :
52 * <ul>
53 * <li> <code>'XUserInputInterception.XModel'</code> (of type <code>XModel</code>):
54 * used as model where a mouse click or a key press could be done </li>
55 * </ul> <p>
56 * Test is <b> NOT </b> multithread compilant. <p>
57 * @see com.sun.star.awt.XUserInputInterception
59 public class _XUserInputInterception extends MultiMethodTest {
60 public XUserInputInterception oObj = null;
62 private XModel m_XModel = null;
64 /** the listener 1 for the mouse click test */
65 private MyMouseClickHandler1 m_MouseListener1 = null;
66 /** the listener 2 for the mouse click test */
67 private MyMouseClickHandler2 m_MouseListener2 = null;
69 /** the listener 1 for the key event test */
70 private MyKeyHandler1 m_KeyListener1 = null;
71 /** the listener 2 for the key event test */
72 private MyKeyHandler2 m_KeyListener2 = null;
74 /** indicates if the mousePressed event was called*/
75 private boolean m_mousePressed1 = false;
76 /** indicates if the mouseReleased event was called*/
77 private boolean m_mouseReleased1 = false;
79 /** indicates if the mousePressed event was called*/
80 private boolean m_mousePressed2 = false;
81 /** indicates if the mouseReleased event was called*/
82 private boolean m_mouseReleased2 = false;
84 /** indicates if the mousePressed event was called*/
85 private boolean m_keyPressed1 = false;
86 /** indicates if the mouseReleased event was called*/
87 private boolean m_keyReleased1 = false;
89 /** indicates if the mousePressed event was called*/
90 private boolean m_keyPressed2 = false;
91 /** indicates if the mouseReleased event was called*/
92 private boolean m_keyReleased2 = false;
94 /** get the object rlation XUserInputInterception.XModel from the
95 * test environment
97 protected void before() {
98 log.print("try to get object relation 'XUserInputInterception.XModel': ");
99 m_XModel = (XModel)tEnv.getObjRelation("XUserInputInterception.XModel");
100 if (m_XModel == null) log.println("failed => null");
101 else log.println("OK");
106 * This test adds two different key listener to the object. <p>
108 * Has <b> OK </b> if no exception is thrown.
110 public void _addKeyHandler() {
112 log.println("creating key listener 1");
113 m_KeyListener1 = new MyKeyHandler1();
115 log.println("creating key listener 2");
116 m_KeyListener2 = new MyKeyHandler2();
119 log.println("adding key listener 1");
120 oObj.addKeyHandler(m_KeyListener1);
123 log.println("adding key listener 2");
124 oObj.addKeyHandler(m_KeyListener2);
126 tRes.tested("addKeyHandler()", true);
130 * The test requires <CODE>addKeyHandler()</CODE> which adds two key listener.
131 * Then one of them will be removed. In a second thread a key event is released
132 * by the <CODE>robot</CODE> class.<p>
133 * Has <b> OK </b> status if only one of the listener are triggered. <p>
134 * The following method tests are to be completed successfully before :
135 * <ul>
136 * <li> <code> addKeyHandler() </code> : adds two key listener </li>
137 * </ul>
139 public void _removeKeyHandler() {
140 requiredMethod("addKeyHandler()");
142 log.println("remove key listener 2");
144 oObj.removeKeyHandler(m_KeyListener2);
146 log.println("starting thread to check the key listener...");
147 EventTrigger et = new EventTrigger(m_XModel, EventTriggerType.KEY_TEXT_INTO_DOC);
149 et.run();
151 util.utils.shortWait(tParam.getInt(util.PropertyName.SHORT_WAIT) * 2);
152 log.println("key listener thread should be finished.");
155 boolean bOK = m_keyPressed1 & m_keyReleased1 &
156 ! m_keyPressed2 & ! m_keyReleased2;
158 if (! bOK){
159 log.println("The key listener has not the expectd status:");
160 log.println("listener\texpected\tgot");
161 log.println("keyPressed1\ttrue\t"+m_keyPressed1);
162 log.println("keyReleased1\ttrue\t"+m_keyReleased1);
163 log.println("keyPressed2\tfalse\t"+m_keyPressed2);
164 log.println("keyReleased2\tfalse\t"+m_keyReleased2);
167 log.println("remove Key listener 1");
168 oObj.removeKeyHandler(m_KeyListener1);
170 tRes.tested("removeKeyHandler()", bOK);
174 * This test adds two different mouse klick listener to the object. <p>
176 * Has <b> OK </b> if no exception is thrown.
178 public void _addMouseClickHandler() {
179 log.println("creating mouse listener 1");
180 m_MouseListener1 = new MyMouseClickHandler1();
181 log.println("creating mouse listener 2");
182 m_MouseListener2 = new MyMouseClickHandler2();
184 log.println("adding mouse listener 1");
185 oObj.addMouseClickHandler(m_MouseListener1);
186 log.println("adding mouse listener 2");
187 oObj.addMouseClickHandler(m_MouseListener2);
189 tRes.tested("addMouseClickHandler()", true);
193 * The test requires <CODE>addMouseClickHandler()</CODE> which adds two key listener.
194 * Then one of them will be removed. In a second thread a mouse klick event is released
195 * by the <CODE>robot</CODE> class.<p>
196 * Has <b> OK </b> status if only one of the listener are triggered. <p>
197 * The following method tests are to be completed successfully before :
198 * <ul>
199 * <li> <code> addMouseKlickHandler() </code> : adds two key listener </li>
200 * </ul>
202 public void _removeMouseClickHandler() {
203 requiredMethod("addMouseClickHandler");
205 log.println("remove mouse listener 2");
207 oObj.removeMouseClickHandler(m_MouseListener2);
209 log.println("starting thread to check the mouse listener...");
210 EventTrigger et = new EventTrigger(m_XModel, EventTriggerType.MOUSE_KLICK_INTO_DOC);
212 et.run();
214 util.utils.shortWait(tParam.getInt(util.PropertyName.SHORT_WAIT) * 2);
215 log.println("mouse listener thread should be finished.");
217 boolean bOK = m_mousePressed1 & m_mouseReleased1 &
218 ! m_mousePressed2 & ! m_mouseReleased2;
220 if (! bOK){
221 log.println("The mouse listener has not the expectd status:");
222 log.println("listener\t\texpected\tgot");
223 log.println("mousePressed1\ttrue\t\t"+m_mousePressed1);
224 log.println("mouseReleased1\ttrue\t\t"+m_mouseReleased1);
225 log.println("mousePressed2\tfalse\t\t"+m_mousePressed2);
226 log.println("mouseReleased2\tfalse\t\t"+m_mouseReleased2);
229 log.println("remove mouse listener 1");
230 oObj.removeMouseClickHandler(m_MouseListener1);
232 tRes.tested("removeMouseClickHandler()", bOK);
237 * Forces environment recreation.
239 protected void after() {
240 disposeEnvironment();
244 * Listener which added and its method must be called
245 * on <code>keyPressed</code> and <code>keyReleased</code> call.
247 public class MyKeyHandler1 implements XKeyHandler {
249 * This event sets the member <code>m_keyPressed</coed> to
250 * <code>true</code>
251 * @param oEvent The key event informs about the pressed key.
252 * @return returns <CODE>TRUE</CODE> in erery case
254 public boolean keyPressed( KeyEvent oEvent ){
255 log.println("XKeyHandler 1: keyPressed-Event");
256 m_keyPressed1 = true;
257 return true;
260 * This event sets the member <code>m_keyReleased</coed> to
261 * <code>true</code>
262 * @param oEvent The key event informs about the pressed key.
263 * @return returns <CODE>TRUE</CODE> in erery case
265 public boolean keyReleased( KeyEvent oEvent ){
266 log.println("XKeyHandler 1: keyReleased-Event");
267 m_keyReleased1 = true;
268 return true;
271 * This event does nothing useful
272 * @param oEvent refers to the object that fired the event.
274 public void disposing( EventObject oEvent ){
275 log.println("XKeyHandler 1: disposing-Event");
279 * Listener which added and its method must be called
280 * on <code>keyPressed</code> and <code>keyReleased</code> call.
282 public class MyKeyHandler2 implements XKeyHandler {
284 * This event sets the member <code>m_keyPressed</coed> to
285 * <code>true</code>
286 * @param oEvent The key event informs about the pressed key.
287 * @return returns <CODE>TRUE</CODE> in erery case
289 public boolean keyPressed( KeyEvent oEvent ){
290 log.println("XKeyHandler 2: keyPressed-Event: " +
291 "This should not be happen because listener is removed!");
292 m_keyPressed2 = true;
293 return true;
296 * This event sets the member <code>m_keyReleased</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 keyReleased( KeyEvent oEvent ){
302 log.println("XKeyHandler 2: keyReleased-Event: " +
303 "This should not be happen because listener is removed!");
304 m_keyReleased2 = true;
305 return true;
308 * This event does nothing useful
309 * @param oEvent refers to the object that fired the event.
311 public void disposing( EventObject oEvent ){
312 log.println("XKeyHandler 2: disposing-Event: " +
313 "This should not be happen because listener is removed!");
318 * Listener which added and its method must be called
319 * on <code>mousePressed</code> and <code>mouseReleased</code> call.
321 public class MyMouseClickHandler1 implements XMouseClickHandler {
323 * This event sets the member <code>m_mousePressed</coed> to
324 * <code>true</code>
325 * @param oEvent The mouse event informs about the kind of mouse event.
326 * @return returns <CODE>TRUE</CODE> in erery case
328 public boolean mousePressed( MouseEvent oEvent ){
329 log.println("XMouseClickHandler 1: mousePressed-Event");
330 m_mousePressed1 = true;
331 return true;
334 * This event sets the member <code>m_mouseReleased</coed> to
335 * <code>true</code>
336 * @param oEvent The mouse event informs about the kind of mouse event.
337 * @return returns <CODE>TRUE</CODE> in erery case
339 public boolean mouseReleased( MouseEvent oEvent ){
340 log.println("XMouseClickHandler 1: mouseReleased-Event");
341 m_mouseReleased1 = true;
342 return true;
345 * This event does nothing useful
346 * @param oEvent refers to the object that fired the event.
348 public void disposing( EventObject oEvent ){
349 log.println("XMouseClickHandler 1: disposing-Event");
354 * Listener which added and removed. Its method must NOT be called
355 * on <code>mousePressed</code> and <code>mouseReleased</code> call.
357 public class MyMouseClickHandler2 implements XMouseClickHandler {
359 * This event sets the member <code>m_mousePressed</coed> to
360 * <code>true</code>
361 * @param oEvent The mouse event informs about the kind of mouse event.
362 * @return returns <CODE>TRUE</CODE> in erery case
364 public boolean mousePressed( MouseEvent oEvent ){
365 log.println("XMouseClickHandler 2: mousePressed-Event: " +
366 "This should not be happen because listener is removed!");
367 m_mousePressed2 = true;
368 return true;
371 * This event sets the member <code>m_mouseReleased</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 mouseReleased( MouseEvent oEvent ){
377 log.println("XMouseClickHandler 2: mouseReleased-Event: " +
378 "This should not be happen because listener is removed!");
379 m_mouseReleased2 = true;
380 return true;
383 * This event does nothing useful
384 * @param oEvent refers to the object that fired the event.
386 public void disposing( EventObject oEvent ){
387 log.println("XMouseClickHandler 2: disposing-Event: " +
388 " This should not be happen because listener is removed!");
393 * To check the events this class is a thread which click a mouse button and
394 * press a key with the <CODE>Robot</CODE> class
395 * @see java.awt.Robot
397 private class EventTrigger extends Thread{
400 * represents a <CODE>AccessibilityTools</CODE>
402 private final AccessibilityTools at = new AccessibilityTools();
404 * represents an <CODE>EventType</CODE>
405 * @see EventTest.EventTriggerType
407 private int eventType = 0;
409 * represents a <CODE>XModel</CODE> of a document
411 private XModel xModel = null;
414 * Creates an instacne of this class. The parameter <CODE>eType</CODE> represents
415 * the kind of event which will be triggert at <CODE>run()</CODE>
416 * @param model the model of a document
417 * @param eType the kind of event which should be trigger
419 public EventTrigger(XModel model, int eType)
421 this.xModel = model;
422 this.eventType = eType;
426 * Triggers the event which is represented by <CODE>eventType</CODE>
427 * The scenarios are:
428 * <ul>
429 * <li>EventTest.EventTriggerType.MOUSE_KLICK_INTO_DOC
430 * which calls
431 * <li><CODE>clickIntoDoc</CODE></LI>
432 * </LI>
433 * <li>EventTest.EventTriggerType.KEY_TEXT_INTO_DOC
434 * which calls
435 * <li><CODE>clickIntodoc</CODE></LI>
436 * <li><CODE>keyIntoDoc</CODE></LI>
437 * </LI>
438 * </UL>
440 public void run(){
442 switch (this.eventType){
444 case EventTriggerType.MOUSE_KLICK_INTO_DOC:
445 clickIntoDoc();
446 break;
447 case EventTriggerType.KEY_TEXT_INTO_DOC:
448 clickIntoDoc();
449 keyIntoDoc();
450 break;
455 * This method cklicks into the middel of a document. It uses Accessibility
456 * to get the document and query for its position and its range to calculate
457 * the middle. This values was used for <CODE>Robot</CODE> Class. This
458 * Robot class is able to move the mouse and to cklick a mouse button
459 * @see java.awt.Robot
461 private void clickIntoDoc(){
462 try{
464 util.DesktopTools.bringWindowToFront(xModel);
466 XWindow xWindow = AccessibilityTools.getCurrentWindow(
467 (XMultiServiceFactory) tParam.getMSF(),
468 xModel);
470 XAccessible xRoot = AccessibilityTools.getAccessibleObject(xWindow);
474 XAccessibleContext xPanel = AccessibilityTools.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL);
475 XAccessibleComponent xPanelCont = UnoRuntime.queryInterface(XAccessibleComponent.class, xPanel);
477 // the position of the panel
478 Point point = xPanelCont.getLocationOnScreen();
480 // the range of the panel
481 Rectangle rect = xPanelCont.getBounds();
483 try {
484 Robot rob = new Robot();
485 int x = point.X + (rect.Width / 2);
486 int y = point.Y + (rect.Height / 2);
487 log.println("try to klick into the middle of the document");
488 rob.mouseMove(x, y);
489 rob.mousePress(InputEvent.BUTTON1_MASK);
490 rob.mouseRelease(InputEvent.BUTTON1_MASK);
491 } catch (java.awt.AWTException e) {
492 log.println("couldn't press mouse button");
494 } catch (java.lang.Exception e){
495 log.println("could not click into the scroll bar: " + e.toString());
500 * This method press the "A" key. Therefore it uses the <CODE>Robot</CODE>
501 * class.
502 * @see java.awt.Robot
504 private void keyIntoDoc(){
505 try {
506 Robot rob = new Robot();
507 log.println("try to press 'A'");
508 rob.keyPress(java.awt.event.KeyEvent.VK_A);
509 rob.keyRelease(java.awt.event.KeyEvent.VK_A);
510 } catch (java.awt.AWTException e) {
511 log.println("couldn't press key");
517 /** This interface represents all possible actions which could be used
518 * in the <CODE>EventTrigger</CODE> class.
519 * @see EventTest.EventTrigger
521 private interface EventTriggerType{
523 /** klick the mouse into the scroll bar*/
524 final public static int MOUSE_KLICK_INTO_DOC = 1;
526 /** write some text into a spread sheet*/
527 final public static int KEY_TEXT_INTO_DOC = 2;