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