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