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