xref: /AOO41X/main/qadevOOo/tests/java/ifc/accessibility/_XAccessibleEventBroadcaster.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.accessibility;
29 
30 import lib.MultiMethodTest;
31 import lib.Status;
32 import lib.StatusException;
33 
34 import com.sun.star.accessibility.AccessibleEventObject;
35 import com.sun.star.accessibility.XAccessible;
36 import com.sun.star.accessibility.XAccessibleContext;
37 import com.sun.star.accessibility.XAccessibleEventBroadcaster;
38 import com.sun.star.accessibility.XAccessibleEventListener;
39 import com.sun.star.lang.EventObject;
40 import com.sun.star.uno.UnoRuntime;
41 
42 /**
43  * Testing <code>
44  * com.sun.star.accessibility.XAccessibleEventBroadcaster</code>
45  * interface methods :
46  * <ul>
47  *  <li><code> addEventListener()</code></li>
48  *  <li><code> removeEventListener()</code></li>
49  * </ul> <p>
50  *
51  * This test needs the following object relations :
52  * <ul>
53  *  <li> <code>'EventProducer'</code> (of type
54  *  <code>ifc.accessibility._XAccessibleEventBroadcaster.EventProducer</code>):
55  *   this must be an implementation of the interface which could perform
56  *   some actions for generating any kind of <code>AccessibleEvent</code></li>
57  * <ul> <p>
58  *
59  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
60  */
61 public class _XAccessibleEventBroadcaster extends MultiMethodTest {
62 
63     public static interface EventProducer {
64         void fireEvent();
65     }
66 
67     public XAccessibleEventBroadcaster oObj = null;
68     public String EventMsg = "";
69     public boolean destroy = false;
70 
71 
72     /**
73      * Listener implementation which registers listener calls.
74      */
75     private class EvListener implements XAccessibleEventListener {
76         public AccessibleEventObject notifiedEvent = null ;
77         public void notifyEvent(AccessibleEventObject ev) {
78             log.println("Listener, Event : " + ev.EventId);
79             System.out.println("EventID: " + ev.EventId);
80             Object old=ev.OldValue;
81             if (old instanceof com.sun.star.accessibility.XAccessible) {
82                 System.out.println("Old: "+((XAccessible)old).getAccessibleContext().getAccessibleName());
83             }
84 
85             Object nev=ev.NewValue;
86             if (nev instanceof com.sun.star.accessibility.XAccessible) {
87                 System.out.println("New: "+((XAccessible)nev).getAccessibleContext().getAccessibleName());
88             }
89             notifiedEvent = ev;
90         }
91 
92         public void disposing(EventObject ev) {}
93     }
94 
95     /**
96      * Retrieves relation.
97      * @throws StatusException If the relation is not found.
98      */
99     public void before() {
100         prod = (EventProducer) tEnv.getObjRelation("EventProducer") ;
101         if (prod == null) {
102             throw new StatusException(Status.failed("Relation missed."));
103         }
104         EventMsg = (String) tEnv.getObjRelation("EventMsg");
105         Object dp = tEnv.getObjRelation("Destroy");
106         if (dp != null) {
107             destroy=true;
108         }
109     }
110 
111     EventProducer prod = null ;
112     EvListener list = new EvListener();
113 
114     /**
115      * Adds two listeners and fires event by mean of object relation. <p>
116      * Has <b> OK </b> status if both listeners were called
117      */
118     public void _addEventListener() {
119         log.println("adding listener");
120         oObj.addEventListener(list);
121         boolean isTransient = chkTransient(tEnv.getTestObject());
122         log.println("fire event");
123         prod.fireEvent() ;
124 
125         try {
126             Thread.sleep(3000);
127         }
128         catch (InterruptedException ex) {
129         }
130 
131         boolean works = true;
132 
133         if (list.notifiedEvent == null) {
134             if (!isTransient) {
135                 log.println("listener wasn't called");
136                 works = false;
137             } else {
138                 log.println("Object is Transient, listener isn't expected to be called");
139             }
140             oObj.removeEventListener(list);
141         }
142 
143         if (EventMsg != null) {
144             log.println(EventMsg);
145             tRes.tested("addEventListener()", Status.skipped(true) );
146             return;
147         }
148 
149         tRes.tested("addEventListener()", works );
150     }
151 
152     /**
153      * Removes one of two listeners added before and and fires event
154      * by mean of object relation. <p>
155      *
156      * Has <b> OK </b> status if the removed listener wasn't called. <p>
157      *
158      * The following method tests are to be completed successfully before :
159      * <ul>
160      *  <li> <code>addEventListener()</code> : to have added listeners </li>
161      * </ul>
162      */
163     public void _removeEventListener() {
164         requiredMethod("addEventListener()");
165 
166         list.notifiedEvent = null;
167 
168         log.println("remove listener");
169         oObj.removeEventListener(list);
170 
171         log.println("fire event");
172         prod.fireEvent() ;
173 
174         try {
175             Thread.sleep(500);
176         }
177         catch (InterruptedException ex) {
178         }
179 
180         if (list.notifiedEvent == null) {
181             log.println("listener wasn't called -- OK");
182         }
183 
184         tRes.tested("removeEventListener()", list.notifiedEvent == null);
185 
186     }
187 
188     protected static boolean chkTransient(Object Testcase) {
189         boolean ret = false;
190         XAccessibleContext accCon = (XAccessibleContext)
191                     UnoRuntime.queryInterface(XAccessibleContext.class,Testcase);
192         if (accCon.getAccessibleStateSet().contains(
193             com.sun.star.accessibility.AccessibleStateType.TRANSIENT)){
194             if (!accCon.getAccessibleParent().getAccessibleContext().getAccessibleStateSet().contains(
195                 com.sun.star.accessibility.AccessibleStateType.MANAGES_DESCENDANTS)) {
196                 throw new lib.StatusException(lib.Status.failed("Parent doesn't manage descendents"));
197             }
198             ret=true;
199         }
200         return ret;
201     }
202 
203     /**
204     * Forces environment recreation.
205     */
206     protected void after() {
207         if (destroy) disposeEnvironment();
208     }
209 
210 
211 }
212 
213