Branch libreoffice-5-0-4
[LibreOffice.git] / framework / qa / complex / XUserInputInterception / EventTest.java
blob86c0051d1f7d74cb77eaa2b27c986fc3890a90ba
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 complex.XUserInputInterception;
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.*;
34 import com.sun.star.lang.*;
35 import com.sun.star.lang.EventObject;
36 import com.sun.star.sheet.XSpreadsheetDocument;
37 import com.sun.star.text.XTextDocument;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XInterface;
40 import com.sun.star.util.*;
41 import java.awt.Robot;
42 import java.awt.event.InputEvent;
44 import util.AccessibilityTools;
45 import util.SOfficeFactory;
47 import org.junit.AfterClass;
48 import org.junit.Before;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
51 import org.openoffice.test.OfficeConnection;
52 import static org.junit.Assert.*;
55 /**
56 * This <CODE>ComplexTest</CODE> checks the interface
57 * <CODE>XUserInputInterception</CODE>. Therefore it creates a document,
58 * adds a mouse and a key listener onto the interface and fire the
59 * correspond events. If all listener works as expected the test resluts in
60 * <CODE>OK</CODE> status.
61 * @short Check the interface XUserInputIntercaption
62 * @descr checks is a simple way the interface XUserInputInteraction
64 public class EventTest {
66 // some const
69 // member
71 /** indicates if the mousePressed event was called*/
72 private boolean m_mousePressed = false;
73 /** indicates if the mouseReleased event was called*/
74 private boolean m_mouseReleased = false;
76 /** indicates if the mousePressed event was called*/
77 private boolean m_keyPressed = false;
78 /** indicates if the mouseReleased event was called*/
79 private boolean m_keyReleased = false;
81 /** points to a global StarOffice factory */
82 private SOfficeFactory m_SOF = null;
84 /**
85 * define the miliseconds to wait until a <CODE>EventTrigger</CODE> thread should
86 * be finished with its work
88 static final int m_threadWait = 3000;
91 // test environment
94 /**
95 * creates the mebmer <CODE>m_xMSF</CODE> and <CODE>m_SOF</CODE>
96 * @short Create the environment for following tests.
97 * @descr create an empty test frame, where we can load
98 * different components inside.
100 @Before public void before() {
101 // create frame instance
102 try {
103 // get a soffice factory object
104 m_SOF = SOfficeFactory.getFactory(getMSF());
106 } catch(java.lang.Throwable ex) {
107 fail("Could not create the XUserInputInterception instance.");
113 * closes the document
114 * @short close the document.
115 * @param xDoc the document to close
117 public void closeDoc(XInterface xDoc) {
118 XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xDoc);
119 try {
120 xClose.close(false);
121 } catch(com.sun.star.util.CloseVetoException exVeto) {
122 System.out.println("document couldn't be closed successfully.");
127 * creates a text document and check the <CODE>XMouseClickHandler</CODE> and
128 * <CODE>XKeyHandler</CODE>
129 * @see com.sun.star.awt.XKeyHandler
130 * @see com.sun.star.awt.XMouseClickHandler
132 @Test public void checkTextDocument(){
134 XTextDocument xDoc = null;
136 try{
137 xDoc = m_SOF.createTextDoc("WriterTest");
138 } catch (com.sun.star.uno.Exception e){
139 fail("Could not create a text document: " +e.toString());
142 checkListener(xDoc);
144 closeDoc(xDoc);
148 * creates an impress document and check the <CODE>XMouseClickHandler</CODE> and
149 * <CODE>XKeyHandler</CODE>
150 * @see com.sun.star.awt.XKeyHandler
151 * @see com.sun.star.awt.XMouseClickHandler
153 @Test public void checkImpressDocument(){
155 XComponent xDoc = null;
157 try{
158 xDoc = m_SOF.createImpressDoc("ImpressTest");
159 } catch (com.sun.star.uno.Exception e){
160 fail("Could not create an impress document: " +e.toString());
163 checkListener(xDoc);
165 closeDoc(xDoc);
169 * creates a math document and check the <CODE>XMouseClickHandler</CODE> and
170 * <CODE>XKeyHandler</CODE>
171 * @see com.sun.star.awt.XKeyHandler
172 * @see com.sun.star.awt.XMouseClickHandler
174 @Test public void checkMathDocument(){
176 XComponent xDoc = null;
178 try{
179 xDoc = m_SOF.createMathDoc("MathTest");
180 } catch (com.sun.star.uno.Exception e){
181 fail("Could not create a math document: " +e.toString());
184 checkListener(xDoc);
186 closeDoc(xDoc);
190 * creates a draw document and check the <CODE>XMouseClickHandler</CODE> and
191 * <CODE>XKeyHandler</CODE>
192 * @see com.sun.star.awt.XKeyHandler
193 * @see com.sun.star.awt.XMouseClickHandler
195 @Test public void checkDrawDocument(){
197 XComponent xDoc = null;
199 try{
200 xDoc = m_SOF.createDrawDoc("DrawTest");
201 } catch (com.sun.star.uno.Exception e){
202 fail("Could not create a draw document: " +e.toString());
205 checkListener(xDoc);
207 closeDoc(xDoc);
211 * creates a calc document and check the <CODE>XMouseClickHandler</CODE> and
212 * <CODE>XKeyHandler</CODE>
213 * @see com.sun.star.awt.XKeyHandler
214 * @see com.sun.star.awt.XMouseClickHandler
216 @Test public void checkCalcDocument(){
218 XSpreadsheetDocument xDoc = null;
220 try{
221 xDoc = m_SOF.createCalcDoc("CalcTest");
222 } catch (com.sun.star.uno.Exception e){
223 fail("Could not create a calc document: " +e.toString());
226 checkListener(xDoc);
227 closeDoc(xDoc);
231 * This is the central test method. It is called by ceck[DOCTYPE]Document. It
232 * creates the <CODE>XUserInputInterception</CODE> from the document and call the
233 * <CODE>checkMouseListener</CODE> test and the <CODE>checkKeyListener</CODE> test
234 * @param xDoc the document to test
236 private void checkListener(XInterface xDoc){
238 XModel xModel = UnoRuntime.queryInterface(XModel.class, xDoc);
240 XUserInputInterception xUII = getUII(xModel);
242 checkMouseListener(xUII, xModel);
243 checkKeyListener(xUII, xModel);
247 * Creates a <CODE>MyKeyHandler</CODE> and adds it to the
248 * <CODE>XUserInputInterception</CODE>. Then an <CODE>EventTrigger</CODE> thread
249 * was created and started.
250 * Has <CODE>OK</CODE> if the members <CODE>m_keyPressed</CODE> and
251 * <CODE>m_keyReleased</CODE> are <CODE>TRUE</CODE>
252 * @param xUII the XUserInputInterception
253 * @param xModel the XModel of a document
254 * @see EventTest.MyKeyHandler
255 * @see EventTest.EventTrigger
257 private void checkKeyListener(XUserInputInterception xUII, XModel xModel) {
258 m_keyPressed = false;
259 m_keyReleased = false;
261 MyKeyHandler keyListener = new MyKeyHandler();
263 xUII.addKeyHandler(keyListener);
265 System.out.println("starting thread to check the key listener...");
266 EventTrigger et = new EventTrigger(xModel, EventTriggerType.KEY_TEXT_INTO_DOC);
268 et.run();
270 util.utils.pause(m_threadWait);
271 System.out.println("key listener thread should be finished.");
273 assertTrue("key event does not work!", m_keyPressed && m_keyReleased);
274 xUII.removeKeyHandler(keyListener);
279 * Creates a <CODE>MyMouseClickHandler</CODE> and adds it to the
280 * <CODE>XUserInputInterception</CODE>. Then an <CODE>EventTrigger</CODE> thread
281 * was created and started.
282 * Has <CODE>OK</CODE> if the members <CODE>m_mousePressed</CODE> and
283 * <CODE>m_mouseReleased</CODE> are <CODE>TRUE</CODE>
284 * @param xUII the XUserInputInterception
285 * @param xModel the XModel of a document
286 * @see EventTest.MyMouseClickHandler
287 * @see EventTest.EventTrigger
289 private void checkMouseListener(XUserInputInterception xUII, XModel xModel) {
291 m_mousePressed = false;
292 m_mouseReleased = false;
294 MyMouseClickHandler mouseListener = new MyMouseClickHandler();
296 xUII.addMouseClickHandler(mouseListener);
298 System.out.println("starting thread to check the mouse listener...");
299 EventTrigger et = new EventTrigger(xModel, EventTriggerType.MOUSE_KLICK_INTO_DOC);
301 et.run();
303 util.utils.pause(m_threadWait);
304 System.out.println("mouse listener thread should be finished.");
306 assertTrue("mouse event does not work!", m_mousePressed && m_mouseReleased);
307 xUII.removeMouseClickHandler(mouseListener);
311 * returns the <CODE>XUserInputInterception</CODE> from the <CODE>XMdoel</CODE>
312 * @param xModel the XModel of a document
313 * @return the <CODE>XUserInputInterception</CODE> of the document
315 private XUserInputInterception getUII(XModel xModel){
317 XController xController = xModel.getCurrentController();
319 XUserInputInterception xUII = UnoRuntime.queryInterface(XUserInputInterception.class, xController);
320 if (xUII == null) {
321 fail("could not get XUserInputInterception from XContoller");
323 return xUII;
327 * Listener which added and its method must be called
328 * on <code>keyPressed</code> and <code>keyReleased</code> call.
330 private class MyKeyHandler implements XKeyHandler {
332 * This event sets the member <code>m_keyPressed</coed> to
333 * <code>true</code>
334 * @param oEvent The key event informs about the pressed key.
335 * @return returns <CODE>TRUE</CODE> in erery case
337 public boolean keyPressed( KeyEvent oEvent ){
338 System.out.println("XKeyHandler: keyPressed-Event");
339 m_keyPressed = true;
340 return true;
343 * This event sets the member <code>m_keyReleased</coed> to
344 * <code>true</code>
345 * @param oEvent The key event informs about the pressed key.
346 * @return returns <CODE>TRUE</CODE> in erery case
348 public boolean keyReleased( KeyEvent oEvent ){
349 System.out.println("XKeyHandler: keyReleased-Event");
350 m_keyReleased = true;
351 return true;
354 * This event does nothing useful
355 * @param oEvent refers to the object that fired the event.
357 public void disposing( EventObject oEvent ){
358 System.out.println("XKeyHandler: disposing-Event");
363 * Listener which added and its method must be called
364 * on <code>mousePressed</code> and <code>mouseReleased</code> call.
366 private class MyMouseClickHandler implements XMouseClickHandler {
368 * This event sets the member <code>m_mousePressed</coed> to
369 * <code>true</code>
370 * @param oEvent The mouse event informs about the kind of mouse event.
371 * @return returns <CODE>TRUE</CODE> in erery case
373 public boolean mousePressed( MouseEvent oEvent ){
374 System.out.println("XMouseClickHandler: mousePressed-Event");
375 m_mousePressed = true;
376 return true;
379 * This event sets the member <code>m_mouseReleased</coed> to
380 * <code>true</code>
381 * @param oEvent The mouse event informs about the kind of mouse event.
382 * @return returns <CODE>TRUE</CODE> in erery case
384 public boolean mouseReleased( MouseEvent oEvent ){
385 System.out.println("XMouseClickHandler: mouseReleased-Event");
386 m_mouseReleased = true;
387 return true;
390 * This event does nothing useful
391 * @param oEvent refers to the object that fired the event.
393 public void disposing( EventObject oEvent ){
394 System.out.println("XMouseClickHandler: disposing-Event");
399 * To check the events this class is a thread which click a mouse button and
400 * press a key with the <CODE>Robot</CODE> class
401 * @see java.awt.Robot
403 private class EventTrigger implements Runnable {
406 * represents an <CODE>EventType</CODE>
407 * @see EventTest.EventTriggerType
409 private final int eventType;
411 * represents a <CODE>XModel</CODE> of a document
413 private final XModel xModel;
416 * Creates an instacne of this class. The parameter <CODE>eType</CODE> represents
417 * the kind of event which will be triggert at <CODE>run()</CODE>
418 * @param model the model of a document
419 * @param eType the kind of event which should be trigger
421 public EventTrigger(XModel model, int eType)
423 this.xModel = model;
424 this.eventType = eType;
428 * Triggers the event which is represented by <CODE>eventType</CODE>
429 * The scenarios are:
430 * <ul>
431 * <li>EventTest.EventTriggerType.MOUSE_KLICK_INTO_DOC
432 * which calls
433 * <li><CODE>clickIntoDoc</CODE></LI>
434 * </LI>
435 * <li>EventTest.EventTriggerType.KEY_TEXT_INTO_DOC
436 * which calls
437 * <li><CODE>clickIntodoc</CODE></LI>
438 * <li><CODE>keyIntoDoc</CODE></LI>
439 * </LI>
440 * </UL>
442 public void run(){
444 switch (this.eventType){
446 case EventTriggerType.MOUSE_KLICK_INTO_DOC:
447 clickIntoDoc();
448 break;
449 case EventTriggerType.KEY_TEXT_INTO_DOC:
450 clickIntoDoc();
451 keyIntoDoc();
452 break;
457 * This method clicks into the middle of a document. It uses Accessibility
458 * to get the document and query for its position and its range to calculate
459 * the middle. This values was used for <CODE>Robot</CODE> Class. This
460 * Robot class is able to move the mouse and to click a mouse button
461 * @see java.awt.Robot
463 private void clickIntoDoc(){
464 try{
465 // get the position and the range of a scroll bar
467 XWindow xWindow = AccessibilityTools.getCurrentWindow(
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 System.out.println("try to click 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 System.out.println("couldn't press mouse button");
494 } catch (java.lang.Exception e){
495 System.out.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 System.out.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 System.out.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 /** click the mouse into the scroll bar*/
524 int MOUSE_KLICK_INTO_DOC = 1;
526 /** write some text into a spread sheet*/
527 int KEY_TEXT_INTO_DOC = 2;
533 private XMultiServiceFactory getMSF()
535 return UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
538 // setup and close connections
539 @BeforeClass public static void setUpConnection() throws Exception {
540 System.out.println("setUpConnection()");
541 connection.setUp();
544 @AfterClass public static void tearDownConnection()
545 throws InterruptedException, com.sun.star.uno.Exception
547 System.out.println("tearDownConnection()");
548 connection.tearDown();
551 private static final OfficeConnection connection = new OfficeConnection();