60 lines
2.0 KiB
Go
60 lines
2.0 KiB
Go
package host
|
|
|
|
import "testing"
|
|
|
|
func TestEvalWheelFilterAndOnDemandLifecycle(t *testing.T) {
|
|
rules := []InputFilterRule{{
|
|
ID: "sample-wheel-filter",
|
|
PluginID: "example.input.wheel-filter",
|
|
Event: "mouse_wheel",
|
|
Axis: "vertical",
|
|
Transform: "invert",
|
|
Scope: "vm_foreground",
|
|
VMNames: map[string]bool{"mac1": true},
|
|
Vertical: true,
|
|
Horizontal: false,
|
|
}}
|
|
|
|
// Matching VM + vertical axis -> inverted.
|
|
if d, ok := EvalWheelFilter(rules, "mac1", false, 120); !ok || d != -120 {
|
|
t.Fatalf("vertical mac1 = (%d, %v), want (-120, true)", d, ok)
|
|
}
|
|
// Non-matching horizontal axis -> untouched.
|
|
if d, ok := EvalWheelFilter(rules, "mac1", true, 120); ok || d != 120 {
|
|
t.Fatalf("horizontal mac1 = (%d, %v), want (120, false)", d, ok)
|
|
}
|
|
// Non-matching VM -> untouched.
|
|
if d, ok := EvalWheelFilter(rules, "mac2", false, 120); ok || d != 120 {
|
|
t.Fatalf("vertical mac2 = (%d, %v), want (120, false)", d, ok)
|
|
}
|
|
// Overflow guard for -32768.
|
|
if d, ok := EvalWheelFilter(rules, "mac1", false, -32768); !ok || d != 32767 {
|
|
t.Fatalf("overflow guard = (%d, %v), want (32767, true)", d, ok)
|
|
}
|
|
|
|
// On-demand hook lifecycle: no hook installed until BOTH rule AND matching target exist.
|
|
svc := NewInputService(nil)
|
|
defer svc.Close()
|
|
|
|
if svc.IsMouseHookInstalled() {
|
|
t.Fatal("mouse hook must NOT be installed when no rules exist")
|
|
}
|
|
svc.SetFilterRules(rules)
|
|
if svc.IsMouseHookInstalled() {
|
|
t.Fatal("mouse hook must NOT be installed when no matching VM is running")
|
|
}
|
|
svc.SetTargets([]IMETarget{{PID: 1234, VMName: "mac2"}})
|
|
if svc.IsMouseHookInstalled() {
|
|
t.Fatal("mouse hook must NOT be installed when running VM does not match rule")
|
|
}
|
|
svc.SetTargets([]IMETarget{{PID: 1234, VMName: "mac1"}})
|
|
if !svc.IsMouseHookInstalled() {
|
|
t.Fatal("mouse hook SHOULD be installed when matching VM is running")
|
|
}
|
|
// Disable plugin (clear rules) -> hook immediately uninstalled.
|
|
svc.SetFilterRules(nil)
|
|
if svc.IsMouseHookInstalled() {
|
|
t.Fatal("mouse hook MUST be uninstalled when filter rules are cleared")
|
|
}
|
|
}
|