|
| 1 | +/* |
| 2 | + * Copyright (c) 2007, 2024, 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 | +import java.awt.Button; |
| 25 | +import java.awt.FileDialog; |
| 26 | +import java.awt.GridBagConstraints; |
| 27 | +import java.awt.GridBagLayout; |
| 28 | +import java.awt.Insets; |
| 29 | +import java.awt.Label; |
| 30 | +import java.awt.TextField; |
| 31 | +import java.awt.Toolkit; |
| 32 | +import java.lang.reflect.InvocationTargetException; |
| 33 | +import javax.swing.JFrame; |
| 34 | +import javax.swing.JScrollPane; |
| 35 | +import javax.swing.JTextArea; |
| 36 | +import jtreg.SkippedException; |
| 37 | + |
| 38 | +/* |
| 39 | + * @test |
| 40 | + * @bug 6260676 |
| 41 | + * @summary FileDialog.setDirectory() does not work properly, XToolkit |
| 42 | + * @requires (os.family == "linux") |
| 43 | + * @library /java/awt/regtesthelpers |
| 44 | + * @library /test/lib |
| 45 | + * @build PassFailJFrame |
| 46 | + * @build jtreg.SkippedException |
| 47 | + * @run main/manual FileDialogReturnTest |
| 48 | + */ |
| 49 | + |
| 50 | +public class FileDialogReturnTest { |
| 51 | + |
| 52 | + private static JFrame initialize() { |
| 53 | + JFrame frame = new JFrame("File Dialog Return Test Frame"); |
| 54 | + GridBagConstraints gbc = new GridBagConstraints(); |
| 55 | + GridBagLayout grid = new GridBagLayout(); |
| 56 | + frame.setLayout(grid); |
| 57 | + JTextArea textOutput = new JTextArea(8, 30); |
| 58 | + textOutput.setLineWrap(true); |
| 59 | + JScrollPane textScrollPane = new JScrollPane(textOutput); |
| 60 | + gbc.insets = new Insets(5, 5, 5, 5); |
| 61 | + |
| 62 | + gbc.gridx = 0; |
| 63 | + gbc.gridy = 0; |
| 64 | + gbc.gridwidth = 1; |
| 65 | + gbc.fill = GridBagConstraints.WEST; |
| 66 | + frame.add(new Label("File:"), gbc); |
| 67 | + |
| 68 | + TextField fileField = new TextField("", 20); |
| 69 | + gbc.gridx = 1; |
| 70 | + gbc.gridy = 0; |
| 71 | + gbc.fill = GridBagConstraints.HORIZONTAL; |
| 72 | + frame.add(fileField, gbc); |
| 73 | + |
| 74 | + gbc.gridx = 0; |
| 75 | + gbc.gridy = 1; |
| 76 | + gbc.fill = GridBagConstraints.WEST; |
| 77 | + frame.add(new Label("Dir:"), gbc); |
| 78 | + |
| 79 | + TextField dirField = new TextField("", 20); |
| 80 | + gbc.gridx = 1; |
| 81 | + gbc.gridy = 1; |
| 82 | + gbc.fill = GridBagConstraints.HORIZONTAL; |
| 83 | + frame.add(dirField, gbc); |
| 84 | + |
| 85 | + Button button = new Button("Show"); |
| 86 | + gbc.gridx = 0; |
| 87 | + gbc.gridy = 2; |
| 88 | + gbc.gridwidth = 2; |
| 89 | + gbc.fill = GridBagConstraints.CENTER; |
| 90 | + frame.add(button, gbc); |
| 91 | + |
| 92 | + gbc.gridx = 0; |
| 93 | + gbc.gridy = 3; |
| 94 | + gbc.gridwidth = 2; |
| 95 | + frame.add(textScrollPane, gbc); |
| 96 | + |
| 97 | + button.addActionListener(e -> { |
| 98 | + FileDialog fd = new FileDialog(frame); |
| 99 | + fd.setFile(fileField.getText()); |
| 100 | + fd.setDirectory(dirField.getText()); |
| 101 | + fd.setVisible(true); |
| 102 | + |
| 103 | + textOutput.append("[file=" + fd.getFile()+"]\n"); |
| 104 | + textOutput.append("[dir=" + fd.getDirectory()+"]\n"); |
| 105 | + textOutput.setCaretPosition(textOutput.getText().length()); |
| 106 | + |
| 107 | + }); |
| 108 | + frame.pack(); |
| 109 | + return frame; |
| 110 | + } |
| 111 | + |
| 112 | + public static void main(String[] args) throws InterruptedException, |
| 113 | + InvocationTargetException { |
| 114 | + String instructions = """ |
| 115 | + 1) The test shows the 'File Dialog Return Test Frame' frame |
| 116 | + that contains two text fields, one button and an output area. |
| 117 | + 2) Input something into the 'File:' text field or just keep the field empty. |
| 118 | + 3) Input something into the 'Dir:' text field or just keep the field empty. |
| 119 | + 4) Press the 'Show' button and a file dialog will appear. |
| 120 | + 5-1) Cancel the file dialog, e.g. by selecting the 'close' menu item. |
| 121 | + If the output window shows that 'file'/'dir' values are null |
| 122 | + then the test passes, otherwise the test fails. |
| 123 | + 5-2) Select any file by double clicking on it. |
| 124 | + If the output window shows that 'file'/'dir' values are not-null |
| 125 | + then the test passes, otherwise the test fails. |
| 126 | + """; |
| 127 | + |
| 128 | + String toolkit = Toolkit.getDefaultToolkit().getClass().getName(); |
| 129 | + if (!toolkit.equals("sun.awt.X11.XToolkit")) { |
| 130 | + throw new SkippedException("Test is not designed for toolkit " + toolkit); |
| 131 | + } |
| 132 | + |
| 133 | + PassFailJFrame.builder() |
| 134 | + .title("File Dialog Return Test Instructions") |
| 135 | + .instructions(instructions) |
| 136 | + .rows((int) instructions.lines().count() + 1) |
| 137 | + .columns(50) |
| 138 | + .testUI(FileDialogReturnTest::initialize) |
| 139 | + .build() |
| 140 | + .awaitAndCheck(); |
| 141 | + } |
| 142 | +} |
0 commit comments