Files
magpie/internal/gui/syshourcycle_test.go
T
yetone 50ce30d56e gui: times follow the system's 12 or 24 hour clock (iXerol on X)
An en-US Mac set to 24-Hour Time showed Routing's request times in 12
hours: no webview hands the page the system's clock, and navigator.language
carries only the region. boot.js now says it as hourCycle ("h23" or "h12",
left out when the system names none, and for a browser tab, which keeps its
own): macOS from AppleICUForce24HourTime / AppleICUForce12HourTime, Windows
from Control Panel\International sTimeFormat, Linux from gsettings
clock-format. intlLang() adds -u-hc-<cycle> to its tag, so every formatter
that takes it follows, and the places that forced 24 hours (hour12: false,
hourCycle h23) spread hourClock() instead: 24 unless the system says 12.
Read once per 30s, so a change in System Settings shows at the next load.

Run on mote: new hc-clock.test.cjs (en-US on a 24h system, en-GB on a 12h
one, a silent system), chromium and webkit; Go tests for the three readers
and boot.js; gofmt; go vet darwin, windows and linux nogui; internal/gui
tests; GUI files of gui-*, i18n-*, panel-*, quota-*, reset-*, routing-*,
usage-* in chromium and webkit. TestCoreScriptBudget was over by 77 bytes
and passes with two app.js comments cut, no raise.
Broken by hand: hc-clock goes red (2 of 3) without the intlLang suffix.
Not tried: a real Windows or Linux session's setting (the readers are
unit-tested on their formats), a live Mac.
2026-11-11 04:13:38 -08:00

74 lines
2.1 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 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)
}
}