137 lines
4.2 KiB
Go
137 lines
4.2 KiB
Go
//go:build windows
|
|
|
|
package host
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
"unsafe"
|
|
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
)
|
|
|
|
func TestVMCloseConfirmation(t *testing.T) {
|
|
const ownerEnv = "REIMS_GO_TEST_CLOSE_DIALOG_OWNER"
|
|
if os.Getenv(ownerEnv) != "" {
|
|
runCloseDialogOwner(t)
|
|
return
|
|
}
|
|
ownerCtx, stopOwner := context.WithTimeout(context.Background(), 25*time.Second)
|
|
defer stopOwner()
|
|
proc := exec.CommandContext(ownerCtx, os.Args[0], "-test.run=^TestVMCloseConfirmation$")
|
|
proc.Env = append(os.Environ(), ownerEnv+"=1")
|
|
proc.Stderr = os.Stderr
|
|
stdout, err := proc.StdoutPipe()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := proc.Start(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() {
|
|
stopOwner()
|
|
_ = proc.Wait()
|
|
}()
|
|
var owner uintptr
|
|
if _, err := fmt.Fscan(bufio.NewReader(stdout), &owner); err != nil || owner == 0 {
|
|
t.Fatalf("read test display HWND: %v", err)
|
|
}
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
canSuspend bool
|
|
button uintptr
|
|
want VMCloseChoice
|
|
}{
|
|
{"shutdown", true, vcIDLeft, VMCloseShutdown},
|
|
{"suspend", true, vcIDRight, VMCloseSuspend},
|
|
{"escape", true, vcIDCancel, VMCloseCancel},
|
|
{"unsupported", false, vcIDCancel, VMCloseCancel},
|
|
} {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
|
|
result := make(chan error, 1)
|
|
name := fmt.Sprintf("%s-%d", tc.name, os.Getpid())
|
|
go func() {
|
|
result <- exerciseVMCloseDialog(ctx, owner, name, tc.button, tc.canSuspend)
|
|
}()
|
|
choice, err := ConfirmVMClose(ctx, owner, name, i18n.ZhCN, "dark", tc.canSuspend)
|
|
deadline := ctx.Err()
|
|
cancel()
|
|
if probeErr := <-result; probeErr != nil || err != nil || choice != tc.want || deadline != nil {
|
|
t.Fatalf("%s: choice %v, want %v, dialog error %v, probe error %v, deadline %v", tc.name, choice, tc.want, err, probeErr, deadline)
|
|
}
|
|
}
|
|
}
|
|
|
|
func runCloseDialogOwner(t *testing.T) {
|
|
runtime.LockOSThread()
|
|
defer runtime.UnlockOSThread()
|
|
class, _ := syscall.UTF16PtrFromString("STATIC")
|
|
offscreen := int32(-16000)
|
|
owner, _, err := user32.NewProc("CreateWindowExW").Call(0x80, uintptr(unsafe.Pointer(class)), 0, 0x80000000,
|
|
uintptr(offscreen), uintptr(offscreen), 1280, 720, 0, 0, 0, 0)
|
|
if owner == 0 {
|
|
t.Fatal(err)
|
|
}
|
|
defer vcDestroyWindow.Call(owner)
|
|
fmt.Fprintln(os.Stdout, owner)
|
|
for {
|
|
var msg vcMessage
|
|
ret, _, _ := procGetMessageW.Call(uintptr(unsafe.Pointer(&msg)), 0, 0, 0)
|
|
if int32(ret) <= 0 {
|
|
return
|
|
}
|
|
vcDispatchMsg.Call(uintptr(unsafe.Pointer(&msg)))
|
|
}
|
|
}
|
|
|
|
func exerciseVMCloseDialog(ctx context.Context, owner uintptr, name string, button uintptr, canSuspend bool) error {
|
|
findWindowW := user32.NewProc("FindWindowW")
|
|
title, _ := syscall.UTF16PtrFromString("关闭「" + name + "」?")
|
|
ticker := time.NewTicker(10 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
for ctx.Err() == nil {
|
|
hwnd, _, _ := findWindowW.Call(0, uintptr(unsafe.Pointer(title)))
|
|
if visible, _, _ := procIsWindowVisible.Call(hwnd); visible != 0 {
|
|
actualOwner, _, _ := procGetWindow.Call(hwnd, 4) // GW_OWNER
|
|
if actualOwner != owner {
|
|
return fmt.Errorf("dialog owner %#x, want %#x", actualOwner, owner)
|
|
}
|
|
var guest, dialog vcRect
|
|
procGetWindowRect.Call(owner, uintptr(unsafe.Pointer(&guest)))
|
|
procGetWindowRect.Call(hwnd, uintptr(unsafe.Pointer(&dialog)))
|
|
dx := guest.left + guest.right - dialog.left - dialog.right
|
|
dy := guest.top + guest.bottom - dialog.top - dialog.bottom
|
|
if dx > 1 || dx < -1 || dy > 1 || dy < -1 {
|
|
return fmt.Errorf("dialog %+v is not centered over %+v", dialog, guest)
|
|
}
|
|
suspend, _, _ := vcGetDlgItem.Call(hwnd, vcIDRight)
|
|
if enabled, _, _ := vcIsWindowEnabled.Call(suspend); suspend == 0 || (enabled != 0) != canSuspend {
|
|
return fmt.Errorf("suspend button availability differs from %t", canSuspend)
|
|
}
|
|
control, _, _ := vcGetDlgItem.Call(hwnd, button)
|
|
if control == 0 {
|
|
return fmt.Errorf("missing button %d", button)
|
|
}
|
|
if button == vcIDCancel {
|
|
vcPostMessage.Call(control, 0x0100, 0x1b, 0) // WM_KEYDOWN, VK_ESCAPE
|
|
} else {
|
|
vcPostMessage.Call(control, 0x00f5, 0, 0) // BM_CLICK
|
|
}
|
|
return nil
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
case <-ticker.C:
|
|
}
|
|
}
|
|
return fmt.Errorf("waiting for confirmation window: %w", ctx.Err())
|
|
}
|