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