//go:build darwin
|
|
package axinput
|
|
import "testing"
|
|
func TestPrepareClipboardFallbackFailsWhenPasteboardWriteFails(t *testing.T) {
|
withClipboardHooks(t,
|
func() clipboardSnapshot { return clipboardSnapshot{text: "original", hasText: true} },
|
func(text string) bool { return false },
|
func() bool { return true },
|
)
|
|
driver := &darwinDriver{}
|
result := driver.PrepareClipboardFallback("recognized text")
|
if result.OK || result.Prepared || result.Token != "" {
|
t.Fatalf("result = %+v, want failed prepare without token", result)
|
}
|
}
|
|
func TestPrepareClipboardFallbackCreatesTokenAfterVerifiedWrite(t *testing.T) {
|
var wrote string
|
withClipboardHooks(t,
|
func() clipboardSnapshot { return clipboardSnapshot{text: "original", hasText: true} },
|
func(text string) bool {
|
wrote = text
|
return true
|
},
|
func() bool { return true },
|
)
|
|
driver := &darwinDriver{}
|
result := driver.PrepareClipboardFallback("recognized text")
|
if !result.OK || !result.Prepared || result.Token == "" {
|
t.Fatalf("result = %+v, want prepared token", result)
|
}
|
if wrote != "recognized text" {
|
t.Fatalf("wrote = %q", wrote)
|
}
|
}
|
|
func TestCopyTextToClipboardWritesWithoutRestoreToken(t *testing.T) {
|
var wrote string
|
withClipboardHooks(t,
|
func() clipboardSnapshot { return clipboardSnapshot{text: "original", hasText: true} },
|
func(text string) bool {
|
wrote = text
|
return true
|
},
|
func() bool { return true },
|
)
|
|
driver := &darwinDriver{}
|
result := driver.CopyTextToClipboard("recognized text")
|
if !result.OK || !result.Prepared || result.Token != "" {
|
t.Fatalf("result = %+v, want clipboard write without restore token", result)
|
}
|
if wrote != "recognized text" {
|
t.Fatalf("wrote = %q", wrote)
|
}
|
}
|
|
func TestRestoreClipboardFallbackReportsRestoreWriteFailure(t *testing.T) {
|
writes := 0
|
current := clipboardSnapshot{text: "recognized text", hasText: true}
|
withClipboardHooks(t,
|
func() clipboardSnapshot { return current },
|
func(text string) bool {
|
writes++
|
return writes == 1
|
},
|
func() bool { return true },
|
)
|
|
driver := &darwinDriver{}
|
prepared := driver.PrepareClipboardFallback("recognized text")
|
if !prepared.OK || prepared.Token == "" {
|
t.Fatalf("prepare = %+v", prepared)
|
}
|
restored := driver.RestoreClipboardFallback(prepared.Token)
|
if restored.OK || restored.Restored {
|
t.Fatalf("restore = %+v, want failure when original clipboard cannot be written", restored)
|
}
|
}
|
|
func withClipboardHooks(t *testing.T, read func() clipboardSnapshot, write func(string) bool, clear func() bool) {
|
t.Helper()
|
origRead := readClipboardSnapshot
|
origWrite := writeClipboardText
|
origClear := clearClipboardText
|
readClipboardSnapshot = read
|
writeClipboardText = write
|
clearClipboardText = clear
|
t.Cleanup(func() {
|
readClipboardSnapshot = origRead
|
writeClipboardText = origWrite
|
clearClipboardText = origClear
|
})
|
}
|