test(core): drain capture commands before the layer change
1 file · +12 -6
M internal/core/capture_test.go +12 -6
| @@ -68,6 +68,16 @@ func (r *captureRig) layer(id ports.WindowID, layer ports.Layer, keyboard uint32 | |||
| 68 | 68 | return ports.LayerSurface{ID: id, Layer: layer, Anchor: ports.AnchorTop | ports.AnchorLeft, Width: 10, Height: 10, Output: "OUT-1", Keyboard: keyboard} | |
| 69 | 69 | } | |
| 70 | 70 | ||
| 71 | + | // drainCommands drops the commands already queued. Call it only once the | |
| 72 | + | // core is quiet (after receiving a scene, which is published after its | |
| 73 | + | // commands) and before sending the event under test: draining after the | |
| 74 | + | // send races with the core emitting the commands the test waits for. | |
| 75 | + | func (r *captureRig) drainCommands() { | |
| 76 | + | for len(r.commands) > 0 { | |
| 77 | + | <-r.commands | |
| 78 | + | } | |
| 79 | + | } | |
| 80 | + | ||
| 71 | 81 | // excludedScene waits for a scene whose exclusion list has n entries. | |
| 72 | 82 | func (r *captureRig) excludedScene(t *testing.T, n int) ports.Scene { | |
| 73 | 83 | t.Helper() | |
| @@ -372,10 +382,8 @@ func TestCaptureExclusionKeyboardClamp(t *testing.T) { | |||
| 372 | 382 | r.client <- ports.CaptureExclusionBegin{Session: 1} | |
| 373 | 383 | r.client <- ports.CaptureExclusionLayer{Session: 1, Layer: 10, Attached: true} | |
| 374 | 384 | r.excludedScene(t, 1) | |
| 385 | + | r.drainCommands() | |
| 375 | 386 | r.client <- ports.LayerChanged{Layers: []ports.LayerSurface{r.layer(10, ports.LayerOverlay, 1), r.layer(11, ports.LayerOverlay, 1)}} | |
| 376 | - | for len(r.commands) > 0 { | |
| 377 | - | <-r.commands | |
| 378 | - | } | |
| 379 | 387 | scene(t, r.scenes) | |
| 380 | 388 | for { | |
| 381 | 389 | if f, ok := receive(t, r.commands).(ports.FocusWindow); ok { | |
| @@ -396,9 +404,7 @@ func TestCaptureExclusionKeyboardNone(t *testing.T) { | |||
| 396 | 404 | r.client <- ports.CaptureExclusionBegin{Session: 1} | |
| 397 | 405 | r.client <- ports.CaptureExclusionLayer{Session: 1, Layer: 10, Attached: true} | |
| 398 | 406 | r.excludedScene(t, 1) | |
| 399 | - | for len(r.commands) > 0 { | |
| 400 | - | <-r.commands | |
| 401 | - | } | |
| 407 | + | r.drainCommands() | |
| 402 | 408 | r.client <- ports.WindowMapped{ID: 1} | |
| 403 | 409 | scene(t, r.scenes) | |
| 404 | 410 | for len(r.commands) > 0 { | |
| @@ -68,6 +68,16 @@ func (r *captureRig) layer(id ports.WindowID, layer ports.Layer, keyboard uint32 | |||||
| 68 | return ports.LayerSurface{ID: id, Layer: layer, Anchor: ports.AnchorTop | ports.AnchorLeft, Width: 10, Height: 10, Output: "OUT-1", Keyboard: keyboard} | 68 | return ports.LayerSurface{ID: id, Layer: layer, Anchor: ports.AnchorTop | ports.AnchorLeft, Width: 10, Height: 10, Output: "OUT-1", Keyboard: keyboard} | ||
| 69 | } | 69 | } | ||
| 70 | 70 | ||||
| 71 | + | // drainCommands drops the commands already queued. Call it only once the | |||
| 72 | + | // core is quiet (after receiving a scene, which is published after its | |||
| 73 | + | // commands) and before sending the event under test: draining after the | |||
| 74 | + | // send races with the core emitting the commands the test waits for. | |||
| 75 | + | func (r *captureRig) drainCommands() { | |||
| 76 | + | for len(r.commands) > 0 { | |||
| 77 | + | <-r.commands | |||
| 78 | + | } | |||
| 79 | + | } | |||
| 80 | + | ||||
| 71 | // excludedScene waits for a scene whose exclusion list has n entries. | 81 | // excludedScene waits for a scene whose exclusion list has n entries. | ||
| 72 | func (r *captureRig) excludedScene(t *testing.T, n int) ports.Scene { | 82 | func (r *captureRig) excludedScene(t *testing.T, n int) ports.Scene { | ||
| 73 | t.Helper() | 83 | t.Helper() | ||
| @@ -372,10 +382,8 @@ func TestCaptureExclusionKeyboardClamp(t *testing.T) { | |||||
| 372 | r.client <- ports.CaptureExclusionBegin{Session: 1} | 382 | r.client <- ports.CaptureExclusionBegin{Session: 1} | ||
| 373 | r.client <- ports.CaptureExclusionLayer{Session: 1, Layer: 10, Attached: true} | 383 | r.client <- ports.CaptureExclusionLayer{Session: 1, Layer: 10, Attached: true} | ||
| 374 | r.excludedScene(t, 1) | 384 | r.excludedScene(t, 1) | ||
| 385 | + | r.drainCommands() | |||
| 375 | r.client <- ports.LayerChanged{Layers: []ports.LayerSurface{r.layer(10, ports.LayerOverlay, 1), r.layer(11, ports.LayerOverlay, 1)}} | 386 | r.client <- ports.LayerChanged{Layers: []ports.LayerSurface{r.layer(10, ports.LayerOverlay, 1), r.layer(11, ports.LayerOverlay, 1)}} | ||
| 376 | - | for len(r.commands) > 0 { | |||
| 377 | - | <-r.commands | |||
| 378 | - | } | |||
| 379 | scene(t, r.scenes) | 387 | scene(t, r.scenes) | ||
| 380 | for { | 388 | for { | ||
| 381 | if f, ok := receive(t, r.commands).(ports.FocusWindow); ok { | 389 | if f, ok := receive(t, r.commands).(ports.FocusWindow); ok { | ||
| @@ -396,9 +404,7 @@ func TestCaptureExclusionKeyboardNone(t *testing.T) { | |||||
| 396 | r.client <- ports.CaptureExclusionBegin{Session: 1} | 404 | r.client <- ports.CaptureExclusionBegin{Session: 1} | ||
| 397 | r.client <- ports.CaptureExclusionLayer{Session: 1, Layer: 10, Attached: true} | 405 | r.client <- ports.CaptureExclusionLayer{Session: 1, Layer: 10, Attached: true} | ||
| 398 | r.excludedScene(t, 1) | 406 | r.excludedScene(t, 1) | ||
| 399 | - | for len(r.commands) > 0 { | 407 | + | r.drainCommands() |
| 400 | - | <-r.commands | |||
| 401 | - | } | |||
| 402 | r.client <- ports.WindowMapped{ID: 1} | 408 | r.client <- ports.WindowMapped{ID: 1} | ||
| 403 | scene(t, r.scenes) | 409 | scene(t, r.scenes) | ||
| 404 | for len(r.commands) > 0 { | 410 | for len(r.commands) > 0 { | ||