1 /*
   2  * Copyright (c) 2017, 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 8179014 
  27  * @requires (os.family == "Windows")
  28  * @summary Check if JFileChooser crashes with GodMode Directory.
  29  * @run main/manual JFileChooserTest
  30  */
  31 import java.awt.Color;
  32 import java.awt.GridBagConstraints;
  33 import java.awt.GridBagLayout;
  34 import java.util.concurrent.CountDownLatch;
  35 import javax.swing.JPanel;
  36 import javax.swing.JTextArea;
  37 import javax.swing.SwingUtilities;
  38 import javax.swing.JButton;
  39 import javax.swing.JFrame;
  40 import java.awt.event.ActionEvent;
  41 import java.awt.event.ActionListener;
  42 import java.util.concurrent.TimeUnit;
  43 import javax.swing.JFileChooser;
  44 import javax.swing.UIManager;
  45 
  46 public class JFileChooserTest {
  47 
  48     public static void main(String args[]) throws Exception {
  49         final CountDownLatch latch = new CountDownLatch(1);
  50         TestUI test = new TestUI(latch);
  51         SwingUtilities.invokeAndWait(() -> {
  52             try {
  53                 test.createUI();
  54             } catch (Exception ex) {
  55                 throw new RuntimeException("Exception while creating UI");
  56             }
  57         });
  58 
  59         boolean status = latch.await(5, TimeUnit.MINUTES);
  60 
  61         if (!status) {
  62             System.out.println("Test timed out.");
  63         }
  64 
  65         SwingUtilities.invokeAndWait(() -> {
  66             try {
  67                 test.disposeUI();
  68             } catch (Exception ex) {
  69                 throw new RuntimeException("Exception while disposing UI");
  70             }
  71         });
  72 
  73         if (test.testResult == false) {
  74             throw new RuntimeException("Test Failed.");
  75         }
  76     }
  77 }
  78 
  79 class TestUI {
  80 
  81     private static JFrame mainFrame;
  82     private static JPanel mainControlPanel;
  83 
  84     private static JTextArea instructionTextArea;
  85 
  86     private static JPanel resultButtonPanel;
  87     private static JButton passButton;
  88     private static JButton failButton;
  89 
  90     private static GridBagLayout layout;
  91     private final CountDownLatch latch;
  92     public boolean testResult = false;
  93 
  94     public TestUI(CountDownLatch latch) throws Exception {
  95         this.latch = latch;
  96     }
  97 
  98     public final void createUI() throws Exception {
  99         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 100         mainFrame = new JFrame("JFileChooserTest");
 101 
 102         layout = new GridBagLayout();
 103         mainControlPanel = new JPanel(layout);
 104         resultButtonPanel = new JPanel(layout);
 105 
 106         GridBagConstraints gbc = new GridBagConstraints();
 107 
 108         // Create Test instructions
 109         String instructions
 110                 = "INSTRUCTIONS:"
 111                 + "\n 1. Create a new folder on the desktop."
 112                 + "\n 2. Rename the folder exactly as given below: "
 113                 + "\n    GodMode.{ED7BA470-8E54-465E-825C-99712043E01C} "
 114                 + "\n 3. Click on Launch Button. "
 115                 + "\n    Check if JFileChooser is launched successfully. "
 116                 + "\n    If yes, close the JFileChooser and click Pass, "
 117                 + "\n    else Fail. ";
 118 
 119         instructionTextArea = new JTextArea();
 120         instructionTextArea.setText(instructions);
 121         instructionTextArea.setEnabled(false);
 122         instructionTextArea.setDisabledTextColor(Color.black);
 123         instructionTextArea.setBackground(Color.white);
 124 
 125         gbc.gridx = 0;
 126         gbc.gridy = 0;
 127         gbc.fill = GridBagConstraints.HORIZONTAL;
 128         mainControlPanel.add(instructionTextArea, gbc);
 129         JButton launchButton = new JButton("Launch");
 130         launchButton.setActionCommand("Launch");
 131         launchButton.addActionListener((ActionEvent e) -> {
 132             JFileChooser fileChooser = new JFileChooser();
 133             fileChooser.showOpenDialog(null);
 134         }
 135         );
 136 
 137         gbc.gridx = 0;
 138         gbc.gridy = 1;
 139         mainControlPanel.add(launchButton, gbc);
 140 
 141         passButton = new JButton("Pass");
 142         passButton.setActionCommand("Pass");
 143         passButton.addActionListener((ActionEvent e) -> {
 144             testResult = true;
 145             mainFrame.dispose();
 146             latch.countDown();
 147 
 148         });
 149         failButton = new JButton("Fail");
 150         failButton.setActionCommand("Fail");
 151         failButton.addActionListener(new ActionListener() {
 152             @Override
 153             public void actionPerformed(ActionEvent e) {
 154                 testResult = false;
 155                 mainFrame.dispose();
 156                 latch.countDown();
 157             }
 158         });
 159         gbc.gridx = 0;
 160         gbc.gridy = 0;
 161         resultButtonPanel.add(passButton, gbc);
 162         gbc.gridx = 1;
 163         gbc.gridy = 0;
 164         resultButtonPanel.add(failButton, gbc);
 165 
 166         gbc.gridx = 0;
 167         gbc.gridy = 2;
 168         mainControlPanel.add(resultButtonPanel, gbc);
 169 
 170         mainFrame.add(mainControlPanel);
 171         mainFrame.pack();
 172         mainFrame.setVisible(true);
 173     }
 174 
 175     public void disposeUI() {
 176         mainFrame.setVisible(false);
 177         mainFrame.dispose();
 178     }
 179 }