2021-04-08 20:26:57 -04:00
|
|
|
package zenity_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2021-07-12 20:43:52 -04:00
|
|
|
"fmt"
|
2021-04-08 20:26:57 -04:00
|
|
|
"os"
|
2021-06-18 11:16:04 -04:00
|
|
|
"runtime"
|
2021-04-08 20:26:57 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncruces/zenity"
|
2021-04-30 14:19:14 -04:00
|
|
|
"go.uber.org/goleak"
|
2021-04-08 20:26:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func ExamplePassword() {
|
|
|
|
zenity.Password(zenity.Title("Type your password"))
|
|
|
|
}
|
|
|
|
|
2021-06-18 11:16:04 -04:00
|
|
|
func ExamplePassword_username() {
|
|
|
|
zenity.Password(
|
|
|
|
zenity.Title("Type your username and password"),
|
|
|
|
zenity.Username())
|
|
|
|
}
|
|
|
|
|
2021-04-30 14:19:14 -04:00
|
|
|
func TestPassword_timeout(t *testing.T) {
|
|
|
|
defer goleak.VerifyNone(t)
|
2021-07-29 11:20:50 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second/5)
|
2021-04-30 14:19:14 -04:00
|
|
|
defer cancel()
|
2021-04-08 20:26:57 -04:00
|
|
|
|
2021-04-29 11:05:28 -04:00
|
|
|
_, _, err := zenity.Password(zenity.Context(ctx))
|
2021-07-06 20:01:22 -04:00
|
|
|
if skip, err := skip(err); skip {
|
2021-06-18 10:04:09 -04:00
|
|
|
t.Skip("skipping:", err)
|
|
|
|
}
|
2021-04-08 20:26:57 -04:00
|
|
|
if !os.IsTimeout(err) {
|
|
|
|
t.Error("did not timeout:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 14:19:14 -04:00
|
|
|
func TestPassword_cancel(t *testing.T) {
|
|
|
|
defer goleak.VerifyNone(t)
|
2021-04-08 20:26:57 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
|
|
|
|
2021-04-29 11:05:28 -04:00
|
|
|
_, _, err := zenity.Password(zenity.Context(ctx))
|
2021-07-06 20:01:22 -04:00
|
|
|
if skip, err := skip(err); skip {
|
2021-06-18 10:04:09 -04:00
|
|
|
t.Skip("skipping:", err)
|
|
|
|
}
|
2021-04-08 20:26:57 -04:00
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Error("was not canceled:", err)
|
|
|
|
}
|
|
|
|
}
|
2021-06-18 11:16:04 -04:00
|
|
|
|
|
|
|
func TestPassword_username(t *testing.T) {
|
|
|
|
defer goleak.VerifyNone(t)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
_, _, err := zenity.Password(zenity.Context(ctx), zenity.Username())
|
2021-07-06 20:01:22 -04:00
|
|
|
if skip, err := skip(err); skip {
|
2021-06-18 11:16:04 -04:00
|
|
|
t.Skip("skipping:", err)
|
|
|
|
}
|
2022-05-11 12:49:15 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
2022-05-03 09:20:22 -04:00
|
|
|
if errors.Is(err, zenity.ErrUnsupported) {
|
|
|
|
t.Skip("was not unsupported:", err)
|
|
|
|
} else {
|
2021-06-18 11:16:04 -04:00
|
|
|
t.Error("was not unsupported:", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Error("was not canceled:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 20:43:52 -04:00
|
|
|
|
|
|
|
func TestPassword_script(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
call string
|
|
|
|
opts []zenity.Option
|
|
|
|
usr string
|
|
|
|
pwd string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{name: "Cancel", call: "cancel", err: zenity.ErrCanceled},
|
|
|
|
{name: "Password", call: "enter pwd", pwd: "pwd"},
|
|
|
|
{name: "User", call: "enter usr and pwd (if supported)", usr: "usr", pwd: "pwd",
|
|
|
|
opts: []zenity.Option{zenity.Username()}},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
zenity.Info(fmt.Sprintf("In the password dialog, %s.", tt.call))
|
|
|
|
usr, pwd, err := zenity.Password(tt.opts...)
|
|
|
|
if skip, err := skip(err); skip {
|
|
|
|
t.Skip("skipping:", err)
|
|
|
|
}
|
2022-05-03 09:20:22 -04:00
|
|
|
if errors.Is(err, zenity.ErrUnsupported) {
|
2021-07-12 20:43:52 -04:00
|
|
|
t.Skip("was not unsupported:", err)
|
|
|
|
}
|
|
|
|
if usr != tt.usr || pwd != tt.pwd || err != tt.err {
|
|
|
|
t.Errorf("Password() = %q, %q, %v; want %q, %q, %v", usr, pwd, err, tt.usr, tt.pwd, tt.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|