Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JFileChooser/4847375/bug4847375.java
38854 views
/*1* Copyright (c) 2010, 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 484737526* @summary JFileChooser Create New Folder button is disabled incorrectly27* @author Pavel Porvatov28*/2930import sun.awt.OSInfo;31import sun.awt.shell.ShellFolder;3233import javax.swing.*;34import java.awt.*;35import java.lang.reflect.Method;3637public class bug4847375 {38private final String newFolderToolTipText;3940private final String lookAndFeel;4142public static void main(String[] args) throws Exception {43if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {44System.out.println("The test is suitable only for Windows OS. Skipped.");4546return;47}4849SwingUtilities.invokeAndWait(new Runnable() {50public void run() {51new bug4847375("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");5253new bug4847375("javax.swing.plaf.metal.MetalLookAndFeel");54}55});56}5758private static Object[][] DIRECTORIES = new Object[][]{59{"getDesktop", Boolean.TRUE},60{"getDrives", Boolean.FALSE}, // My computer61{"getRecent", Boolean.TRUE},62{"getNetwork", Boolean.FALSE},63{"getPersonal", Boolean.TRUE},64};6566private bug4847375(String lookAndFeel) {67this.lookAndFeel = lookAndFeel;6869try {70UIManager.setLookAndFeel(lookAndFeel);71} catch (Exception e) {72fail("Cannot set LookAndFeel", e);73}7475JFileChooser fileChooser = new JFileChooser();7677// Find button NewFolder78newFolderToolTipText = UIManager.getString("FileChooser.newFolderToolTipText", fileChooser.getLocale());7980if (newFolderToolTipText == null || newFolderToolTipText.length() == 0) {81fail("Cannot find NewFolderButton in FileChooser (tooltip doesn't exist)");8283return;84}8586JButton newFolderButton = findNewFolderButton(fileChooser);8788if (newFolderButton == null) {89fail("Cannot find NewFolderButton in FileChooser");9091return;92}9394for (Object[] objects : DIRECTORIES) {95String getterName = (String) objects[0];96Boolean enabledNewFolder = (Boolean) objects[1];9798fileChooser.setCurrentDirectory(getWin32Folder(getterName));99100if (newFolderButton.isEnabled() != enabledNewFolder) {101fail("Enabled state of NewFolderButton should be " + enabledNewFolder +102" for Win32ShellFolderManager2." + getterName + "()");103}104}105}106107private JButton findNewFolderButton(Container container) {108JButton result = null;109110for (int i = 0; i < container.getComponentCount(); i++) {111Component c = container.getComponent(i);112113if (c instanceof JButton && newFolderToolTipText.equals(((JButton) c).getToolTipText())) {114if (result != null) {115fail("Two or more NewFolderButton found in FileChooser");116}117118result = (JButton) c;119}120121if (c instanceof Container) {122JButton button = findNewFolderButton((Container) c);123124if (result == null) {125result = button;126} else {127if (button != null) {128fail("Two or more NewFolderButton found in FileChooser");129}130}131}132}133134return result;135}136137private ShellFolder getWin32Folder(String getterName) {138try {139Class win32ShellFolderManager2 = Class.forName("sun.awt.shell.Win32ShellFolderManager2");140141Method method = win32ShellFolderManager2.getDeclaredMethod(getterName);142method.setAccessible(true);143144return (ShellFolder) method.invoke(null);145} catch (Exception e) {146fail("Cannot call '" + getterName + "' in the Win32ShellFolderManager2 class", e);147148return null;149}150}151152private void fail(String s) {153throw new RuntimeException("Test failed: " + s);154}155156private void fail(String s, Throwable e) {157throw new RuntimeException("Test failed for LookAndFeel " + lookAndFeel + ": " + s, e);158}159}160161162