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 org.netbeans.jemmy.ComponentChooser; 28 import org.netbeans.jemmy.LookAndFeelProvider; 29 import org.netbeans.jemmy.util.Dumper; 30 import org.netbeans.jemmy.util.Platform; 31 import org.testng.ITestResult; 32 import org.testng.annotations.AfterMethod; 33 import org.testng.annotations.BeforeClass; 34 import org.testng.annotations.Test; 35 36 import javax.swing.JFileChooser; 37 import javax.swing.UIManager; 38 39 import java.awt.Component; 40 import java.awt.Container; 41 import java.io.File; 42 import java.io.FileNotFoundException; 43 import java.io.IOException; 44 import java.nio.file.Path; 45 import java.nio.file.Paths; 46 47 import static org.testng.Assert.assertTrue; 48 49 public class FileChooserTest { 50 51 private static final String USER_HOME = "user.home"; 52 private static final String DESKTOP = "Desktop"; 53 private JFrameOperator frame; 54 private JFileChooserOperator fileChooser; 55 private File dir, file; 56 57 @BeforeClass 58 public void setup() throws IOException { 59 dir = File.createTempFile("testDir", ""); 60 dir.delete(); dir.mkdirs(); 61 File.createTempFile("aestFile", ".txt", dir).deleteOnExit(); 62 file = File.createTempFile("testFile", ".txt", dir); 63 file.deleteOnExit(); 64 File.createTempFile("zestFile", ".txt", dir).deleteOnExit(); 65 } 66 67 public void setUpBeforeMethod() { 68 FileChooserApp.show(dir); 69 frame = new JFrameOperator("Sample File Chooser"); 70 fileChooser = new JFileChooserOperator( 71 JFileChooserOperator.findJFileChooser((Container) frame.getSource())); 72 } 73 74 @AfterMethod 75 public void tearDown(ITestResult result) throws FileNotFoundException { 76 frame.setVisible(false); 77 frame.dispose(); 78 if(!result.isSuccess()) 79 Dumper.dumpAll(new File(UIManager.getLookAndFeel().getClass().getSimpleName() 80 + "_" + result.getMethod() + "-dump.xml").getAbsolutePath()); 81 } 82 83 @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class) 84 public void testSelection(String lookAndFeel) throws Exception { 85 UIManager.setLookAndFeel(lookAndFeel); 86 setUpBeforeMethod(); 87 fileChooser.selectFile(file.getName()); 88 fileChooser.waitState(new ComponentChooser() { 89 @Override 90 public boolean checkComponent(Component comp) { 91 return ((JFileChooser)comp).getSelectedFile() != null && 92 fileChooser.getSelectedFile().getName().equals(file.getName()); 93 } 94 95 @Override 96 public String getDescription() { 97 return "test file is selected"; 98 } 99 }); 100 } 101 102 @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class) 103 public void testCount(String lookAndFeel) throws Exception { 104 UIManager.setLookAndFeel(lookAndFeel); 105 setUpBeforeMethod(); 106 assertTrue(fileChooser.getFileCount() >= 3); 107 } 108 109 @Test(dataProvider = "availableLookAndFeels", dataProviderClass = LookAndFeelProvider.class) 110 public void testGoHome(String lookAndFeel) throws Exception { 111 UIManager.setLookAndFeel(lookAndFeel); 112 setUpBeforeMethod(); 113 // In Aqua, GTK and Motif L&Fs, JFileChooser does not have 114 // "Go Home" button. 115 if (!UIManager.getLookAndFeel().getID().equals("Aqua") 116 && !UIManager.getLookAndFeel().getID().equals("Motif") 117 && !UIManager.getLookAndFeel().getID().equals("GTK")) { 118 File previousDirectory = fileChooser.getCurrentDirectory(); 119 fileChooser.goHome(); 120 // In Windows, pressing goHome navigates to Desktop inside the home directory. 121 // This is the expected behavior for windows. 122 if (!Platform.isWindows()) { 123 waitCurrentPath(Paths.get(System.getProperty(USER_HOME))); 124 } else { 125 waitCurrentPath(Paths.get(System.getProperty(USER_HOME)).resolve(DESKTOP)); 126 } 127 fileChooser.setCurrentDirectory(previousDirectory); 128 fileChooser.rescanCurrentDirectory(); 129 } 130 } 131 132 private void waitCurrentPath(Path expectedPath) { 133 fileChooser.waitState(chooser -> fileChooser.getCurrentDirectory().toPath().equals(expectedPath)); 134 } 135 }