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