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