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