Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / frame / _XDispatch.java
blob3cc536f7a331c96b9f9251cf03da23e1c3ab2d7a
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc.frame;
22 import com.sun.star.beans.PropertyValue;
23 import com.sun.star.frame.XDispatch;
24 import com.sun.star.util.URL;
25 import lib.MultiMethodTest;
26 import lib.Status;
27 import lib.StatusException;
28 import com.sun.star.frame.XNotifyingDispatch;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.frame.DispatchResultEvent;
32 /**
33 * Testing <code>com.sun.star.frame.XDispatch</code>
34 * interface methods :
35 * <ul>
36 * <li><code> dispatch()</code></li>
37 * <li><code> addStatusListener()</code></li>
38 * <li><code> removeStatusListener()</code></li>
39 * </ul> <p>
40 * This test needs the following object relations :
41 * <ul>
42 * <li> <code>'XDispatch.URL'</code> (of type <code>com.sun.star.util.URL
43 * </code>): URL for passing to <code>dispatch()</code> method. </li>
44 * <ul> <p>
45 * @see com.sun.star.frame.XDispatch
46 * @see com.sun.star.frame.XNotifyingDispatch
47 * @see ifc.frame._XDispatch
48 * @see ifc.frame._XNotifyingDispatch
51 public class _XDispatch extends MultiMethodTest {
53 public XDispatch oObj = null;
55 /**
56 * Listener implementation which sets flags on appropriate method calls
58 protected static class TestStatusListener implements
59 com.sun.star.frame.XStatusListener {
60 public boolean statusChangedCalled = false ;
61 private final java.io.PrintWriter log;
63 public TestStatusListener(java.io.PrintWriter log) {
64 this.log = log ;
67 public void disposing(com.sun.star.lang.EventObject e) {
68 log.println(" disposing was called.") ;
71 public void statusChanged(com.sun.star.frame.FeatureStateEvent e) {
72 statusChangedCalled = true ;
73 log.println(" statusChanged was called.") ;
74 log.println(" FeatureURL = '" + e.FeatureURL + "'");
75 log.println(" FeatureDescriptor = '" + e.FeatureDescriptor + "'");
76 log.println(" IsEnabled = " + e.IsEnabled);
77 log.println(" Requery = " + e.Requery);
78 log.println(" State = '" + e.State.toString() + "'");
83 /**
84 * Listener implementation which sets flags on appropriate method calls
86 protected static class TestNotificationListener implements
87 com.sun.star.frame.XDispatchResultListener {
88 public boolean finishedDispatch = false ;
89 private final java.io.PrintWriter log;
91 public TestNotificationListener(java.io.PrintWriter log) {
92 this.log = log ;
95 public void disposing(com.sun.star.lang.EventObject e) {
96 log.println(" disposing was called.") ;
99 public void dispatchFinished( DispatchResultEvent e) {
100 finishedDispatch = true ;
101 log.println(" dispatchFinished was called.") ;
106 TestStatusListener listener = null ;
107 TestNotificationListener notificationListener = null;
108 URL url = null ;
111 * Not all implementations could call the
112 * <code>com.sun.star.frame.XStatusListener</code>. For this purposes the
113 * <code>com.sun.star.frame.XDispatchWithNotification</code> was designed.
114 * If <code>com.sun.star.frame.XStatusListener</code> was not called and
115 * <code>com.sun.star.frame.XStatusListener</code> is present, it was used
116 * to check listeners.
118 private boolean checkXDispatchWithNotification()
120 XNotifyingDispatch xND = UnoRuntime.queryInterface(XNotifyingDispatch.class, oObj);
121 if ( xND != null) {
122 log.println(" XNotifyingDispatch found:");
123 PropertyValue[] arguments = (PropertyValue[])
124 tEnv.getObjRelation("XNotifyingDispatchArgument");
126 notificationListener = new TestNotificationListener(log) ;
127 xND.dispatchWithNotification(url, arguments, notificationListener);
129 waitForEventIdle();
131 log.println(" Listener called: "+ notificationListener.finishedDispatch);
133 return notificationListener.finishedDispatch;
134 } else {
135 return false;
140 * Retrieves object relations and creates new listeners.
141 * @throws StatusException If one of relations not found.
143 @Override
144 public void before() {
145 listener = new TestStatusListener(log) ;
146 url = (URL) tEnv.getObjRelation("XDispatch.URL") ;
148 if (url == null) throw new StatusException
149 (Status.failed("Relation not found.")) ;
153 * Calls the method using URL from relation. <p>
154 * Has <b> OK </b> status if one listener (not removed) is called, and
155 * another (removed) is not.
156 * The following method tests are to be completed successfully before :
157 * <ul>
158 * <li> <code>addStatusListener</code> :
159 * to check that the listener is called
160 * </li>
161 * </ul>
163 public void _dispatch() {
164 requiredMethod("addStatusListener()") ;
166 boolean result = true ;
168 oObj.dispatch(url, new PropertyValue[0]) ;
170 waitForEventIdle();
172 log.println("Listener called: "+ listener.statusChangedCalled);
174 result = listener.statusChangedCalled;
176 if (!result) {
177 result = checkXDispatchWithNotification();
180 tRes.tested("dispatch()", result) ;
184 * Adds two listeners. <p>
185 * Has <b> OK </b> status if no runtime exceptions occurred.
187 public void _addStatusListener() {
189 boolean result = true ;
190 oObj.addStatusListener(listener, url) ;
192 tRes.tested("addStatusListener()", result) ;
196 * Removes the listener added before. <p>
197 * Has <b> OK </b> status if the dispatch call doesn't call the listener.
198 * The following method tests are to be completed successfully before :
199 * <ul>
200 * <li> <code> dispatch() </code> : to have a listener to remove
201 * </li>
202 * </ul>
204 public void _removeStatusListener() {
205 requiredMethod("dispatch()") ;
206 listener.statusChangedCalled = false;
207 boolean result = true ;
208 oObj.removeStatusListener(listener, url) ;
210 oObj.dispatch(url, new PropertyValue[0]) ;
212 waitForEventIdle();
214 System.out.println("Listener called: "+ listener.statusChangedCalled);
216 result = ! listener.statusChangedCalled;
218 tRes.tested("removeStatusListener()", result) ;