Skip to content

Commit

Permalink
implementation Test() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hrntknr committed Jan 22, 2022
1 parent 10ba29a commit e25371e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 6 deletions.
4 changes: 4 additions & 0 deletions clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ const (
// guarantee one read at a time.
var lock = sync.Mutex{}

func Test() bool {
return test()
}

// Read returns a chunk of bytes of the clipboard data if it presents
// in the desired format t presents. Otherwise, it returns nil.
func Read(t Format) []byte {
Expand Down
4 changes: 4 additions & 0 deletions clipboard_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import (
"golang.org/x/mobile/app"
)

func test() bool {
return true
}

func read(t Format) (buf []byte, err error) {
switch t {
case FmtText:
Expand Down
4 changes: 4 additions & 0 deletions clipboard_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import (
"unsafe"
)

func test() bool {
return true
}

func read(t Format) (buf []byte, err error) {
var (
data unsafe.Pointer
Expand Down
4 changes: 4 additions & 0 deletions clipboard_ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (
"unsafe"
)

func test() bool {
return true
}

func read(t Format) (buf []byte, err error) {
switch t {
case FmtText:
Expand Down
25 changes: 20 additions & 5 deletions clipboard_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,18 @@ and initialize a virtual frame buffer:
Then this package should be ready to use.
`

func init() {
func test() bool {
ok := C.clipboard_test()
if ok < 0 {
panic(errmsg)
}
return ok == 0
}

var canAccessClipbord = false

func read(t Format) (buf []byte, err error) {
if !canAccessClipbord && !test() {
panic(errmsg)
}
canAccessClipbord = true
switch t {
case FmtText:
return readc("UTF8_STRING")
Expand All @@ -77,6 +81,10 @@ func read(t Format) (buf []byte, err error) {
}

func readc(t string) ([]byte, error) {
if !canAccessClipbord && !test() {
panic(errmsg)
}
canAccessClipbord = true
ct := C.CString(t)
defer C.free(unsafe.Pointer(ct))

Expand All @@ -97,7 +105,10 @@ func readc(t string) ([]byte, error) {
// write writes the given data to clipboard and
// returns true if success or false if failed.
func write(t Format, buf []byte) (<-chan struct{}, error) {

if !canAccessClipbord && !test() {
panic(errmsg)
}
canAccessClipbord = true
var s string
switch t {
case FmtText:
Expand Down Expand Up @@ -139,6 +150,10 @@ func write(t Format, buf []byte) (<-chan struct{}, error) {
}

func watch(ctx context.Context, t Format) <-chan []byte {
if !canAccessClipbord && !test() {
panic(errmsg)
}
canAccessClipbord = true
recv := make(chan []byte, 1)
ti := time.NewTicker(time.Second)
last := Read(t)
Expand Down
6 changes: 5 additions & 1 deletion clipboard_nocgo.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
//go:build !windows && !cgo
// +build !windows,!cgo

package clipboard
package

import "context"

func test() bool {
panic("clipboard: cannot use when CGO_ENABLED=0")
}

func read(t Format) (buf []byte, err error) {
panic("clipboard: cannot use when CGO_ENABLED=0")
}
Expand Down
13 changes: 13 additions & 0 deletions clipboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ func init() {
clipboard.Debug = true
}

func TestClipboardTest(t *testing.T) {
if runtime.GOOS != "windows" {
if val, ok := os.LookupEnv("CGO_ENABLED"); ok && val == "0" {
t.Skip("CGO_ENABLED is set to 0")
}
}
t.Run("test", func(t *testing.T) {
if !clipboard.Test() {
t.Fatalf("clipboard test failed")
}
})
}

func TestClipboard(t *testing.T) {
if runtime.GOOS != "windows" {
if val, ok := os.LookupEnv("CGO_ENABLED"); ok && val == "0" {
Expand Down
4 changes: 4 additions & 0 deletions clipboard_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import (
"unsafe"
)

func test() bool {
return true
}

// readText reads the clipboard and returns the text data if presents.
// The caller is responsible for opening/closing the clipboard before
// calling this function.
Expand Down

0 comments on commit e25371e

Please sign in to comment.