Timeouts, cancellation (tests).

This commit is contained in:
Nuno Cruces 2020-01-29 14:15:21 +00:00
parent 7c4199bdd1
commit ba1f15ed0e
13 changed files with 80 additions and 26 deletions

View file

@ -29,7 +29,7 @@ func TestSelectColorTimeout(t *testing.T) {
_, err := zenity.SelectColor(zenity.Context(ctx)) _, err := zenity.SelectColor(zenity.Context(ctx))
if !os.IsTimeout(err) { if !os.IsTimeout(err) {
t.Error("did not timeout", err) t.Error("did not timeout:", err)
} }
cancel() cancel()
@ -41,6 +41,6 @@ func TestSelectColorCancel(t *testing.T) {
_, err := zenity.SelectColor(zenity.Context(ctx)) _, err := zenity.SelectColor(zenity.Context(ctx))
if !errors.Is(err, context.Canceled) { if !errors.Is(err, context.Canceled) {
t.Error("not canceled", err) t.Error("was not canceled:", err)
} }
} }

View file

@ -54,8 +54,8 @@ func selectColor(options []Option) (color.Color, error) {
defer unhookWindowsHookEx.Call(hook) defer unhookWindowsHookEx.Call(hook)
} }
n, _, _ := chooseColor.Call(uintptr(unsafe.Pointer(&args))) s, _, _ := chooseColor.Call(uintptr(unsafe.Pointer(&args)))
if n == 0 { if s == 0 {
return nil, commDlgError() return nil, commDlgError()
} }

View file

@ -83,7 +83,7 @@ func TestFileTimeout(t *testing.T) {
_, err := f(zenity.Context(ctx)) _, err := f(zenity.Context(ctx))
if !os.IsTimeout(err) { if !os.IsTimeout(err) {
t.Error("did not timeout", err) t.Error("did not timeout:", err)
} }
cancel() cancel()
@ -97,7 +97,7 @@ func TestFileCancel(t *testing.T) {
for _, f := range fileFuncs { for _, f := range fileFuncs {
_, err := f(zenity.Context(ctx)) _, err := f(zenity.Context(ctx))
if !errors.Is(err, context.Canceled) { if !errors.Is(err, context.Canceled) {
t.Error("not canceled", err) t.Error("was not canceled:", err)
} }
} }
} }

View file

@ -46,8 +46,8 @@ func selectFile(options []Option) (string, error) {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
n, _, _ := getOpenFileName.Call(uintptr(unsafe.Pointer(&args))) s, _, _ := getOpenFileName.Call(uintptr(unsafe.Pointer(&args)))
if n == 0 { if s == 0 {
return "", commDlgError() return "", commDlgError()
} }
return syscall.UTF16ToString(res[:]), nil return syscall.UTF16ToString(res[:]), nil
@ -82,8 +82,8 @@ func selectFileMutiple(options []Option) ([]string, error) {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
n, _, _ := getOpenFileName.Call(uintptr(unsafe.Pointer(&args))) s, _, _ := getOpenFileName.Call(uintptr(unsafe.Pointer(&args)))
if n == 0 { if s == 0 {
return nil, commDlgError() return nil, commDlgError()
} }
@ -149,8 +149,8 @@ func selectFileSave(options []Option) (string, error) {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
n, _, _ := getSaveFileName.Call(uintptr(unsafe.Pointer(&args))) s, _, _ := getSaveFileName.Call(uintptr(unsafe.Pointer(&args)))
if n == 0 { if s == 0 {
return "", commDlgError() return "", commDlgError()
} }
return syscall.UTF16ToString(res[:]), nil return syscall.UTF16ToString(res[:]), nil

7
go.mod
View file

@ -2,4 +2,9 @@ module github.com/ncruces/zenity
go 1.13 go 1.13
require golang.org/x/image v0.0.0-20191214001246-9130b4cfad52 require (
go.uber.org/goleak v1.0.0 // test
golang.org/x/image v0.0.0-20191214001246-9130b4cfad52
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f // indirect
golang.org/x/tools v0.0.0-20200129045341-207d3de1faaf // indirect
)

39
go.sum
View file

@ -1,4 +1,41 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
go.uber.org/goleak v1.0.0 h1:qsup4IcBdlmsnGfqyLl4Ntn3C2XCCuKAE7DwHpScyUo=
go.uber.org/goleak v1.0.0/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/image v0.0.0-20191214001246-9130b4cfad52 h1:2fktqPPvDiVEEVT/vSTeoUPXfmRxRaGy6GU8jypvEn0= golang.org/x/image v0.0.0-20191214001246-9130b4cfad52 h1:2fktqPPvDiVEEVT/vSTeoUPXfmRxRaGy6GU8jypvEn0=
golang.org/x/image v0.0.0-20191214001246-9130b4cfad52/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/image v0.0.0-20191214001246-9130b4cfad52/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f h1:J5lckAjkw6qYlOZNj90mLYNTEKDvWeuc1yieZ8qUzUE=
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200129045341-207d3de1faaf h1:mFgR10kFfr83r2+nXf0GZC2FKrFhMSs9NdJ0YdEaGiY=
golang.org/x/tools v0.0.0-20200129045341-207d3de1faaf/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View file

@ -51,7 +51,7 @@ func TestMessageTimeout(t *testing.T) {
_, err := f("text", zenity.Context(ctx)) _, err := f("text", zenity.Context(ctx))
if !os.IsTimeout(err) { if !os.IsTimeout(err) {
t.Error("did not timeout", err) t.Error("did not timeout:", err)
} }
cancel() cancel()
@ -65,7 +65,7 @@ func TestMessageCancel(t *testing.T) {
for _, f := range msgFuncs { for _, f := range msgFuncs {
_, err := f("text", zenity.Context(ctx)) _, err := f("text", zenity.Context(ctx))
if !errors.Is(err, context.Canceled) { if !errors.Is(err, context.Canceled) {
t.Error("not canceled", err) t.Error("was not canceled:", err)
} }
} }
} }

View file

@ -52,17 +52,17 @@ func message(kind messageKind, text string, options []Option) (bool, error) {
defer unhookWindowsHookEx.Call(hook) defer unhookWindowsHookEx.Call(hook)
} }
n, _, err := messageBox.Call(0, s, _, err := messageBox.Call(0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(opts.title))), flags) uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(opts.title))), flags)
if n == 0 { if s == 0 {
return false, err return false, err
} }
if n == 7 || n == 2 && kind != questionKind { // IDNO if s == 7 || s == 2 && kind != questionKind { // IDNO
return false, ErrExtraButton return false, ErrExtraButton
} }
if n == 1 || n == 6 { // IDOK, IDYES if s == 1 || s == 6 { // IDOK, IDYES
return true, nil return true, nil
} }
return false, nil return false, nil

View file

@ -21,6 +21,6 @@ func TestNotifyCancel(t *testing.T) {
err := zenity.Notify("text", zenity.Context(ctx)) err := zenity.Notify("text", zenity.Context(ctx))
if !errors.Is(err, context.Canceled) { if !errors.Is(err, context.Canceled) {
t.Error("not canceled", err) t.Error("was not canceled:", err)
} }
} }

View file

@ -37,8 +37,8 @@ func notify(text string, options []Option) error {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
n, _, err := shellNotifyIcon.Call(0 /* NIM_ADD */, uintptr(unsafe.Pointer(&args))) s, _, err := shellNotifyIcon.Call(0 /* NIM_ADD */, uintptr(unsafe.Pointer(&args)))
if n == 0 { if s == 0 {
if errno, ok := err.(syscall.Errno); ok && errno == 0 { if errno, ok := err.(syscall.Errno); ok && errno == 0 {
_, err = Info(text, Title(opts.title), Icon(opts.icon)) _, err = Info(text, Title(opts.title), Icon(opts.icon))
} }

View file

@ -33,11 +33,11 @@ var (
) )
func commDlgError() error { func commDlgError() error {
n, _, _ := commDlgExtendedError.Call() s, _, _ := commDlgExtendedError.Call()
if n == 0 { if s == 0 {
return nil return nil
} else { } else {
return fmt.Errorf("Common Dialog error: %x", n) return fmt.Errorf("Common Dialog error: %x", s)
} }
} }

View file

@ -86,6 +86,7 @@ func Icon(icon DialogIcon) Option {
return funcOption(func(o *options) { o.icon = icon }) return funcOption(func(o *options) { o.icon = icon })
} }
// Context returns an Option to set a Context that can dismiss the dialog.
func Context(ctx context.Context) Option { func Context(ctx context.Context) Option {
return funcOption(func(o *options) { o.ctx = ctx }) return funcOption(func(o *options) { o.ctx = ctx })
} }

11
zenity_test.go Normal file
View file

@ -0,0 +1,11 @@
package zenity_test
import (
"testing"
"go.uber.org/goleak"
)
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}