1 /*
   2  * Copyright (c) 2015, 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 8071668
  28   @summary Check whether clipboard see changes from external process after taking ownership
  29   @author Anton Nashatyrev: area=datatransfer
  30   @library /test/lib
  31   @build jdk.test.lib.Utils
  32   @run main ClipboardInterVMTest
  33 */
  34 
  35 import jdk.test.lib.Utils;
  36 
  37 import java.awt.*;
  38 import java.awt.datatransfer.*;
  39 import java.io.BufferedReader;
  40 import java.io.File;
  41 import java.io.IOException;
  42 import java.io.Reader;
  43 import java.util.ArrayList;
  44 import java.util.Arrays;
  45 import java.util.List;
  46 import java.util.concurrent.CountDownLatch;
  47 import java.util.concurrent.TimeUnit;
  48 
  49 public class ClipboardInterVMTest {
  50 
  51     static CountDownLatch lostOwnershipMonitor = new CountDownLatch(1);
  52     static CountDownLatch flavorChangedMonitor = new CountDownLatch(1);
  53     static Process process;
  54 
  55     public static void main(String[] args) throws Throwable {
  56         Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
  57 
  58         if (args.length > 0) {
  59             System.out.println("Changing clip...");
  60             clip.setContents(new StringSelection("pong"), null);
  61             System.out.println("done");
  62             // keeping this process running for a while since on Mac the clipboard
  63             // will be invalidated via NSApplicationDidBecomeActiveNotification
  64             // callback in the main process after this child process finishes
  65             Thread.sleep(60 * 1000);
  66             return;
  67         };
  68 
  69 
  70         clip.setContents(new CustomSelection(), new ClipboardOwner() {
  71             @Override
  72             public void lostOwnership(Clipboard clipboard, Transferable contents) {
  73                 System.out.println("ClipboardInterVMTest.lostOwnership");
  74                 lostOwnershipMonitor.countDown();
  75             }
  76         });
  77 
  78         clip.addFlavorListener(new FlavorListener() {
  79             @Override
  80             public void flavorsChanged(FlavorEvent e) {
  81                 System.out.println("ClipboardInterVMTest.flavorsChanged");
  82                 flavorChangedMonitor.countDown();
  83             }
  84         });
  85 
  86         System.out.println("Starting external clipborad modifier...");
  87         new Thread(() -> runTest(ClipboardInterVMTest.class.getCanonicalName(), "pong")).start();
  88 
  89         String content = "";
  90         long startTime = System.currentTimeMillis();
  91         while (System.currentTimeMillis() - startTime < 30 * 1000) {
  92             Transferable c = clip.getContents(null);
  93             if (c.isDataFlavorSupported(DataFlavor.plainTextFlavor)) {
  94                 Reader reader = DataFlavor.plainTextFlavor.getReaderForText(c);
  95                 content = new BufferedReader(reader).readLine();
  96                 System.out.println(content);
  97                 if (content.equals("pong")) {
  98                     break;
  99                 }
 100             }
 101             Thread.sleep(200);
 102         }
 103 
 104         if (!lostOwnershipMonitor.await(10, TimeUnit.SECONDS)) {
 105             throw new RuntimeException("No LostOwnership event received.");
 106         };
 107 
 108         if (!flavorChangedMonitor.await(10, TimeUnit.SECONDS)) {
 109             throw new RuntimeException("No LostOwnership event received.");
 110         };
 111 
 112         if (!content.equals("pong")) {
 113             throw new RuntimeException("Content was not passed.");
 114         }
 115 
 116         process.destroy();
 117 
 118         System.out.println("Passed.");
 119     }
 120 
 121     private static void runTest(String main, String... args)  {
 122 
 123         try {
 124             List<String> opts = new ArrayList<>();
 125             opts.add(getJavaExe());
 126             opts.addAll(Arrays.asList(Utils.getTestJavaOpts()));
 127             opts.add("-cp");
 128             opts.add(System.getProperty("test.class.path", System.getProperty("java.class.path")));
 129 
 130             opts.add(main);
 131             opts.addAll(Arrays.asList(args));
 132 
 133             ProcessBuilder pb = new ProcessBuilder(opts.toArray(new String[0]));
 134             process = pb.start();
 135         } catch (Throwable throwable) {
 136             throw new RuntimeException(throwable);
 137         }
 138     }
 139 
 140     private static String getJavaExe() throws IOException {
 141         File p  = new File(System.getProperty("java.home"), "bin");
 142         File j = new File(p, "java");
 143         if (!j.canRead()) {
 144             j = new File(p, "java.exe");
 145         }
 146         if (!j.canRead()) {
 147             throw new RuntimeException("Can't find java executable in " + p);
 148         }
 149         return j.getCanonicalPath();
 150     }
 151 
 152     static class CustomSelection implements Transferable {
 153         private static final DataFlavor[] flavors = { DataFlavor.allHtmlFlavor };
 154 
 155         public DataFlavor[] getTransferDataFlavors() {
 156             return flavors;
 157         }
 158 
 159         public boolean isDataFlavorSupported(DataFlavor flavor) {
 160             return flavors[0].equals(flavor);
 161         }
 162 
 163         public Object getTransferData(DataFlavor flavor)
 164                 throws UnsupportedFlavorException, java.io.IOException {
 165             if (isDataFlavorSupported(flavor)) {
 166                 return "ping";
 167             } else {
 168                 throw new UnsupportedFlavorException(flavor);
 169             }
 170         }
 171     }
 172 }