157 lines
6.0 KiB
Go
157 lines
6.0 KiB
Go
package host
|
|
|
|
import (
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseFullscreenHotkey(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
raw, canonical string
|
|
want FullscreenHotkey
|
|
}{
|
|
{"", DefaultFullscreenHotkey, FullscreenHotkey{Ctrl: true, Shift: true, VK: 'F', Key: "F"}},
|
|
{" \t ", DefaultFullscreenHotkey, FullscreenHotkey{Ctrl: true, Shift: true, VK: 'F', Key: "F"}},
|
|
{" meta + return + option + SHIFT + control ", "Ctrl+Shift+Alt+Win+Enter", FullscreenHotkey{Ctrl: true, Shift: true, Alt: true, Win: true, VK: 0x0D, Key: "Enter"}},
|
|
{"super+esc", "Win+Escape", FullscreenHotkey{Win: true, VK: 0x1B, Key: "Escape"}},
|
|
{"Ctrl+0", "Ctrl+0", FullscreenHotkey{Ctrl: true, VK: '0', Key: "0"}},
|
|
{"f1", "F1", FullscreenHotkey{VK: 0x70, Key: "F1"}},
|
|
{"f9", "F9", FullscreenHotkey{VK: 0x78, Key: "F9"}},
|
|
{"F10", "F10", FullscreenHotkey{VK: 0x79, Key: "F10"}},
|
|
{"F12", "F12", FullscreenHotkey{VK: 0x7B, Key: "F12"}},
|
|
{"alt+pagedown", "Alt+PageDown", FullscreenHotkey{Alt: true, VK: 0x22, Key: "PageDown"}},
|
|
{"shift+space", "Shift+Space", FullscreenHotkey{Shift: true, VK: 0x20, Key: "Space"}},
|
|
} {
|
|
t.Run(tc.raw, func(t *testing.T) {
|
|
hk, canonical, err := ParseFullscreenHotkey(tc.raw)
|
|
if err != nil || hk != tc.want || canonical != tc.canonical {
|
|
t.Fatalf("got %+v, %q, %v; want %+v, %q", hk, canonical, err, tc.want, tc.canonical)
|
|
}
|
|
})
|
|
}
|
|
for _, raw := range []string{
|
|
"Ctrl+Shift", "Ctrl+A+B", "Ctrl+Control+A", "Option+Alt+F1", "Meta+Win+F1",
|
|
"A", "7", "Tab", "Ctrl++F", "F0", "F01", "Ctrl+F13", "Ctrl+F24",
|
|
"delete+alt+control", "Alt+F4", "Alt+Tab", "Win+L", "Alt+Shift+Tab",
|
|
} {
|
|
t.Run(raw, func(t *testing.T) {
|
|
if _, _, err := ParseFullscreenHotkey(raw); err == nil {
|
|
t.Fatalf("accepted invalid hotkey %q", raw)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFullscreenHotkeyMatcher(t *testing.T) {
|
|
hk, _, err := ParseFullscreenHotkey(DefaultFullscreenHotkey)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
matcher := FullscreenHotkeyMatcher{Hotkey: hk}
|
|
mods := FullscreenHotkeyMods{Ctrl: true, Shift: true}
|
|
extra := FullscreenHotkeyMods{Ctrl: true, Shift: true, Alt: true}
|
|
for _, step := range []struct {
|
|
name string
|
|
key uint32
|
|
down, foreground bool
|
|
mods FullscreenHotkeyMods
|
|
consume, trigger bool
|
|
}{
|
|
{"other key", 'G', true, true, mods, false, false},
|
|
{"first press outside VM", 'F', true, false, mods, false, false},
|
|
{"unmatched press repeats inside VM", 'F', true, true, mods, false, false},
|
|
{"unmatched release", 'F', false, true, mods, false, false},
|
|
{"extra modifier", 'F', true, true, extra, false, false},
|
|
{"release extra modifier chord", 'F', false, true, extra, false, false},
|
|
{"match", 'F', true, true, mods, true, true},
|
|
{"auto repeat", 'F', true, true, mods, true, false},
|
|
{"repeat after focus and modifier change", 'F', true, false, FullscreenHotkeyMods{}, true, false},
|
|
{"release after focus change", 'F', false, false, FullscreenHotkeyMods{}, true, false},
|
|
{"second release", 'F', false, false, mods, false, false},
|
|
{"next press", 'F', true, true, mods, true, true},
|
|
} {
|
|
consume, trigger := matcher.Handle(step.key, step.down, step.foreground, step.mods)
|
|
if consume != step.consume || trigger != step.trigger {
|
|
t.Fatalf("%s: got (%v, %v), want (%v, %v)", step.name, consume, trigger, step.consume, step.trigger)
|
|
}
|
|
}
|
|
matcher.Reset()
|
|
if consume, trigger := matcher.Handle('F', false, false, mods); consume || trigger {
|
|
t.Fatalf("reset left suppression active: (%v, %v)", consume, trigger)
|
|
}
|
|
}
|
|
|
|
func TestFullscreenHotkeyHookLifecycle(t *testing.T) {
|
|
svc := &FullscreenHotkeyService{}
|
|
t.Cleanup(svc.Close)
|
|
targets := []IMETarget{{VMName: "mac", PID: 1234}}
|
|
callback := func(IMETarget, uintptr) {}
|
|
hotkey, _, _ := ParseFullscreenHotkey("")
|
|
assertHook := func(want bool) {
|
|
t.Helper()
|
|
if got := svc.HookActive(); got != want {
|
|
t.Fatalf("fullscreen hook installed = %t, want %t", got, want)
|
|
}
|
|
}
|
|
svc.Sync(true, hotkey, nil, callback)
|
|
assertHook(false)
|
|
svc.Sync(true, hotkey, targets, callback)
|
|
assertHook(runtime.GOOS == "windows")
|
|
svc.Sync(true, hotkey, nil, callback)
|
|
assertHook(false)
|
|
svc.Sync(true, hotkey, targets, callback)
|
|
assertHook(runtime.GOOS == "windows")
|
|
svc.Sync(false, hotkey, targets, callback)
|
|
assertHook(false)
|
|
svc.Sync(true, hotkey, targets, callback)
|
|
assertHook(runtime.GOOS == "windows")
|
|
svc.Close()
|
|
svc.Sync(true, hotkey, targets, callback)
|
|
assertHook(false)
|
|
}
|
|
|
|
func TestFullscreenHotkeyReconfigurationWhileHeld(t *testing.T) {
|
|
f8, _, _ := ParseFullscreenHotkey("F8")
|
|
f9, _, _ := ParseFullscreenHotkey("F9")
|
|
m := FullscreenHotkeyMatcher{Hotkey: f8}
|
|
if consume, trigger := m.Handle(f8.VK, true, true, FullscreenHotkeyMods{}); !consume || !trigger {
|
|
t.Fatalf("F8 down = (%t, %t)", consume, trigger)
|
|
}
|
|
m.setHotkey(f9)
|
|
for _, down := range []bool{true, false} {
|
|
if consume, trigger := m.Handle(f8.VK, down, false, FullscreenHotkeyMods{}); !consume || trigger {
|
|
t.Fatalf("old F8 event down=%t = (%t, %t)", down, consume, trigger)
|
|
}
|
|
}
|
|
if consume, trigger := m.Handle(f9.VK, true, true, FullscreenHotkeyMods{}); !consume || !trigger {
|
|
t.Fatalf("F9 down = (%t, %t)", consume, trigger)
|
|
}
|
|
m.Reset()
|
|
if consume, trigger := m.Handle(f9.VK, true, true, FullscreenHotkeyMods{}); !consume || !trigger {
|
|
t.Fatalf("F9 down after reset = (%t, %t)", consume, trigger)
|
|
}
|
|
}
|
|
|
|
func TestFullscreenPhysicalModifiersIgnoreInjectedKeys(t *testing.T) {
|
|
for i := 0; i < len(fullscreenModifierVKs); i += 2 {
|
|
left, right := fullscreenModifierVKs[i], fullscreenModifierVKs[i+1]
|
|
var m fullscreenPhysicalModifiers
|
|
m.handle(left, true, false)
|
|
first := m.mods()
|
|
if first == (FullscreenHotkeyMods{}) {
|
|
t.Fatalf("physical modifier %#x was ignored", left)
|
|
}
|
|
m.handle(left, false, true)
|
|
m.handle(right, true, false)
|
|
m.handle(left, false, false)
|
|
if got := m.mods(); got != first {
|
|
t.Fatalf("physical right modifier %#x: got %+v, want %+v", right, got, first)
|
|
}
|
|
m.handle(right, false, false)
|
|
m.handle(left, true, true)
|
|
if got := m.mods(); got != (FullscreenHotkeyMods{}) {
|
|
t.Fatalf("injected modifier %#x changed physical state: %+v", left, got)
|
|
}
|
|
}
|
|
}
|