Path: blob/master/test/jdk/javax/swing/JFileChooser/4847375/bug4847375.java
41153 views
/*1* Copyright (c) 2010, 2017, 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 4847375 817136326* @requires (os.family == "windows")27* @summary JFileChooser Create New Folder button is disabled incorrectly28* @author Pavel Porvatov29* @modules java.desktop/sun.awt30* java.desktop/sun.awt.shell:+open31*/3233import sun.awt.OSInfo;34import sun.awt.shell.ShellFolder;3536import javax.swing.*;37import java.awt.*;38import java.lang.reflect.Method;3940public class bug4847375 {41private final String newFolderToolTipText;4243private final String lookAndFeel;4445public static void main(String[] args) throws Exception {46if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {47System.out.println("The test is suitable only for Windows OS. Skipped.");4849return;50}5152SwingUtilities.invokeAndWait(new Runnable() {53public void run() {54new bug4847375("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");5556new bug4847375("javax.swing.plaf.metal.MetalLookAndFeel");57}58});59}6061private static Object[][] DIRECTORIES = new Object[][]{62{"getDesktop", Boolean.TRUE},63{"getDrives", Boolean.FALSE}, // My computer64{"getRecent", Boolean.TRUE},65{"getNetwork", Boolean.FALSE},66{"getPersonal", Boolean.TRUE},67};6869private bug4847375(String lookAndFeel) {70this.lookAndFeel = lookAndFeel;7172try {73UIManager.setLookAndFeel(lookAndFeel);74} catch (Exception e) {75fail("Cannot set LookAndFeel", e);76}7778JFileChooser fileChooser = new JFileChooser();7980// Find button NewFolder81newFolderToolTipText = UIManager.getString("FileChooser.newFolderToolTipText", fileChooser.getLocale());8283if (newFolderToolTipText == null || newFolderToolTipText.length() == 0) {84fail("Cannot find NewFolderButton in FileChooser (tooltip doesn't exist)");8586return;87}8889JButton newFolderButton = findNewFolderButton(fileChooser);9091if (newFolderButton == null) {92fail("Cannot find NewFolderButton in FileChooser");9394return;95}9697for (Object[] objects : DIRECTORIES) {98String getterName = (String) objects[0];99Boolean enabledNewFolder = (Boolean) objects[1];100101fileChooser.setCurrentDirectory(getWin32Folder(getterName));102103if (newFolderButton.isEnabled() != enabledNewFolder) {104fail("Enabled state of NewFolderButton should be " + enabledNewFolder +105" for Win32ShellFolderManager2." + getterName + "()");106}107}108}109110private JButton findNewFolderButton(Container container) {111JButton result = null;112113for (int i = 0; i < container.getComponentCount(); i++) {114Component c = container.getComponent(i);115116if (c instanceof JButton && newFolderToolTipText.equals(((JButton) c).getToolTipText())) {117if (result != null) {118fail("Two or more NewFolderButton found in FileChooser");119}120121result = (JButton) c;122}123124if (c instanceof Container) {125JButton button = findNewFolderButton((Container) c);126127if (result == null) {128result = button;129} else {130if (button != null) {131fail("Two or more NewFolderButton found in FileChooser");132}133}134}135}136137return result;138}139140private ShellFolder getWin32Folder(String getterName) {141try {142Class win32ShellFolderManager2 = Class.forName("sun.awt.shell.Win32ShellFolderManager2");143144Method method = win32ShellFolderManager2.getDeclaredMethod(getterName);145method.setAccessible(true);146147return (ShellFolder) method.invoke(null);148} catch (Exception e) {149fail("Cannot call '" + getterName + "' in the Win32ShellFolderManager2 class", e);150151return null;152}153}154155private void fail(String s) {156throw new RuntimeException("Test failed: " + s);157}158159private void fail(String s, Throwable e) {160throw new RuntimeException("Test failed for LookAndFeel " + lookAndFeel + ": " + s, e);161}162}163164165