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