|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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.io.File; |
| 25 | +import java.util.List; |
| 26 | +import javax.swing.Icon; |
| 27 | +import javax.swing.JFileChooser; |
| 28 | +import javax.swing.JFrame; |
| 29 | +import javax.swing.SwingUtilities; |
| 30 | +import javax.swing.UIManager; |
| 31 | +import javax.swing.filechooser.FileSystemView; |
| 32 | + |
| 33 | +/* |
| 34 | + * @test id=metal |
| 35 | + * @bug 8139228 |
| 36 | + * @summary JFileChooser should not render Directory names in HTML format |
| 37 | + * @library /java/awt/regtesthelpers |
| 38 | + * @build PassFailJFrame |
| 39 | + * @run main/manual HTMLFileName metal |
| 40 | + */ |
| 41 | + |
| 42 | +/* |
| 43 | + * @test id=system |
| 44 | + * @bug 8139228 |
| 45 | + * @summary JFileChooser should not render Directory names in HTML format |
| 46 | + * @library /java/awt/regtesthelpers |
| 47 | + * @build PassFailJFrame |
| 48 | + * @run main/manual HTMLFileName system |
| 49 | + */ |
| 50 | + |
| 51 | +public class HTMLFileName { |
| 52 | + private static final String INSTRUCTIONS = """ |
| 53 | + <html> |
| 54 | + <ol> |
| 55 | + <li>FileChooser shows up a virtual directory and file with name |
| 56 | + <html><h1 color=#ff00ff><font face="Comic Sans MS">Swing Rocks!. |
| 57 | + <li>On "HTML disabled" frame : |
| 58 | + <ol> |
| 59 | + <li>Verify that the folder and file name must be plain text. |
| 60 | + <li>If the name in file pane window and also in directory |
| 61 | + ComboBox remains in plain text, then press <b>Pass</b>. |
| 62 | + If it appears to be in HTML format with Pink color as |
| 63 | + shown, then press <b>Fail</b>. |
| 64 | + </ol> |
| 65 | +
|
| 66 | + <li>On "HTML enabled" frame : |
| 67 | + <ol> |
| 68 | + <li>Verify that the folder and file name remains in HTML |
| 69 | + format with name "Swing Rocks!" pink in color as shown. |
| 70 | + <li>If the name in file pane window and also in directory |
| 71 | + ComboBox remains in HTML format string, then press <b>Pass</b>. |
| 72 | + If it appears to be in plain text, then press <b>Fail</b>. |
| 73 | + </ol> |
| 74 | + </ol> |
| 75 | + </html> |
| 76 | + """; |
| 77 | + |
| 78 | + public static void main(String[] args) throws Exception { |
| 79 | + if (args.length < 1) { |
| 80 | + throw new IllegalArgumentException("Look-and-Feel keyword is required"); |
| 81 | + } |
| 82 | + |
| 83 | + final String lafClassName; |
| 84 | + switch (args[0]) { |
| 85 | + case "metal" -> lafClassName = UIManager.getCrossPlatformLookAndFeelClassName(); |
| 86 | + case "system" -> lafClassName = UIManager.getSystemLookAndFeelClassName(); |
| 87 | + default -> throw new IllegalArgumentException("Unsupported Look-and-Feel keyword: " + args[0]); |
| 88 | + } |
| 89 | + |
| 90 | + SwingUtilities.invokeAndWait(() -> { |
| 91 | + try { |
| 92 | + UIManager.setLookAndFeel(lafClassName); |
| 93 | + } catch (Exception e) { |
| 94 | + throw new RuntimeException(e); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + System.out.println("Test for LookAndFeel " + lafClassName); |
| 99 | + PassFailJFrame.builder() |
| 100 | + .instructions(INSTRUCTIONS) |
| 101 | + .columns(45) |
| 102 | + .testUI(HTMLFileName::initialize) |
| 103 | + .positionTestUIBottomRowCentered() |
| 104 | + .build() |
| 105 | + .awaitAndCheck(); |
| 106 | + System.out.println("Test passed for LookAndFeel " + lafClassName); |
| 107 | + } |
| 108 | + |
| 109 | + private static List<JFrame> initialize() { |
| 110 | + return List.of(createFileChooser(true), createFileChooser(false)); |
| 111 | + } |
| 112 | + |
| 113 | + private static JFrame createFileChooser(boolean htmlEnabled) { |
| 114 | + JFileChooser jfc = new JFileChooser(new VirtualFileSystemView()); |
| 115 | + jfc.putClientProperty("html.disable", htmlEnabled); |
| 116 | + jfc.setControlButtonsAreShown(false); |
| 117 | + |
| 118 | + JFrame frame = new JFrame((htmlEnabled) ? "HTML enabled" : "HTML disabled"); |
| 119 | + frame.add(jfc); |
| 120 | + frame.pack(); |
| 121 | + return frame; |
| 122 | + } |
| 123 | + |
| 124 | + private static class VirtualFileSystemView extends FileSystemView { |
| 125 | + @Override |
| 126 | + public File createNewFolder(File containingDir) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public File[] getRoots() { |
| 132 | + return new File[]{ |
| 133 | + new File("/", "<html><h1 color=#ff00ff><font " + |
| 134 | + "face=\"Comic Sans MS\">Swing Rocks!"), |
| 135 | + new File("/", "virtualFile2.txt"), |
| 136 | + new File("/", "virtualFolder") |
| 137 | + }; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public File getHomeDirectory() { |
| 142 | + return new File("/"); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public File getDefaultDirectory() { |
| 147 | + return new File("/"); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public File[] getFiles(File dir, boolean useFileHiding) { |
| 152 | + // Simulate a virtual folder structure |
| 153 | + return new File[]{ |
| 154 | + new File("/", "<html><h1 color=#ff00ff><font " + |
| 155 | + "face=\"Comic Sans MS\">Swing Rocks!"), |
| 156 | + new File(dir, "virtualFile2.txt"), |
| 157 | + new File(dir, "virtualFolder") |
| 158 | + }; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public Icon getSystemIcon(File f) { |
| 163 | + return null; |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments