Path: blob/master/test/jdk/javax/swing/JFileChooser/FileSystemView/SystemIconTest.java
66646 views
/*1* Copyright (c) 2021, 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*/2223/*24* @test25* @bug 818204326* @summary Access to Windows Large Icons27* @run main SystemIconTest28*/29import javax.swing.Icon;30import javax.swing.ImageIcon;31import javax.swing.filechooser.FileSystemView;32import java.awt.image.MultiResolutionImage;33import java.io.File;3435public class SystemIconTest {36static final FileSystemView fsv = FileSystemView.getFileSystemView();3738public static void main(String[] args) {39testSystemIcon();40negativeTests();41}4243static void testSystemIcon() {44String os = System.getProperty("os.name");45if (os.startsWith("Windows")) {46String windir = System.getenv("windir");47testSystemIcon(new File(windir), true);48testSystemIcon(new File(windir + "/explorer.exe"),49true);50} else {51String homedir = System.getProperty("user.home");52testSystemIcon(new File(homedir), false);53}54}5556static void negativeTests() {57Icon icon;58try {59icon = fsv.getSystemIcon(new File("."), -1, 16);60throw new RuntimeException("Negative size icon should throw invalid argument exception");61} catch (IllegalArgumentException iae) {62// Expected63}6465icon = fsv.getSystemIcon(new File("thereisdefinitelynosuchfile.why"),6616, 16);67if (icon != null) {68throw new RuntimeException("Icons for files with invalid names should be null");69}70}7172static void testSystemIcon(File file, boolean implComplete) {73int[] sizes = new int[] {16, 32, 48, 64, 128};74for (int size : sizes) {75Icon i = fsv.getSystemIcon(file, size, size);7677if (i == null) {78throw new RuntimeException(file.getAbsolutePath() + " icon is null");79}8081if (!(i instanceof ImageIcon)) {82// Default UI resource icon returned - it is not covered83// by new implementation so we can not test it84continue;85}8687ImageIcon icon = (ImageIcon) i;88//Enable below to see the icon89//JLabel label = new JLabel(icon);90//JOptionPane.showMessageDialog(null, label);9192if (implComplete && icon.getIconWidth() != size) {93throw new RuntimeException("Wrong icon size " +94icon.getIconWidth() + " when requested " + size +95" for file " + file.getAbsolutePath());96}9798if (icon.getImage() instanceof MultiResolutionImage) {99MultiResolutionImage mri = (MultiResolutionImage) icon.getImage();100if (mri.getResolutionVariant(size, size) == null) {101throw new RuntimeException("There is no suitable variant for the size "102+ size + " in the multi resolution icon " + file.getAbsolutePath());103}104} else {105if (implComplete) {106throw new RuntimeException("icon is supposed to be" +107" multi-resolution but it is not for " + file.getAbsolutePath());108}109}110}111}112}113114115