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