package termview import ( "fmt" "reflect" "testing" "gioui.org/f32" "gioui.org/io/key" "gioui.org/io/pointer" "100gex/internal/rex" ) // event sends a mouse event with the given buttons held after it. func (h *selectHarness) event(kind pointer.Kind, p f32.Point, held pointer.Buttons, mods key.Modifiers) { h.router.Queue(pointer.Event{Kind: kind, Source: pointer.Mouse, Position: p, Buttons: held, Modifiers: mods, Time: h.now}) h.frame() } // reports records what the view sends to the program, as // action:button@x,y. func reports(h *selectHarness) *[]string { var got []string h.v.OnMouse = func(e rex.MouseEvent) { got = append(got, fmt.Sprintf("%s:%s@%d,%d", e.Action, e.Button, e.X, e.Y)) } return &got } // TestMouseToShell checks that a shell, which doesn't read the mouse, gets // nothing: clicks and drags select. func TestMouseToShell(t *testing.T) { h := newSelectHarness(t) got := reports(h) h.mouse(pointer.Press, h.at(0, 0)) h.mouse(pointer.Drag, h.past(4, 0)) h.mouse(pointer.Release, h.past(4, 0)) if len(*got) != 0 { t.Errorf("a shell got mouse reports: %v", *got) } if s := h.selected(); s != "hello" { t.Errorf("selected %q, want hello", s) } } // TestMouseToProgram clicks and drags over a program that reads the mouse: // it gets them, and nothing is selected. Motion goes only once the cell // changes. func TestMouseToProgram(t *testing.T) { h := newSelectHarness(t) got := reports(h) h.term.VTWrite([]byte("\x1b[?1002h")) h.frame() h.mouse(pointer.Press, h.at(1, 1)) h.mouse(pointer.Drag, h.past(1, 1)) // same cell: nothing h.mouse(pointer.Drag, h.at(3, 2)) h.mouse(pointer.Release, h.at(3, 2)) h.event(pointer.Press, h.at(5, 0), pointer.ButtonSecondary, 0) h.event(pointer.Release, h.at(5, 0), 0, 0) h.event(pointer.Move, h.at(6, 0), 0, 0) // no button, and 1002: nothing want := []string{"press:left@1,1", "motion:unknown@3,2", "release:left@3,2", "press:right@5,0", "release:right@5,0"} if !reflect.DeepEqual(*got, want) { t.Errorf("reports\n got %v\nwant %v", *got, want) } if s := h.selected(); s != "" { t.Errorf("clicking a program selected %q", s) } } // TestMouseMotion checks that a program that asks for all motion (1003) // gets moves with no button held. func TestMouseMotion(t *testing.T) { h := newSelectHarness(t) got := reports(h) h.term.VTWrite([]byte("\x1b[?1003h")) h.frame() h.event(pointer.Move, h.at(2, 1), 0, 0) h.event(pointer.Move, h.past(2, 1), 0, 0) h.event(pointer.Move, h.at(4, 1), 0, 0) want := []string{"motion:unknown@2,1", "motion:unknown@4,1"} if !reflect.DeepEqual(*got, want) { t.Errorf("reports\n got %v\nwant %v", *got, want) } } // TestShiftSelects checks that a Shift-drag over a program that reads the // mouse selects, as in other terminals, and that the program hears none of // it, even once Shift is let go mid-drag. func TestShiftSelects(t *testing.T) { h := newSelectHarness(t) got := reports(h) h.term.VTWrite([]byte("\x1b[?1003h")) h.frame() h.event(pointer.Press, h.at(0, 0), pointer.ButtonPrimary, key.ModShift) h.event(pointer.Move, h.past(2, 0), pointer.ButtonPrimary, key.ModShift) h.event(pointer.Move, h.past(4, 0), pointer.ButtonPrimary, 0) h.event(pointer.Release, h.past(4, 0), 0, 0) if len(*got) != 0 { t.Errorf("a Shift-drag reached the program: %v", *got) } if s := h.selected(); s != "hello" { t.Errorf("Shift-drag selected %q, want hello", s) } // A click on the program then clears the selection. h.event(pointer.Press, h.at(1, 1), pointer.ButtonPrimary, 0) h.event(pointer.Release, h.at(1, 1), 0, 0) if s := h.selected(); s != "" { t.Errorf("after a click on the program, selection %q", s) } } // TestMouseCancel checks that a gesture taken elsewhere lets go of the // program's buttons, so the program doesn't think one is stuck down. func TestMouseCancel(t *testing.T) { h := newSelectHarness(t) got := reports(h) h.term.VTWrite([]byte("\x1b[?1000h")) h.frame() h.event(pointer.Press, h.at(1, 0), pointer.ButtonPrimary, 0) h.event(pointer.Cancel, f32.Point{}, 0, 0) want := []string{"press:left@1,0", "release:left@1,0"} if !reflect.DeepEqual(*got, want) { t.Errorf("reports\n got %v\nwant %v", *got, want) } }