xref: /AOO41X/main/qadevOOo/tests/java/ifc/accessibility/_XAccessibleEventBroadcaster.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package ifc.accessibility;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import lib.MultiMethodTest;
31*cdf0e10cSrcweir import lib.Status;
32*cdf0e10cSrcweir import lib.StatusException;
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir import com.sun.star.accessibility.AccessibleEventObject;
35*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessible;
36*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleContext;
37*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleEventBroadcaster;
38*cdf0e10cSrcweir import com.sun.star.accessibility.XAccessibleEventListener;
39*cdf0e10cSrcweir import com.sun.star.lang.EventObject;
40*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir /**
43*cdf0e10cSrcweir  * Testing <code>
44*cdf0e10cSrcweir  * com.sun.star.accessibility.XAccessibleEventBroadcaster</code>
45*cdf0e10cSrcweir  * interface methods :
46*cdf0e10cSrcweir  * <ul>
47*cdf0e10cSrcweir  *  <li><code> addEventListener()</code></li>
48*cdf0e10cSrcweir  *  <li><code> removeEventListener()</code></li>
49*cdf0e10cSrcweir  * </ul> <p>
50*cdf0e10cSrcweir  *
51*cdf0e10cSrcweir  * This test needs the following object relations :
52*cdf0e10cSrcweir  * <ul>
53*cdf0e10cSrcweir  *  <li> <code>'EventProducer'</code> (of type
54*cdf0e10cSrcweir  *  <code>ifc.accessibility._XAccessibleEventBroadcaster.EventProducer</code>):
55*cdf0e10cSrcweir  *   this must be an implementation of the interface which could perform
56*cdf0e10cSrcweir  *   some actions for generating any kind of <code>AccessibleEvent</code></li>
57*cdf0e10cSrcweir  * <ul> <p>
58*cdf0e10cSrcweir  *
59*cdf0e10cSrcweir  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
60*cdf0e10cSrcweir  */
61*cdf0e10cSrcweir public class _XAccessibleEventBroadcaster extends MultiMethodTest {
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     public static interface EventProducer {
64*cdf0e10cSrcweir         void fireEvent();
65*cdf0e10cSrcweir     }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     public XAccessibleEventBroadcaster oObj = null;
68*cdf0e10cSrcweir     public String EventMsg = "";
69*cdf0e10cSrcweir     public boolean destroy = false;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     /**
73*cdf0e10cSrcweir      * Listener implementation which registers listener calls.
74*cdf0e10cSrcweir      */
75*cdf0e10cSrcweir     private class EvListener implements XAccessibleEventListener {
76*cdf0e10cSrcweir         public AccessibleEventObject notifiedEvent = null ;
77*cdf0e10cSrcweir         public void notifyEvent(AccessibleEventObject ev) {
78*cdf0e10cSrcweir             log.println("Listener, Event : " + ev.EventId);
79*cdf0e10cSrcweir             System.out.println("EventID: " + ev.EventId);
80*cdf0e10cSrcweir             Object old=ev.OldValue;
81*cdf0e10cSrcweir             if (old instanceof com.sun.star.accessibility.XAccessible) {
82*cdf0e10cSrcweir                 System.out.println("Old: "+((XAccessible)old).getAccessibleContext().getAccessibleName());
83*cdf0e10cSrcweir             }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir             Object nev=ev.NewValue;
86*cdf0e10cSrcweir             if (nev instanceof com.sun.star.accessibility.XAccessible) {
87*cdf0e10cSrcweir                 System.out.println("New: "+((XAccessible)nev).getAccessibleContext().getAccessibleName());
88*cdf0e10cSrcweir             }
89*cdf0e10cSrcweir             notifiedEvent = ev;
90*cdf0e10cSrcweir         }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir         public void disposing(EventObject ev) {}
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     /**
96*cdf0e10cSrcweir      * Retrieves relation.
97*cdf0e10cSrcweir      * @throws StatusException If the relation is not found.
98*cdf0e10cSrcweir      */
99*cdf0e10cSrcweir     public void before() {
100*cdf0e10cSrcweir         prod = (EventProducer) tEnv.getObjRelation("EventProducer") ;
101*cdf0e10cSrcweir         if (prod == null) {
102*cdf0e10cSrcweir             throw new StatusException(Status.failed("Relation missed."));
103*cdf0e10cSrcweir         }
104*cdf0e10cSrcweir         EventMsg = (String) tEnv.getObjRelation("EventMsg");
105*cdf0e10cSrcweir         Object dp = tEnv.getObjRelation("Destroy");
106*cdf0e10cSrcweir         if (dp != null) {
107*cdf0e10cSrcweir             destroy=true;
108*cdf0e10cSrcweir         }
109*cdf0e10cSrcweir     }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     EventProducer prod = null ;
112*cdf0e10cSrcweir     EvListener list = new EvListener();
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     /**
115*cdf0e10cSrcweir      * Adds two listeners and fires event by mean of object relation. <p>
116*cdf0e10cSrcweir      * Has <b> OK </b> status if both listeners were called
117*cdf0e10cSrcweir      */
118*cdf0e10cSrcweir     public void _addEventListener() {
119*cdf0e10cSrcweir         log.println("adding listener");
120*cdf0e10cSrcweir         oObj.addEventListener(list);
121*cdf0e10cSrcweir         boolean isTransient = chkTransient(tEnv.getTestObject());
122*cdf0e10cSrcweir         log.println("fire event");
123*cdf0e10cSrcweir         prod.fireEvent() ;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir         try {
126*cdf0e10cSrcweir             Thread.sleep(3000);
127*cdf0e10cSrcweir         }
128*cdf0e10cSrcweir         catch (InterruptedException ex) {
129*cdf0e10cSrcweir         }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir         boolean works = true;
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir         if (list.notifiedEvent == null) {
134*cdf0e10cSrcweir             if (!isTransient) {
135*cdf0e10cSrcweir                 log.println("listener wasn't called");
136*cdf0e10cSrcweir                 works = false;
137*cdf0e10cSrcweir             } else {
138*cdf0e10cSrcweir                 log.println("Object is Transient, listener isn't expected to be called");
139*cdf0e10cSrcweir             }
140*cdf0e10cSrcweir             oObj.removeEventListener(list);
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir         if (EventMsg != null) {
144*cdf0e10cSrcweir             log.println(EventMsg);
145*cdf0e10cSrcweir             tRes.tested("addEventListener()", Status.skipped(true) );
146*cdf0e10cSrcweir             return;
147*cdf0e10cSrcweir         }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         tRes.tested("addEventListener()", works );
150*cdf0e10cSrcweir     }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir     /**
153*cdf0e10cSrcweir      * Removes one of two listeners added before and and fires event
154*cdf0e10cSrcweir      * by mean of object relation. <p>
155*cdf0e10cSrcweir      *
156*cdf0e10cSrcweir      * Has <b> OK </b> status if the removed listener wasn't called. <p>
157*cdf0e10cSrcweir      *
158*cdf0e10cSrcweir      * The following method tests are to be completed successfully before :
159*cdf0e10cSrcweir      * <ul>
160*cdf0e10cSrcweir      *  <li> <code>addEventListener()</code> : to have added listeners </li>
161*cdf0e10cSrcweir      * </ul>
162*cdf0e10cSrcweir      */
163*cdf0e10cSrcweir     public void _removeEventListener() {
164*cdf0e10cSrcweir         requiredMethod("addEventListener()");
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir         list.notifiedEvent = null;
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir         log.println("remove listener");
169*cdf0e10cSrcweir         oObj.removeEventListener(list);
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir         log.println("fire event");
172*cdf0e10cSrcweir         prod.fireEvent() ;
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         try {
175*cdf0e10cSrcweir             Thread.sleep(500);
176*cdf0e10cSrcweir         }
177*cdf0e10cSrcweir         catch (InterruptedException ex) {
178*cdf0e10cSrcweir         }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir         if (list.notifiedEvent == null) {
181*cdf0e10cSrcweir             log.println("listener wasn't called -- OK");
182*cdf0e10cSrcweir         }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir         tRes.tested("removeEventListener()", list.notifiedEvent == null);
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir     protected static boolean chkTransient(Object Testcase) {
189*cdf0e10cSrcweir         boolean ret = false;
190*cdf0e10cSrcweir         XAccessibleContext accCon = (XAccessibleContext)
191*cdf0e10cSrcweir                     UnoRuntime.queryInterface(XAccessibleContext.class,Testcase);
192*cdf0e10cSrcweir         if (accCon.getAccessibleStateSet().contains(
193*cdf0e10cSrcweir             com.sun.star.accessibility.AccessibleStateType.TRANSIENT)){
194*cdf0e10cSrcweir             if (!accCon.getAccessibleParent().getAccessibleContext().getAccessibleStateSet().contains(
195*cdf0e10cSrcweir                 com.sun.star.accessibility.AccessibleStateType.MANAGES_DESCENDANTS)) {
196*cdf0e10cSrcweir                 throw new lib.StatusException(lib.Status.failed("Parent doesn't manage descendents"));
197*cdf0e10cSrcweir             }
198*cdf0e10cSrcweir             ret=true;
199*cdf0e10cSrcweir         }
200*cdf0e10cSrcweir         return ret;
201*cdf0e10cSrcweir     }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir     /**
204*cdf0e10cSrcweir     * Forces environment recreation.
205*cdf0e10cSrcweir     */
206*cdf0e10cSrcweir     protected void after() {
207*cdf0e10cSrcweir         if (destroy) disposeEnvironment();
208*cdf0e10cSrcweir     }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir }
212*cdf0e10cSrcweir 
213