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