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 /* @test
  25  * @bug 8065861
  26  * @summary Test to check pressing Escape key sets 'canceled' property of ProgressMonitor
  27  * @run main ProgressMonitorEscapeKeyPress
  28  */
  29 
  30 import java.awt.AWTException;
  31 import java.awt.EventQueue;
  32 import java.awt.Robot;
  33 import java.awt.event.KeyEvent;
  34 import javax.swing.JFrame;
  35 import javax.swing.ProgressMonitor;
  36 import javax.swing.SwingUtilities;
  37 
  38 public class ProgressMonitorEscapeKeyPress {
  39 
  40     static ProgressMonitor monitor;
  41     static int counter = 0;
  42     static TestThread robotThread;
  43     static JFrame frame;
  44 
  45 
  46     public static void main(String[] args) throws Exception {
  47 
  48         createTestUI();
  49 
  50         monitor = new ProgressMonitor(frame, "Progress", null, 0, 100);
  51 
  52         robotThread = new TestThread();
  53         robotThread.start();
  54 
  55         for (counter = 0; counter <= 100; counter += 10) {
  56             Thread.sleep(1000);
  57 
  58             EventQueue.invokeAndWait(new Runnable() {
  59                 @Override
  60                 public void run() {
  61                     if (!monitor.isCanceled()) {
  62                         monitor.setProgress(counter);
  63                         System.out.println("Progress bar is in progress");
  64                     }
  65                 }
  66             });
  67 
  68             if (monitor.isCanceled()) {
  69                 break;
  70             }
  71         }
  72 
  73         disposeTestUI();
  74 
  75         if (counter >= monitor.getMaximum()) {
  76             throw new RuntimeException("Escape key did not cancel the ProgressMonitor");
  77         }
  78     }
  79 
  80      private static void createTestUI() throws Exception {
  81         SwingUtilities.invokeAndWait(new Runnable() {
  82            @Override
  83            public void run() {
  84                 frame = new JFrame("Test");
  85                 frame.setSize(300, 300);
  86                 frame.setLocationByPlatform(true);
  87                 frame.setVisible(true);
  88               }});
  89      }
  90 
  91 
  92      private static void disposeTestUI() throws Exception {
  93            SwingUtilities.invokeAndWait(() -> {
  94                frame.dispose();
  95            });
  96        }
  97 }
  98 
  99 
 100 class TestThread extends Thread {
 101 
 102     Robot testRobot;
 103 
 104     TestThread() throws AWTException {
 105         super();
 106         testRobot = new Robot();
 107     }
 108 
 109     @Override
 110     public void run() {
 111         try {
 112             // Sleep for 5 seconds - so that the ProgressMonitor starts
 113             Thread.sleep(5000);
 114 
 115             // Press and release Escape key
 116             testRobot.keyPress(KeyEvent.VK_ESCAPE);
 117             testRobot.delay(20);
 118             testRobot.keyRelease(KeyEvent.VK_ESCAPE);
 119         } catch (InterruptedException ex) {
 120             throw new RuntimeException("Exception in TestThread");
 121         }
 122     }
 123 }
 124