Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / script / _XEventAttacherManager.java
blobc6bcbd886071abff612d7b4297cdd3adcd0070ed
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.script;
21 import lib.MultiMethodTest;
23 import com.sun.star.lang.EventObject;
24 import com.sun.star.lang.XMultiServiceFactory;
25 import com.sun.star.script.ScriptEvent;
26 import com.sun.star.script.ScriptEventDescriptor;
27 import com.sun.star.script.XEventAttacherManager;
28 import com.sun.star.script.XScriptListener;
30 /**
31 * Testing <code>com.sun.star.script.XEventAttacherManager</code>
32 * interface methods :
33 * <ul>
34 * <li><code> registerScriptEvent()</code></li>
35 * <li><code> registerScriptEvents()</code></li>
36 * <li><code> revokeScriptEvent()</code></li>
37 * <li><code> revokeScriptEvents()</code></li>
38 * <li><code> insertEntry()</code></li>
39 * <li><code> removeEntry()</code></li>
40 * <li><code> getScriptEvents()</code></li>
41 * <li><code> attach()</code></li>
42 * <li><code> detach()</code></li>
43 * <li><code> addScriptListener()</code></li>
44 * <li><code> removeScriptListener()</code></li>
45 * </ul> <p>
46 * @see com.sun.star.script.XEventAttacherManager
48 public class _XEventAttacherManager extends MultiMethodTest {
50 /**
51 * oObj filled by MultiMethodTest
53 public XEventAttacherManager oObj = null;
55 private static final int index = 0;
57 /**
58 * Test calls the method and stores index of new entry. <p>
59 * Has <b> OK </b> status if the method successfully returns
60 * and no exceptions were thrown. <p>
62 public void _insertEntry() {
63 try {
64 oObj.insertEntry(index);
65 tRes.tested("insertEntry()", true);
66 } catch (com.sun.star.lang.IllegalArgumentException e) {
67 log.println("insertEntry(" + index
68 + ") throws unexpected exception "
69 + e.getMessage());
70 e.printStackTrace(log);
71 tRes.tested("insertEntry()", false);
75 ScriptEventDescriptor desc;
77 /**
78 * Test creates <code>ScriptEventDescriptor</code>, registers
79 * the script event and stores the descriptor. <p>
80 * Has <b> OK </b> status if the method successfully returns
81 * and no exceptions were thrown. <p>
82 * The following method tests are to be completed successfully before :
83 * <ul>
84 * <li> <code> insertEntry() </code> : to have entry's index</li>
85 * </ul>
86 * @see com.sun.star.script.ScriptEventDescriptor
88 public void _registerScriptEvent() {
89 requiredMethod("insertEntry()");
90 desc = new ScriptEventDescriptor(
91 "XEventListener1",
92 "disposing", "", "Basic", "");
94 try {
95 oObj.registerScriptEvent(index, desc);
96 tRes.tested("registerScriptEvent()", true);
97 } catch (com.sun.star.lang.IllegalArgumentException e) {
98 log.println("registerScriptEvent() throws unexpected exception "
99 + e.getMessage());
100 e.printStackTrace(log);
101 tRes.tested("registerScriptEvent()", false);
105 ScriptEventDescriptor descs[];
108 * Test creates array of <code>ScriptEventDescriptor</code>, registers
109 * this script events and stores the descriptors. <p>
110 * Has <b> OK </b> status if the method successfully returns
111 * and no exceptions were thrown. <p>
112 * The following method tests are to be completed successfully before :
113 * <ul>
114 * <li> <code> insertEntry() </code> : to have entry's index</li>
115 * </ul>
116 * @see com.sun.star.script.ScriptEventDescriptor
118 public void _registerScriptEvents() {
119 requiredMethod("insertEntry()");
120 descs = new ScriptEventDescriptor[] {
121 new ScriptEventDescriptor(
122 "XEventListener2",
123 "disposing", "", "Basic", ""),
124 new ScriptEventDescriptor(
125 "XEventListener3",
126 "disposing", "", "Basic", "")
129 try {
130 oObj.registerScriptEvents(index, descs);
131 tRes.tested("registerScriptEvents()", true);
132 } catch (com.sun.star.lang.IllegalArgumentException e) {
133 log.println("registerScriptEvents() throws unexpected exception "
134 + e.getMessage());
135 e.printStackTrace(log);
136 tRes.tested("registerScriptEvents()", false);
141 * Test calls the method and checks returned value. <p>
142 * Has <b> OK </b> status if returned array of descriptors contains
143 * array of descriptors registered by methods <code>registerScriptEvents</code>
144 * and <code>registerScriptEvent</code> and no exceptions were thrown. <p>
145 * The following method tests are to be completed successfully before :
146 * <ul>
147 * <li> <code> registerScriptEvent() </code> :
148 * to have registered descriptor </li>
149 * <li> <code> registerScriptEvents() </code> :
150 * to have registered descriptors </li>
151 * </ul>
153 public void _getScriptEvents() {
154 requiredMethod("registerScriptEvent()");
155 requiredMethod("registerScriptEvents()");
157 ScriptEventDescriptor[] res;
159 try {
160 res = oObj.getScriptEvents(index);
161 } catch (com.sun.star.lang.IllegalArgumentException e) {
162 log.println("registerScriptEvents() throws unexpected exception "
163 + e.getMessage());
164 e.printStackTrace(log);
165 tRes.tested("registerScriptEvents()", false);
166 return;
169 // checking the desc and descs are in script events
170 tRes.tested("getScriptEvents()",
171 contains(res, desc) && containsArray(res, descs));
173 log.println("Script events :") ;
174 printEvents(res) ;
178 * Method checks that array of descriptors contains the concrete desciptor.
179 * @param container the array of descriptors
180 * @param evt the descriptor which presence in the array is checked
181 * @return true if the descriptor presence in the array
183 boolean contains(ScriptEventDescriptor[] container,
184 ScriptEventDescriptor evt) {
185 for (int i = 0; i < container.length; i++) {
186 if (equal(container[i], evt)) {
187 return true;
191 return false;
195 * Method checks that one array of descriptors contains
196 * another array of descriptors.
197 * @param container the array of descriptors
198 * @param events the array of descriptors which presence
199 * in array <code>container</code> is checked
200 * @return true if the array <code>events</code> contains in the array
201 * <code>container</code>
203 boolean containsArray(ScriptEventDescriptor[] container,
204 ScriptEventDescriptor[] events) {
205 for (int i = 0; i < events.length; i++) {
206 if (!contains(container, events[i])) {
207 return false;
211 return true;
215 * Compares descriptor <code>evt1</code> to descriptor <code>evt2</code>.
216 * Two descriptors are considered equal if all their fields are equal.
217 * @return true if the argument is not <code>null</code> and
218 * the descriptors are equal; false otherwise
220 boolean equal(ScriptEventDescriptor evt1,
221 ScriptEventDescriptor evt2) {
222 return evt1.ListenerType.equals(evt2.ListenerType)
223 && evt1.EventMethod.equals(evt2.EventMethod)
224 && evt1.ScriptType.equals(evt2.ScriptType)
225 && evt1.ScriptCode.equals(evt2.ScriptCode)
226 && evt1.AddListenerParam.equals(evt2.AddListenerParam);
230 * Prints fields of descriptor <code>evt</code> to log.
231 * @param evt the descriptor that needs to be printed to log
233 void printEvent(ScriptEventDescriptor evt) {
234 if (evt == null) {
235 log.println("null");
236 } else {
237 log.println("\"" + evt.ListenerType + "\",\""
238 + evt.EventMethod + "\",\""
239 + evt.ScriptType + "\",\""
240 + evt.ScriptCode + "\",\""
241 + evt.AddListenerParam + "\"");
246 * Prints the descriptors to log.
247 * @param events the array of descriptors that need to be printed to log
249 void printEvents(ScriptEventDescriptor events[]) {
250 if (events == null) {
251 log.println("null");
252 } else {
253 for (int i = 0; i < events.length; i++) {
254 printEvent(events[i]);
259 Object attachedObject;
262 * Test creates instance of <code>NamingService</code> (arbitrarily),
263 * stores it and attaches it to the entry with index stored in the method
264 * <code>insertEntry()</code>. <p>
265 * Has <b> OK </b> status if the method successfully returns
266 * and no exceptions were thrown. <p>
267 * The following method tests are to be completed successfully before :
268 * <ul>
269 * <li> <code> insertEntry() </code> : to have entry's index for attach</li>
271 public void _attach() {
272 requiredMethod("insertEntry()");
274 try {
275 XMultiServiceFactory xMSF = tParam.getMSF();
276 attachedObject = xMSF.createInstance
277 ( "com.sun.star.uno.NamingService" );
278 } catch( com.sun.star.uno.Exception e ) {
279 log.println("com.sun.star.uno.NamingService not available" );
280 e.printStackTrace(log);
281 tRes.tested("attach()", false);
282 return;
284 if (attachedObject == null) {
285 log.println("com.sun.star.uno.NamingService not available" );
286 tRes.tested("attach()", false);
287 return;
290 try {
291 oObj.attach(index, attachedObject, "param");
292 tRes.tested("attach()", true);
293 } catch (com.sun.star.lang.IllegalArgumentException e) {
294 log.println("attach() throws exception "
295 + e.getMessage());
296 e.printStackTrace(log);
297 tRes.tested("attach()", false);
298 } catch (com.sun.star.lang.ServiceNotRegisteredException e) {
299 log.println("attach() throws exception "
300 + e.getMessage());
301 e.printStackTrace(log);
302 tRes.tested("attach()", false);
307 * Test calls the method for the object that was stored in the method
308 * <code>attach()</code>. <p>
309 * Has <b> OK </b> status if the method successfully returns
310 * and no exceptions were thrown. <p>
311 * The following method tests are to be completed successfully before :
312 * <ul>
313 * <li> <code> attach() </code> : to have attached object </li>
314 * </ul>
316 public void _detach() {
317 requiredMethod("attach()");
319 try {
320 oObj.detach(index, attachedObject);
321 tRes.tested("detach()", true);
322 } catch (com.sun.star.lang.IllegalArgumentException e) {
323 log.println("detach() throws unexpected exception "
324 + e.getMessage());
325 e.printStackTrace(log);
326 tRes.tested("detach()", false);
331 * Test revokes script event that was registered by method
332 * <code>registerScriptEvent()</code> and checks that the description
333 * was removed. <p>
334 * Has <b> OK </b> status if description was successfully removed. <p>
335 * The following method tests are to be completed successfully before :
336 * <ul>
337 * <li> <code> registerScriptEvent() </code> :
338 * to have registered descriptor </li>
339 * </ul>
340 * The following method tests are to be executed before :
341 * <ul>
342 * <li> <code> getScriptEvents() </code> :
343 * this method must be executed first </li>
344 * </ul>
346 public void _revokeScriptEvent() {
347 requiredMethod("registerScriptEvent()");
348 executeMethod("getScriptEvents()") ;
350 try {
351 oObj.revokeScriptEvent(index, desc.ListenerType,
352 desc.EventMethod, "");
354 ScriptEventDescriptor[] res = oObj.getScriptEvents(index);
355 // checking that the desc has been removed
356 tRes.tested("revokeScriptEvent()", !contains(res, desc));
357 printEvents(res) ;
358 } catch (com.sun.star.lang.IllegalArgumentException e) {
359 log.println("revokeScriptEvent() throws unexpected exception "
360 + e.getMessage());
361 e.printStackTrace(log);
362 tRes.tested("revokeScriptEvent()", false);
367 * Test revokes script events that was registered by method
368 * <code>registerScriptEvents()</code> and checks that the descriptions
369 * were removed. <p>
370 * Has <b> OK </b> status if descriptions were successfully removed. <p>
371 * The following method tests are to be completed successfully before :
372 * <ul>
373 * <li> <code> revokeScriptEvent() </code> :
374 * this method must be executed first </li>
375 * </ul>
376 * The following method tests are to be executed before :
377 * <ul>
378 * <li> <code> getScriptEvents() </code> :
379 * this method must be executed first </li>
380 * </ul>
382 public void _revokeScriptEvents() {
383 requiredMethod("revokeScriptEvent()");
384 executeMethod("getScriptEvents()") ;
386 try {
387 oObj.revokeScriptEvents(index);
389 ScriptEventDescriptor[] res = oObj.getScriptEvents(index);
390 // checking that all events have been removed
391 tRes.tested("revokeScriptEvents()",
392 res == null || res.length == 0);
393 } catch (com.sun.star.lang.IllegalArgumentException e) {
394 log.println("revokeScriptEvents() throws unexpected exception "
395 + e.getMessage());
396 e.printStackTrace(log);
397 tRes.tested("revokeScriptEvents()", false);
402 * Test calls the method with entry's index that was stored in method
403 * <code>insertEntry()</code>. <p>
404 * Has <b> OK </b> status if the method successfully returns
405 * and no exceptions were thrown. <p>
406 * The following method tests are to be completed successfully before :
407 * <ul>
408 * <li> <code> insertEntry() </code> :
409 * to have entry's index </li>
411 public void _removeEntry() {
412 requiredMethod("insertEntry()");
413 try {
414 oObj.removeEntry(index);
415 tRes.tested("removeEntry()", true);
416 } catch (com.sun.star.lang.IllegalArgumentException e) {
417 log.println("removeEntry(" + index
418 + ") throws unexpected exception "
419 + e.getMessage());
420 e.printStackTrace(log);
421 tRes.tested("removeEntry()", false);
425 XScriptListener listener;
428 * Test creates object that supports interface <code>XScriptListener</code>,
429 * stores it and addes this scripts listener. <p>
430 * Has <b> OK </b> status if the method successfully returns
431 * and no exceptions were thrown. <p>
432 * @see com.sun.star.script.XScriptListener
434 public void _addScriptListener() {
435 listener = new MyScriptListener();
437 try {
438 oObj.addScriptListener(listener);
439 tRes.tested("addScriptListener()", true);
440 } catch (com.sun.star.lang.IllegalArgumentException e) {
441 log.println("addScriptListener() throws unexpected exception "
442 + e.getMessage());
443 e.printStackTrace(log);
444 tRes.tested("addScriptListener()", false);
449 * Test removes script listener that was stored in method
450 * <code>addScriptListener()</code>. <p>
451 * Has <b> OK </b> status if the method successfully returns
452 * and no exceptions were thrown. <p>
453 * The following method tests are to be completed successfully before :
454 * <ul>
455 * <li> <code> addScriptListener() </code> :
456 * to have script listener </li>
457 * </ul>
459 public void _removeScriptListener() {
460 requiredMethod("addScriptListener()");
462 try {
463 oObj.removeScriptListener(listener);
464 tRes.tested("removeScriptListener()", true);
465 } catch (com.sun.star.lang.IllegalArgumentException e) {
466 log.println("removeScriptListener() throws unexpected exception "
467 + e.getMessage());
468 e.printStackTrace(log);
469 tRes.tested("removeScriptListener()", false);
474 * Class implement interface <code>XScriptListener</code>
475 * for test of the method <code>addScriptListener()</code>.
476 * No functionality implemented.
477 * @see com.sun.star.script.XScriptListener
479 class MyScriptListener implements XScriptListener {
480 public void firing(ScriptEvent evt) {
483 public Object approveFiring(ScriptEvent evt) {
484 return evt.Helper;
487 public void disposing(EventObject evt) {