From dbac411b2e422560704a146d739a93ca2e4d4c12 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 14 Nov 2026 17:44:54 +0800 Subject: [PATCH] gui: TestRestartedWindowIsDrawn puts the other app in front as the user would The behind and cover cases start a WinForms window as "the app the user is in" and wait 20 s for it to hold the foreground. Windows lets it take the front only while the test's old magpie may give it away, and that depends on what held the foreground before it: run alone on the Windows box at d987cc7d5, quiet-behind failed with "old: the other app never came to the front" after the asked case's window had the front, and in a whole-package run three other behind/cover cases did. The restart under test never ran in them. Now, from a second on, the old process presses F15 (a key nothing answers) and brings the form to the front, as the user's own press and click would; input this process sent is what lets it give the foreground away. Run on the Windows box, interactive session: the whole test (13 cases) at d987cc7d5 with this change, 13 of 13 green (1 red without it); at this tree with the no-attention change below it, 13 of 13 green, twice (4 red without it: quiet-behind, quiet-behind-max, quiet-cover, quiet-cover-max, all at the same setup step). --- internal/gui/quiet_restart_windows_test.go | 25 +++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/internal/gui/quiet_restart_windows_test.go b/internal/gui/quiet_restart_windows_test.go index 8e33d5629..2ad6b43bc 100644 --- a/internal/gui/quiet_restart_windows_test.go +++ b/internal/gui/quiet_restart_windows_test.go @@ -20,6 +20,7 @@ import ( "github.com/wailsapp/wails/v3/pkg/application" "github.com/wailsapp/wails/v3/pkg/events" + "github.com/wailsapp/wails/v3/pkg/w32" "golang.org/x/sys/windows" "github.com/yetone/magpie/internal/proc" @@ -240,6 +241,21 @@ func restartDrawPage(loaded chan<- struct{}, seen chan<- string) http.Handler { }) } +// userFront brings the window titled title to the front as the user's +// own key press and click would: a key is input this process sent, which +// lets it give the foreground away (SetForegroundWindow's rules). +func userFront(title string) { + t, _ := windows.UTF16PtrFromString(title) + hwnd := w32.FindWindowW(nil, t) + if hwnd == 0 { + return + } + const vkF15, keyUp = 0x7E, 0x2 // a key nothing answers + w32.KeybdEvent(vkF15, 0, 0, 0) + w32.KeybdEvent(vkF15, 0, keyUp, 0) + w32.SetForegroundWindow(hwnd) +} + // restartDrawOld is the magpie before: its window up and drawn, it starts // the new one as update's launch does and quits. func restartDrawOld(mode string, report func(string, ...any)) { @@ -282,12 +298,19 @@ func restartDrawOld(mode string, report func(string, ...any)) { } other := uint32(ps.Process.Pid) os.Setenv("MAGPIE_TEST_OTHER_PID", strconv.Itoa(int(other))) - for deadline := time.Now().Add(20 * time.Second); ; time.Sleep(100 * time.Millisecond) { + for deadline, click := time.Now().Add(20*time.Second), time.Now().Add(time.Second); ; time.Sleep(100 * time.Millisecond) { var pid uint32 windows.GetWindowThreadProcessId(windows.GetForegroundWindow(), &pid) if pid == other { break } + // Windows lets the form take the front only while this + // process may (it ends in front no matter what came + // before it): the user puts it there, as a click would + if time.Now().After(click) { + userFront("other app") + click = time.Now().Add(time.Second) + } if time.Now().After(deadline) { fail("old: the other app never came to the front") }