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