Path: blob/master/test/jdk/javax/swing/JFileChooser/TestJFileChooserNewFolderAction.java
41152 views
/*1* Copyright (c) 2020, 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* @key headful26* @bug 823104227* @requires (os.family == "mac")28* @summary Verify that JFileChooser's New Folder option works correctly29* in AquaLookAndFeel30* @run main/manual TestJFileChooserNewFolderAction31*/3233import javax.swing.JFrame;34import javax.swing.JTextArea;35import javax.swing.JPanel;36import javax.swing.SwingUtilities;37import javax.swing.JButton;38import javax.swing.JFileChooser;39import javax.swing.UIManager;40import javax.swing.UnsupportedLookAndFeelException;41import java.awt.Color;42import java.awt.GridBagLayout;43import java.awt.GridBagConstraints;44import java.awt.Insets;45import java.awt.event.ActionEvent;46import java.util.concurrent.CountDownLatch;47import java.util.concurrent.TimeUnit;4849public class TestJFileChooserNewFolderAction {50private static final JFrame instructionFrame = new JFrame();51private static final String lafName = "AquaLookAndFeel";52private static volatile boolean testPassed = false;53private static volatile CountDownLatch countDownLatch;5455private static final String INSTRUCTIONS = "INSTRUCTIONS:\n\n" +56"Make sure there is no folder named \"uninitializedValue\" in\n"+57"JFileChooser directory, else change JFileChooser directory.\n"+58"Click on \"New Folder\" button in JFileChooser. \n" +59"A JOptionPane will be opened to write the name of the folder.\n"+60"Press \"ESC\" key to close the JOptionPane without creating \n"+61"new folder. Verify that JFileChooser current directory has not\n"+62"changed and there is no folder named \"uninitializedValue\"\n"+63"created in the JFileChooser directory.\n" +64"If yes, Press Pass, Otherwise, Press Fail.\n";656667public static void main(String[] args) throws Exception{6869if (!System.getProperty("os.name").startsWith("Mac")) {70System.out.println("This test is meant for Mac platform only");71return;72}7374countDownLatch = new CountDownLatch(1);75for (UIManager.LookAndFeelInfo lookAndFeelInfo :76UIManager.getInstalledLookAndFeels()) {77if (lookAndFeelInfo.getClassName().contains(lafName)) {78try {79UIManager.setLookAndFeel(lookAndFeelInfo.getClassName());80} catch (final UnsupportedLookAndFeelException ignored) {81System.out.println("Aqua L&F could not be set, so this " +82"test can not be run in this scenario ");83return;84}85}86}8788SwingUtilities.invokeAndWait(TestJFileChooserNewFolderAction::createInstructionUI);8990SwingUtilities.invokeAndWait(TestJFileChooserNewFolderAction::createTestUI);9192countDownLatch.await(15, TimeUnit.MINUTES);9394SwingUtilities.invokeAndWait(TestJFileChooserNewFolderAction::disposeUI);95if (!testPassed) {96throw new RuntimeException("Test failed!");97}98}99100private static void createInstructionUI() {101GridBagLayout layout = new GridBagLayout();102JPanel mainControlPanel = new JPanel(layout);103JPanel resultButtonPanel = new JPanel(layout);104105GridBagConstraints gbc = new GridBagConstraints();106107gbc.gridx = 0;108gbc.gridy = 0;109gbc.insets = new Insets(5, 15, 5, 15);110gbc.fill = GridBagConstraints.HORIZONTAL;111112JTextArea instructionTextArea = new JTextArea();113instructionTextArea.setText(INSTRUCTIONS);114instructionTextArea.setEditable(false);115instructionTextArea.setBackground(Color.white);116mainControlPanel.add(instructionTextArea, gbc);117118JButton passButton = new JButton("Pass");119passButton.setActionCommand("Pass");120passButton.addActionListener((ActionEvent e) -> {121testPassed = true;122countDownLatch.countDown();123124});125126JButton failButton = new JButton("Fail");127failButton.setActionCommand("Fail");128failButton.addActionListener(e -> {129countDownLatch.countDown();130});131132gbc.gridx = 0;133gbc.gridy = 0;134135resultButtonPanel.add(passButton, gbc);136137gbc.gridx = 1;138gbc.gridy = 0;139resultButtonPanel.add(failButton, gbc);140141gbc.gridx = 0;142gbc.gridy = 2;143mainControlPanel.add(resultButtonPanel, gbc);144145instructionFrame.pack();146instructionFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);147instructionFrame.add(mainControlPanel);148instructionFrame.pack();149instructionFrame.setVisible(true);150}151152private static void createTestUI() {153new JFileChooser().showSaveDialog(null);154}155156private static void disposeUI() {157instructionFrame.dispose();158}159}160161162