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