Skip to content

Commit f54bdb5

Browse files
committed
8328648: Remove applet usage from JFileChooser tests bug4150029
Backport-of: 021ed6aea92f770ebeae65175d94797f7c418c82
1 parent 7fbd271 commit f54bdb5

File tree

2 files changed

+96
-95
lines changed

2 files changed

+96
-95
lines changed

test/jdk/javax/swing/JFileChooser/4150029/bug4150029.html

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 96 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,73 +21,117 @@
2121
* questions.
2222
*/
2323

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;
2934

3035
import jdk.test.lib.Platform;
3136

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;
3554

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);
3858

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+
}
4566
}
46-
}
4767

48-
String tmpDir = System.getProperty("java.io.tmpdir");
68+
String tmpDir = System.getProperty("java.io.tmpdir");
4969

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+
});
5298
}
99+
}
53100

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+
}
68112

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);
70120

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();
76122

77-
e.printStackTrace();
78-
}
123+
// check backspace key at subDir level
124+
clickBackSpace();
79125

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");
85128
}
86129
}
87130

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();
92136
}
93137
}

0 commit comments

Comments
 (0)