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 |
Bram Moolenaar | c8b3dda | 2019-04-12 21:42:52 +0200 | [diff] [blame] | 121 | |
Bram Moolenaar | 39f76c6 | 2019-04-13 22:13:23 +0200 | [diff] [blame] | 122 | " When 'ttymouse' is 'xterm', row/col bigger than 223 are not supported. |
| 123 | if ttymouse_val !=# 'xterm' || row <= 223 |
| 124 | call MouseLeftClick(row, col) |
| 125 | let row -= 1 |
| 126 | call MouseLeftDrag(row, col) |
| 127 | call assert_equal(rowseparator - 1, winheight(0) + 1) |
| 128 | let row += 1 |
| 129 | call MouseLeftDrag(row, col) |
| 130 | call assert_equal(rowseparator, winheight(0) + 1) |
| 131 | call MouseLeftRelease(row, col) |
| 132 | call assert_equal(rowseparator, winheight(0) + 1) |
Bram Moolenaar | c8b3dda | 2019-04-12 21:42:52 +0200 | [diff] [blame] | 133 | endif |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 134 | bwipe! |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 135 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 136 | " Split vertically and test dragging the vertical window separator. |
| 137 | vsplit |
| 138 | let colseparator = winwidth(0) + 1 |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 139 | let row = 1 |
| 140 | let col = colseparator |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 141 | |
Bram Moolenaar | 39f76c6 | 2019-04-13 22:13:23 +0200 | [diff] [blame] | 142 | " When 'ttymouse' is 'xterm', row/col bigger than 223 are not supported. |
| 143 | if ttymouse_val !=# 'xterm' || col <= 223 |
| 144 | call MouseLeftClick(row, col) |
| 145 | let col -= 1 |
| 146 | call MouseLeftDrag(row, col) |
| 147 | call assert_equal(colseparator - 1, winwidth(0) + 1) |
| 148 | let col += 1 |
| 149 | call MouseLeftDrag(row, col) |
| 150 | call assert_equal(colseparator, winwidth(0) + 1) |
| 151 | call MouseLeftRelease(row, col) |
| 152 | call assert_equal(colseparator, winwidth(0) + 1) |
| 153 | endif |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 154 | bwipe! |
| 155 | endfor |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 156 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 157 | let &mouse = save_mouse |
| 158 | let &term = save_term |
| 159 | let &ttymouse = save_ttymouse |
| 160 | endfunc |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 161 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 162 | func Test_xterm_mouse_drag_statusline() |
| 163 | let save_mouse = &mouse |
| 164 | let save_term = &term |
| 165 | let save_ttymouse = &ttymouse |
Bram Moolenaar | ca57ab5 | 2019-04-13 14:53:16 +0200 | [diff] [blame] | 166 | let save_laststatus = &laststatus |
| 167 | set mouse=a term=xterm laststatus=2 |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 168 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 169 | for ttymouse_val in ['xterm', 'sgr'] |
| 170 | exe 'set ttymouse=' . ttymouse_val |
| 171 | |
| 172 | call assert_equal(1, &cmdheight) |
| 173 | let rowstatusline = winheight(0) + 1 |
| 174 | let row = rowstatusline |
| 175 | let col = 1 |
Bram Moolenaar | c8b3dda | 2019-04-12 21:42:52 +0200 | [diff] [blame] | 176 | |
| 177 | if ttymouse_val ==# 'xterm' && row > 223 |
| 178 | " When 'ttymouse' is 'xterm', row/col bigger than 223 are not supported. |
| 179 | continue |
| 180 | endif |
| 181 | |
Bram Moolenaar | 3fbd2d7 | 2019-04-11 23:56:16 +0200 | [diff] [blame] | 182 | call MouseLeftClick(row, col) |
| 183 | let row -= 1 |
| 184 | call MouseLeftDrag(row, col) |
| 185 | call assert_equal(2, &cmdheight) |
| 186 | call assert_equal(rowstatusline - 1, winheight(0) + 1) |
| 187 | let row += 1 |
| 188 | call MouseLeftDrag(row, col) |
| 189 | call assert_equal(1, &cmdheight) |
| 190 | call assert_equal(rowstatusline, winheight(0) + 1) |
| 191 | call MouseLeftRelease(row, col) |
| 192 | call assert_equal(1, &cmdheight) |
| 193 | call assert_equal(rowstatusline, winheight(0) + 1) |
| 194 | endfor |
| 195 | |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 196 | let &mouse = save_mouse |
| 197 | let &term = save_term |
| 198 | let &ttymouse = save_ttymouse |
Bram Moolenaar | ca57ab5 | 2019-04-13 14:53:16 +0200 | [diff] [blame] | 199 | let &laststatus = save_laststatus |
| 200 | endfunc |
| 201 | |
| 202 | func Test_xterm_mouse_click_tab() |
| 203 | let save_mouse = &mouse |
| 204 | let save_term = &term |
| 205 | let save_ttymouse = &ttymouse |
| 206 | set mouse=a term=xterm |
| 207 | let row = 1 |
| 208 | |
| 209 | for ttymouse_val in ['xterm', 'sgr'] |
| 210 | exe 'set ttymouse=' . ttymouse_val |
| 211 | e Xfoo |
| 212 | tabnew Xbar |
| 213 | |
| 214 | let a = split(execute(':tabs'), "\n") |
| 215 | call assert_equal(['Tab page 1', |
| 216 | \ ' Xfoo', |
| 217 | \ 'Tab page 2', |
| 218 | \ '> Xbar'], a) |
| 219 | |
| 220 | " Test clicking on tab names in the tabline at the top. |
| 221 | let col = 2 |
Bram Moolenaar | 39f76c6 | 2019-04-13 22:13:23 +0200 | [diff] [blame] | 222 | redraw |
Bram Moolenaar | ca57ab5 | 2019-04-13 14:53:16 +0200 | [diff] [blame] | 223 | call MouseLeftClick(row, col) |
| 224 | call MouseLeftRelease(row, col) |
| 225 | let a = split(execute(':tabs'), "\n") |
| 226 | call assert_equal(['Tab page 1', |
| 227 | \ '> Xfoo', |
| 228 | \ 'Tab page 2', |
| 229 | \ ' Xbar'], a) |
| 230 | |
| 231 | let col = 9 |
| 232 | call MouseLeftClick(row, col) |
| 233 | call MouseLeftRelease(row, col) |
| 234 | let a = split(execute(':tabs'), "\n") |
| 235 | call assert_equal(['Tab page 1', |
| 236 | \ ' Xfoo', |
| 237 | \ 'Tab page 2', |
| 238 | \ '> Xbar'], a) |
| 239 | |
| 240 | %bwipe! |
| 241 | endfor |
| 242 | |
| 243 | let &mouse = save_mouse |
| 244 | let &term = save_term |
| 245 | let &ttymouse = save_ttymouse |
Bram Moolenaar | 3fb01a5 | 2019-04-09 21:52:02 +0200 | [diff] [blame] | 246 | endfunc |
Bram Moolenaar | 39f76c6 | 2019-04-13 22:13:23 +0200 | [diff] [blame] | 247 | |
| 248 | func Test_xterm_mouse_click_X_to_close_tab() |
| 249 | let save_mouse = &mouse |
| 250 | let save_term = &term |
| 251 | let save_ttymouse = &ttymouse |
| 252 | set mouse=a term=xterm |
| 253 | let row = 1 |
| 254 | let col = &columns |
| 255 | |
| 256 | for ttymouse_val in ['xterm', 'sgr'] |
| 257 | if ttymouse_val ==# 'xterm' && col > 223 |
| 258 | " When 'ttymouse' is 'xterm', row/col bigger than 223 are not supported. |
| 259 | continue |
| 260 | endif |
| 261 | exe 'set ttymouse=' . ttymouse_val |
| 262 | e Xtab1 |
| 263 | tabnew Xtab2 |
| 264 | tabnew Xtab3 |
| 265 | tabn 2 |
| 266 | |
| 267 | let a = split(execute(':tabs'), "\n") |
| 268 | call assert_equal(['Tab page 1', |
| 269 | \ ' Xtab1', |
| 270 | \ 'Tab page 2', |
| 271 | \ '> Xtab2', |
| 272 | \ 'Tab page 3', |
| 273 | \ ' Xtab3'], a) |
| 274 | |
| 275 | " Click on "X" in tabline to close current tab i.e. Xtab2. |
| 276 | redraw |
| 277 | call MouseLeftClick(row, col) |
| 278 | call MouseLeftRelease(row, col) |
| 279 | let a = split(execute(':tabs'), "\n") |
| 280 | call assert_equal(['Tab page 1', |
| 281 | \ ' Xtab1', |
| 282 | \ 'Tab page 2', |
| 283 | \ '> Xtab3'], a) |
| 284 | |
| 285 | %bwipe! |
| 286 | endfor |
| 287 | |
| 288 | let &mouse = save_mouse |
| 289 | let &term = save_term |
| 290 | let &ttymouse = save_ttymouse |
| 291 | endfunc |
Bram Moolenaar | e3e3828 | 2019-04-15 20:55:31 +0200 | [diff] [blame] | 292 | |
| 293 | func Test_xterm_mouse_drag_to_move_tab() |
| 294 | let save_mouse = &mouse |
| 295 | let save_term = &term |
| 296 | let save_ttymouse = &ttymouse |
| 297 | " Set 'mousetime' to 1 to avoid recognizing a double-click in the loop |
| 298 | set mouse=a term=xterm mousetime=1 |
| 299 | let row = 1 |
| 300 | |
| 301 | for ttymouse_val in ['xterm', 'sgr'] |
| 302 | exe 'set ttymouse=' . ttymouse_val |
| 303 | e Xtab1 |
| 304 | tabnew Xtab2 |
| 305 | |
| 306 | let a = split(execute(':tabs'), "\n") |
| 307 | call assert_equal(['Tab page 1', |
| 308 | \ ' Xtab1', |
| 309 | \ 'Tab page 2', |
| 310 | \ '> Xtab2'], a) |
| 311 | redraw |
| 312 | |
| 313 | " Click in tab2 and drag it to tab1. |
| 314 | " Check getcharmod() to verify that click is not |
| 315 | " interpreted as a spurious double-click. |
| 316 | call MouseLeftClick(row, 10) |
| 317 | call assert_equal(0, getcharmod()) |
| 318 | for col in [9, 8, 7, 6] |
| 319 | call MouseLeftDrag(row, col) |
| 320 | endfor |
| 321 | call MouseLeftRelease(row, col) |
| 322 | let a = split(execute(':tabs'), "\n") |
| 323 | call assert_equal(['Tab page 1', |
| 324 | \ '> Xtab2', |
| 325 | \ 'Tab page 2', |
| 326 | \ ' Xtab1'], a) |
| 327 | |
Bram Moolenaar | 7f27976 | 2019-04-15 21:48:22 +0200 | [diff] [blame] | 328 | " brief sleep to avoid causing a double-click |
| 329 | sleep 20m |
Bram Moolenaar | e3e3828 | 2019-04-15 20:55:31 +0200 | [diff] [blame] | 330 | %bwipe! |
| 331 | endfor |
| 332 | |
| 333 | let &mouse = save_mouse |
| 334 | let &term = save_term |
| 335 | let &ttymouse = save_ttymouse |
| 336 | set mousetime& |
| 337 | endfunc |
| 338 | |
| 339 | func Test_xterm_mouse_double_click_to_create_tab() |
| 340 | let save_mouse = &mouse |
| 341 | let save_term = &term |
| 342 | let save_ttymouse = &ttymouse |
| 343 | " Set 'mousetime' to a small value, so that double-click works but we don't |
| 344 | " have to wait long to avoid a triple-click. |
| 345 | set mouse=a term=xterm mousetime=100 |
| 346 | let row = 1 |
| 347 | let col = 10 |
| 348 | |
| 349 | for ttymouse_val in ['xterm', 'sgr'] |
| 350 | exe 'set ttymouse=' . ttymouse_val |
| 351 | e Xtab1 |
| 352 | tabnew Xtab2 |
| 353 | |
| 354 | let a = split(execute(':tabs'), "\n") |
| 355 | call assert_equal(['Tab page 1', |
| 356 | \ ' Xtab1', |
| 357 | \ 'Tab page 2', |
| 358 | \ '> Xtab2'], a) |
| 359 | |
| 360 | redraw |
| 361 | call MouseLeftClick(row, col) |
| 362 | " Check getcharmod() to verify that first click is not |
| 363 | " interpreted as a spurious double-click. |
| 364 | call assert_equal(0, getcharmod()) |
| 365 | call MouseLeftRelease(row, col) |
| 366 | call MouseLeftClick(row, col) |
| 367 | call assert_equal(32, getcharmod()) " double-click |
| 368 | call MouseLeftRelease(row, col) |
| 369 | let a = split(execute(':tabs'), "\n") |
| 370 | call assert_equal(['Tab page 1', |
| 371 | \ ' Xtab1', |
| 372 | \ 'Tab page 2', |
| 373 | \ '> [No Name]', |
| 374 | \ 'Tab page 3', |
| 375 | \ ' Xtab2'], a) |
| 376 | |
| 377 | if ttymouse_val !=# 'sgr' |
| 378 | " We need to sleep, or else MouseLeftClick() in next loop |
| 379 | " iteration will be interpreted as a spurious triple-click. |
| 380 | sleep 100m |
| 381 | endif |
| 382 | %bwipe! |
| 383 | endfor |
| 384 | |
| 385 | let &mouse = save_mouse |
| 386 | let &term = save_term |
| 387 | let &ttymouse = save_ttymouse |
| 388 | set mousetime& |
| 389 | endfunc |