From 8e4c74f4f8ffd9cf5d9fdefb8dff1bcbd9cd9fbd Mon Sep 17 00:00:00 2001 From: Adam Saponara Date: Sat, 4 Jun 2022 18:43:54 -0400 Subject: [PATCH] convert test_mod from xvkdb- to memfd-based test (was flaky) --- tests/test_mod/expected.ansi | 16 ++++----- tests/test_mod/test.php | 67 +++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/tests/test_mod/expected.ansi b/tests/test_mod/expected.ansi index f93d095..07d9a3e 100644 --- a/tests/test_mod/expected.ansi +++ b/tests/test_mod/expected.ansi @@ -1,11 +1,11 @@ -#5 \[Up] -> key=65517 mod=0 -#5 \S\[Up] -> key=65517 mod=4 -#5 \A\[Up] -> key=65517 mod=1 -#5 \A\S\[Up] -> key=65517 mod=5 -#5 \C\[Up] -> key=65517 mod=2 -#5 \C\S\[Up] -> key=65517 mod=6 -#5 \C\A\[Up] -> key=65517 mod=3 -#5 \C\A\S\[Up] -> key=65517 mod=7 +#5event=1,4,65517,0 +#5event=1,1,65517,0 +#5event=1,5,65517,0 +#5event=1,2,65517,0 +#5event=1,6,65517,0 +#5event=1,3,65517,0 +#5event=1,7,65517,0 + diff --git a/tests/test_mod/test.php b/tests/test_mod/test.php index aab5d15..7731ba2 100755 --- a/tests/test_mod/test.php +++ b/tests/test_mod/test.php @@ -1,37 +1,48 @@ ffi->tb_init(); - -$w = $test->ffi->tb_width(); -$h = $test->ffi->tb_height(); +// init termbox with a "fake" tty backed by memfds +$libc = FFI::cdef( + 'int memfd_create(const char *name, unsigned int flags);' . + 'int close(int fd);' +); +$ttyin = $libc->memfd_create('ttyin', 0); +$ttyout = $libc->memfd_create('ttyout', 0); +$test->ffi->tb_init_rwfd($ttyin, $ttyout); +$input_data = + "\x1b[1;2A" . // TB_KEY_ARROW_UP, TB_MOD_SHIFT + "\x1b[1;3A" . // TB_KEY_ARROW_UP, TB_MOD_ALT + "\x1b[1;4A" . // TB_KEY_ARROW_UP, TB_MOD_ALT | TB_MOD_SHIFT + "\x1b[1;5A" . // TB_KEY_ARROW_UP, TB_MOD_CTRL + "\x1b[1;6A" . // TB_KEY_ARROW_UP, TB_MOD_CTRL | TB_MOD_SHIFT + "\x1b[1;7A" . // TB_KEY_ARROW_UP, TB_MOD_CTRL | TB_MOD_ALT + "\x1b[1;8A" ; // TB_KEY_ARROW_UP, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT +$fttyin = fopen("php://fd/$ttyin", 'w'); +$nbytes = fwrite($fttyin, $input_data); +fseek($fttyin, strlen($input_data) * -1, SEEK_CUR); -$y = 0; -foreach (['\[Up]'] as $key) { - foreach ([false, true] as $mod_ctrl) { - foreach ([false, true] as $mod_alt) { - foreach ([false, true] as $mod_shift) { - $xvkbd_cmd = ''; - if ($mod_ctrl) $xvkbd_cmd .= '\C'; - if ($mod_alt) $xvkbd_cmd .= '\A'; - if ($mod_shift) $xvkbd_cmd .= '\S'; - $xvkbd_cmd .= $key; - $test->xvkbd($xvkbd_cmd); +// record events that termbox emits +$events = []; +$test->ffi->tb_set_input_mode($test->defines['TB_INPUT_ALT']); +$e = $test->ffi->new('struct tb_event'); +do { + $rv = $test->ffi->tb_peek_event(FFI::addr($e), 1000); + if ($rv == 0) { + $events[] = [ $e->type, $e->mod, $e->key, $e->ch ]; + } +} while ($rv == 0); - $event = $test->ffi->new('struct tb_event'); - $rv = $test->ffi->tb_peek_event(FFI::addr($event), 1000); +// close fake termbox setup +fclose($fttyin); +$libc->close($ttyin); +$libc->close($ttyout); +$test->ffi->tb_shutdown(); - $test->ffi->tb_printf(0, $y, 0, 0, "%16s -> key=%d mod=%d", - $xvkbd_cmd, - $event->key, - $event->mod, - ); - $y += 1; - } - } - } +// display events +$test->ffi->tb_init(); +$y = 0; +foreach ($events as $e) { + $test->ffi->tb_printf(0, $y++, 0, 0, "event=%s", implode(',', $e)); } - $test->ffi->tb_present(); - $test->screencap(); -- 2.39.5