Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/EventQueue/6980209/bug6980209.java
38828 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 javax.swing.*;30import java.awt.*;31import java.awt.event.ActionEvent;32import java.awt.event.ActionListener;33import java.awt.event.KeyEvent;34import java.awt.event.KeyListener;35import java.util.logging.Logger;3637public class bug6980209 implements ActionListener {38private final static Logger log =39Logger.getLogger("java.awt.event.WaitDispatchSupport");40public static final int ATTEMPTS = 100;41public static final int EVENTS = 5;4243private static boolean runInEDT;44private static JFrame frame;45private static int disorderCounter = 0;46private static Boolean enterReturn;47private static Boolean exitReturn;48private static int dispatchedEvents;4950public static void main(String[] args) throws Exception {51System.out.println(52"PLEASE DO NOT TOUCH KEYBOARD AND MOUSE DURING THE TEST RUN!");53// log.setLevel(java.util.logging.Level.FINE);54// log.setLevel(java.util.logging.Level.FINEST);55try {56SwingUtilities.invokeAndWait(new Runnable() {57public void run() {58frame = new JFrame();59frame.setUndecorated(true);60frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);61setup(frame);62}63});64testExitBeforeEnter();65System.out.println("Run random test in EDT");66runInEDT = true;67testRandomly();68System.out.println("Run random test in another thread");69runInEDT = false;70testRandomly();71System.out.println("ok");7273} finally {74SwingUtilities.invokeAndWait(new Runnable() {75@Override76public void run() {77frame.dispose();78}79});80}81}8283private static void testExitBeforeEnter() throws Exception {84final SecondaryLoop loop =85Toolkit.getDefaultToolkit().getSystemEventQueue()86.createSecondaryLoop();87loop.exit();88Robot robot = new Robot();89robot.mouseWheel(1);90robot.waitForIdle();91SwingUtilities.invokeAndWait(new Runnable() {92@Override93public void run() {94if(loop.enter()) {95throw new RuntimeException("Wrong enter() return value");96}97}98});99}100101private static void testRandomly() throws AWTException {102disorderCounter = 0;103final Robot robot = new Robot();104for (int i = 0; i < ATTEMPTS; i++) {105enterReturn = null;106exitReturn = null;107dispatchedEvents = 0;108synchronized (bug6980209.class) {109try {110for (int j = 0; j < EVENTS; j++) {111robot.keyPress(KeyEvent.VK_1);112robot.keyRelease(KeyEvent.VK_1);113}114115// trigger the button action that starts secondary loop116robot.keyPress(KeyEvent.VK_SPACE);117robot.keyRelease(KeyEvent.VK_SPACE);118119for (int j = 0; j < EVENTS; j++) {120robot.keyPress(KeyEvent.VK_1);121robot.keyRelease(KeyEvent.VK_1);122}123long time = System.nanoTime();124// wait for enter() returns125bug6980209.class.wait(1000);126if (enterReturn == null) {127System.out.println("wait time=" +128((System.nanoTime() - time) / 1E9) +129" seconds");130throw new RuntimeException(131"It seems the secondary loop will never end");132}133if (!enterReturn) disorderCounter++;134135robot.waitForIdle();136if (dispatchedEvents <1372 * EVENTS) { //check that all events are dispatched138throw new RuntimeException(139"KeyEvent.VK_1 has been lost!");140}141142} catch (InterruptedException e) {143throw new RuntimeException("Interrupted!");144}145}146}147if (disorderCounter == 0) {148System.out.println(149"Zero disordered enter/exit caught. It is recommended to run scenario again");150} else {151System.out.println(152"Disordered calls is " + disorderCounter + " from " +153ATTEMPTS);154}155}156157private static void setup(final JFrame frame) {158JButton jButton = new JButton("Button");159frame.getContentPane().add(jButton);160jButton.addActionListener(new bug6980209());161frame.pack();162frame.setVisible(true);163jButton.setFocusable(true);164jButton.requestFocus();165jButton.addKeyListener(new KeyListener() {166@Override167public void keyTyped(KeyEvent e) {168}169170@Override171public void keyPressed(KeyEvent e) {172if (e.getKeyChar() == '1') dispatchedEvents++;173}174175@Override176public void keyReleased(KeyEvent e) {177if (e.getKeyChar() == '1') dispatchedEvents++;178}179});180}181182183@Override184public void actionPerformed(ActionEvent e) {185if (runInEDT) {186runSecondaryLoop();187return;188}189new Thread("Secondary loop run thread") {190@Override191public void run() {192runSecondaryLoop();193}194}.start();195}196197private static void runSecondaryLoop() {198log.fine("\n---TEST START---");199200final SecondaryLoop loop =201Toolkit.getDefaultToolkit().getSystemEventQueue()202.createSecondaryLoop();203204final Object LOCK = new Object(); //lock to start simultaneously205Thread exitThread = new Thread("Exit thread") {206@Override207public void run() {208synchronized (LOCK) {209LOCK.notify();210}211Thread.yield();212exitReturn = loop.exit();213log.fine("exit() returns " + exitReturn);214}215};216217synchronized (LOCK) {218try {219exitThread.start();220LOCK.wait();221} catch (InterruptedException e1) {222throw new RuntimeException("What?");223}224}225226enterReturn = loop.enter();227log.fine("enter() returns " + enterReturn);228229try {230exitThread.join();231} catch (InterruptedException e) {232throw new RuntimeException("What?");233}234synchronized (bug6980209.class) {235bug6980209.class.notifyAll();236}237log.fine("\n---TEST END---");238}239}240241242