Testing goodies.

This commit is contained in:
hjozwiak committed 2026-11-24 19:29:50 -05:00
commit a3308aa9fd
43 files changed
+4008 -54

No files matched your search

+12
View File
@@ -38,6 +38,10 @@ const (
FirstChild
PreviousSibling
NextSibling
// NextItem and PreviousItem walk the window in reading order, skipping
// unnamed containers, like TalkBack's linear navigation.
NextItem
PreviousItem
)
func (direction Direction) String() string {
@@ -50,6 +54,10 @@ func (direction Direction) String() string {
return "previous-sibling"
case NextSibling:
return "next-sibling"
case NextItem:
return "next-item"
case PreviousItem:
return "previous-item"
default:
return fmt.Sprintf("direction(%d)", direction)
}
@@ -144,6 +152,10 @@ func (cursor *Cursor) Move(direction Direction) (position Position, moved bool,
next, ok, err = cursor.tree.PreviousSibling(cursor.current)
case NextSibling:
next, ok, err = cursor.tree.NextSibling(cursor.current)
case NextItem:
next, ok, err = cursor.itemLocked(true)
case PreviousItem:
next, ok, err = cursor.itemLocked(false)
default:
return Position{}, false, fmt.Errorf("%w: %d", ErrInvalidDirection, direction)
}
+128
View File
@@ -0,0 +1,128 @@
// Copyright (c) 2026 Storm Dragon
// Authors: Storm Dragon, Angus Kola
// SPDX-License-Identifier: GPL-3.0-or-later
package review
import (
"strings"
"git.stormux.org/storm/cthulhu/internal/atspi"
)
// maxItemSteps bounds one item move, so a huge or malformed tree can never
// keep the review worker busy.
const maxItemSteps = 4000
// Unnamed layout containers are not items: item navigation and touch
// exploration skip them, as TalkBack and VoiceOver do. Named ones, such as a
// named landmark or toolbar, are items.
var itemSkippedRoles = map[string]bool{
"section": true, "panel": true, "landmark": true, "filler": true, "grouping": true,
"layered pane": true, "scroll pane": true, "split pane": true, "viewport": true,
"tool bar": true, "internal frame": true, "redundant object": true, "unknown": true,
}
// ItemSkips reports whether item navigation passes over an object.
func ItemSkips(snapshot atspi.Snapshot) bool {
return itemSkippedRoles[snapshot.Role] && strings.TrimSpace(snapshot.Name) == ""
}
// topLevelLocked reports whether reference is a window: its parent is an
// application or it has no parent. Item navigation never leaves a window.
func (cursor *Cursor) topLevelLocked(reference atspi.ObjectReference) (bool, error) {
parent, ok, err := cursor.tree.Parent(reference)
if err != nil || !ok {
return true, err
}
snapshot, err := cursor.tree.Resolve(parent)
if err != nil {
return true, err
}
return snapshot.Role == "application" || snapshot.Role == "desktop frame", nil
}
// adjacentLocked returns the next or previous object in reading order, which
// is a depth-first walk of the window.
func (cursor *Cursor) adjacentLocked(reference atspi.ObjectReference, forward bool) (atspi.ObjectReference, bool, error) {
tree := cursor.tree
if forward {
if child, ok, err := tree.FirstChild(reference); err != nil || ok {
return child, ok, err
}
for node, depth := reference, 0; depth < maxItemSteps; depth++ {
top, err := cursor.topLevelLocked(node)
if err != nil || top {
return atspi.ObjectReference{}, false, err
}
if sibling, ok, err := tree.NextSibling(node); err != nil || ok {
return sibling, ok, err
}
parent, ok, err := tree.Parent(node)
if err != nil || !ok {
return atspi.ObjectReference{}, false, err
}
node = parent
}
return atspi.ObjectReference{}, false, nil
}
top, err := cursor.topLevelLocked(reference)
if err != nil || top {
return atspi.ObjectReference{}, false, err
}
sibling, ok, err := tree.PreviousSibling(reference)
if err != nil {
return atspi.ObjectReference{}, false, err
}
if !ok {
return tree.Parent(reference)
}
// The previous item is the last object inside the previous sibling.
for node, depth := sibling, 0; depth < maxItemSteps; depth++ {
child, ok, err := tree.LastChild(node)
if err != nil {
return atspi.ObjectReference{}, false, err
}
if !ok {
return node, true, nil
}
node = child
}
return sibling, true, nil
}
// itemLocked finds the next or previous item, skipping unnamed containers.
func (cursor *Cursor) itemLocked(forward bool) (atspi.ObjectReference, bool, error) {
current := cursor.current
for step := 0; step < maxItemSteps; step++ {
next, ok, err := cursor.adjacentLocked(current, forward)
if err != nil || !ok {
return atspi.ObjectReference{}, false, err
}
snapshot, err := cursor.tree.Resolve(next)
if err != nil {
return atspi.ObjectReference{}, false, err
}
if !ItemSkips(snapshot) {
return next, true, nil
}
current = next
}
return atspi.ObjectReference{}, false, nil
}
// Set moves the cursor to an object in its tree without a focus change, for
// example the object a finger explored on the touch screen.
func (cursor *Cursor) Set(id atspi.ObjectID) error {
cursor.mu.Lock()
defer cursor.mu.Unlock()
reference, err := cursor.tree.Reference(id)
if err != nil {
return err
}
if _, err := cursor.tree.Resolve(reference); err != nil {
return err
}
cursor.setCurrentLocked(reference)
return nil
}
+172
View File
@@ -0,0 +1,172 @@
// Copyright (c) 2026 Storm Dragon
// Authors: Storm Dragon, Angus Kola
// SPDX-License-Identifier: GPL-3.0-or-later
package review
import (
"testing"
"time"
"git.stormux.org/storm/cthulhu/internal/atspi"
)
func roleNode(path, name, role string, children ...string) atspi.TreeNode {
snapshot := atspi.NewSnapshot(path, name, role, "", "")
snapshot.BusName = ":1.42"
ids := make([]atspi.ObjectID, len(children))
for i, child := range children {
ids[i] = objectID(child)
}
return atspi.TreeNode{Snapshot: snapshot, Children: ids}
}
// application
//
// frame "Window"
// section (unnamed)
// link "One"
// paragraph "Two"
// link "Inner"
// panel (unnamed)
// push button "Three"
// frame "Other"
// push button "Elsewhere"
func itemTree(t *testing.T) *atspi.AccessibleTree {
t.Helper()
tree, err := atspi.NewAccessibleTree(objectID("/app"), []atspi.TreeNode{
roleNode("/app", "Browser", "application", "/window", "/other"),
roleNode("/window", "Window", "frame", "/section", "/panel"),
roleNode("/section", "", "section", "/one", "/two"),
roleNode("/one", "One", "link"),
roleNode("/two", "Two", "paragraph", "/inner"),
roleNode("/inner", "Inner", "link"),
roleNode("/panel", "", "panel", "/three"),
roleNode("/three", "Three", "push button"),
roleNode("/other", "Other", "frame", "/elsewhere"),
roleNode("/elsewhere", "Elsewhere", "push button"),
})
if err != nil {
t.Fatal(err)
}
return tree
}
func walk(t *testing.T, cursor *Cursor, direction Direction) []string {
t.Helper()
var names []string
for range 20 {
position, moved, err := cursor.Move(direction)
if err != nil {
t.Fatal(err)
}
if !moved {
return names
}
names = append(names, position.Current.Name)
}
t.Fatal("item navigation did not stop at the window edge")
return nil
}
func equal(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func TestBehaviorNextItemReadsTheWindowInOrderAndStopsAtItsEnd(t *testing.T) {
cursor, err := NewCursor(itemTree(t))
if err != nil {
t.Fatal(err)
}
if _, err := cursor.SyncFocus(objectID("/one")); err != nil {
t.Fatal(err)
}
if got, want := walk(t, cursor, NextItem), []string{"Two", "Inner", "Three"}; !equal(got, want) {
t.Fatalf("next items = %v, want %v", got, want)
}
position, _ := cursor.Current()
if position.Current.Name != "Three" {
t.Fatalf("a failed move must stay on the last item, got %q", position.Current.Name)
}
}
func TestBehaviorPreviousItemWalksBackToTheWindow(t *testing.T) {
cursor, err := NewCursor(itemTree(t))
if err != nil {
t.Fatal(err)
}
if _, err := cursor.SyncFocus(objectID("/three")); err != nil {
t.Fatal(err)
}
if got, want := walk(t, cursor, PreviousItem), []string{"Inner", "Two", "One", "Window"}; !equal(got, want) {
t.Fatalf("previous items = %v, want %v", got, want)
}
}
func TestBehaviorSetMovesReviewWithoutFocus(t *testing.T) {
cursor, err := NewCursor(itemTree(t))
if err != nil {
t.Fatal(err)
}
if _, err := cursor.SyncFocus(objectID("/one")); err != nil {
t.Fatal(err)
}
if err := cursor.Set(objectID("/inner")); err != nil {
t.Fatal(err)
}
position, moved, err := cursor.Move(NextItem)
if err != nil || !moved || position.Current.Name != "Three" {
t.Fatalf("next item after set = %q, moved %v, error %v", position.Current.Name, moved, err)
}
// The same focus again must not pull review back from the touched object.
if position, err := cursor.SyncFocus(objectID("/one")); err != nil || position.Current.Name != "Three" {
t.Fatalf("unchanged focus moved review to %q, error %v", position.Current.Name, err)
}
if err := cursor.Set(objectID("/missing")); err == nil {
t.Fatal("setting an object outside the tree must fail")
}
}
func TestBehaviorItemSkipsOnlyUnnamedContainers(t *testing.T) {
for _, tc := range []struct {
role, name string
skip bool
}{
{"section", "", true}, {"panel", " ", true}, {"landmark", "", true}, {"tool bar", "", true},
{"tool bar", "Bookmarks", false}, {"landmark", "Main", false},
{"link", "", false}, {"push button", "", false}, {"entry", "", false},
} {
if got := ItemSkips(atspi.Snapshot{Role: tc.role, Name: tc.name}); got != tc.skip {
t.Errorf("role %q name %q: skip=%v, want %v", tc.role, tc.name, got, tc.skip)
}
}
}
// A touch user may never produce a focus event. Touching an object must still
// give item navigation a starting point.
func TestBehaviorReviewObjectWorksWithoutAnyFocus(t *testing.T) {
capturer := &fakeCapturer{calls: make(chan captureCall, 1)}
worker, cancel := startWorker(t, capturer, time.Second)
defer cancel()
tree := itemTree(t)
if err := worker.ReviewObject(objectID("/one")); err != nil {
t.Fatal(err)
}
call := <-capturer.calls
call.result <- captureResult{tree: tree}
if err := worker.Move(NextItem); err != nil {
t.Fatal(err)
}
result := waitResult(t, worker)
if result.Err != nil || !result.Moved || result.Position.Current.Name != "Two" {
t.Fatalf("next item without focus = %q, moved %v, error %v", result.Position.Current.Name, result.Moved, result.Err)
}
}
+55
View File
@@ -110,3 +110,58 @@ func (worker *Worker) captureCursor(ctx context.Context, cursor *Cursor) (*Curso
}
return cursor, nil
}
// ReviewObject moves object review to an object without speaking, for example
// the object a finger explored on the touch screen, so item navigation
// continues from there. It is queued in order with navigation.
func (worker *Worker) ReviewObject(id atspi.ObjectID) error {
if !id.Valid() {
return errors.New("review object: invalid identity")
}
select {
case worker.requests <- request{reviewObject: id}:
return nil
default:
return ErrRequestQueueFull
}
}
// reviewObject places the cursor on id. A missing object recaptures the tree
// once, because the object may have appeared after the last capture. It
// works without a known focus: a touch user may never produce a focus event,
// so the touched object itself becomes the review position.
func (worker *Worker) reviewObject(ctx context.Context, cursor *Cursor, id atspi.ObjectID) (*Cursor, bool) {
cursor, err := worker.syncLatestFocus(ctx, cursor, true)
if err != nil {
return cursor, false
}
if cursor != nil && cursor.Set(id) == nil {
return cursor, true
}
recovered, err := worker.captureTree(ctx, cursor)
if err != nil {
return cursor, false
}
return recovered, recovered.Set(id) == nil
}
// captureTree captures the desktop for a cursor, synchronizing it to focus
// only when a focus is known.
func (worker *Worker) captureTree(ctx context.Context, cursor *Cursor) (*Cursor, error) {
if worker.focus().Valid() {
return worker.captureCursor(ctx, cursor)
}
captureContext, cancelCapture := context.WithTimeout(ctx, worker.captureTimeout)
tree, err := worker.capturer.CaptureDesktop(captureContext)
cancelCapture()
if err != nil {
return nil, fmt.Errorf("capture object review tree: %w", err)
}
if tree == nil {
return nil, errors.New("capture object review tree: nil tree")
}
if cursor != nil && cursor.tree == tree {
return cursor, nil
}
return NewCursor(tree)
}
+13 -1
View File
@@ -256,7 +256,7 @@ func (worker *Worker) ClearFocus() {
// Move queues a semantic navigation request without waiting for AT-SPI.
func (worker *Worker) Move(direction Direction) error {
if direction < Parent || direction > NextSibling {
if direction < Parent || direction > PreviousItem {
return fmt.Errorf("%w: %d", ErrInvalidDirection, direction)
}
select {
@@ -358,6 +358,18 @@ func (worker *Worker) run(ctx context.Context, done chan<- struct{}) {
request.interest.result <- interestReply{interest, err}
continue
}
if request.reviewObject.Valid() {
if next, ok := worker.reviewObject(ctx, cursor, request.reviewObject); ok {
cursor = next
explicitInterest = objectInterest
worker.stateMutex.Lock()
interestFocus, interestRevision = worker.latestFocus, worker.focusRevision
worker.stateMutex.Unlock()
} else if next != nil {
cursor = next
}
continue
}
if (request.whereAmI || request.currentLine || request.brailleRoute != nil || request.nativeCaret || request.readAll != nil || request.line || request.listSelection != nil || request.target != 0 || request.caretCommand != 0 || request.activate || request.fragment != "" || request.unit != 0) && request.documentEpoch != documentEpoch {
if request.brailleRoute != nil {
request.brailleRoute.done <- atspi.ErrStaleObject
+1
View File
@@ -124,6 +124,7 @@ type request struct {
listSelection *documentListSelection
brailleRoute *documentBrailleRequest
interest *interestRequest
reviewObject atspi.ObjectID
nativeCaret bool
nativeCaretFocus atspi.ObjectID
nativeCaretOffset int