bug4847375.java revision 3308
3308N/A/*
3308N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3308N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3308N/A *
3308N/A * This code is free software; you can redistribute it and/or modify it
3308N/A * under the terms of the GNU General Public License version 2 only, as
3308N/A * published by the Free Software Foundation.
3308N/A *
3308N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3308N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3308N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3308N/A * version 2 for more details (a copy is included in the LICENSE file that
3308N/A * accompanied this code).
3308N/A *
3308N/A * You should have received a copy of the GNU General Public License version
3308N/A * 2 along with this work; if not, write to the Free Software Foundation,
3308N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3308N/A *
3308N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3308N/A * or visit www.oracle.com if you need additional information or have any
3308N/A * questions.
3308N/A */
3308N/A
3308N/A/*
3308N/A * @test
3308N/A * @bug 4847375
3308N/A * @summary JFileChooser Create New Folder button is disabled incorrectly
3308N/A * @author Pavel Porvatov
3308N/A */
3308N/A
3308N/Aimport sun.awt.OSInfo;
3308N/Aimport sun.awt.shell.ShellFolder;
3308N/A
3308N/Aimport javax.swing.*;
3308N/Aimport java.awt.*;
3308N/Aimport java.lang.reflect.Method;
3308N/A
3308N/Apublic class bug4847375 {
3308N/A private final String newFolderToolTipText;
3308N/A
3308N/A private final String lookAndFeel;
3308N/A
3308N/A public static void main(String[] args) throws Exception {
3308N/A if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
3308N/A System.out.println("The test is suitable only for Windows OS. Skipped.");
3308N/A
3308N/A return;
3308N/A }
3308N/A
3308N/A SwingUtilities.invokeAndWait(new Runnable() {
3308N/A public void run() {
3308N/A new bug4847375("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
3308N/A
3308N/A new bug4847375("javax.swing.plaf.metal.MetalLookAndFeel");
3308N/A }
3308N/A });
3308N/A }
3308N/A
3308N/A private static Object[][] DIRECTORIES = new Object[][]{
3308N/A {"getDesktop", Boolean.TRUE},
3308N/A {"getDrives", Boolean.FALSE}, // My computer
3308N/A {"getRecent", Boolean.TRUE},
3308N/A {"getNetwork", Boolean.FALSE},
3308N/A {"getPersonal", Boolean.TRUE},
3308N/A };
3308N/A
3308N/A private bug4847375(String lookAndFeel) {
3308N/A this.lookAndFeel = lookAndFeel;
3308N/A
3308N/A try {
3308N/A UIManager.setLookAndFeel(lookAndFeel);
3308N/A } catch (Exception e) {
3308N/A fail("Cannot set LookAndFeel", e);
3308N/A }
3308N/A
3308N/A JFileChooser fileChooser = new JFileChooser();
3308N/A
3308N/A // Find button NewFolder
3308N/A newFolderToolTipText = UIManager.getString("FileChooser.newFolderToolTipText", fileChooser.getLocale());
3308N/A
3308N/A if (newFolderToolTipText == null || newFolderToolTipText.length() == 0) {
3308N/A fail("Cannot find NewFolderButton in FileChooser (tooltip doesn't exist)");
3308N/A
3308N/A return;
3308N/A }
3308N/A
3308N/A JButton newFolderButton = findNewFolderButton(fileChooser);
3308N/A
3308N/A if (newFolderButton == null) {
3308N/A fail("Cannot find NewFolderButton in FileChooser");
3308N/A
3308N/A return;
3308N/A }
3308N/A
3308N/A for (Object[] objects : DIRECTORIES) {
3308N/A String getterName = (String) objects[0];
3308N/A Boolean enabledNewFolder = (Boolean) objects[1];
3308N/A
3308N/A fileChooser.setCurrentDirectory(getWin32Folder(getterName));
3308N/A
3308N/A if (newFolderButton.isEnabled() != enabledNewFolder) {
3308N/A fail("Enabled state of NewFolderButton should be " + enabledNewFolder +
3308N/A " for Win32ShellFolderManager2." + getterName + "()");
3308N/A }
3308N/A }
3308N/A }
3308N/A
3308N/A private JButton findNewFolderButton(Container container) {
3308N/A JButton result = null;
3308N/A
3308N/A for (int i = 0; i < container.getComponentCount(); i++) {
3308N/A Component c = container.getComponent(i);
3308N/A
3308N/A if (c instanceof JButton && newFolderToolTipText.equals(((JButton) c).getToolTipText())) {
3308N/A if (result != null) {
3308N/A fail("Two or more NewFolderButton found in FileChooser");
3308N/A }
3308N/A
3308N/A result = (JButton) c;
3308N/A }
3308N/A
3308N/A if (c instanceof Container) {
3308N/A JButton button = findNewFolderButton((Container) c);
3308N/A
3308N/A if (result == null) {
3308N/A result = button;
3308N/A } else {
3308N/A if (button != null) {
3308N/A fail("Two or more NewFolderButton found in FileChooser");
3308N/A }
3308N/A }
3308N/A }
3308N/A }
3308N/A
3308N/A return result;
3308N/A }
3308N/A
3308N/A private ShellFolder getWin32Folder(String getterName) {
3308N/A try {
3308N/A Class win32ShellFolderManager2 = Class.forName("sun.awt.shell.Win32ShellFolderManager2");
3308N/A
3308N/A Method method = win32ShellFolderManager2.getDeclaredMethod(getterName);
3308N/A method.setAccessible(true);
3308N/A
3308N/A return (ShellFolder) method.invoke(null);
3308N/A } catch (Exception e) {
3308N/A fail("Cannot call '" + getterName + "' in the Win32ShellFolderManager2 class", e);
3308N/A
3308N/A return null;
3308N/A }
3308N/A }
3308N/A
3308N/A private void fail(String s) {
3308N/A throw new RuntimeException("Test failed: " + s);
3308N/A }
3308N/A
3308N/A private void fail(String s, Throwable e) {
3308N/A throw new RuntimeException("Test failed for LookAndFeel " + lookAndFeel + ": " + s, e);
3308N/A }
3308N/A}