|
1 | 1 | /* |
2 | | - * Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2010, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
21 | 21 | * questions. |
22 | 22 | */ |
23 | 23 |
|
24 | | -/* |
25 | | - bug 4150029 8006087 |
26 | | - summary BackSpace keyboard button does not lead to parent directory |
27 | | - author Oleg Mokhovikov |
28 | | -*/ |
| 24 | +import java.awt.BorderLayout; |
| 25 | +import java.awt.Point; |
| 26 | +import java.awt.Robot; |
| 27 | +import java.awt.event.InputEvent; |
| 28 | +import java.awt.event.KeyEvent; |
| 29 | +import java.io.File; |
| 30 | +import javax.swing.JFileChooser; |
| 31 | +import javax.swing.JFrame; |
| 32 | +import javax.swing.SwingUtilities; |
| 33 | +import javax.swing.UIManager; |
29 | 34 |
|
30 | 35 | import jdk.test.lib.Platform; |
31 | 36 |
|
32 | | -import javax.swing.*; |
33 | | -import java.io.File; |
34 | | -import java.io.IOException; |
| 37 | +/* |
| 38 | + * @test |
| 39 | + * @bug 4150029 8006087 |
| 40 | + * @key headful |
| 41 | + * @summary BackSpace keyboard button does not lead to parent directory |
| 42 | + * @library /test/lib |
| 43 | + * @build jdk.test.lib.Platform |
| 44 | + * @run main bug4150029 |
| 45 | + */ |
| 46 | + |
| 47 | +public class bug4150029 { |
| 48 | + private static JFrame frame; |
| 49 | + private static JFileChooser fileChooser; |
| 50 | + private static Robot robot; |
| 51 | + private static File prevDir; |
| 52 | + private static File crntDir; |
| 53 | + private static volatile Point p; |
35 | 54 |
|
36 | | -public class bug4150029 extends JApplet { |
37 | | - private boolean res; |
| 55 | + public static void main(String[] args) throws Exception { |
| 56 | + robot = new Robot(); |
| 57 | + robot.setAutoDelay(100); |
38 | 58 |
|
39 | | - public void init() { |
40 | | - if (Platform.isOSX()) { |
41 | | - try { |
42 | | - UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); |
43 | | - } catch (Exception e) { |
44 | | - throw new RuntimeException(e); |
| 59 | + try { |
| 60 | + if (Platform.isOSX()) { |
| 61 | + try { |
| 62 | + UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); |
| 63 | + } catch (Exception e) { |
| 64 | + throw new RuntimeException(e); |
| 65 | + } |
45 | 66 | } |
46 | | - } |
47 | 67 |
|
48 | | - String tmpDir = System.getProperty("java.io.tmpdir"); |
| 68 | + String tmpDir = System.getProperty("java.io.tmpdir"); |
49 | 69 |
|
50 | | - if (tmpDir.length() == 0) {//'java.io.tmpdir' isn't guaranteed to be defined |
51 | | - tmpDir = System.getProperty("user.home"); |
| 70 | + //'java.io.tmpdir' isn't guaranteed to be defined |
| 71 | + if (tmpDir.length() == 0) { |
| 72 | + tmpDir = System.getProperty("user.home"); |
| 73 | + } |
| 74 | + System.out.println("Temp directory: " + tmpDir); |
| 75 | + |
| 76 | + File testDir = new File(tmpDir, "testDir"); |
| 77 | + testDir.mkdir(); |
| 78 | + testDir.deleteOnExit(); |
| 79 | + System.out.println("Created directory: " + testDir); |
| 80 | + |
| 81 | + File subDir = new File(testDir, "subDir"); |
| 82 | + subDir.mkdir(); |
| 83 | + subDir.deleteOnExit(); |
| 84 | + System.out.println("Created sub-directory: " + subDir); |
| 85 | + |
| 86 | + SwingUtilities.invokeAndWait(() -> { |
| 87 | + createAndShowUI(); |
| 88 | + fileChooser.setCurrentDirectory(subDir); |
| 89 | + }); |
| 90 | + |
| 91 | + doTesting(); |
| 92 | + } finally { |
| 93 | + SwingUtilities.invokeAndWait(() -> { |
| 94 | + if (frame != null) { |
| 95 | + frame.dispose(); |
| 96 | + } |
| 97 | + }); |
52 | 98 | } |
| 99 | + } |
53 | 100 |
|
54 | | - System.out.println("Temp directory: " + tmpDir); |
55 | | - |
56 | | - File testDir = new File(tmpDir, "testDir"); |
57 | | - |
58 | | - testDir.mkdir(); |
59 | | - |
60 | | - File subDir = new File(testDir, "subDir"); |
61 | | - |
62 | | - subDir.mkdir(); |
63 | | - |
64 | | - System.out.println("Created directory: " + testDir); |
65 | | - System.out.println("Created sub-directory: " + subDir); |
66 | | - |
67 | | - JFileChooser fileChooser = new JFileChooser(testDir); |
| 101 | + private static void createAndShowUI() { |
| 102 | + frame = new JFrame("Backspace Shortcut for Directory Navigation Test"); |
| 103 | + frame.setLayout(new BorderLayout()); |
| 104 | + fileChooser = new JFileChooser(); |
| 105 | + fileChooser.setControlButtonsAreShown(false); |
| 106 | + frame.add(fileChooser, BorderLayout.CENTER); |
| 107 | + frame.pack(); |
| 108 | + frame.setLocationRelativeTo(null); |
| 109 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 110 | + frame.setVisible(true); |
| 111 | + } |
68 | 112 |
|
69 | | - fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
| 113 | + private static void doTesting() throws Exception { |
| 114 | + SwingUtilities.invokeAndWait(() -> { |
| 115 | + p = frame.getLocationOnScreen(); |
| 116 | + }); |
| 117 | + robot.mouseMove(p.x + 200, p.y + 200); |
| 118 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 119 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
70 | 120 |
|
71 | | - try { |
72 | | - res = fileChooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION || |
73 | | - testDir.getCanonicalPath().equals(fileChooser.getSelectedFile().getCanonicalPath()); |
74 | | - } catch (IOException e) { |
75 | | - res = false; |
| 121 | + robot.waitForIdle(); |
76 | 122 |
|
77 | | - e.printStackTrace(); |
78 | | - } |
| 123 | + // check backspace key at subDir level |
| 124 | + clickBackSpace(); |
79 | 125 |
|
80 | | - try { |
81 | | - subDir.delete(); |
82 | | - testDir.delete(); |
83 | | - } catch (SecurityException e) { |
84 | | - e.printStackTrace(); |
| 126 | + if (prevDir.equals(crntDir)) { |
| 127 | + throw new RuntimeException("BackSpace does not lead to parent directory"); |
85 | 128 | } |
86 | 129 | } |
87 | 130 |
|
88 | | - public void destroy() { |
89 | | - if (!res) { |
90 | | - throw new RuntimeException("BackSpace keyboard button does not lead to parent directory"); |
91 | | - } |
| 131 | + private static void clickBackSpace() { |
| 132 | + prevDir = fileChooser.getCurrentDirectory(); |
| 133 | + robot.keyPress(KeyEvent.VK_BACK_SPACE); |
| 134 | + robot.keyRelease(KeyEvent.VK_BACK_SPACE); |
| 135 | + crntDir = fileChooser.getCurrentDirectory(); |
92 | 136 | } |
93 | 137 | } |
0 commit comments