Files
magpie/internal/gui/dock_test.go
T
yetone 751ebe8f5a gui: magpie leads Command-Tab's order after opening its window on a busy Mac too, not only when the main thread keeps up (#1651)
TestDockJoinLeadsCommandTab failed under a full local go test ./...: the
test app was LaunchServices' front app, yet last in Command-Tab's order
(lsappinfo visibleProcessList), kitty first.

Cause: setDock decided whether to activate magpie again by way of the
Dock (#1381's fix) from [NSApp isActive]. The Mac makes an activated app
the front app at once, but NSApp says it is active only once its main
thread has taken the activation in. On a loaded machine the join came
first, isActive read NO, dockStep chose a plain activate (a no-op for
the front app), and magpie stayed last. A temporary print in setDock
showed active=false in every failing run (3 of 5 at load ~120) and true
in the passing ones. The same can happen to a user when the panel's
focus and Open come close together on a busy Mac.

Fix: joining the Dock to the front always goes by way of the Dock
(activateAgain), whatever NSApp says. When magpie truly isn't active
(the tray's Open), the Dock's turn in front changes nothing: magpie is
activated after it either way. dockStep loses its active argument and
appActive is gone. docs/subsystems/gui-shell.md item 7 says so.

The test now activates magpie, waits for LaunchServices to call it the
front app, and joins, all in one turn of the main thread, so the join
always comes before NSApp knows: the busy-machine order, made certain.

Run:
- New test on the old dockStep: 5 of 5 red (magpie not first, kitty first).
  With the fix: 25 of 25 green at load ~120-135.
- The old test (no injected order) with the fix: 10 of 10 green at load
  ~180; before the fix 3 of 5 red at load ~120.
- TestDockStep (table without active). go test ./internal/gui (GUI
  build, darwin) 5 runs green. One run before them failed and its
  output wasn't kept, so which test failed is unknown. After rebasing
  onto 18e82adc6, one package run failed TestTrayCellClickReleasedPanel
  (its own AppKit child, no setDock on its path); it passed alone and in
  3 package runs after. Filed as #1661, cause not found.
  go test -tags nogui ./internal/gui green. gofmt -l clean; go vet
  ./internal/gui (darwin), GOOS=linux and GOOS=windows go vet -tags nogui
  ./internal/gui clean.

Not tried: a real magpie build with osascript Command-Tabs (as 15872d977
did) after this change; the full-screen join (dockOnFullscreen), which
still reads isActive at NSWindowWillEnterFullScreen, long after any
activation, and is unchanged.
2026-10-12 00:57:27 +08:00

32 lines
1.2 KiB
Go

package gui
import "testing"
// Joining the Dock to the front is activated by way of the Dock, so magpie
// leads Command-Tab's order (#1381), whether NSApp says it is active yet or
// not (#1651); anything else is as before.
func TestDockStep(t *testing.T) {
cases := []struct {
regular, on, front bool
want dockMove
}{
// already where it should be: nothing
{true, true, true, dockMove{}},
{false, false, true, dockMove{}},
// the window opens (the panel, the tray's Open, a second magpie):
// joins, activated by way of the Dock
{false, true, true, dockMove{policy: true, again: true, front: true}},
// joins for another reason, not to the front: not activated
{false, true, false, dockMove{policy: true}},
// the window closed: leaves, stays where it is
{true, false, false, dockMove{policy: true}},
// Settings takes it out with the window open: brought back to the front
{true, false, true, dockMove{policy: true, front: true}},
}
for _, c := range cases {
if got := dockStep(c.regular, c.on, c.front); got != c.want {
t.Errorf("dockStep(regular=%v, on=%v, front=%v) = %+v, want %+v", c.regular, c.on, c.front, got, c.want)
}
}
}