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