// Copyright (c) 2026 Storm Dragon // Authors: Storm Dragon, Angus Kola // SPDX-License-Identifier: GPL-3.0-or-later package atspi import ( "context" "errors" "testing" ) func TestDocumentSoundGeometryWaitsForBrowserScroll(t *testing.T) { ready := SoundGeometry{ObjectKnown: true, ReferenceKnown: true, Object: Rectangle{X: 18, Y: 147, Width: 102, Height: 13}, Reference: Rectangle{Width: 800, Height: 600}} for _, linked := range []bool{false, true} { calls := 0 geometry, err := retryDocumentSoundGeometry(context.Background(), linked, func(context.Context) ([2]SoundGeometry, error) { calls++ // Scroll completion can reach the heading and link at different times. if calls == 1 { return [2]SoundGeometry{}, nil } if calls == 2 { return [2]SoundGeometry{ready, {}}, nil } return [2]SoundGeometry{ready, ready}, nil }) wantCalls := 2 if linked { wantCalls = 3 } if err != nil || calls != wantCalls || geometry[0] != ready || linked && geometry[1] != ready { t.Fatalf("linked=%t: calls=%d geometry=%+v err=%v", linked, calls, geometry, err) } } } func TestDocumentSoundGeometryRetryBoundsAndCancellation(t *testing.T) { for _, scenario := range []string{"ready", "unavailable", "stale", "cancelled", "already cancelled"} { t.Run(scenario, func(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() if scenario == "already cancelled" { cancel() } calls := 0 _, err := retryDocumentSoundGeometry(ctx, false, func(context.Context) ([2]SoundGeometry, error) { calls++ switch scenario { case "ready": return [2]SoundGeometry{{ObjectKnown: true, ReferenceKnown: true}, {}}, nil case "stale": return [2]SoundGeometry{}, ErrStaleObject case "cancelled": cancel() } return [2]SoundGeometry{}, nil }) wantCalls, wantErr := 1, error(nil) switch scenario { case "unavailable": wantCalls = 6 case "stale": wantErr = ErrStaleObject case "cancelled": wantErr = context.Canceled case "already cancelled": wantCalls, wantErr = 0, context.Canceled } if calls != wantCalls || !errors.Is(err, wantErr) { t.Fatalf("calls=%d err=%v, want calls=%d err=%v", calls, err, wantCalls, wantErr) } }) } }