| 🛟 Updated. 28d5985 k33g 18h ago | 1 | package ui |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestStatusBarDrawsItsHints(t *testing.T) { |
| 9 | screen, root := newTestScreen(t, 40, 2) |
| 10 | bar := NewStatusBar( |
| 11 | StatusItem{Key: "F1", Label: "Help"}, |
| 12 | StatusItem{Key: "F2", Label: "Save"}, |
| 13 | ) |
| 14 | bar.SetBounds(Rect{X: 0, Y: 1, W: 40, H: 1}) |
| 15 | |
| 16 | bar.Draw(root, testTheme(t)) |
| 17 | screen.Show() |
| 18 | |
| 19 | got := screenLines(t, screen)[1] |
| 20 | if !strings.Contains(got, "F1 Help") || !strings.Contains(got, "F2 Save") { |
| 21 | t.Errorf("the status bar is %q, want both hints", got) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestAMessageReplacesTheHints(t *testing.T) { |
| 26 | screen, root := newTestScreen(t, 40, 2) |
| 27 | bar := NewStatusBar(StatusItem{Key: "F2", Label: "Save"}) |
| 28 | bar.SetBounds(Rect{X: 0, Y: 1, W: 40, H: 1}) |
| 29 | |
| 30 | bar.SetMessage("File saved") |
| 31 | bar.Draw(root, testTheme(t)) |
| 32 | screen.Show() |
| 33 | |
| 34 | got := screenLines(t, screen)[1] |
| 35 | if !strings.Contains(got, "File saved") { |
| 36 | t.Errorf("the status bar is %q, want the message", got) |
| 37 | } |
| 38 | if strings.Contains(got, "F2") { |
| 39 | t.Errorf("the status bar is %q, want the hints hidden while a message shows", got) |
| 40 | } |
| 41 | if bar.Message() != "File saved" { |
| 42 | t.Errorf("Message() = %q", bar.Message()) |
| 43 | } |
| 44 | |
| 45 | bar.SetMessage("") |
| 46 | bar.Draw(root, testTheme(t)) |
| 47 | screen.Show() |
| 48 | if !strings.Contains(screenLines(t, screen)[1], "F2") { |
| 49 | t.Error("clearing the message did not bring the hints back") |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestExtraTextIsRightAligned(t *testing.T) { |
| 54 | screen, root := newTestScreen(t, 40, 2) |
| 55 | bar := NewStatusBar(StatusItem{Key: "F1", Label: "Help"}) |
| 56 | bar.SetBounds(Rect{X: 0, Y: 1, W: 40, H: 1}) |
| 57 | |
| 58 | bar.SetExtra("12:5") |
| 59 | bar.Draw(root, testTheme(t)) |
| 60 | screen.Show() |
| 61 | |
| 62 | got := screenLines(t, screen)[1] |
| 63 | if !strings.HasSuffix(strings.TrimRight(got, " "), "12:5") { |
| 64 | t.Errorf("the status bar is %q, want the extra text at the right", got) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestClickingAHintRunsItsAction(t *testing.T) { |
| 69 | ran := "" |
| 70 | bar := NewStatusBar( |
| 71 | StatusItem{Key: "F1", Label: "Help", Action: func() { ran = "help" }}, |
| 72 | StatusItem{Key: "F2", Label: "Save", Action: func() { ran = "save" }}, |
| 73 | ) |
| 74 | bar.SetBounds(Rect{X: 0, Y: 5, W: 40, H: 1}) |
| 75 | |
| 76 | // "F1 Help" occupies columns 1..8, so "F2 Save" starts at 10. |
| 77 | bar.HandleMouse(clickEvent(11, 5)) |
| 78 | |
| 79 | if ran != "save" { |
| 80 | t.Errorf("the action that ran was %q, want save", ran) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestAClickOffTheStatusBarIsNotItsBusiness(t *testing.T) { |
| 85 | bar := NewStatusBar(StatusItem{Key: "F1", Label: "Help", Action: func() {}}) |
| 86 | bar.SetBounds(Rect{X: 0, Y: 5, W: 40, H: 1}) |
| 87 | |
| 88 | if bar.HandleMouse(clickEvent(3, 0)) { |
| 89 | t.Error("the status bar claimed a click from another row") |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func TestClickingPastTheLastHintDoesNothing(t *testing.T) { |
| 94 | ran := 0 |
| 95 | bar := NewStatusBar(StatusItem{Key: "F1", Label: "Help", Action: func() { ran++ }}) |
| 96 | bar.SetBounds(Rect{X: 0, Y: 5, W: 40, H: 1}) |
| 97 | |
| 98 | bar.HandleMouse(clickEvent(35, 5)) |
| 99 | |
| 100 | if ran != 0 { |
| 101 | t.Error("a click on the empty part of the bar ran an action") |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestSetItemsReplacesTheHints(t *testing.T) { |
| 106 | screen, root := newTestScreen(t, 40, 2) |
| 107 | bar := NewStatusBar(StatusItem{Key: "F1", Label: "Help"}) |
| 108 | bar.SetBounds(Rect{X: 0, Y: 1, W: 40, H: 1}) |
| 109 | |
| 110 | bar.SetItems(StatusItem{Key: "Esc", Label: "Cancel"}) |
| 111 | bar.Draw(root, testTheme(t)) |
| 112 | screen.Show() |
| 113 | |
| 114 | got := screenLines(t, screen)[1] |
| 115 | if strings.Contains(got, "Help") || !strings.Contains(got, "Esc Cancel") { |
| 116 | t.Errorf("the status bar is %q, want only the new hint", got) |
| 117 | } |
| 118 | } |