2020-01-18 07:40:16 -05:00
|
|
|
package zenity_test
|
|
|
|
|
|
|
|
import (
|
2020-01-29 06:09:06 -05:00
|
|
|
"context"
|
|
|
|
"errors"
|
2021-07-07 08:24:46 -04:00
|
|
|
"fmt"
|
2020-01-18 07:40:16 -05:00
|
|
|
"image/color"
|
2020-01-29 06:09:06 -05:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2020-01-18 07:40:16 -05:00
|
|
|
|
2024-07-10 00:06:01 -04:00
|
|
|
"git.bigun.dev/evan/zenity"
|
|
|
|
"git.bigun.dev/evan/zenity/internal/zenutil"
|
2021-04-30 14:19:14 -04:00
|
|
|
"go.uber.org/goleak"
|
2020-01-18 07:40:16 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleSelectColor() {
|
|
|
|
zenity.SelectColor(
|
2020-01-21 11:13:45 -05:00
|
|
|
zenity.Color(color.NRGBA{R: 0x66, G: 0x33, B: 0x99, A: 0x80}))
|
2020-01-18 07:40:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleSelectColor_palette() {
|
|
|
|
zenity.SelectColor(
|
|
|
|
zenity.ShowPalette(),
|
2020-01-21 11:13:45 -05:00
|
|
|
zenity.Color(color.NRGBA{R: 0x66, G: 0x33, B: 0x99, A: 0xff}))
|
2020-01-18 07:40:16 -05:00
|
|
|
}
|
2020-01-29 06:09:06 -05:00
|
|
|
|
2021-04-30 14:19:14 -04:00
|
|
|
func TestSelectColor_timeout(t *testing.T) {
|
2022-05-18 09:48:09 -04:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping test in short mode.")
|
|
|
|
}
|
|
|
|
|
2021-04-30 14:19:14 -04:00
|
|
|
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()
|
2020-01-29 06:09:06 -05:00
|
|
|
|
|
|
|
_, err := zenity.SelectColor(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)
|
|
|
|
}
|
2020-01-29 06:09:06 -05:00
|
|
|
if !os.IsTimeout(err) {
|
2020-01-29 09:15:21 -05:00
|
|
|
t.Error("did not timeout:", err)
|
2020-01-29 06:09:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-30 14:19:14 -04:00
|
|
|
func TestSelectColor_cancel(t *testing.T) {
|
|
|
|
defer goleak.VerifyNone(t)
|
2020-01-29 06:09:06 -05:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
_, err := zenity.SelectColor(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)
|
|
|
|
}
|
2020-01-29 06:09:06 -05:00
|
|
|
if !errors.Is(err, context.Canceled) {
|
2020-01-29 09:15:21 -05:00
|
|
|
t.Error("was not canceled:", err)
|
2020-01-29 06:09:06 -05:00
|
|
|
}
|
|
|
|
}
|
2021-07-07 08:24:46 -04:00
|
|
|
|
|
|
|
func TestSelectColor_script(t *testing.T) {
|
2022-05-18 09:48:09 -04:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping test in short mode.")
|
|
|
|
}
|
|
|
|
|
2021-07-07 08:24:46 -04:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
call string
|
2021-07-12 20:43:52 -04:00
|
|
|
opts []zenity.Option
|
2021-07-07 08:24:46 -04:00
|
|
|
want color.Color
|
|
|
|
err error
|
|
|
|
}{
|
2021-07-12 20:43:52 -04:00
|
|
|
{name: "Cancel", call: "cancel", err: zenity.ErrCanceled},
|
|
|
|
{name: "Black", call: "choose black", want: color.Black},
|
|
|
|
{name: "White", call: "choose white", want: color.White},
|
|
|
|
{name: "Rebecca", call: "press OK", want: color.NRGBA{R: 0x66, G: 0x33, B: 0x99, A: 0xff},
|
|
|
|
opts: []zenity.Option{zenity.ShowPalette(), zenity.Color(color.NRGBA{R: 0x66, G: 0x33, B: 0x99, A: 0xff})}},
|
2021-07-07 08:24:46 -04:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
zenity.Info(fmt.Sprintf("In the color selection dialog, %s.", tt.call))
|
2021-07-12 20:43:52 -04:00
|
|
|
got, err := zenity.SelectColor(tt.opts...)
|
2021-07-07 08:24:46 -04:00
|
|
|
if skip, err := skip(err); skip {
|
|
|
|
t.Skip("skipping:", err)
|
|
|
|
}
|
2021-07-12 20:43:52 -04:00
|
|
|
if !zenutil.ColorEquals(got, tt.want) || err != tt.err {
|
|
|
|
t.Errorf("SelectColor() = %v, %v; want %v, %v", got, err, tt.want, tt.err)
|
2021-07-07 08:24:46 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|