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