|
| 1 | +/* |
| 2 | + * Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + @test |
| 26 | + @bug 4240721 |
| 27 | + @summary Test Component.getListeners API added in 1.3 |
| 28 | + @key headful |
| 29 | + @run main GetListenersTest |
| 30 | + */ |
| 31 | + |
| 32 | +import java.awt.Button; |
| 33 | +import java.awt.Canvas; |
| 34 | +import java.awt.Checkbox; |
| 35 | +import java.awt.CheckboxMenuItem; |
| 36 | +import java.awt.Choice; |
| 37 | +import java.awt.Component; |
| 38 | +import java.awt.EventQueue; |
| 39 | +import java.awt.FlowLayout; |
| 40 | +import java.awt.Frame; |
| 41 | +import java.awt.Label; |
| 42 | +import java.awt.Menu; |
| 43 | +import java.awt.MenuBar; |
| 44 | +import java.awt.MenuItem; |
| 45 | +import java.awt.Panel; |
| 46 | +import java.awt.Scrollbar; |
| 47 | +import java.awt.TextArea; |
| 48 | +import java.awt.TextField; |
| 49 | +import java.awt.event.ActionEvent; |
| 50 | +import java.awt.event.ActionListener; |
| 51 | +import java.awt.event.AdjustmentEvent; |
| 52 | +import java.awt.event.AdjustmentListener; |
| 53 | +import java.awt.event.ComponentAdapter; |
| 54 | +import java.awt.event.ComponentListener; |
| 55 | +import java.awt.event.ContainerAdapter; |
| 56 | +import java.awt.event.ContainerListener; |
| 57 | +import java.awt.event.FocusAdapter; |
| 58 | +import java.awt.event.FocusListener; |
| 59 | +import java.awt.event.HierarchyBoundsAdapter; |
| 60 | +import java.awt.event.HierarchyBoundsListener; |
| 61 | +import java.awt.event.HierarchyEvent; |
| 62 | +import java.awt.event.HierarchyListener; |
| 63 | +import java.awt.event.InputMethodEvent; |
| 64 | +import java.awt.event.InputMethodListener; |
| 65 | +import java.awt.event.ItemEvent; |
| 66 | +import java.awt.event.ItemListener; |
| 67 | +import java.awt.event.KeyAdapter; |
| 68 | +import java.awt.event.KeyListener; |
| 69 | +import java.awt.event.MouseAdapter; |
| 70 | +import java.awt.event.MouseListener; |
| 71 | +import java.awt.event.MouseMotionAdapter; |
| 72 | +import java.awt.event.MouseMotionListener; |
| 73 | +import java.awt.event.MouseWheelEvent; |
| 74 | +import java.awt.event.MouseWheelListener; |
| 75 | +import java.awt.event.TextEvent; |
| 76 | +import java.awt.event.TextListener; |
| 77 | +import java.awt.event.WindowAdapter; |
| 78 | +import java.awt.event.WindowEvent; |
| 79 | +import java.awt.event.WindowFocusListener; |
| 80 | +import java.awt.event.WindowListener; |
| 81 | +import java.awt.event.WindowStateListener; |
| 82 | +import java.beans.BeanInfo; |
| 83 | +import java.beans.EventSetDescriptor; |
| 84 | +import java.beans.Introspector; |
| 85 | +import java.beans.PropertyChangeEvent; |
| 86 | +import java.beans.PropertyChangeListener; |
| 87 | +import java.lang.reflect.Method; |
| 88 | +import java.util.EventListener; |
| 89 | + |
| 90 | +public class GetListenersTest { |
| 91 | + |
| 92 | + public static void main(String args[]) throws Exception { |
| 93 | + EventQueue.invokeAndWait(()-> { |
| 94 | + // Create frame with a bunch of components |
| 95 | + // and test that each component returns |
| 96 | + // the right type of listeners from Component.getListeners |
| 97 | + GLTFrame gltFrame = new GLTFrame(); |
| 98 | + try { |
| 99 | + gltFrame.initAndShowGui(); |
| 100 | + gltFrame.test(); |
| 101 | + } catch (Exception e) { |
| 102 | + throw new RuntimeException("Test failed", e); |
| 103 | + } finally { |
| 104 | + gltFrame.dispose(); |
| 105 | + } |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + /* |
| 110 | + * Checks an object has a listener for every support listener type |
| 111 | + */ |
| 112 | + static void checkForListenersOfEveryType(Object object) throws Exception { |
| 113 | + Class type = object.getClass(); |
| 114 | + |
| 115 | + BeanInfo info = Introspector.getBeanInfo(type); |
| 116 | + EventSetDescriptor esets[] = info.getEventSetDescriptors(); |
| 117 | + |
| 118 | + // ensure there are listeners for every type |
| 119 | + for (int nset = 0; nset < esets.length; nset++) { |
| 120 | + Class listenerType = esets[nset].getListenerType(); |
| 121 | + EventListener listener[] = getListeners(object, listenerType); |
| 122 | + // Skip PropertyChangeListener for now |
| 123 | + if (listener.length == 0 && validListenerToTest(listenerType)) { |
| 124 | + throw new RuntimeException("getListeners didn't return type " |
| 125 | + + listenerType); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + System.out.println("************"); |
| 130 | + System.out.println("PASSED: getListeners on " |
| 131 | + + object + " has all the right listeners."); |
| 132 | + System.out.println("************"); |
| 133 | + } |
| 134 | + |
| 135 | + /* |
| 136 | + * Calls getListeners on the object |
| 137 | + */ |
| 138 | + static EventListener[] getListeners(Object object, Class type) |
| 139 | + throws Exception { |
| 140 | + Method methods[] = object.getClass().getMethods(); |
| 141 | + Method method = null; |
| 142 | + |
| 143 | + for (int nmethod = 0; nmethod < methods.length; nmethod++) { |
| 144 | + if (methods[nmethod].getName().equals("getListeners")) { |
| 145 | + method = methods[nmethod]; |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | + if (method == null) { |
| 150 | + throw new RuntimeException("Object " |
| 151 | + + object + " has no getListeners method"); |
| 152 | + } |
| 153 | + Class params[] = {type}; |
| 154 | + EventListener listeners[] = null; |
| 155 | + listeners = (EventListener[]) method.invoke(object, params); |
| 156 | + System.out.println("Listeners of type: " + type + " on " + object); |
| 157 | + GetListenersTest.printArray(listeners); |
| 158 | + return listeners; |
| 159 | + } |
| 160 | + |
| 161 | + /* |
| 162 | + * Adds a listener of every type to the object |
| 163 | + */ |
| 164 | + static void addDummyListenersOfEveryType(Object object) throws Exception { |
| 165 | + Class type = object.getClass(); |
| 166 | + |
| 167 | + BeanInfo info = Introspector.getBeanInfo(type); |
| 168 | + EventSetDescriptor esets[] = info.getEventSetDescriptors(); |
| 169 | + |
| 170 | + // add every kind of listener |
| 171 | + for (int nset = 0; nset < esets.length; nset++) { |
| 172 | + Class listenerType = esets[nset].getListenerType(); |
| 173 | + EventListener listener = makeListener(listenerType); |
| 174 | + Method addListenerMethod = esets[nset].getAddListenerMethod(); |
| 175 | + Object params[] = {listener}; |
| 176 | + addListenerMethod.invoke(object, params); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + /* |
| 181 | + * Determines what listeners to exclude from the test for now |
| 182 | + */ |
| 183 | + static boolean validListenerToTest(Class listenerType) { |
| 184 | + /* Don't have any provision for PropertyChangeListeners... */ |
| 185 | + if ( listenerType == PropertyChangeListener.class ) { |
| 186 | + return false; |
| 187 | + } |
| 188 | + |
| 189 | + return true; |
| 190 | + } |
| 191 | + |
| 192 | + static void testGetListeners(Object object) throws Exception { |
| 193 | + GetListenersTest.addDummyListenersOfEveryType(object); |
| 194 | + GetListenersTest.checkForListenersOfEveryType(object); |
| 195 | + } |
| 196 | + |
| 197 | + static void printArray(Object objects[]) { |
| 198 | + System.out.println("{"); |
| 199 | + for(int n = 0; n < objects.length; n++) { |
| 200 | + System.out.println("\t"+objects[n]+","); |
| 201 | + } |
| 202 | + System.out.println("}"); |
| 203 | + } |
| 204 | + |
| 205 | + /* |
| 206 | + * Makes a dummy listener implementation for the given listener type |
| 207 | + */ |
| 208 | + static EventListener makeListener(Class listenerType) throws Exception { |
| 209 | + Object map[][] = { |
| 210 | + {ActionListener.class, MyActionAdapter.class}, |
| 211 | + {AdjustmentListener.class, MyAdjustmentAdapter.class}, |
| 212 | + {ComponentListener.class, MyComponentAdapter.class}, |
| 213 | + {ContainerListener.class, MyContainerAdapter.class}, |
| 214 | + {FocusListener.class, MyFocusAdapter.class}, |
| 215 | + {HierarchyBoundsListener.class, MyHierarchyBoundsAdapter.class}, |
| 216 | + {HierarchyListener.class, MyHierarchyAdapter.class}, |
| 217 | + {InputMethodListener.class, MyInputMethodAdapter.class}, |
| 218 | + {ItemListener.class, MyItemAdapter.class}, |
| 219 | + {KeyListener.class, MyKeyAdapter.class}, |
| 220 | + {MouseListener.class, MyMouseAdapter.class}, |
| 221 | + {MouseMotionListener.class, MyMouseMotionAdapter.class}, |
| 222 | + {MouseWheelListener.class, MyMouseWheelAdapter.class}, |
| 223 | + {TextListener.class, MyTextAdapter.class}, |
| 224 | + {WindowListener.class, MyWindowAdapter.class}, |
| 225 | + {WindowFocusListener.class, MyWindowFocusAdapter.class}, |
| 226 | + {WindowStateListener.class, MyWindowStateAdapter.class}, |
| 227 | + {PropertyChangeListener.class, MyPropertyChangeAdapter.class}, |
| 228 | + }; |
| 229 | + |
| 230 | + for (int n = 0; n < map.length; n++) { |
| 231 | + if (map[n][0] == listenerType) { |
| 232 | + Class adapterClass = (Class) map[n][1]; |
| 233 | + EventListener listener = |
| 234 | + (EventListener) adapterClass.newInstance(); |
| 235 | + return listener; |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + throw new RuntimeException("No adapter found for listener type " |
| 240 | + + listenerType); |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | +class GLTFrame extends Frame { |
| 245 | + MenuItem mitem; |
| 246 | + CheckboxMenuItem cmitem; |
| 247 | + |
| 248 | + GLTFrame() { |
| 249 | + super("Component.getListeners API Test"); |
| 250 | + } |
| 251 | + |
| 252 | + public void initAndShowGui() { |
| 253 | + setLayout(new FlowLayout()); |
| 254 | + |
| 255 | + add(new Label("Label")); |
| 256 | + add(new Button("Button")); |
| 257 | + add(new Checkbox("Checkbox")); |
| 258 | + Choice c = new Choice(); |
| 259 | + c.add("choice"); |
| 260 | + java.awt.List l = new java.awt.List(); |
| 261 | + l.add("list"); |
| 262 | + add(new Scrollbar()); |
| 263 | + add(new TextField("TextField")); |
| 264 | + add(new TextArea("TextArea")); |
| 265 | + add(new Panel()); |
| 266 | + add(new Canvas()); |
| 267 | + |
| 268 | + MenuBar menuBar = new MenuBar(); |
| 269 | + Menu menu = new Menu("Menu"); |
| 270 | + mitem = new MenuItem("Item 1"); |
| 271 | + cmitem = new CheckboxMenuItem("Item 2"); |
| 272 | + menu.add(mitem); |
| 273 | + menu.add(cmitem); |
| 274 | + menuBar.add(menu); |
| 275 | + setMenuBar(menuBar); |
| 276 | + |
| 277 | + pack(); |
| 278 | + setVisible(true); |
| 279 | + } |
| 280 | + |
| 281 | + public void test() throws Exception { |
| 282 | + // test Frame.getListeners |
| 283 | + GetListenersTest.testGetListeners(this); |
| 284 | + |
| 285 | + // |
| 286 | + // test getListeners on menu items |
| 287 | + // |
| 288 | + GetListenersTest.testGetListeners(mitem); |
| 289 | + GetListenersTest.testGetListeners(cmitem); |
| 290 | + |
| 291 | + // |
| 292 | + // test getListeners on all AWT Components |
| 293 | + // |
| 294 | + Component components[] = getComponents(); |
| 295 | + for (int nc = 0; nc < components.length; nc++) { |
| 296 | + GetListenersTest.testGetListeners(components[nc]); |
| 297 | + } |
| 298 | + } |
| 299 | +} |
| 300 | + |
| 301 | +/************************************************ |
| 302 | + * Dummy listener implementations we add to our components/models/objects |
| 303 | + */ |
| 304 | + |
| 305 | +class MyPropertyChangeAdapter implements PropertyChangeListener { |
| 306 | + public void propertyChange(PropertyChangeEvent evt) {} |
| 307 | +} |
| 308 | + |
| 309 | +class MyActionAdapter implements ActionListener { |
| 310 | + public void actionPerformed(ActionEvent ev) { |
| 311 | + } |
| 312 | +} |
| 313 | + |
| 314 | +class MyAdjustmentAdapter implements AdjustmentListener { |
| 315 | + public void adjustmentValueChanged(AdjustmentEvent e) { |
| 316 | + } |
| 317 | +} |
| 318 | + |
| 319 | +class MyHierarchyAdapter implements HierarchyListener { |
| 320 | + public void hierarchyChanged(HierarchyEvent e) { |
| 321 | + } |
| 322 | +} |
| 323 | + |
| 324 | +class MyInputMethodAdapter implements InputMethodListener { |
| 325 | + public void inputMethodTextChanged(InputMethodEvent event) { |
| 326 | + } |
| 327 | + |
| 328 | + public void caretPositionChanged(InputMethodEvent event) { |
| 329 | + } |
| 330 | +} |
| 331 | + |
| 332 | +class MyItemAdapter implements ItemListener { |
| 333 | + public void itemStateChanged(ItemEvent e) { |
| 334 | + } |
| 335 | +} |
| 336 | + |
| 337 | +class MyTextAdapter implements TextListener { |
| 338 | + public void textValueChanged(TextEvent e) { |
| 339 | + } |
| 340 | +} |
| 341 | + |
| 342 | +class MyComponentAdapter extends ComponentAdapter { |
| 343 | +} |
| 344 | + |
| 345 | +class MyContainerAdapter extends ContainerAdapter { |
| 346 | +} |
| 347 | + |
| 348 | +class MyFocusAdapter extends FocusAdapter { |
| 349 | +} |
| 350 | + |
| 351 | +class MyHierarchyBoundsAdapter extends HierarchyBoundsAdapter { |
| 352 | +} |
| 353 | + |
| 354 | +class MyKeyAdapter extends KeyAdapter { |
| 355 | +} |
| 356 | + |
| 357 | +class MyMouseAdapter extends MouseAdapter { |
| 358 | +} |
| 359 | + |
| 360 | +class MyMouseMotionAdapter extends MouseMotionAdapter { |
| 361 | +} |
| 362 | + |
| 363 | +class MyMouseWheelAdapter implements MouseWheelListener { |
| 364 | + public void mouseWheelMoved(MouseWheelEvent e) {} |
| 365 | +} |
| 366 | + |
| 367 | +class MyWindowAdapter extends WindowAdapter { |
| 368 | +} |
| 369 | + |
| 370 | +class MyWindowFocusAdapter implements WindowFocusListener { |
| 371 | + public void windowGainedFocus(WindowEvent t) {} |
| 372 | + public void windowLostFocus(WindowEvent t) {} |
| 373 | +} |
| 374 | + |
| 375 | +class MyWindowStateAdapter extends WindowAdapter { |
| 376 | +} |
0 commit comments