xref: /trunk/main/ucb/qa/complex/tdoc/_XComponent.java (revision 67e470da)
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 complex.tdoc;
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 import share.LogWriter;
34 
35 /**
36 * Testing <code>com.sun.star.lang.XComponent</code>
37 * interface methods :
38 * <ul>
39 *  <li><code> dispose()</code></li>
40 *  <li><code> addEventListener()</code></li>
41 *  <li><code> removeEventListener()</code></li>
42 * </ul>
43 * After this interface test object <b>must be recreated</b>. <p>
44 * Multithreaded test ability <b>not implemented</b> yet.
45 * @see com.sun.star.lang.XComponent
46 */
47 public class _XComponent {
48 
49     public static XComponent oObj = null;
50     private XNameContainer xContainer = null;
51     private XComponent altDispose = null;
52     public LogWriter log = null;
53 
54     boolean listenerDisposed[] = new boolean[2];
55     String[] Loutput = new String[2];
56 
57     /**
58     * Listener which added but not removed, and its method must be called
59     * on <code>dispose</code> call.
60     */
61     public class MyEventListener implements XEventListener {
disposing( EventObject oEvent )62         public void disposing ( EventObject oEvent ) {
63             Loutput[0] = Thread.currentThread() + " is DISPOSING EV1" + this;
64             listenerDisposed[0] = true;
65         }
66     };
67 
68     /**
69     * Listener which added and then removed, and its method must <b>not</b>
70     * be called on <code>dispose</code> call.
71     */
72     public class MyEventListener2 implements XEventListener {
disposing( EventObject oEvent )73         public void disposing ( EventObject oEvent ) {
74             Loutput[0] = Thread.currentThread() + " is DISPOSING EV2" + this;
75             listenerDisposed[1] = true;
76         }
77     };
78 
79     XEventListener listener1 = new MyEventListener();
80     XEventListener listener2 = new MyEventListener2();
81 
82     /**
83      * For the cfgmgr2.OSetElement tests: dispose the owner element.
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 boolean _addEventListener() {
97 
98         listenerDisposed[0] = false;
99         listenerDisposed[1] = false;
100 
101         oObj.addEventListener( listener1 );
102         oObj.addEventListener( listener2 );
103 
104         return true;
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 boolean _removeEventListener() {
117 //        executeMethod("addEventListener()");
118         if (disposed) return true;
119         // the second listener should not be called
120         oObj.removeEventListener( listener2 );
121         log.println(Thread.currentThread() + " is removing EL " + listener2);
122         return true;
123     } // finished _removeEventListener()
124 
125     static boolean disposed = false;
126 
127     /**
128     * Disposes the object and then check appropriate listeners were
129     * called or not. <p>
130     * Method tests to be completed successfully :
131     * <ul>
132     * <li> <code>removeEventListener</code> : method must remove one of two
133     *    listeners. </li>
134     * </ul> <p>
135     * Has OK status if liseter removed wasn't called and other listener
136     * was.
137     */
_dispose()138     public boolean _dispose() {
139         disposed = false;
140 //        executeMethod("removeEventListener()");
141 
142         log.println( "begin dispose" + Thread.currentThread());
143         oObj.dispose();
144 
145         try {
146             Thread.sleep(500) ;
147         } catch (InterruptedException e) {}
148         if (Loutput[0]!=null) log.println(Loutput[0]);
149         if (Loutput[1]!=null) log.println(Loutput[1]);
150         log.println( "end dispose" + Thread.currentThread());
151         disposed = true;
152 
153         // check that dispose() works OK.
154         return  listenerDisposed[0] && !listenerDisposed[1];
155 
156     } // finished _dispose()
157 
158     /**
159     * Forces object recreation.
160     */
after()161     protected void after() {
162 //        disposeEnvironment();
163     }
164 
165 } // finished class _XComponent
166 
167 
168