jdk/test/javax/swing/JFileChooser/8067660/FileChooserTest.java
changeset 34799 0aaf5e7f0af9
child 47170 ac621266a4b8
equal deleted inserted replaced
34798:84d33f91b71e 34799:0aaf5e7f0af9
       
     1 /*
       
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8067660
       
    27  * @summary JFileChooser create new folder fails silently
       
    28  * @requires (os.family == "windows")
       
    29  * @run main/manual FileChooserTest
       
    30  */
       
    31 import java.awt.Panel;
       
    32 import java.awt.TextArea;
       
    33 import java.awt.event.ActionEvent;
       
    34 import java.awt.event.ActionListener;
       
    35 import javax.swing.JButton;
       
    36 import javax.swing.JDialog;
       
    37 import javax.swing.JFileChooser;
       
    38 import javax.swing.JFrame;
       
    39 import javax.swing.SwingUtilities;
       
    40 
       
    41 public class FileChooserTest {
       
    42 
       
    43     private static boolean theTestPassed;
       
    44     private static boolean testGeneratedInterrupt;
       
    45     private static Thread mainThread;
       
    46     private static int sleepTime = 30000;
       
    47     public static  JFileChooser fileChooser;
       
    48 
       
    49     private static void init() throws Exception {
       
    50 
       
    51         SwingUtilities.invokeAndWait(new Runnable() {
       
    52             @Override
       
    53             public void run() {
       
    54                 String[] instructions
       
    55                         = {
       
    56                             "1) Create a folder with read only permissions",
       
    57                             "2) Click on run test button.It will open a open dialog"
       
    58                             + " Navigate to the newly created read only folder",
       
    59                             "3) Click on the create new folder button in open dialog",
       
    60                             "4) If an error message does not pops up"
       
    61                             + "test failed otherwise passed.",
       
    62                             "5) Pressing Pass/Fail button will mark test as "
       
    63                             + "pass/fail and will shutdown JVM"};
       
    64 
       
    65                 Sysout.createDialogWithInstructions(instructions);
       
    66                 Sysout.printInstructions(instructions);
       
    67             }
       
    68         });
       
    69     }
       
    70 
       
    71     /**
       
    72      * ***************************************************
       
    73      * Standard Test Machinery Section DO NOT modify anything in this section --
       
    74      * it's a standard chunk of code which has all of the synchronisation
       
    75      * necessary for the test harness. By keeping it the same in all tests, it
       
    76      * is easier to read and understand someone else's test, as well as insuring
       
    77      * that all tests behave correctly with the test harness. There is a section
       
    78      * following this for test-defined classes
       
    79      */
       
    80     public static void main(String args[]) throws Exception {
       
    81 
       
    82         mainThread = Thread.currentThread();
       
    83         try {
       
    84             init();
       
    85         } catch (Exception ex) {
       
    86             return;
       
    87         }
       
    88         try {
       
    89             mainThread.sleep(sleepTime);
       
    90         } catch (InterruptedException ex) {
       
    91             Sysout.dispose();
       
    92             if (!theTestPassed && testGeneratedInterrupt) {
       
    93                 throw new RuntimeException("Test Failed");
       
    94             }
       
    95         }
       
    96         if (!testGeneratedInterrupt) {
       
    97             Sysout.dispose();
       
    98             throw new RuntimeException("Test Failed");
       
    99         }
       
   100     }
       
   101 
       
   102     public static synchronized void pass() {
       
   103         theTestPassed = true;
       
   104         testGeneratedInterrupt = true;
       
   105         mainThread.interrupt();
       
   106     }
       
   107 
       
   108     public static synchronized void fail() {
       
   109         theTestPassed = false;
       
   110         testGeneratedInterrupt = true;
       
   111         mainThread.interrupt();
       
   112     }
       
   113 }
       
   114 
       
   115 /**
       
   116  * This is part of the standard test machinery. It creates a dialog (with the
       
   117  * instructions), and is the interface for sending text messages to the user. To
       
   118  * print the instructions, send an array of strings to Sysout.createDialog
       
   119  * WithInstructions method. Put one line of instructions per array entry. To
       
   120  * display a message for the tester to see, simply call Sysout.println with the
       
   121  * string to be displayed. This mimics System.out.println but works within the
       
   122  * test harness as well as standalone.
       
   123  */
       
   124 class Sysout {
       
   125 
       
   126     private static TestDialog dialog;
       
   127     private static JFrame frame;
       
   128 
       
   129     public static void createDialogWithInstructions(String[] instructions) {
       
   130         frame = new JFrame();
       
   131         dialog = new TestDialog(frame, "Instructions");
       
   132         dialog.printInstructions(instructions);
       
   133         dialog.setVisible(true);
       
   134         println("Any messages for the tester will display here.");
       
   135     }
       
   136 
       
   137     public static void printInstructions(String[] instructions) {
       
   138         dialog.printInstructions(instructions);
       
   139     }
       
   140 
       
   141     public static void println(String messageIn) {
       
   142         dialog.displayMessage(messageIn);
       
   143     }
       
   144 
       
   145     public static void dispose() {
       
   146         Sysout.println("Shutting down the Java process..");
       
   147         if(FileChooserTest.fileChooser != null) {
       
   148             FileChooserTest.fileChooser.cancelSelection();
       
   149         }
       
   150         frame.dispose();
       
   151         dialog.dispose();
       
   152     }
       
   153 }
       
   154 
       
   155 /**
       
   156  * This is part of the standard test machinery. It provides a place for the test
       
   157  * instructions to be displayed, and a place for interactive messages to the
       
   158  * user to be displayed. To have the test instructions displayed, see Sysout. To
       
   159  * have a message to the user be displayed, see Sysout. Do not call anything in
       
   160  * this dialog directly.
       
   161  */
       
   162 class TestDialog extends JDialog {
       
   163 
       
   164     private TextArea instructionsText;
       
   165     private TextArea messageText;
       
   166     private int maxStringLength = 80;
       
   167     private Panel buttonP = new Panel();
       
   168     private JButton run = new JButton("Run");
       
   169     private JButton passB = new JButton("Pass");
       
   170     private JButton failB = new JButton("Fail");
       
   171 
       
   172     public TestDialog(JFrame frame, String name) {
       
   173         super(frame, name);
       
   174         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
   175         int scrollBoth = TextArea.SCROLLBARS_BOTH;
       
   176         instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);
       
   177         add("North", instructionsText);
       
   178 
       
   179         messageText = new TextArea("", 5, maxStringLength, scrollBoth);
       
   180         add("Center", messageText);
       
   181 
       
   182         buttonP.add("East", run);
       
   183         buttonP.add("East", passB);
       
   184         buttonP.add("West", failB);
       
   185         passB.setEnabled(false);
       
   186         failB.setEnabled(false);
       
   187         add("South", buttonP);
       
   188 
       
   189         run.addActionListener(new ActionListener() {
       
   190 
       
   191             @Override
       
   192             public void actionPerformed(ActionEvent ae) {
       
   193                 FileChooserTest.fileChooser = new JFileChooser();
       
   194                 FileChooserTest.fileChooser.showOpenDialog(null);
       
   195                 passB.setEnabled(true);
       
   196                 failB.setEnabled(true);
       
   197             }
       
   198         });
       
   199 
       
   200         passB.addActionListener(new ActionListener() {
       
   201 
       
   202             @Override
       
   203             public void actionPerformed(ActionEvent ae) {
       
   204                 FileChooserTest.pass();
       
   205             }
       
   206         });
       
   207 
       
   208         failB.addActionListener(new ActionListener() {
       
   209 
       
   210             @Override
       
   211             public void actionPerformed(ActionEvent ae) {
       
   212                 FileChooserTest.fail();
       
   213             }
       
   214         });
       
   215         pack();
       
   216 
       
   217         setVisible(true);
       
   218     }
       
   219 
       
   220     public void printInstructions(String[] instructions) {
       
   221         instructionsText.setText("");
       
   222 
       
   223         String printStr, remainingStr;
       
   224         for (String instruction : instructions) {
       
   225             remainingStr = instruction;
       
   226             while (remainingStr.length() > 0) {
       
   227                 if (remainingStr.length() >= maxStringLength) {
       
   228                     int posOfSpace = remainingStr.
       
   229                             lastIndexOf(' ', maxStringLength - 1);
       
   230 
       
   231                     if (posOfSpace <= 0) {
       
   232                         posOfSpace = maxStringLength - 1;
       
   233                     }
       
   234 
       
   235                     printStr = remainingStr.substring(0, posOfSpace + 1);
       
   236                     remainingStr = remainingStr.substring(posOfSpace + 1);
       
   237                 } else {
       
   238                     printStr = remainingStr;
       
   239                     remainingStr = "";
       
   240                 }
       
   241                 instructionsText.append(printStr + "\n");
       
   242             }
       
   243         }
       
   244 
       
   245     }
       
   246 
       
   247     public void displayMessage(String messageIn) {
       
   248         messageText.append(messageIn + "\n");
       
   249     }
       
   250 }