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