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