Path: blob/master/test/jdk/javax/swing/JFileChooser/4525475/JFileChooserReadOnlyTest.java
66646 views
/*1* Copyright (c) 2004, 2022, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Component;24import java.awt.Container;25import java.util.Arrays;26import java.util.List;27import java.util.stream.Collectors;28import javax.swing.Action;29import javax.swing.JButton;30import javax.swing.JFileChooser;31import javax.swing.SwingUtilities;32import javax.swing.UIManager;33import javax.swing.UIManager.LookAndFeelInfo;34import javax.swing.UnsupportedLookAndFeelException;3536import static javax.swing.UIManager.getInstalledLookAndFeels;3738/*39* @test40* @key headful41* @bug 452547542* @summary This testcase tests JDK-4525475 bug fix, checks whether JFileChooser43* allows modification to the file-system by way of the "New Folder"44* button or not, ideally a read-only JFileChooser shouldn't allow it.45* @run main JFileChooserReadOnlyTest46*/47public class JFileChooserReadOnlyTest {4849private static volatile boolean result = true;50private static volatile boolean newFolderFound = false;5152public static void main(String[] args) throws Exception {5354List<String> lafs = Arrays.stream(getInstalledLookAndFeels())55.map(LookAndFeelInfo::getClassName)56.collect(Collectors.toList());57for (final String laf : lafs) {58if (!setLookAndFeel(laf)) {59continue;60}6162// Test1, Read Only JFileChooser63SwingUtilities.invokeAndWait(64() -> createAndTestCustomFileChooser(true));65System.out.println("It's a read-only JFileChooser " +66(newFolderFound ? "but it has" :67"and it doesn't have") +68" a New Folder Button found" +69", So the Test1 " +70(result ? "Passed" : "Failed") + " for " + laf);7172// Test2, Read/Write JFileChooser73/* Skipping Motif and Aqua L&Fs74For Motif L&F, the 'New Folder' button is never shown.75The Aqua L&F behaves similar to the native FileChooser:76the 'Open' dialog doesn't show the 'New Folder' button,77but it shows the button for the 'Save' dialog.78*/79if (!(laf.contains("Motif") || laf.contains("Aqua"))) {80SwingUtilities.invokeAndWait(81() -> createAndTestCustomFileChooser(false));82System.out.println("It's not a read-only JFileChooser " +83(newFolderFound ? "and it has" :84"but it doesn't have") +85" a New Folder Button" + ", So the Test2 " +86(result ? "Passed" : "Failed") + " for " +87laf);88}8990if (result) {91System.out.println("Test Passed for " + laf);92} else {93throw new RuntimeException(94"Test Failed, JFileChooser readOnly flag is not " +95"working properly for " + laf);96}97}98}99100private static void createAndTestCustomFileChooser(boolean readOnly) {101newFolderFound = false;102UIManager.put("FileChooser.readOnly", Boolean.valueOf(readOnly));103JFileChooser jfc = new JFileChooser();104checkNewFolderButton(jfc, readOnly);105result = result && (readOnly ^ newFolderFound);106}107108private static void checkNewFolderButton(Container c, boolean readOnly) {109int n = c.getComponentCount();110for (int i = 0; i < n && !newFolderFound; i++) {111Component comp = c.getComponent(i);112if (comp instanceof JButton) {113JButton b = (JButton) comp;114Action action = b.getAction();115if (action != null116&& "New Folder".equals(action.getValue(Action.NAME))) {117newFolderFound = true;118System.out.println(119"New Folder Button Found when readOnly = " +120readOnly);121}122} else if (comp instanceof Container) {123checkNewFolderButton((Container) comp, readOnly);124}125}126}127128private static boolean setLookAndFeel(String lafName) {129try {130UIManager.setLookAndFeel(lafName);131} catch (UnsupportedLookAndFeelException ignored) {132System.out.println("Ignoring Unsupported L&F: " + lafName);133return false;134} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {135throw new RuntimeException(e);136}137return true;138}139140}141142143