Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / ifc / script / _XEventAttacherManager.java
blob4bd3a37c4f21ee54e6a38fa9f400a4d0b3a3cdee
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XEventAttacherManager.java,v $
10 * $Revision: 1.5 $
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 ************************************************************************/
31 package ifc.script;
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;
42 /**
43 * Testing <code>com.sun.star.script.XEventAttacherManager</code>
44 * interface methods :
45 * <ul>
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>
57 * </ul> <p>
58 * @see com.sun.star.script.XEventAttacherManager
60 public class _XEventAttacherManager extends MultiMethodTest {
62 /**
63 * oObj filled by MultiMethodTest
65 public XEventAttacherManager oObj = null;
67 int index;
69 /**
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() {
75 index = 0;
76 try {
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 "
82 + e.getMessage());
83 e.printStackTrace(log);
84 tRes.tested("insertEntry()", false);
88 ScriptEventDescriptor desc;
90 /**
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 :
96 * <ul>
97 * <li> <code> insertEntry() </code> : to have entry's index</li>
98 * </ul>
99 * @see com.sun.star.script.ScriptEventDescriptor
101 public void _registerScriptEvent() {
102 requiredMethod("insertEntry()");
103 desc = new ScriptEventDescriptor(
104 "XEventListener1",
105 "disposing", "", "Basic", "");
107 try {
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 "
112 + e.getMessage());
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 :
126 * <ul>
127 * <li> <code> insertEntry() </code> : to have entry's index</li>
128 * </ul>
129 * @see com.sun.star.script.ScriptEventDescriptor
131 public void _registerScriptEvents() {
132 requiredMethod("insertEntry()");
133 descs = new ScriptEventDescriptor[] {
134 new ScriptEventDescriptor(
135 "XEventListener2",
136 "disposing", "", "Basic", ""),
137 new ScriptEventDescriptor(
138 "XEventListener3",
139 "disposing", "", "Basic", "")
142 try {
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 "
147 + e.getMessage());
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 :
159 * <ul>
160 * <li> <code> registerScriptEvent() </code> :
161 * to have registered descriptor </li>
162 * <li> <code> registerScriptEvents() </code> :
163 * to have registered descriptors </li>
164 * </ul>
166 public void _getScriptEvents() {
167 requiredMethod("registerScriptEvent()");
168 requiredMethod("registerScriptEvents()");
170 ScriptEventDescriptor[] res;
172 try {
173 res = oObj.getScriptEvents(index);
174 } catch (com.sun.star.lang.IllegalArgumentException e) {
175 log.println("registerScriptEvents() throws unexpected exception "
176 + e.getMessage());
177 e.printStackTrace(log);
178 tRes.tested("registerScriptEvents()", false);
179 return;
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 :") ;
187 printEvents(res) ;
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)) {
200 return true;
204 return false;
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])) {
220 return false;
224 return true;
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) {
247 if (evt == null) {
248 log.println("null");
249 } else {
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) {
264 log.println("null");
265 } else {
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 :
281 * <ul>
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()");
288 try {
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);
296 return;
299 try {
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 "
304 + e.getMessage());
305 e.printStackTrace(log);
306 tRes.tested("attach()", false);
307 } catch (com.sun.star.lang.ServiceNotRegisteredException e) {
308 log.println("attach() throws exception "
309 + e.getMessage());
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 :
321 * <ul>
322 * <li> <code> attach() </code> : to have attached object </li>
323 * </ul>
325 public void _detach() {
326 requiredMethod("attach()");
328 try {
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 "
333 + e.getMessage());
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
342 * was removed. <p>
343 * Has <b> OK </b> status if description was successfully removed. <p>
344 * The following method tests are to be completed successfully before :
345 * <ul>
346 * <li> <code> registerScriptEvent() </code> :
347 * to have registered descriptor </li>
348 * </ul>
349 * The following method tests are to be executed before :
350 * <ul>
351 * <li> <code> getScriptEvents() </code> :
352 * this method must be executed first </li>
353 * </ul>
355 public void _revokeScriptEvent() {
356 requiredMethod("registerScriptEvent()");
357 executeMethod("getScriptEvents()") ;
359 try {
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));
366 printEvents(res) ;
367 } catch (com.sun.star.lang.IllegalArgumentException e) {
368 log.println("revokeScriptEvent() throws unexpected exception "
369 + e.getMessage());
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
378 * were removed. <p>
379 * Has <b> OK </b> status if descriptions were successfully removed. <p>
380 * The following method tests are to be completed successfully before :
381 * <ul>
382 * <li> <code> revokeScriptEvent() </code> :
383 * this method must be executed first </li>
384 * </ul>
385 * The following method tests are to be executed before :
386 * <ul>
387 * <li> <code> getScriptEvents() </code> :
388 * this method must be executed first </li>
389 * </ul>
391 public void _revokeScriptEvents() {
392 requiredMethod("revokeScriptEvent()");
393 executeMethod("getScriptEvents()") ;
395 try {
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 "
404 + e.getMessage());
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 :
416 * <ul>
417 * <li> <code> insertEntry() </code> :
418 * to have entry's index </li>
420 public void _removeEntry() {
421 requiredMethod("insertEntry()");
422 try {
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 "
428 + e.getMessage());
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();
446 try {
447 oObj.addScriptListener(listener);
448 tRes.tested("addScriptListener()", true);
449 } catch (com.sun.star.lang.IllegalArgumentException e) {
450 log.println("addScriptListener() throws unexpected exception "
451 + e.getMessage());
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 :
463 * <ul>
464 * <li> <code> addScriptListener() </code> :
465 * to have script listener </li>
466 * </ul>
468 public void _removeScriptListener() {
469 requiredMethod("addScriptListener()");
471 try {
472 oObj.removeScriptListener(listener);
473 tRes.tested("removeScriptListener()", true);
474 } catch (com.sun.star.lang.IllegalArgumentException e) {
475 log.println("removeScriptListener() throws unexpected exception "
476 + e.getMessage());
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) {
493 return evt.Helper;
496 public void disposing(EventObject evt) {