Files
magpie/internal/gui/syshourcycle_test.go
T
yetone 4940aed1d8 gui: times follow the system's region, not the webview's (iXerol on X)
A Mac set to English with Japan as its region (AppleLocale en_JP, 24
hours) showed 12 hours: with neither AppleICUForce24HourTime nor
AppleICUForce12HourTime set, the page formatted in WKWebView's
navigator.language (en-US). boot.js now carries the system's own tag as
"locale" (macOS: `defaults read -g AppleLocale`, with @rg= winning;
Windows: Control Panel\International LocaleName; Linux: LC_ALL, LC_TIME,
LANG), and intlLang takes it over navigator.language for English. The two
forced clock switches still win over the region's clock. A browser tab
keeps its own locale.

Run (mica): go test -run 'HourCycle|LocaleTag|Boot' ./internal/gui ok;
gofmt -l clean; go vet darwin, linux nogui, windows on internal/gui clean;
GUI hc-clock, reset-clock-formats, dates-gui-lang, i18n-keys, header-relang,
desktop-lang in chromium and webkit: 0 fail. Real AppleLocale on this Mac
reads en_SG. Broken by hand: intlLang back to navigator.language turns the
new en-JP case red in both engines. Not tried: a real Windows or Linux
session with a regional format that differs from the display language; full
suite starts after the tag.
2026-11-12 17:16:34 +08:00

106 lines
3.2 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestForcedHourCycle(t *testing.T) {
for _, c := range []struct{ f24, f12, want string }{
{"1\n", "", "h23"},
{"", "1\n", "h12"},
{"1\n", "1\n", "h23"},
{"", "", ""},
{"0\n", "0\n", ""},
{"Error: Could not find key", "", ""},
} {
if got := forcedHourCycle(c.f24, c.f12); got != c.want {
t.Errorf("forcedHourCycle(%q, %q) = %q, want %q", c.f24, c.f12, got, c.want)
}
}
}
func TestLocaleTag(t *testing.T) {
for raw, want := range map[string]string{
"en_JP\n": "en-JP",
"en_US": "en-US",
"en-JP": "en-JP",
"en_JP.UTF-8": "en-JP",
"en_US@rg=jpzzzz": "en-JP",
"en_US@calendar=japanese": "en-US",
"zh_Hans_CN": "zh-CN",
"en": "en",
"C": "",
"POSIX": "",
"C.UTF-8": "",
"": "",
} {
if got := localeTag(raw); got != want {
t.Errorf("localeTag(%q) = %q, want %q", raw, got, want)
}
}
}
func TestWindowsHourCycle(t *testing.T) {
for format, want := range map[string]string{
"HH:mm:ss": "h23",
"H:mm:ss": "h23",
"h:mm:ss tt": "h12",
"hh:mm:ss tt": "h12",
"tt h:mm:ss": "h12",
"HH'h'mm": "h23",
"'h'HH:mm": "h23", // the quoted h is text
"'h' 'H'": "",
"": "",
} {
if got := windowsHourCycle(format); got != want {
t.Errorf("windowsHourCycle(%q) = %q, want %q", format, got, want)
}
}
}
func TestGnomeHourCycle(t *testing.T) {
for out, want := range map[string]string{"'24h'\n": "h23", "'12h'\n": "h12", "": "", "'other'": ""} {
if got := gnomeHourCycle(out); got != want {
t.Errorf("gnomeHourCycle(%q) = %q, want %q", out, got, want)
}
}
}
// boot.js tells the app's page the clock the system shows, before it
// paints, and a browser tab nothing: the browser formats its own times.
func TestBootSaysHourCycle(t *testing.T) {
builtinHome(t)
prev := systemHourCycle
t.Cleanup(func() { systemHourCycle = prev })
boot := func(h http.Handler) string {
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest("GET", "/boot.js", nil))
return rec.Body.String()
}
systemHourCycle = func() string { return "h23" }
if b := boot(Handler(nil, nil)); !strings.Contains(b, `"hourCycle":"h23"`) {
t.Fatalf("the app, set to 24 hours: %s", b)
}
if b := boot(webPageHandler(t, webHost{}, nil)); strings.Contains(b, "hourCycle") {
t.Fatalf("a browser tab keeps its own clock: %s", b)
}
systemHourCycle = func() string { return "" }
if b := boot(Handler(nil, nil)); strings.Contains(b, "hourCycle") {
t.Fatalf("the system names none: %s", b)
}
// a Mac set to English in Japan: no clock override, and the region is
// the system's (iXerol on X)
prevLoc := systemLocale
t.Cleanup(func() { systemLocale = prevLoc })
systemLocale = func() string { return "en-JP" }
if b := boot(Handler(nil, nil)); !strings.Contains(b, `"locale":"en-JP"`) || strings.Contains(b, "hourCycle") {
t.Fatalf("the app, region Japan and no override: %s", b)
}
if b := boot(webPageHandler(t, webHost{}, nil)); strings.Contains(b, "locale") {
t.Fatalf("a browser tab keeps its own locale: %s", b)
}
}