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.animation.AnimationTimer;
  31 import javafx.application.Platform;
  32 import org.junit.Assert;
  33 import org.junit.Test;
  34 import org.junit.runners.Parameterized;
  35 
  36 import java.util.Collection;
  37 import java.util.concurrent.CountDownLatch;
  38 
  39 public class RapidTapTest extends ParameterizedTestBase {
  40 
  41     public RapidTapTest(TestTouchDevice device) {
  42         super(device);
  43     }
  44 
  45     @Parameterized.Parameters
  46     public static Collection<Object[]> data() {
  47         return TestTouchDevices.getTouchDeviceParameters(1);
  48     }
  49 
  50     /** 20 quick taps */
  51     @Test
  52     public void tapTwentyTimes() throws Exception {
  53         for (int i = 0; i < 20; i++) {
  54             int p = device.addPoint(width / 2, height / 2);
  55             device.sync();
  56             TestLog.waitForLogContaining("TouchPoint: PRESSED", 3000);
  57             TestLog.waitForLogContaining("Mouse pressed", 3000);
  58             device.removePoint(p);
  59             device.sync();
  60         }
  61         TestRunnable.invokeAndWaitUntilSuccess(() -> {
  62             Assert.assertEquals(20, TestLog.countLogContaining(
  63                     "TouchPoint: PRESSED"));
  64             Assert.assertEquals(20, TestLog.countLogContaining(
  65                     "TouchPoint: RELEASED"));
  66             Assert.assertEquals(20,
  67                                 TestLog.countLogContaining("Mouse pressed"));
  68             Assert.assertEquals(20,
  69                                 TestLog.countLogContaining("Mouse released"));
  70             Assert.assertEquals(20,
  71                                 TestLog.countLogContaining("Mouse clicked"));
  72         }, 3000);
  73     }
  74 
  75     /** 20 quick taps while the application thread is busy */
  76     @Test
  77     public void tapTwentyTimesUnderStress() throws Exception {
  78         final CountDownLatch latch = new CountDownLatch(1);
  79         // throttle the application thread
  80         final AnimationTimer a = new AnimationTimer() {
  81             @Override
  82             public void handle(long now) {
  83                 // Spin for 50ms adjusted for time scale
  84                 double spinTime = Math.round(50000000.0 * TestApplication.getTimeScale());
  85                 long end = now + Math.round(spinTime);
  86                 latch.countDown();
  87                 while (System.nanoTime() < end) { } // spin
  88             }
  89         };
  90         Platform.runLater(a::start);
  91         latch.await();
  92         try {
  93             for (int i = 0; i < 20; i++) {
  94                 int p = device.addPoint(width / 2, height / 2);
  95                 device.sync();
  96                 TestLog.waitForLogContaining("TouchPoint: PRESSED", 3000);
  97                 TestLog.waitForLogContaining("Mouse pressed", 3000);
  98                 device.removePoint(p);
  99                 device.sync();
 100             }
 101             TestRunnable.invokeAndWaitUntilSuccess(() -> {
 102                 Assert.assertEquals(20, TestLog.countLogContaining("TouchPoint: PRESSED"));
 103                 Assert.assertEquals(20, TestLog.countLogContaining("TouchPoint: RELEASED"));
 104                 Assert.assertEquals(20, TestLog.countLogContaining("Mouse pressed"));
 105                 Assert.assertEquals(20, TestLog.countLogContaining("Mouse released"));
 106                 Assert.assertEquals(20, TestLog.countLogContaining("Mouse clicked"));
 107             }, 10000);
 108         } finally {
 109             Platform.runLater(a::stop);
 110         }
 111     }
 112 
 113 }