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