package mujica import ( "testing" "github.com/egoist/mygo/ui" ) func TestPasswordInputMaskPasteAndReveal(t *testing.T) { pw, changes, submits := "", 0, 0 tt := ui.NewTester(func(c *ui.Context) { Use(c, Settings{}) r := PasswordInput(c, &pw, PasswordInputOptions{Label: "Password"}) if r.Changed() { changes++ } if r.Submitted() { submits++ } }, 400, 200) _ = tt.Click("Password") tt.SetClipboard("s3cr") tt.Command("paste") tt.Type("et") if pw != "s3cret" || changes != 2 { t.Fatalf("value %q changes %d", pw, changes) } tt.SetClipboard("") tt.Command("selectAll") tt.Command("copy") if tt.Clipboard() != "" { t.Fatalf("masked field copied %q", tt.Clipboard()) } for _, s := range tt.Texts() { if s == "s3cret" { t.Fatal("masked password shows in the texts") } } if err := tt.Click("Show password"); err != nil { t.Fatal(err) } if _, ok := tt.Find("Hide password"); !ok || pw != "s3cret" { t.Fatalf("after reveal: value %q, texts %q", pw, tt.Texts()) } _ = tt.Click("Password") tt.Command("selectAll") tt.Command("copy") if tt.Clipboard() != "s3cret" { t.Fatalf("revealed field copied %q", tt.Clipboard()) } tt.Key(0, ui.KeyEnter) if submits != 1 { t.Fatalf("submits %d", submits) } _ = tt.Click("Hide password") if _, ok := tt.Find("Show password"); !ok { t.Fatal("hide did not mask again") } } func TestPasswordInputToggleByKeyboard(t *testing.T) { pw := "x" tt := ui.NewTester(func(c *ui.Context) { Use(c, Settings{}) PasswordInput(c, &pw, PasswordInputOptions{Label: "PIN"}) }, 400, 200) tt.Key(0, ui.KeyTab) tt.Key(0, ui.KeyTab) tt.Key(0, ui.KeySpace) if _, ok := tt.Find("Hide password"); !ok { t.Fatal("Space on the toggle did not reveal") } } func TestPasswordInputDisabled(t *testing.T) { pw := "locked" tt := ui.NewTester(func(c *ui.Context) { Use(c, Settings{}) PasswordInput(c, &pw, PasswordInputOptions{Label: "Off", Disabled: true}) }, 400, 200) _ = tt.Click("Show password") _ = tt.Click("Off") tt.Type("zz") if pw != "locked" { t.Fatalf("disabled value %q", pw) } if _, ok := tt.Find("Show password"); !ok { t.Fatal("disabled toggle revealed") } } func TestPasswordInputSnapshot(t *testing.T) { snapshot(t, "password_input", 420, 220, func(c *ui.Context) { a, b, e := "", "versailles", "short" ui.Column(c).Padding(24).Gap(12).Width(320).Children(func() { PasswordInput(c, &a, PasswordInputOptions{Label: "A", Placeholder: "Password"}) PasswordInput(c, &b, PasswordInputOptions{Label: "B"}) PasswordInput(c, &e, PasswordInputOptions{Label: "E", Error: "At least 8 characters"}) }) }) }