Files
Reims-Go/internal/vmstart/keymap_test.go
T
棠渚听潮 7c18815f53 feat(input,ui): add per-VM focus-exclusive host shortcut capture and hook promotion
- Add per-VM `capture_host_shortcuts` option persisted in `.reims-go-keymap.json`
  (independent from modifier preset and backward-compatible with legacy configs
  and Wails callers omitting the field). When enabled, unmapped Win and Alt keys
  route through the existing Guest Tools v8 hidutil carrier (`lcmd`/`rcmd`/`lopt`/
  `ropt`) while the VM is focused, preventing host Win/Alt shortcuts (e.g. PixPin
  Alt+A, Alt+Tab, Alt+F4, Ctrl+Alt+A, Win+...) without modifying explicit custom
  bindings or bumping Guest Tools version.
- Monitor `EVENT_SYSTEM_FOREGROUND` and `EVENT_OBJECT_NAMECHANGE` on the Windows
  keymap hook thread to immediately release held modifiers on focus loss and
  promote `WH_KEYBOARD_LL` back to the top of the hook chain on VM focus entry
  and SDL3 in-window re-capture, with per-key timestamp filtering for delayed
  focus-loss events, swap-window event deduplication, Win64 32-bit parameter
  narrowing, and fallback timer recovery.
- Serialize `SetVMKeymap` read-merge-save under `keymapMu`, expose the toggle in
  Settings with zh-CN/en-US i18n, and add Go + frontend unit tests.
2026-12-02 10:00:09 +08:00

129 lines
4.8 KiB
Go

package vmstart
import (
"bytes"
"encoding/json"
"maps"
"os"
"path/filepath"
"testing"
)
func TestVMKeymapCapturePersistence(t *testing.T) {
dir := t.TempDir()
file := filepath.Join(dir, keymapConfigFile)
if err := os.WriteFile(file, []byte(`{"preset":"win_to_cmd"}`), 0600); err != nil {
t.Fatal(err)
}
if got := ReadVMKeymap(dir); got.Preset != KeymapWinToCmd || got.CaptureHostShortcuts {
t.Fatalf("legacy config = %+v", got)
}
cfg := VMKeymapConfig{Preset: KeymapOff, CaptureHostShortcuts: true}
got, err := SaveVMKeymap(dir, cfg)
if err != nil || got.Preset != KeymapOff || !got.CaptureHostShortcuts || got.Bindings != nil {
t.Fatalf("save capture only = %+v, %v", got, err)
}
data, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
var compact bytes.Buffer
if err := json.Compact(&compact, data); err != nil {
t.Fatal(err)
}
if want := `{"preset":"off","capture_host_shortcuts":true}`; compact.String() != want {
t.Fatalf("capture only JSON = %s, want %s", compact.String(), want)
}
if got := ReadVMKeymap(dir); got.Preset != KeymapOff || !got.CaptureHostShortcuts || got.Bindings != nil {
t.Fatalf("read capture only = %+v", got)
}
if _, err := SaveVMKeymap(dir, DefaultVMKeymapConfig()); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(file); !os.IsNotExist(err) {
t.Fatalf("turning capture off did not remove config: %v", err)
}
}
func TestVMKeymapPersistence(t *testing.T) {
dir := t.TempDir()
if got := ReadVMKeymap(dir); got.Preset != KeymapOff {
t.Fatalf("default = %+v", got)
}
cfg := VMKeymapConfig{Preset: " CUSTOM ", Bindings: map[HostModifier]GuestModifier{
" LWIN ": " LCMD ", "capslock": "esc", "lctrl": "lctrl", "rwin": "default",
}}
want := map[HostModifier]GuestModifier{"lwin": "lcmd", "capslock": "esc"}
for i := 0; i < 2; i++ {
got, err := SaveVMKeymap(dir, cfg)
if err != nil || got.Preset != KeymapCustom || !maps.Equal(got.Bindings, want) {
t.Fatalf("save = %+v, %v", got, err)
}
}
for _, bad := range []VMKeymapConfig{
{Preset: "bad"},
{Preset: KeymapCustom, Bindings: map[HostModifier]GuestModifier{"win": "esc"}},
{Preset: KeymapCustom, Bindings: map[HostModifier]GuestModifier{"lwin": "bad"}},
{Preset: KeymapCustom, Bindings: map[HostModifier]GuestModifier{"lwin": "lcmd", " LWIN": "esc"}},
} {
if _, err := SaveVMKeymap(dir, bad); err == nil {
t.Fatalf("accepted %+v", bad)
}
}
if got := ReadVMKeymap(dir); got.Preset != KeymapCustom || !maps.Equal(got.Bindings, want) {
t.Fatalf("read after invalid updates = %+v", got)
}
if _, err := SaveVMKeymap(dir, DefaultVMKeymapConfig()); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Join(dir, keymapConfigFile)); !os.IsNotExist(err) {
t.Fatalf("disabled config was not removed: %v", err)
}
}
func TestKeymapPresets(t *testing.T) {
for _, tc := range []struct {
preset KeymapPreset
want map[HostModifier]GuestModifier
}{
{KeymapOff, nil},
{KeymapWinToCmd, map[HostModifier]GuestModifier{"lwin": "lcmd", "rwin": "rcmd"}},
{KeymapSwapAltWin, map[HostModifier]GuestModifier{"lwin": "lopt", "rwin": "ropt", "lalt": "lcmd", "ralt": "rcmd"}},
} {
bindings := ResolveKeymapBindings(VMKeymapConfig{Preset: tc.preset})
if !maps.Equal(bindings, tc.want) || NeedsGuestCarrier(bindings) != (len(tc.want) > 0) {
t.Fatalf("%s = %v", tc.preset, bindings)
}
}
}
func TestEffectiveKeymapBindings(t *testing.T) {
defaults := map[HostModifier]GuestModifier{"lwin": "lcmd", "rwin": "rcmd", "lalt": "lopt", "ralt": "ropt"}
for _, tc := range []struct {
preset KeymapPreset
explicit map[HostModifier]GuestModifier
want map[HostModifier]GuestModifier
}{
{KeymapOff, nil, defaults},
{KeymapWinToCmd, map[HostModifier]GuestModifier{"lwin": "lcmd", "rwin": "rcmd"}, defaults},
{KeymapSwapAltWin, map[HostModifier]GuestModifier{"lwin": "lopt", "rwin": "ropt", "lalt": "lcmd", "ralt": "rcmd"},
map[HostModifier]GuestModifier{"lwin": "lopt", "rwin": "ropt", "lalt": "lcmd", "ralt": "rcmd"}},
{KeymapCustom, map[HostModifier]GuestModifier{"lwin": "lctrl", "lalt": "disabled", "capslock": "esc"},
map[HostModifier]GuestModifier{"lwin": "lctrl", "rwin": "rcmd", "lalt": "disabled", "ralt": "ropt", "capslock": "esc"}},
} {
t.Run(string(tc.preset), func(t *testing.T) {
cfg := VMKeymapConfig{Preset: tc.preset, Bindings: tc.explicit, CaptureHostShortcuts: true}
if got := EffectiveKeymapBindings(cfg); !maps.Equal(got, tc.want) || !NeedsGuestCarrier(got) {
t.Fatalf("capture enabled = %v, want %v", got, tc.want)
}
if got := ResolveKeymapBindings(cfg); !maps.Equal(got, tc.explicit) {
t.Fatalf("explicit bindings = %v, want %v", got, tc.explicit)
}
cfg.CaptureHostShortcuts = false
if got := EffectiveKeymapBindings(cfg); !maps.Equal(got, tc.explicit) {
t.Fatalf("capture disabled = %v, want %v", got, tc.explicit)
}
})
}
}