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. 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 org.netbeans.jemmy.ComponentChooser;
  28 import org.netbeans.jemmy.util.Dumper;
  29 import org.testng.ITestResult;
  30 import org.testng.annotations.AfterClass;
  31 import org.testng.annotations.AfterMethod;
  32 import org.testng.annotations.BeforeClass;
  33 import org.testng.annotations.Test;
  34 
  35 import javax.swing.JFileChooser;
  36 import java.awt.Component;
  37 import java.awt.Container;
  38 import java.io.File;
  39 import java.io.FileNotFoundException;
  40 import java.io.IOException;
  41 
  42 import static org.testng.Assert.assertEquals;
  43 import static org.testng.Assert.assertNotNull;
  44 import static org.testng.Assert.assertTrue;
  45 
  46 public class FileChooserTest {
  47 
  48     private JFileChooserOperator fileChooser;
  49     private File dir, file;
  50 
  51     @BeforeClass
  52     public void setup() throws IOException {
  53         dir = File.createTempFile("testDir", "");
  54         dir.delete(); dir.mkdirs();
  55         File.createTempFile("aestFile", ".txt", dir).deleteOnExit();
  56         file = File.createTempFile("testFile", ".txt", dir);
  57         file.deleteOnExit();
  58         File.createTempFile("zestFile", ".txt", dir).deleteOnExit();
  59         FileChooserApp.show(dir);
  60         JFrameOperator frame = new JFrameOperator("Sample File Chooser");
  61         fileChooser = new JFileChooserOperator(
  62                 JFileChooserOperator.findJFileChooser((Container) frame.getSource()));
  63     }
  64     @AfterMethod
  65     public void tearDown(ITestResult result) throws FileNotFoundException {
  66         if(!result.isSuccess())
  67             Dumper.dumpAll(new File(result.getMethod() + "-dump.xml").getAbsolutePath());
  68     }
  69     @Test
  70     public void testSelection() {
  71         fileChooser.selectFile(file.getName());
  72         fileChooser.waitState(new ComponentChooser() {
  73             @Override
  74             public boolean checkComponent(Component comp) {
  75                 return ((JFileChooser)comp).getSelectedFile() != null &&
  76                         fileChooser.getSelectedFile().getName().equals(file.getName());
  77             }
  78 
  79             @Override
  80             public String getDescription() {
  81                 return "test file is selected";
  82             }
  83         });
  84     }
  85     @Test
  86     public void testCount() {
  87         assertTrue(fileChooser.getFileCount() >= 3);
  88     }
  89 }