package ui import ( "testing" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) func TestArrowNavigation(t *testing.T) { f := tview.NewForm(). AddDropDown("A", []string{"x", "y", "z"}, 0, nil). AddCheckbox("B", true, nil). AddButton("Add", nil).AddButton("Cancel", nil) arrowNavigation(f) var focus func(p tview.Primitive) focus = func(p tview.Primitive) { p.Focus(focus) } f.Focus(focus) cap := f.GetInputCapture() send := func(key tcell.Key) { cap(tcell.NewEventKey(key, 0, 0)) } dd := f.GetFormItem(0).(*tview.DropDown) send(tcell.KeyRight) if i, _ := dd.GetCurrentOption(); i != 1 { t.Fatalf("Right: option %d, want 1", i) } send(tcell.KeyLeft) send(tcell.KeyLeft) if i, _ := dd.GetCurrentOption(); i != 2 { t.Fatalf("Left wraps: option %d, want 2", i) } send(tcell.KeyDown) if item, _ := f.GetFocusedItemIndex(); item != 1 { t.Fatalf("Down: item %d, want 1", item) } send(tcell.KeyDown) send(tcell.KeyDown) send(tcell.KeyDown) // clamps at the last button if _, b := f.GetFocusedItemIndex(); b != 1 { t.Fatalf("button %d, want 1", b) } send(tcell.KeyLeft) if _, b := f.GetFocusedItemIndex(); b != 0 { t.Fatalf("Left on buttons: %d, want 0", b) } send(tcell.KeyUp) if item, _ := f.GetFocusedItemIndex(); item != 1 { t.Fatalf("Up from first button: item %d, want 1", item) } } func TestSearchNowFollowsMonitored(t *testing.T) { f := tview.NewForm(). AddDropDown("P", []string{"a"}, 0, nil). AddDropDown("R", []string{"a"}, 0, nil). AddCheckbox("M", true, nil). AddCheckbox("S", true, nil). AddButton("Add", nil) arrowNavigation(f) var focus func(p tview.Primitive) focus = func(p tview.Primitive) { p.Focus(focus) } f.Focus(focus) mon := f.GetFormItem(2).(*tview.Checkbox) search := f.GetFormItem(3).(*tview.Checkbox) blocked := 0 sync := bindSearchNow(search, mon.IsChecked, func() { blocked++ }) mon.SetChangedFunc(func(bool) { sync() }) cap := f.GetInputCapture() key := func(k tcell.Key) { cap(tcell.NewEventKey(k, 0, 0)) } space := func(item tview.FormItem) { item.InputHandler()(tcell.NewEventKey(tcell.KeyRune, ' ', 0), func(tview.Primitive) {}) } focused := func() (n int) { for i := 0; i < f.GetFormItemCount(); i++ { if f.GetFormItem(i).HasFocus() { n++ } } return n } key(tcell.KeyDown) key(tcell.KeyDown) // on "Monitored" space(mon) // unmonitor if search.IsChecked() { t.Error("Search now must be unchecked when not monitored") } key(tcell.KeyUp) if n := focused(); n != 1 { t.Fatalf("%d fields have focus after moving up, want 1", n) } if item, _ := f.GetFocusedItemIndex(); item != 1 { t.Errorf("focus on item %d, want 1", item) } key(tcell.KeyDown) key(tcell.KeyDown) // on "Search now" space(search) if search.IsChecked() || blocked != 1 { t.Errorf("ticking while blocked: checked=%v blocked=%d", search.IsChecked(), blocked) } key(tcell.KeyUp) space(mon) // monitor again if !search.IsChecked() { t.Error("Search now should return to its previous choice") } }