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 com.sun.javafx.PlatformUtil;
  31 import org.junit.After;
  32 import org.junit.Assert;
  33 import org.junit.Assume;
  34 import org.junit.Before;
  35 import org.junit.Ignore;
  36 import org.junit.Test;
  37 import org.junit.runners.Parameterized;
  38 
  39 import java.util.Collection;
  40 
  41 /**
  42  * Rotate tests generated by two touch points.
  43  * By default rotation starting form 0 degrees position - on Axis Y,
  44  * but it can be sent from any other location on the object
  45  *  */
  46 public class RotateTest extends ParameterizedTestBase {
  47 
  48     private int newX1;
  49     private int newY1;
  50     private static final int ZERO_ANGLE = 0;
  51     private int centerX;
  52     private int centerY;
  53     private int radius;
  54     private int p1;
  55     private int p2;
  56 
  57     public RotateTest(TestTouchDevice device) {
  58         super(device);
  59     }
  60 
  61     @Parameterized.Parameters
  62     public static Collection<Object[]> data() {
  63         return TestTouchDevices.getTouchDeviceParameters(2);
  64     }
  65 
  66     @Before
  67     public void init() {
  68         //Rotate tests should be run only on platforms that support current feature
  69         Assume.assumeTrue(Boolean.getBoolean("com.sun.javafx.gestures.rotate"));
  70         centerX = (int) Math.round(width * 0.5);
  71         centerY = (int) Math.round(height * 0.5);
  72         radius = (int) Math.round(height * 0.45);
  73     }
  74 
  75     @After
  76     public void releaseAll() throws Exception {
  77         if (device.getPressedPoints() == 2) {
  78             TestLog.reset();
  79             device.removePoint(p1);
  80             device.removePoint(p2);
  81             device.sync();
  82         }
  83     }
  84 
  85     private void updateNewTouchPoint(int angle, int radius, int centerX, int centerY) {
  86 
  87         int transformedAngle = 90 - angle;
  88         newX1 = centerX + (int) Math.round(radius *
  89                 Math.cos(Math.toRadians(transformedAngle)));
  90         newY1 = centerY - (int) Math.round(radius *
  91                 Math.sin(Math.toRadians(transformedAngle)));
  92     }
  93 
  94     private int getDistance(int xPoint1, int yPoint1, int xPoint2, int yPoint2) {
  95         double d = Math.sqrt(Math.pow((xPoint1 - xPoint2), 2)
  96                 + Math.pow((yPoint1 - yPoint2), 2));
  97         return (int) d;
  98     }
  99 
 100     private int getRotateThreshold() {
 101         String s = System.getProperty("com.sun.javafx.gestures.rotate.threshold");
 102         if (s != null) {
 103             return Integer.valueOf(s);
 104         } else {
 105             return 5;
 106         }
 107     }
 108 
 109     private void Rotate(int startAngle, int radius, int x2, int y2, int angleStep,
 110                         int numOfIterations) throws Exception {
 111 
 112         int totalAngle = angleStep;
 113         updateNewTouchPoint(startAngle, radius, x2, y2);
 114 
 115         TestLog.reset();
 116         p1 = device.addPoint(newX1, newY1);
 117         p2 = device.addPoint(x2, y2);
 118         device.sync();
 119         //verify pressing two fingers
 120         TestLog.waitForLogContaining("TouchPoint: PRESSED %d, %d", newX1, newY1);
 121         TestLog.waitForLogContaining("TouchPoint: PRESSED %d, %d", x2, y2);
 122 
 123         //saving previous coordinates:
 124         int previousX = newX1;
 125         int previousY = newY1;
 126 
 127         updateNewTouchPoint((angleStep + startAngle), radius, x2, y2);
 128 
 129         Assume.assumeTrue(getDistance(previousX, previousY, newX1, newY1 )
 130                 > device.getTapRadius());
 131 
 132         //start the rotation
 133         TestLog.reset();
 134         device.setPoint(p1, newX1, newY1);
 135         device.sync();
 136         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", newX1, newY1);
 137         TestLog.waitForLogContaining("TouchPoint: STATIONARY %d, %d", x2, y2);
 138 
 139         if (Math.abs(angleStep) >= getRotateThreshold()) {
 140             TestLog.waitForLogContaining("Rotation started, angle: " + ZERO_ANGLE
 141                 + ", total angle: " + ZERO_ANGLE + ", inertia value: false");
 142             TestLog.waitForLogContaining("Rotation, angle: " + angleStep
 143                 + ", total angle: " + totalAngle
 144                 + ", inertia value: false");
 145         } else {
 146             Assert.assertEquals(0, TestLog.countLogContaining("Rotation started"));
 147             Assert.assertEquals(0, TestLog.countLogContaining("Rotation, angle"));
 148         }
 149         boolean passedTheThreshold =false;
 150         if (numOfIterations >= 2) {
 151             for (int i = 2; i <= numOfIterations; i++) {
 152                 updateNewTouchPoint(angleStep * i + startAngle, radius, x2, y2);
 153                 totalAngle += angleStep;
 154                 TestLog.reset();
 155                 device.setPoint(p1, newX1, newY1);
 156                 device.sync();
 157 
 158                 TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", newX1, newY1);
 159                 TestLog.waitForLogContaining("TouchPoint: STATIONARY %d, %d", x2, y2);
 160 
 161                 String expectedLog;
 162                 if (Math.abs(angleStep) < getRotateThreshold()) {
 163                     if(Math.abs(totalAngle) >= getRotateThreshold()) {
 164                         if (!passedTheThreshold) {
 165                             expectedLog = "Rotation, angle: " + totalAngle
 166                                 + ", total angle: " + totalAngle
 167                                 + ", inertia value: false";
 168                             passedTheThreshold = true;
 169                         } else {
 170                             expectedLog = "Rotation, angle: " + angleStep
 171                                 + ", total angle: " + totalAngle
 172                                 + ", inertia value: false";
 173                         }
 174                     } else {
 175                         expectedLog = "sync";
 176                     }
 177                 } else {
 178                     expectedLog = "Rotation, angle: " + angleStep
 179                             + ", total angle: " + totalAngle
 180                             + ", inertia value: false";
 181                 }
 182                 TestLog.waitForLogContaining(expectedLog);
 183             }
 184         }
 185         TestLog.reset();
 186         device.removePoint(p1);
 187         device.removePoint(p2);
 188         device.sync();
 189         //verify fingers release
 190         TestLog.waitForLogContaining("TouchPoint: RELEASED %d, %d", newX1, newY1);
 191         TestLog.waitForLogContaining("TouchPoint: RELEASED %d, %d", x2, y2);
 192         if (Math.abs(totalAngle) >= getRotateThreshold()) {
 193             TestLog.waitForLogContaining("Rotation finished, angle: " + ZERO_ANGLE
 194                     + ", total angle: " + totalAngle + ", inertia value: false");
 195             Assert.assertEquals(1, TestLog.countLogContaining("Rotation "
 196                     + "finished, " + "angle: " + ZERO_ANGLE
 197                     + ", total angle: " + totalAngle
 198                     + ", inertia value: false"));
 199         } else {
 200             Assert.assertEquals(0, TestLog.countLogContaining("Rotation finished, "
 201                     + "angle: " + ZERO_ANGLE + ", total angle: " + totalAngle
 202                     + ", inertia value: false"));
 203         }
 204         if (TestLog.countLogContaining("Rotation finished") > 0) {
 205             TestLog.waitForLogContainingSubstrings("Rotation", "inertia value: true");
 206         }
 207         TestLog.reset();
 208         p2 = device.addPoint(x2, y2);
 209         device.sync();
 210         device.removePoint(p2);
 211         device.sync();
 212         TestLog.waitForLogContaining("TouchPoint: RELEASED %d, %d", x2, y2);
 213     }
 214 
 215     private void Rotate(int radius, int x2, int y2, int angleStep,
 216                         int numOfIterations) throws Exception {
 217         Rotate(0, radius, x2, y2, angleStep, numOfIterations);
 218     }
 219 
 220     private void Rotate(int startAngle, int angleStep, int numOfIterations) throws Exception {
 221         Rotate(startAngle, radius, centerX, centerY, angleStep, numOfIterations);
 222     }
 223 
 224     private void Rotate(int angleStep, int numOfIterations) throws Exception {
 225         Rotate(0, radius, centerX, centerY, angleStep, numOfIterations);
 226     }
 227 
 228     /**
 229      * Tap two fingers, drag a little bit upper finger right in order move,
 230      * but not enough for rotation.
 231      */
 232     @Test
 233     public void testSmallStepRightNoRotateSent() throws Exception {
 234         Rotate(4, 1);
 235     }
 236 
 237     /**
 238      * Tap two fingers, drag a little bit upper finger right in order move,
 239      * but not enough for rotation, then make 2 more small moves
 240      */
 241     @Test
 242     public void testRotateRightByFewSmallSteps() throws Exception {
 243         Rotate(4, 5);
 244     }
 245 
 246     /**
 247      * Tap two fingers, drag upper finger right in order to rotate
 248      */
 249     @Test
 250     public void testRotateRight() throws Exception {
 251         Rotate(15, 6);
 252     }
 253 
 254     /**
 255      * Tap two fingers, rotate the object right (by 3 steps, 50 degrees each)
 256      */
 257     @Test
 258     public void testRotateRightBigSteps() throws Exception {
 259         Rotate(50, 3);
 260     }
 261 
 262     /**
 263      * Tap two fingers, rotate the object right by only 1 very big step - 80 degrees
 264      */
 265     @Test
 266     @Ignore //RT-36616
 267     public void testRotateRightOneBigStep() throws Exception {
 268         Rotate(80, 1);
 269     }
 270 
 271     /**
 272      * Tap two fingers, drag a little bit upper finger left in order move,
 273      * but not enough for rotation.
 274      */
 275     @Test
 276     public void testSmallStepLeftNoRotateSent() throws Exception {
 277         Rotate(-4, 1);
 278     }
 279 
 280     /**
 281      * Tap two fingers, drag a little bit upper finger left in order move,
 282      * but not enough for rotation, then make 9 more small moves
 283      */
 284     @Test
 285     public void testRotateLeftByFewSmallSteps() throws Exception {
 286         Rotate(-4, 10);
 287     }
 288 
 289     /**
 290      * Tap two fingers, drag upper finger left in order to rotate
 291      */
 292     @Test
 293     public void testRotateLeft() throws Exception {
 294         Rotate(-10, 4);
 295     }
 296 
 297     /**
 298      * Tap two fingers, rotate the object left (by 5 steps, 40 degrees each)
 299      */
 300     @Test
 301     public void testRotateLeftBigSteps() throws Exception {
 302         Rotate(-40, 5);
 303     }
 304 
 305     /**
 306      * Tap two fingers, rotate the object left by only 1 very big step - 70 degrees
 307      */
 308     @Test
 309     @Ignore //RT-36616
 310     public void testRotateLeftOneBigStep() throws Exception {
 311         Rotate(-70, 1);
 312     }
 313 
 314     /**
 315      * Tap two fingers in 45 degrees, rotate the object right
 316      */
 317     @Test
 318     public void testRotateRightFrom45Degrees() throws Exception {
 319         Rotate(45, 20, 3);
 320     }
 321 
 322     /**
 323      * Tap two fingers in 45 degrees, rotate the object left
 324      */
 325     @Test
 326     public void testRotateLeftFrom45Degrees() throws Exception {
 327         Rotate(45, -20, 3);
 328     }
 329 
 330     /**
 331      * Tap two fingers in -45 degrees, rotate the object right
 332      */
 333     @Test
 334     public void testRotateRightFromMinus45Degrees() throws Exception {
 335         Rotate(-45, 20, 3);
 336     }
 337 
 338     /**
 339      * Tap two fingers in -45 degrees, rotate the object left
 340      */
 341     @Test
 342     public void testRotateLeftFromMinus45Degrees() throws Exception {
 343         Rotate(-45, -20, 3);
 344     }
 345 
 346     /**
 347      * Tap two fingers in 140 degrees, rotate the object right
 348      */
 349     @Test
 350     public void testRotateRightFrom140Degrees() throws Exception {
 351         Rotate(140, 20, 3);
 352     }
 353 
 354     /**
 355      * Tap two fingers in 140 degrees, rotate the object left
 356      */
 357     @Test
 358     public void testRotateLeftFrom140Degrees() throws Exception {
 359         Rotate(140, -20, 3);
 360     }
 361 
 362     /**
 363      * Tap two fingers in -140 degrees, rotate the object right
 364      */
 365     @Test
 366     public void testRotateRightFromMinus140Degrees() throws Exception {
 367         Rotate(-140, 20, 3);
 368     }
 369 
 370     /**
 371      * Tap two fingers in -140 degrees, rotate the object left
 372      */
 373     @Test
 374     public void testRotateLeftFromMinus140Degrees() throws Exception {
 375         Rotate(-140, -20, 3);
 376     }
 377 
 378 }