diff --git a/touchpad_test.go b/touchpad_test.go index 68176ea..6f02c68 100644 --- a/touchpad_test.go +++ b/touchpad_test.go @@ -1,6 +1,9 @@ package uinput -import "testing" +import ( + "fmt" + "testing" +) func TestBasicTouchPadMoves(t *testing.T) { absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768) @@ -52,3 +55,131 @@ func TestBasicTouchPadMoves(t *testing.T) { t.Fatalf("Failed to close device. Last error was: %s\n", err) } } + +func TestTouchPadCreationFailsOnEmptyPath(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) + } + } + }() + CreateTouchPad("", []byte("TouchDevice"), 0, 1024, 0, 768) + t.Fatalf("Empty path did not yield a panic") +} + +func TestTouchPadCreationFailsOnNonExistentPathName(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) + } + } + }() + CreateTouchPad(path, []byte("TouchDevice"), 0, 1024, 0, 768) + t.Fatalf("Invalid path did not yield a panic") +} + +func TestTouchPadCreationFailsIfNameIsTooLong(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) + } + } + }() + CreateTouchPad("/dev/uinput", []byte(name), 0, 1024, 0, 768) + t.Fatalf("Invalid name did not yield a panic") +} + +func TestTouchPadMoveToFailsOnClosedDevice(t *testing.T) { + absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768) + if err != nil { + t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err) + } + absDev.Close() + err = absDev.MoveTo(1, 1) + if err == nil { + t.Fatalf("Expected error due to closed device, but no error was returned.") + } +} + +func TestTouchPadLeftClickFailsOnClosedDevice(t *testing.T) { + absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768) + if err != nil { + t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err) + } + absDev.Close() + err = absDev.LeftClick() + if err == nil { + t.Fatalf("Expected error due to closed device, but no error was returned.") + } +} + +func TestTouchPadLeftPressFailsOnClosedDevice(t *testing.T) { + absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768) + if err != nil { + t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err) + } + absDev.Close() + err = absDev.LeftPress() + if err == nil { + t.Fatalf("Expected error due to closed device, but no error was returned.") + } +} + +func TestTouchPadLeftReleaseFailsOnClosedDevice(t *testing.T) { + absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768) + if err != nil { + t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err) + } + absDev.Close() + err = absDev.LeftRelease() + if err == nil { + t.Fatalf("Expected error due to closed device, but no error was returned.") + } +} + +func TestTouchPadRightClickFailsOnClosedDevice(t *testing.T) { + absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768) + if err != nil { + t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err) + } + absDev.Close() + err = absDev.RightClick() + if err == nil { + t.Fatalf("Expected error due to closed device, but no error was returned.") + } +} + +func TestTouchPadRightPressFailsOnClosedDevice(t *testing.T) { + absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768) + if err != nil { + t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err) + } + absDev.Close() + err = absDev.RightPress() + if err == nil { + t.Fatalf("Expected error due to closed device, but no error was returned.") + } +} + +func TestTouchPadRightReleaseFailsOnClosedDevice(t *testing.T) { + absDev, err := CreateTouchPad("/dev/uinput", []byte("Test TouchPad"), 0, 1024, 0, 768) + if err != nil { + t.Fatalf("Failed to create the virtual touch pad. Last error was: %s\n", err) + } + absDev.Close() + err = absDev.RightRelease() + if err == nil { + t.Fatalf("Expected error due to closed device, but no error was returned.") + } +}