1 /*
   2  * Copyright (c) 2005, 2007, 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  * @bug 6541987
  27  * @summary Tests closing by ESC
  28  * @author Sergey Malenkov
  29  */
  30 
  31 import java.awt.AWTException;
  32 import java.awt.Color;
  33 import java.awt.Robot;
  34 import java.awt.Toolkit;
  35 import java.awt.Window;
  36 import java.awt.event.KeyEvent;
  37 
  38 import javax.swing.JColorChooser;
  39 import javax.swing.JFrame;
  40 import javax.swing.SwingUtilities;
  41 
  42 public class Test6541987 implements Runnable {
  43     private static Robot robot;
  44 
  45     public static void main(String[] args) throws AWTException {
  46         robot = new Robot();
  47         // test escape after selection
  48         start();
  49         click(KeyEvent.VK_ESCAPE);
  50         robot.waitForIdle();
  51         // test double escape after editing
  52         start();
  53         click(KeyEvent.VK_1);
  54         click(KeyEvent.VK_0);
  55         click(KeyEvent.VK_ESCAPE);
  56         click(KeyEvent.VK_ESCAPE);
  57         robot.waitForIdle();
  58         // all windows should be closed
  59         for (Window window : Window.getWindows()) {
  60             if (window.isVisible()) {
  61                 throw new Error("found visible window: " + window.getName());
  62             }
  63         }
  64     }
  65 
  66     private static void start() {
  67         SwingUtilities.invokeLater(new Test6541987());
  68         click(KeyEvent.VK_ALT, KeyEvent.VK_H);
  69         click(KeyEvent.VK_TAB);
  70         click(KeyEvent.VK_TAB);
  71         click(KeyEvent.VK_TAB);
  72         click(KeyEvent.VK_TAB);
  73     }
  74 
  75     private static void click(int...keys) {
  76         robot.waitForIdle();
  77         for (int key : keys) {
  78             robot.keyPress(key);
  79         }
  80         for (int key : keys) {
  81             robot.keyRelease(key);
  82         }
  83     }
  84 
  85     public void run() {
  86         String title = getClass().getName();
  87         JFrame frame = new JFrame(title);
  88         frame.setVisible(true);
  89 
  90         Color color = JColorChooser.showDialog(frame, title, Color.BLACK);
  91         if (color != null) {
  92             throw new Error("unexpected color: " + color);
  93         }
  94         frame.setVisible(false);
  95         frame.dispose();
  96     }
  97 }