1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.awt;
25 
26 import com.sun.star.awt.FocusEvent;
27 import com.sun.star.awt.KeyEvent;
28 import com.sun.star.awt.MouseEvent;
29 import com.sun.star.awt.PaintEvent;
30 import com.sun.star.awt.Point;
31 import com.sun.star.awt.PosSize;
32 import com.sun.star.awt.Rectangle;
33 import com.sun.star.awt.Size;
34 import com.sun.star.awt.WindowEvent;
35 import com.sun.star.awt.XFocusListener;
36 import com.sun.star.awt.XKeyListener;
37 import com.sun.star.awt.XMouseListener;
38 import com.sun.star.awt.XMouseMotionListener;
39 import com.sun.star.awt.XPaintListener;
40 import com.sun.star.awt.XWindow;
41 import com.sun.star.awt.XWindowListener;
42 import com.sun.star.drawing.XControlShape;
43 import com.sun.star.lang.EventObject;
44 import lib.MultiMethodTest;
45 import util.ValueComparer;
46 
47 /**
48 * Testing <code>com.sun.star.awt.XWindow</code>
49 * interface methods :
50 * <ul>
51 *  <li><code> setPosSize()</code></li>
52 *  <li><code> getPosSize()</code></li>
53 *  <li><code> setVisible()</code></li>
54 *  <li><code> setEnable()</code></li>
55 *  <li><code> setFocus()</code></li>
56 *  <li><code> addWindowListener()</code></li>
57 *  <li><code> removeWindowListener()</code></li>
58 *  <li><code> addFocusListener()</code></li>
59 *  <li><code> removeFocusListener()</code></li>
60 *  <li><code> addKeyListener()</code></li>
61 *  <li><code> removeKeyListener()</code></li>
62 *  <li><code> addMouseListener()</code></li>
63 *  <li><code> removeMouseListener()</code></li>
64 *  <li><code> addMouseMotionListener()</code></li>
65 *  <li><code> removeMouseMotionListener()</code></li>
66 *  <li><code> addPaintListener()</code></li>
67 *  <li><code> removePaintListener()</code></li>
68 * </ul> <p>
69 * This test needs the following object relations :
70 * <ul>
71 *  <li> <code>'XWindow.AnotherWindow'</code> (of type <code>XWindow</code>):
72 *   Some another window which can gain focus so the tested one
73 *   must lost it. </li>
74 *  <li> <code>'XWindow.ControlShape'</code> <b>optional</b>
75 *   (of type <code>XControlShape</code>):
76 *   Some shapes cann't change their size within fixed ControlShape
77 *   and their size could be changed only if size of container
78 *   ControlShape is changed. For such shapes this relation should
79 *   be passed for proper <code>addWindowListener</code> test. </li>
80 * </ul> <p>
81 * Test is <b> NOT </b> multithread compilant. <p>
82 * @see com.sun.star.awt.XWindow
83 */
84 public class _XWindow extends MultiMethodTest {
85     public XWindow oObj = null;
86     private Rectangle posSize = null ;
87     private XWindow win = null;
88 
89     /**
90     * Test calls the method. <p>
91     * Has <b> OK </b> status if the method does not return null.
92     */
_getPosSize()93     public void _getPosSize() {
94         posSize = oObj.getPosSize() ;
95         tRes.tested("getPosSize()", posSize != null) ;
96     }
97 
98     /**
99     * After defining Rectangle structure to be set, test calls the method. <p>
100     * Has <b> OK </b> status if structure obtained using getPosSize() is
101     * equal to structure previously set using setPosSize(). <p>
102     * The following method tests are to be completed successfully before :
103     * <ul>
104     *  <li> <code> getPosSize() </code> :  returns the outer bounds of
105     *  the window </li>
106     * </ul>
107     */
_setPosSize()108     public void _setPosSize() {
109         Rectangle newRec = new Rectangle();
110 
111         requiredMethod("getPosSize()");
112         newRec.X = posSize.X + 1;
113         newRec.Y = posSize.Y + 1;
114         newRec.Width = posSize.Width - 3;
115         newRec.Height = posSize.Height - 3;
116         oObj.setPosSize(newRec.X, newRec.Y, newRec.Width, newRec.Height,
117             PosSize.POSSIZE);
118         Rectangle gPS = oObj.getPosSize();
119         log.println("Was : (" + posSize.X + ", " + posSize.Y + ", " +
120             posSize.Width + ", " + posSize.Height + "), ");
121         log.println("Set : (" + newRec.X + ", " + newRec.Y + ", " +
122             newRec.Width + ", " + newRec.Height + "), ");
123         log.println("Get : (" + gPS.X + ", " + gPS.Y + ", " +
124             gPS.Width + ", " + gPS.Height + "). ");
125         tRes.tested("setPosSize()", ValueComparer.equalValue(newRec, gPS) );
126     }
127 
128     /**
129     * At first object relation 'XWindow.AnotherWindow' is obtained.
130     * Then test calls the methods of two 'XWindow' objects several times to be
131     * sure that the focus has changed. <p>
132     * Has <b> OK </b> status if the method successfully returns
133     * and no exceptions were thrown.
134     */
_setFocus()135     public void _setFocus() {
136         win = (XWindow) tEnv.getObjRelation("XWindow.AnotherWindow");
137         oObj.setFocus();
138         win.setFocus();
139         oObj.setFocus();
140         tRes.tested("setFocus()", true);
141     }
142 
143     /**
144     * Test calls the method twice with two parameters: 'true' and 'false'. <p>
145     * Has <b> OK </b> status if the method successfully returns
146     * and no exceptions were thrown.
147     */
_setVisible()148     public void _setVisible() {
149         oObj.setVisible(false);
150         oObj.setVisible(true);
151         tRes.tested("setVisible()", true);
152     }
153 
154     /**
155     * Test calls the method twice with two parameters: 'true' and 'false'. <p>
156     * Has <b> OK </b> status if the method successfully returns
157     * and no exceptions were thrown.
158     */
_setEnable()159     public void _setEnable() {
160         oObj.setEnable(false);
161         oObj.setEnable(true);
162         tRes.tested("setEnable()", true);
163     }
164 
165 
166     /**
167     * A class we use to test addWindowListener() and
168     * removeWindowListener()
169     */
170     public class TestWindowListener implements XWindowListener {
171         public boolean resized = false ;
172         public boolean moved = false ;
173         public boolean hidden = false ;
174         public boolean shown = false ;
175 
init()176         public void init() {
177             resized = false ;
178             moved = false ;
179             hidden = false ;
180             shown = false ;
181         }
182 
windowResized(WindowEvent e)183         public void windowResized(WindowEvent e) {
184             resized = true ;
185         }
windowMoved(WindowEvent e)186         public void windowMoved(WindowEvent e) {
187             moved = true ;
188         }
windowHidden(EventObject e)189         public void windowHidden(EventObject e) {
190             hidden = true ;
191         }
windowShown(EventObject e)192         public void windowShown(EventObject e) {
193             shown = true ;
194         }
disposing(EventObject e)195         public void disposing(EventObject e) {}
196 
197     }
198 
199     private TestWindowListener wListener = new TestWindowListener() ;
200 
201 
202     /**
203     * Test calls the method. Then we check if listener's methods were called
204     * when we move, resize, hide and show the window. The resizing is
205     * performed depending on 'XWindow.ControlShape' existance. If this
206     * relation exists then the size and position of container control
207     * shape is changed, else the position and size of window itself is
208     * chaged<p>
209     *
210     * Has <b> OK </b> status if methods of wListener were called when
211     * corresponding events occured. <p>
212     *
213     * The following method tests are to be executed before :
214     * <ul>
215     *  <li> <code> setPosSize() </code>: sets the outer bounds of the
216     *  window</li>
217     *  <li> <code> setVisible() </code>: shows or hides the window
218     *  depending on the parameter</li>
219     * </ul>
220     */
_addWindowListener()221     public void _addWindowListener() {
222         executeMethod("setPosSize()");
223         executeMethod("setVisible()");
224         boolean result = true ;
225 
226         oObj.addWindowListener(wListener);
227 
228         // testing wListener.windowMoved()
229         XControlShape ctrlShape = (XControlShape)
230             tEnv.getObjRelation("XWindow.ControlShape");
231         log.println("change object position and size...");
232 
233         if (ctrlShape != null) {
234             try {
235                 Size sz = ctrlShape.getSize();
236                 sz.Height += 100;
237                 ctrlShape.setSize(sz);
238                 Point pos = ctrlShape.getPosition();
239                 pos.X += 100 ;
240                 ctrlShape.setPosition(pos);
241             } catch (com.sun.star.beans.PropertyVetoException e) {
242                 log.println("Couldn't change size or position: ");
243                 e.printStackTrace(log);
244             }
245         } else {
246             oObj.setPosSize(posSize.X + 2, 0, 0, 0, PosSize.X);
247             oObj.setPosSize(0, 0, 100, 100, PosSize.WIDTH);
248         }
249 
250         shortWait();
251         boolean res = wListener.resized && wListener.moved &&
252             !wListener.hidden && !wListener.shown;
253         result &= res;
254         if (!res) {
255             log.println("\twindowHidden()  wasn't called: " + !wListener.hidden);
256             log.println("\twindowShown()   wasn't called: " + !wListener.shown);
257             log.println("\twindowResized()    was called: " + wListener.resized);
258             log.println("\twindowMoved()      was called: " + wListener.moved);
259         } else {
260             log.println("windowMoved() and windowResized() was called");
261         }
262 
263         // testing wListener.windowHidden()
264         wListener.init();
265         shortWait();
266         log.println("set object invisible...");
267         oObj.setVisible(false);
268         shortWait();
269         res = wListener.hidden && !wListener.resized
270                         && !wListener.moved && !wListener.shown;
271         result &= res;
272         if (!res) {
273             log.println("\twindowHidden()     was called: " + wListener.hidden);
274             log.println("\twindowShown()   wasn't called: " + !wListener.shown);
275             log.println("\twindowResized() wasn't called: " + !wListener.resized);
276             log.println("\twindowMoved()   wasn't called: " + !wListener.moved);
277         } else {
278             log.println("windowHidden() was called");
279         }
280 
281         // testing wListener.windowShown()
282         wListener.init() ;
283         shortWait();
284         log.println("set object visible...");
285         oObj.setVisible(true) ;
286         shortWait();
287         res = wListener.shown && !wListener.resized &&
288                 !wListener.hidden && !wListener.moved;
289         result &= res;
290         if (!res) {
291             log.println("\twindowHidden()  wasn't called: " + !wListener.hidden);
292             log.println("\twindowShown()      was called: " + wListener.shown);
293             log.println("\twindowResized() wasn't called: " + !wListener.resized);
294             log.println("\twindowMoved()   wasn't called: " + !wListener.moved);
295         } else {
296             log.println("windowShown() was called");
297         }
298 
299         tRes.tested("addWindowListener()", result) ;
300     }
301 
302 
303     /**
304     * Test calls the method. Then we change window and check that listener's
305     * methods were not called. <p>
306     * Has <b> OK </b> status if listener does not react on window events.<p>
307     * The following method tests are to be completed successfully before :
308     * <ul>
309     *  <li> <code> addWindowListener() </code>: adds window listener to the
310     *  object </li>
311     * </ul>
312     */
_removeWindowListener()313     public void _removeWindowListener() {
314         requiredMethod("addWindowListener()");
315         oObj.removeWindowListener(wListener);
316         wListener.init();
317         oObj.setPosSize(posSize.X, posSize.Y,
318             posSize.Width , posSize.Height, PosSize.POSSIZE);
319         oObj.setVisible(false);
320         oObj.setVisible(true);
321         boolean res = !(wListener.resized || wListener.moved
322             || wListener.hidden || wListener.shown);
323 
324         tRes.tested("removeWindowListener()", res);
325     }
326 
327     /**
328     * A class we use to test addFocusListener() and
329     * removeFocusListener()
330     */
331     public class TestFocusListener implements XFocusListener {
332         public boolean gained = false ;
333         public boolean lost = false ;
334 
focusGained(FocusEvent e)335         public void focusGained(FocusEvent e) {
336             gained = true ;
337         }
focusLost(FocusEvent e)338         public void focusLost(FocusEvent e) {
339             lost = true ;
340         }
init()341         public void init() {
342             gained = false;
343             lost = false;
344         }
disposing(EventObject e)345         public void disposing(EventObject e) {}
346 
347     }
348 
349     private TestFocusListener fListener = new TestFocusListener();
350 
351     /**
352     * Test calls the method. Then we change focus and check that listener's
353     * methods were called. <p>
354     * Has <b> OK </b> status if methods of fListener were called when
355     * corresponding events occured. <p>
356     * The following method tests are to be completed successfully before :
357     * <ul>
358     *  <li> <code> setFocus() </code>: sets the focus to the window </li>
359     * </ul>
360     */
_addFocusListener()361     public void _addFocusListener() {
362         boolean result = true ;
363 
364         requiredMethod("setFocus()");
365         oObj.addFocusListener(fListener) ;
366 
367         // testing fListener.lost()
368         oObj.setFocus();
369         shortWait();
370         win.setFocus();
371         shortWait();
372         result &= fListener.lost;
373         if (!fListener.lost) {
374             log.println("Lost focus was not notified about") ;
375         }
376 
377         // testing fListener.gained()
378         oObj.setFocus() ;
379         shortWait();
380         result &= fListener.gained;
381         if (!fListener.gained) {
382             log.println("Gained focus was not notified about") ;
383         }
384 
385         tRes.tested("addFocusListener()", result) ;
386     }
387 
388     /**
389     * Test calls the method. Then we change focus and check that listener's
390     * methods were not called. <p>
391     * Has <b> OK </b> status if listener does not react on focus changing. <p>
392     * The following method tests are to be completed successfully before :
393     * <ul>
394     *  <li> <code> addFocusListener() </code> : adds focus listener to
395     * the object </li>
396     * </ul>
397     */
_removeFocusListener()398     public void _removeFocusListener() {
399         requiredMethod("addFocusListener()");
400         oObj.removeFocusListener(fListener);
401         fListener.init();
402         oObj.setFocus();
403         win.setFocus();
404         oObj.setFocus();
405         boolean res = !(fListener.gained || fListener.lost);
406         tRes.tested("removeFocusListener()", res);
407     }
408 
409     /**
410     * A class we use to test addKeyListener() and
411     * removeKeyListener()
412     */
413     public class TestKeyListener implements XKeyListener {
414         public boolean pressed = false;
415         public boolean released = false;
keyPressed(KeyEvent e)416         public void keyPressed(KeyEvent e) { pressed = true; }
keyReleased(KeyEvent e)417         public void keyReleased(KeyEvent e) { released = true; }
disposing(EventObject e)418         public void disposing(EventObject e) {}
init()419         public void init() { pressed = false; released = false; }
420     }
421 
422     private TestKeyListener kListener = new TestKeyListener();
423 
424     /**
425     * Test calls the method. <p>
426     * Has <b> OK </b> status if no exceptions were thrown. <p>
427     */
_addKeyListener()428     public void _addKeyListener() {
429         oObj.addKeyListener(kListener);
430         tRes.tested("addKeyListener()", true);
431     }
432 
433     /**
434     * Test calls the method. <p>
435     * Has <b> OK </b> status if no exceptions were thrown. <p>
436     * The following method tests are to be completed successfully before :
437     * <ul>
438     *  <li> <code> addKeyListener() </code> : adds key listener to
439     *  the object </li>
440     * </ul>
441     */
_removeKeyListener()442     public void _removeKeyListener() {
443         requiredMethod("addKeyListener()");
444         oObj.removeKeyListener(kListener);
445         tRes.tested("removeKeyListener()", true);
446     }
447 
448     /**
449     * A class we use to test addMouseListener() and
450     * removeMouseListener()
451     */
452     public class TestMouseListener implements XMouseListener {
453         public boolean pressed = false;
454         public boolean released = false;
455         public boolean entered = false;
456         public boolean exited = false;
457 
mousePressed(MouseEvent e)458         public void mousePressed(MouseEvent e) {
459             pressed = true;
460         }
461 
mouseReleased(MouseEvent e)462         public void mouseReleased(MouseEvent e) {
463             released = true;
464         }
465 
mouseEntered(MouseEvent e)466         public void mouseEntered(MouseEvent e) {
467             entered = true;
468         }
469 
mouseExited(MouseEvent e)470         public void mouseExited(MouseEvent e) {
471             exited = true;
472         }
473 
disposing(EventObject e)474         public void disposing(EventObject e) {}
475 
init()476         public void init() {
477             pressed = false;
478             released = false;
479             exited = false;
480             entered = false;
481         }
482 
483     }
484 
485     private TestMouseListener mListener = new TestMouseListener();
486 
487     /**
488     * Test calls the method. <p>
489     * Has <b> OK </b> status if no exceptions were thrown. <p>
490     */
_addMouseListener()491     public void _addMouseListener() {
492         oObj.addMouseListener(mListener);
493         tRes.tested("addMouseListener()", true);
494     }
495 
496     /**
497     * Test calls the method. <p>
498     * Has <b> OK </b> status if no exceptions were thrown. <p>
499     * The following method tests are to be completed successfully before :
500     * <ul>
501     *  <li> <code> addMouseListener() </code> : adds mouse listener to
502     * the object</li>
503     * </ul>
504     */
_removeMouseListener()505     public void _removeMouseListener() {
506         requiredMethod("addMouseListener()");
507         oObj.removeMouseListener(mListener);
508         tRes.tested("removeMouseListener()", true);
509     }
510 
511     /**
512     * A class we use to test addMouseMotionListener() and
513     * removeMouseMotionListener()
514     */
515     public class TestMouseMotionListener implements XMouseMotionListener {
516         public boolean dragged = false;
517         public boolean moved = false;
518 
mouseDragged(MouseEvent e)519         public void mouseDragged(MouseEvent e) {
520             dragged = true;
521         }
522 
mouseMoved(MouseEvent e)523         public void mouseMoved(MouseEvent e) {
524             moved = true;
525         }
526 
disposing(EventObject e)527         public void disposing(EventObject e) {}
528 
init()529         public void init() {
530             dragged = false;
531             moved = false;
532         }
533 
534     }
535 
536     private TestMouseMotionListener mmListener = new TestMouseMotionListener();
537 
538     /**
539     * Test calls the method. <p>
540     * Has <b> OK </b> status if no exceptions were thrown. <p>
541     */
_addMouseMotionListener()542     public void _addMouseMotionListener() {
543         oObj.addMouseMotionListener(mmListener);
544         tRes.tested("addMouseMotionListener()", true);
545     }
546 
547     /**
548     * Test calls the method. <p>
549     * Has <b> OK </b> status if no exceptions were thrown. <p>
550     * The following method tests are to be completed successfully before :
551     * <ul>
552     *  <li> <code> addMouseMotionListener() </code> : adds mouse motion
553     * listener to the object</li>
554     * </ul>
555     */
_removeMouseMotionListener()556     public void _removeMouseMotionListener() {
557         requiredMethod("addMouseMotionListener()");
558         oObj.removeMouseMotionListener(mmListener);
559         tRes.tested("removeMouseMotionListener()", true);
560     }
561 
562     /**
563     * A class we use to test addPaintListener() and
564     * removePaintListener()
565     */
566     public class TestPaintListener implements XPaintListener {
567         public boolean paint = false;
568 
windowPaint(PaintEvent e)569         public void windowPaint(PaintEvent e) {
570             paint = true;
571         }
572 
disposing(EventObject e)573         public void disposing(EventObject e) {}
574 
init()575         public void init() {
576             paint = false;
577         }
578 
579     }
580 
581     private TestPaintListener pListener = new TestPaintListener();
582 
583     /**
584     * Test calls the method. <p>
585     * Has <b> OK </b> status if no exceptions were thrown. <p>
586     */
_addPaintListener()587     public void _addPaintListener() {
588         oObj.addPaintListener(pListener);
589         tRes.tested("addPaintListener()", true);
590     }
591 
592     /**
593     * Test calls the method. <p>
594     * Has <b> OK </b> status if no exceptions were thrown. <p>
595     * The following method tests are to be completed successfully before :
596     * <ul>
597     *  <li> <code> addPaintListener() </code> : adds paint listener to
598     * the object </li>
599     * </ul>
600     */
_removePaintListener()601     public void _removePaintListener() {
602         requiredMethod("addPaintListener()");
603         oObj.removePaintListener(pListener);
604         tRes.tested("removePaintListener()", true);
605     }
606 
607     /**
608     * Sleeps for 0.2 sec. to allow StarOffice to react on <code>
609     * reset</code> call.
610     */
shortWait()611     private void shortWait() {
612         try {
613             Thread.sleep(2000) ;
614         } catch (InterruptedException e) {
615             log.println("While waiting :" + e) ;
616         }
617     }
618 
619 }
620 
621