blob: add7678018fda54011bf358a51a507bdcf3ec13d [file] [log] [blame]
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02001" Tests for the terminal window.
2" This is split in two, because it can take a lot of time.
3" See test_terminal.vim and test_terminal2.vim for further tests.
4
5source check.vim
6CheckFeature terminal
7
8source shared.vim
9source screendump.vim
10source mouse.vim
11source term_util.vim
12
13let $PROMPT_COMMAND=''
14
15func Test_terminal_altscreen()
16 " somehow doesn't work on MS-Windows
17 CheckUnix
18 let cmd = "cat Xtext\<CR>"
19
20 let buf = term_start(&shell, {})
21 call writefile(["\<Esc>[?1047h"], 'Xtext')
22 call term_sendkeys(buf, cmd)
23 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
24
25 call writefile(["\<Esc>[?1047l"], 'Xtext')
26 call term_sendkeys(buf, cmd)
27 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
28
29 call term_sendkeys(buf, "exit\r")
30 exe buf . "bwipe!"
31 call delete('Xtext')
32endfunc
33
34func Test_terminal_shell_option()
35 if has('unix')
36 " exec is a shell builtin command, should fail without a shell.
37 term exec ls runtest.vim
38 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
39 bwipe!
40
41 term ++shell exec ls runtest.vim
42 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
43 bwipe!
44 elseif has('win32')
45 " dir is a shell builtin command, should fail without a shell.
46 try
47 term dir /b runtest.vim
48 call WaitForAssert({-> assert_match('job failed\|cannot access .*: No such file or directory', term_getline(bufnr(), 1))})
49 catch /CreateProcess/
50 " ignore
51 endtry
52 bwipe!
53
54 term ++shell dir /b runtest.vim
55 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
56 bwipe!
57 endif
58endfunc
59
60func Test_terminal_invalid_arg()
61 call assert_fails('terminal ++xyz', 'E181:')
62endfunc
63
64func Test_terminal_in_popup()
65 CheckRunVimInTerminal
66
67 let text =<< trim END
68 some text
69 to edit
70 in a popup window
71 END
72 call writefile(text, 'Xtext')
73 let cmd = GetVimCommandCleanTerm()
74 let lines = [
75 \ 'call setline(1, range(20))',
76 \ 'hi PopTerm ctermbg=grey',
77 \ 'func OpenTerm(setColor)',
78 \ " set noruler",
79 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
80 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
81 \ ' if a:setColor',
82 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
83 \ ' endif',
84 \ 'endfunc',
85 \ 'func HidePopup()',
86 \ ' call popup_hide(g:winid)',
87 \ 'endfunc',
88 \ 'func ClosePopup()',
89 \ ' call popup_close(g:winid)',
90 \ 'endfunc',
91 \ 'func ReopenPopup()',
92 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
93 \ 'endfunc',
94 \ ]
95 call writefile(lines, 'XtermPopup')
96 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
97 call TermWait(buf, 100)
98 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
99 call TermWait(buf, 100)
100 call term_sendkeys(buf, ":\<CR>")
101 call TermWait(buf, 100)
102 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
103 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
104
105 call term_sendkeys(buf, ":q\<CR>")
106 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
107
108 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
109 call TermWait(buf, 150)
110 call term_sendkeys(buf, ":set hlsearch\<CR>")
111 call TermWait(buf, 100)
112 call term_sendkeys(buf, "/edit\<CR>")
113 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
114
115 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
116 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
117 call term_sendkeys(buf, "\<CR>")
118 call TermWait(buf, 50)
119
120 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
121 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
122
123 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
124 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
125
126 " Go to terminal-Normal mode and visually select text.
127 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
128 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
129
130 " Back to job mode, redraws
131 call term_sendkeys(buf, "A")
132 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
133
134 call TermWait(buf, 50)
135 call term_sendkeys(buf, ":q\<CR>")
136 call TermWait(buf, 150) " wait for terminal to vanish
137
138 call StopVimInTerminal(buf)
139 call delete('Xtext')
140 call delete('XtermPopup')
141endfunc
142
143" Check a terminal in popup window uses the default mininum size.
144func Test_terminal_in_popup_min_size()
145 CheckRunVimInTerminal
146
147 let text =<< trim END
148 another text
149 to show
150 in a popup window
151 END
152 call writefile(text, 'Xtext')
153 let lines = [
154 \ 'call setline(1, range(20))',
155 \ 'func OpenTerm()',
156 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
157 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
158 \ 'endfunc',
159 \ ]
160 call writefile(lines, 'XtermPopup')
161 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
162 call TermWait(buf, 100)
163 call term_sendkeys(buf, ":set noruler\<CR>")
164 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
165 call TermWait(buf, 50)
166 call term_sendkeys(buf, ":\<CR>")
167 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
168
169 call TermWait(buf, 50)
170 call term_sendkeys(buf, ":q\<CR>")
171 call TermWait(buf, 50) " wait for terminal to vanish
172 call StopVimInTerminal(buf)
173 call delete('Xtext')
174 call delete('XtermPopup')
175endfunc
176
177" Check a terminal in popup window with different colors
178func Terminal_in_popup_colored(group_name, highlight_cmd, highlight_opt)
179 CheckRunVimInTerminal
180 CheckUnix
181
182 let lines = [
183 \ 'call setline(1, range(20))',
184 \ 'func OpenTerm()',
185 \ " let s:buf = term_start('cat', #{hidden: 1, "
186 \ .. a:highlight_opt .. "})",
187 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
188 \ 'endfunc',
189 \ a:highlight_cmd,
190 \ ]
191 call writefile(lines, 'XtermPopup')
192 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
193 call TermWait(buf, 100)
194 call term_sendkeys(buf, ":set noruler\<CR>")
195 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
196 call TermWait(buf, 50)
197 call term_sendkeys(buf, "hello\<CR>")
198 call VerifyScreenDump(buf, 'Test_terminal_popup_' .. a:group_name, {})
199
200 call term_sendkeys(buf, "\<C-D>")
201 call TermWait(buf, 50)
202 call term_sendkeys(buf, ":q\<CR>")
203 call TermWait(buf, 50) " wait for terminal to vanish
204 call StopVimInTerminal(buf)
205 call delete('XtermPopup')
206endfunc
207
208func Test_terminal_in_popup_colored_Terminal()
209 call Terminal_in_popup_colored("Terminal", "highlight Terminal ctermfg=blue ctermbg=yellow", "")
210endfunc
211
212func Test_terminal_in_popup_colored_group()
213 call Terminal_in_popup_colored("MyTermCol", "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue", "term_highlight: 'MyTermCol',")
214endfunc
215
216func Test_double_popup_terminal()
217 let buf1 = term_start(&shell, #{hidden: 1})
218 let win1 = popup_create(buf1, {})
219 let buf2 = term_start(&shell, #{hidden: 1})
220 call assert_fails('call popup_create(buf2, {})', 'E861:')
221 call popup_close(win1)
222 exe buf1 .. 'bwipe!'
223 exe buf2 .. 'bwipe!'
224endfunc
225
226func Test_issue_5607()
227 let wincount = winnr('$')
228 exe 'terminal' &shell &shellcmdflag 'exit'
229 let job = term_getjob(bufnr())
230 call WaitForAssert({-> assert_equal("dead", job_status(job))})
231
232 let old_wincolor = &wincolor
233 try
234 set wincolor=
235 finally
236 let &wincolor = old_wincolor
237 bw!
238 endtry
239endfunc
240
241func Test_hidden_terminal()
242 let buf = term_start(&shell, #{hidden: 1})
243 call assert_equal('', bufname('^$'))
244 call StopShellInTerminal(buf)
245endfunc
246
247func Test_term_nasty_callback()
248 CheckExecutable sh
249
250 set hidden
251 let g:buf0 = term_start('sh', #{hidden: 1, term_finish: 'close'})
252 call popup_create(g:buf0, {})
253 call assert_fails("call term_start(['sh', '-c'], #{curwin: 1})", 'E863:')
254
255 call popup_clear(1)
256 set hidden&
257endfunc
258
259func Test_term_and_startinsert()
260 CheckRunVimInTerminal
261 CheckUnix
262
263 let lines =<< trim EOL
264 put='some text'
265 term
266 startinsert
267 EOL
268 call writefile(lines, 'XTest_startinsert')
269 let buf = RunVimInTerminal('-S XTest_startinsert', {})
270
271 call term_sendkeys(buf, "exit\r")
272 call WaitForAssert({-> assert_equal("some text", term_getline(buf, 1))})
273 call term_sendkeys(buf, "0l")
274 call term_sendkeys(buf, "A<\<Esc>")
275 call WaitForAssert({-> assert_equal("some text<", term_getline(buf, 1))})
276
277 call StopVimInTerminal(buf)
278 call delete('XTest_startinsert')
279endfunc
280
281" Test for passing invalid arguments to terminal functions
282func Test_term_func_invalid_arg()
283 call assert_fails('let b = term_getaltscreen([])', 'E745:')
284 call assert_fails('let a = term_getattr(1, [])', 'E730:')
285 call assert_fails('let c = term_getcursor([])', 'E745:')
286 call assert_fails('let l = term_getline([], 1)', 'E745:')
287 call assert_fails('let l = term_getscrolled([])', 'E745:')
288 call assert_fails('let s = term_getsize([])', 'E745:')
289 call assert_fails('let s = term_getstatus([])', 'E745:')
290 call assert_fails('let s = term_scrape([], 1)', 'E745:')
291 call assert_fails('call term_sendkeys([], "a")', 'E745:')
292 call assert_fails('call term_setapi([], "")', 'E745:')
293 call assert_fails('call term_setrestore([], "")', 'E745:')
294 call assert_fails('call term_setkill([], "")', 'E745:')
295 if has('gui') || has('termguicolors')
296 call assert_fails('let p = term_getansicolors([])', 'E745:')
297 call assert_fails('call term_setansicolors([], [])', 'E745:')
298 endif
299endfunc
300
301" Test for sending various special keycodes to a terminal
302func Test_term_keycode_translation()
303 CheckRunVimInTerminal
304
305 let buf = RunVimInTerminal('', {})
306 call term_sendkeys(buf, ":set nocompatible\<CR>")
307
308 let keys = ["\<F1>", "\<F2>", "\<F3>", "\<F4>", "\<F5>", "\<F6>", "\<F7>",
309 \ "\<F8>", "\<F9>", "\<F10>", "\<F11>", "\<F12>", "\<Home>",
310 \ "\<S-Home>", "\<C-Home>", "\<End>", "\<S-End>", "\<C-End>",
311 \ "\<Ins>", "\<Del>", "\<Left>", "\<S-Left>", "\<C-Left>", "\<Right>",
312 \ "\<S-Right>", "\<C-Right>", "\<Up>", "\<S-Up>", "\<Down>",
313 \ "\<S-Down>"]
314 let output = ['<F1>', '<F2>', '<F3>', '<F4>', '<F5>', '<F6>', '<F7>',
315 \ '<F8>', '<F9>', '<F10>', '<F11>', '<F12>', '<Home>', '<S-Home>',
316 \ '<C-Home>', '<End>', '<S-End>', '<C-End>', '<Insert>', '<Del>',
317 \ '<Left>', '<S-Left>', '<C-Left>', '<Right>', '<S-Right>',
318 \ '<C-Right>', '<Up>', '<S-Up>', '<Down>', '<S-Down>']
319
320 call term_sendkeys(buf, "i")
321 for i in range(len(keys))
322 call term_sendkeys(buf, "\<C-U>\<C-K>" .. keys[i])
323 call WaitForAssert({-> assert_equal(output[i], term_getline(buf, 1))})
324 endfor
325
326 let keypad_keys = ["\<k0>", "\<k1>", "\<k2>", "\<k3>", "\<k4>", "\<k5>",
327 \ "\<k6>", "\<k7>", "\<k8>", "\<k9>", "\<kPoint>", "\<kPlus>",
328 \ "\<kMinus>", "\<kMultiply>", "\<kDivide>"]
329 let keypad_output = ['0', '1', '2', '3', '4', '5',
330 \ '6', '7', '8', '9', '.', '+',
331 \ '-', '*', '/']
332 for i in range(len(keypad_keys))
333 " TODO: Mysteriously keypad 3 and 9 do not work on some systems.
334 if keypad_output[i] == '3' || keypad_output[i] == '9'
335 continue
336 endif
337 call term_sendkeys(buf, "\<C-U>" .. keypad_keys[i])
338 call WaitForAssert({-> assert_equal(keypad_output[i], term_getline(buf, 1))})
339 endfor
340
341 call feedkeys("\<C-U>\<kEnter>\<BS>one\<C-W>.two", 'xt')
342 call WaitForAssert({-> assert_equal('two', term_getline(buf, 1))})
343
344 call StopVimInTerminal(buf)
345endfunc
346
347" Test for using the mouse in a terminal
348func Test_term_mouse()
349 CheckNotGui
350 CheckRunVimInTerminal
351
352 let save_mouse = &mouse
353 let save_term = &term
354 let save_ttymouse = &ttymouse
355 let save_clipboard = &clipboard
356 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
357
358 let lines =<< trim END
359 one two three four five
360 red green yellow red blue
361 vim emacs sublime nano
362 END
363 call writefile(lines, 'Xtest_mouse')
364
365 " Create a terminal window running Vim for the test with mouse enabled
366 let prev_win = win_getid()
367 let buf = RunVimInTerminal('Xtest_mouse -n', {})
368 call term_sendkeys(buf, ":set nocompatible\<CR>")
369 call term_sendkeys(buf, ":set mouse=a term=xterm ttymouse=sgr\<CR>")
370 call term_sendkeys(buf, ":set clipboard=\<CR>")
371 call term_sendkeys(buf, ":set mousemodel=extend\<CR>")
372 call term_wait(buf)
373 redraw!
374
375 " Use the mouse to enter the terminal window
376 call win_gotoid(prev_win)
377 call feedkeys(MouseLeftClickCode(1, 1), 'x')
378 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
379 call assert_equal(1, getwininfo(win_getid())[0].terminal)
380
381 " Test for <LeftMouse> click/release
382 call test_setmouse(2, 5)
383 call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
384 call test_setmouse(3, 8)
385 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
386 call term_wait(buf, 50)
387 call term_sendkeys(buf, ":call writefile([json_encode(getpos('.'))], 'Xbuf')\<CR>")
388 call term_wait(buf, 50)
389 let pos = json_decode(readfile('Xbuf')[0])
390 call assert_equal([3, 8], pos[1:2])
391
392 " Test for selecting text using mouse
393 call delete('Xbuf')
394 call test_setmouse(2, 11)
395 call term_sendkeys(buf, "\<LeftMouse>")
396 call test_setmouse(2, 16)
397 call term_sendkeys(buf, "\<LeftRelease>y")
398 call term_wait(buf, 50)
399 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
400 call term_wait(buf, 50)
401 call assert_equal('yellow', readfile('Xbuf')[0])
402
403 " Test for selecting text using doubleclick
404 call delete('Xbuf')
405 call test_setmouse(1, 11)
406 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>")
407 call test_setmouse(1, 17)
408 call term_sendkeys(buf, "\<LeftRelease>y")
409 call term_wait(buf, 50)
410 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
411 call term_wait(buf, 50)
412 call assert_equal('three four', readfile('Xbuf')[0])
413
414 " Test for selecting a line using triple click
415 call delete('Xbuf')
416 call test_setmouse(3, 2)
417 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>y")
418 call term_wait(buf, 50)
419 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
420 call term_wait(buf, 50)
421 call assert_equal("vim emacs sublime nano\n", readfile('Xbuf')[0])
422
423 " Test for selecting a block using qudraple click
424 call delete('Xbuf')
425 call test_setmouse(1, 11)
426 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>")
427 call test_setmouse(3, 13)
428 call term_sendkeys(buf, "\<LeftRelease>y")
429 call term_wait(buf, 50)
430 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
431 call term_wait(buf, 50)
432 call assert_equal("ree\nyel\nsub", readfile('Xbuf')[0])
433
434 " Test for extending a selection using right click
435 call delete('Xbuf')
436 call test_setmouse(2, 9)
437 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
438 call test_setmouse(2, 16)
439 call term_sendkeys(buf, "\<RightMouse>\<RightRelease>y")
440 call term_wait(buf, 50)
441 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
442 call term_wait(buf, 50)
443 call assert_equal("n yellow", readfile('Xbuf')[0])
444
445 " Test for pasting text using middle click
446 call delete('Xbuf')
447 call term_sendkeys(buf, ":let @r='bright '\<CR>")
448 call test_setmouse(2, 22)
449 call term_sendkeys(buf, "\"r\<MiddleMouse>\<MiddleRelease>")
450 call term_wait(buf, 50)
451 call term_sendkeys(buf, ":call writefile([getline(2)], 'Xbuf')\<CR>")
452 call term_wait(buf, 50)
453 call assert_equal("red bright blue", readfile('Xbuf')[0][-15:])
454
455 " cleanup
456 call term_wait(buf)
457 call StopVimInTerminal(buf)
458 let &mouse = save_mouse
459 let &term = save_term
460 let &ttymouse = save_ttymouse
461 let &clipboard = save_clipboard
462 set mousetime&
463 call delete('Xtest_mouse')
464 call delete('Xbuf')
465endfunc
466
467" Test for modeless selection in a terminal
468func Test_term_modeless_selection()
469 CheckUnix
470 CheckNotGui
471 CheckRunVimInTerminal
472 CheckFeature clipboard_working
473
474 let save_mouse = &mouse
475 let save_term = &term
476 let save_ttymouse = &ttymouse
477 set mouse=a term=xterm ttymouse=sgr mousetime=200
478 set clipboard=autoselectml
479
480 let lines =<< trim END
481 one two three four five
482 red green yellow red blue
483 vim emacs sublime nano
484 END
485 call writefile(lines, 'Xtest_modeless')
486
487 " Create a terminal window running Vim for the test with mouse disabled
488 let prev_win = win_getid()
489 let buf = RunVimInTerminal('Xtest_modeless -n', {})
490 call term_sendkeys(buf, ":set nocompatible\<CR>")
491 call term_sendkeys(buf, ":set mouse=\<CR>")
492 call term_wait(buf)
493 redraw!
494
495 " Use the mouse to enter the terminal window
496 call win_gotoid(prev_win)
497 call feedkeys(MouseLeftClickCode(1, 1), 'x')
498 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
499 call term_wait(buf)
500 call assert_equal(1, getwininfo(win_getid())[0].terminal)
501
502 " Test for copying a modeless selection to clipboard
503 let @* = 'clean'
504 " communicating with X server may take a little time
505 sleep 100m
506 call feedkeys(MouseLeftClickCode(2, 3), 'x')
507 call feedkeys(MouseLeftDragCode(2, 11), 'x')
508 call feedkeys(MouseLeftReleaseCode(2, 11), 'x')
509 call assert_equal("d green y", @*)
510
511 " cleanup
512 call term_wait(buf)
513 call StopVimInTerminal(buf)
514 let &mouse = save_mouse
515 let &term = save_term
516 let &ttymouse = save_ttymouse
517 set mousetime& clipboard&
518 call delete('Xtest_modeless')
519 new | only!
520endfunc
521
522
523" vim: shiftwidth=2 sts=2 expandtab