blob: a351799a7e02ee8b8168231e76f5bfc83d7d7b08 [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.
Bram Moolenaar066b12e2020-07-28 21:40:27 +020046 " However, if dir.exe (which might be provided by Cygwin/MSYS2) exists in
47 " the %PATH%, "term dir" succeeds unintentionally. Use dir.com instead.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020048 try
Bram Moolenaar066b12e2020-07-28 21:40:27 +020049 term dir.com /b runtest.vim
50 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020051 catch /CreateProcess/
52 " ignore
53 endtry
54 bwipe!
55
Bram Moolenaar066b12e2020-07-28 21:40:27 +020056 " This should execute the dir builtin command even with ".com".
57 term ++shell dir.com /b runtest.vim
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020058 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
59 bwipe!
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020060 else
61 throw 'Skipped: does not work on this platform'
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020062 endif
63endfunc
64
65func Test_terminal_invalid_arg()
66 call assert_fails('terminal ++xyz', 'E181:')
67endfunc
68
Bram Moolenaar87fd0922021-11-20 13:47:45 +000069" Check a terminal with different colors
70func Terminal_color(group_name, highlight_cmds, highlight_opt, open_cmds)
71 CheckRunVimInTerminal
72 CheckUnix
73
74 let lines = [
75 \ 'call setline(1, range(20))',
76 \ 'func OpenTerm()',
77 \ ' set noruler',
78 \ " call term_start('cat', #{vertical: 1, " .. a:highlight_opt .. "})",
79 \ ] + a:open_cmds + [
80 \ 'endfunc',
81 \ ] + a:highlight_cmds
82 call writefile(lines, 'XtermStart')
83 let buf = RunVimInTerminal('-S XtermStart', #{rows: 15})
84 call TermWait(buf, 100)
85 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
86 call TermWait(buf, 50)
87 call term_sendkeys(buf, "hello\<CR>")
88 call VerifyScreenDump(buf, 'Test_terminal_color_' .. a:group_name, {})
89
90 call term_sendkeys(buf, "\<C-D>")
91 call TermWait(buf, 50)
92 call StopVimInTerminal(buf)
93 call delete('XtermStart')
94endfunc
95
96func Test_terminal_color_Terminal()
97 call Terminal_color("Terminal", [
98 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
99 \ ], "", [])
100endfunc
101
102func Test_terminal_color_group()
103 call Terminal_color("MyTermCol", [
104 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
105 \ ], "term_highlight: 'MyTermCol',", [])
106endfunc
107
108func Test_terminal_color_wincolor()
109 call Terminal_color("MyWinCol", [
110 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
111 \ ], "", [
112 \ 'set wincolor=MyWinCol',
113 \ ])
114endfunc
115
116func Test_terminal_color_group_over_Terminal()
117 call Terminal_color("MyTermCol_over_Terminal", [
118 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
119 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
120 \ ], "term_highlight: 'MyTermCol',", [])
121endfunc
122
123func Test_terminal_color_wincolor_over_group()
124 call Terminal_color("MyWinCol_over_group", [
125 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
126 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
127 \ ], "term_highlight: 'MyTermCol',", [
128 \ 'set wincolor=MyWinCol',
129 \ ])
130endfunc
131
132func Test_terminal_color_wincolor_split()
133 CheckRunVimInTerminal
134 CheckUnix
135
136 let lines = [
137 \ 'call setline(1, range(20))',
138 \ 'func OpenTerm()',
139 \ ' set noruler',
140 \ " call term_start('cat', #{vertical: 1, term_highlight: 'MyTermCol'})",
141 \ 'endfunc',
142 \ 'highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue',
143 \ 'highlight MyWinCol ctermfg=red ctermbg=darkyellow',
144 \ 'highlight MyWinCol2 ctermfg=black ctermbg=blue',
145 \ ]
146 call writefile(lines, 'XtermStart')
147 let buf = RunVimInTerminal('-S XtermStart', #{rows: 15})
148 call TermWait(buf, 100)
149 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
150 call TermWait(buf, 50)
151 call term_sendkeys(buf, "hello\<CR>")
152 call TermWait(buf, 50)
153
154 call term_sendkeys(buf, "\<C-W>:split\<CR>")
155 call term_sendkeys(buf, "\<C-W>:set wincolor=MyWinCol\<CR>")
156 call VerifyScreenDump(buf, 'Test_terminal_wincolor_split_MyWinCol', {})
157
158 call term_sendkeys(buf, "\<C-W>b:2sb\<CR>")
159 call term_sendkeys(buf, "\<C-W>:set wincolor=MyWinCol2\<CR>")
160 call VerifyScreenDump(buf, 'Test_terminal_wincolor_split_MyWinCol2', {})
161
162 call term_sendkeys(buf, "\<C-D>")
163 call TermWait(buf, 50)
164 call StopVimInTerminal(buf)
165 call delete('XtermStart')
166endfunc
167
168func Test_terminal_color_transp_Terminal()
169 call Terminal_color("transp_Terminal", [
170 \ "highlight Terminal ctermfg=blue",
171 \ ], "", [])
172endfunc
173
174func Test_terminal_color_transp_group()
175 call Terminal_color("transp_MyTermCol", [
176 \ "highlight MyTermCol ctermfg=darkgreen",
177 \ ], "term_highlight: 'MyTermCol',", [])
178endfunc
179
180func Test_terminal_color_transp_wincolor()
181 call Terminal_color("transp_MyWinCol", [
182 \ "highlight MyWinCol ctermfg=red",
183 \ ], "", [
184 \ 'set wincolor=MyWinCol',
185 \ ])
186endfunc
187
188func Test_terminal_color_gui_Terminal()
189 CheckFeature termguicolors
190 call Terminal_color("gui_Terminal", [
191 \ "set termguicolors",
192 \ "highlight Terminal guifg=#3344ff guibg=#b0a700",
193 \ ], "", [])
194endfunc
195
196func Test_terminal_color_gui_group()
197 CheckFeature termguicolors
198 call Terminal_color("gui_MyTermCol", [
199 \ "set termguicolors",
200 \ "highlight MyTermCol guifg=#007800 guibg=#6789ff",
201 \ ], "term_highlight: 'MyTermCol',", [])
202endfunc
203
204func Test_terminal_color_gui_wincolor()
205 CheckFeature termguicolors
206 call Terminal_color("gui_MyWinCol", [
207 \ "set termguicolors",
208 \ "highlight MyWinCol guifg=#fe1122 guibg=#818100",
209 \ ], "", [
210 \ 'set wincolor=MyWinCol',
211 \ ])
212endfunc
213
214func Test_terminal_color_gui_transp_Terminal()
215 CheckFeature termguicolors
216 call Terminal_color("gui_transp_Terminal", [
217 \ "set termguicolors",
218 \ "highlight Terminal guifg=#3344ff",
219 \ ], "", [])
220endfunc
221
222func Test_terminal_color_gui_transp_group()
223 CheckFeature termguicolors
224 call Terminal_color("gui_transp_MyTermCol", [
225 \ "set termguicolors",
226 \ "highlight MyTermCol guifg=#007800",
227 \ ], "term_highlight: 'MyTermCol',", [])
228endfunc
229
230func Test_terminal_color_gui_transp_wincolor()
231 CheckFeature termguicolors
232 call Terminal_color("gui_transp_MyWinCol", [
233 \ "set termguicolors",
234 \ "highlight MyWinCol guifg=#fe1122",
235 \ ], "", [
236 \ 'set wincolor=MyWinCol',
237 \ ])
238endfunc
239
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200240func Test_terminal_in_popup()
241 CheckRunVimInTerminal
242
243 let text =<< trim END
244 some text
245 to edit
246 in a popup window
247 END
248 call writefile(text, 'Xtext')
249 let cmd = GetVimCommandCleanTerm()
250 let lines = [
251 \ 'call setline(1, range(20))',
252 \ 'hi PopTerm ctermbg=grey',
253 \ 'func OpenTerm(setColor)',
254 \ " set noruler",
255 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
256 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
257 \ ' if a:setColor',
258 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
259 \ ' endif',
260 \ 'endfunc',
261 \ 'func HidePopup()',
262 \ ' call popup_hide(g:winid)',
263 \ 'endfunc',
264 \ 'func ClosePopup()',
265 \ ' call popup_close(g:winid)',
266 \ 'endfunc',
267 \ 'func ReopenPopup()',
268 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
269 \ 'endfunc',
270 \ ]
271 call writefile(lines, 'XtermPopup')
272 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
273 call TermWait(buf, 100)
274 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100275 call TermWait(buf, 500)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200276 call term_sendkeys(buf, ":\<CR>")
277 call TermWait(buf, 100)
278 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
279 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
280
281 call term_sendkeys(buf, ":q\<CR>")
282 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
283
284 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100285 call TermWait(buf, 500)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200286 call term_sendkeys(buf, ":set hlsearch\<CR>")
287 call TermWait(buf, 100)
288 call term_sendkeys(buf, "/edit\<CR>")
289 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
290
291 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
292 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
293 call term_sendkeys(buf, "\<CR>")
294 call TermWait(buf, 50)
295
296 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
297 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
298
299 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
300 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
301
302 " Go to terminal-Normal mode and visually select text.
303 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
304 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
305
306 " Back to job mode, redraws
307 call term_sendkeys(buf, "A")
308 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
309
310 call TermWait(buf, 50)
311 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100312 call TermWait(buf, 250) " wait for terminal to vanish
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200313
314 call StopVimInTerminal(buf)
315 call delete('Xtext')
316 call delete('XtermPopup')
317endfunc
318
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100319" Check a terminal in popup window uses the default minimum size.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200320func Test_terminal_in_popup_min_size()
321 CheckRunVimInTerminal
322
323 let text =<< trim END
324 another text
325 to show
326 in a popup window
327 END
328 call writefile(text, 'Xtext')
329 let lines = [
330 \ 'call setline(1, range(20))',
331 \ 'func OpenTerm()',
332 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
333 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
334 \ 'endfunc',
335 \ ]
336 call writefile(lines, 'XtermPopup')
337 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
338 call TermWait(buf, 100)
339 call term_sendkeys(buf, ":set noruler\<CR>")
340 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
341 call TermWait(buf, 50)
342 call term_sendkeys(buf, ":\<CR>")
343 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
344
345 call TermWait(buf, 50)
346 call term_sendkeys(buf, ":q\<CR>")
347 call TermWait(buf, 50) " wait for terminal to vanish
348 call StopVimInTerminal(buf)
349 call delete('Xtext')
350 call delete('XtermPopup')
351endfunc
352
353" Check a terminal in popup window with different colors
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000354func Terminal_in_popup_color(group_name, highlight_cmds, highlight_opt, popup_cmds, popup_opt)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200355 CheckRunVimInTerminal
356 CheckUnix
357
358 let lines = [
359 \ 'call setline(1, range(20))',
360 \ 'func OpenTerm()',
361 \ " let s:buf = term_start('cat', #{hidden: 1, "
362 \ .. a:highlight_opt .. "})",
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000363 \ ' let g:winid = popup_create(s:buf, #{border: [], '
364 \ .. a:popup_opt .. '})',
365 \ ] + a:popup_cmds + [
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200366 \ 'endfunc',
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000367 \ ] + a:highlight_cmds
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200368 call writefile(lines, 'XtermPopup')
369 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
370 call TermWait(buf, 100)
371 call term_sendkeys(buf, ":set noruler\<CR>")
372 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
373 call TermWait(buf, 50)
374 call term_sendkeys(buf, "hello\<CR>")
375 call VerifyScreenDump(buf, 'Test_terminal_popup_' .. a:group_name, {})
376
377 call term_sendkeys(buf, "\<C-D>")
378 call TermWait(buf, 50)
379 call term_sendkeys(buf, ":q\<CR>")
380 call TermWait(buf, 50) " wait for terminal to vanish
381 call StopVimInTerminal(buf)
382 call delete('XtermPopup')
383endfunc
384
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000385func Test_terminal_in_popup_color_Terminal()
386 call Terminal_in_popup_color("Terminal", [
387 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
388 \ ], "", [], "")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200389endfunc
390
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000391func Test_terminal_in_popup_color_group()
392 call Terminal_in_popup_color("MyTermCol", [
393 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
394 \ ], "term_highlight: 'MyTermCol',", [], "")
395endfunc
396
397func Test_terminal_in_popup_color_wincolor()
398 call Terminal_in_popup_color("MyWinCol", [
399 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
400 \ ], "", [
401 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
402 \ ], "")
403endfunc
404
405func Test_terminal_in_popup_color_popup_highlight()
406 call Terminal_in_popup_color("MyPopupHlCol", [
407 \ "highlight MyPopupHlCol ctermfg=cyan ctermbg=green",
408 \ ], "", [], "highlight: 'MyPopupHlCol'")
409endfunc
410
411func Test_terminal_in_popup_color_group_over_Terminal()
412 call Terminal_in_popup_color("MyTermCol_over_Terminal", [
413 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
414 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
415 \ ], "term_highlight: 'MyTermCol',", [], "")
416endfunc
417
418func Test_terminal_in_popup_color_wincolor_over_group()
419 call Terminal_in_popup_color("MyWinCol_over_group", [
420 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
421 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
422 \ ], "term_highlight: 'MyTermCol',", [
423 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
424 \ ], "")
425endfunc
426
427func Test_terminal_in_popup_color_transp_Terminal()
428 call Terminal_in_popup_color("transp_Terminal", [
429 \ "highlight Terminal ctermfg=blue",
430 \ ], "", [], "")
431endfunc
432
433func Test_terminal_in_popup_color_transp_group()
434 call Terminal_in_popup_color("transp_MyTermCol", [
435 \ "highlight MyTermCol ctermfg=darkgreen",
436 \ ], "term_highlight: 'MyTermCol',", [], "")
437endfunc
438
439func Test_terminal_in_popup_color_transp_wincolor()
440 call Terminal_in_popup_color("transp_MyWinCol", [
441 \ "highlight MyWinCol ctermfg=red",
442 \ ], "", [
443 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
444 \ ], "")
445endfunc
446
447func Test_terminal_in_popup_color_transp_popup_highlight()
448 call Terminal_in_popup_color("transp_MyPopupHlCol", [
449 \ "highlight MyPopupHlCol ctermfg=cyan",
450 \ ], "", [], "highlight: 'MyPopupHlCol'")
451endfunc
452
453func Test_terminal_in_popup_color_gui_Terminal()
454 CheckFeature termguicolors
455 call Terminal_in_popup_color("gui_Terminal", [
456 \ "set termguicolors",
457 \ "highlight Terminal guifg=#3344ff guibg=#b0a700",
458 \ ], "", [], "")
459endfunc
460
461func Test_terminal_in_popup_color_gui_group()
462 CheckFeature termguicolors
463 call Terminal_in_popup_color("gui_MyTermCol", [
464 \ "set termguicolors",
465 \ "highlight MyTermCol guifg=#007800 guibg=#6789ff",
466 \ ], "term_highlight: 'MyTermCol',", [], "")
467endfunc
468
469func Test_terminal_in_popup_color_gui_wincolor()
470 CheckFeature termguicolors
471 call Terminal_in_popup_color("gui_MyWinCol", [
472 \ "set termguicolors",
473 \ "highlight MyWinCol guifg=#fe1122 guibg=#818100",
474 \ ], "", [
475 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
476 \ ], "")
477endfunc
478
479func Test_terminal_in_popup_color_gui_popup_highlight()
480 CheckFeature termguicolors
481 call Terminal_in_popup_color("gui_MyPopupHlCol", [
482 \ "set termguicolors",
483 \ "highlight MyPopupHlCol guifg=#00e8f0 guibg=#126521",
484 \ ], "", [], "highlight: 'MyPopupHlCol'")
485endfunc
486
487func Test_terminal_in_popup_color_gui_transp_Terminal()
488 CheckFeature termguicolors
489 call Terminal_in_popup_color("gui_transp_Terminal", [
490 \ "set termguicolors",
491 \ "highlight Terminal guifg=#3344ff",
492 \ ], "", [], "")
493endfunc
494
495func Test_terminal_in_popup_color_gui_transp_group()
496 CheckFeature termguicolors
497 call Terminal_in_popup_color("gui_transp_MyTermCol", [
498 \ "set termguicolors",
499 \ "highlight MyTermCol guifg=#007800",
500 \ ], "term_highlight: 'MyTermCol',", [], "")
501endfunc
502
503func Test_terminal_in_popup_color_gui_transp_wincolor()
504 CheckFeature termguicolors
505 call Terminal_in_popup_color("gui_transp_MyWinCol", [
506 \ "set termguicolors",
507 \ "highlight MyWinCol guifg=#fe1122",
508 \ ], "", [
509 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
510 \ ], "")
511endfunc
512
513func Test_terminal_in_popup_color_gui_transp_popup_highlight()
514 CheckFeature termguicolors
515 call Terminal_in_popup_color("gui_transp_MyPopupHlCol", [
516 \ "set termguicolors",
517 \ "highlight MyPopupHlCol guifg=#00e8f0",
518 \ ], "", [], "highlight: 'MyPopupHlCol'")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200519endfunc
520
521func Test_double_popup_terminal()
522 let buf1 = term_start(&shell, #{hidden: 1})
523 let win1 = popup_create(buf1, {})
524 let buf2 = term_start(&shell, #{hidden: 1})
525 call assert_fails('call popup_create(buf2, {})', 'E861:')
526 call popup_close(win1)
527 exe buf1 .. 'bwipe!'
528 exe buf2 .. 'bwipe!'
529endfunc
530
531func Test_issue_5607()
532 let wincount = winnr('$')
533 exe 'terminal' &shell &shellcmdflag 'exit'
534 let job = term_getjob(bufnr())
535 call WaitForAssert({-> assert_equal("dead", job_status(job))})
536
537 let old_wincolor = &wincolor
538 try
539 set wincolor=
540 finally
541 let &wincolor = old_wincolor
542 bw!
543 endtry
544endfunc
545
546func Test_hidden_terminal()
547 let buf = term_start(&shell, #{hidden: 1})
548 call assert_equal('', bufname('^$'))
549 call StopShellInTerminal(buf)
550endfunc
551
552func Test_term_nasty_callback()
553 CheckExecutable sh
554
555 set hidden
556 let g:buf0 = term_start('sh', #{hidden: 1, term_finish: 'close'})
557 call popup_create(g:buf0, {})
558 call assert_fails("call term_start(['sh', '-c'], #{curwin: 1})", 'E863:')
559
560 call popup_clear(1)
561 set hidden&
562endfunc
563
564func Test_term_and_startinsert()
565 CheckRunVimInTerminal
566 CheckUnix
567
568 let lines =<< trim EOL
569 put='some text'
570 term
571 startinsert
572 EOL
573 call writefile(lines, 'XTest_startinsert')
574 let buf = RunVimInTerminal('-S XTest_startinsert', {})
575
576 call term_sendkeys(buf, "exit\r")
577 call WaitForAssert({-> assert_equal("some text", term_getline(buf, 1))})
578 call term_sendkeys(buf, "0l")
579 call term_sendkeys(buf, "A<\<Esc>")
580 call WaitForAssert({-> assert_equal("some text<", term_getline(buf, 1))})
581
582 call StopVimInTerminal(buf)
583 call delete('XTest_startinsert')
584endfunc
585
586" Test for passing invalid arguments to terminal functions
587func Test_term_func_invalid_arg()
588 call assert_fails('let b = term_getaltscreen([])', 'E745:')
589 call assert_fails('let a = term_getattr(1, [])', 'E730:')
590 call assert_fails('let c = term_getcursor([])', 'E745:')
591 call assert_fails('let l = term_getline([], 1)', 'E745:')
592 call assert_fails('let l = term_getscrolled([])', 'E745:')
593 call assert_fails('let s = term_getsize([])', 'E745:')
594 call assert_fails('let s = term_getstatus([])', 'E745:')
595 call assert_fails('let s = term_scrape([], 1)', 'E745:')
596 call assert_fails('call term_sendkeys([], "a")', 'E745:')
597 call assert_fails('call term_setapi([], "")', 'E745:')
598 call assert_fails('call term_setrestore([], "")', 'E745:')
599 call assert_fails('call term_setkill([], "")', 'E745:')
600 if has('gui') || has('termguicolors')
601 call assert_fails('let p = term_getansicolors([])', 'E745:')
602 call assert_fails('call term_setansicolors([], [])', 'E745:')
603 endif
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100604 let buf = term_start('echo')
605 call assert_fails('call term_setapi(' .. buf .. ', {})', 'E731:')
606 call assert_fails('call term_setkill(' .. buf .. ', {})', 'E731:')
607 call assert_fails('call term_setrestore(' .. buf .. ', {})', 'E731:')
608 exe buf . "bwipe!"
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200609endfunc
610
611" Test for sending various special keycodes to a terminal
612func Test_term_keycode_translation()
613 CheckRunVimInTerminal
614
615 let buf = RunVimInTerminal('', {})
616 call term_sendkeys(buf, ":set nocompatible\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100617 call term_sendkeys(buf, ":set timeoutlen=20\<CR>")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200618
619 let keys = ["\<F1>", "\<F2>", "\<F3>", "\<F4>", "\<F5>", "\<F6>", "\<F7>",
620 \ "\<F8>", "\<F9>", "\<F10>", "\<F11>", "\<F12>", "\<Home>",
621 \ "\<S-Home>", "\<C-Home>", "\<End>", "\<S-End>", "\<C-End>",
622 \ "\<Ins>", "\<Del>", "\<Left>", "\<S-Left>", "\<C-Left>", "\<Right>",
623 \ "\<S-Right>", "\<C-Right>", "\<Up>", "\<S-Up>", "\<Down>",
624 \ "\<S-Down>"]
625 let output = ['<F1>', '<F2>', '<F3>', '<F4>', '<F5>', '<F6>', '<F7>',
626 \ '<F8>', '<F9>', '<F10>', '<F11>', '<F12>', '<Home>', '<S-Home>',
627 \ '<C-Home>', '<End>', '<S-End>', '<C-End>', '<Insert>', '<Del>',
628 \ '<Left>', '<S-Left>', '<C-Left>', '<Right>', '<S-Right>',
629 \ '<C-Right>', '<Up>', '<S-Up>', '<Down>', '<S-Down>']
630
631 call term_sendkeys(buf, "i")
632 for i in range(len(keys))
633 call term_sendkeys(buf, "\<C-U>\<C-K>" .. keys[i])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100634 call WaitForAssert({-> assert_equal(output[i], term_getline(buf, 1))}, 200)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200635 endfor
636
637 let keypad_keys = ["\<k0>", "\<k1>", "\<k2>", "\<k3>", "\<k4>", "\<k5>",
638 \ "\<k6>", "\<k7>", "\<k8>", "\<k9>", "\<kPoint>", "\<kPlus>",
639 \ "\<kMinus>", "\<kMultiply>", "\<kDivide>"]
640 let keypad_output = ['0', '1', '2', '3', '4', '5',
641 \ '6', '7', '8', '9', '.', '+',
642 \ '-', '*', '/']
643 for i in range(len(keypad_keys))
644 " TODO: Mysteriously keypad 3 and 9 do not work on some systems.
645 if keypad_output[i] == '3' || keypad_output[i] == '9'
646 continue
647 endif
648 call term_sendkeys(buf, "\<C-U>" .. keypad_keys[i])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100649 call WaitForAssert({-> assert_equal(keypad_output[i], term_getline(buf, 1))}, 100)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200650 endfor
651
652 call feedkeys("\<C-U>\<kEnter>\<BS>one\<C-W>.two", 'xt')
653 call WaitForAssert({-> assert_equal('two', term_getline(buf, 1))})
654
655 call StopVimInTerminal(buf)
656endfunc
657
658" Test for using the mouse in a terminal
659func Test_term_mouse()
660 CheckNotGui
661 CheckRunVimInTerminal
662
663 let save_mouse = &mouse
664 let save_term = &term
665 let save_ttymouse = &ttymouse
666 let save_clipboard = &clipboard
667 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
668
669 let lines =<< trim END
670 one two three four five
671 red green yellow red blue
672 vim emacs sublime nano
673 END
674 call writefile(lines, 'Xtest_mouse')
675
676 " Create a terminal window running Vim for the test with mouse enabled
677 let prev_win = win_getid()
678 let buf = RunVimInTerminal('Xtest_mouse -n', {})
679 call term_sendkeys(buf, ":set nocompatible\<CR>")
680 call term_sendkeys(buf, ":set mouse=a term=xterm ttymouse=sgr\<CR>")
681 call term_sendkeys(buf, ":set clipboard=\<CR>")
682 call term_sendkeys(buf, ":set mousemodel=extend\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200683 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200684 redraw!
685
686 " Use the mouse to enter the terminal window
687 call win_gotoid(prev_win)
688 call feedkeys(MouseLeftClickCode(1, 1), 'x')
689 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
690 call assert_equal(1, getwininfo(win_getid())[0].terminal)
691
692 " Test for <LeftMouse> click/release
693 call test_setmouse(2, 5)
694 call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
695 call test_setmouse(3, 8)
696 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200697 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200698 call term_sendkeys(buf, ":call writefile([json_encode(getpos('.'))], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200699 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200700 let pos = json_decode(readfile('Xbuf')[0])
701 call assert_equal([3, 8], pos[1:2])
702
703 " Test for selecting text using mouse
704 call delete('Xbuf')
705 call test_setmouse(2, 11)
706 call term_sendkeys(buf, "\<LeftMouse>")
707 call test_setmouse(2, 16)
708 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200709 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200710 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200711 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200712 call assert_equal('yellow', readfile('Xbuf')[0])
713
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000714 " Test for selecting text using double click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200715 call delete('Xbuf')
716 call test_setmouse(1, 11)
717 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>")
718 call test_setmouse(1, 17)
719 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200720 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200721 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200722 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200723 call assert_equal('three four', readfile('Xbuf')[0])
724
725 " Test for selecting a line using triple click
726 call delete('Xbuf')
727 call test_setmouse(3, 2)
728 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200729 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200730 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200731 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200732 call assert_equal("vim emacs sublime nano\n", readfile('Xbuf')[0])
733
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000734 " Test for selecting a block using quadruple click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200735 call delete('Xbuf')
736 call test_setmouse(1, 11)
737 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>")
738 call test_setmouse(3, 13)
739 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200740 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200741 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200742 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200743 call assert_equal("ree\nyel\nsub", readfile('Xbuf')[0])
744
745 " Test for extending a selection using right click
746 call delete('Xbuf')
747 call test_setmouse(2, 9)
748 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
749 call test_setmouse(2, 16)
750 call term_sendkeys(buf, "\<RightMouse>\<RightRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200751 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200752 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200753 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200754 call assert_equal("n yellow", readfile('Xbuf')[0])
755
756 " Test for pasting text using middle click
757 call delete('Xbuf')
758 call term_sendkeys(buf, ":let @r='bright '\<CR>")
759 call test_setmouse(2, 22)
760 call term_sendkeys(buf, "\"r\<MiddleMouse>\<MiddleRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200761 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200762 call term_sendkeys(buf, ":call writefile([getline(2)], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200763 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200764 call assert_equal("red bright blue", readfile('Xbuf')[0][-15:])
765
766 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200767 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200768 call StopVimInTerminal(buf)
769 let &mouse = save_mouse
770 let &term = save_term
771 let &ttymouse = save_ttymouse
772 let &clipboard = save_clipboard
773 set mousetime&
774 call delete('Xtest_mouse')
775 call delete('Xbuf')
776endfunc
777
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200778" Test for sync buffer cwd with shell's pwd
779func Test_terminal_sync_shell_dir()
780 CheckUnix
781 " The test always use sh (see src/testdir/unix.vim).
782 " However, BSD's sh doesn't seem to play well with OSC 7 escape sequence.
783 CheckNotBSD
784
785 set asd
786 " , is
787 " 1. a valid character for directory names
788 " 2. a reserved character in url-encoding
789 let chars = ",a"
790 " "," is url-encoded as '%2C'
791 let chars_url = "%2Ca"
792 let tmpfolder = fnamemodify(tempname(),':h').'/'.chars
793 let tmpfolder_url = fnamemodify(tempname(),':h').'/'.chars_url
794 call mkdir(tmpfolder, "p")
795 let buf = Run_shell_in_terminal({})
796 call term_sendkeys(buf, "echo -ne $'\\e\]7;file://".tmpfolder_url."\\a'\<CR>")
797 "call term_sendkeys(buf, "cd ".tmpfolder."\<CR>")
798 call TermWait(buf)
799 if has("mac")
800 let expected = "/private".tmpfolder
801 else
802 let expected = tmpfolder
803 endif
804 call assert_equal(expected, getcwd(winnr()))
Bram Moolenaar82820d92021-03-30 20:54:28 +0200805
806 set noasd
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200807endfunc
808
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200809" Test for modeless selection in a terminal
810func Test_term_modeless_selection()
811 CheckUnix
812 CheckNotGui
813 CheckRunVimInTerminal
814 CheckFeature clipboard_working
815
816 let save_mouse = &mouse
817 let save_term = &term
818 let save_ttymouse = &ttymouse
819 set mouse=a term=xterm ttymouse=sgr mousetime=200
820 set clipboard=autoselectml
821
822 let lines =<< trim END
823 one two three four five
824 red green yellow red blue
825 vim emacs sublime nano
826 END
827 call writefile(lines, 'Xtest_modeless')
828
829 " Create a terminal window running Vim for the test with mouse disabled
830 let prev_win = win_getid()
831 let buf = RunVimInTerminal('Xtest_modeless -n', {})
832 call term_sendkeys(buf, ":set nocompatible\<CR>")
833 call term_sendkeys(buf, ":set mouse=\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200834 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200835 redraw!
836
837 " Use the mouse to enter the terminal window
838 call win_gotoid(prev_win)
839 call feedkeys(MouseLeftClickCode(1, 1), 'x')
840 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200841 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200842 call assert_equal(1, getwininfo(win_getid())[0].terminal)
843
844 " Test for copying a modeless selection to clipboard
845 let @* = 'clean'
846 " communicating with X server may take a little time
847 sleep 100m
848 call feedkeys(MouseLeftClickCode(2, 3), 'x')
849 call feedkeys(MouseLeftDragCode(2, 11), 'x')
850 call feedkeys(MouseLeftReleaseCode(2, 11), 'x')
851 call assert_equal("d green y", @*)
852
853 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200854 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200855 call StopVimInTerminal(buf)
856 let &mouse = save_mouse
857 let &term = save_term
858 let &ttymouse = save_ttymouse
859 set mousetime& clipboard&
860 call delete('Xtest_modeless')
861 new | only!
862endfunc
863
Bram Moolenaara4b44262020-07-12 21:38:29 +0200864func Test_terminal_getwinpos()
865 CheckRunVimInTerminal
866
867 " split, go to the bottom-right window
868 split
869 wincmd j
870 set splitright
871
Bram Moolenaar42095212020-07-21 21:48:58 +0200872 let buf = RunVimInTerminal('', {'cols': 60})
873 call TermWait(buf, 100)
874 call term_sendkeys(buf, ":echo getwinpos(500)\<CR>")
Bram Moolenaara4b44262020-07-12 21:38:29 +0200875
876 " Find the output of getwinpos() in the bottom line.
877 let rows = term_getsize(buf)[0]
878 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
879 let line = term_getline(buf, rows)
880 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
881 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
882
883 " Position must be bigger than the getwinpos() result of Vim itself.
884 " The calculation in the console assumes a 10 x 7 character cell.
885 " In the GUI it can be more, let's assume a 20 x 14 cell.
886 " And then add 100 / 200 tolerance.
887 let [xroot, yroot] = getwinpos()
888 let winpos = 50->getwinpos()
889 call assert_equal(xroot, winpos[0])
890 call assert_equal(yroot, winpos[1])
Bram Moolenaar7dfc5ce2020-09-05 15:05:30 +0200891 let [winrow, wincol] = win_screenpos(0)
Bram Moolenaara4b44262020-07-12 21:38:29 +0200892 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
893 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
894 call assert_inrange(xroot + 2, xroot + xoff, xpos)
895 call assert_inrange(yroot + 2, yroot + yoff, ypos)
896
897 call TermWait(buf)
898 call term_sendkeys(buf, ":q\<CR>")
899 call StopVimInTerminal(buf)
Bram Moolenaara4b44262020-07-12 21:38:29 +0200900 set splitright&
901 only!
902endfunc
903
ichizokc3f91c02021-12-17 09:44:33 +0000904func Test_terminal_term_start_error()
905 func s:term_start_error() abort
906 try
907 return term_start([[]])
908 catch
909 return v:exception
910 finally
911 "
912 endtry
913 endfunc
914 autocmd WinEnter * call type(0)
915
916 " Must not crash in s:term_start_error, nor the exception thrown.
917 let result = s:term_start_error()
918 call assert_match('^Vim(return):E730:', result)
919
920 autocmd! WinEnter
921 delfunc s:term_start_error
922endfunc
923
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200924
925" vim: shiftwidth=2 sts=2 expandtab