1 /*
   2  * Copyright (c) 2013, 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.TestLogShim;
  29 import test.robot.com.sun.glass.ui.monocle.TestApplication;
  30 import org.junit.After;
  31 import org.junit.Assert;
  32 import org.junit.Before;
  33 import org.junit.Rule;
  34 import org.junit.Test;
  35 import org.junit.rules.TestName;
  36 
  37 public class MouseLagTest {
  38 
  39     private UInput ui;
  40     @Rule public TestName name = new TestName();
  41 
  42     @Before public void setUpScreen() throws Exception {
  43         TestLogShim.reset();
  44         TestLogShim.log(name.getMethodName());
  45         TestApplication.showFullScreenScene();
  46         TestApplication.addMouseListeners();
  47         TestApplication.movePointerTo(300, 300);
  48         initDevice();
  49     }
  50 
  51     public void initDevice() throws Exception {
  52         ui = new UInput();
  53         ui.processLine("OPEN");
  54         ui.processLine("EVBIT EV_KEY");
  55         ui.processLine("EVBIT EV_SYN");
  56         ui.processLine("KEYBIT BTN_LEFT");
  57         ui.processLine("EVBIT EV_REL");
  58         ui.processLine("RELBIT REL_X");
  59         ui.processLine("RELBIT REL_Y");
  60         ui.processLine("PROPERTY ID_INPUT_MOUSE 1");
  61         ui.processLine("CREATE");
  62     }
  63 
  64     @After public void destroyDevice() throws Exception {
  65         if (ui != null) {
  66             try {
  67                 ui.processLine("DESTROY");
  68             } catch (RuntimeException e) { }
  69             ui.processLine("CLOSE");
  70             ui.dispose();
  71         }
  72     }
  73 
  74     /** Make sure we can process 500 mouse motion events per second. We are
  75      * not required to report all these events, but must report the last one.
  76      */
  77     @Test
  78     public void testMouseLag() throws Exception {
  79         byte[] moveLeft = new byte[256];
  80         int offset;
  81         offset = ui.writeLine(moveLeft, 0, "EV_REL REL_X -1");
  82         offset = ui.writeLine(moveLeft, offset, "EV_REL REL_Y 0");
  83         int moveLeftLength = ui.writeLine(moveLeft, offset, "EV_SYN");
  84         byte[] moveRight = new byte[256];
  85         offset = ui.writeLine(moveRight, 0, "EV_REL REL_X 1");
  86         offset = ui.writeLine(moveRight, offset, "EV_REL REL_Y 0");
  87         int moveRightLength = ui.writeLine(moveRight, offset, "EV_SYN");
  88         byte[] moveDown = new byte[256];
  89         offset = ui.writeLine(moveDown, 0, "EV_REL REL_X 0");
  90         offset = ui.writeLine(moveDown, offset, "EV_REL REL_Y 1");
  91         int moveDownLength = ui.writeLine(moveDown, offset, "EV_SYN");
  92 
  93         long startTime = System.currentTimeMillis();
  94         for (int i = 0; i < 10; i++) {
  95             for (int j = 0; j < 150; j++) {
  96                 ui.write(moveLeft, 0, moveLeftLength);
  97             }
  98             ui.write(moveDown, 0, moveDownLength);
  99             for (int j = 0; j < 150; j++) {
 100                 ui.write(moveRight, 0, moveRightLength);
 101             }
 102         }
 103 
 104         long t = System.currentTimeMillis() - startTime;
 105         // Make sure events could be sent in the required time
 106         Assert.assertTrue("Took " + t + "ms to send 3000 events, of which "
 107                           + TestLogShim.countLogContaining("moved")
 108                           + " were received",
 109                           t < 6000l * TestApplication.getTimeScale());
 110         TestLogShim.log("Sent 3000 events in " + t + "ms");
 111         // Make sure events could be delivered in the required time
 112         TestLogShim.waitForLog("Mouse moved: 300, 310", 6000l - t);
 113     }
 114 
 115 }