1 /*
   2  * Copyright (c) 2013, 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   @bug 8005932
  26   @summary Java 7 on mac os x only provides text clipboard formats
  27   @author mikhail.cherkasov@oracle.com
  28   @library ../../regtesthelpers
  29   @library ../../regtesthelpers/process
  30   @build Util
  31   @build ProcessResults ProcessCommunicator
  32   @run applet/othervm MissedHtmlAndRtfBug.html
  33 */
  34 
  35 import java.awt.*;
  36 import java.awt.datatransfer.DataFlavor;
  37 import java.awt.event.*;
  38 import java.applet.Applet;
  39 import java.io.File;
  40 import java.util.ArrayList;
  41 
  42 import test.java.awt.regtesthelpers.process.ProcessCommunicator;
  43 import test.java.awt.regtesthelpers.process.ProcessResults;
  44 import test.java.awt.regtesthelpers.Util;
  45 import sun.awt.OSInfo;
  46 
  47 import static java.lang.Thread.sleep;
  48 
  49 public class MissedHtmlAndRtfBug extends Applet {
  50     public void init() {
  51         setLayout(new BorderLayout());
  52     }//End  init()
  53 
  54     public void start() {
  55         if (OSInfo.getOSType() != OSInfo.OSType.MACOSX
  56                 && OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
  57             System.out.println("This test is for Windows and Mac only. Passed.");
  58             return;
  59         }
  60 
  61         final Frame sourceFrame = new Frame("Source frame");
  62         final SourcePanel sourcePanel = new SourcePanel();
  63         sourceFrame.add(sourcePanel);
  64         sourceFrame.pack();
  65         sourceFrame.addWindowListener(new WindowAdapter() {
  66             @Override
  67             public void windowClosing(WindowEvent e) {
  68                 sourceFrame.dispose();
  69             }
  70         });
  71         sourceFrame.setVisible(true);
  72 
  73         Util.waitForIdle(null);
  74 
  75         NextFramePositionCalculator positionCalculator = new NextFramePositionCalculator(sourceFrame);
  76 
  77         ArrayList<String> args = new ArrayList<String>(5);
  78         args.add(String.valueOf(positionCalculator.getNextLocationX()));
  79         args.add(String.valueOf(positionCalculator.getNextLocationY()));
  80         args.add(String.valueOf(AbsoluteComponentCenterCalculator.calculateXCenterCoordinate(sourcePanel)));
  81         args.add(String.valueOf(AbsoluteComponentCenterCalculator.calculateYCenterCoordinate(sourcePanel)));
  82         args.add(concatStrings(DataFlavorSearcher.RICH_TEXT_NAMES));
  83 
  84         ProcessResults processResults =
  85 //                ProcessCommunicator.executeChildProcess(this.getClass(), "/Users/mcherkasov/ws/clipboard/DataFlover/out/production/DataFlover" +
  86 //                        " -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 ",
  87 //                        args.toArray(new String[0]));
  88                 ProcessCommunicator.executeChildProcess(this.getClass(),
  89                         "." + File.separator + System.getProperty("java.class.path"), args.toArray(new String[]{}));
  90 
  91         verifyTestResults(processResults);
  92 
  93         args.set(args.size() - 1, concatStrings(DataFlavorSearcher.HTML_NAMES));
  94 
  95         ProcessCommunicator.executeChildProcess(this.getClass(),
  96                 "." + File.separator + System.getProperty("java.class.path"), args.toArray(new String[]{}));
  97         verifyTestResults(processResults);
  98 
  99 
 100     }// start()
 101 
 102     private String concatStrings(String[] strings) {
 103         StringBuffer result = new StringBuffer("\"");
 104         for (int i = 0; i < strings.length; i++) {
 105             result.append(strings[i]);
 106             result.append(",");
 107         }
 108         result.append("\"");
 109         return result.toString();
 110     }
 111 
 112 
 113     private static void verifyTestResults(ProcessResults processResults) {
 114         if (InterprocessMessages.DATA_IS_CORRUPTED ==
 115                 processResults.getExitValue()) {
 116             processResults.printProcessErrorOutput(System.err);
 117             throw new RuntimeException("TEST IS FAILED: Target has received" +
 118                     " corrupted data.");
 119         }
 120         processResults.verifyStdErr(System.err);
 121         processResults.verifyProcessExitValue(System.err);
 122         processResults.printProcessStandartOutput(System.out);
 123     }
 124 
 125     //We cannot make an instance of the applet without the default constructor
 126     public MissedHtmlAndRtfBug() {
 127         super();
 128     }
 129 
 130     //We need in this constructor to pass frame position between JVMs
 131     public MissedHtmlAndRtfBug(Point targetFrameLocation, Point dragSourcePoint, DataFlavor df)
 132             throws InterruptedException {
 133         final Frame targetFrame = new Frame("Target frame");
 134         final TargetPanel targetPanel = new TargetPanel(targetFrame, df);
 135         targetFrame.add(targetPanel);
 136         targetFrame.addWindowListener(new WindowAdapter() {
 137             @Override
 138             public void windowClosing(WindowEvent e) {
 139                 targetFrame.dispose();
 140             }
 141         });
 142         targetFrame.setLocation(targetFrameLocation);
 143         targetFrame.pack();
 144         targetFrame.setVisible(true);
 145 
 146         doTest(dragSourcePoint, targetPanel);
 147     }
 148 
 149     private void doTest(Point dragSourcePoint, TargetPanel targetPanel) {
 150         Util.waitForIdle(null);
 151 
 152         final Robot robot = Util.createRobot();
 153 
 154         robot.mouseMove((int) dragSourcePoint.getX(), (int) dragSourcePoint.getY());
 155         try {
 156             sleep(100);
 157             robot.mousePress(InputEvent.BUTTON1_MASK);
 158             sleep(100);
 159             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 160             sleep(100);
 161         } catch (InterruptedException e) {
 162             e.printStackTrace();
 163         }
 164 
 165         Util.drag(robot, dragSourcePoint, new Point(AbsoluteComponentCenterCalculator.calculateXCenterCoordinate(targetPanel),
 166                 AbsoluteComponentCenterCalculator.calculateYCenterCoordinate(targetPanel)),
 167                 InputEvent.BUTTON1_MASK);
 168     }
 169 
 170 
 171     enum InterprocessArguments {
 172         TARGET_FRAME_X_POSITION_ARGUMENT,
 173         TARGET_FRAME_Y_POSITION_ARGUMENT,
 174         DRAG_SOURCE_POINT_X_ARGUMENT,
 175         DRAG_SOURCE_POINT_Y_ARGUMENT,
 176         DATA_FLAVOR_NAMES;
 177 
 178         int extractInt(String[] args) {
 179             return Integer.parseInt(args[this.ordinal()]);
 180         }
 181 
 182         String[] extractStringArray(String[] args) {
 183             return args[this.ordinal()].replaceAll("\"", "").split(",");
 184         }
 185     }
 186 
 187     public static void main(String[] args) {
 188         Point dragSourcePoint = new Point(InterprocessArguments.DRAG_SOURCE_POINT_X_ARGUMENT.extractInt(args),
 189                 InterprocessArguments.DRAG_SOURCE_POINT_Y_ARGUMENT.extractInt(args));
 190         Point targetFrameLocation = new Point(InterprocessArguments.TARGET_FRAME_X_POSITION_ARGUMENT.extractInt(args),
 191                 InterprocessArguments.TARGET_FRAME_Y_POSITION_ARGUMENT.extractInt(args));
 192         String[] names = InterprocessArguments.DATA_FLAVOR_NAMES.extractStringArray(args);
 193 
 194         DataFlavor df = DataFlavorSearcher.getByteDataFlavorForNative(names);
 195         try {
 196             new MissedHtmlAndRtfBug(targetFrameLocation, dragSourcePoint, df);
 197         } catch (InterruptedException e) {
 198             e.printStackTrace();
 199         }
 200     }
 201 
 202 
 203 }