Skip to content

Commit 43c8c5c

Browse files
author
Satyen Subramaniam
committed
8354495: Open source several AWT DataTransfer tests
Backport-of: 9c86ac27236a67ff7d84447821d89772b993f7e1
1 parent e4bb47b commit 43c8c5c

File tree

4 files changed

+465
-0
lines changed

4 files changed

+465
-0
lines changed

test/jdk/ProblemList.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ java/awt/Mixing/NonOpaqueInternalFrame.java 7124549 macosx-all
186186
java/awt/Focus/ActualFocusedWindowTest/ActualFocusedWindowRetaining.java 6829264 generic-all
187187
java/awt/datatransfer/DragImage/MultiResolutionDragImageTest.java 8080982 generic-all
188188
java/awt/datatransfer/SystemFlavorMap/AddFlavorTest.java 8079268 linux-all
189+
java/awt/datatransfer/ClipboardPerformanceTest.java 8029022 windows-all
190+
java/awt/datatransfer/HTMLTransferConsoleOutputTest.java 8237254 macosx-all
191+
java/awt/datatransfer/ImageTransferCrashTest.java 8237253 macosx-all
189192
java/awt/Toolkit/RealSync/Test.java 6849383 linux-all
190193
java/awt/LightweightComponent/LightweightEventTest/LightweightEventTest.java 8159252,8324782 windows-all,macosx-all
191194
java/awt/EventDispatchThread/HandleExceptionOnEDT/HandleExceptionOnEDT.java 8072110 macosx-all
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright (c) 2001, 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 4463560
27+
* @requires (os.family == "windows")
28+
* @summary Tests that datatransfer doesn't take too much time to complete
29+
* @key headful
30+
* @library /test/lib
31+
* @run main/timeout=300 ClipboardPerformanceTest
32+
*/
33+
34+
import java.awt.Toolkit;
35+
import java.awt.datatransfer.Clipboard;
36+
import java.awt.datatransfer.DataFlavor;
37+
import java.awt.datatransfer.StringSelection;
38+
import java.awt.datatransfer.Transferable;
39+
import java.util.concurrent.TimeUnit;
40+
import java.util.concurrent.TimeoutException;
41+
42+
import jdk.test.lib.process.OutputAnalyzer;
43+
import jdk.test.lib.process.ProcessTools;
44+
45+
public class ClipboardPerformanceTest {
46+
public static final int CODE_FAILURE = 1;
47+
public static final int CODE_OTHER_FAILURE = 2;
48+
static String eoln;
49+
static char[] text;
50+
public static final int ARRAY_SIZE = 100000;
51+
public static final int RATIO_THRESHOLD = 10;
52+
53+
public static void main(String[] args) throws Exception {
54+
if (args.length == 0) {
55+
ClipboardPerformanceTest clipboardPerformanceTest = new ClipboardPerformanceTest();
56+
clipboardPerformanceTest.initialize();
57+
return;
58+
}
59+
60+
long before, after, oldTime, newTime;
61+
float ratio;
62+
63+
try {
64+
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
65+
before = System.currentTimeMillis();
66+
String ss = (String) t.getTransferData(new DataFlavor("text/plain; class=java.lang.String"));
67+
after = System.currentTimeMillis();
68+
69+
System.err.println("Size: " + ss.length());
70+
newTime = after - before;
71+
System.err.println("Time consumed: " + newTime);
72+
73+
initArray();
74+
75+
StringBuffer buf = new StringBuffer(new String(text));
76+
int eoln_len = eoln.length();
77+
before = System.currentTimeMillis();
78+
79+
for (int i = 0; i + eoln_len <= buf.length(); i++) {
80+
if (eoln.equals(buf.substring(i, i + eoln_len))) {
81+
buf.replace(i, i + eoln_len, "\n");
82+
}
83+
}
84+
85+
after = System.currentTimeMillis();
86+
oldTime = after - before;
87+
System.err.println("Old algorithm: " + oldTime);
88+
ratio = oldTime / newTime;
89+
System.err.println("Ratio: " + ratio);
90+
91+
if (ratio < RATIO_THRESHOLD) {
92+
System.out.println("Time ratio failure!!");
93+
System.exit(CODE_FAILURE);
94+
}
95+
} catch (Throwable e) {
96+
e.printStackTrace();
97+
System.exit(CODE_OTHER_FAILURE);
98+
}
99+
System.out.println("Test Pass!");
100+
}
101+
102+
public static void initArray() {
103+
text = new char[ARRAY_SIZE + 2];
104+
105+
for (int i = 0; i < ARRAY_SIZE; i += 3) {
106+
text[i] = '\r';
107+
text[i + 1] = '\n';
108+
text[i + 2] = 'a';
109+
}
110+
eoln = "\r\n";
111+
}
112+
113+
public void initialize() throws Exception {
114+
initArray();
115+
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
116+
cb.setContents(new StringSelection(new String(text)), null);
117+
118+
ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(
119+
ClipboardPerformanceTest.class.getName(),
120+
"child"
121+
);
122+
123+
Process process = ProcessTools.startProcess("Child", pb);
124+
OutputAnalyzer outputAnalyzer = new OutputAnalyzer(process);
125+
126+
if (!process.waitFor(15, TimeUnit.SECONDS)) {
127+
process.destroyForcibly();
128+
throw new TimeoutException("Timed out waiting for Child");
129+
}
130+
131+
outputAnalyzer.shouldHaveExitValue(0);
132+
}
133+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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 4638351
27+
* @summary tests that HTML transfer doesn't cause console output
28+
* @key headful
29+
* @library /test/lib
30+
* @run main HTMLTransferConsoleOutputTest
31+
*/
32+
33+
import java.awt.Toolkit;
34+
import java.awt.datatransfer.Clipboard;
35+
import java.awt.datatransfer.ClipboardOwner;
36+
import java.awt.datatransfer.DataFlavor;
37+
import java.awt.datatransfer.Transferable;
38+
import java.awt.datatransfer.UnsupportedFlavorException;
39+
import java.io.ByteArrayOutputStream;
40+
import java.io.IOException;
41+
import java.io.OutputStream;
42+
import java.io.PrintStream;
43+
import java.io.UnsupportedEncodingException;
44+
import java.util.concurrent.TimeUnit;
45+
import java.util.concurrent.TimeoutException;
46+
47+
import jdk.test.lib.process.OutputAnalyzer;
48+
import jdk.test.lib.process.ProcessTools;
49+
50+
public class HTMLTransferConsoleOutputTest implements ClipboardOwner {
51+
static final Clipboard clipboard =
52+
Toolkit.getDefaultToolkit().getSystemClipboard();
53+
static final DataFlavor dataFlavor =
54+
new DataFlavor("text/html; class=java.lang.String", null);
55+
static final String magic = "TESTMAGICSTRING";
56+
static final Transferable transferable = new Transferable() {
57+
final DataFlavor[] flavors = new DataFlavor[]{dataFlavor};
58+
final String data = "<html><body>" + magic + "</html></body>";
59+
60+
public DataFlavor[] getTransferDataFlavors() {
61+
return flavors;
62+
}
63+
64+
public boolean isDataFlavorSupported(DataFlavor df) {
65+
return dataFlavor.equals(df);
66+
}
67+
68+
public Object getTransferData(DataFlavor df)
69+
throws UnsupportedFlavorException {
70+
if (!isDataFlavorSupported(df)) {
71+
throw new UnsupportedFlavorException(df);
72+
}
73+
return data;
74+
}
75+
};
76+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
77+
public static final int CLIPBOARD_DELAY = 1000;
78+
79+
public static void main(String[] args) throws Exception {
80+
if (args.length == 0) {
81+
HTMLTransferConsoleOutputTest htmlTransferConsoleOutputTest = new HTMLTransferConsoleOutputTest();
82+
htmlTransferConsoleOutputTest.initialize();
83+
return;
84+
}
85+
final ClipboardOwner clipboardOwner = new ClipboardOwner() {
86+
public void lostOwnership(Clipboard clip,
87+
Transferable contents) {
88+
System.exit(0);
89+
}
90+
};
91+
clipboard.setContents(transferable, clipboardOwner);
92+
final Object o = new Object();
93+
synchronized (o) {
94+
try {
95+
o.wait();
96+
} catch (InterruptedException ie) {
97+
ie.printStackTrace();
98+
}
99+
}
100+
System.out.println("Test Pass!");
101+
}
102+
103+
public void initialize() throws Exception {
104+
clipboard.setContents(transferable, this);
105+
ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(
106+
HTMLTransferConsoleOutputTest.class.getName(),
107+
"child"
108+
);
109+
110+
Process process = ProcessTools.startProcess("Child", pb);
111+
OutputAnalyzer outputAnalyzer = new OutputAnalyzer(process);
112+
113+
if (!process.waitFor(15, TimeUnit.SECONDS)) {
114+
process.destroyForcibly();
115+
throw new TimeoutException("Timed out waiting for Child");
116+
}
117+
118+
byte[] bytes = baos.toByteArray();
119+
String string = null;
120+
try {
121+
string = new String(bytes, "ASCII");
122+
} catch (UnsupportedEncodingException uee) {
123+
uee.printStackTrace();
124+
}
125+
if (string.lastIndexOf(magic) != -1) {
126+
throw new RuntimeException("Test failed. Output contains:" +
127+
string);
128+
}
129+
130+
outputAnalyzer.shouldHaveExitValue(0);
131+
}
132+
133+
134+
static class ForkOutputStream extends OutputStream {
135+
final OutputStream outputStream1;
136+
final OutputStream outputStream2;
137+
138+
public ForkOutputStream(OutputStream os1, OutputStream os2) {
139+
outputStream1 = os1;
140+
outputStream2 = os2;
141+
}
142+
143+
public void write(int b) throws IOException {
144+
outputStream1.write(b);
145+
outputStream2.write(b);
146+
}
147+
148+
public void flush() throws IOException {
149+
outputStream1.flush();
150+
outputStream2.flush();
151+
}
152+
153+
public void close() throws IOException {
154+
outputStream1.close();
155+
outputStream2.close();
156+
}
157+
}
158+
159+
public void lostOwnership(Clipboard clip, Transferable contents) {
160+
final Runnable r = () -> {
161+
try {
162+
Thread.sleep(CLIPBOARD_DELAY);
163+
} catch (InterruptedException e) {
164+
e.printStackTrace();
165+
}
166+
final PrintStream oldOut = System.out;
167+
final PrintStream newOut =
168+
new PrintStream(new ForkOutputStream(oldOut, baos));
169+
Transferable t = clipboard.getContents(null);
170+
try {
171+
System.setOut(newOut);
172+
t.getTransferData(dataFlavor);
173+
System.setOut(oldOut);
174+
} catch (IOException | UnsupportedFlavorException ioe) {
175+
ioe.printStackTrace();
176+
}
177+
clipboard.setContents(transferable, null);
178+
};
179+
final Thread t = new Thread(r);
180+
t.start();
181+
}
182+
}

0 commit comments

Comments
 (0)