1 /*
   2  * Copyright (c) 2014, 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 test.robot.com.sun.glass.ui.monocle.TestApplication;
  30 import test.robot.com.sun.glass.ui.monocle.input.devices.TestTouchDevice;
  31 import test.robot.com.sun.glass.ui.monocle.input.devices.TestTouchDevices;
  32 import javafx.geometry.Rectangle2D;
  33 import javafx.scene.input.TouchEvent;
  34 import org.junit.*;
  35 import org.junit.runners.Parameterized;
  36 import java.util.ArrayList;
  37 import java.util.Collection;
  38 import java.util.List;
  39 
  40 public class SingleTouchNonFullScreenTest extends ParameterizedTestBase {
  41 
  42     private static final TestCase[] TEST_CASES = {
  43             new TestCase(200, 100, 400, 300, 200, 100, 599, 399),
  44             new TestCase(100, 200, 400, 300, 100, 200, 499, 499),
  45     };
  46 
  47     private TestCase testCase;
  48 
  49     static class TestCase {
  50         Rectangle2D stageBounds;
  51         int x1, y1, x2, y2;
  52         TestCase(double winX, double winY, double width,double height, int x1, int y1, int x2, int y2) {
  53             this.stageBounds = new Rectangle2D(winX, winY, width, height);
  54             this.x1 = x1;
  55             this.y1 = y1;
  56             this.x2 = x2;
  57             this.y2 = y2;
  58         }
  59 
  60         public String toString() {
  61             return "TestCase[stage bounds=("
  62                     + stageBounds.getMinX()
  63                     + "," + stageBounds.getMinY()
  64                     + "," + stageBounds.getWidth()
  65                     + "," + stageBounds.getHeight() + ")"
  66                     + ", x1=" + x1
  67                     + ", y1=" + y1
  68                     + ", x2=" + x2
  69                     + ", y2=" + y2 + "]";
  70         }
  71     }
  72 
  73     public SingleTouchNonFullScreenTest(TestTouchDevice device, TestCase testCase)
  74     {
  75         super(device, testCase.stageBounds);
  76         this.testCase = testCase;
  77         TestLogShim.format("Starting test with %s, %s", device, testCase);
  78     }
  79 
  80     @Parameterized.Parameters
  81     public static Collection<Object[]> data() {
  82         List<Object[]> params = new ArrayList<>();
  83         List<TestTouchDevice> devices = TestTouchDevices.getTouchDevices();
  84         for (TestTouchDevice device : devices) {
  85             for (TestCase testCase : TEST_CASES) {
  86                 params.add(new Object[]{device, testCase});
  87             }
  88         }
  89         return params;
  90     }
  91 
  92     @Before
  93     public void addListener() throws Exception {
  94         TestApplication.getStage().getScene().addEventHandler(
  95                 TouchEvent.TOUCH_PRESSED,
  96                 e -> TestLogShim.format("Touch pressed [relative]: %.0f, %.0f",
  97                         e.getTouchPoint().getX(),
  98                         e.getTouchPoint().getY())
  99         );
 100 
 101         TestApplication.getStage().getScene().addEventHandler(
 102                 TouchEvent.TOUCH_RELEASED,
 103                         e -> TestLogShim.format("Touch released [relative]: %.0f, %.0f",
 104                                 e.getTouchPoint().getX(),
 105                                 e.getTouchPoint().getY()));
 106 
 107         TestApplication.getStage().getScene().addEventHandler(
 108                 TouchEvent.TOUCH_MOVED,
 109                         e -> TestLogShim.format("Touch moved [relative]: %.0f, %.0f",
 110                                 e.getTouchPoint().getX(),
 111                                 e.getTouchPoint().getY()));
 112 
 113     }
 114 
 115     /**
 116      * Touch down and up
 117      */
 118     @Test
 119     public void tap() throws Exception {
 120         final int x1 = testCase.x1;
 121         final int y1 = testCase.y1;
 122 
 123         final int relX1 = x1 - (int) stageBounds.getMinX();
 124         final int relY1 = y1 - (int) stageBounds.getMinY();
 125         // tap
 126         int p = device.addPoint(x1, y1);
 127         device.sync();
 128         // release
 129         device.removePoint(p);
 130         device.sync();
 131         TestLogShim.waitForLog("Mouse pressed: %d, %d", x1, y1);
 132         TestLogShim.waitForLog("Mouse released: %d, %d", x1, y1);
 133         TestLogShim.waitForLog("Mouse clicked: %d, %d", x1, y1);
 134         TestLogShim.waitForLog("Touch pressed [relative]: %d, %d", relX1, relY1);
 135         TestLogShim.waitForLog("Touch pressed: %d, %d", x1, y1);
 136         TestLogShim.waitForLog("Touch released [relative]: %d, %d", relX1, relY1);
 137         TestLogShim.waitForLog("Touch released: %d, %d", x1, y1);
 138 
 139         // Check that the touch event has one touch point.
 140         Assert.assertEquals("Expected only one touch point", 0,
 141                             TestLogShim.getLog().stream()
 142                             .filter(s -> s.startsWith("Touch points count"))
 143                             .filter(s -> !s.startsWith("Touch points count: [1]")).count());
 144     }
 145 
 146     /**
 147      * Touch down, drag, touch up
 148      */
 149     @Test
 150     public void tapAndDrag() throws Exception {
 151         final int x1 = testCase.x1;
 152         final int y1 = testCase.y1;
 153         final int x2 = testCase.x2;
 154         final int y2 = testCase.y2;
 155         final int relX1 = x1 - (int) stageBounds.getMinX();
 156         final int relY1 = y1 - (int) stageBounds.getMinY();
 157         final int relX2 = x2 - (int) stageBounds.getMinX();
 158         final int relY2 = y2 - (int) stageBounds.getMinY();
 159         // tap
 160         int p = device.addPoint(x1, y1);
 161         device.sync();
 162         // drag
 163         device.setPoint(p, x2, y2);
 164         device.sync();
 165         // release
 166         device.removePoint(p);
 167         device.sync();
 168         TestLogShim.waitForLog("Mouse pressed: %d, %d", x1, y1);
 169         TestLogShim.waitForLog("Mouse dragged: %d, %d", x2, y2);
 170         TestLogShim.waitForLog("Mouse released: %d, %d", x2, y2);
 171         TestLogShim.waitForLog("Mouse clicked: %d, %d", x2, y2);
 172         TestLogShim.waitForLog("Touch pressed [relative]: %d, %d", relX1, relY1);
 173         TestLogShim.waitForLog("Touch pressed: %d, %d", x1, y1);
 174         TestLogShim.waitForLog("Touch moved [relative]: %d, %d", relX2, relY2);
 175         TestLogShim.waitForLog("Touch moved: %d, %d", x2, y2);
 176         TestLogShim.waitForLog("Touch released [relative]: %d, %d", relX2, relY2);
 177         TestLogShim.waitForLog("Touch released: %d, %d", x2, y2);
 178         // Check that the touch event has one touch point.
 179         Assert.assertEquals("Expected only one touch point", 0,
 180                             TestLogShim.getLog().stream()
 181                             .filter(s -> s.startsWith("Touch points count"))
 182                             .filter(s -> !s.startsWith("Touch points count: [1]")).count());
 183     }
 184 }