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.scene.input.GestureEvent;
  33 import org.junit.*;
  34 import org.junit.runners.Parameterized;
  35 
  36 import java.util.Collection;
  37 
  38 public class SwipeSimpleTest extends ParameterizedTestBase {
  39 
  40     private final int SWIPE_THRESHOLD = 10;
  41     int startPointX;
  42     int startPointY;
  43 
  44     static {
  45         System.setProperty("com.sun.javafx.isEmbedded", "true");
  46     }
  47 
  48     public SwipeSimpleTest(TestTouchDevice device) {
  49         super(device);
  50     }
  51 
  52     @Parameterized.Parameters
  53     public static Collection<Object[]> data() {
  54         return TestTouchDevices.getTouchDeviceParameters(1);
  55     }
  56 
  57     private int getDelta() throws Exception {
  58         int max = Math.max(SWIPE_THRESHOLD, device.getTapRadius());
  59         return max + 1;
  60     }
  61 
  62     @BeforeClass
  63     public static void beforeInit() {
  64         System.setProperty("com.sun.javafx.gestures.swipe.maxduration", "1200");
  65     }
  66     
  67     @Before
  68     public void addListener() throws Exception {
  69         TestApplication.getStage().getScene().addEventHandler(
  70                 GestureEvent.ANY,
  71                 e -> TestLogShim.format("%s at %.0f, %.0f",
  72                         e.getEventType(),
  73                         e.getScreenX(),
  74                         e.getScreenY()));
  75         startPointX = (int) Math.round(width * 0.5);
  76         startPointY = (int) Math.round(height * 0.5);
  77     }
  78 
  79     private void swipe(Point[] points, String expectedSwipe) throws Exception {
  80         Assert.assertTrue(points.length > 1);
  81         int x = points[0].getX();
  82         int y = points[0].getY();
  83         TestLogShim.reset();
  84         int p1 = device.addPoint(x, y);
  85         device.sync();
  86         for (int i = 1; i < points.length; i++) {
  87             device.setPoint(p1, points[i].getX(), points[i].getY());
  88             device.sync();
  89         }
  90         device.removePoint(p1);
  91         device.sync();
  92         int finalX = points[points.length - 1].getX();
  93         int finalY = points[points.length - 1].getY();
  94         TestLogShim.waitForLogContaining("Touch released: %d, %d", finalX, finalY);
  95         TestLogShim.waitForLogContaining("Mouse released: %d, %d", finalX, finalY);
  96         TestLogShim.waitForLogContaining("Mouse clicked: %d, %d", finalX, finalY);
  97         if (expectedSwipe != null) {
  98             TestLogShim.waitForLogContaining(expectedSwipe);
  99         } else {
 100             Assert.assertEquals(0, TestLogShim.countLogContaining("SWIPE"));
 101         }
 102     }
 103 
 104     @Test
 105     public void testSwipeRight1() throws Exception {
 106         Point[] path = {new Point(startPointX, startPointY),
 107                 new Point(startPointX + getDelta(), startPointY)};
 108         swipe(path, "SWIPE_RIGHT");
 109     }
 110 
 111     @Test
 112     public void testSwipeRight2() throws Exception {
 113         Point[] path = {new Point(startPointX, startPointY), new Point(65,
 114                 getDelta(), startPointX, startPointY)};
 115         swipe(path, "SWIPE_RIGHT");
 116     }
 117 
 118     @Test
 119     public void testSwipeRight3() throws Exception {
 120         Point[] path = {new Point(startPointX, startPointY), new Point(80,
 121                 getDelta(), startPointX, startPointY)};
 122         swipe(path, "SWIPE_RIGHT");
 123     }
 124 
 125     @Test
 126     public void testSwipeRight4() throws Exception {
 127         Point[] path = {new Point(startPointX, startPointY), new Point(115,
 128                 getDelta(), startPointX, startPointY)};
 129         swipe(path, "SWIPE_RIGHT");
 130     }
 131 
 132     @Test
 133     public void testSwipeLeft1() throws Exception {
 134         Point[] path = {new Point(startPointX, startPointY),
 135                 new Point(startPointX - getDelta(), startPointY)};
 136         swipe(path, "SWIPE_LEFT");
 137     }
 138 
 139     @Test
 140     public void testSwipeLeft2() throws Exception {
 141         Point[] path = {new Point(startPointX, startPointY), new Point(-65,
 142                 getDelta(), startPointX, startPointY)};
 143         swipe(path, "SWIPE_LEFT");
 144     }
 145 
 146     @Test
 147     public void testSwipeLeft3() throws Exception {
 148         Point[] path = {new Point(startPointX, startPointY), new Point(-80,
 149                 getDelta(), startPointX, startPointY)};
 150         swipe(path, "SWIPE_LEFT");
 151     }
 152 
 153     @Test
 154     public void testSwipeLeft4() throws Exception {
 155         Point[] path = {new Point(startPointX, startPointY), new Point(-115,
 156                 getDelta(), startPointX, startPointY)};
 157         swipe(path, "SWIPE_LEFT");
 158     }
 159 
 160     @Test
 161     public void testSwipeUp1() throws Exception {
 162         Point[] path = {new Point(startPointX, startPointY),
 163                 new Point(startPointX, startPointY - getDelta())};
 164         swipe(path, "SWIPE_UP");
 165     }
 166 
 167     @Test
 168     public void testSwipeUp2() throws Exception {
 169         Point[] path = {new Point(startPointX, startPointY), new Point(25,
 170                 getDelta(), startPointX, startPointY)};
 171         swipe(path, "SWIPE_UP");
 172     }
 173 
 174     @Test
 175     public void testSwipeUp3() throws Exception {
 176         Point[] path = {new Point(startPointX, startPointY), new Point(-25,
 177                 getDelta(), startPointX, startPointY)};
 178         swipe(path, "SWIPE_UP");
 179     }
 180 
 181     @Test
 182     public void testSwipeDown1() throws Exception {
 183         Point[] path = {new Point(startPointX, startPointY),
 184                 new Point(startPointX, startPointY + getDelta())};
 185         swipe(path, "SWIPE_DOWN");
 186     }
 187 
 188     @Test
 189     public void testSwipeDown2() throws Exception {
 190         Point[] path = {new Point(startPointX, startPointY), new Point(155,
 191                 getDelta(), startPointX, startPointY)};
 192         swipe(path, "SWIPE_DOWN");
 193     }
 194 
 195     @Test
 196     public void testSwipeDown3() throws Exception {
 197         Point[] path = {new Point(startPointX, startPointY), new Point(-155,
 198                 getDelta(), startPointX, startPointY)};
 199         swipe(path, "SWIPE_DOWN");
 200     }
 201 
 202     @Test
 203     public void testNoSwipeUp() throws Exception {
 204         Point[] path = {new Point(startPointX, startPointY), new Point(31,
 205                 getDelta(), startPointX, startPointY)};
 206         swipe(path, null);
 207     }
 208 
 209     @Test
 210     public void testNoSwipeRight() throws Exception {
 211         Point[] path = {new Point(startPointX, startPointY), new Point(59,
 212                 getDelta(), startPointX, startPointY)};
 213         swipe(path, null);
 214     }
 215 
 216     @Test
 217     public void testSwipeUp4Points() throws Exception {
 218         int delta = getDelta();
 219         Point p1 = new Point(startPointX, startPointY);
 220         Point p2 = new Point(45, delta, p1);
 221         Point p3 = new Point(22, delta, p2);
 222         Point p4 = new Point(10, delta, p3);
 223         Point[] path = {p1, p2, p3, p4};
 224         swipe(path, "SWIPE_UP");
 225     }
 226 
 227     private class Point {
 228         int x;
 229         int y;
 230 
 231         Point(int x, int y) {
 232             this.x = x;
 233             this.y = y;
 234         }
 235 
 236         /**
 237          * Creation of new point that located a "distance" from (centerX, centerY),
 238          * and defined by "angle" variable, when 0 degrees position is on axis y.
 239          */
 240         Point(int angle, int distance, int centerX, int centerY) {
 241             int transformedAngle = 90 - angle;
 242             this.x = centerX + (int) Math.round(distance * Math.cos(Math
 243                     .toRadians(transformedAngle)));
 244             this.y = centerY - (int) Math.round(distance * Math.sin(Math
 245                     .toRadians(transformedAngle)));
 246         }
 247 
 248         Point(int angle, int radius, Point p) {
 249             this(angle, radius, p.getX(), p.getY());
 250         }
 251 
 252         public int getX() {
 253             return x;
 254         }
 255 
 256         public int getY() {
 257             return y;
 258         }
 259     }
 260 }