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 8019280
  27  * @summary CFileDialog should show Choose for Mac OS X for Directory selection
  28  * @requires (os.family=="mac")
  29  * @run main/manual ValidateDirectorySelection
  30  */
  31 import java.awt.Button;
  32 import java.awt.FileDialog;
  33 import java.awt.Frame;
  34 import java.awt.GridBagConstraints;
  35 import java.awt.GridBagLayout;
  36 import java.awt.Panel;
  37 import java.awt.TextArea;
  38 import java.awt.event.ActionEvent;
  39 import java.awt.event.ActionListener;
  40 import java.awt.event.WindowAdapter;
  41 import java.awt.event.WindowEvent;
  42 import java.util.logging.Level;
  43 import java.util.logging.Logger;
  44 
  45 public class ValidateDirectorySelection {
  46 
  47     private static Frame frame;
  48     private static FileDialog chooser;
  49     private static TextArea textArea;
  50     private static Button openCFileDialogButton;
  51     private static Button passButton;
  52     private static Button failButton;
  53     private static boolean theTestPassed;
  54     private static boolean testGeneratedInterrupt;
  55     private static Thread mainThread;
  56     private static final int sleepTime = 300000;
  57 
  58     public static void main(String[] args) throws Exception {
  59         mainThread = Thread.currentThread();
  60         try {
  61             createUIandManualTest();
  62             mainThread.sleep(sleepTime);
  63         } catch (InterruptedException ex) {
  64             cleanUp();
  65             if (!theTestPassed && testGeneratedInterrupt) {
  66                 throw new RuntimeException("Test Failed");
  67             }
  68         } catch (Exception ex) {
  69             cleanUp();
  70             Logger.getLogger(ValidateDirectorySelection.class.getName())
  71                     .log(Level.SEVERE, null, ex);
  72             return;
  73         }
  74         if (!testGeneratedInterrupt) {
  75             cleanUp();
  76             throw new RuntimeException("Test Timed out");
  77         }
  78     }
  79 
  80     public static synchronized void pass() {
  81         theTestPassed = true;
  82         testGeneratedInterrupt = true;
  83         mainThread.interrupt();
  84     }
  85 
  86     public static synchronized void fail() {
  87         theTestPassed = false;
  88         testGeneratedInterrupt = true;
  89         mainThread.interrupt();
  90     }
  91 
  92     private static void createUIandManualTest() throws Exception {
  93         frame = new Frame("Validate for directory selection only");
  94         frame.setLayout(new GridBagLayout());
  95         frame.addWindowListener(new WindowAdapter() {
  96             @Override
  97             public void windowClosing(WindowEvent we) {
  98                 try {
  99                     cleanUp();
 100                 } catch (Exception ex) {
 101                     Logger.getLogger(ValidateDirectorySelection.class.getName())
 102                             .log(Level.SEVERE, null, ex);
 103                 }
 104             }
 105         });
 106         String instructions = "\n(1) Press Directory Dialog button."
 107                 + "\n(2) Find a button labeled Choose."
 108                 + "\n(3) Make due note of the availability of the Choose button"
 109                 + "\n(4) This is expected behavior for Directory chooser in Mac"
 110                 + "\n\nIf the Choose button is validated, press Pass button."
 111                 + "\nOtherwise, press Fail button. \n";
 112         textArea = new TextArea();
 113         textArea.setText(instructions);
 114         openCFileDialogButton = new Button(" Directory Dialog ");
 115         openCFileDialogButton.addActionListener(new ActionListener() {
 116             @Override
 117             public void actionPerformed(ActionEvent e) {
 118                 System.setProperty("apple.awt.fileDialogForDirectories",
 119                         "true");
 120                 chooser = new FileDialog(frame, "Select Target Folder");
 121                 chooser.setVisible(true);
 122             }
 123         });
 124         passButton = new Button(" Pass ");
 125         passButton.addActionListener(new ActionListener() {
 126             @Override
 127             public void actionPerformed(ActionEvent e) {
 128                 ValidateDirectorySelection.pass();
 129             }
 130         });
 131         failButton = new Button(" Fail ");
 132         failButton.addActionListener(new ActionListener() {
 133             @Override
 134             public void actionPerformed(ActionEvent e) {
 135                 ValidateDirectorySelection.fail();
 136             }
 137         });
 138         GridBagConstraints gbc = new GridBagConstraints();
 139         gbc.gridx = 0;
 140         gbc.gridy = 0;
 141         frame.add(textArea, gbc);
 142         Panel buttonPanel = new Panel();
 143         gbc.gridx = 0;
 144         gbc.gridy = 0;
 145         buttonPanel.add(openCFileDialogButton, gbc);
 146         gbc.gridx = 1;
 147         gbc.gridy = 0;
 148         buttonPanel.add(passButton, gbc);
 149         gbc.gridx = 2;
 150         gbc.gridy = 0;
 151         buttonPanel.add(failButton, gbc);
 152         gbc.fill = GridBagConstraints.HORIZONTAL;
 153         gbc.gridx = 0;
 154         gbc.gridy = 1;
 155         frame.add(buttonPanel, gbc);
 156         frame.pack();
 157         frame.setLocationRelativeTo(null);
 158         frame.setVisible(true);
 159     }
 160 
 161     private static void cleanUp() {
 162         if (frame != null) {
 163             frame.dispose();
 164         }
 165     }
 166 }