Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JLayer/6872503/bug6872503.java
38918 views
/*1* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 687250325* @summary Checks that JLayer correctly works with its AWTEventListener26* @author Alexander Potochkin27*/2829import javax.swing.*;30import java.awt.*;31import java.awt.event.AWTEventListener;32import java.awt.event.AWTEventListenerProxy;3334public class bug6872503 {3536static JLayer<Component> l1;37static JLayer<Component> l2;3839private static void createGui() {40Toolkit toolkit = Toolkit.getDefaultToolkit();41int length = toolkit.getAWTEventListeners().length;4243l1 = new JLayer<Component>();44l1.setLayerEventMask(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);4546l2 = new JLayer<Component>();47l2.setLayerEventMask(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.KEY_EVENT_MASK);4849if (isLayerEventControllerAdded()) {50throw new RuntimeException("Unexpected AWTEventListener was added");51}5253JFrame frame = new JFrame();54frame.setLayout(new FlowLayout());55frame.add(l1);56frame.add(l2);5758if (isLayerEventControllerAdded()) {59throw new RuntimeException("Unexpected AWTEventListener was added");60}6162frame.pack();6364if (!isLayerEventControllerAdded()) {65throw new RuntimeException("AWTEventListener was not added");66}6768if (!layerEventControllerMaskEquals(l1.getLayerEventMask() | l2.getLayerEventMask())) {69throw new RuntimeException("Wrong mask for AWTEventListener");70}7172frame.dispose();7374if (isLayerEventControllerAdded()) {75throw new RuntimeException("Unexpected AWTEventListener was added");76}77}7879static boolean isLayerEventControllerAdded() {80Toolkit toolkit = Toolkit.getDefaultToolkit();81AWTEventListener layerEventController = null;82for (AWTEventListener listener : toolkit.getAWTEventListeners()) {83if (listener instanceof AWTEventListenerProxy) {84listener = ((AWTEventListenerProxy)listener).getListener();8586}87if ("LayerEventController".equals(listener.getClass().getSimpleName())) {88if (layerEventController != null) {89throw new RuntimeException("Duplicated LayerEventController");90}91layerEventController = listener;92}93}94boolean ret = layerEventController != null;95if (ret) {96System.out.println("LayerEventController found");97} else {98System.out.println("No LayerEventController");99}100return ret;101}102103static boolean layerEventControllerMaskEquals(long mask) {104Toolkit toolkit = Toolkit.getDefaultToolkit();105AWTEventListener layerEventController = null;106for (AWTEventListener listener : toolkit.getAWTEventListeners(mask)) {107if (listener instanceof AWTEventListenerProxy) {108listener = ((AWTEventListenerProxy)listener).getListener();109110}111if ("LayerEventController".equals(listener.getClass().getSimpleName())) {112if (layerEventController != null) {113throw new RuntimeException("Duplicated LayerEventController");114}115layerEventController = listener;116}117}118boolean ret = layerEventController != null;119if (ret) {120System.out.println("LayerEventController with the correct mask found");121} else {122System.out.println("No LayerEventController with the correct mask");123}124return ret;125}126127public static void main(String[] args) throws Exception {128SwingUtilities.invokeAndWait(new Runnable() {129public void run() {130bug6872503.createGui();131}132});133}134}135136137