< prev index next >

test/org/netbeans/jemmy/operators/FileChooserTest.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation. Oracle designates this

@@ -23,30 +23,36 @@
  * questions.
  */
 package org.netbeans.jemmy.operators;
 
 import org.netbeans.jemmy.ComponentChooser;
+import org.netbeans.jemmy.LookAndFeelProvider;
 import org.netbeans.jemmy.util.Dumper;
+import org.netbeans.jemmy.util.Platform;
 import org.testng.ITestResult;
-import org.testng.annotations.AfterClass;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 import javax.swing.JFileChooser;
+import javax.swing.UIManager;
+
 import java.awt.Component;
 import java.awt.Container;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertTrue;
 
 public class FileChooserTest {
 
+    private static final String USER_HOME = "user.home";
+    private static final String DESKTOP = "Desktop";
+    private JFrameOperator frame;
     private JFileChooserOperator fileChooser;
     private File dir, file;
 
     @BeforeClass
     public void setup() throws IOException {

@@ -54,22 +60,32 @@
         dir.delete(); dir.mkdirs();
         File.createTempFile("aestFile", ".txt", dir).deleteOnExit();
         file = File.createTempFile("testFile", ".txt", dir);
         file.deleteOnExit();
         File.createTempFile("zestFile", ".txt", dir).deleteOnExit();
+    }
+    
+    public void setUpBeforeMethod() {
         FileChooserApp.show(dir);
-        JFrameOperator frame = new JFrameOperator("Sample File Chooser");
+        frame = new JFrameOperator("Sample File Chooser");
         fileChooser = new JFileChooserOperator(
                 JFileChooserOperator.findJFileChooser((Container) frame.getSource()));
     }
+    
     @AfterMethod
     public void tearDown(ITestResult result) throws FileNotFoundException {
+        frame.setVisible(false);
+        frame.dispose();
         if(!result.isSuccess())
-            Dumper.dumpAll(new File(result.getMethod() + "-dump.xml").getAbsolutePath());
+            Dumper.dumpAll(new File(UIManager.getLookAndFeel().getClass().getSimpleName()
+                    + "_" + result.getMethod() + "-dump.xml").getAbsolutePath());
     }
-    @Test
-    public void testSelection() {
+    
+    @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
+    public void testSelection(String lookAndFeel) throws Exception {
+        UIManager.setLookAndFeel(lookAndFeel);
+        setUpBeforeMethod();
         fileChooser.selectFile(file.getName());
         fileChooser.waitState(new ComponentChooser() {
             @Override
             public boolean checkComponent(Component comp) {
                 return ((JFileChooser)comp).getSelectedFile() != null &&

@@ -80,10 +96,40 @@
             public String getDescription() {
                 return "test file is selected";
             }
         });
     }
-    @Test
-    public void testCount() {
+    
+    @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
+    public void testCount(String lookAndFeel) throws Exception {
+        UIManager.setLookAndFeel(lookAndFeel);
+        setUpBeforeMethod();
         assertTrue(fileChooser.getFileCount() >= 3);
     }
+    
+    @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class)
+    public void testGoHome(String lookAndFeel) throws Exception {
+        UIManager.setLookAndFeel(lookAndFeel);
+        setUpBeforeMethod();
+        // In Aqua, GTK and Motif L&Fs, JFileChooser does not have
+        // "Go Home" button.
+        if (!UIManager.getLookAndFeel().getID().equals("Aqua")
+                && !UIManager.getLookAndFeel().getID().equals("Motif")
+                && !UIManager.getLookAndFeel().getID().equals("GTK")) {
+            File previousDirectory = fileChooser.getCurrentDirectory();
+            fileChooser.goHome();
+            // In Windows, pressing goHome navigates to Desktop inside the home directory.
+            // This is the expected behavior for windows.
+            if (!Platform.isWindows()) {
+                waitCurrentPath(Paths.get(System.getProperty(USER_HOME)));
+            } else {
+                waitCurrentPath(Paths.get(System.getProperty(USER_HOME)).resolve(DESKTOP));
+            }
+            fileChooser.setCurrentDirectory(previousDirectory);
+            fileChooser.rescanCurrentDirectory();
+        }
+    }
+    
+    private void waitCurrentPath(Path expectedPath) {
+        fileChooser.waitState(chooser -> fileChooser.getCurrentDirectory().toPath().equals(expectedPath));
+    }
 }
< prev index next >