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