Path: blob/master/test/jdk/javax/swing/JFileChooser/ExternalDriveNameTest.java
41149 views
/*1* Copyright (c) 2018, 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 819195727* @requires (os.family == "Windows")28* @summary Check Verifies that external drive names are shown properly when29* "Desktop" is selected in JFileChooser30* @run main/manual ExternalDriveNameTest31*/32import java.awt.Color;33import java.awt.GridBagConstraints;34import java.awt.GridBagLayout;35import java.util.concurrent.CountDownLatch;36import javax.swing.JPanel;37import javax.swing.JTextArea;38import javax.swing.SwingUtilities;39import javax.swing.JButton;40import javax.swing.JFrame;41import java.awt.event.ActionEvent;42import java.awt.event.ActionListener;43import java.util.concurrent.TimeUnit;44import javax.swing.JFileChooser;4546public class ExternalDriveNameTest {4748public static void main(String args[]) throws Exception {49final CountDownLatch latch = new CountDownLatch(1);50TestUI test = new TestUI(latch);51SwingUtilities.invokeAndWait(() -> {52try {53test.createUI();54} catch (Exception ex) {55throw new RuntimeException("Exception while creating UI");56}57});5859boolean status = latch.await(5, TimeUnit.MINUTES);6061if (!status) {62System.out.println("Test timed out.");63}6465SwingUtilities.invokeAndWait(() -> {66try {67test.disposeUI();68} catch (Exception ex) {69throw new RuntimeException("Exception while disposing UI");70}71});7273if (test.testResult == false) {74throw new RuntimeException("Test Failed.");75}76}77}7879class TestUI {8081private static JFrame mainFrame;82private static JPanel mainControlPanel;8384private static JTextArea instructionTextArea;8586private static JPanel resultButtonPanel;87private static JButton passButton;88private static JButton failButton;8990private static GridBagLayout layout;91private final CountDownLatch latch;92public boolean testResult = false;9394public TestUI(CountDownLatch latch) throws Exception {95this.latch = latch;96}9798public final void createUI() throws Exception {99mainFrame = new JFrame("JFileChooser_ExternalDriveNameTest");100101layout = new GridBagLayout();102mainControlPanel = new JPanel(layout);103resultButtonPanel = new JPanel(layout);104105GridBagConstraints gbc = new GridBagConstraints();106107// Create Test instructions108String instructions109= "INSTRUCTIONS:" +110"\n 1. This is a Windows 10 specific test. If you are not on " +111"Windows 10, press Pass." +112"\n 2. Make sure you have an External Drive attached to your " +113"computer." +114"\n 3. Open a JFileChooser by clicking on launch button." +115"\n 4. In JFileChooser dropdown, there are two Desktop " +116"locations." +117"\n 5. One Desktop is child of My PC and one is parent of it." +118"\n 6. Open the parent Desktop folder." +119"\n 7. You should see the External Drive in the list of " +120"files." +121"\n 8. If the External drive name is empty (it does not have " +122"any name), press Fail, else press Pass.";123124instructionTextArea = new JTextArea();125instructionTextArea.setText(instructions);126instructionTextArea.setEnabled(false);127instructionTextArea.setDisabledTextColor(Color.black);128instructionTextArea.setBackground(Color.white);129130gbc.gridx = 0;131gbc.gridy = 0;132gbc.fill = GridBagConstraints.HORIZONTAL;133mainControlPanel.add(instructionTextArea, gbc);134JButton launchButton = new JButton("Launch");135launchButton.setActionCommand("Launch");136launchButton.addActionListener((ActionEvent e) -> {137JFileChooser fileChooser = new JFileChooser();138fileChooser.showOpenDialog(null);139}140);141142gbc.gridx = 0;143gbc.gridy = 1;144mainControlPanel.add(launchButton, gbc);145146passButton = new JButton("Pass");147passButton.setActionCommand("Pass");148passButton.addActionListener((ActionEvent e) -> {149testResult = true;150mainFrame.dispose();151latch.countDown();152153});154failButton = new JButton("Fail");155failButton.setActionCommand("Fail");156failButton.addActionListener(new ActionListener() {157@Override158public void actionPerformed(ActionEvent e) {159testResult = false;160mainFrame.dispose();161latch.countDown();162}163});164gbc.gridx = 0;165gbc.gridy = 0;166resultButtonPanel.add(passButton, gbc);167gbc.gridx = 1;168gbc.gridy = 0;169resultButtonPanel.add(failButton, gbc);170171gbc.gridx = 0;172gbc.gridy = 2;173mainControlPanel.add(resultButtonPanel, gbc);174175mainFrame.add(mainControlPanel);176mainFrame.pack();177mainFrame.setVisible(true);178}179180public void disposeUI() {181mainFrame.setVisible(false);182mainFrame.dispose();183}184}185186187