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