Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/awt/EventQueue/6980209/bug6980209.java
48795 views
/*1* Copyright (c) 2015, 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 698020925@summary Make tracking SecondaryLoop.enter/exit methods easier26@author Semyon Sadetsky27*/2829import sun.util.logging.PlatformLogger;3031import javax.swing.*;32import java.awt.*;33import java.awt.event.ActionEvent;34import java.awt.event.ActionListener;35import java.awt.event.KeyEvent;36import java.awt.event.KeyListener;3738public class bug6980209 implements ActionListener {39private final static PlatformLogger log =40PlatformLogger.getLogger("java.awt.event.WaitDispatchSupport");41public static final int ATTEMPTS = 100;42public static final int EVENTS = 5;4344private static boolean runInEDT;45private static JFrame frame;46private static int disorderCounter = 0;47private static Boolean enterReturn;48private static Boolean exitReturn;49private static int dispatchedEvents;5051public static void main(String[] args) throws Exception {52System.out.println(53"PLEASE DO NOT TOUCH KEYBOARD AND MOUSE DURING THE TEST RUN!");54// log.setLevel(PlatformLogger.Level.FINE);55// log.setLevel(PlatformLogger.Level.FINEST);56try {57SwingUtilities.invokeAndWait(new Runnable() {58public void run() {59frame = new JFrame();60frame.setUndecorated(true);61frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);62setup(frame);63}64});65testExitBeforeEnter();66System.out.println("Run random test in EDT");67runInEDT = true;68testRandomly();69System.out.println("Run random test in another thread");70runInEDT = false;71testRandomly();72System.out.println("ok");7374} finally {75SwingUtilities.invokeAndWait(new Runnable() {76@Override77public void run() {78frame.dispose();79}80});81}82}8384private static void testExitBeforeEnter() throws Exception {85final SecondaryLoop loop =86Toolkit.getDefaultToolkit().getSystemEventQueue()87.createSecondaryLoop();88loop.exit();89Robot robot = new Robot();90robot.mouseWheel(1);91robot.waitForIdle();92SwingUtilities.invokeAndWait(new Runnable() {93@Override94public void run() {95if(loop.enter()) {96throw new RuntimeException("Wrong enter() return value");97}98}99});100}101102private static void testRandomly() throws AWTException {103disorderCounter = 0;104final Robot robot = new Robot();105for (int i = 0; i < ATTEMPTS; i++) {106enterReturn = null;107exitReturn = null;108dispatchedEvents = 0;109synchronized (bug6980209.class) {110try {111for (int j = 0; j < EVENTS; j++) {112robot.keyPress(KeyEvent.VK_1);113robot.keyRelease(KeyEvent.VK_1);114}115116// trigger the button action that starts secondary loop117robot.keyPress(KeyEvent.VK_SPACE);118robot.keyRelease(KeyEvent.VK_SPACE);119120for (int j = 0; j < EVENTS; j++) {121robot.keyPress(KeyEvent.VK_1);122robot.keyRelease(KeyEvent.VK_1);123}124long time = System.nanoTime();125// wait for enter() returns126bug6980209.class.wait(1000);127if (enterReturn == null) {128System.out.println("wait time=" +129((System.nanoTime() - time) / 1E9) +130" seconds");131throw new RuntimeException(132"It seems the secondary loop will never end");133}134if (!enterReturn) disorderCounter++;135136robot.waitForIdle();137if (dispatchedEvents <1382 * EVENTS) { //check that all events are dispatched139throw new RuntimeException(140"KeyEvent.VK_1 has been lost!");141}142143} catch (InterruptedException e) {144throw new RuntimeException("Interrupted!");145}146}147}148if (disorderCounter == 0) {149System.out.println(150"Zero disordered enter/exit caught. It is recommended to run scenario again");151} else {152System.out.println(153"Disordered calls is " + disorderCounter + " from " +154ATTEMPTS);155}156}157158private static void setup(final JFrame frame) {159JButton jButton = new JButton("Button");160frame.getContentPane().add(jButton);161jButton.addActionListener(new bug6980209());162frame.pack();163frame.setVisible(true);164jButton.setFocusable(true);165jButton.requestFocus();166jButton.addKeyListener(new KeyListener() {167@Override168public void keyTyped(KeyEvent e) {169}170171@Override172public void keyPressed(KeyEvent e) {173if (e.getKeyChar() == '1') dispatchedEvents++;174}175176@Override177public void keyReleased(KeyEvent e) {178if (e.getKeyChar() == '1') dispatchedEvents++;179}180});181}182183184@Override185public void actionPerformed(ActionEvent e) {186if (runInEDT) {187runSecondaryLoop();188return;189}190new Thread("Secondary loop run thread") {191@Override192public void run() {193runSecondaryLoop();194}195}.start();196}197198private static void runSecondaryLoop() {199log.fine("\n---TEST START---");200201final SecondaryLoop loop =202Toolkit.getDefaultToolkit().getSystemEventQueue()203.createSecondaryLoop();204205final Object LOCK = new Object(); //lock to start simultaneously206Thread exitThread = new Thread("Exit thread") {207@Override208public void run() {209synchronized (LOCK) {210LOCK.notify();211}212Thread.yield();213exitReturn = loop.exit();214log.fine("exit() returns " + exitReturn);215}216};217218synchronized (LOCK) {219try {220exitThread.start();221LOCK.wait();222} catch (InterruptedException e1) {223throw new RuntimeException("What?");224}225}226227enterReturn = loop.enter();228log.fine("enter() returns " + enterReturn);229230try {231exitThread.join();232} catch (InterruptedException e) {233throw new RuntimeException("What?");234}235synchronized (bug6980209.class) {236bug6980209.class.notifyAll();237}238log.fine("\n---TEST END---");239}240}241242