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