175 lines
4.7 KiB
Go
175 lines
4.7 KiB
Go
package appwindow
|
|
|
|
import (
|
|
"context"
|
|
"image"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/guesttools"
|
|
)
|
|
|
|
type captureConsole struct{ Console }
|
|
|
|
type pasteConsole struct {
|
|
Console
|
|
sent chan string
|
|
}
|
|
|
|
func (c pasteConsole) SendKey(_ context.Context, _, key string, down bool) error {
|
|
if down {
|
|
c.sent <- key
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c pasteConsole) ReleaseInputs(context.Context, string, []string, []string) error {
|
|
c.sent <- "released"
|
|
return nil
|
|
}
|
|
|
|
func TestPasteWaitsForClipboardAndCancelsOnBlur(t *testing.T) {
|
|
for _, name := range []string{"applied", "blur"} {
|
|
t.Run(name, func(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
window := guesttools.ActiveWindow{ID: 1}
|
|
ready, entered := make(chan struct{}), make(chan struct{})
|
|
console := pasteConsole{sent: make(chan string, 8)}
|
|
s := &session{
|
|
SessionOptions: SessionOptions{
|
|
Console: console,
|
|
Window: func(time.Duration) (guesttools.ActiveWindow, bool) { return window, true },
|
|
PrepareClipboard: func(ctx context.Context) (func(), error) {
|
|
close(entered)
|
|
select {
|
|
case <-ready:
|
|
return func() {}, nil
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
}
|
|
},
|
|
},
|
|
id: 1, tag: 1, mapping: frameMapping{window: window}, lastFrame: time.Now(),
|
|
}
|
|
queue := &inputQueue{ready: make(chan struct{}, 1)}
|
|
done := make(chan error, 1)
|
|
go func() { done <- s.forwardInput(ctx, queue) }()
|
|
defer func() {
|
|
cancel()
|
|
<-done
|
|
}()
|
|
|
|
queue.push(ViewerEvent{Kind: ViewerFocus, Down: true})
|
|
queue.push(ViewerEvent{Kind: ViewerKey, FrameTag: 1, VirtualKey: 0x11, Down: true})
|
|
queue.push(ViewerEvent{Kind: ViewerKey, FrameTag: 1, VirtualKey: 'V', Down: true})
|
|
select {
|
|
case <-entered:
|
|
case <-ctx.Done():
|
|
t.Fatal("clipboard wait did not start")
|
|
}
|
|
if key := <-console.sent; key != "meta_l" {
|
|
t.Fatalf("first key = %q", key)
|
|
}
|
|
select {
|
|
case key := <-console.sent:
|
|
t.Fatalf("forwarded %q before clipboard changed", key)
|
|
default:
|
|
}
|
|
want := "v"
|
|
if name == "blur" {
|
|
queue.push(ViewerEvent{Kind: ViewerFocus})
|
|
want = "released"
|
|
} else {
|
|
close(ready)
|
|
}
|
|
select {
|
|
case key := <-console.sent:
|
|
if key != want {
|
|
t.Fatalf("event = %q, want %q", key, want)
|
|
}
|
|
case <-ctx.Done():
|
|
t.Fatalf("did not receive %q", want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInputQueueKeepsClickBetweenMoves(t *testing.T) {
|
|
q := &inputQueue{ready: make(chan struct{}, 1)}
|
|
for _, event := range []ViewerEvent{
|
|
{Kind: ViewerMouseMove, X: 1},
|
|
{Kind: ViewerMouseMove, X: 2},
|
|
{Kind: ViewerMouseButton, X: 2, Down: true},
|
|
{Kind: ViewerMouseMove, X: 3},
|
|
{Kind: ViewerMouseMove, X: 4},
|
|
} {
|
|
if !q.push(event) {
|
|
t.Fatal("queue unexpectedly full")
|
|
}
|
|
}
|
|
for _, want := range []ViewerEvent{
|
|
{Kind: ViewerMouseMove, X: 2},
|
|
{Kind: ViewerMouseButton, X: 2, Down: true},
|
|
{Kind: ViewerMouseMove, X: 4},
|
|
} {
|
|
select {
|
|
case <-q.ready:
|
|
default:
|
|
t.Fatal("queue did not notify its reader")
|
|
}
|
|
if got := q.pop().ViewerEvent; got != want {
|
|
t.Fatalf("event = %+v, want %+v", got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (captureConsole) ScreendumpDisplay(_ context.Context, _ string, path, device string, head int) (string, error) {
|
|
if device != "gfx" || head != 0 {
|
|
return "", ErrNoWindow
|
|
}
|
|
ppm := append([]byte("P6\n8 6\n255\n"), make([]byte, 8*6*3)...)
|
|
return "ppm", os.WriteFile(path, ppm, 0o600)
|
|
}
|
|
|
|
func TestCaptureTracksDisplayedGeometry(t *testing.T) {
|
|
current := guesttools.ActiveWindow{
|
|
ID: 1, AppName: "TextEdit", X: -1, Y: 1, Width: 4, Height: 2, DisplayWidth: 4, DisplayHeight: 3,
|
|
}
|
|
s := &session{
|
|
SessionOptions: SessionOptions{
|
|
Window: func(time.Duration) (guesttools.ActiveWindow, bool) { return current, true },
|
|
Console: captureConsole{},
|
|
},
|
|
path: filepath.Join(t.TempDir(), "test.ppm"), id: 1,
|
|
}
|
|
frame, err := s.capture(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if frame.Image.Bounds() != image.Rect(0, 0, 6, 4) {
|
|
t.Fatalf("crop bounds = %v", frame.Image.Bounds())
|
|
}
|
|
event := ViewerEvent{Kind: ViewerMouseButton, FrameTag: frame.Tag}
|
|
if mapping, ok := s.eventMapping(event); !ok || mapping.crop.Min != image.Pt(0, 2) {
|
|
t.Fatalf("event mapping = %#v, %v", mapping, ok)
|
|
}
|
|
current.X = 1
|
|
if _, ok := s.eventMapping(event); ok {
|
|
t.Fatal("accepted input against a stale window position")
|
|
}
|
|
next, err := s.capture(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if next.Tag == frame.Tag {
|
|
t.Fatal("changed window position retained the previous frame tag")
|
|
}
|
|
current.ID++
|
|
if _, err := s.capture(context.Background()); err != ErrNoWindow {
|
|
t.Fatalf("capture after guest changed windows = %v", err)
|
|
}
|
|
}
|