|
| 1 | +/* |
| 2 | + * Copyright (c) 2011, 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.FileDialog; |
| 25 | +import java.awt.Frame; |
| 26 | +import java.awt.Toolkit; |
| 27 | +import java.io.File; |
| 28 | +import java.io.IOException; |
| 29 | +import java.lang.reflect.InvocationTargetException; |
| 30 | +import javax.swing.JButton; |
| 31 | +import jtreg.SkippedException; |
| 32 | + |
| 33 | +/* |
| 34 | + * @test |
| 35 | + * @bug 6260659 |
| 36 | + * @summary File Name set programmatically in FileDialog is overridden during navigation, XToolkit |
| 37 | + * @requires (os.family == "linux") |
| 38 | + * @library /java/awt/regtesthelpers |
| 39 | + * @library /test/lib |
| 40 | + * @build PassFailJFrame |
| 41 | + * @build jtreg.SkippedException |
| 42 | + * @run main/manual FileNameOverrideTest |
| 43 | + */ |
| 44 | + |
| 45 | +public class FileNameOverrideTest { |
| 46 | + private final static String clickDirName = "Directory for double click"; |
| 47 | + |
| 48 | + private static JButton initialize() { |
| 49 | + final String fileName = "input"; |
| 50 | + final String dirPath = System.getProperty("user.dir");; |
| 51 | + |
| 52 | + JButton showBtn = new JButton("Show File Dialog"); |
| 53 | + showBtn.addActionListener(w -> { |
| 54 | + FileDialog fd = new FileDialog((Frame) null, "Open"); |
| 55 | + fd.setFile(fileName); |
| 56 | + fd.setDirectory(dirPath); |
| 57 | + fd.setVisible(true); |
| 58 | + String output = fd.getFile(); |
| 59 | + fd.dispose(); |
| 60 | + if (fileName.equals(output)) { |
| 61 | + PassFailJFrame.forcePass(); |
| 62 | + } else { |
| 63 | + PassFailJFrame.forceFail("File name mismatch: " |
| 64 | + + fileName + " vs " + output); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + try { |
| 69 | + File tmpFileUp = new File(dirPath + File.separator + fileName); |
| 70 | + File tmpDir = new File(dirPath + File.separator + clickDirName); |
| 71 | + File tmpFileIn = new File(tmpDir.getAbsolutePath() + File.separator + fileName); |
| 72 | + tmpDir.mkdir(); |
| 73 | + tmpFileUp.createNewFile(); |
| 74 | + tmpFileIn.createNewFile(); |
| 75 | + } catch (IOException ex) { |
| 76 | + throw new RuntimeException("Cannot create test folder", ex); |
| 77 | + } |
| 78 | + |
| 79 | + return showBtn; |
| 80 | + } |
| 81 | + |
| 82 | + public static void main(String[] args) throws InterruptedException, |
| 83 | + InvocationTargetException { |
| 84 | + String instructions = """ |
| 85 | + 1) Click on 'Show File Dialog' button. A file dialog will come up. |
| 86 | + 2) Double-click on '$clickDirName' and click OK or Open |
| 87 | + to confirm selection (label on the button depends on the current window manager). |
| 88 | + 3) Test will pass or fail automatically. |
| 89 | + """.replace("$clickDirName", clickDirName); |
| 90 | + |
| 91 | + String toolkit = Toolkit.getDefaultToolkit().getClass().getName(); |
| 92 | + if (!toolkit.equals("sun.awt.X11.XToolkit")) { |
| 93 | + throw new SkippedException("Test is not designed for toolkit " + toolkit); |
| 94 | + } |
| 95 | + |
| 96 | + PassFailJFrame.builder() |
| 97 | + .title("File Dialog Return Test Instructions") |
| 98 | + .instructions(instructions) |
| 99 | + .rows((int) instructions.lines().count() + 1) |
| 100 | + .columns(50) |
| 101 | + .splitUIBottom(FileNameOverrideTest::initialize) |
| 102 | + .build() |
| 103 | + .awaitAndCheck(); |
| 104 | + } |
| 105 | +} |
0 commit comments