Extracted from prototypes/yetano-webview (AP1): packages assets, document, bridge and window (macOS Cocoa/WKWebView via cgo, Windows Win32/WebView2 pure Go). No local HTTP listener or port: assets go through the WebView's resource handler, app routes through the native message binding. Own go.mod and gui/vX.Y.Z tags keep cgo and the WebView dependencies out of the main core module. Adds backend-free tests for the window's dispatch/destroy guards and a yet-gui migration guide. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
76 lines
2.1 KiB
Go
76 lines
2.1 KiB
Go
package window
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
// fakeBackend queues dispatched functions like a real UI loop (run them
|
|
// with drain) and records calls.
|
|
type fakeBackend struct {
|
|
queue []func()
|
|
evals []string
|
|
destroyed int
|
|
}
|
|
|
|
func (f *fakeBackend) drain() {
|
|
for len(f.queue) > 0 {
|
|
g := f.queue[0]
|
|
f.queue = f.queue[1:]
|
|
g()
|
|
}
|
|
}
|
|
|
|
func (f *fakeBackend) Init(string) {}
|
|
func (f *fakeBackend) Navigate(string) {}
|
|
func (f *fakeBackend) Run() {}
|
|
func (f *fakeBackend) Terminate() {}
|
|
func (f *fakeBackend) Dispatch(g func()) { f.queue = append(f.queue, g) }
|
|
func (f *fakeBackend) Eval(js string) { f.evals = append(f.evals, js) }
|
|
func (f *fakeBackend) Destroy() { f.destroyed++ }
|
|
|
|
func TestDispatchAfterDestroyIsDropped(t *testing.T) {
|
|
fb := &fakeBackend{}
|
|
w := &Window{b: fb, alive: true, name: "fake"}
|
|
ran := 0
|
|
w.Dispatch(func() { ran++; w.Eval("a") })
|
|
w.Dispatch(func() { ran++ })
|
|
fb.drain()
|
|
w.Dispatch(func() { ran++ }) // queued, Destroy comes first
|
|
w.Destroy()
|
|
fb.drain()
|
|
w.Destroy() // idempotent
|
|
w.Dispatch(func() { ran++ })
|
|
w.Eval("b")
|
|
if ran != 2 || len(fb.evals) != 1 || fb.destroyed != 1 || w.DroppedDispatches() != 1 || len(fb.queue) != 0 {
|
|
t.Fatalf("ran=%d evals=%v destroyed=%d dropped=%d", ran, fb.evals, fb.destroyed, w.DroppedDispatches())
|
|
}
|
|
}
|
|
|
|
func TestSnapshotUnsupported(t *testing.T) {
|
|
w := &Window{b: &fakeBackend{}, alive: true, name: "fake"}
|
|
var got error
|
|
w.Snapshot("x.png", func(err error) { got = err })
|
|
if got == nil {
|
|
t.Fatal("expected an error from a backend without snapshot support")
|
|
}
|
|
w.SetAppearance("dark") // no-op without support, must not panic
|
|
}
|
|
|
|
func TestTaskTableCancel(t *testing.T) {
|
|
var tt taskTable
|
|
ctx1, c1 := context.WithCancel(context.Background())
|
|
ctx2, c2 := context.WithCancel(context.Background())
|
|
tt.add(1, c1)
|
|
tt.add(2, c2)
|
|
tt.cancel(1)
|
|
if ctx1.Err() == nil || ctx2.Err() != nil {
|
|
t.Fatal("cancel(1) must cancel only task 1")
|
|
}
|
|
tt.cancel(1) // unknown id: no-op
|
|
tt.cancelAll()
|
|
if ctx2.Err() == nil {
|
|
t.Fatal("cancelAll must cancel the remaining tasks")
|
|
}
|
|
}
|