|
| 1 | +/* |
| 2 | + * Copyright 2025 JetBrains s.r.o. |
| 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 | +import javax.swing.JButton; |
| 25 | +import javax.swing.JFrame; |
| 26 | +import javax.swing.JLabel; |
| 27 | +import javax.swing.JOptionPane; |
| 28 | +import javax.swing.JPanel; |
| 29 | +import javax.swing.SwingUtilities; |
| 30 | +import java.awt.GridLayout; |
| 31 | +import java.awt.event.WindowAdapter; |
| 32 | +import java.awt.event.WindowEvent; |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.concurrent.CompletableFuture; |
| 35 | + |
| 36 | +import jdk.test.lib.process.ProcessTools; |
| 37 | + |
| 38 | +/** |
| 39 | + * @test |
| 40 | + * @summary Verifies cancelling window close operation does not close the window |
| 41 | + * @requires os.family == "linux" |
| 42 | + * @library /test/lib |
| 43 | + * @key headful |
| 44 | + * @run main/manual ConfirmWindowClose |
| 45 | + */ |
| 46 | +public class ConfirmWindowClose { |
| 47 | + static final CompletableFuture<RuntimeException> swingError = new CompletableFuture<>(); |
| 48 | + static Process testProcess; |
| 49 | + |
| 50 | + public static void main(String[] args) throws Exception { |
| 51 | + if (args.length > 0) { |
| 52 | + SwingUtilities.invokeLater(ConfirmWindowClose::showTestUI); |
| 53 | + } else { |
| 54 | + try { |
| 55 | + SwingUtilities.invokeAndWait(ConfirmWindowClose::showControlUI); |
| 56 | + swingError.get(); |
| 57 | + } finally { |
| 58 | + if (testProcess != null) { |
| 59 | + testProcess.destroy(); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private static void showTestUI() { |
| 66 | + JFrame frame = new JFrame("ConfirmWindowClose Test"); |
| 67 | + frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); |
| 68 | + |
| 69 | + JLabel l = new JLabel("<html><h1>INSTRUCTIONS</h1><p>Click on the close button on the window.</p></html>"); |
| 70 | + frame.getContentPane().add(l); |
| 71 | + |
| 72 | + frame.addWindowListener(new WindowAdapter() { |
| 73 | + @Override |
| 74 | + public void windowClosing(WindowEvent e) { |
| 75 | + int result = JOptionPane.showConfirmDialog( |
| 76 | + frame, |
| 77 | + "Do you really want to close it? (click No first)", |
| 78 | + "Confirm Exit", |
| 79 | + JOptionPane.YES_NO_CANCEL_OPTION, |
| 80 | + JOptionPane.QUESTION_MESSAGE |
| 81 | + ); |
| 82 | + |
| 83 | + if (result == JOptionPane.YES_OPTION) { |
| 84 | + frame.dispose(); |
| 85 | + } |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + frame.pack(); |
| 90 | + frame.setVisible(true); |
| 91 | + } |
| 92 | + |
| 93 | + private static void showControlUI() { |
| 94 | + JFrame frame = new JFrame("ConfirmWindow Control Frame"); |
| 95 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 96 | + |
| 97 | + JPanel content = new JPanel(); |
| 98 | + var layout = new GridLayout(4, 1, 10, 10); |
| 99 | + content.setLayout(layout); |
| 100 | + JButton runButton = new JButton("Launch Test"); |
| 101 | + runButton.addActionListener(e -> { |
| 102 | + ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(ConfirmWindowClose.class.getName(), "runTest"); |
| 103 | + try { |
| 104 | + testProcess = pb.start(); |
| 105 | + } catch (IOException ex) { |
| 106 | + swingError.complete(new RuntimeException(ex)); |
| 107 | + throw new RuntimeException(ex); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + JButton passButton = new JButton("Pass"); |
| 112 | + passButton.addActionListener(e -> {swingError.complete(null);}); |
| 113 | + |
| 114 | + JButton failButton = new JButton("Fail"); |
| 115 | + failButton.addActionListener(e -> {swingError.completeExceptionally(new RuntimeException("The tester has pressed FAILED"));}); |
| 116 | + |
| 117 | + content.add(runButton); |
| 118 | + content.add(failButton); |
| 119 | + content.add(passButton); |
| 120 | + content.add(new JLabel("<html><center><h1>INSTRUCTIONS</h1></center>" + |
| 121 | + "<p>Press Launch Test</p>" + |
| 122 | + "<p>In the 'ConfirmWindowClose Test' window that appears, click the window close icon on the titlebar.</p>" + |
| 123 | + "<p>When the confirmation dialog appears click No.</p>" + |
| 124 | + "<p>Make sure that the 'ConfirmWindowClose Test' has not disappeared.</p>" + |
| 125 | + "<p>If the window has not disappeared, press Pass here; otherwise, press Fail.</p></html>")); |
| 126 | + frame.setContentPane(content); |
| 127 | + frame.pack(); |
| 128 | + frame.setVisible(true); |
| 129 | + } |
| 130 | +} |
0 commit comments