From 109b3c70b4045fd35b37aaca6816b1874ed7ff86 Mon Sep 17 00:00:00 2001 From: Evan Hu Date: Wed, 18 Nov 2026 12:49:46 +0900 Subject: [PATCH] fix(nsgotest): wait for style transition ends --- CHANGELOG.md | 1 + base/confirm_test.go | 2 +- base/menukeys_test.go | 2 +- base/popups_test.go | 6 ++-- core/commit.go | 11 +++++++ docs/architecture/testing.md | 2 +- docs/public/en/testing.md | 2 +- docs/public/zh/testing.md | 2 +- nsgotest/nsgotest.go | 2 +- nsgotest/nsgotest_test.go | 57 ++++++++++++++++++++++++++++++++++++ ui/dialog_test.go | 7 ++++- ui/tabs_test.go | 2 +- 12 files changed, 85 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcc58d513..1398e86d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [Unreleased] +- Make `nsgotest` steps advance through pending style transitions so `OnTransitionEnd` and closing popups settle before a step returns (#335 by @houko). - Add HarmonyOS as a preview target, whose ArkTS host runs the engine in ArkWeb and draws it with ArkUI views, in `nsgo build`, `nsgo run` and `nsgo test -target harmonyos` (#79 by @togotop). - Add in-app purchases on macOS, iOS and tvOS through StoreKit 2: the store's products and prices, purchases answering cancelled and pending apart from failed with the signed transaction, updates made outside the app, finishing, current entitlements and restoring, in Go, TSX and `nsgotest`, when `nsgo.yaml` sets `apple.purchases` (#353 by @houko). - Add `desktop.SetWindowWorkspaces` and `WindowOptions.Workspaces`, which keep a window on every Space and over other apps' full screen spaces on macOS, at any level but the desktop's, which stays behind them, and on every virtual desktop on X11, with `desktop.CanSetWindowWorkspaces`, which says whether it takes effect (#572 by @houko). diff --git a/base/confirm_test.go b/base/confirm_test.go index 07aa092a0..b855e21f0 100644 --- a/base/confirm_test.go +++ b/base/confirm_test.go @@ -40,7 +40,7 @@ func TestReturnRunsTheDefaultActionOnce(t *testing.T) { AccessibilityDescription: "They are lost otherwise.", TestID: "layer", Children: content, Animation: core.ModalNone, PopupStyle: core.Style{Transition: core.Transition{Duration: 200 * time.Millisecond, Properties: core.TransitionOpacity}}, EndingStyle: core.Style{Opacity: core.Alpha(0)}} - }, nsgotest.Environment(c.env)) + }, nsgotest.Environment(c.env), nsgotest.ManualFrames()) layer := app.Find("role:alertdialog") if layer.Props.Label != "Save" || layer.Props.Description != "They are lost otherwise." { t.Fatalf("layer accessibility = %+v", layer.Props) diff --git a/base/menukeys_test.go b/base/menukeys_test.go index 6d8a64417..0495488d2 100644 --- a/base/menukeys_test.go +++ b/base/menukeys_test.go @@ -190,7 +190,7 @@ func TestAMenuGoingTakesNoKeys(t *testing.T) { base.MenuItem{Label: "Copy", OnSelect: func() { chosen = append(chosen, "Copy") }, Style: core.Style{Height: core.Pt(30), Width: core.Pt(100)}}, base.MenuItem{Label: "Paste", OnSelect: func() { chosen = append(chosen, "Paste") }, Style: core.Style{Height: core.Pt(30), Width: core.Pt(100)}}, }} - }, nsgotest.Environment(core.Environment{Idiom: core.IdiomDesktop, OS: core.OSMacOS})) + }, nsgotest.Environment(core.Environment{Idiom: core.IdiomDesktop, OS: core.OSMacOS}), nsgotest.ManualFrames()) app.Key("tab") app.Key("enter") app.Key("enter") diff --git a/base/popups_test.go b/base/popups_test.go index 01ddae22b..f49992aef 100644 --- a/base/popups_test.go +++ b/base/popups_test.go @@ -223,12 +223,12 @@ func TestPopoversOnTouchScreensTakeThePressOutside(t *testing.T) { func TestDesktopPopupsComeInAndGoThroughTheirPhases(t *testing.T) { var log []string - app := startDesktop(t, func(cx *core.Scope) core.Node { + app := nsgotest.Start(t, func(cx *core.Scope) core.Node { return base.Popover{Trigger: core.Text{Content: "Pop"}, Label: "Pop", OnOpen: func() { log = append(log, "open") }, OnClose: func() { log = append(log, "close") }, PopupClass: "transition-opacity duration-200", StartingClass: "opacity-0", EndingClass: "opacity-0", Children: []core.Node{core.Text{Content: "inside"}}} - }) + }, nsgotest.Environment(core.Environment{Idiom: core.IdiomDesktop}), nsgotest.ManualFrames()) opacity := func() float32 { return app.Find("text:inside").Parent.Style.Opacity } app.Press("label:Pop") if got := opacity(); got != 0 { @@ -624,7 +624,7 @@ func TestPopoverGivesTheFocusBackAsItCloses(t *testing.T) { Children: []core.Node{core.Input{Ref: field, TestID: "field", Style: core.Style{Width: core.Pt(80), Height: core.Pt(20)}}}}, base.Button{Label: "Other", TestID: "other", OnPress: func(core.PressEvent) {}, Style: core.Style{Height: core.Pt(20)}}, }} - }, nsgotest.Environment(core.Environment{Idiom: core.IdiomDesktop, OS: core.OSMacOS})) + }, nsgotest.Environment(core.Environment{Idiom: core.IdiomDesktop, OS: core.OSMacOS}), nsgotest.ManualFrames()) app.Key("tab") app.Key("enter") if !app.Find("id:field").Focused { diff --git a/core/commit.go b/core/commit.go index ffa0128ea..f3add7cf7 100644 --- a/core/commit.go +++ b/core/commit.go @@ -155,6 +155,17 @@ func (t *Tree) timeTransitionEnds() { t.ending = t.ending[:0] } +// PendingTransitionEnds reports whether a live view has a style transition +// whose OnTransitionEnd callback has not run yet. +func (t *Tree) PendingTransitionEnds() bool { + for _, n := range t.nodes { + if n != nil && !n.deleted && !n.owner.disposed && n.endStop != nil { + return true + } + } + return false +} + // shrinkItems drops the items' slice for a smaller one once a commit of n // items left most of it unused: the slice the first commit grew would stay // this tree's capacity for good, and a long list keep megabytes for the diff --git a/docs/architecture/testing.md b/docs/architecture/testing.md index 0091c0889..d4b2cc1c0 100644 --- a/docs/architecture/testing.md +++ b/docs/architecture/testing.md @@ -39,7 +39,7 @@ Steps (`steps.go`, `pointer.go`) act as a user does and send what a platform sen `Ref.Focus` commands follow the same platform stops in the headless model: phones focus fields, while desktop and web environments also focus enabled pressable and `Focusable` views. A plain, hidden or `NoFocus` view or a view covered by a modal leaves the current focus alone. This differs from the `Focus` step, which reaches a stop as a user does. -Each step then takes frames while an animation that ends runs or a list binds the rows it paces ([lists](scrolling.md#lists)), for at most 2 seconds of clock time. Frames the app requests itself do not hold a step, since its own loops may never end; `ManualFrames` keeps steps from taking frames at all, for tests that look at animations on the way. `Advance` moves the clock a frame at a time; `FastForward` jumps it, running each due timer once. +Each step then takes frames while an animation that ends, a style transition whose `OnTransitionEnd` is pending, or a list binds the rows it paces ([lists](scrolling.md#lists)), for at most 2 seconds of clock time. Frames the app requests itself do not hold a step, since its own loops may never end; `ManualFrames` keeps steps from taking frames at all, for tests that look at animations on the way. `Advance` moves the clock a frame at a time; `FastForward` jumps it, running each due timer once. Headless `nsgo test` adds `nsgocheck` to the effective build tags, preserving `GOFLAGS` and explicit `-- -tags` values. In such a build, each graph created with `RootOptions.CheckCommits` (every `nsgotest` app outside `Measure`, and the core's test graphs) records its creating goroutine and rejects access from another, while the graph is idle, with a `Scope.Post` diagnostic ([core](core.md)); an engine passes `engine.Options.CheckCommits` to its root. Target tests include the same diagnostics under `nsgotest`. Native `nsgodev` builds enable them; release builds contain no goroutine lookup or ownership branch; WebAssembly omits this goroutine-ID assertion in development and test builds because browser callbacks may resume on a different managed goroutine; commit-consistency checks still run. On native hosts, test graphs and their cleanup must run on the same goroutine. diff --git a/docs/public/en/testing.md b/docs/public/en/testing.md index 316dcf913..a0df9537f 100644 --- a/docs/public/en/testing.md +++ b/docs/public/en/testing.md @@ -77,7 +77,7 @@ An Android test that panics or exits before reporting its result fails with the On `web` the tests run in a headless Chrome, Chromium or Edge the tool finds, or in the browser executable `NSGO_BROWSER` names. The tool waits up to 30 seconds for it to start, stops waiting when the command is cancelled, and reports the browser's error output if it exits or never answers. -Each step, and `app.Settle()`, waits for the engine's pending commit before counting 100 ms without batches or finite animations. The whole wait stops after five seconds even if the app keeps changing; a slow first dialog needs no extra post or sleep in the test. +Each step, and `app.Settle()`, waits for the engine's pending commit before counting 100 ms without batches or finite animations. Headless steps also advance style transitions until their `OnTransitionEnd` callbacks run. The whole wait stops after five seconds even if the app keeps changing; a slow first dialog needs no extra post or sleep in the test. On Android, the tool wakes a sleeping screen and dismisses a lock screen without a credential before building and before each test. A device locked with a PIN, password or pattern must be unlocked before the run can continue; the error names the device. diff --git a/docs/public/zh/testing.md b/docs/public/zh/testing.md index 03119c0a6..42aa6b1ac 100644 --- a/docs/public/zh/testing.md +++ b/docs/public/zh/testing.md @@ -75,7 +75,7 @@ go tool nsgo test -target macos 在 `web` 上,测试运行在工具找到的无头 Chrome、Chromium 或 Edge 中,也可以用 `NSGO_BROWSER` 指定浏览器的可执行文件。工具最多等待 30 秒让它启动,命令被取消时立即停止等待;浏览器提前退出或一直不响应时,会报告它的错误输出。 -每个步骤和 `app.Settle()` 都会先等引擎完成待提交的更新,再等待连续 100 ms 没有新批次或有限动画。即使应用持续变化,整个等待也会在五秒后结束;首次构建较慢的对话框不需要测试额外投递任务或休眠。 +每个步骤和 `app.Settle()` 都会先等引擎完成待提交的更新,再等待连续 100 ms 没有新批次或有限动画。无头步骤也会推进样式过渡,直到它们的 `OnTransitionEnd` 回调执行。即使应用持续变化,整个等待也会在五秒后结束;首次构建较慢的对话框不需要测试额外投递任务或休眠。 在 Android 上,工具会在构建前和每个测试前唤醒休眠的屏幕,并关闭没有凭据的锁屏。设有 PIN、密码或图案的设备必须先解锁才能继续;错误消息会指出设备。 diff --git a/nsgotest/nsgotest.go b/nsgotest/nsgotest.go index 0bfb4cd2a..e21587abf 100644 --- a/nsgotest/nsgotest.go +++ b/nsgotest/nsgotest.go @@ -736,7 +736,7 @@ func (a *App) done() { } a.Settle() limit := a.Clock.Now().Add(settleLimit) - for !a.manualFrames && (a.anim.Ending() || a.tree.Pacing()) && a.Clock.Now().Before(limit) { + for !a.manualFrames && (a.anim.Ending() || a.tree.Pacing() || a.tree.PendingTransitionEnds()) && a.Clock.Now().Before(limit) { a.Clock.Advance(driver.FrameInterval) a.runFrame() a.Settle() diff --git a/nsgotest/nsgotest_test.go b/nsgotest/nsgotest_test.go index 9a2276d46..4b5d8aa2e 100644 --- a/nsgotest/nsgotest_test.go +++ b/nsgotest/nsgotest_test.go @@ -10,11 +10,68 @@ import ( "testing" "time" + "github.com/nsgo-dev/nsgo/base" "github.com/nsgo-dev/nsgo/core" "github.com/nsgo-dev/nsgo/desktop" "github.com/nsgo-dev/nsgo/internal/driver" ) +func TestStepsWaitForEndingStyleTransition(t *testing.T) { + var open *core.Signal[bool] + app := Start(t, func(cx *core.Scope) core.Node { + open = core.NewSignal(cx, true) + return base.Dialog{ + Open: open.Get, Animation: core.ModalNone, TestID: "dialog", + PopupStyle: core.Style{ + Width: core.Pt(100), Height: core.Pt(100), + Transition: core.Transition{Duration: 200 * time.Millisecond, Properties: core.TransitionOpacity}, + }, + EndingStyle: core.Style{Opacity: core.Alpha(0)}, + After: []core.Node{core.View{ + Role: core.RoleButton, Label: "close", TestID: "close", + OnPress: func(core.PressEvent) { open.Set(false) }, + Style: core.Style{Width: core.Pt(40), Height: core.Pt(40)}, + }}, + } + }) + app.Press("id:close") + if app.Exists("id:dialog") { + t.Fatal("Press returned while the dialog's ending transition still runs") + } +} + +func TestManualFramesKeepsEndingStyleTransitionPending(t *testing.T) { + var open *core.Signal[bool] + app := Start(t, func(cx *core.Scope) core.Node { + open = core.NewSignal(cx, true) + return base.Dialog{ + Open: open.Get, Animation: core.ModalNone, TestID: "dialog", + PopupStyle: core.Style{ + Width: core.Pt(100), + Height: core.Pt(100), + Transition: core.Transition{ + Duration: 200 * time.Millisecond, + Properties: core.TransitionOpacity, + }, + }, + EndingStyle: core.Style{Opacity: core.Alpha(0)}, + After: []core.Node{core.View{ + Role: core.RoleButton, Label: "close", TestID: "close", + OnPress: func(core.PressEvent) { open.Set(false) }, + Style: core.Style{Width: core.Pt(40), Height: core.Pt(40)}, + }}, + } + }, ManualFrames()) + app.Press("id:close") + if !app.Exists("id:dialog") { + t.Fatal("ManualFrames advanced the ending transition") + } + app.Advance(200 * time.Millisecond) + if app.Exists("id:dialog") { + t.Fatal("ending transition did not finish after Advance") + } +} + // Frames follow the manual clock: Advance runs the frame an app waits for // at each display interval, with the frame's time from that clock. func TestFramesFollowTheClock(t *testing.T) { diff --git a/ui/dialog_test.go b/ui/dialog_test.go index 8d0dc63f5..6cbe351c2 100644 --- a/ui/dialog_test.go +++ b/ui/dialog_test.go @@ -7,6 +7,7 @@ import ( "github.com/nsgo-dev/nsgo/base" "github.com/nsgo-dev/nsgo/core" + "github.com/nsgo-dev/nsgo/nsgotest" ) // TestDialog opens a dialog, centered on desktops and a sheet on phones, @@ -15,6 +16,10 @@ import ( func TestDialog(t *testing.T) { each(t, func(t *testing.T, e core.Environment) { var confirmed bool + var opts []nsgotest.Option + if e.Idiom == core.IdiomDesktop { + opts = append(opts, nsgotest.ManualFrames()) + } app, p := start(t, e, func(cx *core.Scope) core.Node { open, alert := core.NewSignal(cx, false), core.NewSignal(cx, false) return core.View{Style: core.Style{AlignItems: core.AlignStart, Gap: 8}, Children: []core.Node{ @@ -30,7 +35,7 @@ func TestDialog(t *testing.T) { Actions: []AlertAction{{Text: "Cancel", Variant: ButtonSecondary, TestID: "keep"}, {Text: "Delete", Color: ColorsOf(cx).Danger, TestID: "confirm", OnPress: func() { confirmed = true }}}}, }} - }) + }, opts...) app.Press("open") d := app.Find("dialog") if e.Idiom == core.IdiomDesktop { diff --git a/ui/tabs_test.go b/ui/tabs_test.go index a9e8af1a3..7e2c85faf 100644 --- a/ui/tabs_test.go +++ b/ui/tabs_test.go @@ -92,7 +92,7 @@ func TestTabsContent(t *testing.T) { // TestTabsSlide slides the indicator on a change of tab alone. func TestTabsSlide(t *testing.T) { headless(t) - app, _ := start(t, desktopEnv, tabsContent) + app, _ := start(t, desktopEnv, tabsContent, nsgotest.ManualFrames()) thumb := func() *nsgotest.View { return app.Find("segmented").Children[0] } if d := thumb().Style.Transition.Duration; d != 0 { t.Fatalf("the first placement slides for %v", d)