Path: blob/master/test/jdk/javax/swing/JFileChooser/FileSystemView/SystemIconTest.java
41154 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) {75ImageIcon icon = (ImageIcon) fsv.getSystemIcon(file, size, size);7677//Enable below to see the icon78//JLabel label = new JLabel(icon);79//JOptionPane.showMessageDialog(null, label);8081if (icon == null) {82throw new RuntimeException("icon is null!!!");83}8485if (implComplete && icon.getIconWidth() != size) {86throw new RuntimeException("Wrong icon size " +87icon.getIconWidth() + " when requested " + size);88}8990if (icon.getImage() instanceof MultiResolutionImage) {91MultiResolutionImage mri = (MultiResolutionImage) icon.getImage();92if (mri.getResolutionVariant(size, size) == null) {93throw new RuntimeException("There is no suitable variant for the size "94+ size + " in the multi resolution icon");95}96} else {97if (implComplete) {98throw new RuntimeException("icon is supposed to be multi-resolution but it is not");99}100}101}102}103}104105106