merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / awt / _XWindow.java
blob9422ff6b5153d5ba7a1d6619696a0c3fe31f10fa
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XWindow.java,v $
10 * $Revision: 1.6 $
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 ************************************************************************/
31 package ifc.awt;
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;
54 /**
55 * Testing <code>com.sun.star.awt.XWindow</code>
56 * interface methods :
57 * <ul>
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>
75 * </ul> <p>
76 * This test needs the following object relations :
77 * <ul>
78 * <li> <code>'XWindow.AnotherWindow'</code> (of type <code>XWindow</code>):
79 * Some another window which can gain focus so the tested one
80 * must lost it. </li>
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>
87 * </ul> <p>
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;
96 /**
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 :
110 * <ul>
111 * <li> <code> getPosSize() </code> : returns the outer bounds of
112 * the window </li>
113 * </ul>
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,
124 PosSize.POSSIZE);
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");
144 oObj.setFocus();
145 win.setFocus();
146 oObj.setFocus();
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 ;
183 public void init() {
184 resized = false ;
185 moved = false ;
186 hidden = false ;
187 shown = false ;
190 public void windowResized(WindowEvent e) {
191 resized = true ;
193 public void windowMoved(WindowEvent e) {
194 moved = true ;
196 public void windowHidden(EventObject e) {
197 hidden = true ;
199 public void windowShown(EventObject e) {
200 shown = true ;
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
215 * chaged<p>
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 :
221 * <ul>
222 * <li> <code> setPosSize() </code>: sets the outer bounds of the
223 * window</li>
224 * <li> <code> setVisible() </code>: shows or hides the window
225 * depending on the parameter</li>
226 * </ul>
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) {
241 try {
242 Size sz = ctrlShape.getSize();
243 sz.Height += 100;
244 ctrlShape.setSize(sz);
245 Point pos = ctrlShape.getPosition();
246 pos.X += 100 ;
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);
252 } else {
253 oObj.setPosSize(posSize.X + 2, 0, 0, 0, PosSize.X);
254 oObj.setPosSize(0, 0, 100, 100, PosSize.WIDTH);
257 shortWait();
258 boolean res = wListener.resized && wListener.moved &&
259 !wListener.hidden && !wListener.shown;
260 result &= res;
261 if (!res) {
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);
266 } else {
267 log.println("windowMoved() and windowResized() was called");
270 // testing wListener.windowHidden()
271 wListener.init();
272 shortWait();
273 log.println("set object invisible...");
274 oObj.setVisible(false);
275 shortWait();
276 res = wListener.hidden && !wListener.resized
277 && !wListener.moved && !wListener.shown;
278 result &= res;
279 if (!res) {
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);
284 } else {
285 log.println("windowHidden() was called");
288 // testing wListener.windowShown()
289 wListener.init() ;
290 shortWait();
291 log.println("set object visible...");
292 oObj.setVisible(true) ;
293 shortWait();
294 res = wListener.shown && !wListener.resized &&
295 !wListener.hidden && !wListener.moved;
296 result &= res;
297 if (!res) {
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);
302 } else {
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 :
315 * <ul>
316 * <li> <code> addWindowListener() </code>: adds window listener to the
317 * object </li>
318 * </ul>
320 public void _removeWindowListener() {
321 requiredMethod("addWindowListener()");
322 oObj.removeWindowListener(wListener);
323 wListener.init();
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) {
343 gained = true ;
345 public void focusLost(FocusEvent e) {
346 lost = true ;
348 public void init() {
349 gained = false;
350 lost = false;
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 :
364 * <ul>
365 * <li> <code> setFocus() </code>: sets the focus to the window </li>
366 * </ul>
368 public void _addFocusListener() {
369 boolean result = true ;
371 requiredMethod("setFocus()");
372 oObj.addFocusListener(fListener) ;
374 // testing fListener.lost()
375 oObj.setFocus();
376 shortWait();
377 win.setFocus();
378 shortWait();
379 result &= fListener.lost;
380 if (!fListener.lost) {
381 log.println("Lost focus was not notified about") ;
384 // testing fListener.gained()
385 oObj.setFocus() ;
386 shortWait();
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 :
400 * <ul>
401 * <li> <code> addFocusListener() </code> : adds focus listener to
402 * the object </li>
403 * </ul>
405 public void _removeFocusListener() {
406 requiredMethod("addFocusListener()");
407 oObj.removeFocusListener(fListener);
408 fListener.init();
409 oObj.setFocus();
410 win.setFocus();
411 oObj.setFocus();
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 :
444 * <ul>
445 * <li> <code> addKeyListener() </code> : adds key listener to
446 * the object </li>
447 * </ul>
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) {
466 pressed = true;
469 public void mouseReleased(MouseEvent e) {
470 released = true;
473 public void mouseEntered(MouseEvent e) {
474 entered = true;
477 public void mouseExited(MouseEvent e) {
478 exited = true;
481 public void disposing(EventObject e) {}
483 public void init() {
484 pressed = false;
485 released = false;
486 exited = false;
487 entered = false;
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 :
507 * <ul>
508 * <li> <code> addMouseListener() </code> : adds mouse listener to
509 * the object</li>
510 * </ul>
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) {
527 dragged = true;
530 public void mouseMoved(MouseEvent e) {
531 moved = true;
534 public void disposing(EventObject e) {}
536 public void init() {
537 dragged = false;
538 moved = false;
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 :
558 * <ul>
559 * <li> <code> addMouseMotionListener() </code> : adds mouse motion
560 * listener to the object</li>
561 * </ul>
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) {
577 paint = true;
580 public void disposing(EventObject e) {}
582 public void init() {
583 paint = false;
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 :
603 * <ul>
604 * <li> <code> addPaintListener() </code> : adds paint listener to
605 * the object </li>
606 * </ul>
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>
616 * reset</code> call.
618 private void shortWait() {
619 try {
620 Thread.sleep(2000) ;
621 } catch (InterruptedException e) {
622 log.println("While waiting :" + e) ;