1 /*
   2  * Copyright (c) 2017, 2018, 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. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.netbeans.jemmy.operators;
  26 
  27 import static org.testng.Assert.assertTrue;
  28 
  29 import java.awt.Component;
  30 import java.awt.Container;
  31 import java.io.File;
  32 import java.io.FileNotFoundException;
  33 import java.io.IOException;
  34 
  35 import javax.swing.JFileChooser;
  36 import javax.swing.UIManager;
  37 import javax.swing.filechooser.FileSystemView;
  38 
  39 import org.netbeans.jemmy.ComponentChooser;
  40 import org.netbeans.jemmy.LookAndFeelProvider;
  41 import org.netbeans.jemmy.util.Dumper;
  42 import org.testng.ITestResult;
  43 import org.testng.annotations.AfterMethod;
  44 import org.testng.annotations.BeforeClass;
  45 import org.testng.annotations.BeforeMethod;
  46 import org.testng.annotations.Test;
  47 
  48 public class FileChooserTest {
  49 
  50     private JFrameOperator frame;
  51     private JFileChooserOperator fileChooser;
  52     private File dir, file;
  53 
  54     @BeforeClass
  55     public void setup() throws IOException {
  56         dir = File.createTempFile("testDir", "");
  57         dir.delete(); dir.mkdirs();
  58         File.createTempFile("aestFile", ".txt", dir).deleteOnExit();
  59         file = File.createTempFile("testFile", ".txt", dir);
  60         file.deleteOnExit();
  61         File.createTempFile("zestFile", ".txt", dir).deleteOnExit();
  62     }
  63 
  64     @BeforeMethod
  65     public void setUpBeforeMethod(Object[] args) throws Exception {
  66         UIManager.setLookAndFeel((String)args[0]);
  67         FileChooserApp.show(dir);
  68         frame = new JFrameOperator("Sample File Chooser");
  69         fileChooser = new JFileChooserOperator(
  70                 JFileChooserOperator.findJFileChooser((Container) frame.getSource()));
  71     }
  72 
  73     @AfterMethod
  74     public void tearDown(ITestResult result) throws FileNotFoundException {
  75         frame.setVisible(false);
  76         frame.dispose();
  77         if(!result.isSuccess())
  78             Dumper.dumpAll(new File(UIManager.getLookAndFeel().getClass().getSimpleName()
  79                     + "_" + result.getMethod() + "-dump.xml").getAbsolutePath());
  80     }
  81 
  82     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
  83     public void testSelection(String lookAndFeel) throws Exception {
  84         fileChooser.selectFile(file.getName());
  85         fileChooser.waitState(new ComponentChooser() {
  86             @Override
  87             public boolean checkComponent(Component comp) {
  88                 return ((JFileChooser)comp).getSelectedFile() != null &&
  89                         fileChooser.getSelectedFile().getName().equals(file.getName());
  90             }
  91 
  92             @Override
  93             public String getDescription() {
  94                 return "test file is selected";
  95             }
  96         });
  97     }
  98 
  99     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
 100     public void testCount(String lookAndFeel) throws Exception {
 101         assertTrue(fileChooser.getFileCount() >= 3);
 102     }
 103 
 104     @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
 105     public void testGoHome(String lookAndFeel) throws Exception {
 106         // In Aqua, GTK and Motif L&Fs, JFileChooser does not have
 107         // "Go Home" button.
 108         if (!UIManager.getLookAndFeel().getID().equals("Aqua")
 109                 && !UIManager.getLookAndFeel().getID().equals("Motif")
 110                 && !UIManager.getLookAndFeel().getID().equals("GTK")) {
 111             File previousDirectory = fileChooser.getCurrentDirectory();
 112             fileChooser.goHome();
 113             fileChooser.waitState(chooser -> fileChooser.getCurrentDirectory().getPath().equals(
 114                     FileSystemView.getFileSystemView().getHomeDirectory().getPath()));
 115             fileChooser.setCurrentDirectory(previousDirectory);
 116             fileChooser.rescanCurrentDirectory();
 117         }
 118     }
 119 }