Skip to content

Commit 90a37b0

Browse files
committed
8354554: Open source several clipboard tests batch1
Backport-of: 4873eec
1 parent 0c5b28b commit 90a37b0

File tree

5 files changed

+637
-0
lines changed

5 files changed

+637
-0
lines changed

test/jdk/ProblemList.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ java/awt/print/PrinterJob/GlyphPositions.java 7003378 generic-all
251251
java/awt/Choice/ChoiceMouseWheelTest/ChoiceMouseWheelTest.java 6849371 macosx-all,linux-all
252252
java/awt/Component/GetScreenLocTest/GetScreenLocTest.java 4753654 generic-all
253253
java/awt/Component/SetEnabledPerformance/SetEnabledPerformance.java 8165863 macosx-all
254+
java/awt/Clipboard/PasteNullToTextComponentsTest.java 8234140 macosx-all,windows-all
255+
java/awt/Clipboard/NoOwnerNoTargetsTest.java 8234140 macosx-all
256+
java/awt/Clipboard/LostOwnershipChainTest/SystemClipboard2ProcTest.java 8234140 macosx-all
254257
java/awt/Clipboard/HTMLTransferTest/HTMLTransferTest.java 8017454 macosx-all
255258
java/awt/Frame/MiscUndecorated/RepaintTest.java 8266244 macosx-aarch64
256259
java/awt/Modal/FileDialog/FileDialogAppModal1Test.java 7186009 macosx-all
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2002, 2025, 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+
* @bug 4177171 4180145 4180148
27+
* @summary Can't copy to clipboard
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual ClipRWTest
31+
*/
32+
33+
import java.awt.Button;
34+
import java.awt.FlowLayout;
35+
import java.awt.Frame;
36+
import java.awt.TextField;
37+
import java.awt.datatransfer.Clipboard;
38+
import java.awt.datatransfer.DataFlavor;
39+
import java.awt.datatransfer.StringSelection;
40+
41+
public class ClipRWTest {
42+
private static final String INSTRUCTIONS = """
43+
1. Type some text in the text field and press Copy Text.
44+
2. Switch to a _native_ application (e.g. Notepad) and paste the text in
45+
3. Verify the text that is pasted matches what you typed in the Java window
46+
4. In the native app, type some new text and copy it
47+
5. Switch back to the test frame and press Paste Text
48+
6. Verify the text that is pasted matches what you typed in the native app
49+
""";
50+
51+
public static void main(String[] args) throws Exception {
52+
PassFailJFrame.builder()
53+
.title("ClipRWTest Instructions")
54+
.instructions(INSTRUCTIONS)
55+
.columns(40)
56+
.testUI(ClipFrame::new)
57+
.build()
58+
.awaitAndCheck();
59+
}
60+
61+
private static class ClipFrame extends Frame {
62+
TextField field =new TextField(50);
63+
Button copyText = new Button("Copy Text");
64+
Button pasteText = new Button("Paste Text");
65+
Clipboard clipboard;
66+
67+
public ClipFrame() {
68+
super("ClipRWTest 4177171");
69+
setLayout(new FlowLayout());
70+
71+
clipboard = getToolkit().getSystemClipboard();
72+
73+
add(field);
74+
add(copyText);
75+
add(pasteText);
76+
77+
copyText.addActionListener(
78+
ev -> {
79+
String text = field.getText();
80+
try {
81+
clipboard.setContents(new StringSelection(text), null);
82+
} catch (Exception ex) {
83+
ex.printStackTrace();
84+
}
85+
}
86+
);
87+
88+
pasteText.addActionListener(
89+
ev -> {
90+
String text = "";
91+
try {
92+
text = (String) clipboard.getContents(null)
93+
.getTransferData(DataFlavor.stringFlavor);
94+
} catch (Exception ex) {
95+
ex.printStackTrace();
96+
}
97+
field.setText(text);
98+
}
99+
);
100+
101+
pack();
102+
}
103+
}
104+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* Copyright (c) 2002, 2025, 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+
* @bug 4683804
27+
* @summary Tests that in ClipboardOwner.lostOwnership() Clipboard.getContents()
28+
* returns actual contents of the clipboard and Clipboard.setContents()
29+
* can set contents of the clipboard and its owner. The clipboard is
30+
* the system clipboard and the owners of the clipboard are in
31+
* 2 different processes.
32+
* @key headful
33+
* @library /test/lib
34+
* @run main SystemClipboard2ProcTest
35+
*/
36+
37+
import java.awt.Toolkit;
38+
import java.awt.datatransfer.Clipboard;
39+
import java.awt.datatransfer.ClipboardOwner;
40+
import java.awt.datatransfer.DataFlavor;
41+
import java.awt.datatransfer.StringSelection;
42+
import java.awt.datatransfer.Transferable;
43+
import java.util.concurrent.TimeUnit;
44+
import java.util.concurrent.TimeoutException;
45+
46+
import jdk.test.lib.process.OutputAnalyzer;
47+
import jdk.test.lib.process.ProcessTools;
48+
49+
public class SystemClipboard2ProcTest {
50+
51+
public static void main(String[] args) throws Exception {
52+
SystemClipboardOwner.run();
53+
54+
if (SystemClipboardOwner.failed) {
55+
throw new RuntimeException("test failed: can not get actual " +
56+
"contents of the clipboard or set owner of the clipboard");
57+
} else {
58+
System.err.println("test passed");
59+
}
60+
}
61+
}
62+
63+
class SystemClipboardOwner implements ClipboardOwner {
64+
static volatile boolean failed;
65+
66+
private static final Object LOCK = new Object();
67+
68+
private static final int CHAIN_LENGTH = 5;
69+
private final static Clipboard clipboard =
70+
Toolkit.getDefaultToolkit().getSystemClipboard();
71+
72+
private int m, id;
73+
74+
public SystemClipboardOwner(int m) { this.m = m; id = m; }
75+
76+
public void lostOwnership(Clipboard cb, Transferable contents) {
77+
System.err.println(id + " lost clipboard ownership");
78+
79+
Transferable t = getClipboardContents(cb, null);
80+
// for test passing if t.getTransferData() will throw an exception
81+
String msg = "" + (m + 1);
82+
try {
83+
msg = (String)t.getTransferData(DataFlavor.stringFlavor);
84+
} catch (Exception e) {
85+
System.err.println(id + " can't getTransferData: " + e);
86+
}
87+
System.err.println(id + " Clipboard.getContents(): " + msg);
88+
if (!msg.equals("" + (m + 1))) {
89+
failed = true;
90+
System.err.println("Clipboard.getContents() returned incorrect contents!");
91+
}
92+
93+
m += 2;
94+
if (m <= CHAIN_LENGTH) {
95+
System.err.println(id + " Clipboard.setContents(): " + m);
96+
setClipboardContents(cb, new StringSelection(m + ""), this);
97+
}
98+
if (m >= CHAIN_LENGTH) {
99+
synchronized (LOCK) {
100+
LOCK.notifyAll();
101+
}
102+
}
103+
}
104+
105+
public static void run() throws Exception {
106+
SystemClipboardOwner cbo1 = new SystemClipboardOwner(0);
107+
System.err.println(cbo1.m + " Clipboard.setContents(): " + cbo1.m);
108+
setClipboardContents(clipboard, new StringSelection(cbo1.m + ""),
109+
cbo1);
110+
111+
ProcessBuilder pb = ProcessTools
112+
.createTestJavaProcessBuilder(SystemClipboardOwner.class.getName());
113+
114+
Process process = ProcessTools.startProcess("Child", pb);
115+
OutputAnalyzer outputAnalyzer = new OutputAnalyzer(process);
116+
117+
if (!process.waitFor(15, TimeUnit.SECONDS)) {
118+
process.destroyForcibly();
119+
throw new TimeoutException("Timed out waiting for Child");
120+
}
121+
122+
outputAnalyzer.shouldHaveExitValue(0);
123+
124+
if (cbo1.m < CHAIN_LENGTH) {
125+
failed = true;
126+
System.err.println("chain of calls of lostOwnership() broken!");
127+
}
128+
}
129+
130+
public static void main(String[] args) throws InterruptedException {
131+
SystemClipboardOwner cbo2 = new SystemClipboardOwner(1);
132+
System.err.println(cbo2.m + " Clipboard.setContents(): " + cbo2.m);
133+
synchronized (LOCK) {
134+
setClipboardContents(clipboard, new StringSelection(cbo2.m + ""),
135+
cbo2);
136+
LOCK.wait();
137+
}
138+
}
139+
140+
private static void setClipboardContents(Clipboard cb,
141+
Transferable contents,
142+
ClipboardOwner owner) {
143+
synchronized (cb) {
144+
boolean set = false;
145+
while (!set) {
146+
try {
147+
cb.setContents(contents, owner);
148+
set = true;
149+
} catch (IllegalStateException ise) {
150+
try { Thread.sleep(100); }
151+
catch (InterruptedException e) { e.printStackTrace(); }
152+
}
153+
}
154+
}
155+
}
156+
157+
private static Transferable getClipboardContents(Clipboard cb,
158+
Object requestor) {
159+
synchronized (cb) {
160+
while (true) {
161+
try {
162+
Transferable t = cb.getContents(requestor);
163+
return t;
164+
} catch (IllegalStateException ise) {
165+
try { Thread.sleep(100); }
166+
catch (InterruptedException e) { e.printStackTrace(); }
167+
}
168+
}
169+
}
170+
}
171+
}

0 commit comments

Comments
 (0)