1 /*
   2  * Copyright (c) 2009, 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  * @key headful
  27  * @bug 6872503
  28  * @summary Checks that JLayer correctly works with its AWTEventListener
  29  * @author Alexander Potochkin
  30  */
  31 
  32 import javax.swing.*;
  33 import java.awt.*;
  34 import java.awt.event.AWTEventListener;
  35 import java.awt.event.AWTEventListenerProxy;
  36 
  37 public class bug6872503 {
  38 
  39     static JLayer<Component> l1;
  40     static JLayer<Component> l2;
  41 
  42     private static void createGui() {
  43         Toolkit toolkit = Toolkit.getDefaultToolkit();
  44         int length = toolkit.getAWTEventListeners().length;
  45 
  46         l1 = new JLayer<Component>();
  47         l1.setLayerEventMask(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);
  48 
  49         l2 = new JLayer<Component>();
  50         l2.setLayerEventMask(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.KEY_EVENT_MASK);
  51 
  52         if (isLayerEventControllerAdded()) {
  53             throw new RuntimeException("Unexpected AWTEventListener was added");
  54         }
  55 
  56         JFrame frame = new JFrame();
  57         frame.setLayout(new FlowLayout());
  58         frame.add(l1);
  59         frame.add(l2);
  60 
  61         if (isLayerEventControllerAdded()) {
  62             throw new RuntimeException("Unexpected AWTEventListener was added");
  63         }
  64 
  65         frame.pack();
  66 
  67         if (!isLayerEventControllerAdded()) {
  68             throw new RuntimeException("AWTEventListener was not added");
  69         }
  70 
  71         if (!layerEventControllerMaskEquals(l1.getLayerEventMask() | l2.getLayerEventMask())) {
  72              throw new RuntimeException("Wrong mask for AWTEventListener");
  73         }
  74 
  75         frame.dispose();
  76 
  77         if (isLayerEventControllerAdded()) {
  78             throw new RuntimeException("Unexpected AWTEventListener was added");
  79         }
  80     }
  81 
  82     static boolean isLayerEventControllerAdded() {
  83         Toolkit toolkit = Toolkit.getDefaultToolkit();
  84         AWTEventListener layerEventController = null;
  85         for (AWTEventListener listener : toolkit.getAWTEventListeners()) {
  86             if (listener instanceof AWTEventListenerProxy) {
  87                 listener = ((AWTEventListenerProxy)listener).getListener();
  88 
  89             }
  90             if ("LayerEventController".equals(listener.getClass().getSimpleName())) {
  91                 if (layerEventController != null) {
  92                     throw new RuntimeException("Duplicated LayerEventController");
  93                 }
  94                 layerEventController = listener;
  95             }
  96         }
  97         boolean ret = layerEventController != null;
  98         if (ret) {
  99             System.out.println("LayerEventController found");
 100         } else {
 101             System.out.println("No LayerEventController");
 102         }
 103         return ret;
 104     }
 105 
 106     static boolean layerEventControllerMaskEquals(long mask) {
 107         Toolkit toolkit = Toolkit.getDefaultToolkit();
 108         AWTEventListener layerEventController = null;
 109         for (AWTEventListener listener : toolkit.getAWTEventListeners(mask)) {
 110             if (listener instanceof AWTEventListenerProxy) {
 111                 listener = ((AWTEventListenerProxy)listener).getListener();
 112 
 113             }
 114             if ("LayerEventController".equals(listener.getClass().getSimpleName())) {
 115                 if (layerEventController != null) {
 116                     throw new RuntimeException("Duplicated LayerEventController");
 117                 }
 118                 layerEventController = listener;
 119             }
 120         }
 121         boolean ret = layerEventController != null;
 122         if (ret) {
 123             System.out.println("LayerEventController with the correct mask found");
 124         } else {
 125             System.out.println("No LayerEventController with the correct mask");
 126         }
 127         return ret;
 128     }
 129 
 130     public static void main(String[] args) throws Exception {
 131         SwingUtilities.invokeAndWait(new Runnable() {
 132             public void run() {
 133                 bug6872503.createGui();
 134             }
 135         });
 136     }
 137 }