|
| 1 | +package gocui |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +// A view and its parent view, with the child holding the focus. |
| 10 | +func setupParentAndChildView(t *testing.T, g *Gui) (*View, *View) { |
| 11 | + t.Helper() |
| 12 | + |
| 13 | + parent, _ := g.SetView("parent", 0, 0, 20, 10, 0) |
| 14 | + child, _ := g.SetView("child", 0, 10, 20, 12, 0) |
| 15 | + child.ParentView = parent |
| 16 | + _, err := g.SetCurrentView(child.Name()) |
| 17 | + assert.NoError(t, err) |
| 18 | + |
| 19 | + return parent, child |
| 20 | +} |
| 21 | + |
| 22 | +func TestKeybindingOfParentViewIsUsedWhenChildHasNone(t *testing.T) { |
| 23 | + g := newTestGui(t) |
| 24 | + parent, child := setupParentAndChildView(t, g) |
| 25 | + |
| 26 | + pressed := []string{} |
| 27 | + g.SetKeybinding(parent.Name(), NewKeyName(KeyArrowDown), func(*Gui, *View) error { |
| 28 | + pressed = append(pressed, "parent") |
| 29 | + return nil |
| 30 | + }) |
| 31 | + g.SetKeybinding(child.Name(), NewKeyName(KeyEnter), func(*Gui, *View) error { |
| 32 | + pressed = append(pressed, "child") |
| 33 | + return nil |
| 34 | + }) |
| 35 | + |
| 36 | + assert.NoError(t, g.onKey(&GocuiEvent{Type: eventKey, Key: NewKeyName(KeyArrowDown)})) |
| 37 | + assert.NoError(t, g.onKey(&GocuiEvent{Type: eventKey, Key: NewKeyName(KeyEnter)})) |
| 38 | + |
| 39 | + assert.Equal(t, []string{"parent", "child"}, pressed) |
| 40 | +} |
| 41 | + |
| 42 | +func TestFirstMatchingKeybindingOfParentViewWins(t *testing.T) { |
| 43 | + g := newTestGui(t) |
| 44 | + parent, _ := setupParentAndChildView(t, g) |
| 45 | + |
| 46 | + pressed := []string{} |
| 47 | + for _, name := range []string{"first", "second"} { |
| 48 | + g.SetKeybinding(parent.Name(), NewKeyName(KeyArrowDown), func(*Gui, *View) error { |
| 49 | + pressed = append(pressed, name) |
| 50 | + return nil |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + assert.NoError(t, g.onKey(&GocuiEvent{Type: eventKey, Key: NewKeyName(KeyArrowDown)})) |
| 55 | + |
| 56 | + assert.Equal(t, []string{"first"}, pressed) |
| 57 | +} |
| 58 | + |
| 59 | +func TestEmbeddedViewsAreFocusedTogether(t *testing.T) { |
| 60 | + g := newTestGui(t) |
| 61 | + parent, child := setupParentAndChildView(t, g) |
| 62 | + sibling, _ := g.SetView("sibling", 0, 12, 20, 14, 0) |
| 63 | + sibling.ParentView = parent |
| 64 | + unrelated, _ := g.SetView("unrelated", 30, 0, 50, 10, 0) |
| 65 | + |
| 66 | + assert.True(t, g.hasFocus(child)) |
| 67 | + assert.True(t, g.hasFocus(parent)) |
| 68 | + assert.True(t, g.hasFocus(sibling)) |
| 69 | + assert.False(t, g.hasFocus(unrelated)) |
| 70 | + |
| 71 | + _, err := g.SetCurrentView(unrelated.Name()) |
| 72 | + assert.NoError(t, err) |
| 73 | + |
| 74 | + assert.True(t, g.hasFocus(unrelated)) |
| 75 | + assert.False(t, g.hasFocus(parent)) |
| 76 | + assert.False(t, g.hasFocus(child)) |
| 77 | +} |
| 78 | + |
| 79 | +func TestPrintableKeysGoToTheFieldBeingTypedIn(t *testing.T) { |
| 80 | + for _, test := range []struct { |
| 81 | + name string |
| 82 | + keybindOnEdit bool |
| 83 | + declineKeybinding bool |
| 84 | + expectedPresses int |
| 85 | + expectedEdits int |
| 86 | + }{ |
| 87 | + {name: "the field gets the key", expectedEdits: 1}, |
| 88 | + {name: "the parent view gets the key", keybindOnEdit: true, expectedPresses: 1}, |
| 89 | + { |
| 90 | + name: "the field gets the key the parent view declined", |
| 91 | + keybindOnEdit: true, |
| 92 | + declineKeybinding: true, |
| 93 | + expectedPresses: 1, |
| 94 | + expectedEdits: 1, |
| 95 | + }, |
| 96 | + } { |
| 97 | + t.Run(test.name, func(t *testing.T) { |
| 98 | + g := newTestGui(t) |
| 99 | + parent, child := setupParentAndChildView(t, g) |
| 100 | + child.Editable = true |
| 101 | + child.KeybindOnEdit = test.keybindOnEdit |
| 102 | + |
| 103 | + edits := 0 |
| 104 | + child.Editor = EditorFunc(func(*View, Key) bool { |
| 105 | + edits++ |
| 106 | + return true |
| 107 | + }) |
| 108 | + presses := 0 |
| 109 | + g.SetKeybinding(parent.Name(), NewKeyRune('j'), func(*Gui, *View) error { |
| 110 | + presses++ |
| 111 | + if test.declineKeybinding { |
| 112 | + return ErrKeybindingNotHandled |
| 113 | + } |
| 114 | + return nil |
| 115 | + }) |
| 116 | + |
| 117 | + assert.NoError(t, g.onKey(&GocuiEvent{Type: eventKey, Key: NewKeyRune('j')})) |
| 118 | + |
| 119 | + assert.Equal(t, test.expectedPresses, presses) |
| 120 | + assert.Equal(t, test.expectedEdits, edits) |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestUnhandledKeybindingOfParentViewFallsThroughToEditor(t *testing.T) { |
| 126 | + g := newTestGui(t) |
| 127 | + parent, child := setupParentAndChildView(t, g) |
| 128 | + |
| 129 | + edited := []Key{} |
| 130 | + child.Editable = true |
| 131 | + child.Editor = EditorFunc(func(_ *View, key Key) bool { |
| 132 | + edited = append(edited, key) |
| 133 | + return true |
| 134 | + }) |
| 135 | + g.SetKeybinding(parent.Name(), NewKeyName(KeyArrowDown), func(*Gui, *View) error { |
| 136 | + return ErrKeybindingNotHandled |
| 137 | + }) |
| 138 | + |
| 139 | + assert.NoError(t, g.onKey(&GocuiEvent{Type: eventKey, Key: NewKeyName(KeyArrowDown)})) |
| 140 | + |
| 141 | + assert.Equal(t, []Key{NewKeyName(KeyArrowDown)}, edited) |
| 142 | +} |
0 commit comments