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 
  26 import java.awt.AWTException;
  27 import java.awt.BorderLayout;
  28 import java.awt.Button;
  29 import java.awt.EventQueue;
  30 import java.awt.Frame;
  31 import java.awt.Robot;
  32 import java.awt.event.KeyAdapter;
  33 import java.awt.event.KeyEvent;
  34 import java.awt.event.MouseAdapter;
  35 import java.awt.event.MouseEvent;
  36 import java.io.File;
  37 import java.lang.reflect.InvocationTargetException;
  38 import org.jemmy.Point;
  39 import org.jemmy.control.Wrap;
  40 import org.jemmy.env.Environment;
  41 import org.jemmy.env.Timeout;
  42 import org.jemmy.image.AWTImage;
  43 import org.jemmy.timing.State;
  44 import org.jemmy.timing.Waiter;
  45 import org.testng.annotations.AfterClass;
  46 import org.testng.annotations.AfterMethod;
  47 import org.testng.annotations.BeforeClass;
  48 import org.testng.annotations.BeforeMethod;
  49 import org.testng.annotations.Test;
  50 
  51 import static org.testng.Assert.assertTrue;
  52 
  53 
  54 /**
  55  *
  56  * @author Alexander Kouznetsov <mrkam@mail.ru>
  57  */
  58 public class SmoothMoveTest {
  59 
  60     final static Timeout TIMEOUT = new Timeout("Wait for state to be reached", 10000);
  61     final static Timeout DELTA_TIMEOUT = new Timeout("Delta timeout of wait for state to be reached", 1000);
  62 
  63     public SmoothMoveTest() {
  64     }
  65 
  66     @BeforeClass
  67     public static void setUpClass() throws Exception {
  68         File workdir = new File(System.getProperty("user.dir") + File.separator +
  69                 "build" + File.separator +
  70                 "test" + File.separator +
  71                 "results");
  72         workdir.mkdirs();
  73         AWTImage.setImageRoot(workdir);
  74     }
  75 
  76     @AfterClass
  77     public static void tearDownClass() throws Exception {
  78         RobotDriver.exit();
  79     }
  80 
  81     Frame frm;
  82     Button btn;
  83     Wrap<Object> area;
  84     private volatile boolean mousePressed;
  85     private volatile boolean mouseReleased;
  86     private volatile boolean mouseMoved;
  87     private volatile boolean mouseClicked;
  88     private volatile boolean mouseDragged;
  89     private volatile boolean keyPressed;
  90     private volatile boolean keyReleased;
  91     RobotDriver instance;
  92     Robot rb;
  93 
  94     @BeforeMethod
  95     public void setUp() throws InterruptedException, AWTException, InvocationTargetException {
  96 
  97         Environment.getEnvironment().setProperty(AWTRobotInputFactory.ROBOT_MOUSE_SMOOTHNESS_PROPERTY, "5");
  98 
  99         EventQueue.invokeAndWait(new Runnable() {
 100 
 101             public void run() {
 102                 frm = new Frame("some frame");
 103                 frm.setSize(100, 100);
 104                 frm.setLocation(100, 100);
 105                 btn = new Button("some button");
 106                 MouseAdapter m = new MouseAdapter() {
 107 
 108                     @Override
 109                     public void mousePressed(MouseEvent e) {
 110                         System.out.println("mousePressed event triggered: " + e);
 111                         System.out.flush();
 112                         if ((e.getButton() & MouseEvent.BUTTON1) != 0 && e.isShiftDown()) {
 113                             mousePressed = true;
 114                         }
 115                     }
 116 
 117                     @Override
 118                     public void mouseReleased(MouseEvent e) {
 119                         System.out.println("mouseReleased event triggered: " + e);
 120                         System.out.flush();
 121                         if ((e.getButton() & MouseEvent.BUTTON2) != 0 && e.isControlDown()) {
 122                             mouseReleased = true;
 123                         }
 124                     }
 125 
 126                     @Override
 127                     public void mouseMoved(MouseEvent e) {
 128                         System.out.println("mouseMoved event triggered: " + e);
 129                         System.out.flush();
 130                         mouseMoved = true;
 131                     }
 132 
 133                     @Override
 134                     public void mouseClicked(MouseEvent e) {
 135                         System.out.println("mouseClicked event triggered: " + e);
 136                         System.out.flush();
 137                         if ((e.getButton() & MouseEvent.BUTTON3) != 0 && e.isAltDown()) {
 138                             mouseClicked = true;
 139                         }
 140                     }
 141 
 142                     @Override
 143                     public void mouseDragged(MouseEvent e) {
 144                         System.out.println("mouseDragged event triggered: " + e);
 145                         System.out.flush();
 146                         if ((e.getModifiers() & MouseEvent.BUTTON2_MASK) != 0) {
 147                             mouseDragged = true;
 148                         }
 149                     }
 150 
 151                 };
 152                 btn.addMouseListener(m);
 153                 btn.addMouseMotionListener(m);
 154                 btn.addKeyListener(new KeyAdapter() {
 155 
 156                     @Override
 157                     public void keyPressed(KeyEvent e) {
 158                         System.out.println("keyPressed event triggered: " + e);
 159                         System.out.flush();
 160                         if (e.getKeyCode() == KeyEvent.VK_A && e.isShiftDown()) {
 161                             keyPressed = true;
 162                         }
 163                     }
 164 
 165                     @Override
 166                     public void keyReleased(KeyEvent e) {
 167                         System.out.println("keyReleased event triggered: " + e);
 168                         System.out.flush();
 169                         if (e.getKeyCode() == KeyEvent.VK_B) {
 170                             keyReleased = true;
 171                         }
 172                     }
 173 
 174                 });
 175                 frm.add(btn, BorderLayout.SOUTH);
 176                 frm.doLayout();
 177                 instance = new RobotDriver(Environment.getEnvironment());
 178                 frm.setVisible(true);
 179                 btn.requestFocusInWindow();
 180             }
 181         });
 182 
 183         rb = new Robot();
 184         rb.waitForIdle();
 185 
 186         RobotExecutor.get().setRunInOtherJVM(false);
 187     }
 188 
 189     @AfterMethod
 190     public void tearDown() throws InterruptedException, InvocationTargetException {
 191         EventQueue.invokeAndWait(new Runnable() {
 192 
 193             public void run() {
 194                 frm.setVisible(false);
 195             }
 196         });
 197     }
 198 
 199     /**
 200      * Test of moveMouse method in right-down direction of class RobotDriver.
 201      */
 202     @Test
 203     public void testMoveSmoothMousePP() throws InterruptedException {
 204         System.out.println("testMoveSmoothMousePP");
 205         Thread.sleep(3000);
 206         mouseMoved = false;
 207         java.awt.Point locationOnScreen = btn.getLocationOnScreen();
 208         System.out.println("Moving mouse");
 209         Point startPoint = new Point(locationOnScreen.x - 10, locationOnScreen.y - 10);
 210         Point endPoint = new Point(locationOnScreen.x + btn.getWidth() + 10, locationOnScreen.y + btn.getHeight() + 10);
 211         instance.moveMouse(startPoint);
 212         Thread.sleep(100);
 213         instance.moveMouse(endPoint);
 214 
 215         rb.waitForIdle();
 216         new Waiter(TIMEOUT, DELTA_TIMEOUT).ensureState(new State<Boolean>(){
 217 
 218             public Boolean reached() {
 219                 return mouseMoved ? true : null;
 220             }
 221 
 222         });
 223         assertTrue(mouseMoved);
 224     }
 225 
 226     /**
 227      * Test of moveMouse method in left-down direction of class RobotDriver.
 228      */
 229     @Test
 230     public void testMoveSmoothMouseNP() throws InterruptedException {
 231         System.out.println("testMoveSmoothMouseNP");
 232         Thread.sleep(3000);
 233         mouseMoved = false;
 234         java.awt.Point locationOnScreen = btn.getLocationOnScreen();
 235         System.out.println("Moving mouse");
 236         Point startPoint = new Point(locationOnScreen.x + btn.getWidth() + 10, locationOnScreen.y - 10);
 237         Point endPoint = new Point(locationOnScreen.x - 10, locationOnScreen.y + btn.getHeight() + 10);
 238         instance.moveMouse(startPoint);
 239         Thread.sleep(100);
 240         instance.moveMouse(endPoint);
 241 
 242         rb.waitForIdle();
 243         new Waiter(TIMEOUT, DELTA_TIMEOUT).ensureState(new State<Boolean>(){
 244 
 245             public Boolean reached() {
 246                 return mouseMoved ? true : null;
 247             }
 248 
 249         });
 250         assertTrue(mouseMoved);
 251     }
 252 
 253     /**
 254      * Test of moveMouse method in left-up direction of class RobotDriver.
 255      */
 256     @Test
 257     public void testMoveSmoothMouseNN() throws InterruptedException {
 258         System.out.println("testMoveSmoothMouseNN");
 259         Thread.sleep(3000);
 260         mouseMoved = false;
 261         java.awt.Point locationOnScreen = btn.getLocationOnScreen();
 262         System.out.println("Moving mouse");
 263         Point startPoint = new Point(locationOnScreen.x + btn.getWidth() + 10, locationOnScreen.y + btn.getHeight() + 10);
 264         Point endPoint = new Point(locationOnScreen.x - 10, locationOnScreen.y - 10);
 265         instance.moveMouse(startPoint);
 266         Thread.sleep(100);
 267         instance.moveMouse(endPoint);
 268 
 269         rb.waitForIdle();
 270         new Waiter(TIMEOUT, DELTA_TIMEOUT).ensureState(new State<Boolean>(){
 271 
 272             public Boolean reached() {
 273                 return mouseMoved ? true : null;
 274             }
 275 
 276         });
 277         assertTrue(mouseMoved);
 278     }
 279 
 280     /**
 281      * Test of moveMouse method in right-up direction of class RobotDriver.
 282      */
 283     @Test
 284     public void testMoveSmoothMousePN() throws InterruptedException {
 285         System.out.println("testMoveSmoothMousePN");
 286         Thread.sleep(3000);
 287         mouseMoved = false;
 288         java.awt.Point locationOnScreen = btn.getLocationOnScreen();
 289         System.out.println("Moving mouse");
 290         Point startPoint = new Point(locationOnScreen.x - 10, locationOnScreen.y + btn.getHeight() + 10);
 291         Point endPoint = new Point(locationOnScreen.x + btn.getWidth() + 10, locationOnScreen.y - 10);
 292         instance.moveMouse(startPoint);
 293         Thread.sleep(100);
 294         instance.moveMouse(endPoint);
 295 
 296         rb.waitForIdle();
 297         new Waiter(TIMEOUT, DELTA_TIMEOUT).ensureState(new State<Boolean>(){
 298 
 299             public Boolean reached() {
 300                 return mouseMoved ? true : null;
 301             }
 302 
 303         });
 304         assertTrue(mouseMoved);
 305     }
 306 
 307     /**
 308      * Test of moveMouse method along X axis of class RobotDriver.
 309      */
 310     @Test
 311     public void testMoveSmoothMouseX() throws InterruptedException {
 312         System.out.println("testMoveSmoothMouseX");
 313         Thread.sleep(3000);
 314         mouseMoved = false;
 315         java.awt.Point locationOnScreen = btn.getLocationOnScreen();
 316         System.out.println("Moving mouse");
 317         Point startPoint = new Point(locationOnScreen.x - 10,
 318                 locationOnScreen.y + btn.getHeight() / 2);
 319         Point endPoint = new Point(locationOnScreen.x + btn.getWidth() + 10,
 320                 locationOnScreen.y + btn.getHeight() / 2);
 321         instance.moveMouse(startPoint);
 322         Thread.sleep(100);
 323         instance.moveMouse(endPoint);
 324 
 325         rb.waitForIdle();
 326         new Waiter(TIMEOUT, DELTA_TIMEOUT).ensureState(new State<Boolean>(){
 327 
 328             public Boolean reached() {
 329                 return mouseMoved ? true : null;
 330             }
 331 
 332         });
 333         assertTrue(mouseMoved);
 334     }
 335 
 336     /**
 337      * Test of moveMouse method along Y axis of class RobotDriver.
 338      */
 339     @Test
 340     public void testMoveSmoothMouseY() throws InterruptedException {
 341         System.out.println("testMoveSmoothMouseY");
 342         Thread.sleep(3000);
 343         mouseMoved = false;
 344         java.awt.Point locationOnScreen = btn.getLocationOnScreen();
 345         System.out.println("Moving mouse");
 346         Point startPoint = new Point(locationOnScreen.x + btn.getWidth() / 2,
 347                 locationOnScreen.y - 10);
 348         Point endPoint = new Point(locationOnScreen.x + btn.getWidth() / 2,
 349                 locationOnScreen.y + btn.getHeight() + 10);
 350         instance.moveMouse(startPoint);
 351         Thread.sleep(100);
 352         instance.moveMouse(endPoint);
 353 
 354         rb.waitForIdle();
 355         new Waiter(TIMEOUT, DELTA_TIMEOUT).ensureState(new State<Boolean>(){
 356 
 357             public Boolean reached() {
 358                 return mouseMoved ? true : null;
 359             }
 360 
 361         });
 362         assertTrue(mouseMoved);
 363     }
 364 
 365     /**
 366      * Test of moveMouse method with smooth set to false, of class RobotDriver.
 367      */
 368     @Test
 369     public void testMoveSmoothMouseDifferentDrivers() throws InterruptedException {
 370         System.out.println("testMoveSmoothMouseDifferentDrivers");
 371         Thread.sleep(3000);
 372         mouseMoved = false;
 373         java.awt.Point locationOnScreen = btn.getLocationOnScreen();
 374         System.out.println("Moving mouse");
 375         Point startPoint = new Point(locationOnScreen.x + btn.getWidth() + 10, locationOnScreen.y - 10);
 376         Point endPoint = new Point(locationOnScreen.x - 10, locationOnScreen.y + btn.getHeight() + 10);
 377         instance.moveMouse(startPoint);
 378         Thread.sleep(100);
 379         instance = new RobotDriver(Environment.getEnvironment());
 380         instance.moveMouse(endPoint);
 381 
 382         rb.waitForIdle();
 383         new Waiter(TIMEOUT, DELTA_TIMEOUT).ensureState(new State<Boolean>(){
 384 
 385             public Boolean reached() {
 386                 return mouseMoved ? true : null;
 387             }
 388 
 389         });
 390         assertTrue(mouseMoved);
 391     }
 392 }