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 javafx.geometry.Rectangle2D;
  31 import javafx.stage.Screen;
  32 import org.junit.After;
  33 import org.junit.Assert;
  34 import org.junit.Before;
  35 import org.junit.Test;
  36 
  37 public class CreateDeviceTest {
  38 
  39     private UInput ui;
  40 
  41     @Before public void initDevice() {
  42         TestLogShim.reset();
  43         ui = new UInput();
  44     }
  45 
  46     @After public void destroyDevice() throws InterruptedException {
  47         ui.waitForQuiet();
  48         try {
  49             ui.processLine("DESTROY");
  50         } catch (RuntimeException e) { }
  51         ui.processLine("CLOSE");
  52         ui.dispose();
  53     }
  54 
  55     @Test
  56     public void testCreateKeyDevice() throws Exception {
  57         TestApplication.showFullScreenScene();
  58         TestApplication.addKeyListeners();
  59         ui.processLine("OPEN");
  60         ui.processLine("EVBIT EV_KEY");
  61         ui.processLine("EVBIT EV_SYN");
  62         ui.processLine("KEYBIT KEY_A");
  63         ui.processLine("KEYBIT KEY_LEFTSHIFT");
  64         ui.processLine("PROPERTY ID_INPUT_KEYBOARD 1");
  65         ui.processLine("CREATE");
  66         ui.processLine("EV_KEY KEY_LEFTSHIFT 1");
  67         ui.processLine("EV_SYN");
  68         ui.processLine("EV_KEY KEY_LEFTSHIFT 0");
  69         ui.processLine("EV_SYN");
  70         TestLogShim.waitForLog("Key pressed: SHIFT", 3000);
  71         TestLogShim.clear();
  72 
  73         ui.processLine("EV_KEY KEY_A 1");
  74         ui.processLine("EV_SYN");
  75         ui.processLine("EV_KEY KEY_A 0");
  76         ui.processLine("EV_SYN");
  77         TestLogShim.waitForLog("Key typed: a", 3000);
  78         ui.processLine("EV_KEY KEY_LEFTSHIFT 1");
  79         ui.processLine("EV_SYN");
  80         ui.processLine("EV_KEY KEY_A 1");
  81         ui.processLine("EV_SYN");
  82         ui.processLine("EV_KEY KEY_A 0");
  83         ui.processLine("EV_SYN");
  84         ui.processLine("EV_KEY KEY_LEFTSHIFT 0");
  85         ui.processLine("EV_SYN");
  86         TestLogShim.waitForLog("Key typed: A", 3000);
  87         // make sure only two key typed events were received
  88         Assert.assertEquals("Expected two typed events", 2,
  89                              TestLogShim.getLog().stream().filter(s -> s.startsWith("Key typed")).count());
  90     }
  91 
  92     @Test
  93     public void testCreateMouseDevice() throws Exception {
  94         TestApplication.showFullScreenScene();
  95         TestApplication.addMouseListeners();
  96         TestApplication.movePointerTo(300, 300);
  97         ui.processLine("OPEN");
  98         ui.processLine("EVBIT EV_KEY");
  99         ui.processLine("EVBIT EV_SYN");
 100         ui.processLine("KEYBIT BTN_LEFT");
 101         ui.processLine("EVBIT EV_REL");
 102         ui.processLine("RELBIT REL_X");
 103         ui.processLine("RELBIT REL_Y");
 104         ui.processLine("PROPERTY ID_INPUT_MOUSE 1");
 105         ui.processLine("CREATE");
 106         TestLogShim.clear();
 107         ui.processLine("EV_KEY BTN_LEFT 1");
 108         ui.processLine("EV_SYN");
 109         ui.processLine("EV_KEY BTN_LEFT 0");
 110         ui.processLine("EV_SYN");
 111         TestLogShim.waitForLog("Mouse pressed: 300, 300", 3000);
 112         ui.processLine("EV_REL REL_X -10");
 113         ui.processLine("EV_REL REL_Y -5");
 114         ui.processLine("EV_SYN");
 115         TestLogShim.waitForLog("Mouse moved: 290, 295", 3000);
 116     }
 117 
 118     @Test
 119     public void testCreateTouchDevice() throws Exception {
 120         TestApplication.showFullScreenScene();
 121         TestApplication.addMouseListeners();
 122         ui.processLine("OPEN");
 123         ui.processLine("EVBIT EV_SYN");
 124         ui.processLine("EVBIT EV_KEY");
 125         ui.processLine("KEYBIT BTN_TOUCH");
 126         ui.processLine("EVBIT EV_ABS");
 127         ui.processLine("ABSBIT ABS_X");
 128         ui.processLine("ABSBIT ABS_Y");
 129         ui.processLine("ABSMIN ABS_X 0");
 130         ui.processLine("ABSMAX ABS_X 4095");
 131         ui.processLine("ABSMIN ABS_Y 0");
 132         ui.processLine("ABSMAX ABS_Y 4095");
 133         ui.processLine("PROPBIT INPUT_PROP_POINTER");
 134         ui.processLine("PROPBIT INPUT_PROP_DIRECT");
 135         ui.processLine("PROPERTY ID_INPUT_TOUCHSCREEN 1");
 136         ui.processLine("CREATE");
 137         ui.processLine("EV_KEY BTN_TOUCH 1");
 138         ui.processLine("EV_ABS ABS_X 2048");
 139         ui.processLine("EV_ABS ABS_Y 2048");
 140         ui.processLine("EV_SYN");
 141         ui.processLine("EV_KEY BTN_TOUCH 0");
 142         ui.processLine("EV_ABS ABS_X 2048");
 143         ui.processLine("EV_ABS ABS_Y 2048");
 144         ui.processLine("EV_SYN");
 145         Rectangle2D r = Screen.getPrimary().getBounds();
 146         TestLogShim.waitForLog("Mouse pressed: "
 147                         + (int) r.getWidth() / 2 + ", " + (int) r.getHeight() / 2   , 3000);
 148     }
 149 
 150 }