| 1 |
package input |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/charmbracelet/x/ansi" |
| 8 |
"github.com/charmbracelet/x/ansi/parser" |
| 9 |
) |
| 10 |
|
| 11 |
func TestMouseEvent_String(t *testing.T) { |
| 12 |
tt := []struct { |
| 13 |
name string |
| 14 |
event Event |
| 15 |
expected string |
| 16 |
}{ |
| 17 |
{ |
| 18 |
name: "unknown", |
| 19 |
event: MouseClickEvent{Button: MouseButton(0xff)}, |
| 20 |
expected: "unknown", |
| 21 |
}, |
| 22 |
{ |
| 23 |
name: "left", |
| 24 |
event: MouseClickEvent{Button: MouseLeft}, |
| 25 |
expected: "left", |
| 26 |
}, |
| 27 |
{ |
| 28 |
name: "right", |
| 29 |
event: MouseClickEvent{Button: MouseRight}, |
| 30 |
expected: "right", |
| 31 |
}, |
| 32 |
{ |
| 33 |
name: "middle", |
| 34 |
event: MouseClickEvent{Button: MouseMiddle}, |
| 35 |
expected: "middle", |
| 36 |
}, |
| 37 |
{ |
| 38 |
name: "release", |
| 39 |
event: MouseReleaseEvent{Button: MouseNone}, |
| 40 |
expected: "", |
| 41 |
}, |
| 42 |
{ |
| 43 |
name: "wheelup", |
| 44 |
event: MouseWheelEvent{Button: MouseWheelUp}, |
| 45 |
expected: "wheelup", |
| 46 |
}, |
| 47 |
{ |
| 48 |
name: "wheeldown", |
| 49 |
event: MouseWheelEvent{Button: MouseWheelDown}, |
| 50 |
expected: "wheeldown", |
| 51 |
}, |
| 52 |
{ |
| 53 |
name: "wheelleft", |
| 54 |
event: MouseWheelEvent{Button: MouseWheelLeft}, |
| 55 |
expected: "wheelleft", |
| 56 |
}, |
| 57 |
{ |
| 58 |
name: "wheelright", |
| 59 |
event: MouseWheelEvent{Button: MouseWheelRight}, |
| 60 |
expected: "wheelright", |
| 61 |
}, |
| 62 |
{ |
| 63 |
name: "motion", |
| 64 |
event: MouseMotionEvent{Button: MouseNone}, |
| 65 |
expected: "motion", |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "shift+left", |
| 69 |
event: MouseReleaseEvent{Button: MouseLeft, Mod: ModShift}, |
| 70 |
expected: "shift+left", |
| 71 |
}, |
| 72 |
{ |
| 73 |
name: "shift+left", event: MouseClickEvent{Button: MouseLeft, Mod: ModShift}, |
| 74 |
expected: "shift+left", |
| 75 |
}, |
| 76 |
{ |
| 77 |
name: "ctrl+shift+left", |
| 78 |
event: MouseClickEvent{Button: MouseLeft, Mod: ModCtrl | ModShift}, |
| 79 |
expected: "ctrl+shift+left", |
| 80 |
}, |
| 81 |
{ |
| 82 |
name: "alt+left", |
| 83 |
event: MouseClickEvent{Button: MouseLeft, Mod: ModAlt}, |
| 84 |
expected: "alt+left", |
| 85 |
}, |
| 86 |
{ |
| 87 |
name: "ctrl+left", |
| 88 |
event: MouseClickEvent{Button: MouseLeft, Mod: ModCtrl}, |
| 89 |
expected: "ctrl+left", |
| 90 |
}, |
| 91 |
{ |
| 92 |
name: "ctrl+alt+left", |
| 93 |
event: MouseClickEvent{Button: MouseLeft, Mod: ModAlt | ModCtrl}, |
| 94 |
expected: "ctrl+alt+left", |
| 95 |
}, |
| 96 |
{ |
| 97 |
name: "ctrl+alt+shift+left", |
| 98 |
event: MouseClickEvent{Button: MouseLeft, Mod: ModAlt | ModCtrl | ModShift}, |
| 99 |
expected: "ctrl+alt+shift+left", |
| 100 |
}, |
| 101 |
{ |
| 102 |
name: "ignore coordinates", |
| 103 |
event: MouseClickEvent{X: 100, Y: 200, Button: MouseLeft}, |
| 104 |
expected: "left", |
| 105 |
}, |
| 106 |
{ |
| 107 |
name: "broken type", |
| 108 |
event: MouseClickEvent{Button: MouseButton(120)}, |
| 109 |
expected: "unknown", |
| 110 |
}, |
| 111 |
} |
| 112 |
|
| 113 |
for i := range tt { |
| 114 |
tc := tt[i] |
| 115 |
|
| 116 |
t.Run(tc.name, func(t *testing.T) { |
| 117 |
actual := fmt.Sprint(tc.event) |
| 118 |
|
| 119 |
if tc.expected != actual { |
| 120 |
t.Fatalf("expected %q but got %q", |
| 121 |
tc.expected, |
| 122 |
actual, |
| 123 |
) |
| 124 |
} |
| 125 |
}) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func TestParseX10MouseDownEvent(t *testing.T) { |
| 130 |
encode := func(b byte, x, y int) []byte { |
| 131 |
return []byte{ |
| 132 |
'\x1b', |
| 133 |
'[', |
| 134 |
'M', |
| 135 |
byte(32) + b, |
| 136 |
byte(x + 32 + 1), |
| 137 |
byte(y + 32 + 1), |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
tt := []struct { |
| 142 |
name string |
| 143 |
buf []byte |
| 144 |
expected Event |
| 145 |
}{ |
| 146 |
// Position. |
| 147 |
{ |
| 148 |
name: "zero position", |
| 149 |
buf: encode(0b0000_0000, 0, 0), |
| 150 |
expected: MouseClickEvent{X: 0, Y: 0, Button: MouseLeft}, |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "max position", |
| 154 |
buf: encode(0b0000_0000, 222, 222), // Because 255 (max int8) - 32 - 1. |
| 155 |
expected: MouseClickEvent{X: 222, Y: 222, Button: MouseLeft}, |
| 156 |
}, |
| 157 |
// Simple. |
| 158 |
{ |
| 159 |
name: "left", |
| 160 |
buf: encode(0b0000_0000, 32, 16), |
| 161 |
expected: MouseClickEvent{X: 32, Y: 16, Button: MouseLeft}, |
| 162 |
}, |
| 163 |
{ |
| 164 |
name: "left in motion", |
| 165 |
buf: encode(0b0010_0000, 32, 16), |
| 166 |
expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseLeft}, |
| 167 |
}, |
| 168 |
{ |
| 169 |
name: "middle", |
| 170 |
buf: encode(0b0000_0001, 32, 16), |
| 171 |
expected: MouseClickEvent{X: 32, Y: 16, Button: MouseMiddle}, |
| 172 |
}, |
| 173 |
{ |
| 174 |
name: "middle in motion", |
| 175 |
buf: encode(0b0010_0001, 32, 16), |
| 176 |
expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseMiddle}, |
| 177 |
}, |
| 178 |
{ |
| 179 |
name: "right", |
| 180 |
buf: encode(0b0000_0010, 32, 16), |
| 181 |
expected: MouseClickEvent{X: 32, Y: 16, Button: MouseRight}, |
| 182 |
}, |
| 183 |
{ |
| 184 |
name: "right in motion", |
| 185 |
buf: encode(0b0010_0010, 32, 16), |
| 186 |
expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseRight}, |
| 187 |
}, |
| 188 |
{ |
| 189 |
name: "motion", |
| 190 |
buf: encode(0b0010_0011, 32, 16), |
| 191 |
expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseNone}, |
| 192 |
}, |
| 193 |
{ |
| 194 |
name: "wheel up", |
| 195 |
buf: encode(0b0100_0000, 32, 16), |
| 196 |
expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelUp}, |
| 197 |
}, |
| 198 |
{ |
| 199 |
name: "wheel down", |
| 200 |
buf: encode(0b0100_0001, 32, 16), |
| 201 |
expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelDown}, |
| 202 |
}, |
| 203 |
{ |
| 204 |
name: "wheel left", |
| 205 |
buf: encode(0b0100_0010, 32, 16), |
| 206 |
expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelLeft}, |
| 207 |
}, |
| 208 |
{ |
| 209 |
name: "wheel right", |
| 210 |
buf: encode(0b0100_0011, 32, 16), |
| 211 |
expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelRight}, |
| 212 |
}, |
| 213 |
{ |
| 214 |
name: "release", |
| 215 |
buf: encode(0b0000_0011, 32, 16), |
| 216 |
expected: MouseReleaseEvent{X: 32, Y: 16, Button: MouseNone}, |
| 217 |
}, |
| 218 |
{ |
| 219 |
name: "backward", |
| 220 |
buf: encode(0b1000_0000, 32, 16), |
| 221 |
expected: MouseClickEvent{X: 32, Y: 16, Button: MouseBackward}, |
| 222 |
}, |
| 223 |
{ |
| 224 |
name: "forward", |
| 225 |
buf: encode(0b1000_0001, 32, 16), |
| 226 |
expected: MouseClickEvent{X: 32, Y: 16, Button: MouseForward}, |
| 227 |
}, |
| 228 |
{ |
| 229 |
name: "button 10", |
| 230 |
buf: encode(0b1000_0010, 32, 16), |
| 231 |
expected: MouseClickEvent{X: 32, Y: 16, Button: MouseButton10}, |
| 232 |
}, |
| 233 |
{ |
| 234 |
name: "button 11", |
| 235 |
buf: encode(0b1000_0011, 32, 16), |
| 236 |
expected: MouseClickEvent{X: 32, Y: 16, Button: MouseButton11}, |
| 237 |
}, |
| 238 |
// Combinations. |
| 239 |
{ |
| 240 |
name: "alt+right", |
| 241 |
buf: encode(0b0000_1010, 32, 16), |
| 242 |
expected: MouseClickEvent{X: 32, Y: 16, Mod: ModAlt, Button: MouseRight}, |
| 243 |
}, |
| 244 |
{ |
| 245 |
name: "ctrl+right", |
| 246 |
buf: encode(0b0001_0010, 32, 16), |
| 247 |
expected: MouseClickEvent{X: 32, Y: 16, Mod: ModCtrl, Button: MouseRight}, |
| 248 |
}, |
| 249 |
{ |
| 250 |
name: "left in motion", |
| 251 |
buf: encode(0b0010_0000, 32, 16), |
| 252 |
expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseLeft}, |
| 253 |
}, |
| 254 |
{ |
| 255 |
name: "alt+right in motion", |
| 256 |
buf: encode(0b0010_1010, 32, 16), |
| 257 |
expected: MouseMotionEvent{X: 32, Y: 16, Mod: ModAlt, Button: MouseRight}, |
| 258 |
}, |
| 259 |
{ |
| 260 |
name: "ctrl+right in motion", |
| 261 |
buf: encode(0b0011_0010, 32, 16), |
| 262 |
expected: MouseMotionEvent{X: 32, Y: 16, Mod: ModCtrl, Button: MouseRight}, |
| 263 |
}, |
| 264 |
{ |
| 265 |
name: "ctrl+alt+right", |
| 266 |
buf: encode(0b0001_1010, 32, 16), |
| 267 |
expected: MouseClickEvent{X: 32, Y: 16, Mod: ModAlt | ModCtrl, Button: MouseRight}, |
| 268 |
}, |
| 269 |
{ |
| 270 |
name: "ctrl+wheel up", |
| 271 |
buf: encode(0b0101_0000, 32, 16), |
| 272 |
expected: MouseWheelEvent{X: 32, Y: 16, Mod: ModCtrl, Button: MouseWheelUp}, |
| 273 |
}, |
| 274 |
{ |
| 275 |
name: "alt+wheel down", |
| 276 |
buf: encode(0b0100_1001, 32, 16), |
| 277 |
expected: MouseWheelEvent{X: 32, Y: 16, Mod: ModAlt, Button: MouseWheelDown}, |
| 278 |
}, |
| 279 |
{ |
| 280 |
name: "ctrl+alt+wheel down", |
| 281 |
buf: encode(0b0101_1001, 32, 16), |
| 282 |
expected: MouseWheelEvent{X: 32, Y: 16, Mod: ModAlt | ModCtrl, Button: MouseWheelDown}, |
| 283 |
}, |
| 284 |
// Overflow position. |
| 285 |
{ |
| 286 |
name: "overflow position", |
| 287 |
buf: encode(0b0010_0000, 250, 223), // Because 255 (max int8) - 32 - 1. |
| 288 |
expected: MouseMotionEvent{X: -6, Y: -33, Button: MouseLeft}, |
| 289 |
}, |
| 290 |
} |
| 291 |
|
| 292 |
for i := range tt { |
| 293 |
tc := tt[i] |
| 294 |
|
| 295 |
t.Run(tc.name, func(t *testing.T) { |
| 296 |
actual := parseX10MouseEvent(tc.buf) |
| 297 |
|
| 298 |
if tc.expected != actual { |
| 299 |
t.Fatalf("expected %#v but got %#v", |
| 300 |
tc.expected, |
| 301 |
actual, |
| 302 |
) |
| 303 |
} |
| 304 |
}) |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
func TestParseSGRMouseEvent(t *testing.T) { |
| 309 |
type csiSequence struct { |
| 310 |
params []ansi.Param |
| 311 |
cmd ansi.Cmd |
| 312 |
} |
| 313 |
encode := func(b, x, y int, r bool) *csiSequence { |
| 314 |
re := 'M' |
| 315 |
if r { |
| 316 |
re = 'm' |
| 317 |
} |
| 318 |
return &csiSequence{ |
| 319 |
params: []ansi.Param{ |
| 320 |
ansi.Param(b), |
| 321 |
ansi.Param(x + 1), |
| 322 |
ansi.Param(y + 1), |
| 323 |
}, |
| 324 |
cmd: ansi.Cmd(re) | ('<' << parser.PrefixShift), |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
tt := []struct { |
| 329 |
name string |
| 330 |
buf *csiSequence |
| 331 |
expected Event |
| 332 |
}{ |
| 333 |
// Position. |
| 334 |
{ |
| 335 |
name: "zero position", |
| 336 |
buf: encode(0, 0, 0, false), |
| 337 |
expected: MouseClickEvent{X: 0, Y: 0, Button: MouseLeft}, |
| 338 |
}, |
| 339 |
{ |
| 340 |
name: "225 position", |
| 341 |
buf: encode(0, 225, 225, false), |
| 342 |
expected: MouseClickEvent{X: 225, Y: 225, Button: MouseLeft}, |
| 343 |
}, |
| 344 |
// Simple. |
| 345 |
{ |
| 346 |
name: "left", |
| 347 |
buf: encode(0, 32, 16, false), |
| 348 |
expected: MouseClickEvent{X: 32, Y: 16, Button: MouseLeft}, |
| 349 |
}, |
| 350 |
{ |
| 351 |
name: "left in motion", |
| 352 |
buf: encode(32, 32, 16, false), |
| 353 |
expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseLeft}, |
| 354 |
}, |
| 355 |
{ |
| 356 |
name: "left", |
| 357 |
buf: encode(0, 32, 16, true), |
| 358 |
expected: MouseReleaseEvent{X: 32, Y: 16, Button: MouseLeft}, |
| 359 |
}, |
| 360 |
{ |
| 361 |
name: "middle", |
| 362 |
buf: encode(1, 32, 16, false), |
| 363 |
expected: MouseClickEvent{X: 32, Y: 16, Button: MouseMiddle}, |
| 364 |
}, |
| 365 |
{ |
| 366 |
name: "middle in motion", |
| 367 |
buf: encode(33, 32, 16, false), |
| 368 |
expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseMiddle}, |
| 369 |
}, |
| 370 |
{ |
| 371 |
name: "middle", |
| 372 |
buf: encode(1, 32, 16, true), |
| 373 |
expected: MouseReleaseEvent{X: 32, Y: 16, Button: MouseMiddle}, |
| 374 |
}, |
| 375 |
{ |
| 376 |
name: "right", |
| 377 |
buf: encode(2, 32, 16, false), |
| 378 |
expected: MouseClickEvent{X: 32, Y: 16, Button: MouseRight}, |
| 379 |
}, |
| 380 |
{ |
| 381 |
name: "right", |
| 382 |
buf: encode(2, 32, 16, true), |
| 383 |
expected: MouseReleaseEvent{X: 32, Y: 16, Button: MouseRight}, |
| 384 |
}, |
| 385 |
{ |
| 386 |
name: "motion", |
| 387 |
buf: encode(35, 32, 16, false), |
| 388 |
expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseNone}, |
| 389 |
}, |
| 390 |
{ |
| 391 |
name: "wheel up", |
| 392 |
buf: encode(64, 32, 16, false), |
| 393 |
expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelUp}, |
| 394 |
}, |
| 395 |
{ |
| 396 |
name: "wheel down", |
| 397 |
buf: encode(65, 32, 16, false), |
| 398 |
expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelDown}, |
| 399 |
}, |
| 400 |
{ |
| 401 |
name: "wheel left", |
| 402 |
buf: encode(66, 32, 16, false), |
| 403 |
expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelLeft}, |
| 404 |
}, |
| 405 |
{ |
| 406 |
name: "wheel right", |
| 407 |
buf: encode(67, 32, 16, false), |
| 408 |
expected: MouseWheelEvent{X: 32, Y: 16, Button: MouseWheelRight}, |
| 409 |
}, |
| 410 |
{ |
| 411 |
name: "backward", |
| 412 |
buf: encode(128, 32, 16, false), |
| 413 |
expected: MouseClickEvent{X: 32, Y: 16, Button: MouseBackward}, |
| 414 |
}, |
| 415 |
{ |
| 416 |
name: "backward in motion", |
| 417 |
buf: encode(160, 32, 16, false), |
| 418 |
expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseBackward}, |
| 419 |
}, |
| 420 |
{ |
| 421 |
name: "forward", |
| 422 |
buf: encode(129, 32, 16, false), |
| 423 |
expected: MouseClickEvent{X: 32, Y: 16, Button: MouseForward}, |
| 424 |
}, |
| 425 |
{ |
| 426 |
name: "forward in motion", |
| 427 |
buf: encode(161, 32, 16, false), |
| 428 |
expected: MouseMotionEvent{X: 32, Y: 16, Button: MouseForward}, |
| 429 |
}, |
| 430 |
// Combinations. |
| 431 |
{ |
| 432 |
name: "alt+right", |
| 433 |
buf: encode(10, 32, 16, false), |
| 434 |
expected: MouseClickEvent{X: 32, Y: 16, Mod: ModAlt, Button: MouseRight}, |
| 435 |
}, |
| 436 |
{ |
| 437 |
name: "ctrl+right", |
| 438 |
buf: encode(18, 32, 16, false), |
| 439 |
expected: MouseClickEvent{X: 32, Y: 16, Mod: ModCtrl, Button: MouseRight}, |
| 440 |
}, |
| 441 |
{ |
| 442 |
name: "ctrl+alt+right", |
| 443 |
buf: encode(26, 32, 16, false), |
| 444 |
expected: MouseClickEvent{X: 32, Y: 16, Mod: ModAlt | ModCtrl, Button: MouseRight}, |
| 445 |
}, |
| 446 |
{ |
| 447 |
name: "alt+wheel", |
| 448 |
buf: encode(73, 32, 16, false), |
| 449 |
expected: MouseWheelEvent{X: 32, Y: 16, Mod: ModAlt, Button: MouseWheelDown}, |
| 450 |
}, |
| 451 |
{ |
| 452 |
name: "ctrl+wheel", |
| 453 |
buf: encode(81, 32, 16, false), |
| 454 |
expected: MouseWheelEvent{X: 32, Y: 16, Mod: ModCtrl, Button: MouseWheelDown}, |
| 455 |
}, |
| 456 |
{ |
| 457 |
name: "ctrl+alt+wheel", |
| 458 |
buf: encode(89, 32, 16, false), |
| 459 |
expected: MouseWheelEvent{X: 32, Y: 16, Mod: ModAlt | ModCtrl, Button: MouseWheelDown}, |
| 460 |
}, |
| 461 |
{ |
| 462 |
name: "ctrl+alt+shift+wheel", |
| 463 |
buf: encode(93, 32, 16, false), |
| 464 |
expected: MouseWheelEvent{X: 32, Y: 16, Mod: ModAlt | ModShift | ModCtrl, Button: MouseWheelDown}, |
| 465 |
}, |
| 466 |
} |
| 467 |
|
| 468 |
for i := range tt { |
| 469 |
tc := tt[i] |
| 470 |
|
| 471 |
t.Run(tc.name, func(t *testing.T) { |
| 472 |
actual := parseSGRMouseEvent(tc.buf.cmd, tc.buf.params) |
| 473 |
if tc.expected != actual { |
| 474 |
t.Fatalf("expected %#v but got %#v", |
| 475 |
tc.expected, |
| 476 |
actual, |
| 477 |
) |
| 478 |
} |
| 479 |
}) |
| 480 |
} |
| 481 |
} |