1 /*
   2  * Copyright (c) 2007, 2017, 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 package org.jemmy.input;
  24 
  25 import org.jemmy.JemmyException;
  26 import org.jemmy.control.Wrap;
  27 import org.jemmy.env.Environment;
  28 import org.jemmy.interfaces.ControlInterface;
  29 import org.jemmy.interfaces.ControlInterfaceFactory;
  30 import org.jemmy.interfaces.Drag;
  31 import org.jemmy.interfaces.Keyboard;
  32 import org.jemmy.interfaces.Mouse;
  33 import org.jemmy.interfaces.TypeControlInterface;
  34 import org.jemmy.operators.AWTScreen;
  35 import org.jemmy.operators.Screen;
  36 
  37 /**
  38  *
  39  * @author shura
  40  */
  41 public class AWTRobotInputFactory implements ControlInterfaceFactory {
  42 
  43     /**
  44      * Set this Environment property to true or false to run java.awt.Robot in
  45      * other or the same JVM
  46      */
  47     public static final String OTHER_VM_PROPERTY = "awt.robot.othervm";
  48 
  49     /**
  50      * Set this Environment property to the name of the host where other JVM runs.
  51      * 'localhost' by default
  52      */
  53     public static final String OTHER_VM_HOST_PROPERTY = "awt.robot.othervm.host";
  54 
  55     /**
  56      * Set this Environment property to override the port which is used to
  57      * connect to other JVM
  58      */
  59     public static final String OTHER_VM_PORT_PROPERTY = "awt.robot.othervm.port";
  60 
  61     /**
  62      * Set this Environment property to to the maximum time of waiting for the
  63      * client to connect to the JVM where Robot is running. It also waits the same
  64      * amount of ms for the next connection after the previous terminates.
  65      * Default is 15 min.
  66      */
  67     public static final String OTHER_VM_CONNECTION_TIMEOUT_PROPERTY
  68             = "awt.robot.othervm.connection.timeout";
  69 
  70     /**
  71      * The name of the timeout that is used by default as the delay time for
  72      * java.awt.Robot
  73      * @see java.awt.Robot#setAutoDelay(int)
  74      */
  75     public static final String ROBOT_DELAY_TIMEOUT_NAME = "RobotDriver.DelayTimeout";
  76 
  77     /**
  78      * Set this Environment property to the maximum number of pixels between
  79      * mouse positions during movement
  80      */
  81     public static final String ROBOT_MOUSE_SMOOTHNESS_PROPERTY = "awt.robot.mouse.smoothness";
  82 
  83     /**
  84      * Specifies whether to run java.awt.Robot in other JVM
  85      * @param runInOtherJVM if true then java.awt.Robot will run in other JVM
  86      */
  87     public static void runInOtherJVM(boolean runInOtherJVM) {
  88         RobotExecutor.get().setRunInOtherJVM(runInOtherJVM);
  89     }
  90 
  91     /**
  92      * Returns runInOtherJVM setting
  93      * @return if true then java.awt.Robot is running in other JVM
  94      */
  95     public static boolean isRunInOtherJVM() {
  96         return RobotExecutor.get().isRunInOtherJVM();
  97     }
  98 
  99     /**
 100      * Specifies mouse movements smoothness
 101      * @param mouseSmoothness the maximum number of pixels between
 102      * mouse positions during movement
 103      * @see #ROBOT_MOUSE_SMOOTHNESS_PROPERTY
 104      */
 105     public static void setMouseSmoothness(int mouseSmoothness) {
 106         if(mouseSmoothness <= 0) {
 107             throw new IllegalArgumentException("Mouse smoothness should be greater than zero.");
 108         }
 109         RobotDriver.setMouseSmoothness(mouseSmoothness);
 110     }
 111 
 112     /**
 113      * Gets the mouse movements smoothness
 114      * @return the maximum number of pixels between
 115      * mouse positions during movement
 116      * @see #ROBOT_MOUSE_SMOOTHNESS_PROPERTY
 117      */
 118     public static int getMouseSmoothness() {
 119         return RobotDriver.getMouseSmoothness();
 120     }
 121 
 122     static {
 123         if(Screen.SCREEN == null) {
 124             Screen.setSCREEN(new AWTScreen(Environment.getEnvironment()));
 125         }
 126     }
 127 
 128     public AWTMap getAwtMap() {
 129         return RobotExecutor.get().getAWTMap();
 130     }
 131 
 132     public void setAwtMap(AWTMap awtMap) {
 133         RobotExecutor.get().setAWTMap(awtMap);
 134     }
 135 
 136     public <INTERFACE extends ControlInterface> INTERFACE create(Wrap<?> control, Class<INTERFACE> interfaceClass) {
 137         if(Mouse.class.isAssignableFrom(interfaceClass)) {
 138             return (INTERFACE) new MouseImpl(control);
 139         } else if(Keyboard.class.isAssignableFrom(interfaceClass)) {
 140             return (INTERFACE) new KeyboardImpl(control);
 141         } else if(Drag.class.isAssignableFrom(interfaceClass)) {
 142             return (INTERFACE) new DragImpl(control);
 143         }
 144         throw new JemmyException(AWTRobotInputFactory.class.getName() + " does not support " + interfaceClass.getName());
 145     }
 146 
 147     public <TYPE, INTERFACE extends TypeControlInterface<TYPE>> INTERFACE create(Wrap<?> control, Class<INTERFACE> interfaceClass, Class<TYPE> type) {
 148         throw new JemmyException(AWTRobotInputFactory.class.getName() + " does not support " + interfaceClass.getName());
 149     }
 150 
 151     @Override
 152     public String toString() {
 153         return getClass().getName() + "[otherVM=" + isRunInOtherJVM() + ", mouseSmoothness=" + getMouseSmoothness() + "]";
 154     }
 155 }