69 lines
2.2 KiB
Go
69 lines
2.2 KiB
Go
// Copyright (c) 2026 Storm Dragon
|
|
// Authors: Storm Dragon, Angus Kola
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package review
|
|
|
|
import (
|
|
"context"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.stormux.org/storm/cthulhu/internal/atspi"
|
|
)
|
|
|
|
type whereAmICapturer struct {
|
|
*readAllCapturer
|
|
content atspi.DocumentContent
|
|
reads int
|
|
}
|
|
|
|
func (capturer *whereAmICapturer) ReadFocusedSnapshot(context.Context, atspi.ObjectID) (atspi.DocumentContent, error) {
|
|
capturer.reads++
|
|
return capturer.content, nil
|
|
}
|
|
|
|
func TestWhereAmICapturesFreshSnapshotAndAncestry(t *testing.T) {
|
|
source := atspi.Snapshot{BusName: webID("/entry").BusName, Path: "/entry", Role: "text", Name: "Current item"}
|
|
capturer := &whereAmICapturer{content: atspi.DocumentContent{Snapshot: source,
|
|
SourceContext: []atspi.Snapshot{source, {Role: "frame"}, {Role: "application"}}}}
|
|
worker, cancel := startWorker(t, capturer, time.Second)
|
|
defer cancel()
|
|
worker.SyncFocus(source.ID())
|
|
for revision := uint64(1); revision <= 2; revision++ {
|
|
if err := worker.ReadWhereAmI(revision); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result := waitResult(t, worker)
|
|
if !result.WhereAmI || result.WhereAmIRevision != revision || result.DocumentFocus != source.ID() || result.Err != nil ||
|
|
!reflect.DeepEqual(result.DocumentPosition.Snapshot, source) || !reflect.DeepEqual(result.DocumentCaretContext, capturer.content.SourceContext) {
|
|
t.Fatal("Where Am I lost its snapshot, source lifetime or ancestry", result)
|
|
}
|
|
}
|
|
if capturer.reads != 2 {
|
|
t.Fatal("Where Am I reused a cached capture")
|
|
}
|
|
}
|
|
|
|
func TestWhereAmIRejectsChangedFocusBeforeCapture(t *testing.T) {
|
|
capturer := &whereAmICapturer{}
|
|
worker, err := NewWorker(capturer, time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := worker.ReadWhereAmI(1); err == nil {
|
|
t.Fatal("missing focus queued a native request")
|
|
}
|
|
worker.SyncFocus(webID("/new"))
|
|
result := worker.read_where_am_i(t.Context(), request{readFocus: webID("/old")})
|
|
if result.Err == nil || capturer.reads != 0 {
|
|
t.Fatal("retired focus started a native capture")
|
|
}
|
|
unsupported, _ := NewWorker(&readAllCapturer{}, time.Second)
|
|
unsupported.SyncFocus(webID("/entry"))
|
|
if err := unsupported.ReadWhereAmI(1); err == nil {
|
|
t.Fatal("provider without snapshot support queued a native request")
|
|
}
|
|
}
|