1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir package complex.XUserInputInterception; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleRole; 31*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible; 32*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleComponent; 33*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleContext; 34*cdf0e10cSrcweir import com.sun.star.awt.KeyEvent; 35*cdf0e10cSrcweir import com.sun.star.awt.MouseEvent; 36*cdf0e10cSrcweir import com.sun.star.awt.Point; 37*cdf0e10cSrcweir import com.sun.star.awt.Rectangle; 38*cdf0e10cSrcweir import com.sun.star.awt.XKeyHandler; 39*cdf0e10cSrcweir import com.sun.star.awt.XMouseClickHandler; 40*cdf0e10cSrcweir import com.sun.star.awt.XUserInputInterception; 41*cdf0e10cSrcweir import com.sun.star.awt.XWindow; 42*cdf0e10cSrcweir import com.sun.star.chart.XChartDocument; 43*cdf0e10cSrcweir import com.sun.star.frame.*; 44*cdf0e10cSrcweir import com.sun.star.lang.*; 45*cdf0e10cSrcweir import com.sun.star.lang.EventObject; 46*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument; 47*cdf0e10cSrcweir import com.sun.star.text.XTextDocument; 48*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 49*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 50*cdf0e10cSrcweir import com.sun.star.util.*; 51*cdf0e10cSrcweir import java.awt.Robot; 52*cdf0e10cSrcweir import java.awt.event.InputEvent; 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir import util.AccessibilityTools; 55*cdf0e10cSrcweir import util.SOfficeFactory; 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir // ---------- junit imports ----------------- 58*cdf0e10cSrcweir import org.junit.After; 59*cdf0e10cSrcweir import org.junit.AfterClass; 60*cdf0e10cSrcweir import org.junit.Before; 61*cdf0e10cSrcweir import org.junit.BeforeClass; 62*cdf0e10cSrcweir import org.junit.Test; 63*cdf0e10cSrcweir import org.openoffice.test.OfficeConnection; 64*cdf0e10cSrcweir import static org.junit.Assert.*; 65*cdf0e10cSrcweir // ------------------------------------------ 66*cdf0e10cSrcweir //----------------------------------------------- 67*cdf0e10cSrcweir /** 68*cdf0e10cSrcweir * This <CODE>ComplexTest</CODE> checks the interface 69*cdf0e10cSrcweir * <CODE>XUserInputInterception</CODE>. Therefore it creates a document, 70*cdf0e10cSrcweir * adds a mouse and a key listener onto the interface and fire the 71*cdf0e10cSrcweir * correspond events. If all listener works as expected the test resluts in 72*cdf0e10cSrcweir * <CODE>OK</CODE> status. 73*cdf0e10cSrcweir * @short Check the interface XUserInputIntercaption 74*cdf0e10cSrcweir * @descr checks is a simple way the interface XUserInputInteraction 75*cdf0e10cSrcweir */ 76*cdf0e10cSrcweir public class EventTest { 77*cdf0e10cSrcweir //------------------------------------------- 78*cdf0e10cSrcweir // some const 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir //------------------------------------------- 81*cdf0e10cSrcweir // member 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir /** points to the global uno service manager. */ 84*cdf0e10cSrcweir private XMultiServiceFactory m_xMSF = null; 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir /** indicates if the mousePressed event was called*/ 87*cdf0e10cSrcweir private boolean m_mousePressed = false; 88*cdf0e10cSrcweir /** indicates if the mouseReleased event was called*/ 89*cdf0e10cSrcweir private boolean m_mouseReleased = false; 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir /** indicates if the mousePressed event was called*/ 92*cdf0e10cSrcweir private boolean m_keyPressed = false; 93*cdf0e10cSrcweir /** indicates if the mouseReleased event was called*/ 94*cdf0e10cSrcweir private boolean m_keyReleased = false; 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir /** points to a global StarOffice factory */ 97*cdf0e10cSrcweir private SOfficeFactory m_SOF = null; 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir /** 100*cdf0e10cSrcweir * define the miliseconds to wait until a <CODE>EventTrigger</CODE> thread should 101*cdf0e10cSrcweir * be finished with its work 102*cdf0e10cSrcweir */ 103*cdf0e10cSrcweir final int m_threadWait = 3000; 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir //------------------------------------------- 106*cdf0e10cSrcweir // test environment 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir //------------------------------------------- 109*cdf0e10cSrcweir /** 110*cdf0e10cSrcweir * The test methods are: 111*cdf0e10cSrcweir * <ul> 112*cdf0e10cSrcweir * <li><CODE>checkTextDocument</CODE></LI> 113*cdf0e10cSrcweir * <li><CODE>checkCalcDocument</CODE></LI> 114*cdf0e10cSrcweir * <li><CODE>checkDrawDocument</CODE></LI> 115*cdf0e10cSrcweir * <li><CODE>checkImpressDocument</CODE></LI> 116*cdf0e10cSrcweir * <li><CODE>checkChartDocument</CODE></LI> 117*cdf0e10cSrcweir * <li><CODE>checkMathDocument</CODE></li> 118*cdf0e10cSrcweir * </ul> 119*cdf0e10cSrcweir * @short A function to tell the framework, 120*cdf0e10cSrcweir * which test functions are available. 121*cdf0e10cSrcweir * @return All test methods. 122*cdf0e10cSrcweir * @todo Think about selection of tests from outside ... 123*cdf0e10cSrcweir */ 124*cdf0e10cSrcweir // public String[] getTestMethodNames() { 125*cdf0e10cSrcweir // return new String[] 126*cdf0e10cSrcweir // { "checkTextDocument", 127*cdf0e10cSrcweir // "checkCalcDocument", 128*cdf0e10cSrcweir // "checkDrawDocument", 129*cdf0e10cSrcweir // "checkImpressDocument", 130*cdf0e10cSrcweir // "checkChartDocument", 131*cdf0e10cSrcweir // "checkMathDocument", 132*cdf0e10cSrcweir // }; 133*cdf0e10cSrcweir // } 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir //------------------------------------------- 136*cdf0e10cSrcweir /** 137*cdf0e10cSrcweir * creates the mebmer <CODE>m_xMSF</CODE> and <CODE>m_SOF</CODE> 138*cdf0e10cSrcweir * @short Create the environment for following tests. 139*cdf0e10cSrcweir * @descr create an empty test frame, where we can load 140*cdf0e10cSrcweir * different components inside. 141*cdf0e10cSrcweir */ 142*cdf0e10cSrcweir @Before public void before() { 143*cdf0e10cSrcweir // get uno service manager from global test environment 144*cdf0e10cSrcweir m_xMSF = getMSF(); 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir // create frame instance 147*cdf0e10cSrcweir try { 148*cdf0e10cSrcweir // get a soffice factory object 149*cdf0e10cSrcweir m_SOF = SOfficeFactory.getFactory(getMSF()); 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir } catch(java.lang.Throwable ex) { 152*cdf0e10cSrcweir fail("Could not create the XUserInputInterception instance."); 153*cdf0e10cSrcweir } 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir //------------------------------------------- 157*cdf0e10cSrcweir /** 158*cdf0e10cSrcweir * closes the document 159*cdf0e10cSrcweir * @short close the document. 160*cdf0e10cSrcweir * @param xDoc the document to close 161*cdf0e10cSrcweir */ 162*cdf0e10cSrcweir public void closeDoc(XInterface xDoc) { 163*cdf0e10cSrcweir XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, xDoc); 164*cdf0e10cSrcweir try { 165*cdf0e10cSrcweir xClose.close(false); 166*cdf0e10cSrcweir } catch(com.sun.star.util.CloseVetoException exVeto) { 167*cdf0e10cSrcweir System.out.println("document couldn't be closed successfully."); 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir /** 172*cdf0e10cSrcweir * creates a text document and check the <CODE>XMouseClickHandler</CODE> and 173*cdf0e10cSrcweir * <CODE>XKeyHandler</CODE> 174*cdf0e10cSrcweir * @see com.sun.star.awt.XKeyHandler 175*cdf0e10cSrcweir * @see com.sun.star.awt.XMouseClickHandler 176*cdf0e10cSrcweir */ 177*cdf0e10cSrcweir @Test public void checkTextDocument(){ 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir XTextDocument xDoc = null; 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir try{ 182*cdf0e10cSrcweir xDoc = m_SOF.createTextDoc("WriterTest"); 183*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e){ 184*cdf0e10cSrcweir fail("Could not create a text document: " +e.toString()); 185*cdf0e10cSrcweir } 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir checkListener(xDoc); 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir closeDoc(xDoc); 190*cdf0e10cSrcweir } 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir /** 193*cdf0e10cSrcweir * creates an impress document and check the <CODE>XMouseClickHandler</CODE> and 194*cdf0e10cSrcweir * <CODE>XKeyHandler</CODE> 195*cdf0e10cSrcweir * @see com.sun.star.awt.XKeyHandler 196*cdf0e10cSrcweir * @see com.sun.star.awt.XMouseClickHandler 197*cdf0e10cSrcweir */ 198*cdf0e10cSrcweir @Test public void checkImpressDocument(){ 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir XComponent xDoc = null; 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir try{ 203*cdf0e10cSrcweir xDoc = m_SOF.createImpressDoc("ImpressTest"); 204*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e){ 205*cdf0e10cSrcweir fail("Could not create an impress document: " +e.toString()); 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir checkListener(xDoc); 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir closeDoc(xDoc); 211*cdf0e10cSrcweir } 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir /** 214*cdf0e10cSrcweir * creates a chart document and check the <CODE>XMouseClickHandler</CODE> and 215*cdf0e10cSrcweir * <CODE>XKeyHandler</CODE> 216*cdf0e10cSrcweir * @see com.sun.star.awt.XKeyHandler 217*cdf0e10cSrcweir * @see com.sun.star.awt.XMouseClickHandler 218*cdf0e10cSrcweir */ 219*cdf0e10cSrcweir // TODO! 220*cdf0e10cSrcweir // @Test public void checkChartDocument(){ 221*cdf0e10cSrcweir // 222*cdf0e10cSrcweir // XChartDocument xDoc = null; 223*cdf0e10cSrcweir // 224*cdf0e10cSrcweir // try{ 225*cdf0e10cSrcweir // xDoc = m_SOF.createChartDoc("ChartTest"); 226*cdf0e10cSrcweir // } catch (com.sun.star.uno.Exception e){ 227*cdf0e10cSrcweir // fail("Could not create a chart document: " +e.toString()); 228*cdf0e10cSrcweir // } 229*cdf0e10cSrcweir // 230*cdf0e10cSrcweir // checkListener(xDoc); 231*cdf0e10cSrcweir // 232*cdf0e10cSrcweir // closeDoc(xDoc); 233*cdf0e10cSrcweir // } 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir /** 236*cdf0e10cSrcweir * creates a math document and check the <CODE>XMouseClickHandler</CODE> and 237*cdf0e10cSrcweir * <CODE>XKeyHandler</CODE> 238*cdf0e10cSrcweir * @see com.sun.star.awt.XKeyHandler 239*cdf0e10cSrcweir * @see com.sun.star.awt.XMouseClickHandler 240*cdf0e10cSrcweir */ 241*cdf0e10cSrcweir @Test public void checkMathDocument(){ 242*cdf0e10cSrcweir 243*cdf0e10cSrcweir XComponent xDoc = null; 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir try{ 246*cdf0e10cSrcweir xDoc = m_SOF.createMathDoc("MathTest"); 247*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e){ 248*cdf0e10cSrcweir fail("Could not create a math document: " +e.toString()); 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir checkListener(xDoc); 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir closeDoc(xDoc); 254*cdf0e10cSrcweir } 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir /** 257*cdf0e10cSrcweir * creates a draw document and check the <CODE>XMouseClickHandler</CODE> and 258*cdf0e10cSrcweir * <CODE>XKeyHandler</CODE> 259*cdf0e10cSrcweir * @see com.sun.star.awt.XKeyHandler 260*cdf0e10cSrcweir * @see com.sun.star.awt.XMouseClickHandler 261*cdf0e10cSrcweir */ 262*cdf0e10cSrcweir @Test public void checkDrawDocument(){ 263*cdf0e10cSrcweir 264*cdf0e10cSrcweir XComponent xDoc = null; 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir try{ 267*cdf0e10cSrcweir xDoc = m_SOF.createDrawDoc("DrawTest"); 268*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e){ 269*cdf0e10cSrcweir fail("Could not create a draw document: " +e.toString()); 270*cdf0e10cSrcweir } 271*cdf0e10cSrcweir 272*cdf0e10cSrcweir checkListener(xDoc); 273*cdf0e10cSrcweir 274*cdf0e10cSrcweir closeDoc(xDoc); 275*cdf0e10cSrcweir } 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir /** 278*cdf0e10cSrcweir * creates a calc document and check the <CODE>XMouseClickHandler</CODE> and 279*cdf0e10cSrcweir * <CODE>XKeyHandler</CODE> 280*cdf0e10cSrcweir * @see com.sun.star.awt.XKeyHandler 281*cdf0e10cSrcweir * @see com.sun.star.awt.XMouseClickHandler 282*cdf0e10cSrcweir */ 283*cdf0e10cSrcweir @Test public void checkCalcDocument(){ 284*cdf0e10cSrcweir 285*cdf0e10cSrcweir XSpreadsheetDocument xDoc = null; 286*cdf0e10cSrcweir 287*cdf0e10cSrcweir try{ 288*cdf0e10cSrcweir xDoc = m_SOF.createCalcDoc("CalcTest"); 289*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e){ 290*cdf0e10cSrcweir fail("Could not create a calc document: " +e.toString()); 291*cdf0e10cSrcweir } 292*cdf0e10cSrcweir 293*cdf0e10cSrcweir checkListener(xDoc); 294*cdf0e10cSrcweir closeDoc(xDoc); 295*cdf0e10cSrcweir } 296*cdf0e10cSrcweir 297*cdf0e10cSrcweir /** 298*cdf0e10cSrcweir * This is the central test method. It is called by ceck[DOCTYPE]Document. It 299*cdf0e10cSrcweir * creates the <CODE>XUserInputInterception</CODE> from the document and call the 300*cdf0e10cSrcweir * <CODE>checkMouseListener</CODE> test and the <CODE>checkKeyListener</CODE> test 301*cdf0e10cSrcweir * @param xDoc the document to test 302*cdf0e10cSrcweir */ 303*cdf0e10cSrcweir private void checkListener(XInterface xDoc){ 304*cdf0e10cSrcweir 305*cdf0e10cSrcweir XModel xModel = UnoRuntime.queryInterface(XModel.class, xDoc); 306*cdf0e10cSrcweir 307*cdf0e10cSrcweir XUserInputInterception xUII = getUII(xModel); 308*cdf0e10cSrcweir 309*cdf0e10cSrcweir checkMouseListener(xUII, xModel); 310*cdf0e10cSrcweir checkKeyListener(xUII, xModel); 311*cdf0e10cSrcweir } 312*cdf0e10cSrcweir 313*cdf0e10cSrcweir /** 314*cdf0e10cSrcweir * Creates a <CODE>MyKeyHandler</CODE> and adds it to the 315*cdf0e10cSrcweir * <CODE>XUserInputInterception</CODE>. Then an <CODE>EventTrigger</CODE> thread 316*cdf0e10cSrcweir * was created and started. 317*cdf0e10cSrcweir * Has <CODE>OK</CODE> if the members <CODE>m_keyPressed</CODE> and 318*cdf0e10cSrcweir * <CODE>m_keyReleased</CODE> are <CODE>TRUE</CODE> 319*cdf0e10cSrcweir * @param xUII the XUserInputInterception 320*cdf0e10cSrcweir * @param xModel the XModel of a document 321*cdf0e10cSrcweir * @see EventTest.MyKeyHander 322*cdf0e10cSrcweir * @see EventTest.EventTrigger 323*cdf0e10cSrcweir */ 324*cdf0e10cSrcweir private void checkKeyListener(XUserInputInterception xUII, XModel xModel) { 325*cdf0e10cSrcweir m_keyPressed = false; 326*cdf0e10cSrcweir m_keyReleased = false; 327*cdf0e10cSrcweir 328*cdf0e10cSrcweir MyKeyHandler keyListener = new MyKeyHandler(); 329*cdf0e10cSrcweir 330*cdf0e10cSrcweir xUII.addKeyHandler(keyListener); 331*cdf0e10cSrcweir 332*cdf0e10cSrcweir System.out.println("starting thread to check the key listener..."); 333*cdf0e10cSrcweir EventTrigger et = new EventTrigger(xModel, EventTriggerType.KEY_TEXT_INTO_DOC); 334*cdf0e10cSrcweir 335*cdf0e10cSrcweir et.run(); 336*cdf0e10cSrcweir 337*cdf0e10cSrcweir util.utils.shortWait(m_threadWait); 338*cdf0e10cSrcweir System.out.println("key listener thread should be finished."); 339*cdf0e10cSrcweir 340*cdf0e10cSrcweir assertTrue("key event does not work!", m_keyPressed && m_keyReleased); 341*cdf0e10cSrcweir xUII.removeKeyHandler(keyListener); 342*cdf0e10cSrcweir 343*cdf0e10cSrcweir } 344*cdf0e10cSrcweir 345*cdf0e10cSrcweir /** 346*cdf0e10cSrcweir * Creates a <CODE>MyMouseClickHandler</CODE> and adds it to the 347*cdf0e10cSrcweir * <CODE>XUserInputInterception</CODE>. Then an <CODE>EventTrigger</CODE> thread 348*cdf0e10cSrcweir * was created and started. 349*cdf0e10cSrcweir * Has <CODE>OK</CODE> if the members <CODE>m_mousePressed</CODE> and 350*cdf0e10cSrcweir * <CODE>m_mouseReleased</CODE> are <CODE>TRUE</CODE> 351*cdf0e10cSrcweir * @param xUII the XUserInputInterception 352*cdf0e10cSrcweir * @param xModel the XModel of a document 353*cdf0e10cSrcweir * @see EventTest.MyMouseClickHander 354*cdf0e10cSrcweir * @see EventTest.EventTrigger 355*cdf0e10cSrcweir */ 356*cdf0e10cSrcweir 357*cdf0e10cSrcweir private void checkMouseListener(XUserInputInterception xUII, XModel xModel) { 358*cdf0e10cSrcweir 359*cdf0e10cSrcweir m_mousePressed = false; 360*cdf0e10cSrcweir m_mouseReleased = false; 361*cdf0e10cSrcweir 362*cdf0e10cSrcweir MyMouseClickHandler mouseListener = new MyMouseClickHandler(); 363*cdf0e10cSrcweir 364*cdf0e10cSrcweir xUII.addMouseClickHandler(mouseListener); 365*cdf0e10cSrcweir 366*cdf0e10cSrcweir System.out.println("starting thread to check the mouse listener..."); 367*cdf0e10cSrcweir EventTrigger et = new EventTrigger(xModel, EventTriggerType.MOUSE_KLICK_INTO_DOC); 368*cdf0e10cSrcweir 369*cdf0e10cSrcweir et.run(); 370*cdf0e10cSrcweir 371*cdf0e10cSrcweir util.utils.shortWait(m_threadWait); 372*cdf0e10cSrcweir System.out.println("mouse listener thread should be finished."); 373*cdf0e10cSrcweir 374*cdf0e10cSrcweir assertTrue("mouse event does not work!", m_mousePressed && m_mouseReleased); 375*cdf0e10cSrcweir xUII.removeMouseClickHandler(mouseListener); 376*cdf0e10cSrcweir } 377*cdf0e10cSrcweir 378*cdf0e10cSrcweir /** 379*cdf0e10cSrcweir * returns the <CODE>XUserInputInterception</CODE> from the <CODE>XMdoel</CODE> 380*cdf0e10cSrcweir * @param xModel the XModel of a document 381*cdf0e10cSrcweir * @return the <CODE>XUserInputInterception</CODE> of the document 382*cdf0e10cSrcweir */ 383*cdf0e10cSrcweir private XUserInputInterception getUII(XModel xModel){ 384*cdf0e10cSrcweir 385*cdf0e10cSrcweir XController xController = xModel.getCurrentController(); 386*cdf0e10cSrcweir 387*cdf0e10cSrcweir XUserInputInterception xUII = UnoRuntime.queryInterface(XUserInputInterception.class, xController); 388*cdf0e10cSrcweir if (xUII == null) { 389*cdf0e10cSrcweir fail("could not get XUserInputInterception from XContoller"); 390*cdf0e10cSrcweir } 391*cdf0e10cSrcweir return xUII; 392*cdf0e10cSrcweir } 393*cdf0e10cSrcweir 394*cdf0e10cSrcweir /** 395*cdf0e10cSrcweir * Listener which added and its method must be called 396*cdf0e10cSrcweir * on <code>keyPressed</code> and <code>keyReleased</code> call. 397*cdf0e10cSrcweir */ 398*cdf0e10cSrcweir public class MyKeyHandler implements XKeyHandler { 399*cdf0e10cSrcweir /** 400*cdf0e10cSrcweir * This event sets the member <code>m_keyPressed</coed> to 401*cdf0e10cSrcweir * <code>true</code> 402*cdf0e10cSrcweir * @param oEvent The key event informs about the pressed key. 403*cdf0e10cSrcweir * @return returns <CODE>TRUE</CODE> in erery case 404*cdf0e10cSrcweir */ 405*cdf0e10cSrcweir public boolean keyPressed( KeyEvent oEvent ){ 406*cdf0e10cSrcweir System.out.println("XKeyHandler: keyPressed-Event"); 407*cdf0e10cSrcweir m_keyPressed = true; 408*cdf0e10cSrcweir return true; 409*cdf0e10cSrcweir } 410*cdf0e10cSrcweir /** 411*cdf0e10cSrcweir * This event sets the member <code>m_keyReleased</coed> to 412*cdf0e10cSrcweir * <code>true</code> 413*cdf0e10cSrcweir * @param oEvent The key event informs about the pressed key. 414*cdf0e10cSrcweir * @return returns <CODE>TRUE</CODE> in erery case 415*cdf0e10cSrcweir */ 416*cdf0e10cSrcweir public boolean keyReleased( KeyEvent oEvent ){ 417*cdf0e10cSrcweir System.out.println("XKeyHandler: keyReleased-Event"); 418*cdf0e10cSrcweir m_keyReleased = true; 419*cdf0e10cSrcweir return true; 420*cdf0e10cSrcweir } 421*cdf0e10cSrcweir /** 422*cdf0e10cSrcweir * This event does nothing usefull 423*cdf0e10cSrcweir * @param oEvent refers to the object that fired the event. 424*cdf0e10cSrcweir */ 425*cdf0e10cSrcweir public void disposing( EventObject oEvent ){ 426*cdf0e10cSrcweir System.out.println("XKeyHandler: disposing-Event"); 427*cdf0e10cSrcweir } 428*cdf0e10cSrcweir } 429*cdf0e10cSrcweir 430*cdf0e10cSrcweir /** 431*cdf0e10cSrcweir * Listener which added and its method must be called 432*cdf0e10cSrcweir * on <code>mousePressed</code> and <code>mouseReleased</code> call. 433*cdf0e10cSrcweir */ 434*cdf0e10cSrcweir public class MyMouseClickHandler implements XMouseClickHandler { 435*cdf0e10cSrcweir /** 436*cdf0e10cSrcweir * This event sets the member <code>m_mousePressed</coed> to 437*cdf0e10cSrcweir * <code>true</code> 438*cdf0e10cSrcweir * @param oEvent The mouse event informs about the kind of mouse event. 439*cdf0e10cSrcweir * @return returns <CODE>TRUE</CODE> in erery case 440*cdf0e10cSrcweir */ 441*cdf0e10cSrcweir public boolean mousePressed( MouseEvent oEvent ){ 442*cdf0e10cSrcweir System.out.println("XMouseClickHandler: mousePressed-Event"); 443*cdf0e10cSrcweir m_mousePressed = true; 444*cdf0e10cSrcweir return true; 445*cdf0e10cSrcweir } 446*cdf0e10cSrcweir /** 447*cdf0e10cSrcweir * This event sets the member <code>m_mouseReleased</coed> to 448*cdf0e10cSrcweir * <code>true</code> 449*cdf0e10cSrcweir * @param oEvent The mouse event informs about the kind of mouse event. 450*cdf0e10cSrcweir * @return returns <CODE>TRUE</CODE> in erery case 451*cdf0e10cSrcweir */ 452*cdf0e10cSrcweir public boolean mouseReleased( MouseEvent oEvent ){ 453*cdf0e10cSrcweir System.out.println("XMouseClickHandler: mouseReleased-Event"); 454*cdf0e10cSrcweir m_mouseReleased = true; 455*cdf0e10cSrcweir return true; 456*cdf0e10cSrcweir } 457*cdf0e10cSrcweir /** 458*cdf0e10cSrcweir * This event does nothing usefull 459*cdf0e10cSrcweir * @param oEvent refers to the object that fired the event. 460*cdf0e10cSrcweir */ 461*cdf0e10cSrcweir public void disposing( EventObject oEvent ){ 462*cdf0e10cSrcweir System.out.println("XMouseClickHandler: disposing-Event"); 463*cdf0e10cSrcweir } 464*cdf0e10cSrcweir }; 465*cdf0e10cSrcweir 466*cdf0e10cSrcweir /** 467*cdf0e10cSrcweir * To check the events this class is a thread which click a mouse button and 468*cdf0e10cSrcweir * press a key with the <CODE>Robot</CODE> class 469*cdf0e10cSrcweir * @see java.awt.Robot 470*cdf0e10cSrcweir */ 471*cdf0e10cSrcweir private class EventTrigger extends Thread{ 472*cdf0e10cSrcweir 473*cdf0e10cSrcweir /** 474*cdf0e10cSrcweir * represents a <CODE>AccessibilityTools</CODE> 475*cdf0e10cSrcweir */ 476*cdf0e10cSrcweir private final AccessibilityTools at = new AccessibilityTools(); 477*cdf0e10cSrcweir /** 478*cdf0e10cSrcweir * represents an <CODE>EventType</CODE> 479*cdf0e10cSrcweir * @see EventTest.EventTriggerType 480*cdf0e10cSrcweir */ 481*cdf0e10cSrcweir private int eventType = 0; 482*cdf0e10cSrcweir /** 483*cdf0e10cSrcweir * represents a <CODE>XModel</CODE> of a document 484*cdf0e10cSrcweir */ 485*cdf0e10cSrcweir private XModel xModel = null; 486*cdf0e10cSrcweir 487*cdf0e10cSrcweir /** 488*cdf0e10cSrcweir * Creates an instacne of this class. The parameter <CODE>eType</CODE> represents 489*cdf0e10cSrcweir * the kind of event wich will be triggert at <CODE>run()</CODE> 490*cdf0e10cSrcweir * @param model the model of a document 491*cdf0e10cSrcweir * @param eType the kind of event which should be trigger 492*cdf0e10cSrcweir */ 493*cdf0e10cSrcweir public EventTrigger(XModel model, int eType) 494*cdf0e10cSrcweir { 495*cdf0e10cSrcweir this.xModel = model; 496*cdf0e10cSrcweir this.eventType = eType; 497*cdf0e10cSrcweir } 498*cdf0e10cSrcweir 499*cdf0e10cSrcweir /** 500*cdf0e10cSrcweir * Triggers the event wich is represented by <CODE>eventType</CODE> 501*cdf0e10cSrcweir * The scenarios are: 502*cdf0e10cSrcweir * <ul> 503*cdf0e10cSrcweir * <li>EventTest.EventTriggerType.MOUSE_KLICK_INTO_DOC 504*cdf0e10cSrcweir * which calls 505*cdf0e10cSrcweir * <li><CODE>clickIntoDoc</CODE></LI> 506*cdf0e10cSrcweir * </LI> 507*cdf0e10cSrcweir * <li>EventTest.EventTriggerType.KEY_TEXT_INTO_DOC 508*cdf0e10cSrcweir * which calls 509*cdf0e10cSrcweir * <li><CODE>clickIntodoc</CODE></LI> 510*cdf0e10cSrcweir * <li><CODE>keyIntoDoc</CODE></LI> 511*cdf0e10cSrcweir * </LI> 512*cdf0e10cSrcweir * </UL> 513*cdf0e10cSrcweir */ 514*cdf0e10cSrcweir public void run(){ 515*cdf0e10cSrcweir 516*cdf0e10cSrcweir switch (this.eventType){ 517*cdf0e10cSrcweir 518*cdf0e10cSrcweir case EventTriggerType.MOUSE_KLICK_INTO_DOC: 519*cdf0e10cSrcweir clickIntoDoc(); 520*cdf0e10cSrcweir break; 521*cdf0e10cSrcweir case EventTriggerType.KEY_TEXT_INTO_DOC: 522*cdf0e10cSrcweir clickIntoDoc(); 523*cdf0e10cSrcweir keyIntoDoc(); 524*cdf0e10cSrcweir break; 525*cdf0e10cSrcweir 526*cdf0e10cSrcweir } 527*cdf0e10cSrcweir } 528*cdf0e10cSrcweir /** 529*cdf0e10cSrcweir * This method cklicks into the middel of a document. It uses Accessibility 530*cdf0e10cSrcweir * to get the document and query for its position and its range to calculate 531*cdf0e10cSrcweir * the middle. This values was used for <CODE>Robot</CODE> Class. This 532*cdf0e10cSrcweir * Robot class is able to move the mouse and to cklick a mouse button 533*cdf0e10cSrcweir * @see java.awt.Robot 534*cdf0e10cSrcweir */ 535*cdf0e10cSrcweir private void clickIntoDoc(){ 536*cdf0e10cSrcweir try{ 537*cdf0e10cSrcweir // get the position and the range of a scroll bar 538*cdf0e10cSrcweir 539*cdf0e10cSrcweir XWindow xWindow = at.getCurrentWindow( 540*cdf0e10cSrcweir getMSF(), 541*cdf0e10cSrcweir xModel); 542*cdf0e10cSrcweir 543*cdf0e10cSrcweir XAccessible xRoot = at.getAccessibleObject(xWindow); 544*cdf0e10cSrcweir 545*cdf0e10cSrcweir 546*cdf0e10cSrcweir 547*cdf0e10cSrcweir XAccessibleContext xPanel = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL); 548*cdf0e10cSrcweir XAccessibleComponent xPanelCont = UnoRuntime.queryInterface(XAccessibleComponent.class, xPanel); 549*cdf0e10cSrcweir 550*cdf0e10cSrcweir // the position of the panel 551*cdf0e10cSrcweir Point point = xPanelCont.getLocationOnScreen(); 552*cdf0e10cSrcweir 553*cdf0e10cSrcweir // the range of the panel 554*cdf0e10cSrcweir Rectangle rect = xPanelCont.getBounds(); 555*cdf0e10cSrcweir 556*cdf0e10cSrcweir try { 557*cdf0e10cSrcweir Robot rob = new Robot(); 558*cdf0e10cSrcweir int x = point.X + (rect.Width / 2); 559*cdf0e10cSrcweir int y = point.Y + (rect.Height / 2); 560*cdf0e10cSrcweir System.out.println("try to klick into the middle of the document"); 561*cdf0e10cSrcweir rob.mouseMove(x, y); 562*cdf0e10cSrcweir rob.mousePress(InputEvent.BUTTON1_MASK); 563*cdf0e10cSrcweir rob.mouseRelease(InputEvent.BUTTON1_MASK); 564*cdf0e10cSrcweir } catch (java.awt.AWTException e) { 565*cdf0e10cSrcweir System.out.println("couldn't press mouse button"); 566*cdf0e10cSrcweir } 567*cdf0e10cSrcweir } catch (java.lang.Exception e){ 568*cdf0e10cSrcweir System.out.println("could not click into the scroll bar: " + e.toString()); 569*cdf0e10cSrcweir } 570*cdf0e10cSrcweir } 571*cdf0e10cSrcweir 572*cdf0e10cSrcweir /** 573*cdf0e10cSrcweir * This method press the "A" key. Therefore it uses the <CODE>Robot</CODE> 574*cdf0e10cSrcweir * class. 575*cdf0e10cSrcweir * @see java.awt.Robot 576*cdf0e10cSrcweir */ 577*cdf0e10cSrcweir private void keyIntoDoc(){ 578*cdf0e10cSrcweir try { 579*cdf0e10cSrcweir Robot rob = new Robot(); 580*cdf0e10cSrcweir System.out.println("try to press 'A'"); 581*cdf0e10cSrcweir rob.keyPress(java.awt.event.KeyEvent.VK_A); 582*cdf0e10cSrcweir rob.keyRelease(java.awt.event.KeyEvent.VK_A); 583*cdf0e10cSrcweir } catch (java.awt.AWTException e) { 584*cdf0e10cSrcweir System.out.println("couldn't press key"); 585*cdf0e10cSrcweir } 586*cdf0e10cSrcweir 587*cdf0e10cSrcweir } 588*cdf0e10cSrcweir } 589*cdf0e10cSrcweir 590*cdf0e10cSrcweir /** This interface represents all possible actions which could be used 591*cdf0e10cSrcweir * in the <CODE>EventTrigger</CODE> class. 592*cdf0e10cSrcweir * @see EventTest.EventTrigger 593*cdf0e10cSrcweir */ 594*cdf0e10cSrcweir private interface EventTriggerType{ 595*cdf0e10cSrcweir 596*cdf0e10cSrcweir /** klick the mouse into the scroll bar*/ 597*cdf0e10cSrcweir final public static int MOUSE_KLICK_INTO_DOC = 1; 598*cdf0e10cSrcweir 599*cdf0e10cSrcweir /** write some text into a spread sheet*/ 600*cdf0e10cSrcweir final public static int KEY_TEXT_INTO_DOC = 2; 601*cdf0e10cSrcweir } 602*cdf0e10cSrcweir 603*cdf0e10cSrcweir 604*cdf0e10cSrcweir 605*cdf0e10cSrcweir 606*cdf0e10cSrcweir private XMultiServiceFactory getMSF() 607*cdf0e10cSrcweir { 608*cdf0e10cSrcweir final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager()); 609*cdf0e10cSrcweir return xMSF1; 610*cdf0e10cSrcweir } 611*cdf0e10cSrcweir 612*cdf0e10cSrcweir // setup and close connections 613*cdf0e10cSrcweir @BeforeClass public static void setUpConnection() throws Exception { 614*cdf0e10cSrcweir System.out.println("setUpConnection()"); 615*cdf0e10cSrcweir connection.setUp(); 616*cdf0e10cSrcweir } 617*cdf0e10cSrcweir 618*cdf0e10cSrcweir @AfterClass public static void tearDownConnection() 619*cdf0e10cSrcweir throws InterruptedException, com.sun.star.uno.Exception 620*cdf0e10cSrcweir { 621*cdf0e10cSrcweir System.out.println("tearDownConnection()"); 622*cdf0e10cSrcweir connection.tearDown(); 623*cdf0e10cSrcweir } 624*cdf0e10cSrcweir 625*cdf0e10cSrcweir private static final OfficeConnection connection = new OfficeConnection(); 626*cdf0e10cSrcweir 627*cdf0e10cSrcweir }