2020-11-07 00:43:12 -05:00
|
|
|
package tea
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
func TestMouseEvent_String(t *testing.T) {
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
event MouseEvent
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "unknown",
|
|
|
|
event: MouseEvent{Type: MouseUnknown},
|
|
|
|
expected: "unknown",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "left",
|
|
|
|
event: MouseEvent{Type: MouseLeft},
|
|
|
|
expected: "left",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "right",
|
|
|
|
event: MouseEvent{Type: MouseRight},
|
|
|
|
expected: "right",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "middle",
|
|
|
|
event: MouseEvent{Type: MouseMiddle},
|
|
|
|
expected: "middle",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "release",
|
|
|
|
event: MouseEvent{Type: MouseRelease},
|
|
|
|
expected: "release",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wheel up",
|
|
|
|
event: MouseEvent{Type: MouseWheelUp},
|
|
|
|
expected: "wheel up",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wheel down",
|
|
|
|
event: MouseEvent{Type: MouseWheelDown},
|
|
|
|
expected: "wheel down",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "motion",
|
|
|
|
event: MouseEvent{Type: MouseMotion},
|
|
|
|
expected: "motion",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "alt+left",
|
|
|
|
event: MouseEvent{
|
|
|
|
Type: MouseLeft,
|
|
|
|
Alt: true,
|
|
|
|
},
|
|
|
|
expected: "alt+left",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ctrl+left",
|
|
|
|
event: MouseEvent{
|
|
|
|
Type: MouseLeft,
|
|
|
|
Ctrl: true,
|
|
|
|
},
|
|
|
|
expected: "ctrl+left",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ctrl+alt+left",
|
|
|
|
event: MouseEvent{
|
|
|
|
Type: MouseLeft,
|
|
|
|
Alt: true,
|
|
|
|
Ctrl: true,
|
|
|
|
},
|
|
|
|
expected: "ctrl+alt+left",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ignore coordinates",
|
|
|
|
event: MouseEvent{
|
|
|
|
X: 100,
|
|
|
|
Y: 200,
|
|
|
|
Type: MouseLeft,
|
|
|
|
},
|
|
|
|
expected: "left",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "broken type",
|
|
|
|
event: MouseEvent{
|
|
|
|
Type: MouseEventType(-1000),
|
|
|
|
},
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range tt {
|
|
|
|
tc := tt[i]
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
actual := tc.event.String()
|
|
|
|
|
|
|
|
if tc.expected != actual {
|
|
|
|
t.Fatalf("expected %q but got %q",
|
|
|
|
tc.expected,
|
|
|
|
actual,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseX10MouseEvent(t *testing.T) {
|
|
|
|
encode := func(b byte, x, y int) []byte {
|
|
|
|
return []byte{
|
|
|
|
'\x1b',
|
|
|
|
'[',
|
|
|
|
'M',
|
|
|
|
byte(32) + b,
|
|
|
|
byte(x + 32 + 1),
|
|
|
|
byte(y + 32 + 1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tt := []struct {
|
|
|
|
name string
|
|
|
|
buf []byte
|
2022-10-21 11:00:39 -04:00
|
|
|
expected MouseEvent
|
2020-11-07 00:43:12 -05:00
|
|
|
}{
|
|
|
|
// Position.
|
|
|
|
{
|
|
|
|
name: "zero position",
|
|
|
|
buf: encode(0b0010_0000, 0, 0),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 0,
|
|
|
|
Y: 0,
|
|
|
|
Type: MouseLeft,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "max position",
|
|
|
|
buf: encode(0b0010_0000, 222, 222), // Because 255 (max int8) - 32 - 1.
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 222,
|
|
|
|
Y: 222,
|
|
|
|
Type: MouseLeft,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// Simple.
|
|
|
|
{
|
|
|
|
name: "left",
|
|
|
|
buf: encode(0b0000_0000, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseLeft,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "left in motion",
|
|
|
|
buf: encode(0b0010_0000, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseLeft,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "middle",
|
|
|
|
buf: encode(0b0000_0001, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseMiddle,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "middle in motion",
|
|
|
|
buf: encode(0b0010_0001, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseMiddle,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "right",
|
|
|
|
buf: encode(0b0000_0010, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseRight,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "right in motion",
|
|
|
|
buf: encode(0b0010_0010, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseRight,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "motion",
|
|
|
|
buf: encode(0b0010_0011, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseMotion,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wheel up",
|
|
|
|
buf: encode(0b0100_0000, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseWheelUp,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wheel down",
|
|
|
|
buf: encode(0b0100_0001, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseWheelDown,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "release",
|
|
|
|
buf: encode(0b0000_0011, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseRelease,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// Combinations.
|
|
|
|
{
|
|
|
|
name: "alt+right",
|
|
|
|
buf: encode(0b0010_1010, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseRight,
|
|
|
|
Alt: true,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ctrl+right",
|
|
|
|
buf: encode(0b0011_0010, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseRight,
|
|
|
|
Ctrl: true,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ctrl+alt+right",
|
|
|
|
buf: encode(0b0011_1010, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseRight,
|
|
|
|
Alt: true,
|
|
|
|
Ctrl: true,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "alt+wheel down",
|
|
|
|
buf: encode(0b0100_1001, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseWheelDown,
|
|
|
|
Alt: true,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ctrl+wheel down",
|
|
|
|
buf: encode(0b0101_0001, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseWheelDown,
|
|
|
|
Ctrl: true,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ctrl+alt+wheel down",
|
|
|
|
buf: encode(0b0101_1001, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseWheelDown,
|
|
|
|
Alt: true,
|
|
|
|
Ctrl: true,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// Unknown.
|
|
|
|
{
|
|
|
|
name: "wheel with unknown bit",
|
|
|
|
buf: encode(0b0100_0010, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseUnknown,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unknown with modifier",
|
|
|
|
buf: encode(0b0100_1010, 32, 16),
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: 32,
|
|
|
|
Y: 16,
|
|
|
|
Type: MouseUnknown,
|
|
|
|
Alt: true,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// Overflow position.
|
|
|
|
{
|
|
|
|
name: "overflow position",
|
|
|
|
buf: encode(0b0010_0000, 250, 223), // Because 255 (max int8) - 32 - 1.
|
2022-10-21 11:00:39 -04:00
|
|
|
expected: MouseEvent{
|
|
|
|
X: -6,
|
|
|
|
Y: -33,
|
|
|
|
Type: MouseLeft,
|
2020-11-07 00:43:12 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range tt {
|
|
|
|
tc := tt[i]
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-10-21 11:00:39 -04:00
|
|
|
actual := parseX10MouseEvent(tc.buf)
|
2020-11-07 00:43:12 -05:00
|
|
|
|
2022-10-21 11:00:39 -04:00
|
|
|
if tc.expected != actual {
|
|
|
|
t.Fatalf("expected %#v but got %#v",
|
|
|
|
tc.expected,
|
|
|
|
actual,
|
|
|
|
)
|
2020-11-07 00:43:12 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|