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