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: _XEventAttacherManager.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 ************************************************************************/
33 import lib
.MultiMethodTest
;
35 import com
.sun
.star
.lang
.EventObject
;
36 import com
.sun
.star
.lang
.XMultiServiceFactory
;
37 import com
.sun
.star
.script
.ScriptEvent
;
38 import com
.sun
.star
.script
.ScriptEventDescriptor
;
39 import com
.sun
.star
.script
.XEventAttacherManager
;
40 import com
.sun
.star
.script
.XScriptListener
;
43 * Testing <code>com.sun.star.script.XEventAttacherManager</code>
46 * <li><code> registerScriptEvent()</code></li>
47 * <li><code> registerScriptEvents()</code></li>
48 * <li><code> revokeScriptEvent()</code></li>
49 * <li><code> revokeScriptEvents()</code></li>
50 * <li><code> insertEntry()</code></li>
51 * <li><code> removeEntry()</code></li>
52 * <li><code> getScriptEvents()</code></li>
53 * <li><code> attach()</code></li>
54 * <li><code> detach()</code></li>
55 * <li><code> addScriptListener()</code></li>
56 * <li><code> removeScriptListener()</code></li>
58 * @see com.sun.star.script.XEventAttacherManager
60 public class _XEventAttacherManager
extends MultiMethodTest
{
63 * oObj filled by MultiMethodTest
65 public XEventAttacherManager oObj
= null;
70 * Test calls the method and stores index of new entry. <p>
71 * Has <b> OK </b> status if the method successfully returns
72 * and no exceptions were thrown. <p>
74 public void _insertEntry() {
77 oObj
.insertEntry(index
);
78 tRes
.tested("insertEntry()", true);
79 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
80 log
.println("insertEntry(" + index
81 + ") throws unexpected exception "
83 e
.printStackTrace(log
);
84 tRes
.tested("insertEntry()", false);
88 ScriptEventDescriptor desc
;
91 * Test creates <code>ScriptEventDescriptor</code>, registers
92 * the script event and stores the descriptor. <p>
93 * Has <b> OK </b> status if the method successfully returns
94 * and no exceptions were thrown. <p>
95 * The following method tests are to be completed successfully before :
97 * <li> <code> insertEntry() </code> : to have entry's index</li>
99 * @see com.sun.star.script.ScriptEventDescriptor
101 public void _registerScriptEvent() {
102 requiredMethod("insertEntry()");
103 desc
= new ScriptEventDescriptor(
105 "disposing", "", "Basic", "");
108 oObj
.registerScriptEvent(index
, desc
);
109 tRes
.tested("registerScriptEvent()", true);
110 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
111 log
.println("registerScriptEvent() throws unexpected exception "
113 e
.printStackTrace(log
);
114 tRes
.tested("registerScriptEvent()", false);
118 ScriptEventDescriptor descs
[];
121 * Test creates array of <code>ScriptEventDescriptor</code>, registers
122 * this script events and stores the descriptors. <p>
123 * Has <b> OK </b> status if the method successfully returns
124 * and no exceptions were thrown. <p>
125 * The following method tests are to be completed successfully before :
127 * <li> <code> insertEntry() </code> : to have entry's index</li>
129 * @see com.sun.star.script.ScriptEventDescriptor
131 public void _registerScriptEvents() {
132 requiredMethod("insertEntry()");
133 descs
= new ScriptEventDescriptor
[] {
134 new ScriptEventDescriptor(
136 "disposing", "", "Basic", ""),
137 new ScriptEventDescriptor(
139 "disposing", "", "Basic", "")
143 oObj
.registerScriptEvents(index
, descs
);
144 tRes
.tested("registerScriptEvents()", true);
145 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
146 log
.println("registerScriptEvents() throws unexpected exception "
148 e
.printStackTrace(log
);
149 tRes
.tested("registerScriptEvents()", false);
154 * Test calls the method and checks returned value. <p>
155 * Has <b> OK </b> status if returned array of descriptors contains
156 * array of descriptors registered by methods <code>registerScriptEvents</code>
157 * and <code>registerScriptEvent</code> and no exceptions were thrown. <p>
158 * The following method tests are to be completed successfully before :
160 * <li> <code> registerScriptEvent() </code> :
161 * to have registered descriptor </li>
162 * <li> <code> registerScriptEvents() </code> :
163 * to have registered descriptors </li>
166 public void _getScriptEvents() {
167 requiredMethod("registerScriptEvent()");
168 requiredMethod("registerScriptEvents()");
170 ScriptEventDescriptor
[] res
;
173 res
= oObj
.getScriptEvents(index
);
174 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
175 log
.println("registerScriptEvents() throws unexpected exception "
177 e
.printStackTrace(log
);
178 tRes
.tested("registerScriptEvents()", false);
182 // checking the desc and descs are in script events
183 tRes
.tested("getScriptEvents()",
184 contains(res
, desc
) && containsArray(res
, descs
));
186 log
.println("Script events :") ;
191 * Method checks that array of descriptors contains the concrete desciptor.
192 * @param container the array of descriptors
193 * @param evt the descriptor which presence in the array is checked
194 * @return true if the descriptor presence in the array
196 boolean contains(ScriptEventDescriptor
[] container
,
197 ScriptEventDescriptor evt
) {
198 for (int i
= 0; i
< container
.length
; i
++) {
199 if (equal(container
[i
], evt
)) {
208 * Method checks that one array of descriptors contains
209 * another array of descriptors.
210 * @param container the array of descriptors
211 * @param events the array of descriptors which presence
212 * in array <code>container</code> is checked
213 * @return true if the array <code>events</code> contains in the array
214 * <code>container</code>
216 boolean containsArray(ScriptEventDescriptor
[] container
,
217 ScriptEventDescriptor
[] events
) {
218 for (int i
= 0; i
< events
.length
; i
++) {
219 if (!contains(container
, events
[i
])) {
228 * Compares descriptor <code>evt1</code> to descriptor <code>evt2</code>.
229 * Two descriptors are considered equal if all their fields are equal.
230 * @return true if the argument is not <code>null</code> and
231 * the descriptors are equal; false otherwise
233 boolean equal(ScriptEventDescriptor evt1
,
234 ScriptEventDescriptor evt2
) {
235 return evt1
.ListenerType
.equals(evt2
.ListenerType
)
236 && evt1
.EventMethod
.equals(evt2
.EventMethod
)
237 && evt1
.ScriptType
.equals(evt2
.ScriptType
)
238 && evt1
.ScriptCode
.equals(evt2
.ScriptCode
)
239 && evt1
.AddListenerParam
.equals(evt2
.AddListenerParam
);
243 * Prints fields of descriptor <code>evt</code> to log.
244 * @param evt the descriptor that needs to be printed to log
246 void printEvent(ScriptEventDescriptor evt
) {
250 log
.println("\"" + evt
.ListenerType
+ "\",\""
251 + evt
.EventMethod
+ "\",\""
252 + evt
.ScriptType
+ "\",\""
253 + evt
.ScriptCode
+ "\",\""
254 + evt
.AddListenerParam
+ "\"");
259 * Prints the descriptors to log.
260 * @param events the array of descriptors that need to be printed to log
262 void printEvents(ScriptEventDescriptor events
[]) {
263 if (events
== null) {
266 for (int i
= 0; i
< events
.length
; i
++) {
267 printEvent(events
[i
]);
272 Object attachedObject
;
275 * Test creates instance of <code>TypeDescriptionProvider</code>,
276 * stores it and attaches it to the entry with index stored in the method
277 * <code>insertEntry()</code>. <p>
278 * Has <b> OK </b> status if the method successfully returns
279 * and no exceptions were thrown. <p>
280 * The following method tests are to be completed successfully before :
282 * <li> <code> insertEntry() </code> : to have entry's index for attach</li>
283 * @see com.sun.star.reflection.TypeDescriptionProvider
285 public void _attach() {
286 requiredMethod("insertEntry()");
289 XMultiServiceFactory xMSF
= (XMultiServiceFactory
)tParam
.getMSF();
290 attachedObject
= xMSF
.createInstance
291 ( "com.sun.star.reflection.TypeDescriptionProvider" );
292 } catch( com
.sun
.star
.uno
.Exception e
) {
293 log
.println("Service not available" );
294 e
.printStackTrace(log
);
295 tRes
.tested("attach()", false);
300 oObj
.attach(index
, attachedObject
, "param");
301 tRes
.tested("attach()", true);
302 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
303 log
.println("attach() throws exception "
305 e
.printStackTrace(log
);
306 tRes
.tested("attach()", false);
307 } catch (com
.sun
.star
.lang
.ServiceNotRegisteredException e
) {
308 log
.println("attach() throws exception "
310 e
.printStackTrace(log
);
311 tRes
.tested("attach()", false);
316 * Test calls the method for the object that was stored in the method
317 * <code>attach()</code>. <p>
318 * Has <b> OK </b> status if the method successfully returns
319 * and no exceptions were thrown. <p>
320 * The following method tests are to be completed successfully before :
322 * <li> <code> attach() </code> : to have attached object </li>
325 public void _detach() {
326 requiredMethod("attach()");
329 oObj
.detach(index
, attachedObject
);
330 tRes
.tested("detach()", true);
331 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
332 log
.println("detach() throws unexpected exception "
334 e
.printStackTrace(log
);
335 tRes
.tested("detach()", false);
340 * Test revokes script event that was registered by method
341 * <code>registerScriptEvent()</code> and checks that the description
343 * Has <b> OK </b> status if description was successfully removed. <p>
344 * The following method tests are to be completed successfully before :
346 * <li> <code> registerScriptEvent() </code> :
347 * to have registered descriptor </li>
349 * The following method tests are to be executed before :
351 * <li> <code> getScriptEvents() </code> :
352 * this method must be executed first </li>
355 public void _revokeScriptEvent() {
356 requiredMethod("registerScriptEvent()");
357 executeMethod("getScriptEvents()") ;
360 oObj
.revokeScriptEvent(index
, desc
.ListenerType
,
361 desc
.EventMethod
, "");
363 ScriptEventDescriptor
[] res
= oObj
.getScriptEvents(index
);
364 // checking that the desc has been removed
365 tRes
.tested("revokeScriptEvent()", !contains(res
, desc
));
367 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
368 log
.println("revokeScriptEvent() throws unexpected exception "
370 e
.printStackTrace(log
);
371 tRes
.tested("revokeScriptEvent()", false);
376 * Test revokes script events that was registered by method
377 * <code>registerScriptEvents()</code> and checks that the descriptions
379 * Has <b> OK </b> status if descriptions were successfully removed. <p>
380 * The following method tests are to be completed successfully before :
382 * <li> <code> revokeScriptEvent() </code> :
383 * this method must be executed first </li>
385 * The following method tests are to be executed before :
387 * <li> <code> getScriptEvents() </code> :
388 * this method must be executed first </li>
391 public void _revokeScriptEvents() {
392 requiredMethod("revokeScriptEvent()");
393 executeMethod("getScriptEvents()") ;
396 oObj
.revokeScriptEvents(index
);
398 ScriptEventDescriptor
[] res
= oObj
.getScriptEvents(index
);
399 // checking that all events have been removed
400 tRes
.tested("revokeScriptEvents()",
401 res
== null || res
.length
== 0);
402 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
403 log
.println("revokeScriptEvents() throws unexpected exception "
405 e
.printStackTrace(log
);
406 tRes
.tested("revokeScriptEvents()", false);
411 * Test calls the method with entry's index that was stored in method
412 * <code>insertEntry()</code>. <p>
413 * Has <b> OK </b> status if the method successfully returns
414 * and no exceptions were thrown. <p>
415 * The following method tests are to be completed successfully before :
417 * <li> <code> insertEntry() </code> :
418 * to have entry's index </li>
420 public void _removeEntry() {
421 requiredMethod("insertEntry()");
423 oObj
.removeEntry(index
);
424 tRes
.tested("removeEntry()", true);
425 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
426 log
.println("removeEntry(" + index
427 + ") throws unexpected exception "
429 e
.printStackTrace(log
);
430 tRes
.tested("removeEntry()", false);
434 XScriptListener listener
;
437 * Test creates object that supports interface <code>XScriptListener</code>,
438 * stores it and addes this scripts listener. <p>
439 * Has <b> OK </b> status if the method successfully returns
440 * and no exceptions were thrown. <p>
441 * @see com.sun.star.script.XScriptListener
443 public void _addScriptListener() {
444 listener
= new MyScriptListener();
447 oObj
.addScriptListener(listener
);
448 tRes
.tested("addScriptListener()", true);
449 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
450 log
.println("addScriptListener() throws unexpected exception "
452 e
.printStackTrace(log
);
453 tRes
.tested("addScriptListener()", false);
458 * Test removes script listener that was stored in method
459 * <code>addScriptListener()</code>. <p>
460 * Has <b> OK </b> status if the method successfully returns
461 * and no exceptions were thrown. <p>
462 * The following method tests are to be completed successfully before :
464 * <li> <code> addScriptListener() </code> :
465 * to have script listener </li>
468 public void _removeScriptListener() {
469 requiredMethod("addScriptListener()");
472 oObj
.removeScriptListener(listener
);
473 tRes
.tested("removeScriptListener()", true);
474 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
475 log
.println("removeScriptListener() throws unexpected exception "
477 e
.printStackTrace(log
);
478 tRes
.tested("removeScriptListener()", false);
483 * Class implement interface <code>XScriptListener</code>
484 * for test of the method <code>addScriptListener()</code>.
485 * No functionality implemented.
486 * @see com.sun.star.script.XScriptListener
488 class MyScriptListener
implements XScriptListener
{
489 public void firing(ScriptEvent evt
) {
492 public Object
approveFiring(ScriptEvent evt
) {
496 public void disposing(EventObject evt
) {