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