|
| 1 | +/* |
| 2 | + * Copyright (c) 2004, 2022, 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.Component; |
| 25 | +import java.awt.Container; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.List; |
| 28 | +import java.util.stream.Collectors; |
| 29 | +import javax.swing.Action; |
| 30 | +import javax.swing.JButton; |
| 31 | +import javax.swing.JFileChooser; |
| 32 | +import javax.swing.SwingUtilities; |
| 33 | +import javax.swing.UIManager; |
| 34 | +import javax.swing.UIManager.LookAndFeelInfo; |
| 35 | +import javax.swing.UnsupportedLookAndFeelException; |
| 36 | + |
| 37 | +import static javax.swing.UIManager.getInstalledLookAndFeels; |
| 38 | + |
| 39 | +/* |
| 40 | + * @test |
| 41 | + * @key headful |
| 42 | + * @bug 4525475 |
| 43 | + * @summary This testcase tests JDK-4525475 bug fix, checks whether JFileChooser |
| 44 | + * allows modification to the file-system by way of the "New Folder" |
| 45 | + * button or not, ideally a read-only JFileChooser shouldn't allow it. |
| 46 | + * @run main JFileChooserReadOnlyTest |
| 47 | + */ |
| 48 | +public class JFileChooserReadOnlyTest { |
| 49 | + |
| 50 | + private static volatile boolean result = true; |
| 51 | + private static volatile boolean newFolderFound = false; |
| 52 | + |
| 53 | + public static void main(String[] args) throws Exception { |
| 54 | + |
| 55 | + List<String> lafs = Arrays.stream(getInstalledLookAndFeels()) |
| 56 | + .map(LookAndFeelInfo::getClassName) |
| 57 | + .collect(Collectors.toList()); |
| 58 | + for (final String laf : lafs) { |
| 59 | + if (!setLookAndFeel(laf)) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + // Test1, Read Only JFileChooser |
| 64 | + SwingUtilities.invokeAndWait( |
| 65 | + () -> createAndTestCustomFileChooser(true)); |
| 66 | + System.out.println("It's a read-only JFileChooser " + |
| 67 | + (newFolderFound ? "but it has" : |
| 68 | + "and it doesn't have") + |
| 69 | + " a New Folder Button found" + |
| 70 | + ", So the Test1 " + |
| 71 | + (result ? "Passed" : "Failed") + " for " + laf); |
| 72 | + |
| 73 | + // Test2, Read/Write JFileChooser |
| 74 | + /* Skipping Motif and Aqua L&Fs |
| 75 | + For Motif L&F, the 'New Folder' button is never shown. |
| 76 | + The Aqua L&F behaves similar to the native FileChooser: |
| 77 | + the 'Open' dialog doesn't show the 'New Folder' button, |
| 78 | + but it shows the button for the 'Save' dialog. |
| 79 | + */ |
| 80 | + if (!(laf.contains("Motif") || laf.contains("Aqua"))) { |
| 81 | + SwingUtilities.invokeAndWait( |
| 82 | + () -> createAndTestCustomFileChooser(false)); |
| 83 | + System.out.println("It's not a read-only JFileChooser " + |
| 84 | + (newFolderFound ? "and it has" : |
| 85 | + "but it doesn't have") + |
| 86 | + " a New Folder Button" + ", So the Test2 " + |
| 87 | + (result ? "Passed" : "Failed") + " for " + |
| 88 | + laf); |
| 89 | + } |
| 90 | + |
| 91 | + if (result) { |
| 92 | + System.out.println("Test Passed for " + laf); |
| 93 | + } else { |
| 94 | + throw new RuntimeException( |
| 95 | + "Test Failed, JFileChooser readOnly flag is not " + |
| 96 | + "working properly for " + laf); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static void createAndTestCustomFileChooser(boolean readOnly) { |
| 102 | + newFolderFound = false; |
| 103 | + UIManager.put("FileChooser.readOnly", Boolean.valueOf(readOnly)); |
| 104 | + JFileChooser jfc = new JFileChooser(); |
| 105 | + checkNewFolderButton(jfc, readOnly); |
| 106 | + result = result && (readOnly ^ newFolderFound); |
| 107 | + } |
| 108 | + |
| 109 | + private static void checkNewFolderButton(Container c, boolean readOnly) { |
| 110 | + int n = c.getComponentCount(); |
| 111 | + for (int i = 0; i < n && !newFolderFound; i++) { |
| 112 | + Component comp = c.getComponent(i); |
| 113 | + if (comp instanceof JButton) { |
| 114 | + JButton b = (JButton) comp; |
| 115 | + Action action = b.getAction(); |
| 116 | + if (action != null |
| 117 | + && "New Folder".equals(action.getValue(Action.NAME))) { |
| 118 | + newFolderFound = true; |
| 119 | + System.out.println( |
| 120 | + "New Folder Button Found when readOnly = " + |
| 121 | + readOnly); |
| 122 | + } |
| 123 | + } else if (comp instanceof Container) { |
| 124 | + checkNewFolderButton((Container) comp, readOnly); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static boolean setLookAndFeel(String lafName) { |
| 130 | + try { |
| 131 | + UIManager.setLookAndFeel(lafName); |
| 132 | + } catch (UnsupportedLookAndFeelException ignored) { |
| 133 | + System.out.println("Ignoring Unsupported L&F: " + lafName); |
| 134 | + return false; |
| 135 | + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { |
| 136 | + throw new RuntimeException(e); |
| 137 | + } |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments