Bram Moolenaar | 905dd90 | 2019-04-07 14:21:47 +0200 | [diff] [blame] | 1 | " Tests for decoding escape sequences sent by the terminal. |
| 2 | |
| 3 | " This only works for Unix in a terminal |
| 4 | if has('gui_running') || !has('unix') |
| 5 | finish |
| 6 | endif |
| 7 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 8 | " Helper function to emit a terminal escape code. |
| 9 | func TerminalEscapeCode(code_xterm, code_sgr, row, col, m) |
| 10 | if &ttymouse ==# 'xterm' |
| 11 | " need to use byte encoding here. |
| 12 | let str = list2str([a:code_xterm, a:col + 0x20, a:row + 0x20]) |
| 13 | if has('iconv') |
| 14 | let bytes = iconv(str, 'utf-8', 'latin1') |
| 15 | else |
| 16 | " Hopefully the numbers are not too big. |
| 17 | let bytes = str |
| 18 | endif |
| 19 | call feedkeys("\<Esc>[M" .. bytes, 'Lx!') |
| 20 | elseif &ttymouse ==# 'sgr' |
| 21 | call feedkeys(printf("\<Esc>[<%d;%d;%d%s", a:code_sgr, a:col, a:row, a:m), 'Lx!') |
| 22 | endif |
| 23 | endfunc |
| 24 | |
| 25 | func MouseLeftClick(row, col) |
| 26 | call TerminalEscapeCode(0x20, 0, a:row, a:col, 'M') |
| 27 | endfunc |
| 28 | |
| 29 | func MouseLeftRelease(row, col) |
| 30 | call TerminalEscapeCode(0x23, 3, a:row, a:col, 'm') |
| 31 | endfunc |
| 32 | |
| 33 | func MouseLeftDrag(row, col) |
| 34 | call TerminalEscapeCode(0x43, 0x20, a:row, a:col, 'M') |
| 35 | endfunc |
| 36 | |
| 37 | func MouseWheelUp(row, col) |
| 38 | call TerminalEscapeCode(0x40, 0x40, a:row, a:col, 'M') |
| 39 | endfunc |
| 40 | |
| 41 | func MouseWheelDown(row, col) |
| 42 | call TerminalEscapeCode(0x41, 0x41, a:row, a:col, 'M') |
| 43 | endfunc |
| 44 | |
Bram Moolenaar | 905dd90 | 2019-04-07 14:21:47 +0200 | [diff] [blame] | 45 | func Test_xterm_mouse_click() |
| 46 | new |
| 47 | let save_mouse = &mouse |
| 48 | let save_term = &term |
| 49 | let save_ttymouse = &ttymouse |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 50 | set mouse=a term=xterm |
Bram Moolenaar | 905dd90 | 2019-04-07 14:21:47 +0200 | [diff] [blame] | 51 | call setline(1, ['line 1', 'line 2', 'line 3 is a bit longer']) |
Bram Moolenaar | 905dd90 | 2019-04-07 14:21:47 +0200 | [diff] [blame] | 52 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 53 | for ttymouse_val in ['xterm', 'sgr'] |
| 54 | exe 'set ttymouse=' . ttymouse_val |
| 55 | go |
| 56 | call assert_equal([0, 1, 1, 0], getpos('.')) |
| 57 | let row = 2 |
| 58 | let col = 6 |
| 59 | call MouseLeftClick(row, col) |
| 60 | call MouseLeftRelease(row, col) |
| 61 | call assert_equal([0, 2, 6, 0], getpos('.')) |
| 62 | endfor |
Bram Moolenaar | 905dd90 | 2019-04-07 14:21:47 +0200 | [diff] [blame] | 63 | |
| 64 | let &mouse = save_mouse |
| 65 | let &term = save_term |
| 66 | let &ttymouse = save_ttymouse |
| 67 | bwipe! |
| 68 | endfunc |
| 69 | |
Bram Moolenaar | 049736f | 2019-04-07 21:55:07 +0200 | [diff] [blame] | 70 | func Test_xterm_mouse_wheel() |
| 71 | new |
| 72 | let save_mouse = &mouse |
| 73 | let save_term = &term |
| 74 | let save_ttymouse = &ttymouse |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 75 | set mouse=a term=xterm |
Bram Moolenaar | 049736f | 2019-04-07 21:55:07 +0200 | [diff] [blame] | 76 | call setline(1, range(1, 100)) |
| 77 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 78 | for ttymouse_val in ['xterm', 'sgr'] |
| 79 | exe 'set ttymouse=' . ttymouse_val |
| 80 | go |
| 81 | call assert_equal(1, line('w0')) |
| 82 | call assert_equal([0, 1, 1, 0], getpos('.')) |
Bram Moolenaar | 049736f | 2019-04-07 21:55:07 +0200 | [diff] [blame] | 83 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 84 | call MouseWheelDown(1, 1) |
| 85 | call assert_equal(4, line('w0')) |
| 86 | call assert_equal([0, 4, 1, 0], getpos('.')) |
Bram Moolenaar | 049736f | 2019-04-07 21:55:07 +0200 | [diff] [blame] | 87 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 88 | call MouseWheelDown(1, 1) |
| 89 | call assert_equal(7, line('w0')) |
| 90 | call assert_equal([0, 7, 1, 0], getpos('.')) |
Bram Moolenaar | 049736f | 2019-04-07 21:55:07 +0200 | [diff] [blame] | 91 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 92 | call MouseWheelUp(1, 1) |
| 93 | call assert_equal(4, line('w0')) |
| 94 | call assert_equal([0, 7, 1, 0], getpos('.')) |
Bram Moolenaar | 049736f | 2019-04-07 21:55:07 +0200 | [diff] [blame] | 95 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 96 | call MouseWheelUp(1, 1) |
| 97 | call assert_equal(1, line('w0')) |
| 98 | call assert_equal([0, 7, 1, 0], getpos('.')) |
| 99 | endfor |
Bram Moolenaar | 049736f | 2019-04-07 21:55:07 +0200 | [diff] [blame] | 100 | |
| 101 | let &mouse = save_mouse |
| 102 | let &term = save_term |
| 103 | let &ttymouse = save_ttymouse |
| 104 | bwipe! |
| 105 | endfunc |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 106 | |
| 107 | func Test_xterm_mouse_drag_window_separator() |
| 108 | let save_mouse = &mouse |
| 109 | let save_term = &term |
| 110 | let save_ttymouse = &ttymouse |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 111 | set mouse=a term=xterm |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 112 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 113 | for ttymouse_val in ['xterm', 'sgr'] |
| 114 | exe 'set ttymouse=' . ttymouse_val |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 115 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 116 | " Split horizontally and test dragging the horizontal window separator. |
| 117 | split |
| 118 | let rowseparator = winheight(0) + 1 |
| 119 | let row = rowseparator |
| 120 | let col = 1 |
| 121 | call MouseLeftClick(row, col) |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 122 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 123 | let row -= 1 |
| 124 | call MouseLeftDrag(row, col) |
| 125 | call assert_equal(rowseparator - 1, winheight(0) + 1) |
| 126 | let row += 1 |
| 127 | call MouseLeftDrag(row, col) |
| 128 | call assert_equal(rowseparator, winheight(0) + 1) |
| 129 | call MouseLeftRelease(row, col) |
| 130 | call assert_equal(rowseparator, winheight(0) + 1) |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 131 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 132 | bwipe! |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 133 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 134 | " Split vertically and test dragging the vertical window separator. |
| 135 | vsplit |
| 136 | let colseparator = winwidth(0) + 1 |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 137 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 138 | let row = 1 |
| 139 | let col = colseparator |
| 140 | call MouseLeftClick(row, col) |
| 141 | let col -= 1 |
| 142 | call MouseLeftDrag(row, col) |
| 143 | call assert_equal(colseparator - 1, winwidth(0) + 1) |
| 144 | let col += 1 |
| 145 | call MouseLeftDrag(row, col) |
| 146 | call assert_equal(colseparator, winwidth(0) + 1) |
| 147 | call MouseLeftRelease(row, col) |
| 148 | call assert_equal(colseparator, winwidth(0) + 1) |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 149 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 150 | bwipe! |
| 151 | endfor |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 152 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 153 | let &mouse = save_mouse |
| 154 | let &term = save_term |
| 155 | let &ttymouse = save_ttymouse |
| 156 | endfunc |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 157 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 158 | func Test_xterm_mouse_drag_statusline() |
| 159 | let save_mouse = &mouse |
| 160 | let save_term = &term |
| 161 | let save_ttymouse = &ttymouse |
| 162 | set mouse=a term=xterm |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 163 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 164 | for ttymouse_val in ['xterm', 'sgr'] |
| 165 | exe 'set ttymouse=' . ttymouse_val |
| 166 | |
| 167 | call assert_equal(1, &cmdheight) |
| 168 | let rowstatusline = winheight(0) + 1 |
| 169 | let row = rowstatusline |
| 170 | let col = 1 |
| 171 | call MouseLeftClick(row, col) |
| 172 | let row -= 1 |
| 173 | call MouseLeftDrag(row, col) |
| 174 | call assert_equal(2, &cmdheight) |
| 175 | call assert_equal(rowstatusline - 1, winheight(0) + 1) |
| 176 | let row += 1 |
| 177 | call MouseLeftDrag(row, col) |
| 178 | call assert_equal(1, &cmdheight) |
| 179 | call assert_equal(rowstatusline, winheight(0) + 1) |
| 180 | call MouseLeftRelease(row, col) |
| 181 | call assert_equal(1, &cmdheight) |
| 182 | call assert_equal(rowstatusline, winheight(0) + 1) |
| 183 | endfor |
| 184 | |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 185 | let &mouse = save_mouse |
| 186 | let &term = save_term |
| 187 | let &ttymouse = save_ttymouse |
| 188 | endfunc |