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 org.junit.*;
  31 import org.junit.runners.Parameterized;
  32 
  33 import java.util.Collection;
  34 
  35 /** Zoom tests with two touch points */
  36 public class ZoomTest extends ParameterizedTestBase {
  37 
  38     public ZoomTest(TestTouchDevice device) {
  39         super(device);
  40     }
  41 
  42     @Parameterized.Parameters
  43     public static Collection<Object[]> data() {
  44         return TestTouchDevices.getTouchDeviceParameters(2);
  45     }
  46 
  47     @Before
  48     public void verifyZoomEnabled() {
  49         Assume.assumeTrue(Boolean.getBoolean("com.sun.javafx.gestures.zoom"));
  50     }
  51 
  52     private void tapToStopInertia() throws Exception {
  53         int point1X = (int) Math.round(width * 0.1);
  54         int point1Y = (int) Math.round(height * 0.3);
  55         Assert.assertEquals(0, device.getPressedPoints());
  56         TestLog.reset();
  57         int p = device.addPoint(point1X, point1Y);
  58         device.sync();
  59         device.removePoint(p);
  60         device.sync();
  61         TestLog.waitForLog("Mouse clicked: %d, %d", point1X, point1Y);
  62     }
  63 
  64     /**
  65      * Touch down two fingers,
  66      * drag upper finger up in order move but not enough for zooming,
  67      * drag again to zoom in (make it twice bigger)
  68      */
  69     @Test
  70     public void testZoomInSmallStepBigStep() throws Exception {
  71         int x1 = (int) Math.round(width / 2);
  72         int y1 = (int) Math.round(height * 0.3);
  73         int x2 = (int) Math.round(width / 2);
  74         int y2 = (int) Math.round(height * 0.7);
  75         int step = (int) Math.round(height * 0.1);
  76         int smallStep = (int) device.getTapRadius() + 1;
  77         double threshold = 0;
  78         String s = System.getProperty("com.sun.javafx.gestures.zoom.threshold");
  79         if (s != null) {
  80             threshold = Double.valueOf(s);
  81         } else {
  82             threshold = 0.1;
  83         }
  84         Assume.assumeTrue(((y2 - y1) * threshold) > smallStep);
  85 
  86         TestLog.reset();
  87         //tap two fingers
  88         int p1 = device.addPoint(x1, y1);
  89         int p2 = device.addPoint(x2, y2);
  90         device.sync();
  91 
  92         //verify pressing two fingers
  93         TestLog.waitForLogContaining("TouchPoint: PRESSED %d, %d", x1, y1);
  94         TestLog.waitForLogContaining("TouchPoint: PRESSED %d, %d", x2, y2);
  95 
  96         //drag upper finger up in order move but not enough for zooming
  97         int newy1 = y1 - smallStep;
  98         int newy2 = 0;
  99         TestLog.reset();
 100         device.setPoint(p1, x1, newy1);
 101         device.sync();
 102 
 103         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x1, newy1);
 104         TestLog.waitForLogContaining("TouchPoint: STATIONARY %d, %d", x2, y2);
 105         Assert.assertEquals(0, TestLog.countLogContaining("Zoom started"));
 106 
 107         //drag upper finger up and lower down in order to zoom in
 108         newy1 = y1 - step;
 109         newy2 = y2 + step;
 110         TestLog.reset();
 111         device.setPoint(p1, x1, newy1);
 112         device.setPoint(p2, x2, newy2);
 113         device.sync();
 114         double factor0 = 1.0;
 115         double factor1 = (double) (newy2 - newy1)/(y2 - y1);
 116 
 117         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x1, newy1);
 118         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x2, newy2);
 119         TestLog.waitForLogContaining("Zoom started, factor: " + factor0
 120                 + ", total factor: " + factor0  + ", inertia value: false");
 121         TestLog.waitForLogContaining("Zoom, factor: " + factor1
 122                 + ", total factor: " + factor1 + ", inertia value: false");
 123 
 124         step = step * 2;
 125         newy1 = y1 - step;
 126         newy2 = y2 + step;
 127         TestLog.reset();
 128         device.setPoint(p1, x1, newy1);
 129         device.setPoint(p2, x2, newy2);
 130         device.sync();
 131         double factor2 = (double) (newy2 - newy1)/(y2 - y1 + step);
 132 
 133         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x1, newy1);
 134         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x2, newy2);
 135         TestLog.waitForLogContaining("Zoom, factor: " + factor2
 136                 + ", total factor: " + (factor1 * factor2)
 137                 + ", inertia value: false");
 138 
 139         //release first finger
 140         TestLog.reset();
 141         device.removePoint(p1);
 142         device.sync();
 143         TestLog.waitForLogContaining("TouchPoint: RELEASED %d, %d", x1, newy1);
 144         TestLog.waitForLogContaining("TouchPoint: STATIONARY %d, %d", x2, newy2);
 145         TestLog.waitForLogContaining("Zoom finished, factor: " + factor0
 146                 + ", total factor: " + (factor1 * factor2)
 147                 + ", inertia value: false");
 148         //release second finger
 149         TestLog.reset();
 150         device.removePoint(p2);
 151         device.sync();
 152         TestLog.waitForLogContaining("Touch released: %d, %d", x2, newy2);
 153         TestLog.waitForLogContaining("TouchPoint: RELEASED %d, %d", x2, newy2);
 154         TestLog.waitForLog("Mouse released: %d, %d", x2, newy2);
 155         TestLog.waitForLog("Mouse clicked: %d, %d", x2, newy2);
 156         Assert.assertEquals(1, TestLog.countLogContaining("Mouse clicked: "
 157                 + x2 +", " + newy2));
 158         tapToStopInertia();
 159     }
 160 
 161     /**
 162      * Touch down two fingers, zoom in (make it twice bigger)
 163      */
 164     @Test
 165     public void testZoomIn() throws Exception {
 166         int x1 = (int) Math.round(width / 2);
 167         int y1 = (int) Math.round(height * 0.3);
 168         int x2 = (int) Math.round(width / 2);
 169         int y2 = (int) Math.round(height * 0.7);
 170         int step = (int) Math.round(height * 0.1);
 171 
 172         TestLog.reset();
 173         //tap two fingers
 174         int p1 = device.addPoint(x1, y1);
 175         int p2 = device.addPoint(x2, y2);
 176         device.sync();
 177 
 178         //verify pressing two fingers
 179         TestLog.waitForLogContaining("TouchPoint: PRESSED %d, %d", x1, y1);
 180         TestLog.waitForLogContaining("TouchPoint: PRESSED %d, %d", x2, y2);
 181 
 182         //drag upper finger up and lower down in order to zoom in
 183         int newy1 = y1 - step;
 184         int newy2 = y2 + step;
 185         TestLog.reset();
 186         device.setPoint(p1, x1, newy1);
 187         device.setPoint(p2, x2, newy2);
 188         device.sync();
 189         double factor0 = 1.0;
 190         double factor1 = (double) (newy2 - newy1)/(y2 - y1);
 191 
 192         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x1, newy1);
 193         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x2, newy2);
 194         TestLog.waitForLogContaining("Zoom started, factor: " + factor0
 195                 + ", total factor: " + factor0 + ", inertia value: false");
 196         TestLog.waitForLogContaining("Zoom, factor: " + factor1
 197                 + ", total factor: " + factor1  + ", inertia value: false");
 198 
 199         step = step * 2;
 200         newy1 = y1 - step;
 201         newy2 = y2 + step;
 202         TestLog.reset();
 203         device.setPoint(p1, x1, newy1);
 204         device.setPoint(p2, x2, newy2);
 205         device.sync();
 206         double factor2 = (double) (newy2 - newy1)/(y2 - y1 + step);
 207 
 208         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x1, newy1);
 209         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x2, newy2);
 210         TestLog.waitForLogContaining("Zoom, factor: " + factor2
 211                 + ", total factor: " + (factor1 * factor2)
 212                 + ", inertia value: false");
 213 
 214         //release first finger
 215         TestLog.reset();
 216         device.removePoint(p1);
 217         device.sync();
 218         TestLog.waitForLogContaining("TouchPoint: RELEASED %d, %d",
 219                 x1, newy1);
 220         TestLog.waitForLogContaining("TouchPoint: STATIONARY %d, %d",
 221                 x2, newy2);
 222         TestLog.waitForLogContaining("Zoom finished, factor: " + factor0
 223                 + ", total factor: " + (factor1 * factor2)
 224                 + ", inertia value: false");
 225 
 226         //release second finger
 227         TestLog.reset();
 228         device.removePoint(p2);
 229         device.sync();
 230         TestLog.waitForLogContaining("Touch released: %d, %d", x2, newy2);
 231         TestLog.waitForLogContaining("TouchPoint: RELEASED %d, %d",
 232                 x2, newy2);
 233         TestLog.waitForLog("Mouse released: %d, %d", x2, newy2);
 234         TestLog.waitForLog("Mouse clicked: %d, %d", x2, newy2);
 235         Assert.assertEquals(1, TestLog.countLogContaining("Mouse clicked: "
 236                 + x2 +", " + newy2));
 237         tapToStopInertia();
 238     }
 239 
 240     /**
 241      * Touch down two fingers, zoom out (make it quarter of original size)
 242      */
 243     @Test
 244     public void testZoomOut() throws Exception {
 245         int x1 = (int) Math.round(width / 2);
 246         int y1 = (int) Math.round(height * 0.1);
 247         int x2 = (int) Math.round(width / 2);
 248         int y2 = (int) Math.round(height * 0.9);
 249         int step = (int) Math.round(height * 0.1);
 250         double factor0 = 1.0;
 251 
 252         //tap two fingers
 253         TestLog.reset();
 254         int p1 = device.addPoint(x1, y1);
 255         int p2 = device.addPoint(x2, y2);
 256         device.sync();
 257 
 258         //verify pressing two fingers
 259         TestLog.waitForLogContaining("TouchPoint: PRESSED %d, %d", x1, y1);
 260         TestLog.waitForLogContaining("TouchPoint: PRESSED %d, %d", x2, y2);
 261 
 262         //drag upper finger down and lower - up, in order to zoom out
 263         TestLog.reset();
 264         int newy1 = y1 + step;
 265         int newy2 = y2 - step;
 266         device.setPoint(p1, x1, newy1);
 267         device.setPoint(p2, x2, newy2);
 268         device.sync();
 269         double factor1 = (double) (newy2 - newy1)/(y2 - y1);
 270 
 271         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x1, newy1);
 272         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x2, newy2);
 273         TestLog.waitForLogContaining("Zoom started, factor: " + factor0
 274                 + ", total factor: " + factor0 + ", inertia value: false");
 275         TestLog.waitForLogContaining("Zoom, factor: " + factor1
 276                 + ", total factor: " + factor1 + ", inertia value: false");
 277 
 278         TestLog.reset();
 279         y1 = y1 + step;
 280         y2 = y2 - step;
 281         newy1 = y1 + step;
 282         newy2 = y2 - step;
 283 
 284         device.setPoint(p1, x1, newy1);
 285         device.setPoint(p2, x2, newy2);
 286         device.sync();
 287         double factor2 = (double) (newy2 - newy1)/(y2 - y1);
 288 
 289         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x1, newy1);
 290         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x2, newy2);
 291         TestLog.waitForLogContaining("Zoom, factor: " + factor2
 292                 + ", total factor: " + (factor1 * factor2)
 293                 + ", inertia value: false");
 294 
 295         TestLog.reset();
 296         y1 = y1 + step;
 297         y2 = y2 - step;
 298         newy1 = y1 + step;
 299         newy2 = y2 - step;
 300 
 301         device.setPoint(p1, x1, newy1);
 302         device.setPoint(p2, x2, newy2);
 303         device.sync();
 304         double factor3 = (double) (newy2 - newy1)/(y2 - y1);
 305 
 306         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x1, newy1);
 307         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x2, newy2);
 308         TestLog.waitForLogContaining("Zoom, factor: " + factor3
 309                 + ", total factor: " + (factor1 * factor2 * factor3)
 310                 + ", inertia value: false");
 311 
 312         //release first finger
 313         TestLog.reset();
 314         device.removePoint(p1);
 315         device.sync();
 316         TestLog.waitForLogContaining("TouchPoint: RELEASED %d, %d",
 317                 x1, newy1);
 318         TestLog.waitForLogContaining("TouchPoint: STATIONARY %d, %d",
 319                 x2, newy2);
 320         TestLog.waitForLogContaining("Zoom finished, factor: " + factor0
 321                 + ", total factor: " + (factor1 * factor2 * factor3)
 322                 + ", inertia value: false");
 323 
 324         //release second finger
 325         TestLog.reset();
 326         device.removePoint(p2);
 327         device.sync();
 328         TestLog.waitForLogContaining("Touch released: %d, %d", x2, newy2);
 329         TestLog.waitForLogContaining("TouchPoint: RELEASED %d, %d",
 330                 x2, newy2);
 331         TestLog.waitForLog("Mouse released: %d, %d", x2, newy2);
 332         TestLog.waitForLog("Mouse clicked: %d, %d", x2, newy2);
 333         Assert.assertEquals(1, TestLog.countLogContaining("Mouse clicked: "
 334                 + x2 +", " + newy2));
 335         tapToStopInertia();
 336     }
 337 
 338     /**
 339      * Touch down two fingers,
 340      * drag upper finger down in order move but not enough for zooming,
 341      * drag again to zoom out (quarter of original size)
 342      */
 343     @Test
 344     public void testZoomOutSmallStepBigStep() throws Exception {
 345         int x1 = (int) Math.round(width / 2);
 346         int y1 = (int) Math.round(height * 0.1);
 347         int x2 = (int) Math.round(width / 2);
 348         int y2 = (int) Math.round(height * 0.9);
 349         int step = (int) Math.round(height * 0.1);
 350         int smallStep = (int) device.getTapRadius() + 1;
 351         double threshold = 0;
 352         String s = System.getProperty("com.sun.javafx.gestures.zoom.threshold");
 353         if (s != null) {
 354             threshold = Double.valueOf(s);
 355         } else {
 356             threshold = 0.1;
 357         }
 358         Assume.assumeTrue(((y2 - y1) * threshold) > smallStep);
 359         double factor0 = 1.0;
 360 
 361         TestLog.reset();
 362         //tap two fingers
 363         int p1 = device.addPoint(x1, y1);
 364         int p2 = device.addPoint(x2, y2);
 365         device.sync();
 366 
 367         //verify pressing two fingers
 368         TestLog.waitForLogContaining("TouchPoint: PRESSED %d, %d", x1, y1);
 369         TestLog.waitForLogContaining("TouchPoint: PRESSED %d, %d", x2, y2);
 370 
 371         //drag upper finger down in order move but not enough for zooming
 372         TestLog.reset();
 373         int newy1 = y1 + smallStep;
 374         int newy2 = 0;
 375 
 376         device.setPoint(p1, x1, newy1);
 377         device.sync();
 378 
 379         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x1, newy1);
 380         TestLog.waitForLogContaining("TouchPoint: STATIONARY %d, %d", x2, y2);
 381         Assert.assertEquals(0, TestLog.countLogContaining("Zoom started"));
 382 
 383         //drag upper finger up and lower down in order to zoom in
 384         TestLog.reset();
 385         newy1 = y1 + step;
 386         newy2 = y2 - step;
 387         device.setPoint(p1, x1, newy1);
 388         device.setPoint(p2, x2, newy2);
 389         device.sync();
 390         double factor1 = (double) (newy2 - newy1)/(y2 - y1);
 391 
 392         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x1, newy1);
 393         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x2, newy2);
 394         TestLog.waitForLogContaining("Zoom started, factor: " + factor0
 395                 + ", total factor: " + factor0 + ", inertia value: false");
 396         TestLog.waitForLogContaining("Zoom, factor: " + factor1
 397                 + ", total factor: " + factor1 + ", inertia value: false");
 398         TestLog.reset();
 399         y1 = y1 + step;
 400         y2 = y2 - step;
 401         newy1 = y1 + step;
 402         newy2 = y2 - step;
 403         device.setPoint(p1, x1, newy1);
 404         device.setPoint(p2, x2, newy2);
 405         device.sync();
 406         double factor2 = (double) (newy2 - newy1)/(y2 - y1);
 407 
 408         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x1, newy1);
 409         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x2, newy2);
 410         TestLog.waitForLogContaining("Zoom, factor: " + factor2
 411                 + ", total factor: " + (factor1 * factor2)
 412                 + ", inertia value: false");
 413 
 414         TestLog.reset();
 415         y1 = y1 + step;
 416         y2 = y2 - step;
 417         newy1 = y1 + step;
 418         newy2 = y2 - step;
 419         device.setPoint(p1, x1, newy1);
 420         device.setPoint(p2, x2, newy2);
 421         device.sync();
 422         double factor3 = (double) (newy2 - newy1)/(y2 - y1);
 423 
 424         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x1, newy1);
 425         TestLog.waitForLogContaining("TouchPoint: MOVED %d, %d", x2, newy2);
 426         TestLog.waitForLogContaining("Zoom, factor: " + factor3
 427                 + ", total factor: " + (factor1 * factor2 * factor3)
 428                 + ", inertia value: false");
 429 
 430         //release first finger
 431         TestLog.reset();
 432         device.removePoint(p1);
 433         device.sync();
 434         TestLog.waitForLogContaining("TouchPoint: RELEASED %d, %d",
 435                 x1, newy1);
 436         TestLog.waitForLogContaining("TouchPoint: STATIONARY %d, %d",
 437                 x2, newy2);
 438         TestLog.waitForLogContaining("Zoom finished, factor: " + factor0
 439                 + ", total factor: " + (factor1 * factor2 * factor3)
 440                 + ", inertia value: false");
 441 
 442         //release second finger
 443         TestLog.reset();
 444         device.removePoint(p2);
 445         device.sync();
 446         TestLog.waitForLogContaining("Touch released: %d, %d", x2, newy2);
 447         TestLog.waitForLogContaining("TouchPoint: RELEASED %d, %d",
 448                 x2, newy2);
 449         TestLog.waitForLog("Mouse released: %d, %d", x2, newy2);
 450         TestLog.waitForLog("Mouse clicked: %d, %d", x2, newy2);
 451         Assert.assertEquals(1, TestLog.countLogContaining("Mouse clicked: "
 452                 + x2 +", " + newy2));
 453         tapToStopInertia();
 454     }
 455 }