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