1 /*
   2  * Copyright (c) 2007, 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 /*
  25   @test
  26   @key headful
  27   @bug 6887703
  28   @summary Unsigned applet can retrieve the dragged information before drop action occurs
  29   @library ../../regtesthelpers
  30   @library ../../regtesthelpers/process
  31   @build Util
  32   @build ProcessResults ProcessCommunicator
  33   @run main/othervm DragInterceptorAppletTest main
  34 */
  35 
  36 import java.awt.Point;
  37 import java.awt.Robot;
  38 import java.awt.event.InputEvent;
  39 
  40 import test.java.awt.regtesthelpers.Util;
  41 import test.java.awt.regtesthelpers.process.ProcessCommunicator;
  42 import test.java.awt.regtesthelpers.process.ProcessResults;
  43 
  44 import static java.lang.Thread.sleep;
  45 
  46 public class DragInterceptorAppletTest {
  47 
  48     public void start() {
  49 
  50         SourceFrame sourceFrame = new SourceFrame();
  51 
  52         Util.waitForIdle(null);
  53 
  54         String [] args = new String [] {
  55             String.valueOf(sourceFrame.getNextLocationX()),
  56             String.valueOf(sourceFrame.getNextLocationY()),
  57             String.valueOf(sourceFrame.getDragSourcePointX()),
  58             String.valueOf(sourceFrame.getDragSourcePointY()),
  59         };
  60         String classpath = System.getProperty("java.class.path");
  61         ProcessResults processResults =
  62             ProcessCommunicator.executeChildProcess(this.getClass(),classpath,args);
  63 
  64         verifyTestResults(processResults);
  65 
  66     }// start()
  67 
  68     private static void verifyTestResults(ProcessResults processResults) {
  69 
  70     switch (processResults.getExitValue()) {
  71         case InterprocessMessages.DATA_WAS_INTERCEPTED_AND_EXCEPTION_HANDLER_WAS_NOT_TRIGGERED:
  72             processResults.printProcessErrorOutput(System.err);
  73             throw new RuntimeException("TEST IS FAILED: Target applet can intercept data " +
  74                     "without a clipboard permission and an exception handler was not triggered.");
  75             //Unreachable...
  76 
  77         case InterprocessMessages.DATA_WAS_INTERCEPTED:
  78             processResults.printProcessErrorOutput(System.err);
  79             throw new RuntimeException("TEST IS FAILED: Target applet can intercept data " +
  80                     "without a clipboard permission");
  81             //Unreachable...
  82 
  83         case InterprocessMessages.EXCEPTION_HANDLER_WAS_NOT_TRIGGERED:
  84             processResults.printProcessErrorOutput(System.err);
  85             throw new RuntimeException("TEST IS FAILED: An exception handler was not triggered.");
  86             //Unreachable...
  87 
  88     }
  89 
  90         //    The child process throws an exception. do not look at the stderr.
  91         processResults.verifyStdErr(System.err);
  92         processResults.verifyProcessExitValue(System.err);
  93         processResults.printProcessStandartOutput(System.out);
  94     }
  95 
  96     //We cannot make an instance of the applet without the default constructor
  97     public DragInterceptorAppletTest() {
  98         super();
  99     }
 100 
 101     //We need in this constructor to pass frame position between JVMs
 102     public DragInterceptorAppletTest(Point targetFrameLocation, Point dragSourcePoint)
 103             throws InterruptedException
 104     {
 105         DragInterceptorFrame targetFrame = new DragInterceptorFrame(targetFrameLocation);
 106 
 107         Util.waitForIdle(null);
 108 
 109         final Robot robot = Util.createRobot();
 110 
 111         robot.mouseMove((int)dragSourcePoint.getX(),(int)dragSourcePoint.getY());
 112         sleep(100);
 113         robot.mousePress(InputEvent.BUTTON1_MASK);
 114         sleep(100);
 115         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 116         sleep(100);
 117 
 118         Util.drag(robot, dragSourcePoint, targetFrame.getDropTargetPoint(),
 119                 InputEvent.BUTTON1_MASK);
 120 
 121         sleep(2000);
 122         ProcessCommunicator.destroyProcess();
 123     }
 124 
 125     enum InterprocessArguments {
 126         TARGET_FRAME_X_POSITION_ARGUMENT,
 127         TARGET_FRAME_Y_POSITION_ARGUMENT,
 128         DRAG_SOURCE_POINT_X_ARGUMENT,
 129         DRAG_SOURCE_POINT_Y_ARGUMENT;
 130 
 131         int extract (String [] args) {
 132             return Integer.parseInt(args[this.ordinal()]);
 133         }
 134     }
 135 
 136     public static void main(final String[] args) {
 137         if (args.length > 0 && args[0].equals("main")) {
 138             new DragInterceptorAppletTest().start();
 139             return;
 140         }
 141         Point dragSourcePoint = new Point(InterprocessArguments.DRAG_SOURCE_POINT_X_ARGUMENT.extract(args),
 142                 InterprocessArguments.DRAG_SOURCE_POINT_Y_ARGUMENT.extract(args));
 143         Point targetFrameLocation = new Point(InterprocessArguments.TARGET_FRAME_X_POSITION_ARGUMENT.extract(args),
 144                 InterprocessArguments.TARGET_FRAME_Y_POSITION_ARGUMENT.extract(args));
 145         try {
 146             new DragInterceptorAppletTest(targetFrameLocation, dragSourcePoint);
 147         } catch (InterruptedException e) {
 148             e.printStackTrace();
 149             throw new RuntimeException(e);
 150         }
 151     }
 152 
 153 }// class DragInterceptorAppletTest