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