Skip to content

Commit

Permalink
Add more tests for mouse device
Browse files Browse the repository at this point in the history
  • Loading branch information
bendahl committed May 2, 2018
1 parent 7004d3d commit 2657730
Showing 1 changed file with 178 additions and 1 deletion.
179 changes: 178 additions & 1 deletion mouse_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package uinput

import "testing"
import (
"fmt"
"testing"
)

// This test confirms that all basic mouse events are working as expected.
func TestBasicMouseMoves(t *testing.T) {
Expand Down Expand Up @@ -64,3 +67,177 @@ func TestBasicMouseMoves(t *testing.T) {
t.Fatalf("Failed to close device. Last error was: %s\n", err)
}
}

func TestMouseCreationFailsOnEmptyPath(t *testing.T) {
expected := "device path must not be empty"
defer func() {
if r := recover(); r != nil {
actual := r.(string)
if actual != expected {
t.Fatalf("Expected: %s\nActual: %s", expected, actual)
}
}
}()
CreateMouse("", []byte("MouseDevice"))
t.Fatalf("Empty path did not yield a panic")
}

func TestMouseCreationFailsOnNonExistentPathName(t *testing.T) {
path := "/some/bogus/path"
expected := "device path '" + path + "' does not exist"
defer func() {
if r := recover(); r != nil {
actual := r.(string)
if actual != expected {
t.Fatalf("Expected: %s\nActual: %s", expected, actual)
}
}
}()
CreateMouse(path, []byte("MouseDevice"))
t.Fatalf("Invalid path did not yield a panic")
}

func TestMouseCreationFailsIfNameIsTooLong(t *testing.T) {
name := "adsfdsferqewoirueworiuejdsfjdfa;ljoewrjeworiewuoruew;rj;kdlfjoeai;jfewoaifjef;das"
expected := fmt.Sprintf("device name %s is too long (maximum of %d characters allowed)", name, uinputMaxNameSize)
defer func() {
if r := recover(); r != nil {
actual := r.(string)
if actual != expected {
t.Fatalf("Expected: %s\nActual: %s", expected, actual)
}
}
}()
CreateMouse("/dev/uinput", []byte(name))
t.Fatalf("Invalid name did not yield a panic")
}

func TestMouseLeftClickFailsIfDeviceIsClosed(t *testing.T) {
relDev, err := CreateMouse("/dev/uinput", []byte("Test Basic Mouse"))
if err != nil {
t.Fatalf("Failed to create the virtual mouse. Last error was: %s\n", err)
}
relDev.Close()

err = relDev.LeftClick()
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}

func TestMouseLeftPressFailsIfDeviceIsClosed(t *testing.T) {
relDev, err := CreateMouse("/dev/uinput", []byte("Test Basic Mouse"))
if err != nil {
t.Fatalf("Failed to create the virtual mouse. Last error was: %s\n", err)
}
relDev.Close()

err = relDev.LeftPress()
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}

func TestMouseLeftReleaseFailsIfDeviceIsClosed(t *testing.T) {
relDev, err := CreateMouse("/dev/uinput", []byte("Test Basic Mouse"))
if err != nil {
t.Fatalf("Failed to create the virtual mouse. Last error was: %s\n", err)
}
relDev.Close()

err = relDev.LeftRelease()
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}

func TestMouseRightClickFailsIfDeviceIsClosed(t *testing.T) {
relDev, err := CreateMouse("/dev/uinput", []byte("Test Basic Mouse"))
if err != nil {
t.Fatalf("Failed to create the virtual mouse. Last error was: %s\n", err)
}
relDev.Close()

err = relDev.RightClick()
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}

func TestMouseRightPressFailsIfDeviceIsClosed(t *testing.T) {
relDev, err := CreateMouse("/dev/uinput", []byte("Test Basic Mouse"))
if err != nil {
t.Fatalf("Failed to create the virtual mouse. Last error was: %s\n", err)
}
relDev.Close()

err = relDev.RightPress()
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}

func TestVMouse_RightReleaseFailsIfDeviceIsClosed(t *testing.T) {
relDev, err := CreateMouse("/dev/uinput", []byte("Test Basic Mouse"))
if err != nil {
t.Fatalf("Failed to create the virtual mouse. Last error was: %s\n", err)
}
relDev.Close()

err = relDev.RightRelease()
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}

func TestMouseMoveUpFailsIfDeviceIsClosed(t *testing.T) {
relDev, err := CreateMouse("/dev/uinput", []byte("Test Basic Mouse"))
if err != nil {
t.Fatalf("Failed to create the virtual mouse. Last error was: %s\n", err)
}
relDev.Close()

err = relDev.MoveUp(1)
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}

func TestMouseMoveDownFailsIfDeviceIsClosed(t *testing.T) {
relDev, err := CreateMouse("/dev/uinput", []byte("Test Basic Mouse"))
if err != nil {
t.Fatalf("Failed to create the virtual mouse. Last error was: %s\n", err)
}
relDev.Close()

err = relDev.MoveDown(1)
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}

func TestMouseMoveLeftFailsIfDeviceIsClosed(t *testing.T) {
relDev, err := CreateMouse("/dev/uinput", []byte("Test Basic Mouse"))
if err != nil {
t.Fatalf("Failed to create the virtual mouse. Last error was: %s\n", err)
}
relDev.Close()

err = relDev.MoveLeft(1)
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}

func TestMouseMoveRightFailsIfDeviceIsClosed(t *testing.T) {
relDev, err := CreateMouse("/dev/uinput", []byte("Test Basic Mouse"))
if err != nil {
t.Fatalf("Failed to create the virtual mouse. Last error was: %s\n", err)
}
relDev.Close()

err = relDev.MoveRight(1)
if err == nil {
t.Fatalf("Expected error due to closed device, but no error was returned.")
}
}

0 comments on commit 2657730

Please sign in to comment.