Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JFileChooser/8041694/bug8041694.java
41154 views
1
/*
2
* Copyright (c) 2016, 2019, 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.
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
* @key headful
27
* @bug 8041694
28
* @summary JFileChooser removes trailing spaces in the selected directory name
29
* @author Anton Litvinov
30
* @library /test/lib
31
* @build jdk.test.lib.Platform
32
* @run main bug8041694
33
*/
34
35
import java.awt.AWTException;
36
import java.awt.Robot;
37
import java.awt.event.KeyEvent;
38
import java.io.File;
39
import java.io.IOException;
40
import java.nio.file.Files;
41
import java.util.concurrent.CountDownLatch;
42
import javax.swing.JFileChooser;
43
import javax.swing.SwingUtilities;
44
import javax.swing.UIManager;
45
import javax.swing.UnsupportedLookAndFeelException;
46
import javax.swing.plaf.metal.MetalLookAndFeel;
47
48
import jdk.test.lib.Platform;
49
50
public class bug8041694 {
51
private static volatile File dir1;
52
private static File dir2;
53
private static volatile File selectedDir;
54
55
private static void runTest() {
56
try {
57
// Set Metal L&F to make the test compatible with OS X.
58
UIManager.setLookAndFeel(new MetalLookAndFeel());
59
Robot robot = new Robot();
60
robot.setAutoDelay(100);
61
62
dir1 = Files.createTempDirectory("bug8041694").toFile();
63
if (Platform.isWindows()) {
64
dir2 = new File(String.format(
65
"\\\\?\\%s\\d ", dir1.getAbsolutePath().replace('/', '\\')));
66
} else {
67
dir2 = new File(dir1.getAbsolutePath() + File.separator + "d ");
68
}
69
dir2.mkdir();
70
71
final CountDownLatch fChooserClosedSignal = new CountDownLatch(1);
72
SwingUtilities.invokeLater(new Runnable() {
73
@Override
74
public void run() {
75
try {
76
JFileChooser fChooser = new JFileChooser(dir1);
77
fChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
78
if (fChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
79
selectedDir = fChooser.getSelectedFile();
80
}
81
} finally {
82
fChooserClosedSignal.countDown();
83
}
84
}
85
});
86
87
robot.delay(1000);
88
robot.waitForIdle();
89
robot.keyPress(KeyEvent.VK_D);
90
robot.keyRelease(KeyEvent.VK_D);
91
robot.waitForIdle();
92
robot.keyPress(KeyEvent.VK_SPACE);
93
robot.keyRelease(KeyEvent.VK_SPACE);
94
robot.waitForIdle();
95
robot.keyPress(KeyEvent.VK_ENTER);
96
robot.keyRelease(KeyEvent.VK_ENTER);
97
robot.waitForIdle();
98
99
fChooserClosedSignal.await();
100
if (selectedDir == null) {
101
throw new RuntimeException("No directory was selected in JFileChooser.");
102
}
103
System.out.println(String.format(
104
"The selected directory is '%s'.", selectedDir.getAbsolutePath()));
105
if (selectedDir.getName().equals("d")) {
106
throw new RuntimeException(
107
"JFileChooser removed trailing spaces in the selected directory name. " +
108
"Expected 'd ' got '" + selectedDir.getName() + "'.");
109
} else if (!selectedDir.getName().equals("d ")) {
110
throw new RuntimeException("The selected directory name is not "
111
+ "the expected 'd ' but '" + selectedDir.getName() + "'.");
112
}
113
} catch (UnsupportedLookAndFeelException | AWTException | IOException | InterruptedException e) {
114
throw new RuntimeException(e);
115
} finally {
116
if (dir2 != null) {
117
dir2.delete();
118
}
119
if (dir1 != null) {
120
dir1.delete();
121
}
122
}
123
}
124
125
public static void main(String[] args) {
126
runTest();
127
}
128
}
129
130