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 ifc.frame;
25 
26 
27 import com.sun.star.beans.PropertyValue;
28 import com.sun.star.frame.XDispatch;
29 import com.sun.star.util.URL;
30 import lib.MultiMethodTest;
31 import lib.Status;
32 import lib.StatusException;
33 import com.sun.star.frame.XNotifyingDispatch;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.frame.DispatchResultEvent;
36 
37 /**
38 * Testing <code>com.sun.star.frame.XDispatch</code>
39 * interface methods :
40 * <ul>
41 *  <li><code> dispatch()</code></li>
42 *  <li><code> addStatusListener()</code></li>
43 *  <li><code> removeStatusListener()</code></li>
44 * </ul> <p>
45 * This test needs the following object relations :
46 * <ul>
47 *  <li> <code>'XDispatch.URL'</code> (of type <code>com.sun.star.util.URL
48 *   </code>): URL for passing to <code>dispatch()</code> method. </li>
49 * <ul> <p>
50 * @see com.sun.star.frame.XDispatch
51 * @see com.sun.star.frame.XNotifyingDispatch
52 * @see ifc.frame._XDispatch
53 * @see ifc.frame._XNotifyingDispatch
54 */
55 
56 public class _XDispatch extends MultiMethodTest {
57 
58     public XDispatch oObj = null;
59 
60     /**
61     * Listener implementation which sets flags on appropriate method calls
62     */
63     protected class TestStatusListener implements
64             com.sun.star.frame.XStatusListener {
65         public boolean disposingCalled = false ;
66         public boolean statusChangedCalled = false ;
67         private java.io.PrintWriter log = null ;
68 
TestStatusListener(java.io.PrintWriter log)69         public TestStatusListener(java.io.PrintWriter log) {
70             this.log = log ;
71         }
72 
disposing(com.sun.star.lang.EventObject e)73         public void disposing(com.sun.star.lang.EventObject e) {
74             disposingCalled = true ;
75             log.println(" disposing was called.") ;
76         }
77 
statusChanged(com.sun.star.frame.FeatureStateEvent e)78         public void statusChanged(com.sun.star.frame.FeatureStateEvent e) {
79             statusChangedCalled = true ;
80             log.println(" statusChanged was called.") ;
81             log.println("  FeatureURL = '" + e.FeatureURL + "'");
82             log.println("  FeatureDescriptor = '" + e.FeatureDescriptor + "'");
83             log.println("  IsEnabled = " + e.IsEnabled);
84             log.println("  Requery = " + e.Requery);
85             log.println("  State = '" + e.State.toString() +  "'");
86         }
87 
88     }
89 
90     /**
91     * Listener implementation which sets flags on appropriate method calls
92     */
93     protected class TestNotificationListener implements
94             com.sun.star.frame.XDispatchResultListener {
95         public boolean disposingCalled = false ;
96         public boolean finishedDispatch = false ;
97         private java.io.PrintWriter log = null ;
98 
TestNotificationListener(java.io.PrintWriter log)99         public TestNotificationListener(java.io.PrintWriter log) {
100             this.log = log ;
101         }
102 
disposing(com.sun.star.lang.EventObject e)103         public void disposing(com.sun.star.lang.EventObject e) {
104             disposingCalled = true ;
105             log.println("   disposing was called.") ;
106         }
107 
dispatchFinished( DispatchResultEvent e)108         public void dispatchFinished( DispatchResultEvent e) {
109             finishedDispatch = true ;
110             log.println("   dispatchFinished was called.") ;
111         }
112 
113     }
114 
115     TestStatusListener listener = null ;
116     TestNotificationListener notificationListener = null;
117     URL url = null ;
118 
119     /**
120      * Not all implementations could call the
121      * <code>com.sun.star.frame.XStatusListener</code>. For this purposes the
122      * <code>com.sun.star.frame.XDispatchWithNotification</code> was designed.
123      * If <code>com.sun.star.frame.XStatusListener</code> was not called and
124      * <code>com.sun.star.frame.XStatusListener</code> is present, it was used
125      * to check listeners.
126     */
checkXDispatchWithNotification()127     private boolean checkXDispatchWithNotification()
128     {
129         XNotifyingDispatch xND = (XNotifyingDispatch)
130                       UnoRuntime.queryInterface(XNotifyingDispatch.class, oObj);
131         if ( xND != null) {
132             log.println("   XNotifyingDispatch found:");
133             PropertyValue[] arguments = (PropertyValue[])
134                               tEnv.getObjRelation("XNotifyingDispatchArgument");
135 
136             notificationListener = new TestNotificationListener(log) ;
137             xND.dispatchWithNotification(url, arguments, notificationListener);
138 
139             try {
140                 Thread.sleep(200);
141             }
142             catch(java.lang.InterruptedException e) {}
143 
144             log.println("   Listener called: "+ notificationListener.finishedDispatch);
145 
146             return notificationListener.finishedDispatch;
147         } else {
148             return false;
149         }
150 
151     }
152     /**
153     * Retrieves object relations and creates new listeners.
154     * @throws StatusException If one of relations not found.
155     */
before()156     public void before() {
157         listener = new TestStatusListener(log) ;
158         url = (URL) tEnv.getObjRelation("XDispatch.URL") ;
159 
160         if (url == null) throw new StatusException
161             (Status.failed("Relation not found.")) ;
162     }
163 
164     /**
165     * Calls the method using URL from relation. <p>
166     * Has <b> OK </b> status if one listener (not removed) is called, and
167     * another (removed) is not.
168     * The following method tests are to be completed successfully before :
169     * <ul>
170     *  <li> <code>addStatusListener</code> :
171     *    to check that the listener is called
172     *  </li>
173     * </ul>
174     */
_dispatch()175     public void _dispatch() {
176         requiredMethod("addStatusListener()") ;
177 
178         boolean result = true ;
179 
180         oObj.dispatch(url, new PropertyValue[0]) ;
181 
182         try {
183             Thread.sleep(200);
184         }
185         catch(java.lang.InterruptedException e) {}
186 
187         log.println("Listener called: "+ listener.statusChangedCalled);
188 
189         result = listener.statusChangedCalled;
190 
191         if (result == false) {
192             result = checkXDispatchWithNotification();
193         }
194 
195         tRes.tested("dispatch()", result) ;
196     }
197 
198     /**
199     * Adds two listeners. <p>
200     * Has <b> OK </b> status if no runtime exceptions occurred.
201     */
_addStatusListener()202     public void _addStatusListener() {
203 
204         boolean result = true ;
205         oObj.addStatusListener(listener, url) ;
206 
207         tRes.tested("addStatusListener()", result) ;
208     }
209 
210     /**
211     * Removes the listener added before. <p>
212     * Has <b> OK </b> status if the dispatch call doesn't call the listener.
213     * The following method tests are to be completed successfully before :
214     * <ul>
215     *  <li> <code> dispatch() </code> : to have a listener to remove
216     *  </li>
217     * </ul>
218     */
_removeStatusListener()219     public void _removeStatusListener() {
220         requiredMethod("dispatch()") ;
221         listener.statusChangedCalled = false;
222         boolean result = true ;
223         oObj.removeStatusListener(listener, url) ;
224 
225         oObj.dispatch(url, new PropertyValue[0]) ;
226 
227         try {
228             Thread.sleep(200);
229         }
230         catch(java.lang.InterruptedException e) {}
231 
232         System.out.println("Listener called: "+ listener.statusChangedCalled);
233 
234         result = ! listener.statusChangedCalled;
235 
236         tRes.tested("removeStatusListener()", result) ;
237     }
238 }
239 
240