1 /*
   2  * Copyright (c) 2014, 2016, 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 
  26 package test.robot.com.sun.glass.ui.monocle;
  27 
  28 import com.sun.glass.ui.monocle.TestLogShim;
  29 import javafx.application.Platform;
  30 import javafx.geometry.Point2D;
  31 import javafx.scene.input.KeyCode;
  32 import javafx.scene.input.MouseButton;
  33 import javafx.scene.robot.Robot;
  34 
  35 import org.junit.Before;
  36 import org.junit.Rule;
  37 import org.junit.Test;
  38 import org.junit.rules.TestName;
  39 
  40 import static org.junit.Assert.assertEquals;
  41 
  42 /**
  43  * This is a generic test for Glass robot. It is in the monocle.input package
  44  * because it uses the same test infrastructure as the Monocle input tests.
  45  */
  46 public class RobotTest {
  47 
  48     @Rule public TestName name = new TestName();
  49 
  50     @Before
  51     public void setUpScreen() throws Exception {
  52         TestLogShim.reset();
  53         TestLogShim.log(name.getMethodName());
  54         TestApplication.showFullScreenScene();
  55     }
  56 
  57     @Test
  58     public void clickTest() throws Exception {
  59         TestApplication.getStage().getScene().setOnMouseClicked(
  60                 (e) -> TestLogShim.format("Clicked at %.0f, %.0f",
  61                                       e.getScreenX(), e.getScreenY()));
  62         Platform.runLater(() -> {
  63             Robot robot = new Robot();
  64             robot.mouseMove(300, 400);
  65             robot.mousePress(MouseButton.PRIMARY);
  66             robot.mouseRelease(MouseButton.PRIMARY);
  67             assertEquals(new Point2D(300, 400), robot.getMousePosition());
  68             assertEquals(300, (int) robot.getMouseX());
  69             assertEquals(400, (int) robot.getMouseY());
  70         });
  71         TestLogShim.waitForLog("Clicked at 300, 400");
  72 
  73         Platform.runLater(() -> {
  74             Robot robot = new Robot();
  75             robot.mouseMove(new Point2D(300, 400));
  76             robot.mouseClick(MouseButton.PRIMARY);
  77             assertEquals(new Point2D(300, 400), robot.getMousePosition());
  78             assertEquals(300, (int) robot.getMouseX());
  79             assertEquals(400, (int) robot.getMouseY());
  80         });
  81         TestLogShim.waitForLog("Clicked at 300, 400");
  82     }
  83 
  84     @Test
  85     public void typeTest() throws Exception {
  86         TestApplication.getStage().getScene().setOnKeyTyped(
  87                 (e) ->TestLogShim.format("Typed '%s'", e.getCharacter()));
  88         Platform.runLater(() -> {
  89             Robot robot = new Robot();
  90             robot.keyPress(KeyCode.A);
  91             robot.keyRelease(KeyCode.A);
  92         });
  93         TestLogShim.waitForLog("Typed 'a'");
  94         Platform.runLater(() -> {
  95            Robot robot = new Robot();
  96            robot.keyType(KeyCode.E);
  97         });
  98         TestLogShim.waitForLog("Typed 'e'");
  99         Platform.runLater(() -> {
 100             Robot robot = new Robot();
 101             robot.keyPress(KeyCode.SHIFT);
 102             robot.keyPress(KeyCode.B);
 103             robot.keyRelease(KeyCode.B);
 104             robot.keyRelease(KeyCode.SHIFT);
 105         });
 106         TestLogShim.waitForLog("Typed 'B'");
 107     }
 108 
 109     @Test
 110     public void scrollTest() throws Exception {
 111         TestApplication.getStage().getScene().setOnScroll(
 112                 (e) -> TestLogShim.format("Scroll: %.0f at %.0f, %.0f",
 113                                       Math.signum(e.getDeltaY()),
 114                                       e.getScreenX(),
 115                                       e.getScreenY()));
 116         Platform.runLater(() -> {
 117             Robot robot = new Robot();
 118             robot.mouseMove(300, 300);
 119             robot.mouseWheel(10);
 120         });
 121         TestLogShim.waitForLog("Scroll: 1 at 300, 300");
 122         Platform.runLater(() -> {
 123             Robot robot = new Robot();
 124             robot.mouseMove(310, 320);
 125             robot.mouseWheel(-10);
 126         });
 127         TestLogShim.waitForLog("Scroll: -1 at 310, 320");
 128     }
 129 }