1*76b6b121SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*76b6b121SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*76b6b121SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*76b6b121SAndrew Rist  * distributed with this work for additional information
6*76b6b121SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*76b6b121SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*76b6b121SAndrew Rist  * "License"); you may not use this file except in compliance
9*76b6b121SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*76b6b121SAndrew Rist  *
11*76b6b121SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*76b6b121SAndrew Rist  *
13*76b6b121SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*76b6b121SAndrew Rist  * software distributed under the License is distributed on an
15*76b6b121SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*76b6b121SAndrew Rist  * KIND, either express or implied.  See the License for the
17*76b6b121SAndrew Rist  * specific language governing permissions and limitations
18*76b6b121SAndrew Rist  * under the License.
19*76b6b121SAndrew Rist  *
20*76b6b121SAndrew Rist  *************************************************************/
21*76b6b121SAndrew Rist 
22*76b6b121SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package complex.imageManager;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
27cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
28cdf0e10cSrcweir import com.sun.star.lang.EventObject;
29cdf0e10cSrcweir import com.sun.star.lang.XComponent;
30cdf0e10cSrcweir import com.sun.star.lang.XEventListener;
31cdf0e10cSrcweir import lib.TestParameters;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir /**
34cdf0e10cSrcweir * Testing <code>com.sun.star.lang.XComponent</code>
35cdf0e10cSrcweir * interface methods :
36cdf0e10cSrcweir * <ul>
37cdf0e10cSrcweir *  <li><code> dispose()</code></li>
38cdf0e10cSrcweir *  <li><code> addEventListener()</code></li>
39cdf0e10cSrcweir *  <li><code> removeEventListener()</code></li>
40cdf0e10cSrcweir * </ul>
41cdf0e10cSrcweir * After this interface test object <b>must be recreated</b>. <p>
42cdf0e10cSrcweir * Multithreaded test ability <b>not implemented</b> yet.
43cdf0e10cSrcweir * @see com.sun.star.lang.XComponent
44cdf0e10cSrcweir */
45cdf0e10cSrcweir public class _XComponent {
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     public static XComponent oObj = null;
48cdf0e10cSrcweir     private XNameContainer xContainer = null;
49cdf0e10cSrcweir     private XComponent altDispose = null;
50cdf0e10cSrcweir     TestParameters tEnv = null;
51cdf0e10cSrcweir     boolean listenerDisposed[] = new boolean[2];
52cdf0e10cSrcweir     String[] Loutput = new String[2];
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     /**
55cdf0e10cSrcweir     * Listener which added but not removed, and its method must be called
56cdf0e10cSrcweir     * on <code>dispose</code> call.
57cdf0e10cSrcweir     */
58cdf0e10cSrcweir     public class MyEventListener implements XEventListener {
59cdf0e10cSrcweir         int number = 0;
60cdf0e10cSrcweir         String message = null;
61cdf0e10cSrcweir         public MyEventListener(int number, String message) {
62cdf0e10cSrcweir             this.message = message;
63cdf0e10cSrcweir             this.number = number;
64cdf0e10cSrcweir         }
65cdf0e10cSrcweir         public void disposing ( EventObject oEvent ) {
66cdf0e10cSrcweir             Loutput[number] = Thread.currentThread() + " is DISPOSING " + message + this;
67cdf0e10cSrcweir             listenerDisposed[number] = true;
68cdf0e10cSrcweir         }
69cdf0e10cSrcweir     };
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     XEventListener listener1 = new MyEventListener(0, "EV1");
72cdf0e10cSrcweir     XEventListener listener2 = new MyEventListener(1, "EV2");
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     public _XComponent(TestParameters tEnv, XComponent oObj) {
75cdf0e10cSrcweir         this.tEnv = tEnv;
76cdf0e10cSrcweir         this.oObj = oObj;
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     /**
80cdf0e10cSrcweir      * For the cfgmgr2.OSetElement tests: dispose the owner element.
81cdf0e10cSrcweir      */
82cdf0e10cSrcweir     public void before() {
83cdf0e10cSrcweir         // do not dispose this component, but parent instead
84cdf0e10cSrcweir         altDispose = (XComponent)tEnv.get("XComponent.DisposeThis");
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     /**
89cdf0e10cSrcweir     * Adds two listeners. <p>
90cdf0e10cSrcweir     * Has OK status if then the first listener will receive an event
91cdf0e10cSrcweir     * on <code>dispose</code> method call.
92cdf0e10cSrcweir     */
93cdf0e10cSrcweir     public boolean _addEventListener() {
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         listenerDisposed[0] = false;
96cdf0e10cSrcweir         listenerDisposed[1] = false;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         oObj.addEventListener( listener1 );
99cdf0e10cSrcweir         oObj.addEventListener( listener2 );
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         return true;
102cdf0e10cSrcweir     } // finished _addEventListener()
103cdf0e10cSrcweir 
104cdf0e10cSrcweir     /**
105cdf0e10cSrcweir     * Removes the second of two added listeners. <p>
106cdf0e10cSrcweir     * Method tests to be completed successfully :
107cdf0e10cSrcweir     * <ul>
108cdf0e10cSrcweir     * <li> <code>addEventListener</code> : method must add two listeners. </li>
109cdf0e10cSrcweir     * </ul> <p>
110cdf0e10cSrcweir     * Has OK status if no events will be sent to the second listener on
111cdf0e10cSrcweir     * <code>dispose</code> method call.
112cdf0e10cSrcweir     */
113cdf0e10cSrcweir     public boolean _removeEventListener() {
114cdf0e10cSrcweir         if (disposed)
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir             System.out.println("Hint: already disposed.");
117cdf0e10cSrcweir             return false;
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir         // the second listener should not be called
120cdf0e10cSrcweir         oObj.removeEventListener( listener2 );
121cdf0e10cSrcweir         System.out.println(Thread.currentThread() + " is removing EL " + listener2);
122cdf0e10cSrcweir         return true;
123cdf0e10cSrcweir     } // finished _removeEventListener()
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     static boolean disposed = false;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     /**
128cdf0e10cSrcweir     * Disposes the object and then check appropriate listeners were
129cdf0e10cSrcweir     * called or not. <p>
130cdf0e10cSrcweir     * Method tests to be completed successfully :
131cdf0e10cSrcweir     * <ul>
132cdf0e10cSrcweir     * <li> <code>removeEventListener</code> : method must remove one of two
133cdf0e10cSrcweir     *    listeners. </li>
134cdf0e10cSrcweir     * </ul> <p>
135cdf0e10cSrcweir     * Has OK status if liseter removed wasn't called and other listener
136cdf0e10cSrcweir     * was.
137cdf0e10cSrcweir     */
138cdf0e10cSrcweir     public boolean _dispose() {
139cdf0e10cSrcweir         disposed = false;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         System.out.println( "begin dispose" + Thread.currentThread());
142cdf0e10cSrcweir         XDesktop oDesk = (XDesktop) tEnv.get("Desktop");
143cdf0e10cSrcweir         if (oDesk !=null) {
144cdf0e10cSrcweir             oDesk.terminate();
145cdf0e10cSrcweir         }
146cdf0e10cSrcweir         else {
147cdf0e10cSrcweir             if (altDispose == null)
148cdf0e10cSrcweir             {
149cdf0e10cSrcweir                 oObj.dispose();
150cdf0e10cSrcweir             }
151cdf0e10cSrcweir             else
152cdf0e10cSrcweir             {
153cdf0e10cSrcweir                 altDispose.dispose();
154cdf0e10cSrcweir             }
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         try {
158cdf0e10cSrcweir             Thread.sleep(500) ;
159cdf0e10cSrcweir         } catch (InterruptedException e) {}
160cdf0e10cSrcweir         if (Loutput[0]!=null){
161cdf0e10cSrcweir             System.out.println(Loutput[0]);
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir         if (Loutput[1]!=null) {
164cdf0e10cSrcweir             System.out.println(Loutput[1]);
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir         System.out.println( "end dispose" + Thread.currentThread());
167cdf0e10cSrcweir         disposed = true;
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         // check that dispose() works OK.
170cdf0e10cSrcweir         return  listenerDisposed[0] && !listenerDisposed[1];
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     } // finished _dispose()
173cdf0e10cSrcweir 
174cdf0e10cSrcweir     /**
175cdf0e10cSrcweir     * Forces object recreation.
176cdf0e10cSrcweir     */
177cdf0e10cSrcweir     protected void after() {
178cdf0e10cSrcweir //        disposeEnvironment();
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir } // finished class _XComponent
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 
184