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