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 24 /* 25 * @test 26 * @bug 8081722 27 * @summary Provide public API for file hierarchy provided by 28 * sun.awt.shell.ShellFolder 29 * @author Semyon Sadetsky 30 * @run main ShellFolderQueriesTest 31 */ 32 33 34 import javax.swing.filechooser.FileSystemView; 35 import java.io.File; 36 import java.io.FileNotFoundException; 37 import java.io.FileOutputStream; 38 import java.io.IOException; 39 40 public class ShellFolderQueriesTest { 41 static final String HOME = System.getProperty("user.home"); 42 static final FileSystemView fsv = FileSystemView.getFileSystemView(); 43 44 45 static String scriptBeg = 46 "set WshShell = WScript.CreateObject(\"WScript.Shell\")\n" + 47 "set oShellLink = WshShell.CreateShortcut(\"shortcut.lnk\")\n" + 48 "oShellLink.TargetPath = \""; 49 static String scriptEnd = "\"\noShellLink.WindowStyle = 1\noShellLink.Save"; 50 51 public static void main(String[] args) throws Exception { 52 if(System.getProperty("os.name").toLowerCase().contains("windows")) { 53 System.out.println("Windows detected: will run shortcut test"); 54 testGet(); 55 testLink(); 56 } else { 57 testGet(); 58 } 59 System.out.println("ok"); 60 } 61 62 private static void testLink() throws IOException, InterruptedException { 63 // Create and execute VBS script to create a link 64 File file = createVbsScript(scriptBeg + HOME + scriptEnd); 65 Runtime.getRuntime().exec("cscript " + file.getName(), null, 66 file.getParentFile()).waitFor(); 67 file.delete(); 68 69 File link = new File(file.getParentFile(), "shortcut.lnk"); 70 if (!fsv.isLink(link)) { 71 link.delete(); 72 throw new RuntimeException("Link is not detected"); 73 } 74 75 File location = fsv.getLinkLocation(link); 99 } 100 link.delete(); 101 } 102 103 private static File createVbsScript(String script) throws IOException { 104 File file = File.createTempFile("test", ".vbs"); 105 file.deleteOnExit(); 106 FileOutputStream fos = new FileOutputStream(file); 107 fos.write(script.getBytes()); 108 fos.close(); 109 return file; 110 } 111 112 private static void testGet() { 113 File[] files = fsv.getChooserComboBoxFiles(); 114 for (File file : files) { 115 if (fsv.isLink(file)) { 116 throw new RuntimeException( 117 "Link shouldn't be in FileChooser combobox, " 118 + file.getPath()); 119 } 120 } 121 } 122 } | 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 24 /* 25 * @test 26 * @bug 8081722 8182043 27 * @summary Provide public API for file hierarchy provided by 28 * sun.awt.shell.ShellFolder 29 * @author Semyon Sadetsky 30 * @run main ShellFolderQueriesTest 31 */ 32 33 34 import javax.swing.*; 35 import javax.swing.filechooser.FileSystemView; 36 import java.io.File; 37 import java.io.FileNotFoundException; 38 import java.io.FileOutputStream; 39 import java.io.IOException; 40 41 public class ShellFolderQueriesTest { 42 static final String HOME = System.getProperty("user.home"); 43 static final FileSystemView fsv = FileSystemView.getFileSystemView(); 44 45 46 static String scriptBeg = 47 "set WshShell = WScript.CreateObject(\"WScript.Shell\")\n" + 48 "set oShellLink = WshShell.CreateShortcut(\"shortcut.lnk\")\n" + 49 "oShellLink.TargetPath = \""; 50 static String scriptEnd = "\"\noShellLink.WindowStyle = 1\noShellLink.Save"; 51 52 public static void main(String[] args) throws Exception { 53 if(System.getProperty("os.name").toLowerCase().contains("windows")) { 54 System.out.println("Windows detected: will run shortcut " + 55 "and sytem icons tests"); 56 testGet(); 57 testLink(); 58 testSystemIcon(); 59 } else { 60 testGet(); 61 } 62 System.out.println("ok"); 63 } 64 65 private static void testLink() throws IOException, InterruptedException { 66 // Create and execute VBS script to create a link 67 File file = createVbsScript(scriptBeg + HOME + scriptEnd); 68 Runtime.getRuntime().exec("cscript " + file.getName(), null, 69 file.getParentFile()).waitFor(); 70 file.delete(); 71 72 File link = new File(file.getParentFile(), "shortcut.lnk"); 73 if (!fsv.isLink(link)) { 74 link.delete(); 75 throw new RuntimeException("Link is not detected"); 76 } 77 78 File location = fsv.getLinkLocation(link); 102 } 103 link.delete(); 104 } 105 106 private static File createVbsScript(String script) throws IOException { 107 File file = File.createTempFile("test", ".vbs"); 108 file.deleteOnExit(); 109 FileOutputStream fos = new FileOutputStream(file); 110 fos.write(script.getBytes()); 111 fos.close(); 112 return file; 113 } 114 115 private static void testGet() { 116 File[] files = fsv.getChooserComboBoxFiles(); 117 for (File file : files) { 118 if (fsv.isLink(file)) { 119 throw new RuntimeException( 120 "Link shouldn't be in FileChooser combobox, " 121 + file.getPath()); 122 } 123 } 124 } 125 126 static void testSystemIcon() { 127 try { 128 fsv.getSystemIcon(null, FileSystemView.FILE_ICON_SMALL); 129 } catch (NullPointerException e) { 130 String windir = System.getenv("windir"); 131 testSystemIcon(new File(windir)); 132 testSystemIcon(new File(windir + "/explorer.exe")); 133 return; 134 } 135 throw new RuntimeException("NPE was not thrown"); 136 } 137 138 static void testSystemIcon(File file) { 139 ImageIcon icon = fsv.getSystemIcon(file, FileSystemView.FILE_ICON_SMALL); 140 System.out.println("Small icon size " + icon.getIconWidth()); 141 142 icon = fsv.getSystemIcon(file, FileSystemView.FILE_ICON_LARGE); 143 System.out.println("Large icon size " + icon.getIconWidth()); 144 145 int[] sizes = new int[] {16, 24, 32, 48, 64, 128, 50}; 146 for (int size : sizes) { 147 icon = fsv.getSystemIcon(file, size); 148 if (icon.getIconWidth() != size) { 149 throw new RuntimeException("Wrong icon size " + 150 icon.getIconWidth() + " when requested " + size); 151 } 152 } 153 } 154 } |