xref: /AOO41X/main/qadevOOo/tests/java/ifc/frame/_XFrame.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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.frame;
29 
30 import com.sun.star.awt.XWindow;
31 //import com.sun.star.awt.XWindow;
32 import com.sun.star.frame.FrameAction;
33 import com.sun.star.frame.FrameActionEvent;
34 import com.sun.star.frame.XController;
35 import com.sun.star.frame.XFrame;
36 import com.sun.star.frame.XFrameActionListener;
37 import com.sun.star.frame.XFramesSupplier;
38 import com.sun.star.lang.EventObject;
39 import java.io.PrintWriter;
40 import lib.MultiMethodTest;
41 import lib.TestEnvironment;
42 
43 /**
44 * Testing <code>com.sun.star.frame.XFrame</code>
45 * interface methods:
46 * <ul>
47 *  <li><code> getName() </code></li>
48 *  <li><code> setName() </code></li>
49 *  <li><code> activate() </code></li>
50 *  <li><code> deactivate() </code></li>
51 *  <li><code> isActive() </code></li>
52 *  <li><code> addFrameActionListener() </code></li>
53 *  <li><code> removeFrameActionListener() </code></li>
54 *  <li><code> getCreator() </code></li>
55 *  <li><code> getComponentWindow() </code></li>
56 *  <li><code> getContainerWindow() </code></li>
57 *  <li><code> getController() </code></li>
58 *  <li><code> isTop() </code></li>
59 *  <li><code> findFrame() </code></li>
60 *  <li><code> contextChanged() </code></li>
61 *  <li><code> setCreator() </code></li>
62 *  <li><code> setComponent() </code></li>
63 *  <li><code> initialize() </code></li>
64 * </ul><p>
65 * This test needs the following object relations :
66 * <ul>
67 *  <li> <code>'XFrame'</code> (of type <code>XFrame</code>)
68 *  <b>optional</b>: any frame named 'XFrame'.
69 *  Could be used by <code>findFrame</code> method to try
70 *  to find other frame than itself.</li>
71 *
72 *  <li> <code>'Desktop'</code> (of type <code>Object</code>):
73 *  if exsists, then desktop component is tested</li>
74 * </ul> <p>
75 * Test is <b> NOT </b> multithread compilant. <p>
76 * @see com.sun.star.frame.XFrame
77 */
78 public class _XFrame extends MultiMethodTest {
79     final FrameAction[] actionEvent = new FrameAction[1] ;
80     final boolean[] listenerCalled = new boolean[] {false} ;
81     final boolean[] activatedCalled = new boolean[] {false} ;
82     final boolean[] deactivatedCalled = new boolean[] {false} ;
83     final TestFrameActionListener listener =
84         new TestFrameActionListener() ;
85     public static XFrame oObj = null;
86 
87     /**
88     * Class used to test listeners.
89     */
90     private class TestFrameActionListener
91         implements XFrameActionListener {
92 
93         public void frameAction(FrameActionEvent e) {
94             listenerCalled[0] = true ;
95             activatedCalled[0] |= e.Action == FrameAction.FRAME_ACTIVATED;
96             deactivatedCalled[0] |= e.Action == FrameAction.FRAME_DEACTIVATING;
97             actionEvent[0] = e.Action;
98         }
99 
100         public void disposing(EventObject e) {}
101 
102     }
103 
104     /**
105     * Test calls the method. <p>
106     * Has <b> OK </b> status if the method does not return null.
107     */
108     public void _getName() {
109         String name = oObj.getName() ;
110         if (name == null)
111             log.println("getName() returned null: FAILED") ;
112 
113         tRes.tested("getName()", name!=null) ;
114     }
115 
116     /**
117     * Test calls the method. <p>
118     * Has <b> OK </b> status if set and gotten names are equal.
119     */
120     public void _setName() {
121         String sName = "XFrame" ;
122 
123         oObj.setName(sName);
124         String gName = oObj.getName();
125         boolean res = sName.equals(gName);
126         if (! res)
127             log.println("setName('" + sName +
128                 "'), but getName() return '" + gName + "'") ;
129         tRes.tested("setName()", res);
130     }
131 
132     /**
133     * Test calls the method. <p>
134     * Has <b> OK </b> status if the method successfully returns
135     * and no exceptions were thrown.
136     */
137     public void _activate() {
138         oObj.activate() ;
139         tRes.tested("activate()", true) ;
140     }
141 
142     /**
143     * Test calls the method. <p>
144     * Has <b> OK </b> status if the method successfully returns
145     * and no exceptions were thrown.
146     */
147     public void _deactivate() {
148         oObj.deactivate() ;
149         oObj.activate() ;
150         tRes.tested("deactivate()", true) ;
151     }
152 
153     /**
154     * Test calls the method. Then frame is deactivated and method called
155     * again. <p>
156     * Has <b> OK </b> status if isDesktop() returns true or if the method
157     * always display real status of a frame during activation/deactivation.
158     */
159     public void _isActive() {
160         boolean result = true;
161 
162         if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
163             log.println("Desktop is always active");
164             tRes.tested("isActive()", oObj.isActive()) ;
165             return;
166         }
167 
168         oObj.deactivate();
169         result &= !oObj.isActive();
170         if (oObj.isActive())
171             log.println("after deactivate() method call, isActive() returned true");
172         oObj.activate();
173         result &= oObj.isActive();
174         if (!oObj.isActive())
175             log.println("after activate() method call, isActive() returned false") ;
176         boolean res = isDesktop(log,tEnv,"isActive()");
177         if (res) result=res;
178 
179         tRes.tested("isActive()", result) ;
180     }
181 
182     /**
183     * Test calls the method. Then frame status (activated/deactivated) is
184     * changed, and the listener is checked.<p>
185     * Has <b> OK </b> status if isDesktop() method returnes true, or if the
186     * listener was called and frame was activated.
187     */
188     public void _addFrameActionListener() {
189         boolean result = true ;
190 
191         oObj.addFrameActionListener(listener) ;
192         oObj.activate() ;
193         oObj.deactivate() ;
194         oObj.activate() ;
195 
196         if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
197             log.println("No actions supported by Desktop");
198             tRes.tested("addFrameActionListener()", true) ;
199             return;
200         }
201 
202         try {
203             Thread.sleep(500);
204         }catch (InterruptedException ex) {}
205 
206 
207         if (!listenerCalled[0]) {
208             log.println("listener was not called.") ;
209             result = false ;
210         } else {
211             if (!activatedCalled[0]) {
212                 log.println("Listener was called, FRAME_ACTIVATED was not") ;
213                 result = false ;
214             }
215             if (!deactivatedCalled[0]) {
216                 log.println("Listener was called, FRAME_DEACTIVATING was not") ;
217                 result = false ;
218             }
219         }
220 
221         boolean res = isDesktop(log, tEnv, "addFrameActionListener()");
222         if (res) result=res;
223 
224         tRes.tested("addFrameActionListener()", result) ;
225     }
226 
227     /**
228     * Test calls the method.  Then frame status (activated/deactivated) is
229     * changed, and the listener is checked.<p>
230     * Has <b> OK </b> status if isDesktop() method returns true, or if the
231     * method actually removes listener so it does not react on
232     * activate/deactivate events. <p>
233     * The following method tests are to be completed successfully before :
234     * <ul>
235     *  <li> <code> addFrameActionListener() </code>: adds action listener
236     * to a frame </li>
237     * </ul>
238     */
239     public void _removeFrameActionListener() {
240         boolean result = true;
241 
242         requiredMethod("addFrameActionListener()");
243         listenerCalled[0] = false;
244         oObj.removeFrameActionListener(listener);
245         oObj.activate();
246         oObj.deactivate();
247         oObj.activate();
248         if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
249             log.println("No actions supported by Desktop");
250             tRes.tested("removeFrameActionListener()", true) ;
251             return;
252         }
253 
254         if (listenerCalled[0])
255             log.println("Listener wasn't removed, and was called");
256         boolean res = isDesktop(log, tEnv, "removeFrameActionListener()");
257         if (res) result=res; else result = (!listenerCalled[0]);
258 
259         tRes.tested("removeFrameActionListener()", result);
260     }
261 
262     /**
263     * Test calls the method. <p>
264     * Has <b> OK </b> status if isDesktop() method returns true or if the method
265     * does not return null.
266     */
267     public void _getCreator() {
268         boolean result = true;
269 
270         if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
271             log.println("Desktop has no creator");
272             tRes.tested("getCreator()", true) ;
273             return;
274         }
275 
276         XFramesSupplier creator = oObj.getCreator() ;
277         if (creator == null)
278             log.println("getCreator() returns null") ;
279         boolean res = isDesktop(log,tEnv,"getCreator()");
280         if (res) result=res; else result = (creator != null);
281         tRes.tested("getCreator()", result) ;
282     }
283 
284     /**
285     * Test calls the method. <p>
286     * Has <b> OK </b> status if isDesktop() method returns true or if the method
287     * does not return null.
288     */
289     public void _getComponentWindow() {
290         boolean result = true;
291 
292         XWindow win = oObj.getComponentWindow() ;
293 
294         if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
295             log.println("Desktop has no component window");
296             tRes.tested("getComponentWindow()", true) ;
297             return;
298         }
299 
300         if (win == null)
301             log.println("getComponentWindow() returns null") ;
302         boolean res = isDesktop(log,tEnv,"getComponentWindow()");
303         if (res) result=res; else result = (win != null);
304         tRes.tested("getComponentWindow()", result) ;
305     }
306 
307     /**
308     * Test calls the method. <p>
309     * Has <b> OK </b> status if isDesktop() method returns true or if the method
310     * does not return null.
311     */
312     public void _getContainerWindow() {
313         boolean result = true;
314 
315         if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
316             log.println("Desktop has no container window");
317             tRes.tested("getContainerWindow()", true) ;
318             return;
319         }
320 
321         XWindow win = oObj.getContainerWindow() ;
322         if (win == null)
323             log.println("getContainerWindow() returns null") ;
324         boolean res = isDesktop(log,tEnv,"getContainerWindow()");
325         if (res) result=res; else result = (win != null);
326         tRes.tested("getContainerWindow()", result) ;
327     }
328 
329     /**
330     * Test calls the method. Then returned controller is checked. <p>
331     * Has <b> OK </b> status if isDesktop() method returns true or
332     * if the method returns non-null controller, having frame that's equal to
333     * a (XFrame) oObj.
334     */
335     public void _getController() {
336         boolean result = true;
337         XController ctrl = oObj.getController();
338 
339         if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
340             log.println("Desktop has no controller");
341             tRes.tested("getController()", true) ;
342             return;
343         }
344 
345         if (ctrl == null) {
346             log.println("getController() returns null");
347             result = false;
348         } else {
349             XFrame frm = ctrl.getFrame();
350             if (!oObj.equals(frm)) {
351                 log.println("Frame returned by controller not " +
352                     "equals to frame testing");
353                 result = false;
354             }
355         }
356         boolean res = isDesktop(log, tEnv, "getController()");
357         if (res) result=res;
358         tRes.tested("getController()", result) ;
359     }
360 
361     /**
362     * Test calls the method. <p>
363     * Has <b> OK </b> status if the method successfully returns
364     * and no exceptions were thrown.
365     */
366     public void _isTop() {
367         log.println("isTop() = " + oObj.isTop());
368         tRes.tested("isTop()", true) ;
369     }
370 
371     /**
372     * After obtaining an object relation 'XFrame', test tries to find a frame
373     * named 'XFrame'. <p>
374     * Has <b> OK </b> status if the method returns non-null object that's equal
375     * to previously obtained object relation.
376     */
377     public void _findFrame() {
378         boolean result = true ;
379 
380         XFrame aFrame = (XFrame) tEnv.getObjRelation("XFrame");
381 
382         if (aFrame != null) {
383             log.println("Trying to find a frame with name 'XFrame' ...");
384             XFrame frame = oObj.findFrame("XFrame",
385                 com.sun.star.frame.FrameSearchFlag.GLOBAL) ;
386             if (frame == null) {
387                 log.println("findFrame(\"XFrame,com.sun.star.frame.FrameSearchFlag.GLOBAL\") returns null") ;
388                 result = false ;
389             } else if ( !aFrame.equals(frame) ) {
390                 log.println("findFrame(\"XFrame,com.sun.star.frame.FrameSearchFlag.GLOBAL\") "
391                     + " returns frame which is not equal to passed in relation") ;
392                 result = false ;
393             }
394         }
395 
396         log.println("Trying to find a frame with name '_self' ...");
397         XFrame frame = oObj.findFrame("_self",
398             com.sun.star.frame.FrameSearchFlag.AUTO) ;
399         if (frame == null) {
400             log.println("findFrame(\"_self\") returns null") ;
401             result = false ;
402         } else if ( !oObj.equals(frame) ) {
403             log.println("findFrame(\"_self\") "
404                 + " returns frame which is not equal to tested") ;
405             result = false ;
406         }
407 
408         tRes.tested("findFrame()", result) ;
409     }
410 
411     /**
412     * At first new listener is added, then test calls the method and result
413     * is checked. <p>
414     * Has <b> OK </b> status if isDesktop() method returnes true or if the
415     * listener was called and proper event past to listener.
416     */
417     public void _contextChanged() {
418         boolean result = true;
419         TestFrameActionListener listener = new TestFrameActionListener();
420 
421         if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
422             log.println("Desktop cann't change context");
423             tRes.tested("contextChanged()", true) ;
424             return;
425         }
426 
427         listenerCalled[0] = false;
428         oObj.addFrameActionListener(listener);
429         try {
430             oObj.contextChanged();
431             if ( !listenerCalled[0] ) {
432                 log.println("listener was not called on contextChanged() call.") ;
433                 result = false;
434             } else if (actionEvent[0] != FrameAction.CONTEXT_CHANGED) {
435                 log.println("listener was called, but Action != CONTEXT_CHANGED") ;
436                 result = false;
437             }
438         } finally {
439             oObj.removeFrameActionListener(listener);
440         }
441 
442         boolean res = isDesktop(log, tEnv, "contextChanged()");
443         if (res) result = res;
444         tRes.tested("contextChanged()", result);
445     }
446 
447 
448     /**
449     * Test calls the method. Remembered old creater is restored at the end. <p>
450     * Has <b> OK </b> status if the method sucessfully set new value to (XFrame)
451     * oObj object.
452     */
453     public void _setCreator() {
454         if (tEnv.getTestCase().getObjectName().equals("Desktop")) {
455             log.println("Desktop has no creator");
456             tRes.tested("setCreator()", true) ;
457             return;
458         }
459 
460         XFramesSupplier oldCreator = oObj.getCreator() ;
461         oObj.setCreator(null) ;
462         tRes.tested("setCreator()", oObj.getCreator() == null) ;
463         oObj.setCreator(oldCreator) ;
464     }
465 
466     /**
467     * Test calls the method, then result is checked.<p>
468     * Has <b> OK </b> status if method returns true, and values, set by the
469     * method are nulls, or if method returns false, and values are not changed.
470     * This method destroy the object. Therfore all other methods have to be
471     * executed before :
472     * <ul>
473     *  <li> <code> getName() </code>
474     *  <li> <code> setName() </code>
475     *  <li> <code> activate() </code>
476     *  <li> <code> deactivate() </code>
477     *  <li> <code> isActive() </code>
478     *  <li> <code> addFrameActionListener() </code>
479     *  <li> <code> getComponentWindow() </code>
480     *  <li> <code> getContainerWindow() </code>
481     *  <li> <code> getController() </code>
482     *  <li> <code> isTop() </code>
483     *  <li> <code> findFrame() </code>
484     *  <li> <code> contextChanged() </code>
485     *  <li> <code> setCreator() </code>
486     *  object</li>
487     * </ul>
488     */
489     public void _setComponent() {
490         // setComponent() destr
491         requiredMethod("getName()") ;
492         requiredMethod("setName()") ;
493         requiredMethod("activate()") ;
494         requiredMethod("deactivate()") ;
495         requiredMethod("isActive()") ;
496         requiredMethod("addFrameActionListener()") ;
497         requiredMethod("getComponentWindow()") ;
498         requiredMethod("getContainerWindow()") ;
499         requiredMethod("getController()") ;
500         requiredMethod("isTop()") ;
501         requiredMethod("findFrame()") ;
502         requiredMethod("contextChanged()") ;
503         requiredMethod("setCreator()") ;
504 
505         boolean result = true;
506 
507         XWindow oldWindow = oObj.getComponentWindow();
508         XController oldController = oObj.getController();
509         boolean rs = oObj.setComponent(null, null);
510         if (rs) {  // component must be changed
511             result &= oObj.getComponentWindow() == null;
512             result &= oObj.getController() == null;
513             if (!result)
514                 log.println("setComponent() returns true, but component is " +
515                  "not changed.");
516         } else {   // frame is not allowed to change component
517             result &= oObj.getComponentWindow() == oldWindow ;
518             result &= oObj.getController() == oldController ;
519             if (!result)
520                 log.println("setComponent() returns false, but component is" +
521                  "changed.");
522         }
523         tRes.tested("setComponent()", result);
524 
525     }
526 
527     /**
528      * Test calls the method. <p>
529      * Has <b> OK </b> status if the method successfully returns.
530      * In case a frame should initialised twice, a
531      * <CODE>com.sun.star.uno.RuntimeException</CODE> was thron. This is ok. But since
532      * a com.sun.star.uno.RuntimeException could thrown in any state the message of
533      * the exception must contain a defined string. In this case the test get an
534      * <CODE>OK</CODE> status.
535      * The following method tests are to be completed successfully before :
536      * <ul>
537      *  <li> <code> setComponent() </code> : sets window and controller to the
538      *  object</li>
539      * </ul>
540      */
541     public void _initialize() {
542         requiredMethod("setComponent()") ;
543         XWindow win = oObj.getContainerWindow() ;
544         boolean bOK = true;
545         try {
546             oObj.initialize(win) ;
547         } catch (com.sun.star.uno.RuntimeException e){
548             String message="Frame::initialized() is called more then once, which isnt usefull nor allowed.";
549             if (e.toString().indexOf(message) != -1){
550                 log.println(e.toString());
551                 log.println("methods throws exception, but it's OK");
552             }else{
553                 log.println(e.toString());
554                 bOK=false;
555             }
556 
557         }
558         tRes.tested("initialize()", bOK) ;
559     }
560 
561     /**
562     * Checks does relation 'Desktop' exist. Returns true if exist.
563     */
564     public static boolean isDesktop(PrintWriter log,
565             TestEnvironment env, String method) {
566         Object isD = env.getObjRelation("Desktop");
567         if (isD != null) {
568             log.println("The Desktop doesn't support the method " + method);
569             log.println("It will always return true");
570             return true;
571         }
572         else {
573             return false;
574         }
575     }
576 
577     /**
578     * Forces environment recreation.
579     */
580     public void after() {
581         disposeEnvironment();
582     }
583 }
584