Files
magpie/internal/gui/webview2_test.go
T
yetone ff49ca72fe gui: Windows without the WebView2 runtime says so in a message box and offers the download page, instead of a double-click doing nothing (#1806)
magpie's windows are WebView2, which Windows 10 doesn't always have.
Without it Wails handed the failure to ErrorHandler and exited, and the
desktop build (-H windowsgui) has no console for the log line to land
in: ufec's double-click came up to nothing at all. ufec found the cause,
that Wails' own Env.Info().PlatformInfo["WebView2"] answers "" then, and
that a box can't come from ErrorHandler (it runs on the thread Wails
makes its windows on); this takes both from their branch.

Run asks webView2Check right after application.New, before any window.
Missing, it logs it and shows MessageBoxW (Yes/No, error icon, topmost)
in the reader's language (trayLang): the runtime is missing, Microsoft's
page https://developer.microsoft.com/microsoft-edge/webview2/, that
"magpie web" and "magpie tui" work without it, and whether to open the
page now; Yes opens it (ShellExecute), and Run ends. A key Wails doesn't
set is read as present: refusing to start a magpie that would have
worked is worse than the silent exit. The tray isn't kept: its panel is
a WebView2 window too.

Run: TestWebView2Missing and TestWebView2Notice (all OSes);
TestWebView2MissingFromWails on the Windows box (10.0.0.132, Windows
with WebView2 installed): a child process with Wails pointed at a folder
that isn't a runtime reads missing, the machine's own runtime reads
present. TestWebView2Missing goes red with webView2Missing made to answer
false. On the box, a throwaway -H windowsgui test binary calling
webView2Check with that bogus runtime path, run in the desktop session:
the box came up titled "magpie 需要 WebView2" with the Chinese text, the
page and 是/否 buttons (read back from the dialog's controls), the log
line was written, pressing 是 opened the page in Edge and the check
answered false. go test -tags nogui ./internal/gui in a sandbox HOME;
gofmt -l clean; go vet ./... on darwin, GOOS=linux -tags nogui and
GOOS=windows; windows and linux builds.

Not tried: a machine that really lacks the runtime (the box has it; the
missing case is Wails' lookup failing on a path that isn't a runtime,
the same failure), nor a real magpie.exe start through Run.
2026-10-12 22:03:29 +08:00

55 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gui
import (
"strings"
"testing"
)
// #1806 (ufec): Wails' "WebView2" is "" when its lookup fails, the version
// otherwise; a key it doesn't set says nothing and starts the app.
func TestWebView2Missing(t *testing.T) {
for _, c := range []struct {
name string
info map[string]any
want bool
}{
{"failed lookup", map[string]any{"WebView2": ""}, true},
{"a version", map[string]any{"WebView2": "141.0.3537.71"}, false},
{"not reported", map[string]any{}, false},
{"no info", nil, false},
} {
if got := webView2Missing(c.info); got != c.want {
t.Errorf("%s: %v, want %v", c.name, got, c.want)
}
}
}
// The box names the runtime, gives Microsoft's page and asks to open it
// (Yes opens it), in each language the app speaks, English otherwise.
func TestWebView2Notice(t *testing.T) {
for _, lang := range []string{"en", "zh", "zh-TW", "ja", "de", "pt"} {
title, body := webView2Notice(lang)
if !strings.Contains(title, "WebView2") || !strings.Contains(body, "WebView2") {
t.Errorf("%s: doesn't name WebView2: %q / %q", lang, title, body)
}
if !strings.Contains(body, "https://developer.microsoft.com/microsoft-edge/webview2/") {
t.Errorf("%s: no download page in %q", lang, body)
}
if !strings.Contains(body, "magpie web") || !strings.Contains(body, "magpie tui") {
t.Errorf("%s: doesn't say what works without it: %q", lang, body)
}
if strings.Contains(body, "%!") {
t.Errorf("%s: bad format: %q", lang, body)
}
if !strings.HasSuffix(body, "?") && !strings.HasSuffix(body, "?") {
t.Errorf("%s: doesn't end asking whether to open it: %q", lang, body)
}
}
if _, body := webView2Notice("zh"); !strings.Contains(body, "运行时") {
t.Errorf("zh isn't Chinese: %q", body)
}
if title, _ := webView2Notice("pt"); title != "magpie needs WebView2" {
t.Errorf("pt: %q, want English", title)
}
}