1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XDispatch.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
34 import com
.sun
.star
.beans
.PropertyValue
;
35 import com
.sun
.star
.frame
.XDispatch
;
36 import com
.sun
.star
.util
.URL
;
37 import lib
.MultiMethodTest
;
39 import lib
.StatusException
;
40 import com
.sun
.star
.frame
.XNotifyingDispatch
;
41 import com
.sun
.star
.uno
.UnoRuntime
;
42 import com
.sun
.star
.frame
.DispatchResultEvent
;
45 * Testing <code>com.sun.star.frame.XDispatch</code>
48 * <li><code> dispatch()</code></li>
49 * <li><code> addStatusListener()</code></li>
50 * <li><code> removeStatusListener()</code></li>
52 * This test needs the following object relations :
54 * <li> <code>'XDispatch.URL'</code> (of type <code>com.sun.star.util.URL
55 * </code>): URL for passing to <code>dispatch()</code> method. </li>
57 * @see com.sun.star.frame.XDispatch
58 * @see com.sun.star.frame.XNotifyingDispatch
59 * @see ifc.frame._XDispatch
60 * @see ifc.frame._XNotifyingDispatch
63 public class _XDispatch
extends MultiMethodTest
{
65 public XDispatch oObj
= null;
68 * Listener implementation which sets flags on appropriate method calls
70 protected class TestStatusListener
implements
71 com
.sun
.star
.frame
.XStatusListener
{
72 public boolean disposingCalled
= false ;
73 public boolean statusChangedCalled
= false ;
74 private java
.io
.PrintWriter log
= null ;
76 public TestStatusListener(java
.io
.PrintWriter log
) {
80 public void disposing(com
.sun
.star
.lang
.EventObject e
) {
81 disposingCalled
= true ;
82 log
.println(" disposing was called.") ;
85 public void statusChanged(com
.sun
.star
.frame
.FeatureStateEvent e
) {
86 statusChangedCalled
= true ;
87 log
.println(" statusChanged was called.") ;
88 log
.println(" FeatureURL = '" + e
.FeatureURL
+ "'");
89 log
.println(" FeatureDescriptor = '" + e
.FeatureDescriptor
+ "'");
90 log
.println(" IsEnabled = " + e
.IsEnabled
);
91 log
.println(" Requery = " + e
.Requery
);
92 log
.println(" State = '" + e
.State
.toString() + "'");
98 * Listener implementation which sets flags on appropriate method calls
100 protected class TestNotificationListener
implements
101 com
.sun
.star
.frame
.XDispatchResultListener
{
102 public boolean disposingCalled
= false ;
103 public boolean finishedDispatch
= false ;
104 private java
.io
.PrintWriter log
= null ;
106 public TestNotificationListener(java
.io
.PrintWriter log
) {
110 public void disposing(com
.sun
.star
.lang
.EventObject e
) {
111 disposingCalled
= true ;
112 log
.println(" disposing was called.") ;
115 public void dispatchFinished( DispatchResultEvent e
) {
116 finishedDispatch
= true ;
117 log
.println(" dispatchFinished was called.") ;
122 TestStatusListener listener
= null ;
123 TestNotificationListener notificationListener
= null;
127 * Not all implementations could call the
128 * <code>com.sun.star.frame.XStatusListener</code>. For this purposes the
129 * <code>com.sun.star.frame.XDispatchWithNotification</code> was designed.
130 * If <code>com.sun.star.frame.XStatusListener</code> was not called and
131 * <code>com.sun.star.frame.XStatusListener</code> is present, it was used
132 * to check listeners.
134 private boolean checkXDispatchWithNotification()
136 XNotifyingDispatch xND
= (XNotifyingDispatch
)
137 UnoRuntime
.queryInterface(XNotifyingDispatch
.class, oObj
);
139 log
.println(" XNotifyingDispatch found:");
140 PropertyValue
[] arguments
= (PropertyValue
[])
141 tEnv
.getObjRelation("XNotifyingDispatchArgument");
143 notificationListener
= new TestNotificationListener(log
) ;
144 xND
.dispatchWithNotification(url
, arguments
, notificationListener
);
149 catch(java
.lang
.InterruptedException e
) {}
151 log
.println(" Listener called: "+ notificationListener
.finishedDispatch
);
153 return notificationListener
.finishedDispatch
;
160 * Retrieves object relations and creates new listeners.
161 * @throws StatusException If one of relations not found.
163 public void before() {
164 listener
= new TestStatusListener(log
) ;
165 url
= (URL
) tEnv
.getObjRelation("XDispatch.URL") ;
167 if (url
== null) throw new StatusException
168 (Status
.failed("Relation not found.")) ;
172 * Calls the method using URL from relation. <p>
173 * Has <b> OK </b> status if one listener (not removed) is called, and
174 * another (removed) is not.
175 * The following method tests are to be completed successfully before :
177 * <li> <code>addStatusListener</code> :
178 * to check that the listener is called
182 public void _dispatch() {
183 requiredMethod("addStatusListener()") ;
185 boolean result
= true ;
187 oObj
.dispatch(url
, new PropertyValue
[0]) ;
192 catch(java
.lang
.InterruptedException e
) {}
194 log
.println("Listener called: "+ listener
.statusChangedCalled
);
196 result
= listener
.statusChangedCalled
;
198 if (result
== false) {
199 result
= checkXDispatchWithNotification();
202 tRes
.tested("dispatch()", result
) ;
206 * Adds two listeners. <p>
207 * Has <b> OK </b> status if no runtime exceptions occured.
209 public void _addStatusListener() {
211 boolean result
= true ;
212 oObj
.addStatusListener(listener
, url
) ;
214 tRes
.tested("addStatusListener()", result
) ;
218 * Removes the listener added before. <p>
219 * Has <b> OK </b> status if the dispatch call doesn't call the listener.
220 * The following method tests are to be completed successfully before :
222 * <li> <code> dispatch() </code> : to have a listener to remove
226 public void _removeStatusListener() {
227 requiredMethod("dispatch()") ;
228 listener
.statusChangedCalled
= false;
229 boolean result
= true ;
230 oObj
.removeStatusListener(listener
, url
) ;
232 oObj
.dispatch(url
, new PropertyValue
[0]) ;
237 catch(java
.lang
.InterruptedException e
) {}
239 System
.out
.println("Listener called: "+ listener
.statusChangedCalled
);
241 result
= ! listener
.statusChangedCalled
;
243 tRes
.tested("removeStatusListener()", result
) ;