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