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 .
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
.uno
.UnoRuntime
;
37 import java
.awt
.Robot
;
38 import java
.awt
.event
.InputEvent
;
40 import lib
.MultiMethodTest
;
41 import util
.AccessibilityTools
;
45 * Testing <code>com.sun.star.awt.XUserInputInterception</code>
48 * <li><code> addKeyHandler() </code></li>
49 * <li><code> removeKeyHandler() </code></li>
50 * <li><code> addMouseClickHandler() </code></li>
51 * <li><code> removeMouseClickHandler() </code></li>
53 * This test needs the following object relations :
55 * <li> <code>'XUserInputInterception.XModel'</code> (of type <code>XModel</code>):
56 * used as model where a mouse click or a key press could be done </li>
58 * Test is <b> NOT </b> multithread compliant. <p>
59 * @see com.sun.star.awt.XUserInputInterception
61 public class _XUserInputInterception
extends MultiMethodTest
{
62 public XUserInputInterception oObj
= null;
64 private XModel m_XModel
= null;
66 /** the listener 1 for the mouse click test */
67 private MyMouseClickHandler1 m_MouseListener1
= null;
68 /** the listener 2 for the mouse click test */
69 private MyMouseClickHandler2 m_MouseListener2
= null;
71 /** the listener 1 for the key event test */
72 private MyKeyHandler1 m_KeyListener1
= null;
73 /** the listener 2 for the key event test */
74 private MyKeyHandler2 m_KeyListener2
= null;
76 /** indicates if the mousePressed event was called*/
77 private boolean m_mousePressed1
= false;
78 /** indicates if the mouseReleased event was called*/
79 private boolean m_mouseReleased1
= false;
81 /** indicates if the mousePressed event was called*/
82 private boolean m_mousePressed2
= false;
83 /** indicates if the mouseReleased event was called*/
84 private boolean m_mouseReleased2
= false;
86 /** indicates if the mousePressed event was called*/
87 private boolean m_keyPressed1
= false;
88 /** indicates if the mouseReleased event was called*/
89 private boolean m_keyReleased1
= false;
91 /** indicates if the mousePressed event was called*/
92 private boolean m_keyPressed2
= false;
93 /** indicates if the mouseReleased event was called*/
94 private boolean m_keyReleased2
= false;
96 /** get the object rlation XUserInputInterception.XModel from the
100 protected void before() {
101 log
.print("try to get object relation 'XUserInputInterception.XModel': ");
102 m_XModel
= (XModel
)tEnv
.getObjRelation("XUserInputInterception.XModel");
103 if (m_XModel
== null) log
.println("failed => null");
104 else log
.println("OK");
109 * This test adds two different key listener to the object. <p>
111 * Has <b> OK </b> if no exception is thrown.
113 public void _addKeyHandler() {
115 log
.println("creating key listener 1");
116 m_KeyListener1
= new MyKeyHandler1();
118 log
.println("creating key listener 2");
119 m_KeyListener2
= new MyKeyHandler2();
122 log
.println("adding key listener 1");
123 oObj
.addKeyHandler(m_KeyListener1
);
126 log
.println("adding key listener 2");
127 oObj
.addKeyHandler(m_KeyListener2
);
129 tRes
.tested("addKeyHandler()", true);
133 * The test requires <CODE>addKeyHandler()</CODE> which adds two key listener.
134 * Then one of them will be removed. In a second thread a key event is released
135 * by the <CODE>robot</CODE> class.<p>
136 * Has <b> OK </b> status if only one of the listener are triggered. <p>
137 * The following method tests are to be completed successfully before :
139 * <li> <code> addKeyHandler() </code> : adds two key listener </li>
142 public void _removeKeyHandler() {
143 requiredMethod("addKeyHandler()");
145 log
.println("remove key listener 2");
147 oObj
.removeKeyHandler(m_KeyListener2
);
149 log
.println("starting thread to check the key listener...");
150 EventTrigger et
= new EventTrigger(m_XModel
, EventTriggerType
.KEY_TEXT_INTO_DOC
);
154 util
.utils
.pause(utils
.DEFAULT_SHORT_WAIT_MS
* 2);
155 log
.println("key listener thread should be finished.");
158 boolean bOK
= m_keyPressed1
& m_keyReleased1
&
159 ! m_keyPressed2
& ! m_keyReleased2
;
162 log
.println("The key listener has not the expectd status:");
163 log
.println("listener\texpected\tgot");
164 log
.println("keyPressed1\ttrue\t"+m_keyPressed1
);
165 log
.println("keyReleased1\ttrue\t"+m_keyReleased1
);
166 log
.println("keyPressed2\tfalse\t"+m_keyPressed2
);
167 log
.println("keyReleased2\tfalse\t"+m_keyReleased2
);
170 log
.println("remove Key listener 1");
171 oObj
.removeKeyHandler(m_KeyListener1
);
173 tRes
.tested("removeKeyHandler()", bOK
);
177 * This test adds two different mouse click listener to the object. <p>
179 * Has <b> OK </b> if no exception is thrown.
181 public void _addMouseClickHandler() {
182 log
.println("creating mouse listener 1");
183 m_MouseListener1
= new MyMouseClickHandler1();
184 log
.println("creating mouse listener 2");
185 m_MouseListener2
= new MyMouseClickHandler2();
187 log
.println("adding mouse listener 1");
188 oObj
.addMouseClickHandler(m_MouseListener1
);
189 log
.println("adding mouse listener 2");
190 oObj
.addMouseClickHandler(m_MouseListener2
);
192 tRes
.tested("addMouseClickHandler()", true);
196 * The test requires <CODE>addMouseClickHandler()</CODE> which adds two key listener.
197 * Then one of them will be removed. In a second thread a mouse click event is released
198 * by the <CODE>robot</CODE> class.<p>
199 * Has <b> OK </b> status if only one of the listener are triggered. <p>
200 * The following method tests are to be completed successfully before :
202 * <li> <code> addMouseKlickHandler() </code> : adds two key listener </li>
205 public void _removeMouseClickHandler() {
206 requiredMethod("addMouseClickHandler");
208 log
.println("remove mouse listener 2");
210 oObj
.removeMouseClickHandler(m_MouseListener2
);
212 log
.println("starting thread to check the mouse listener...");
213 EventTrigger et
= new EventTrigger(m_XModel
, EventTriggerType
.MOUSE_KLICK_INTO_DOC
);
217 util
.utils
.pause(utils
.DEFAULT_SHORT_WAIT_MS
* 2);
218 log
.println("mouse listener thread should be finished.");
220 boolean bOK
= m_mousePressed1
& m_mouseReleased1
&
221 ! m_mousePressed2
& ! m_mouseReleased2
;
224 log
.println("The mouse listener has not the expectd status:");
225 log
.println("listener\t\texpected\tgot");
226 log
.println("mousePressed1\ttrue\t\t"+m_mousePressed1
);
227 log
.println("mouseReleased1\ttrue\t\t"+m_mouseReleased1
);
228 log
.println("mousePressed2\tfalse\t\t"+m_mousePressed2
);
229 log
.println("mouseReleased2\tfalse\t\t"+m_mouseReleased2
);
232 log
.println("remove mouse listener 1");
233 oObj
.removeMouseClickHandler(m_MouseListener1
);
235 tRes
.tested("removeMouseClickHandler()", bOK
);
240 * Forces environment recreation.
243 protected void after() {
244 disposeEnvironment();
248 * Listener which added and its method must be called
249 * on <code>keyPressed</code> and <code>keyReleased</code> call.
251 public class MyKeyHandler1
implements XKeyHandler
{
253 * This event sets the member <code>m_keyPressed</coed> to
255 * @param oEvent The key event informs about the pressed key.
256 * @return returns <CODE>TRUE</CODE> in erery case
258 public boolean keyPressed( KeyEvent oEvent
){
259 log
.println("XKeyHandler 1: keyPressed-Event");
260 m_keyPressed1
= true;
264 * This event sets the member <code>m_keyReleased</coed> to
266 * @param oEvent The key event informs about the pressed key.
267 * @return returns <CODE>TRUE</CODE> in erery case
269 public boolean keyReleased( KeyEvent oEvent
){
270 log
.println("XKeyHandler 1: keyReleased-Event");
271 m_keyReleased1
= true;
275 * This event does nothing useful
276 * @param oEvent refers to the object that fired the event.
278 public void disposing( EventObject oEvent
){
279 log
.println("XKeyHandler 1: disposing-Event");
283 * Listener which added and its method must be called
284 * on <code>keyPressed</code> and <code>keyReleased</code> call.
286 public class MyKeyHandler2
implements XKeyHandler
{
288 * This event sets the member <code>m_keyPressed</coed> to
290 * @param oEvent The key event informs about the pressed key.
291 * @return returns <CODE>TRUE</CODE> in erery case
293 public boolean keyPressed( KeyEvent oEvent
){
294 log
.println("XKeyHandler 2: keyPressed-Event: " +
295 "This should not be happen because listener is removed!");
296 m_keyPressed2
= true;
300 * This event sets the member <code>m_keyReleased</coed> to
302 * @param oEvent The key event informs about the pressed key.
303 * @return returns <CODE>TRUE</CODE> in erery case
305 public boolean keyReleased( KeyEvent oEvent
){
306 log
.println("XKeyHandler 2: keyReleased-Event: " +
307 "This should not be happen because listener is removed!");
308 m_keyReleased2
= true;
312 * This event does nothing useful
313 * @param oEvent refers to the object that fired the event.
315 public void disposing( EventObject oEvent
){
316 log
.println("XKeyHandler 2: disposing-Event: " +
317 "This should not be happen because listener is removed!");
322 * Listener which added and its method must be called
323 * on <code>mousePressed</code> and <code>mouseReleased</code> call.
325 public class MyMouseClickHandler1
implements XMouseClickHandler
{
327 * This event sets the member <code>m_mousePressed</coed> to
329 * @param oEvent The mouse event informs about the kind of mouse event.
330 * @return returns <CODE>TRUE</CODE> in erery case
332 public boolean mousePressed( MouseEvent oEvent
){
333 log
.println("XMouseClickHandler 1: mousePressed-Event");
334 m_mousePressed1
= true;
338 * This event sets the member <code>m_mouseReleased</coed> to
340 * @param oEvent The mouse event informs about the kind of mouse event.
341 * @return returns <CODE>TRUE</CODE> in erery case
343 public boolean mouseReleased( MouseEvent oEvent
){
344 log
.println("XMouseClickHandler 1: mouseReleased-Event");
345 m_mouseReleased1
= true;
349 * This event does nothing useful
350 * @param oEvent refers to the object that fired the event.
352 public void disposing( EventObject oEvent
){
353 log
.println("XMouseClickHandler 1: disposing-Event");
358 * Listener which added and removed. Its method must NOT be called
359 * on <code>mousePressed</code> and <code>mouseReleased</code> call.
361 public class MyMouseClickHandler2
implements XMouseClickHandler
{
363 * This event sets the member <code>m_mousePressed</coed> to
365 * @param oEvent The mouse event informs about the kind of mouse event.
366 * @return returns <CODE>TRUE</CODE> in erery case
368 public boolean mousePressed( MouseEvent oEvent
){
369 log
.println("XMouseClickHandler 2: mousePressed-Event: " +
370 "This should not be happen because listener is removed!");
371 m_mousePressed2
= true;
375 * This event sets the member <code>m_mouseReleased</coed> to
377 * @param oEvent The mouse event informs about the kind of mouse event.
378 * @return returns <CODE>TRUE</CODE> in erery case
380 public boolean mouseReleased( MouseEvent oEvent
){
381 log
.println("XMouseClickHandler 2: mouseReleased-Event: " +
382 "This should not be happen because listener is removed!");
383 m_mouseReleased2
= true;
387 * This event does nothing useful
388 * @param oEvent refers to the object that fired the event.
390 public void disposing( EventObject oEvent
){
391 log
.println("XMouseClickHandler 2: disposing-Event: " +
392 " This should not be happen because listener is removed!");
397 * To check the events this class is a thread which click a mouse button and
398 * press a key with the <CODE>Robot</CODE> class
399 * @see java.awt.Robot
401 private class EventTrigger
implements Runnable
{
404 * represents an <CODE>EventType</CODE>
405 * @see EventTest.EventTriggerType
407 private final int eventType
;
409 * represents a <CODE>XModel</CODE> of a document
411 private final XModel xModel
;
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
)
422 this.eventType
= eType
;
426 * Triggers the event which is represented by <CODE>eventType</CODE>
429 * <li>EventTest.EventTriggerType.MOUSE_KLICK_INTO_DOC
431 * <li><CODE>clickIntoDoc</CODE></LI>
433 * <li>EventTest.EventTriggerType.KEY_TEXT_INTO_DOC
435 * <li><CODE>clickIntodoc</CODE></LI>
436 * <li><CODE>keyIntoDoc</CODE></LI>
442 switch (this.eventType
){
444 case EventTriggerType
.MOUSE_KLICK_INTO_DOC
:
447 case EventTriggerType
.KEY_TEXT_INTO_DOC
:
455 * This method clicks into the middle 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 click a mouse button
459 * @see java.awt.Robot
461 private void clickIntoDoc(){
464 util
.DesktopTools
.bringWindowToFront(xModel
);
466 XWindow xWindow
= AccessibilityTools
.getCurrentWindow(
469 XAccessible xRoot
= AccessibilityTools
.getAccessibleObject(xWindow
);
473 XAccessibleContext xPanel
= AccessibilityTools
.getAccessibleObjectForRole(xRoot
, AccessibleRole
.PANEL
);
474 XAccessibleComponent xPanelCont
= UnoRuntime
.queryInterface(XAccessibleComponent
.class, xPanel
);
476 // the position of the panel
477 Point point
= xPanelCont
.getLocationOnScreen();
479 // the range of the panel
480 Rectangle rect
= xPanelCont
.getBounds();
483 Robot rob
= new Robot();
484 int x
= point
.X
+ (rect
.Width
/ 2);
485 int y
= point
.Y
+ (rect
.Height
/ 2);
486 log
.println("try to click into the middle of the document");
488 rob
.mousePress(InputEvent
.BUTTON1_MASK
);
489 rob
.mouseRelease(InputEvent
.BUTTON1_MASK
);
490 } catch (java
.awt
.AWTException e
) {
491 log
.println("couldn't press mouse button");
493 } catch (java
.lang
.Exception e
){
494 log
.println("could not click into the scroll bar: " + e
.toString());
499 * This method press the "A" key. Therefore it uses the <CODE>Robot</CODE>
501 * @see java.awt.Robot
503 private void keyIntoDoc(){
505 Robot rob
= new Robot();
506 log
.println("try to press 'A'");
507 rob
.keyPress(java
.awt
.event
.KeyEvent
.VK_A
);
508 rob
.keyRelease(java
.awt
.event
.KeyEvent
.VK_A
);
509 } catch (java
.awt
.AWTException e
) {
510 log
.println("couldn't press key");
516 /** This interface represents all possible actions which could be used
517 * in the <CODE>EventTrigger</CODE> class.
518 * @see EventTest.EventTrigger
520 private interface EventTriggerType
{
522 /** click the mouse into the scroll bar*/
523 int MOUSE_KLICK_INTO_DOC
= 1;
525 /** write some text into a spread sheet*/
526 int KEY_TEXT_INTO_DOC
= 2;