1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XWindow.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import com
.sun
.star
.awt
.FocusEvent
;
34 import com
.sun
.star
.awt
.KeyEvent
;
35 import com
.sun
.star
.awt
.MouseEvent
;
36 import com
.sun
.star
.awt
.PaintEvent
;
37 import com
.sun
.star
.awt
.Point
;
38 import com
.sun
.star
.awt
.PosSize
;
39 import com
.sun
.star
.awt
.Rectangle
;
40 import com
.sun
.star
.awt
.Size
;
41 import com
.sun
.star
.awt
.WindowEvent
;
42 import com
.sun
.star
.awt
.XFocusListener
;
43 import com
.sun
.star
.awt
.XKeyListener
;
44 import com
.sun
.star
.awt
.XMouseListener
;
45 import com
.sun
.star
.awt
.XMouseMotionListener
;
46 import com
.sun
.star
.awt
.XPaintListener
;
47 import com
.sun
.star
.awt
.XWindow
;
48 import com
.sun
.star
.awt
.XWindowListener
;
49 import com
.sun
.star
.drawing
.XControlShape
;
50 import com
.sun
.star
.lang
.EventObject
;
51 import lib
.MultiMethodTest
;
52 import util
.ValueComparer
;
55 * Testing <code>com.sun.star.awt.XWindow</code>
58 * <li><code> setPosSize()</code></li>
59 * <li><code> getPosSize()</code></li>
60 * <li><code> setVisible()</code></li>
61 * <li><code> setEnable()</code></li>
62 * <li><code> setFocus()</code></li>
63 * <li><code> addWindowListener()</code></li>
64 * <li><code> removeWindowListener()</code></li>
65 * <li><code> addFocusListener()</code></li>
66 * <li><code> removeFocusListener()</code></li>
67 * <li><code> addKeyListener()</code></li>
68 * <li><code> removeKeyListener()</code></li>
69 * <li><code> addMouseListener()</code></li>
70 * <li><code> removeMouseListener()</code></li>
71 * <li><code> addMouseMotionListener()</code></li>
72 * <li><code> removeMouseMotionListener()</code></li>
73 * <li><code> addPaintListener()</code></li>
74 * <li><code> removePaintListener()</code></li>
76 * This test needs the following object relations :
78 * <li> <code>'XWindow.AnotherWindow'</code> (of type <code>XWindow</code>):
79 * Some another window which can gain focus so the tested one
81 * <li> <code>'XWindow.ControlShape'</code> <b>optional</b>
82 * (of type <code>XControlShape</code>):
83 * Some shapes cann't change their size within fixed ControlShape
84 * and their size could be changed only if size of container
85 * ControlShape is changed. For such shapes this relation should
86 * be passed for proper <code>addWindowListener</code> test. </li>
88 * Test is <b> NOT </b> multithread compilant. <p>
89 * @see com.sun.star.awt.XWindow
91 public class _XWindow
extends MultiMethodTest
{
92 public XWindow oObj
= null;
93 private Rectangle posSize
= null ;
94 private XWindow win
= null;
97 * Test calls the method. <p>
98 * Has <b> OK </b> status if the method does not return null.
100 public void _getPosSize() {
101 posSize
= oObj
.getPosSize() ;
102 tRes
.tested("getPosSize()", posSize
!= null) ;
106 * After defining Rectangle structure to be set, test calls the method. <p>
107 * Has <b> OK </b> status if structure obtained using getPosSize() is
108 * equal to structure previously set using setPosSize(). <p>
109 * The following method tests are to be completed successfully before :
111 * <li> <code> getPosSize() </code> : returns the outer bounds of
115 public void _setPosSize() {
116 Rectangle newRec
= new Rectangle();
118 requiredMethod("getPosSize()");
119 newRec
.X
= posSize
.X
+ 1;
120 newRec
.Y
= posSize
.Y
+ 1;
121 newRec
.Width
= posSize
.Width
- 3;
122 newRec
.Height
= posSize
.Height
- 3;
123 oObj
.setPosSize(newRec
.X
, newRec
.Y
, newRec
.Width
, newRec
.Height
,
125 Rectangle gPS
= oObj
.getPosSize();
126 log
.println("Was : (" + posSize
.X
+ ", " + posSize
.Y
+ ", " +
127 posSize
.Width
+ ", " + posSize
.Height
+ "), ");
128 log
.println("Set : (" + newRec
.X
+ ", " + newRec
.Y
+ ", " +
129 newRec
.Width
+ ", " + newRec
.Height
+ "), ");
130 log
.println("Get : (" + gPS
.X
+ ", " + gPS
.Y
+ ", " +
131 gPS
.Width
+ ", " + gPS
.Height
+ "). ");
132 tRes
.tested("setPosSize()", ValueComparer
.equalValue(newRec
, gPS
) );
136 * At first object relation 'XWindow.AnotherWindow' is obtained.
137 * Then test calls the methods of two 'XWindow' objects several times to be
138 * sure that the focus has changed. <p>
139 * Has <b> OK </b> status if the method successfully returns
140 * and no exceptions were thrown.
142 public void _setFocus() {
143 win
= (XWindow
) tEnv
.getObjRelation("XWindow.AnotherWindow");
147 tRes
.tested("setFocus()", true);
151 * Test calls the method twice with two parameters: 'true' and 'false'. <p>
152 * Has <b> OK </b> status if the method successfully returns
153 * and no exceptions were thrown.
155 public void _setVisible() {
156 oObj
.setVisible(false);
157 oObj
.setVisible(true);
158 tRes
.tested("setVisible()", true);
162 * Test calls the method twice with two parameters: 'true' and 'false'. <p>
163 * Has <b> OK </b> status if the method successfully returns
164 * and no exceptions were thrown.
166 public void _setEnable() {
167 oObj
.setEnable(false);
168 oObj
.setEnable(true);
169 tRes
.tested("setEnable()", true);
174 * A class we use to test addWindowListener() and
175 * removeWindowListener()
177 public class TestWindowListener
implements XWindowListener
{
178 public boolean resized
= false ;
179 public boolean moved
= false ;
180 public boolean hidden
= false ;
181 public boolean shown
= false ;
190 public void windowResized(WindowEvent e
) {
193 public void windowMoved(WindowEvent e
) {
196 public void windowHidden(EventObject e
) {
199 public void windowShown(EventObject e
) {
202 public void disposing(EventObject e
) {}
206 private TestWindowListener wListener
= new TestWindowListener() ;
210 * Test calls the method. Then we check if listener's methods were called
211 * when we move, resize, hide and show the window. The resizing is
212 * performed depending on 'XWindow.ControlShape' existance. If this
213 * relation exists then the size and position of container control
214 * shape is changed, else the position and size of window itself is
217 * Has <b> OK </b> status if methods of wListener were called when
218 * corresponding events occured. <p>
220 * The following method tests are to be executed before :
222 * <li> <code> setPosSize() </code>: sets the outer bounds of the
224 * <li> <code> setVisible() </code>: shows or hides the window
225 * depending on the parameter</li>
228 public void _addWindowListener() {
229 executeMethod("setPosSize()");
230 executeMethod("setVisible()");
231 boolean result
= true ;
233 oObj
.addWindowListener(wListener
);
235 // testing wListener.windowMoved()
236 XControlShape ctrlShape
= (XControlShape
)
237 tEnv
.getObjRelation("XWindow.ControlShape");
238 log
.println("change object position and size...");
240 if (ctrlShape
!= null) {
242 Size sz
= ctrlShape
.getSize();
244 ctrlShape
.setSize(sz
);
245 Point pos
= ctrlShape
.getPosition();
247 ctrlShape
.setPosition(pos
);
248 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
249 log
.println("Couldn't change size or position: ");
250 e
.printStackTrace(log
);
253 oObj
.setPosSize(posSize
.X
+ 2, 0, 0, 0, PosSize
.X
);
254 oObj
.setPosSize(0, 0, 100, 100, PosSize
.WIDTH
);
258 boolean res
= wListener
.resized
&& wListener
.moved
&&
259 !wListener
.hidden
&& !wListener
.shown
;
262 log
.println("\twindowHidden() wasn't called: " + !wListener
.hidden
);
263 log
.println("\twindowShown() wasn't called: " + !wListener
.shown
);
264 log
.println("\twindowResized() was called: " + wListener
.resized
);
265 log
.println("\twindowMoved() was called: " + wListener
.moved
);
267 log
.println("windowMoved() and windowResized() was called");
270 // testing wListener.windowHidden()
273 log
.println("set object invisible...");
274 oObj
.setVisible(false);
276 res
= wListener
.hidden
&& !wListener
.resized
277 && !wListener
.moved
&& !wListener
.shown
;
280 log
.println("\twindowHidden() was called: " + wListener
.hidden
);
281 log
.println("\twindowShown() wasn't called: " + !wListener
.shown
);
282 log
.println("\twindowResized() wasn't called: " + !wListener
.resized
);
283 log
.println("\twindowMoved() wasn't called: " + !wListener
.moved
);
285 log
.println("windowHidden() was called");
288 // testing wListener.windowShown()
291 log
.println("set object visible...");
292 oObj
.setVisible(true) ;
294 res
= wListener
.shown
&& !wListener
.resized
&&
295 !wListener
.hidden
&& !wListener
.moved
;
298 log
.println("\twindowHidden() wasn't called: " + !wListener
.hidden
);
299 log
.println("\twindowShown() was called: " + wListener
.shown
);
300 log
.println("\twindowResized() wasn't called: " + !wListener
.resized
);
301 log
.println("\twindowMoved() wasn't called: " + !wListener
.moved
);
303 log
.println("windowShown() was called");
306 tRes
.tested("addWindowListener()", result
) ;
311 * Test calls the method. Then we change window and check that listener's
312 * methods were not called. <p>
313 * Has <b> OK </b> status if listener does not react on window events.<p>
314 * The following method tests are to be completed successfully before :
316 * <li> <code> addWindowListener() </code>: adds window listener to the
320 public void _removeWindowListener() {
321 requiredMethod("addWindowListener()");
322 oObj
.removeWindowListener(wListener
);
324 oObj
.setPosSize(posSize
.X
, posSize
.Y
,
325 posSize
.Width
, posSize
.Height
, PosSize
.POSSIZE
);
326 oObj
.setVisible(false);
327 oObj
.setVisible(true);
328 boolean res
= !(wListener
.resized
|| wListener
.moved
329 || wListener
.hidden
|| wListener
.shown
);
331 tRes
.tested("removeWindowListener()", res
);
335 * A class we use to test addFocusListener() and
336 * removeFocusListener()
338 public class TestFocusListener
implements XFocusListener
{
339 public boolean gained
= false ;
340 public boolean lost
= false ;
342 public void focusGained(FocusEvent e
) {
345 public void focusLost(FocusEvent e
) {
352 public void disposing(EventObject e
) {}
356 private TestFocusListener fListener
= new TestFocusListener();
359 * Test calls the method. Then we change focus and check that listener's
360 * methods were called. <p>
361 * Has <b> OK </b> status if methods of fListener were called when
362 * corresponding events occured. <p>
363 * The following method tests are to be completed successfully before :
365 * <li> <code> setFocus() </code>: sets the focus to the window </li>
368 public void _addFocusListener() {
369 boolean result
= true ;
371 requiredMethod("setFocus()");
372 oObj
.addFocusListener(fListener
) ;
374 // testing fListener.lost()
379 result
&= fListener
.lost
;
380 if (!fListener
.lost
) {
381 log
.println("Lost focus was not notified about") ;
384 // testing fListener.gained()
387 result
&= fListener
.gained
;
388 if (!fListener
.gained
) {
389 log
.println("Gained focus was not notified about") ;
392 tRes
.tested("addFocusListener()", result
) ;
396 * Test calls the method. Then we change focus and check that listener's
397 * methods were not called. <p>
398 * Has <b> OK </b> status if listener does not react on focus changing. <p>
399 * The following method tests are to be completed successfully before :
401 * <li> <code> addFocusListener() </code> : adds focus listener to
405 public void _removeFocusListener() {
406 requiredMethod("addFocusListener()");
407 oObj
.removeFocusListener(fListener
);
412 boolean res
= !(fListener
.gained
|| fListener
.lost
);
413 tRes
.tested("removeFocusListener()", res
);
417 * A class we use to test addKeyListener() and
418 * removeKeyListener()
420 public class TestKeyListener
implements XKeyListener
{
421 public boolean pressed
= false;
422 public boolean released
= false;
423 public void keyPressed(KeyEvent e
) { pressed
= true; }
424 public void keyReleased(KeyEvent e
) { released
= true; }
425 public void disposing(EventObject e
) {}
426 public void init() { pressed
= false; released
= false; }
429 private TestKeyListener kListener
= new TestKeyListener();
432 * Test calls the method. <p>
433 * Has <b> OK </b> status if no exceptions were thrown. <p>
435 public void _addKeyListener() {
436 oObj
.addKeyListener(kListener
);
437 tRes
.tested("addKeyListener()", true);
441 * Test calls the method. <p>
442 * Has <b> OK </b> status if no exceptions were thrown. <p>
443 * The following method tests are to be completed successfully before :
445 * <li> <code> addKeyListener() </code> : adds key listener to
449 public void _removeKeyListener() {
450 requiredMethod("addKeyListener()");
451 oObj
.removeKeyListener(kListener
);
452 tRes
.tested("removeKeyListener()", true);
456 * A class we use to test addMouseListener() and
457 * removeMouseListener()
459 public class TestMouseListener
implements XMouseListener
{
460 public boolean pressed
= false;
461 public boolean released
= false;
462 public boolean entered
= false;
463 public boolean exited
= false;
465 public void mousePressed(MouseEvent e
) {
469 public void mouseReleased(MouseEvent e
) {
473 public void mouseEntered(MouseEvent e
) {
477 public void mouseExited(MouseEvent e
) {
481 public void disposing(EventObject e
) {}
492 private TestMouseListener mListener
= new TestMouseListener();
495 * Test calls the method. <p>
496 * Has <b> OK </b> status if no exceptions were thrown. <p>
498 public void _addMouseListener() {
499 oObj
.addMouseListener(mListener
);
500 tRes
.tested("addMouseListener()", true);
504 * Test calls the method. <p>
505 * Has <b> OK </b> status if no exceptions were thrown. <p>
506 * The following method tests are to be completed successfully before :
508 * <li> <code> addMouseListener() </code> : adds mouse listener to
512 public void _removeMouseListener() {
513 requiredMethod("addMouseListener()");
514 oObj
.removeMouseListener(mListener
);
515 tRes
.tested("removeMouseListener()", true);
519 * A class we use to test addMouseMotionListener() and
520 * removeMouseMotionListener()
522 public class TestMouseMotionListener
implements XMouseMotionListener
{
523 public boolean dragged
= false;
524 public boolean moved
= false;
526 public void mouseDragged(MouseEvent e
) {
530 public void mouseMoved(MouseEvent e
) {
534 public void disposing(EventObject e
) {}
543 private TestMouseMotionListener mmListener
= new TestMouseMotionListener();
546 * Test calls the method. <p>
547 * Has <b> OK </b> status if no exceptions were thrown. <p>
549 public void _addMouseMotionListener() {
550 oObj
.addMouseMotionListener(mmListener
);
551 tRes
.tested("addMouseMotionListener()", true);
555 * Test calls the method. <p>
556 * Has <b> OK </b> status if no exceptions were thrown. <p>
557 * The following method tests are to be completed successfully before :
559 * <li> <code> addMouseMotionListener() </code> : adds mouse motion
560 * listener to the object</li>
563 public void _removeMouseMotionListener() {
564 requiredMethod("addMouseMotionListener()");
565 oObj
.removeMouseMotionListener(mmListener
);
566 tRes
.tested("removeMouseMotionListener()", true);
570 * A class we use to test addPaintListener() and
571 * removePaintListener()
573 public class TestPaintListener
implements XPaintListener
{
574 public boolean paint
= false;
576 public void windowPaint(PaintEvent e
) {
580 public void disposing(EventObject e
) {}
588 private TestPaintListener pListener
= new TestPaintListener();
591 * Test calls the method. <p>
592 * Has <b> OK </b> status if no exceptions were thrown. <p>
594 public void _addPaintListener() {
595 oObj
.addPaintListener(pListener
);
596 tRes
.tested("addPaintListener()", true);
600 * Test calls the method. <p>
601 * Has <b> OK </b> status if no exceptions were thrown. <p>
602 * The following method tests are to be completed successfully before :
604 * <li> <code> addPaintListener() </code> : adds paint listener to
608 public void _removePaintListener() {
609 requiredMethod("addPaintListener()");
610 oObj
.removePaintListener(pListener
);
611 tRes
.tested("removePaintListener()", true);
615 * Sleeps for 0.2 sec. to allow StarOffice to react on <code>
618 private void shortWait() {
621 } catch (InterruptedException e
) {
622 log
.println("While waiting :" + e
) ;