xref: /AOO41X/main/qadevOOo/tests/java/ifc/script/_XEventAttacherManager.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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.script;
29 
30 import lib.MultiMethodTest;
31 
32 import com.sun.star.lang.EventObject;
33 import com.sun.star.lang.XMultiServiceFactory;
34 import com.sun.star.script.ScriptEvent;
35 import com.sun.star.script.ScriptEventDescriptor;
36 import com.sun.star.script.XEventAttacherManager;
37 import com.sun.star.script.XScriptListener;
38 
39 /**
40 * Testing <code>com.sun.star.script.XEventAttacherManager</code>
41 * interface methods :
42 * <ul>
43 *  <li><code> registerScriptEvent()</code></li>
44 *  <li><code> registerScriptEvents()</code></li>
45 *  <li><code> revokeScriptEvent()</code></li>
46 *  <li><code> revokeScriptEvents()</code></li>
47 *  <li><code> insertEntry()</code></li>
48 *  <li><code> removeEntry()</code></li>
49 *  <li><code> getScriptEvents()</code></li>
50 *  <li><code> attach()</code></li>
51 *  <li><code> detach()</code></li>
52 *  <li><code> addScriptListener()</code></li>
53 *  <li><code> removeScriptListener()</code></li>
54 * </ul> <p>
55 * @see com.sun.star.script.XEventAttacherManager
56 */
57 public class _XEventAttacherManager extends MultiMethodTest {
58 
59     /**
60      * oObj filled by MultiMethodTest
61      */
62     public XEventAttacherManager oObj = null;
63 
64     int index;
65 
66     /**
67     * Test calls the method and stores index of new entry. <p>
68     * Has <b> OK </b> status if the method successfully returns
69     * and no exceptions were thrown. <p>
70     */
71     public void _insertEntry() {
72         index = 0;
73         try {
74             oObj.insertEntry(index);
75             tRes.tested("insertEntry()", true);
76         } catch (com.sun.star.lang.IllegalArgumentException e) {
77             log.println("insertEntry(" + index
78                     + ") throws unexpected exception "
79                     + e.getMessage());
80             e.printStackTrace(log);
81             tRes.tested("insertEntry()", false);
82         }
83     }
84 
85     ScriptEventDescriptor desc;
86 
87     /**
88     * Test creates <code>ScriptEventDescriptor</code>, registers
89     * the script event and stores the descriptor. <p>
90     * Has <b> OK </b> status if the method successfully returns
91     * and no exceptions were thrown. <p>
92     * The following method tests are to be completed successfully before :
93     * <ul>
94     *  <li> <code> insertEntry() </code> : to have entry's index</li>
95     * </ul>
96     * @see com.sun.star.script.ScriptEventDescriptor
97     */
98     public void _registerScriptEvent() {
99         requiredMethod("insertEntry()");
100         desc = new ScriptEventDescriptor(
101                     "XEventListener1",
102                     "disposing", "", "Basic", "");
103 
104         try {
105             oObj.registerScriptEvent(index, desc);
106             tRes.tested("registerScriptEvent()", true);
107         } catch (com.sun.star.lang.IllegalArgumentException e) {
108             log.println("registerScriptEvent() throws unexpected exception "
109                     + e.getMessage());
110             e.printStackTrace(log);
111             tRes.tested("registerScriptEvent()", false);
112         }
113     }
114 
115     ScriptEventDescriptor descs[];
116 
117     /**
118     * Test creates array of <code>ScriptEventDescriptor</code>, registers
119     * this script events and stores the descriptors. <p>
120     * Has <b> OK </b> status if the method successfully returns
121     * and no exceptions were thrown. <p>
122     * The following method tests are to be completed successfully before :
123     * <ul>
124     *  <li> <code> insertEntry() </code> : to have entry's index</li>
125     * </ul>
126     * @see com.sun.star.script.ScriptEventDescriptor
127     */
128     public void _registerScriptEvents() {
129         requiredMethod("insertEntry()");
130         descs = new ScriptEventDescriptor[] {
131             new ScriptEventDescriptor(
132                     "XEventListener2",
133                     "disposing", "", "Basic", ""),
134             new ScriptEventDescriptor(
135                     "XEventListener3",
136                     "disposing", "", "Basic", "")
137         };
138 
139         try {
140             oObj.registerScriptEvents(index, descs);
141             tRes.tested("registerScriptEvents()", true);
142         } catch (com.sun.star.lang.IllegalArgumentException e) {
143             log.println("registerScriptEvents() throws unexpected exception "
144                     + e.getMessage());
145             e.printStackTrace(log);
146             tRes.tested("registerScriptEvents()", false);
147         }
148     }
149 
150     /**
151     * Test calls the method and checks returned value. <p>
152     * Has <b> OK </b> status if returned array of descriptors contains
153     * array of descriptors registered by methods <code>registerScriptEvents</code>
154     * and <code>registerScriptEvent</code> and no exceptions were thrown. <p>
155     * The following method tests are to be completed successfully before :
156     * <ul>
157     *  <li> <code> registerScriptEvent() </code> :
158     *   to have registered descriptor </li>
159     *  <li> <code> registerScriptEvents() </code> :
160     *   to have registered descriptors </li>
161     * </ul>
162     */
163     public void _getScriptEvents() {
164         requiredMethod("registerScriptEvent()");
165         requiredMethod("registerScriptEvents()");
166 
167         ScriptEventDescriptor[] res;
168 
169         try {
170             res = oObj.getScriptEvents(index);
171         } catch (com.sun.star.lang.IllegalArgumentException e) {
172             log.println("registerScriptEvents() throws unexpected exception "
173                     + e.getMessage());
174             e.printStackTrace(log);
175             tRes.tested("registerScriptEvents()", false);
176             return;
177         }
178 
179         // checking the desc and descs are in script events
180         tRes.tested("getScriptEvents()",
181                 contains(res, desc) && containsArray(res, descs));
182 
183         log.println("Script events :") ;
184         printEvents(res) ;
185     }
186 
187     /**
188      * Method checks that array of descriptors contains the concrete desciptor.
189      * @param container the array of descriptors
190      * @param evt the descriptor which presence in the array is checked
191      * @return true if the descriptor presence in the array
192      */
193     boolean contains(ScriptEventDescriptor[] container,
194             ScriptEventDescriptor evt) {
195         for (int i = 0; i < container.length; i++) {
196             if (equal(container[i], evt)) {
197                 return true;
198             }
199         }
200 
201         return false;
202     }
203 
204     /**
205      * Method checks that one array of descriptors contains
206      * another array of descriptors.
207      * @param container the array of descriptors
208      * @param events the array of descriptors which presence
209      * in array <code>container</code> is checked
210      * @return true if the array <code>events</code> contains in the array
211      * <code>container</code>
212      */
213     boolean containsArray(ScriptEventDescriptor[] container,
214             ScriptEventDescriptor[] events) {
215         for (int i = 0; i < events.length; i++) {
216             if (!contains(container, events[i])) {
217                 return false;
218             }
219         }
220 
221         return true;
222     }
223 
224     /**
225      * Compares descriptor <code>evt1</code> to descriptor <code>evt2</code>.
226      * Two descriptors are considered equal if all their fields are equal.
227      * @return true if the argument is not <code>null</code> and
228      * the descriptors are equal; false otherwise
229      */
230     boolean equal(ScriptEventDescriptor evt1,
231             ScriptEventDescriptor evt2) {
232         return evt1.ListenerType.equals(evt2.ListenerType)
233             && evt1.EventMethod.equals(evt2.EventMethod)
234             && evt1.ScriptType.equals(evt2.ScriptType)
235             && evt1.ScriptCode.equals(evt2.ScriptCode)
236             && evt1.AddListenerParam.equals(evt2.AddListenerParam);
237     }
238 
239     /**
240      * Prints fields of descriptor <code>evt</code> to log.
241      * @param evt the descriptor that needs to be printed to log
242      */
243     void printEvent(ScriptEventDescriptor evt) {
244         if (evt == null) {
245             log.println("null");
246         } else {
247             log.println("\"" + evt.ListenerType + "\",\""
248                      + evt.EventMethod + "\",\""
249                      + evt.ScriptType + "\",\""
250                      + evt.ScriptCode + "\",\""
251                      + evt.AddListenerParam + "\"");
252         }
253     }
254 
255     /**
256      * Prints the descriptors to log.
257      * @param events the array of descriptors that need to be printed to log
258      */
259     void printEvents(ScriptEventDescriptor events[]) {
260         if (events == null) {
261             log.println("null");
262         } else {
263             for (int i = 0; i < events.length; i++) {
264                 printEvent(events[i]);
265             }
266         }
267     }
268 
269     Object attachedObject;
270 
271     /**
272     * Test creates instance of <code>TypeDescriptionProvider</code>,
273     * stores it and attaches it to the entry with index stored in the method
274     * <code>insertEntry()</code>. <p>
275     * Has <b> OK </b> status if the method successfully returns
276     * and no exceptions were thrown. <p>
277     * The following method tests are to be completed successfully before :
278     * <ul>
279     *  <li> <code> insertEntry() </code> : to have entry's index for attach</li>
280     * @see com.sun.star.reflection.TypeDescriptionProvider
281     */
282     public void _attach() {
283         requiredMethod("insertEntry()");
284 
285         try {
286             XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
287             attachedObject = xMSF.createInstance
288                 ( "com.sun.star.reflection.TypeDescriptionProvider" );
289         } catch( com.sun.star.uno.Exception e ) {
290             log.println("Service not available" );
291             e.printStackTrace(log);
292             tRes.tested("attach()", false);
293             return;
294         }
295 
296         try {
297             oObj.attach(index, attachedObject, "param");
298             tRes.tested("attach()", true);
299         } catch (com.sun.star.lang.IllegalArgumentException e) {
300             log.println("attach() throws exception "
301                     + e.getMessage());
302             e.printStackTrace(log);
303             tRes.tested("attach()", false);
304         } catch (com.sun.star.lang.ServiceNotRegisteredException e) {
305             log.println("attach() throws exception "
306                     + e.getMessage());
307             e.printStackTrace(log);
308             tRes.tested("attach()", false);
309         }
310     }
311 
312     /**
313     * Test calls the method for the object that was stored in the method
314     * <code>attach()</code>. <p>
315     * Has <b> OK </b> status if the method successfully returns
316     * and no exceptions were thrown. <p>
317     * The following method tests are to be completed successfully before :
318     * <ul>
319     *  <li> <code> attach() </code> : to have attached object </li>
320     * </ul>
321     */
322     public void _detach() {
323         requiredMethod("attach()");
324 
325         try {
326             oObj.detach(index, attachedObject);
327             tRes.tested("detach()", true);
328         } catch (com.sun.star.lang.IllegalArgumentException e) {
329             log.println("detach() throws unexpected exception "
330                     + e.getMessage());
331             e.printStackTrace(log);
332             tRes.tested("detach()", false);
333         }
334     }
335 
336     /**
337     * Test revokes script event that was registered by method
338     * <code>registerScriptEvent()</code> and checks that the description
339     * was removed. <p>
340     * Has <b> OK </b> status if description was successfully removed. <p>
341     * The following method tests are to be completed successfully before :
342     * <ul>
343     *  <li> <code> registerScriptEvent() </code> :
344     *   to have registered descriptor </li>
345     * </ul>
346     * The following method tests are to be executed before :
347     * <ul>
348     *  <li> <code> getScriptEvents() </code> :
349     *   this method must be executed first </li>
350     * </ul>
351     */
352     public void _revokeScriptEvent() {
353         requiredMethod("registerScriptEvent()");
354         executeMethod("getScriptEvents()") ;
355 
356         try {
357             oObj.revokeScriptEvent(index, desc.ListenerType,
358                     desc.EventMethod, "");
359 
360             ScriptEventDescriptor[] res = oObj.getScriptEvents(index);
361             // checking that the desc has been removed
362             tRes.tested("revokeScriptEvent()", !contains(res, desc));
363             printEvents(res) ;
364         } catch (com.sun.star.lang.IllegalArgumentException e) {
365             log.println("revokeScriptEvent() throws unexpected exception "
366                     + e.getMessage());
367             e.printStackTrace(log);
368             tRes.tested("revokeScriptEvent()", false);
369         }
370     }
371 
372     /**
373     * Test revokes script events that was registered by method
374     * <code>registerScriptEvents()</code> and checks that the descriptions
375     * were removed. <p>
376     * Has <b> OK </b> status if descriptions were successfully removed. <p>
377     * The following method tests are to be completed successfully before :
378     * <ul>
379     *  <li> <code> revokeScriptEvent() </code> :
380     *   this method must be executed first </li>
381     * </ul>
382     * The following method tests are to be executed before :
383     * <ul>
384     *  <li> <code> getScriptEvents() </code> :
385     *   this method must be executed first </li>
386     * </ul>
387     */
388     public void _revokeScriptEvents() {
389         requiredMethod("revokeScriptEvent()");
390         executeMethod("getScriptEvents()") ;
391 
392         try {
393             oObj.revokeScriptEvents(index);
394 
395             ScriptEventDescriptor[] res = oObj.getScriptEvents(index);
396             // checking that all events have been removed
397             tRes.tested("revokeScriptEvents()",
398                     res == null || res.length == 0);
399         } catch (com.sun.star.lang.IllegalArgumentException e) {
400             log.println("revokeScriptEvents() throws unexpected exception "
401                     + e.getMessage());
402             e.printStackTrace(log);
403             tRes.tested("revokeScriptEvents()", false);
404         }
405     }
406 
407     /**
408     * Test calls the method with entry's index that was stored in method
409     * <code>insertEntry()</code>. <p>
410     * Has <b> OK </b> status if the method successfully returns
411     * and no exceptions were thrown. <p>
412     * The following method tests are to be completed successfully before :
413     * <ul>
414     *  <li> <code> insertEntry() </code> :
415     *  to have entry's index </li>
416     */
417     public void _removeEntry() {
418         requiredMethod("insertEntry()");
419         try {
420             oObj.removeEntry(index);
421             tRes.tested("removeEntry()", true);
422         } catch (com.sun.star.lang.IllegalArgumentException e) {
423             log.println("removeEntry(" + index
424                     + ") throws unexpected exception "
425                     + e.getMessage());
426             e.printStackTrace(log);
427             tRes.tested("removeEntry()", false);
428         }
429     }
430 
431     XScriptListener listener;
432 
433     /**
434     * Test creates object that supports interface <code>XScriptListener</code>,
435     * stores it and addes this scripts listener. <p>
436     * Has <b> OK </b> status if the method successfully returns
437     * and no exceptions were thrown. <p>
438     * @see com.sun.star.script.XScriptListener
439     */
440     public void _addScriptListener() {
441         listener = new MyScriptListener();
442 
443         try {
444             oObj.addScriptListener(listener);
445             tRes.tested("addScriptListener()", true);
446         } catch (com.sun.star.lang.IllegalArgumentException e) {
447             log.println("addScriptListener() throws unexpected exception "
448                     + e.getMessage());
449             e.printStackTrace(log);
450             tRes.tested("addScriptListener()", false);
451         }
452     }
453 
454     /**
455     * Test removes script listener that was stored in method
456     * <code>addScriptListener()</code>. <p>
457     * Has <b> OK </b> status if the method successfully returns
458     * and no exceptions were thrown. <p>
459     * The following method tests are to be completed successfully before :
460     * <ul>
461     *  <li> <code> addScriptListener() </code> :
462     *  to have script listener </li>
463     * </ul>
464     */
465     public void _removeScriptListener() {
466         requiredMethod("addScriptListener()");
467 
468         try {
469             oObj.removeScriptListener(listener);
470             tRes.tested("removeScriptListener()", true);
471         } catch (com.sun.star.lang.IllegalArgumentException e) {
472             log.println("removeScriptListener() throws unexpected exception "
473                     + e.getMessage());
474             e.printStackTrace(log);
475             tRes.tested("removeScriptListener()", false);
476         }
477     }
478 
479     /**
480      * Class implement interface <code>XScriptListener</code>
481      * for test of the method <code>addScriptListener()</code>.
482      * No functionality implemented.
483      * @see com.sun.star.script.XScriptListener
484      */
485     class MyScriptListener implements XScriptListener {
486         public void firing(ScriptEvent evt) {
487         }
488 
489         public Object approveFiring(ScriptEvent evt) {
490             return evt.Helper;
491         }
492 
493         public void disposing(EventObject evt) {
494         }
495     }
496 
497 }
498 
499