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