- Build single-source-of-truth 7-domain locale catalog (`locales/{zh-CN,en-US}/*.json`, 1,100+ keys) and `cmd/i18ngen` generator emitting Go catalogs and eager/lazy-split TypeScript domain modules (`core`/`vm` eager; `wizard`/`settings`/`plugins` lazy)
- Add zero-dependency ICU MessageFormat subset (`{var}`, `{n, number}`, CLDR `plural`, `select`), 8-hop cycle-free regional fallback (`zh-HK/zh-MO -> zh-Hant -> zh-TW -> zh-CN`), `auto` OS display-language preference, and `en-XA` pseudo-localization across Go and TypeScript
- Introduce `CodedError` supportability with `[E_CODE]` badges, collapsible `[detail]` diagnostics, and automatic SMC OSK / SMBIOS serial / MAC / token redaction
- Add Host × Guest macOS dual-terminology annotation (`FormatGuestTerm`) across 6 official Apple guest UI locales and localize Win32 system tray, file pickers, and DPI-adaptive `installguide` overlays with CJK font fallback probing
- Upgrade L1–L4 plugin localization to dual-track v2: Track A full language packs (`files`, WOFF2 `fonts`, cross-plugin `pluginOverrides`), Track B plugin-scoped `l10n` bundles (`HasUIMessages` isolation), live `plugin/localeChanged` notifications, `< 2ms` static CLI pack loading, and `reims-go plugin locale init|check` translator toolchain with `zh-TW` and `ja-JP` reference packs
- Harden security, WCAG 2.1 AA token contrast, `< 768px` responsive drawer & form layouts, and RTL logical properties; add backend security/locale regression tests and frontend `node --test` static/runtime guard suite in CI
# Conflicts:
# frontend/src/i18n.ts
# internal/cli/cli.go
# internal/i18n/i18n.go
536 lines
19 KiB
Go
536 lines
19 KiB
Go
package installguide
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
"unsafe"
|
|
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
)
|
|
|
|
// Exercise the actual Win32 controls without taking focus or starting a VM.
|
|
func TestOwnedGuide(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 destroyWindow.Call(owner)
|
|
g := Guide{Name: "test-vm", PID: uint32(os.Getpid()), Kind: Offline, Locale: i18n.ZhCN, Theme: ThemeLight}
|
|
var w panel
|
|
defer w.close()
|
|
getItem := user32.NewProc("GetDlgItem")
|
|
send := user32.NewProc("SendMessageW")
|
|
peek := user32.NewProc("PeekMessageW")
|
|
for _, step := range []struct {
|
|
from page
|
|
button uintptr
|
|
want page
|
|
}{
|
|
{boot, idFold, compact}, {compact, idFold, boot},
|
|
{boot, idRight, terminal}, {terminal, idRight, command}, {command, idRight, typingCommand},
|
|
{command, idExtra, verifyCommand}, {verifyCommand, idRight, waitInstall}, {waitInstall, idRight, confirm},
|
|
{confirm, idLeft, waitInstall}, {confirm, idRight, working}, {failed, idLeft, command},
|
|
{setup, idRight, network}, {network, idRight, account}, {account, idRight, localUser},
|
|
{localUser, idRight, preferences}, {preferences, idRight, desktop}, {desktop, idRight, done},
|
|
} {
|
|
if err := w.sync(owner, g, step.from, "test failure"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if actual, _, _ := user32.NewProc("GetWindow").Call(w.hwnd, 4); actual != owner {
|
|
t.Fatal("guide has wrong owner")
|
|
}
|
|
var guest, overlay rect
|
|
getWindowRect.Call(owner, uintptr(unsafe.Pointer(&guest)))
|
|
getWindowRect.Call(w.hwnd, uintptr(unsafe.Pointer(&overlay)))
|
|
if overlay.right > guest.right || overlay.left < guest.left || overlay.top < guest.top || overlay.bottom > guest.bottom {
|
|
t.Fatalf("guide outside guest: %+v vs %+v", overlay, guest)
|
|
}
|
|
button, _, _ := getItem.Call(w.hwnd, step.button)
|
|
if button == 0 {
|
|
t.Fatalf("missing button %d on page %s", step.button, step.from)
|
|
}
|
|
send.Call(button, 0x00f5, 0, 0) // BM_CLICK
|
|
var msg message
|
|
ok, _, _ := peek.Call(uintptr(unsafe.Pointer(&msg)), w.hwnd, wmAction, wmAction, 1)
|
|
if ok == 0 || (session{current: step.from, unfolded: boot, returnTo: command}).target(g, msg.wp) != step.want {
|
|
t.Fatalf("click on page %s: %+v", step.from, msg)
|
|
}
|
|
if err := w.sync(0, g, step.from, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if visible, _, _ := user32.NewProc("IsWindowVisible").Call(w.hwnd); visible != 0 {
|
|
t.Fatal("guide still visible over another application")
|
|
}
|
|
w.close()
|
|
}
|
|
|
|
// Live theme switch & atomic swap rebuild the panel and destroy the old HWND.
|
|
g.Theme = ThemeLight
|
|
if err := w.sync(owner, g, command, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
firstHWND := w.hwnd
|
|
if getDialogMeta(firstHWND).dark {
|
|
t.Fatal("expected light dialog meta")
|
|
}
|
|
// Verify link control hugs its label symmetrically instead of spanning the whole card.
|
|
if extra, _, _ := getItem.Call(firstHWND, idExtra); extra != 0 {
|
|
var linkRect, cardRect rect
|
|
getClientRect.Call(extra, uintptr(unsafe.Pointer(&linkRect)))
|
|
getClientRect.Call(firstHWND, uintptr(unsafe.Pointer(&cardRect)))
|
|
if linkRect.right >= cardRect.right/2 {
|
|
t.Fatalf("extra link was not shrunk to text width: link=%d card=%d", linkRect.right, cardRect.right)
|
|
}
|
|
}
|
|
g.Theme = ThemeDark
|
|
if err := w.sync(owner, g, command, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w.hwnd == firstHWND || !getDialogMeta(w.hwnd).dark {
|
|
t.Fatal("theme switch did not rebuild dialog in dark mode")
|
|
}
|
|
if ok, _, _ := isWindow.Call(firstHWND); ok != 0 {
|
|
t.Fatal("atomic swap leaked previous dialog HWND")
|
|
}
|
|
// Verify stale atomic swap (page change) destroys old HWND and shows new page.
|
|
secondHWND := w.hwnd
|
|
w.markStale()
|
|
if err := w.sync(owner, g, failed, "stop timed out"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w.hwnd == secondHWND || getDialogMeta(w.hwnd).stageTone != toneDanger {
|
|
t.Fatalf("failed page swap: hwnd=%#x tone=%d", w.hwnd, getDialogMeta(w.hwnd).stageTone)
|
|
}
|
|
if ok, _, _ := isWindow.Call(secondHWND); ok != 0 {
|
|
t.Fatal("page swap leaked previous dialog HWND")
|
|
}
|
|
|
|
// Verify an owned/small QEMU modal dialog (like "Turn off the VM?") hides
|
|
// the guide instead of stealing ownership and getting covered by it.
|
|
modal, _, err := user32.NewProc("CreateWindowExW").Call(0, uintptr(unsafe.Pointer(class)), 0, 0x80000000,
|
|
uintptr(offscreen), uintptr(offscreen), 360, 160, owner, 0, 0, 0)
|
|
if modal == 0 {
|
|
t.Fatal(err)
|
|
}
|
|
defer destroyWindow.Call(modal)
|
|
if err := w.sync(modal, g, failed, "stop timed out"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w.owner != owner {
|
|
t.Fatal("guide re-parented onto guest modal dialog")
|
|
}
|
|
if visible, _, _ := user32.NewProc("IsWindowVisible").Call(w.hwnd); visible != 0 {
|
|
t.Fatal("guide remained visible over guest modal dialog")
|
|
}
|
|
if err := w.sync(owner, g, failed, "stop timed out"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if visible, _, _ := user32.NewProc("IsWindowVisible").Call(w.hwnd); visible == 0 {
|
|
t.Fatal("guide did not reappear after guest modal dialog closed")
|
|
}
|
|
w.close()
|
|
|
|
if (session{current: working}).target(g, idRight) != working {
|
|
t.Fatal("double click repeated the action")
|
|
}
|
|
|
|
// Verify body, spacing, alignment rails, non-overlap and control labels across
|
|
// built-in locales, cross-language Host/Guest dual-terms, and en-XA pseudo-locale!
|
|
for _, tc := range []struct {
|
|
loc i18n.Locale
|
|
guestLang string
|
|
}{
|
|
{i18n.ZhCN, "zh-Hans:0"},
|
|
{i18n.ZhCN, "en-US:0"}, // Host zh-CN + Guest en-US dual terminology
|
|
{i18n.EnUS, "en-US:0"},
|
|
{i18n.EnUS, "ja-JP:0"}, // Host en-US + Guest ja-JP dual terminology
|
|
{i18n.EnXA, "en-US:0"}, // Extreme text-expansion pseudo-localization stress test
|
|
} {
|
|
g.Locale = tc.loc
|
|
g.GuestLang = tc.guestLang
|
|
g.Theme = ThemeLight
|
|
g.ReleaseKeys = "Ctrl+Alt+Win"
|
|
for _, kind := range []Kind{Offline, Online, BuiltIn} {
|
|
g.Kind = kind
|
|
for _, p := range []page{boot, terminal, command, verifyCommand, selectDisk, formatDisk, reinstall, waitInstall, recoverMenu, mediaChoice, confirmImage, confirm, confirmRepair, restartPending, setup, network, account, localUser, preferences, desktop, restored, confirmTools, toolsTerminal, toolsVerify, working, typingTools, failed, compact} {
|
|
detail := "offline-installer-tahoe-26.7.3.img"
|
|
if p == failed {
|
|
detail = "stop timed out waiting for guest shutdown"
|
|
}
|
|
if err := w.sync(owner, g, p, detail); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c := g.content(p, detail)
|
|
st := getPanelState(w.hwnd)
|
|
if st == nil {
|
|
t.Fatalf("missing panelState for page %s", p)
|
|
}
|
|
dpi := st.dpi
|
|
if p == compact {
|
|
if ctrl, _, _ := getItem.Call(w.hwnd, idFold); ctrl != 0 {
|
|
checkSingleLineFits(t, ctrl, c.right, dp(dpi, 25))
|
|
}
|
|
} else {
|
|
checkCardGeometry(t, w.hwnd, st, p, c)
|
|
if body, _, _ := getItem.Call(w.hwnd, idBody); body != 0 {
|
|
checkBodyFits(t, body, st, c.body)
|
|
}
|
|
if title, _, _ := getItem.Call(w.hwnd, idTitle); title != 0 {
|
|
checkTitleFits(t, title, st, c.title)
|
|
}
|
|
if right, _, _ := getItem.Call(w.hwnd, idRight); right != 0 && c.right != "" && !st.buttonsStacked {
|
|
checkSingleLineFits(t, right, c.right, dp(dpi, 20))
|
|
}
|
|
if left, _, _ := getItem.Call(w.hwnd, idLeft); left != 0 && c.left != "" && !st.buttonsStacked {
|
|
checkSingleLineFits(t, left, c.left, dp(dpi, 20))
|
|
}
|
|
if extra, _, _ := getItem.Call(w.hwnd, idExtra); extra != 0 && c.extra != "" && !st.extraStacked {
|
|
checkSingleLineFits(t, extra, c.extra, dp(dpi, 12))
|
|
}
|
|
if fold, _, _ := getItem.Call(w.hwnd, idFold); fold != 0 && c.fold != "" {
|
|
checkSingleLineFits(t, fold, c.fold, dp(dpi, 12))
|
|
}
|
|
if jump, _, _ := getItem.Call(w.hwnd, idJump); jump != 0 && c.jump != "" && tc.loc != i18n.EnXA {
|
|
checkSingleLineFits(t, jump, c.jump, dp(dpi, 24))
|
|
}
|
|
if hint, _, _ := getItem.Call(w.hwnd, idInputHint); hint != 0 && c.inputHint != "" && tc.loc != i18n.EnXA {
|
|
checkSingleLineFits(t, hint, c.inputHint, 0)
|
|
}
|
|
}
|
|
w.close()
|
|
}
|
|
}
|
|
}
|
|
panelStatesMu.RLock()
|
|
leaked := len(panelStates)
|
|
panelStatesMu.RUnlock()
|
|
if leaked != 0 {
|
|
t.Fatalf("leaked %d panelStates after close", leaked)
|
|
}
|
|
}
|
|
|
|
func TestOverlayOfficialZhTWAndJaJPLanguagePacksFit(t *testing.T) {
|
|
runtime.LockOSThread()
|
|
defer runtime.UnlockOSThread()
|
|
|
|
loadMap := func(rel string) map[string]string {
|
|
raw, err := os.ReadFile(rel)
|
|
if err != nil {
|
|
t.Fatalf("read %s: %v", rel, err)
|
|
}
|
|
var out map[string]string
|
|
if err := json.Unmarshal(raw, &out); err != nil {
|
|
t.Fatalf("unmarshal %s: %v", rel, err)
|
|
}
|
|
return out
|
|
}
|
|
mergeMaps := func(a, b map[string]string) map[string]string {
|
|
m := make(map[string]string, len(a)+len(b))
|
|
for k, v := range a {
|
|
m[k] = v
|
|
}
|
|
for k, v := range b {
|
|
m[k] = v
|
|
}
|
|
return m
|
|
}
|
|
|
|
twDir := filepath.Join("..", "plugin", "testdata", "official.lang.zh-tw", "locales")
|
|
jaDir := filepath.Join("..", "plugin", "testdata", "official.lang.ja-jp", "locales")
|
|
i18n.SetPluginCatalogs(map[i18n.Locale]i18n.PluginCatalog{
|
|
"zh-TW": {
|
|
Fallback: i18n.ZhCN,
|
|
Messages: mergeMaps(loadMap(filepath.Join(twDir, "native.json")), loadMap(filepath.Join(twDir, "macos-glossary.json"))),
|
|
},
|
|
"ja-JP": {
|
|
Fallback: i18n.EnUS,
|
|
Messages: mergeMaps(loadMap(filepath.Join(jaDir, "native.json")), loadMap(filepath.Join(jaDir, "macos-glossary.json"))),
|
|
},
|
|
})
|
|
defer i18n.SetPluginCatalogs(nil)
|
|
|
|
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, 800, 0, 0, 0, 0)
|
|
if owner == 0 {
|
|
t.Fatal(err)
|
|
}
|
|
defer destroyWindow.Call(owner)
|
|
getItem := user32.NewProc("GetDlgItem")
|
|
|
|
g := Guide{
|
|
Name: "macos-tahoe-dev",
|
|
PID: uint32(os.Getpid()),
|
|
Kind: Online,
|
|
ReleaseKeys: "Ctrl+Alt+Win",
|
|
Theme: ThemeLight,
|
|
}
|
|
for _, tc := range []struct {
|
|
loc i18n.Locale
|
|
guestLang string
|
|
}{
|
|
{"zh-TW", "zh-Hant:0"},
|
|
{"zh-TW", "en-US:0"},
|
|
{"ja-JP", "ja-JP:0"},
|
|
{"ja-JP", "en-US:0"},
|
|
} {
|
|
g.Locale = tc.loc
|
|
g.GuestLang = tc.guestLang
|
|
for _, p := range []page{boot, selectDisk, formatDisk, confirm, setup, desktop, compact} {
|
|
var w panel
|
|
if err := w.sync(owner, g, p, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c := g.content(p, "")
|
|
st := getPanelState(w.hwnd)
|
|
if st == nil {
|
|
t.Fatalf("missing panelState for %s/%s", tc.loc, p)
|
|
}
|
|
if p != compact {
|
|
checkCardGeometry(t, w.hwnd, st, p, c)
|
|
if body, _, _ := getItem.Call(w.hwnd, idBody); body != 0 {
|
|
checkBodyFits(t, body, st, c.body)
|
|
}
|
|
if title, _, _ := getItem.Call(w.hwnd, idTitle); title != 0 {
|
|
checkTitleFits(t, title, st, c.title)
|
|
}
|
|
}
|
|
w.close()
|
|
}
|
|
}
|
|
}
|
|
|
|
func childRectInParent(parent, child uintptr) rect {
|
|
var r rect
|
|
getWindowRect.Call(child, uintptr(unsafe.Pointer(&r)))
|
|
var p rect
|
|
getWindowRect.Call(parent, uintptr(unsafe.Pointer(&p)))
|
|
return rect{left: r.left - p.left, top: r.top - p.top, right: r.right - p.left, bottom: r.bottom - p.top}
|
|
}
|
|
|
|
func checkCardGeometry(t *testing.T, hwnd uintptr, st *panelState, p page, c content) {
|
|
t.Helper()
|
|
var card rect
|
|
getClientRect.Call(hwnd, uintptr(unsafe.Pointer(&card)))
|
|
dpi := st.dpi
|
|
padX := dp(dpi, 16)
|
|
rightRail := card.right - padX
|
|
|
|
type itemBox struct {
|
|
id uintptr
|
|
r rect
|
|
}
|
|
var boxes []itemBox
|
|
ids := []uintptr{idStage, idFold, idTitle, idBody, idCode, idInputHint, idLeft, idExtra, idRight, idJump}
|
|
for _, id := range ids {
|
|
ctrl, _, _ := getDlgItem.Call(hwnd, id)
|
|
if ctrl == 0 {
|
|
continue
|
|
}
|
|
r := childRectInParent(hwnd, ctrl)
|
|
if r.left < 0 || r.top < 0 || r.right > card.right || r.bottom > card.bottom {
|
|
t.Errorf("page=%s ctrl=%d out of card %+v: %+v", p, id, card, r)
|
|
}
|
|
switch id {
|
|
case idStage, idTitle, idBody, idCode, idInputHint:
|
|
if r.left != padX {
|
|
t.Errorf("page=%s ctrl=%d left=%d want padX=%d", p, id, r.left, padX)
|
|
}
|
|
case idFold:
|
|
// Fold text right edge aligns optically with rightRail (box extends +dp(8) for symmetric hover pill).
|
|
if r.right-dp(dpi, 8) != rightRail {
|
|
t.Errorf("page=%s fold text right=%d want rightRail=%d", p, r.right-dp(dpi, 8), rightRail)
|
|
}
|
|
case idRight:
|
|
if r.right != rightRail {
|
|
t.Errorf("page=%s primary right=%d want rightRail=%d", p, r.right, rightRail)
|
|
}
|
|
if r.bottom-r.top != dp(dpi, 31) {
|
|
t.Errorf("page=%s primary height=%d want %d", p, r.bottom-r.top, dp(dpi, 31))
|
|
}
|
|
case idLeft:
|
|
if r.bottom-r.top != dp(dpi, 31) {
|
|
t.Errorf("page=%s secondary height=%d want %d", p, r.bottom-r.top, dp(dpi, 31))
|
|
}
|
|
if (!c.isConfirm || st.buttonsStacked) && c.right != "" && r.left != padX {
|
|
t.Errorf("page=%s left button x=%d want padX=%d", p, r.left, padX)
|
|
}
|
|
case idJump:
|
|
if r.left+dp(dpi, 8) != padX || r.right-dp(dpi, 8) != rightRail {
|
|
t.Errorf("page=%s jump text rails (%d,%d) want (%d,%d)", p, r.left+dp(dpi, 8), r.right-dp(dpi, 8), padX, rightRail)
|
|
}
|
|
}
|
|
boxes = append(boxes, itemBox{id: id, r: r})
|
|
}
|
|
for i := 0; i < len(boxes); i++ {
|
|
for j := i + 1; j < len(boxes); j++ {
|
|
a, b := boxes[i].r, boxes[j].r
|
|
if a.left < b.right && a.right > b.left && a.top < b.bottom && a.bottom > b.top {
|
|
t.Errorf("page=%s controls %d (%+v) and %d (%+v) overlap", p, boxes[i].id, a, boxes[j].id, b)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func checkTitleFits(t *testing.T, label uintptr, st *panelState, text string) {
|
|
t.Helper()
|
|
var available rect
|
|
getClientRect.Call(label, uintptr(unsafe.Pointer(&available)))
|
|
wantH := max(st.titleLineH, int32(len(st.titleLines))*st.titleLineH)
|
|
if available.bottom != wantH {
|
|
t.Errorf("title height=%d want exact %d (%d lines)", available.bottom, wantH, len(st.titleLines))
|
|
}
|
|
dc, _, _ := user32.NewProc("GetDC").Call(label)
|
|
defer user32.NewProc("ReleaseDC").Call(label, dc)
|
|
font, _, _ := sendMessage.Call(label, 0x0031, 0, 0) // WM_GETFONT
|
|
for _, line := range st.titleLines {
|
|
if w := measureLineWidth(dc, font, line); w > available.right {
|
|
t.Errorf("wrapped title line %q width %d exceeds %d (full=%q)", line, w, available.right, text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func checkBodyFits(t *testing.T, label uintptr, st *panelState, text string) {
|
|
t.Helper()
|
|
var available rect
|
|
getClientRect.Call(label, uintptr(unsafe.Pointer(&available)))
|
|
wantH := max(st.bodyLineH, int32(len(st.bodyLines))*st.bodyLineH)
|
|
if available.bottom != wantH {
|
|
t.Errorf("body height=%d want exact %d (%d lines)", available.bottom, wantH, len(st.bodyLines))
|
|
}
|
|
dc, _, _ := user32.NewProc("GetDC").Call(label)
|
|
defer user32.NewProc("ReleaseDC").Call(label, dc)
|
|
font, _, _ := sendMessage.Call(label, 0x0031, 0, 0) // WM_GETFONT
|
|
for _, line := range st.bodyLines {
|
|
if w := measureLineWidth(dc, font, line); w > available.right {
|
|
t.Errorf("wrapped body line %q width %d exceeds %d (full=%q)", line, w, available.right, text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func checkSingleLineFits(t *testing.T, ctrl uintptr, text string, padX int32) {
|
|
t.Helper()
|
|
dc, _, _ := user32.NewProc("GetDC").Call(ctrl)
|
|
defer user32.NewProc("ReleaseDC").Call(ctrl, dc)
|
|
font, _, _ := sendMessage.Call(ctrl, 0x0031, 0, 0) // WM_GETFONT
|
|
old, _, _ := selectGDI.Call(dc, font)
|
|
defer selectGDI.Call(dc, old)
|
|
var available rect
|
|
getClientRect.Call(ctrl, uintptr(unsafe.Pointer(&available)))
|
|
needed := available
|
|
s, _ := syscall.UTF16FromString(text)
|
|
drawText.Call(dc, uintptr(unsafe.Pointer(&s[0])), uintptr(len(s)-1), uintptr(unsafe.Pointer(&needed)), 0x400|0x820)
|
|
if needed.right+padX > available.right || needed.bottom > available.bottom {
|
|
t.Errorf("single-line needs %+v (+%dpx pad), have %+v: %q", needed, padX, available, text)
|
|
}
|
|
}
|
|
|
|
func TestWinEventFollowsOwner(t *testing.T) {
|
|
runtime.LockOSThread()
|
|
defer runtime.UnlockOSThread()
|
|
|
|
class, _ := syscall.UTF16PtrFromString("STATIC")
|
|
offX, offY := int32(-16000), int32(-16000)
|
|
// WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE (0x08000080), WS_POPUP | WS_VISIBLE (0x90000000)
|
|
// Offscreen visible window so Win32 emits EVENT_OBJECT_LOCATIONCHANGE without taking focus or showing on screen.
|
|
owner, _, err := user32.NewProc("CreateWindowExW").Call(
|
|
0x08000080, uintptr(unsafe.Pointer(class)), 0, 0x90000000,
|
|
uintptr(offX), uintptr(offY), 1280, 720, 0, 0, 0, 0,
|
|
)
|
|
if owner == 0 {
|
|
t.Fatal(err)
|
|
}
|
|
defer destroyWindow.Call(owner)
|
|
|
|
g := Guide{Name: "test-vm", PID: uint32(os.Getpid()), Kind: Offline, Locale: i18n.ZhCN, Theme: ThemeDark}
|
|
var w panel
|
|
defer w.close()
|
|
if err := w.sync(owner, g, terminal, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
peek := user32.NewProc("PeekMessageW")
|
|
pump := func() {
|
|
var msg message
|
|
for i := 0; i < 64; i++ {
|
|
ok, _, _ := peek.Call(uintptr(unsafe.Pointer(&msg)), 0, 0, 0, 1)
|
|
if ok == 0 {
|
|
break
|
|
}
|
|
translateMessage.Call(uintptr(unsafe.Pointer(&msg)))
|
|
dispatchMessage.Call(uintptr(unsafe.Pointer(&msg)))
|
|
}
|
|
}
|
|
pump()
|
|
|
|
beforeHooks := func() int {
|
|
winEventMu.Lock()
|
|
defer winEventMu.Unlock()
|
|
return len(winEventHooks)
|
|
}()
|
|
|
|
unhook := w.attachHooks(g.PID, func() {})
|
|
defer func() {
|
|
unhook()
|
|
winEventMu.Lock()
|
|
afterHooks := len(winEventHooks)
|
|
winEventMu.Unlock()
|
|
if afterHooks != beforeHooks {
|
|
t.Fatalf("leaked WinEvent hooks: before=%d after=%d", beforeHooks, afterHooks)
|
|
}
|
|
}()
|
|
|
|
waitForOrigin := func(wantX, wantY int32, label string) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(1 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
pump()
|
|
var r rect
|
|
getWindowRect.Call(w.hwnd, uintptr(unsafe.Pointer(&r)))
|
|
if r.left == wantX && r.top == wantY {
|
|
return
|
|
}
|
|
time.Sleep(2 * time.Millisecond)
|
|
}
|
|
var r rect
|
|
getWindowRect.Call(w.hwnd, uintptr(unsafe.Pointer(&r)))
|
|
t.Fatalf("%s: overlay at (%d,%d), want (%d,%d)", label, r.left, r.top, wantX, wantY)
|
|
}
|
|
|
|
var initial rect
|
|
getWindowRect.Call(w.hwnd, uintptr(unsafe.Pointer(&initial)))
|
|
overlayW := initial.right - initial.left
|
|
dpi := windowDPI(owner)
|
|
marginX := dp(dpi, 16)
|
|
offsetY := dp(dpi, 40)
|
|
|
|
// 1. Move owner window: overlay should follow via EVENT_OBJECT_LOCATIONCHANGE with no timer.
|
|
newX, newY := offX+240, offY+130
|
|
setWindowPos.Call(owner, 0, uintptr(newX), uintptr(newY), 0, 0, 0x0015) // SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE
|
|
waitForOrigin(newX+1280-overlayW-marginX, newY+offsetY, "after move")
|
|
|
|
// 2. Resize owner window: overlay should stay pinned to the new top-right corner.
|
|
setWindowPos.Call(owner, 0, 0, 0, 960, 600, 0x0016) // SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE
|
|
waitForOrigin(newX+960-overlayW-marginX, newY+offsetY, "after resize")
|
|
|
|
// 3. Disabling owner (e.g., guest modal dialog) causes followOwner to hide the overlay.
|
|
enableWindow := user32.NewProc("EnableWindow")
|
|
enableWindow.Call(owner, 0)
|
|
defer enableWindow.Call(owner, 1)
|
|
w.followOwner()
|
|
if visible, _, _ := user32.NewProc("IsWindowVisible").Call(w.hwnd); visible != 0 {
|
|
t.Fatal("overlay remained visible after owner was disabled")
|
|
}
|
|
}
|