1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package complex.imageManager;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
31*cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
32*cdf0e10cSrcweir import com.sun.star.lang.EventObject;
33*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
34*cdf0e10cSrcweir import com.sun.star.lang.XEventListener;
35*cdf0e10cSrcweir import lib.TestParameters;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir /**
38*cdf0e10cSrcweir * Testing <code>com.sun.star.lang.XComponent</code>
39*cdf0e10cSrcweir * interface methods :
40*cdf0e10cSrcweir * <ul>
41*cdf0e10cSrcweir *  <li><code> dispose()</code></li>
42*cdf0e10cSrcweir *  <li><code> addEventListener()</code></li>
43*cdf0e10cSrcweir *  <li><code> removeEventListener()</code></li>
44*cdf0e10cSrcweir * </ul>
45*cdf0e10cSrcweir * After this interface test object <b>must be recreated</b>. <p>
46*cdf0e10cSrcweir * Multithreaded test ability <b>not implemented</b> yet.
47*cdf0e10cSrcweir * @see com.sun.star.lang.XComponent
48*cdf0e10cSrcweir */
49*cdf0e10cSrcweir public class _XComponent {
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir     public static XComponent oObj = null;
52*cdf0e10cSrcweir     private XNameContainer xContainer = null;
53*cdf0e10cSrcweir     private XComponent altDispose = null;
54*cdf0e10cSrcweir     TestParameters tEnv = null;
55*cdf0e10cSrcweir     boolean listenerDisposed[] = new boolean[2];
56*cdf0e10cSrcweir     String[] Loutput = new String[2];
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir     /**
59*cdf0e10cSrcweir     * Listener which added but not removed, and its method must be called
60*cdf0e10cSrcweir     * on <code>dispose</code> call.
61*cdf0e10cSrcweir     */
62*cdf0e10cSrcweir     public class MyEventListener implements XEventListener {
63*cdf0e10cSrcweir         int number = 0;
64*cdf0e10cSrcweir         String message = null;
65*cdf0e10cSrcweir         public MyEventListener(int number, String message) {
66*cdf0e10cSrcweir             this.message = message;
67*cdf0e10cSrcweir             this.number = number;
68*cdf0e10cSrcweir         }
69*cdf0e10cSrcweir         public void disposing ( EventObject oEvent ) {
70*cdf0e10cSrcweir             Loutput[number] = Thread.currentThread() + " is DISPOSING " + message + this;
71*cdf0e10cSrcweir             listenerDisposed[number] = true;
72*cdf0e10cSrcweir         }
73*cdf0e10cSrcweir     };
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     XEventListener listener1 = new MyEventListener(0, "EV1");
76*cdf0e10cSrcweir     XEventListener listener2 = new MyEventListener(1, "EV2");
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     public _XComponent(TestParameters tEnv, XComponent oObj) {
79*cdf0e10cSrcweir         this.tEnv = tEnv;
80*cdf0e10cSrcweir         this.oObj = oObj;
81*cdf0e10cSrcweir     }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     /**
84*cdf0e10cSrcweir      * For the cfgmgr2.OSetElement tests: dispose the owner element.
85*cdf0e10cSrcweir      */
86*cdf0e10cSrcweir     public void before() {
87*cdf0e10cSrcweir         // do not dispose this component, but parent instead
88*cdf0e10cSrcweir         altDispose = (XComponent)tEnv.get("XComponent.DisposeThis");
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     /**
93*cdf0e10cSrcweir     * Adds two listeners. <p>
94*cdf0e10cSrcweir     * Has OK status if then the first listener will receive an event
95*cdf0e10cSrcweir     * on <code>dispose</code> method call.
96*cdf0e10cSrcweir     */
97*cdf0e10cSrcweir     public boolean _addEventListener() {
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir         listenerDisposed[0] = false;
100*cdf0e10cSrcweir         listenerDisposed[1] = false;
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir         oObj.addEventListener( listener1 );
103*cdf0e10cSrcweir         oObj.addEventListener( listener2 );
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir         return true;
106*cdf0e10cSrcweir     } // finished _addEventListener()
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     /**
109*cdf0e10cSrcweir     * Removes the second of two added listeners. <p>
110*cdf0e10cSrcweir     * Method tests to be completed successfully :
111*cdf0e10cSrcweir     * <ul>
112*cdf0e10cSrcweir     * <li> <code>addEventListener</code> : method must add two listeners. </li>
113*cdf0e10cSrcweir     * </ul> <p>
114*cdf0e10cSrcweir     * Has OK status if no events will be sent to the second listener on
115*cdf0e10cSrcweir     * <code>dispose</code> method call.
116*cdf0e10cSrcweir     */
117*cdf0e10cSrcweir     public boolean _removeEventListener() {
118*cdf0e10cSrcweir         if (disposed)
119*cdf0e10cSrcweir         {
120*cdf0e10cSrcweir             System.out.println("Hint: already disposed.");
121*cdf0e10cSrcweir             return false;
122*cdf0e10cSrcweir         }
123*cdf0e10cSrcweir         // the second listener should not be called
124*cdf0e10cSrcweir         oObj.removeEventListener( listener2 );
125*cdf0e10cSrcweir         System.out.println(Thread.currentThread() + " is removing EL " + listener2);
126*cdf0e10cSrcweir         return true;
127*cdf0e10cSrcweir     } // finished _removeEventListener()
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir     static boolean disposed = false;
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     /**
132*cdf0e10cSrcweir     * Disposes the object and then check appropriate listeners were
133*cdf0e10cSrcweir     * called or not. <p>
134*cdf0e10cSrcweir     * Method tests to be completed successfully :
135*cdf0e10cSrcweir     * <ul>
136*cdf0e10cSrcweir     * <li> <code>removeEventListener</code> : method must remove one of two
137*cdf0e10cSrcweir     *    listeners. </li>
138*cdf0e10cSrcweir     * </ul> <p>
139*cdf0e10cSrcweir     * Has OK status if liseter removed wasn't called and other listener
140*cdf0e10cSrcweir     * was.
141*cdf0e10cSrcweir     */
142*cdf0e10cSrcweir     public boolean _dispose() {
143*cdf0e10cSrcweir         disposed = false;
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir         System.out.println( "begin dispose" + Thread.currentThread());
146*cdf0e10cSrcweir         XDesktop oDesk = (XDesktop) tEnv.get("Desktop");
147*cdf0e10cSrcweir         if (oDesk !=null) {
148*cdf0e10cSrcweir             oDesk.terminate();
149*cdf0e10cSrcweir         }
150*cdf0e10cSrcweir         else {
151*cdf0e10cSrcweir             if (altDispose == null)
152*cdf0e10cSrcweir             {
153*cdf0e10cSrcweir                 oObj.dispose();
154*cdf0e10cSrcweir             }
155*cdf0e10cSrcweir             else
156*cdf0e10cSrcweir             {
157*cdf0e10cSrcweir                 altDispose.dispose();
158*cdf0e10cSrcweir             }
159*cdf0e10cSrcweir         }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir         try {
162*cdf0e10cSrcweir             Thread.sleep(500) ;
163*cdf0e10cSrcweir         } catch (InterruptedException e) {}
164*cdf0e10cSrcweir         if (Loutput[0]!=null){
165*cdf0e10cSrcweir             System.out.println(Loutput[0]);
166*cdf0e10cSrcweir         }
167*cdf0e10cSrcweir         if (Loutput[1]!=null) {
168*cdf0e10cSrcweir             System.out.println(Loutput[1]);
169*cdf0e10cSrcweir         }
170*cdf0e10cSrcweir         System.out.println( "end dispose" + Thread.currentThread());
171*cdf0e10cSrcweir         disposed = true;
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir         // check that dispose() works OK.
174*cdf0e10cSrcweir         return  listenerDisposed[0] && !listenerDisposed[1];
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     } // finished _dispose()
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir     /**
179*cdf0e10cSrcweir     * Forces object recreation.
180*cdf0e10cSrcweir     */
181*cdf0e10cSrcweir     protected void after() {
182*cdf0e10cSrcweir //        disposeEnvironment();
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir } // finished class _XComponent
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 
188