A few touch ups.

This commit is contained in:
Storm Dragon committed 2026-12-01 03:18:50 -05:00
1 parent 210e949f76
commit 105d6de46e
6 files changed
+209 -20

No files matched your search

+16 -4
View File
@@ -205,13 +205,22 @@ const maxDeadFocusDocuments = 2
// captureRetainedDocumentForDeadFocus handles a focus that died before any
// navigation adopted it, such as Steam's loading spinner after a popup
// closes. captureAnchor cannot help because the dead focus was never a saved
// place. Try the most recently used retained documents of the same provider;
// the first one that still exists becomes the anchor. A live focus never gets
// here: capture keyed on it succeeds or fails with another error.
// place. Try the current document first. Only a confirmed missing document
// permits trying another retained document of the same provider: a timeout or
// failed refresh is not evidence that focus belongs elsewhere. A live focus
// never gets here: capture keyed on it succeeds or fails with another error.
func (worker *Worker) captureRetainedDocumentForDeadFocus(ctx context.Context, current *DocumentCursor, focus atspi.ObjectID, loadEpoch uint64) (*atspi.AccessibleTree, *DocumentCursor, time.Duration, bool) {
if current == nil || worker.documentCursorEpochs[current.tree] != loadEpoch {
return nil, nil, 0, false
}
document, ok := current.savedDocument()
if !ok || document.BusName != focus.BusName || document == focus {
return nil, nil, 0, false
}
candidates := append([]*DocumentCursor{current}, worker.retainedCursorsExcept(current, loadEpoch)...)
var total time.Duration
tried := 0
for _, retained := range worker.retainedCursorsExcept(current, loadEpoch) {
for _, retained := range candidates {
document, ok := retained.savedDocument()
if !ok || document.BusName != focus.BusName || document == focus {
continue
@@ -228,6 +237,9 @@ func (worker *Worker) captureRetainedDocumentForDeadFocus(ctx context.Context, c
if err == nil && tree != nil {
return tree, retained, total, true
}
if !errors.Is(err, atspi.ErrObjectNotFound) && !errors.Is(err, atspi.ErrDeadObject) {
return nil, nil, total, false
}
}
return nil, nil, total, false
}
+121 -1
View File
@@ -1476,6 +1476,123 @@ func TestDeadFocusNeverSavedRecoversThroughRetainedDocument(t *testing.T) {
}
}
// A transient focus can die in the current document without ever becoming a
// saved place. An older document on the same bus must not take over the move.
func TestDeadFocusNeverSavedKeepsCurrentDocument(t *testing.T) {
for _, retainOther := range []bool{false, true} {
t.Run(fmt.Sprint(retainOther), func(t *testing.T) {
ctx := context.Background()
tree := retainedDocumentTree(t, "/doc", "/first", "/second", "/third")
cursor, err := NewDocumentCursor(tree)
if err != nil {
t.Fatal(err)
}
if _, err := cursor.SyncFocus(ctx, webID("/second")); err != nil {
t.Fatal(err)
}
capturer := &retainedDocumentCapturer{trees: map[atspi.ObjectID]*atspi.AccessibleTree{webID("/doc"): tree}}
worker, err := NewWorker(capturer, time.Second)
if err != nil {
t.Fatal(err)
}
if retainOther {
other := retainedDocumentTree(t, "/other", "/old1", "/old2")
otherCursor, err := NewDocumentCursor(other)
if err != nil {
t.Fatal(err)
}
if _, err := otherCursor.SyncFocus(ctx, webID("/old1")); err != nil {
t.Fatal(err)
}
capturer.trees[webID("/other")] = other
worker.rememberDocumentCursor(otherCursor, 0)
}
worker.rememberDocumentCursor(cursor, 0)
worker.SyncFocus(webID("/spinner"))
result, recovered := worker.moveDocument(ctx, cursor, DocumentHeading, 0, false, 0, 0)
if result.Err != nil || !result.Moved || recovered != cursor || result.DocumentPosition.Object.ID() != webID("/third") || worker.focus() != webID("/doc") {
t.Fatalf("dead focus recovery: object=%v focus=%v error=%v captures=%v", result.DocumentPosition.Object.ID(), worker.focus(), result.Err, capturer.captures)
}
if len(capturer.captures) != 2 || capturer.captures[1] != webID("/doc") {
t.Fatalf("captures = %v, want dead focus then current document", capturer.captures)
}
})
}
}
type retainedCaretCapturer struct {
*retainedDocumentCapturer
caretTextReader
}
func TestDeadFocusNeverSavedKeepsCurrentCaretOffset(t *testing.T) {
ctx := context.Background()
reader := caretTextReader{webID("/text"): "Alpha", webID("/next"): "Next"}
cursor := newCaretTextCursor(t, reader)
if move, err := cursor.MoveCaret(ctx, reader, CaretNextCharacter); err != nil || move.Position.Offset != 1 {
t.Fatalf("initial caret move = %+v, %v", move, err)
}
capturer := &retainedCaretCapturer{
retainedDocumentCapturer: &retainedDocumentCapturer{trees: map[atspi.ObjectID]*atspi.AccessibleTree{webID("/doc"): cursor.tree}},
caretTextReader: reader,
}
worker, err := NewWorker(capturer, time.Second)
if err != nil {
t.Fatal(err)
}
worker.rememberDocumentCursor(cursor, 0)
worker.SyncFocus(webID("/spinner"))
result, recovered := worker.moveDocumentCaret(ctx, cursor, CaretNextCharacter, 0, 0)
if result.Err != nil || !result.Moved || recovered != cursor || result.DocumentPosition.Object.ID() != webID("/text") || result.DocumentPosition.Offset != 2 {
t.Fatalf("caret after dead focus: object=%v offset=%d error=%v", result.DocumentPosition.Object.ID(), result.DocumentPosition.Offset, result.Err)
}
}
type failedDocumentCapturer struct {
*retainedDocumentCapturer
failure error
}
func (capturer *failedDocumentCapturer) CaptureDocument(ctx context.Context, focus atspi.ObjectID) (*atspi.AccessibleTree, error) {
if focus == webID("/doc") {
capturer.captures = append(capturer.captures, focus)
return nil, capturer.failure
}
return capturer.retainedDocumentCapturer.CaptureDocument(ctx, focus)
}
func TestDeadFocusRecoveryDoesNotLeaveFailedCurrentDocument(t *testing.T) {
for _, failure := range []error{context.DeadlineExceeded, atspi.ErrStaleObject, atspi.ErrIncompleteTree, nil} {
t.Run(fmt.Sprint(failure), func(t *testing.T) {
ctx := context.Background()
tree := retainedDocumentTree(t, "/doc", "/first")
other := retainedDocumentTree(t, "/other", "/old")
cursor, _ := NewDocumentCursor(tree)
otherCursor, _ := NewDocumentCursor(other)
if _, err := cursor.SyncFocus(ctx, webID("/first")); err != nil {
t.Fatal(err)
}
if _, err := otherCursor.SyncFocus(ctx, webID("/old")); err != nil {
t.Fatal(err)
}
capturer := &failedDocumentCapturer{retainedDocumentCapturer: &retainedDocumentCapturer{trees: map[atspi.ObjectID]*atspi.AccessibleTree{webID("/other"): other}}, failure: failure}
worker, err := NewWorker(capturer, time.Second)
if err != nil {
t.Fatal(err)
}
worker.rememberDocumentCursor(otherCursor, 0)
worker.rememberDocumentCursor(cursor, 0)
worker.SyncFocus(webID("/spinner"))
if _, _, _, ok := worker.captureRetainedDocumentForDeadFocus(ctx, cursor, webID("/spinner"), 0); ok {
t.Fatal("failed current document adopted another document")
}
if len(capturer.captures) != 1 || capturer.captures[0] != webID("/doc") || worker.focus() != webID("/spinner") {
t.Fatalf("recovery after failed current document: captures=%v focus=%v", capturer.captures, worker.focus())
}
})
}
}
// A dead focus on another provider must not be rebound into an unrelated
// application's retained document.
func TestDeadFocusRecoveryStaysOnProviderBus(t *testing.T) {
@@ -1491,9 +1608,12 @@ func TestDeadFocusRecoveryStaysOnProviderBus(t *testing.T) {
}
worker.rememberDocumentCursor(cursorB, 0)
other := atspi.NewObjectID(":9.9", "/spinner")
if _, _, _, ok := worker.captureRetainedDocumentForDeadFocus(context.Background(), nil, other, 0); ok {
if _, _, _, ok := worker.captureRetainedDocumentForDeadFocus(context.Background(), cursorB, other, 0); ok {
t.Fatal("dead focus adopted another provider's document")
}
if len(capturer.captures) != 0 {
t.Fatalf("cross-provider recovery captured documents: %v", capturer.captures)
}
}
type deadStartNavigator struct {