1 /*
   2  * Copyright (c) 2016, 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 8165555
  28  * @summary VM crash after creating Robot second time and accessing key codes in
  29  *          single JVM mode.
  30  * @run main RobotCrash
  31  */
  32 import java.awt.Frame;
  33 import java.awt.Point;
  34 import java.awt.Robot;
  35 import java.awt.event.InputEvent;
  36 import java.awt.event.KeyEvent;
  37 import javax.swing.SwingUtilities;
  38 
  39 public class RobotCrash implements Runnable {
  40 
  41     private Frame frame;
  42 
  43     public void robotKeyPressTest() throws Exception {
  44 
  45         SwingUtilities.invokeAndWait(() -> {
  46             frame = new Frame();
  47             frame.setSize(300, 300);
  48             frame.setVisible(true);
  49         });
  50 
  51         Robot robot = new Robot();
  52         robot.waitForIdle();
  53         Point pt = frame.getLocationOnScreen();
  54         robot.mouseMove(((int) pt.getX() + frame.getWidth()) / 2,
  55                 ((int) pt.getY() + frame.getHeight()) / 2);
  56         robot.waitForIdle();
  57         robot.mousePress(InputEvent.BUTTON1_MASK);
  58         robot.waitForIdle();
  59         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  60         robot.waitForIdle();
  61         robot.keyPress(KeyEvent.VK_ENTER);
  62         robot.waitForIdle();
  63         robot.keyRelease(KeyEvent.VK_ENTER);
  64         robot.waitForIdle();
  65 
  66         SwingUtilities.invokeAndWait(() -> {
  67             frame.dispose();
  68         });
  69     }
  70 
  71     @Override
  72     public void run() {
  73         try {
  74             robotKeyPressTest();
  75         } catch (Exception e) {
  76             throw new RuntimeException("Test Failed" + e.getMessage());
  77         }
  78     }
  79 
  80     public static void main(String[] args) throws Exception {
  81 
  82         for (int i = 0; i < 10; i++) {
  83             Thread t1 = new Thread(new RobotCrash());
  84             t1.start();
  85             t1.join();
  86             Thread t2 = new Thread(new RobotCrash());
  87             t2.start();
  88             t2.join();
  89             Thread.sleep(1000);
  90         }
  91     }
  92 }