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 complex.toolkit.accessibility;
20 
21 import com.sun.star.accessibility.AccessibleEventObject;
22 import com.sun.star.accessibility.AccessibleStateType;
23 import com.sun.star.accessibility.XAccessible;
24 import com.sun.star.accessibility.XAccessibleContext;
25 import com.sun.star.accessibility.XAccessibleEventBroadcaster;
26 import com.sun.star.accessibility.XAccessibleEventListener;
27 import com.sun.star.awt.PosSize;
28 import com.sun.star.awt.Rectangle;
29 import com.sun.star.awt.XWindow;
30 import com.sun.star.lang.EventObject;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.uno.XInterface;
34 
35 /**
36  * Testing <code>
37  * com.sun.star.accessibility.XAccessibleEventBroadcaster</code>
38  * interface methods :
39  * <ul>
40  *  <li><code> addAccessibleEventListener()</code></li>
41  *  <li><code> removeAccessibleEventListener()</code></li>
42  * </ul>
43  *
44  * <p>This test needs the following object relations :</p>
45  * <ul>
46  *  <li> <code>'EventProducer'</code> (of type
47  *  <code>ifc.accessibility._XAccessibleEventBroadcaster.EventProducer</code>):
48  *   this must be an implementation of the interface which could perform
49  *   some actions for generating any kind of <code>AccessibleEvent</code></li>
50  * </ul>
51  *
52  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
53  */
54 public class _XAccessibleEventBroadcaster {
55 
56     private final XAccessibleEventBroadcaster oObj;
57     private final EventProducer prod;
58     private final EvListener list = new EvListener();
59 
60     /**
61      * An event producer
62      */
63     private static class EventProducer {
64         private final XWindow xWindow;
EventProducer(XWindow window)65         private EventProducer(XWindow window) {
66             xWindow = window;
67         }
68 
fireEvent()69        private void fireEvent() {
70             Rectangle newPosSize = xWindow.getPosSize();
71             newPosSize.Width = newPosSize.Width - 20;
72             newPosSize.Height = newPosSize.Height - 20;
73             newPosSize.X = newPosSize.X + 20;
74             newPosSize.Y = newPosSize.Y + 20;
75             xWindow.setPosSize(newPosSize.X, newPosSize.Y, newPosSize.Width,
76                                     newPosSize.Height, PosSize.POSSIZE);
77         }
78     }
79 
80     /**
81      * Listener implementation which registers listener calls.
82      */
83     private class EvListener implements XAccessibleEventListener {
84         public AccessibleEventObject notifiedEvent = null ;
notifyEvent(AccessibleEventObject ev)85         public void notifyEvent(AccessibleEventObject ev) {
86             System.out.println("Listener, Event : " + ev.EventId);
87             System.out.println("EventID: " + ev.EventId);
88             Object old=ev.OldValue;
89             if (old instanceof com.sun.star.accessibility.XAccessible) {
90                 System.out.println("Old: "+((XAccessible)old).getAccessibleContext().getAccessibleName());
91             }
92 
93             Object nev=ev.NewValue;
94             if (nev instanceof com.sun.star.accessibility.XAccessible) {
95                 System.out.println("New: "+((XAccessible)nev).getAccessibleContext().getAccessibleName());
96             }
97             notifiedEvent = ev;
98         }
99 
disposing(EventObject ev)100         public void disposing(EventObject ev) {}
101     }
102 
_XAccessibleEventBroadcaster(XInterface object, XWindow window)103     public _XAccessibleEventBroadcaster(XInterface object, XWindow window) {
104         oObj = UnoRuntime.queryInterface(XAccessibleEventBroadcaster.class, object);
105         prod = new EventProducer(window);
106     }
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(XMultiServiceFactory xMSF)112     public boolean _addEventListener(XMultiServiceFactory xMSF) {
113         System.out.println("adding two listeners");
114         oObj.addAccessibleEventListener(list);
115         boolean isTransient = chkTransient(oObj);
116         System.out.println("fire event");
117         prod.fireEvent() ;
118 
119         util.utils.waitForEventIdle(xMSF);
120 
121         boolean works = true;
122 
123         if (list.notifiedEvent == null) {
124             if (!isTransient) {
125                 System.out.println("listener wasn't called");
126                 works = false;
127             } else {
128                 System.out.println("Object is Transient, listener isn't expected to be called");
129             }
130             oObj.removeAccessibleEventListener(list);
131         }
132 
133         return works;
134     }
135 
136     /**
137      * Removes one of two listeners added before and fires event
138      * by mean of object relation. <p>
139      *
140      * Has <b> OK </b> status if the removed listener wasn't called. <p>
141      *
142      * The following method tests are to be completed successfully before :
143      * <ul>
144      *  <li> <code>addEventListener()</code> : to have added listeners </li>
145      * </ul>
146      */
_removeEventListener(XMultiServiceFactory xMSF)147     public boolean _removeEventListener(XMultiServiceFactory xMSF) {
148 
149         list.notifiedEvent = null;
150 
151         System.out.println("remove first listener");
152         oObj.removeAccessibleEventListener(list);
153 
154         System.out.println("fire event");
155         prod.fireEvent() ;
156 
157         util.utils.waitForEventIdle(xMSF);
158 
159         if (list.notifiedEvent == null) {
160             System.out.println("listener wasn't called -- OK");
161         }
162 
163         return list.notifiedEvent == null;
164 
165     }
166 
chkTransient(Object Testcase)167     private static boolean chkTransient(Object Testcase) {
168         XAccessibleContext accCon = UnoRuntime.queryInterface(XAccessibleContext.class, Testcase);
169         return accCon.getAccessibleStateSet().contains(
170             AccessibleStateType.TRANSIENT)
171             && accCon.getAccessibleParent().getAccessibleContext()
172                 .getAccessibleStateSet().contains(
173                     AccessibleStateType.MANAGES_DESCENDANTS);
174     }
175 
176 }
177 
178