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") } }