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