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.lang;
29 
30 import com.sun.star.container.XNameContainer;
31 import lib.MultiMethodTest;
32 
33 import com.sun.star.frame.XDesktop;
34 import com.sun.star.lang.EventObject;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XEventListener;
37 
38 /**
39 * Testing <code>com.sun.star.lang.XComponent</code>
40 * interface methods :
41 * <ul>
42 *  <li><code> dispose()</code></li>
43 *  <li><code> addEventListener()</code></li>
44 *  <li><code> removeEventListener()</code></li>
45 * </ul>
46 * After this interface test object <b>must be recreated</b>. <p>
47 * Multithreaded test ability <b>not implemented</b> yet.
48 * @see com.sun.star.lang.XComponent
49 */
50 public class _XComponent extends MultiMethodTest {
51 
52     public static XComponent oObj = null;
53     private XNameContainer xContainer = null;
54     private XComponent altDispose = null;
55 
56     boolean listenerDisposed[] = new boolean[2];
57     String[] Loutput = new String[2];
58 
59     /**
60     * Listener which added but not removed, and its method must be called
61     * on <code>dispose</code> call.
62     */
63     public class MyEventListener implements XEventListener {
64         public void disposing ( EventObject oEvent ) {
65             Loutput[0] = Thread.currentThread() + " is DISPOSING EV1" + this;
66             listenerDisposed[0] = true;
67         }
68     };
69 
70     /**
71     * Listener which added and then removed, and its method must <b>not</b>
72     * be called on <code>dispose</code> call.
73     */
74     public class MyEventListener2 implements XEventListener {
75         public void disposing ( EventObject oEvent ) {
76             Loutput[0] = Thread.currentThread() + " is DISPOSING EV2" + this;
77             listenerDisposed[1] = true;
78         }
79     };
80 
81     XEventListener listener1 = new MyEventListener();
82     XEventListener listener2 = new MyEventListener2();
83 
84     /**
85      * For the (no longer existing) cfgmgr2.OSetElement tests: dispose the owner
86      * element.  TODO: Is this needed for anything else, too, or should it be
87      * removed?
88      */
89     protected void before() {
90         // do not dispose this component, but parent instead
91         altDispose = (XComponent)tEnv.getObjRelation("XComponent.DisposeThis");
92 
93     }
94 
95     /**
96     * Adds two listeners. <p>
97     * Has OK status if then the first listener will receive an event
98     * on <code>dispose</code> method call.
99     */
100     public void _addEventListener() {
101 
102         listenerDisposed[0] = false;
103         listenerDisposed[1] = false;
104 
105         oObj.addEventListener( listener1 );
106         oObj.addEventListener( listener2 );
107 
108         return;
109     } // finished _addEventListener()
110 
111     /**
112     * Removes the second of two added listeners. <p>
113     * Method tests to be completed successfully :
114     * <ul>
115     * <li> <code>addEventListener</code> : method must add two listeners. </li>
116     * </ul> <p>
117     * Has OK status if no events will be sent to the second listener on
118     * <code>dispose</code> method call.
119     */
120     public void _removeEventListener() {
121         executeMethod("addEventListener()");
122         if (disposed) return;
123         // the second listener should not be called
124         oObj.removeEventListener( listener2 );
125         log.println(Thread.currentThread() + " is removing EL " + listener2);
126     } // finished _removeEventListener()
127 
128     static boolean disposed = false;
129 
130     /**
131     * Disposes the object and then check appropriate listeners were
132     * called or not. <p>
133     * Method tests to be completed successfully :
134     * <ul>
135     * <li> <code>removeEventListener</code> : method must remove one of two
136     *    listeners. </li>
137     * </ul> <p>
138     * Has OK status if liseter removed wasn't called and other listener
139     * was.
140     */
141     public void _dispose() {
142         disposed = false;
143         executeMethod("removeEventListener()");
144 
145         log.println( "begin dispose in thread " + Thread.currentThread());
146         XDesktop oDesk = (XDesktop) tEnv.getObjRelation("Desktop");
147         if (oDesk !=null) {
148             oDesk.terminate();
149         }
150         else {
151             if (altDispose == null)
152                 oObj.dispose();
153             else
154                 altDispose.dispose();
155         }
156 
157         try {
158             Thread.sleep(500) ;
159         } catch (InterruptedException e) {}
160         if (Loutput[0]!=null) log.println(Loutput[0]);
161         if (Loutput[1]!=null) log.println(Loutput[1]);
162         log.println( "end dispose" + Thread.currentThread());
163         disposed = true;
164 
165         // check that dispose() works OK.
166         tRes.tested("addEventListener()", listenerDisposed[0]);
167         tRes.tested("removeEventListener()", !listenerDisposed[1]);
168         tRes.tested("dispose()", listenerDisposed[0] && !listenerDisposed[1]);
169 
170     } // finished _dispose()
171 
172     /**
173     * Forces object recreation.
174     */
175     protected void after() {
176         disposeEnvironment();
177     }
178 
179 } // finished class _XComponent
180 
181 
182