blob: 5b143719bcea98b87df522ab1529cd21d6f9b847 [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, {})
Bram Moolenaarc4860bd2022-10-15 20:52:26 +010021 call writefile(["\<Esc>[?1047h"], 'Xtext', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020022 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!"
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020031endfunc
32
33func Test_terminal_shell_option()
34 if has('unix')
35 " exec is a shell builtin command, should fail without a shell.
36 term exec ls runtest.vim
37 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
38 bwipe!
39
40 term ++shell exec ls runtest.vim
41 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
42 bwipe!
43 elseif has('win32')
44 " dir is a shell builtin command, should fail without a shell.
Bram Moolenaar066b12e2020-07-28 21:40:27 +020045 " However, if dir.exe (which might be provided by Cygwin/MSYS2) exists in
46 " the %PATH%, "term dir" succeeds unintentionally. Use dir.com instead.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020047 try
Bram Moolenaar066b12e2020-07-28 21:40:27 +020048 term dir.com /b runtest.vim
49 call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020050 catch /CreateProcess/
51 " ignore
52 endtry
53 bwipe!
54
Bram Moolenaar066b12e2020-07-28 21:40:27 +020055 " This should execute the dir builtin command even with ".com".
56 term ++shell dir.com /b runtest.vim
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020057 call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
58 bwipe!
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020059 else
60 throw 'Skipped: does not work on this platform'
Bram Moolenaar18aa13d2020-07-11 13:09:36 +020061 endif
62endfunc
63
64func Test_terminal_invalid_arg()
65 call assert_fails('terminal ++xyz', 'E181:')
66endfunc
67
Bram Moolenaar87fd0922021-11-20 13:47:45 +000068" Check a terminal with different colors
69func Terminal_color(group_name, highlight_cmds, highlight_opt, open_cmds)
70 CheckRunVimInTerminal
71 CheckUnix
72
73 let lines = [
74 \ 'call setline(1, range(20))',
75 \ 'func OpenTerm()',
76 \ ' set noruler',
77 \ " call term_start('cat', #{vertical: 1, " .. a:highlight_opt .. "})",
78 \ ] + a:open_cmds + [
79 \ 'endfunc',
80 \ ] + a:highlight_cmds
Bram Moolenaarc4860bd2022-10-15 20:52:26 +010081 call writefile(lines, 'XtermStart', 'D')
Bram Moolenaar87fd0922021-11-20 13:47:45 +000082 let buf = RunVimInTerminal('-S XtermStart', #{rows: 15})
83 call TermWait(buf, 100)
84 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
85 call TermWait(buf, 50)
86 call term_sendkeys(buf, "hello\<CR>")
87 call VerifyScreenDump(buf, 'Test_terminal_color_' .. a:group_name, {})
88
89 call term_sendkeys(buf, "\<C-D>")
90 call TermWait(buf, 50)
91 call StopVimInTerminal(buf)
Bram Moolenaar87fd0922021-11-20 13:47:45 +000092endfunc
93
94func Test_terminal_color_Terminal()
95 call Terminal_color("Terminal", [
96 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
97 \ ], "", [])
98endfunc
99
100func Test_terminal_color_group()
101 call Terminal_color("MyTermCol", [
102 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
103 \ ], "term_highlight: 'MyTermCol',", [])
104endfunc
105
106func Test_terminal_color_wincolor()
107 call Terminal_color("MyWinCol", [
108 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
109 \ ], "", [
110 \ 'set wincolor=MyWinCol',
111 \ ])
112endfunc
113
114func Test_terminal_color_group_over_Terminal()
115 call Terminal_color("MyTermCol_over_Terminal", [
116 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
117 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
118 \ ], "term_highlight: 'MyTermCol',", [])
119endfunc
120
121func Test_terminal_color_wincolor_over_group()
122 call Terminal_color("MyWinCol_over_group", [
123 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
124 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
125 \ ], "term_highlight: 'MyTermCol',", [
126 \ 'set wincolor=MyWinCol',
127 \ ])
128endfunc
129
130func Test_terminal_color_wincolor_split()
131 CheckRunVimInTerminal
132 CheckUnix
133
134 let lines = [
135 \ 'call setline(1, range(20))',
136 \ 'func OpenTerm()',
137 \ ' set noruler',
138 \ " call term_start('cat', #{vertical: 1, term_highlight: 'MyTermCol'})",
139 \ 'endfunc',
140 \ 'highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue',
141 \ 'highlight MyWinCol ctermfg=red ctermbg=darkyellow',
142 \ 'highlight MyWinCol2 ctermfg=black ctermbg=blue',
143 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100144 call writefile(lines, 'XtermStart', 'D')
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000145 let buf = RunVimInTerminal('-S XtermStart', #{rows: 15})
146 call TermWait(buf, 100)
147 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
148 call TermWait(buf, 50)
149 call term_sendkeys(buf, "hello\<CR>")
150 call TermWait(buf, 50)
151
152 call term_sendkeys(buf, "\<C-W>:split\<CR>")
153 call term_sendkeys(buf, "\<C-W>:set wincolor=MyWinCol\<CR>")
154 call VerifyScreenDump(buf, 'Test_terminal_wincolor_split_MyWinCol', {})
155
156 call term_sendkeys(buf, "\<C-W>b:2sb\<CR>")
157 call term_sendkeys(buf, "\<C-W>:set wincolor=MyWinCol2\<CR>")
158 call VerifyScreenDump(buf, 'Test_terminal_wincolor_split_MyWinCol2', {})
159
160 call term_sendkeys(buf, "\<C-D>")
161 call TermWait(buf, 50)
162 call StopVimInTerminal(buf)
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000163endfunc
164
165func Test_terminal_color_transp_Terminal()
166 call Terminal_color("transp_Terminal", [
167 \ "highlight Terminal ctermfg=blue",
168 \ ], "", [])
169endfunc
170
171func Test_terminal_color_transp_group()
172 call Terminal_color("transp_MyTermCol", [
173 \ "highlight MyTermCol ctermfg=darkgreen",
174 \ ], "term_highlight: 'MyTermCol',", [])
175endfunc
176
177func Test_terminal_color_transp_wincolor()
178 call Terminal_color("transp_MyWinCol", [
179 \ "highlight MyWinCol ctermfg=red",
180 \ ], "", [
181 \ 'set wincolor=MyWinCol',
182 \ ])
183endfunc
184
185func Test_terminal_color_gui_Terminal()
186 CheckFeature termguicolors
187 call Terminal_color("gui_Terminal", [
188 \ "set termguicolors",
189 \ "highlight Terminal guifg=#3344ff guibg=#b0a700",
190 \ ], "", [])
191endfunc
192
193func Test_terminal_color_gui_group()
194 CheckFeature termguicolors
195 call Terminal_color("gui_MyTermCol", [
196 \ "set termguicolors",
197 \ "highlight MyTermCol guifg=#007800 guibg=#6789ff",
198 \ ], "term_highlight: 'MyTermCol',", [])
199endfunc
200
201func Test_terminal_color_gui_wincolor()
202 CheckFeature termguicolors
203 call Terminal_color("gui_MyWinCol", [
204 \ "set termguicolors",
205 \ "highlight MyWinCol guifg=#fe1122 guibg=#818100",
206 \ ], "", [
207 \ 'set wincolor=MyWinCol',
208 \ ])
209endfunc
210
211func Test_terminal_color_gui_transp_Terminal()
212 CheckFeature termguicolors
213 call Terminal_color("gui_transp_Terminal", [
214 \ "set termguicolors",
215 \ "highlight Terminal guifg=#3344ff",
216 \ ], "", [])
217endfunc
218
219func Test_terminal_color_gui_transp_group()
220 CheckFeature termguicolors
221 call Terminal_color("gui_transp_MyTermCol", [
222 \ "set termguicolors",
223 \ "highlight MyTermCol guifg=#007800",
224 \ ], "term_highlight: 'MyTermCol',", [])
225endfunc
226
227func Test_terminal_color_gui_transp_wincolor()
228 CheckFeature termguicolors
229 call Terminal_color("gui_transp_MyWinCol", [
230 \ "set termguicolors",
231 \ "highlight MyWinCol guifg=#fe1122",
232 \ ], "", [
233 \ 'set wincolor=MyWinCol',
234 \ ])
235endfunc
236
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200237func Test_terminal_in_popup()
238 CheckRunVimInTerminal
239
240 let text =<< trim END
241 some text
242 to edit
243 in a popup window
244 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100245 call writefile(text, 'Xtext', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200246 let cmd = GetVimCommandCleanTerm()
247 let lines = [
248 \ 'call setline(1, range(20))',
249 \ 'hi PopTerm ctermbg=grey',
250 \ 'func OpenTerm(setColor)',
251 \ " set noruler",
252 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
253 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
254 \ ' if a:setColor',
255 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
256 \ ' endif',
257 \ 'endfunc',
258 \ 'func HidePopup()',
259 \ ' call popup_hide(g:winid)',
260 \ 'endfunc',
261 \ 'func ClosePopup()',
262 \ ' call popup_close(g:winid)',
263 \ 'endfunc',
264 \ 'func ReopenPopup()',
265 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
266 \ 'endfunc',
267 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100268 call writefile(lines, 'XtermPopup', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200269 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
270 call TermWait(buf, 100)
271 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100272 call TermWait(buf, 500)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200273 call term_sendkeys(buf, ":\<CR>")
274 call TermWait(buf, 100)
275 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
276 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
277
278 call term_sendkeys(buf, ":q\<CR>")
279 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
280
281 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100282 call TermWait(buf, 500)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200283 call term_sendkeys(buf, ":set hlsearch\<CR>")
284 call TermWait(buf, 100)
285 call term_sendkeys(buf, "/edit\<CR>")
286 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
287
288 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
289 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
290 call term_sendkeys(buf, "\<CR>")
291 call TermWait(buf, 50)
292
293 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
294 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
295
296 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
297 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
298
299 " Go to terminal-Normal mode and visually select text.
300 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
301 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
302
303 " Back to job mode, redraws
304 call term_sendkeys(buf, "A")
305 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
306
307 call TermWait(buf, 50)
308 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100309 call TermWait(buf, 250) " wait for terminal to vanish
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200310
311 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200312endfunc
313
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100314" Check a terminal in popup window uses the default minimum size.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200315func Test_terminal_in_popup_min_size()
316 CheckRunVimInTerminal
317
318 let text =<< trim END
319 another text
320 to show
321 in a popup window
322 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100323 call writefile(text, 'Xtext', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200324 let lines = [
325 \ 'call setline(1, range(20))',
326 \ 'func OpenTerm()',
327 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
328 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
329 \ 'endfunc',
330 \ ]
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100331 call writefile(lines, 'XtermPopup', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200332 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
333 call TermWait(buf, 100)
334 call term_sendkeys(buf, ":set noruler\<CR>")
335 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
336 call TermWait(buf, 50)
337 call term_sendkeys(buf, ":\<CR>")
338 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
339
340 call TermWait(buf, 50)
341 call term_sendkeys(buf, ":q\<CR>")
342 call TermWait(buf, 50) " wait for terminal to vanish
343 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200344endfunc
345
346" Check a terminal in popup window with different colors
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000347func Terminal_in_popup_color(group_name, highlight_cmds, highlight_opt, popup_cmds, popup_opt)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200348 CheckRunVimInTerminal
349 CheckUnix
350
351 let lines = [
352 \ 'call setline(1, range(20))',
353 \ 'func OpenTerm()',
354 \ " let s:buf = term_start('cat', #{hidden: 1, "
355 \ .. a:highlight_opt .. "})",
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000356 \ ' let g:winid = popup_create(s:buf, #{border: [], '
357 \ .. a:popup_opt .. '})',
358 \ ] + a:popup_cmds + [
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200359 \ 'endfunc',
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000360 \ ] + a:highlight_cmds
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100361 call writefile(lines, 'XtermPopup', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200362 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
363 call TermWait(buf, 100)
364 call term_sendkeys(buf, ":set noruler\<CR>")
365 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
366 call TermWait(buf, 50)
367 call term_sendkeys(buf, "hello\<CR>")
368 call VerifyScreenDump(buf, 'Test_terminal_popup_' .. a:group_name, {})
369
370 call term_sendkeys(buf, "\<C-D>")
371 call TermWait(buf, 50)
372 call term_sendkeys(buf, ":q\<CR>")
373 call TermWait(buf, 50) " wait for terminal to vanish
374 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200375endfunc
376
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000377func Test_terminal_in_popup_color_Terminal()
378 call Terminal_in_popup_color("Terminal", [
379 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
380 \ ], "", [], "")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200381endfunc
382
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000383func Test_terminal_in_popup_color_group()
384 call Terminal_in_popup_color("MyTermCol", [
385 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
386 \ ], "term_highlight: 'MyTermCol',", [], "")
387endfunc
388
389func Test_terminal_in_popup_color_wincolor()
390 call Terminal_in_popup_color("MyWinCol", [
391 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
392 \ ], "", [
393 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
394 \ ], "")
395endfunc
396
397func Test_terminal_in_popup_color_popup_highlight()
398 call Terminal_in_popup_color("MyPopupHlCol", [
399 \ "highlight MyPopupHlCol ctermfg=cyan ctermbg=green",
400 \ ], "", [], "highlight: 'MyPopupHlCol'")
401endfunc
402
403func Test_terminal_in_popup_color_group_over_Terminal()
404 call Terminal_in_popup_color("MyTermCol_over_Terminal", [
405 \ "highlight Terminal ctermfg=blue ctermbg=yellow",
406 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
407 \ ], "term_highlight: 'MyTermCol',", [], "")
408endfunc
409
410func Test_terminal_in_popup_color_wincolor_over_group()
411 call Terminal_in_popup_color("MyWinCol_over_group", [
412 \ "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue",
413 \ "highlight MyWinCol ctermfg=red ctermbg=darkyellow",
414 \ ], "term_highlight: 'MyTermCol',", [
415 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
416 \ ], "")
417endfunc
418
419func Test_terminal_in_popup_color_transp_Terminal()
420 call Terminal_in_popup_color("transp_Terminal", [
421 \ "highlight Terminal ctermfg=blue",
422 \ ], "", [], "")
423endfunc
424
425func Test_terminal_in_popup_color_transp_group()
426 call Terminal_in_popup_color("transp_MyTermCol", [
427 \ "highlight MyTermCol ctermfg=darkgreen",
428 \ ], "term_highlight: 'MyTermCol',", [], "")
429endfunc
430
431func Test_terminal_in_popup_color_transp_wincolor()
432 call Terminal_in_popup_color("transp_MyWinCol", [
433 \ "highlight MyWinCol ctermfg=red",
434 \ ], "", [
435 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
436 \ ], "")
437endfunc
438
439func Test_terminal_in_popup_color_transp_popup_highlight()
440 call Terminal_in_popup_color("transp_MyPopupHlCol", [
441 \ "highlight MyPopupHlCol ctermfg=cyan",
442 \ ], "", [], "highlight: 'MyPopupHlCol'")
443endfunc
444
445func Test_terminal_in_popup_color_gui_Terminal()
446 CheckFeature termguicolors
447 call Terminal_in_popup_color("gui_Terminal", [
448 \ "set termguicolors",
449 \ "highlight Terminal guifg=#3344ff guibg=#b0a700",
450 \ ], "", [], "")
451endfunc
452
453func Test_terminal_in_popup_color_gui_group()
454 CheckFeature termguicolors
455 call Terminal_in_popup_color("gui_MyTermCol", [
456 \ "set termguicolors",
457 \ "highlight MyTermCol guifg=#007800 guibg=#6789ff",
458 \ ], "term_highlight: 'MyTermCol',", [], "")
459endfunc
460
461func Test_terminal_in_popup_color_gui_wincolor()
462 CheckFeature termguicolors
463 call Terminal_in_popup_color("gui_MyWinCol", [
464 \ "set termguicolors",
465 \ "highlight MyWinCol guifg=#fe1122 guibg=#818100",
466 \ ], "", [
467 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
468 \ ], "")
469endfunc
470
471func Test_terminal_in_popup_color_gui_popup_highlight()
472 CheckFeature termguicolors
473 call Terminal_in_popup_color("gui_MyPopupHlCol", [
474 \ "set termguicolors",
475 \ "highlight MyPopupHlCol guifg=#00e8f0 guibg=#126521",
476 \ ], "", [], "highlight: 'MyPopupHlCol'")
477endfunc
478
479func Test_terminal_in_popup_color_gui_transp_Terminal()
480 CheckFeature termguicolors
481 call Terminal_in_popup_color("gui_transp_Terminal", [
482 \ "set termguicolors",
483 \ "highlight Terminal guifg=#3344ff",
484 \ ], "", [], "")
485endfunc
486
487func Test_terminal_in_popup_color_gui_transp_group()
488 CheckFeature termguicolors
489 call Terminal_in_popup_color("gui_transp_MyTermCol", [
490 \ "set termguicolors",
491 \ "highlight MyTermCol guifg=#007800",
492 \ ], "term_highlight: 'MyTermCol',", [], "")
493endfunc
494
495func Test_terminal_in_popup_color_gui_transp_wincolor()
496 CheckFeature termguicolors
497 call Terminal_in_popup_color("gui_transp_MyWinCol", [
498 \ "set termguicolors",
499 \ "highlight MyWinCol guifg=#fe1122",
500 \ ], "", [
501 \ 'call setwinvar(g:winid, "&wincolor", "MyWinCol")',
502 \ ], "")
503endfunc
504
505func Test_terminal_in_popup_color_gui_transp_popup_highlight()
506 CheckFeature termguicolors
507 call Terminal_in_popup_color("gui_transp_MyPopupHlCol", [
508 \ "set termguicolors",
509 \ "highlight MyPopupHlCol guifg=#00e8f0",
510 \ ], "", [], "highlight: 'MyPopupHlCol'")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200511endfunc
512
513func Test_double_popup_terminal()
514 let buf1 = term_start(&shell, #{hidden: 1})
515 let win1 = popup_create(buf1, {})
516 let buf2 = term_start(&shell, #{hidden: 1})
517 call assert_fails('call popup_create(buf2, {})', 'E861:')
518 call popup_close(win1)
519 exe buf1 .. 'bwipe!'
520 exe buf2 .. 'bwipe!'
521endfunc
522
LemonBoy4a392d22022-04-23 14:07:56 +0100523func Test_escape_popup_terminal()
524 set hidden
525
526 " Cannot escape a terminal popup window using win_gotoid
527 let prev_win = win_getid()
528 eval term_start('sh', #{hidden: 1, term_finish: 'close'})->popup_create({})
529 call assert_fails("call win_gotoid(" .. prev_win .. ")", 'E863:')
530
531 call popup_clear(1)
532 set hidden&
533endfunc
534
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200535func Test_issue_5607()
536 let wincount = winnr('$')
537 exe 'terminal' &shell &shellcmdflag 'exit'
538 let job = term_getjob(bufnr())
539 call WaitForAssert({-> assert_equal("dead", job_status(job))})
540
541 let old_wincolor = &wincolor
542 try
543 set wincolor=
544 finally
545 let &wincolor = old_wincolor
546 bw!
547 endtry
548endfunc
549
550func Test_hidden_terminal()
551 let buf = term_start(&shell, #{hidden: 1})
552 call assert_equal('', bufname('^$'))
553 call StopShellInTerminal(buf)
554endfunc
555
556func Test_term_nasty_callback()
557 CheckExecutable sh
558
559 set hidden
560 let g:buf0 = term_start('sh', #{hidden: 1, term_finish: 'close'})
561 call popup_create(g:buf0, {})
562 call assert_fails("call term_start(['sh', '-c'], #{curwin: 1})", 'E863:')
563
564 call popup_clear(1)
565 set hidden&
566endfunc
567
568func Test_term_and_startinsert()
569 CheckRunVimInTerminal
570 CheckUnix
571
572 let lines =<< trim EOL
573 put='some text'
574 term
575 startinsert
576 EOL
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100577 call writefile(lines, 'XTest_startinsert', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200578 let buf = RunVimInTerminal('-S XTest_startinsert', {})
579
580 call term_sendkeys(buf, "exit\r")
581 call WaitForAssert({-> assert_equal("some text", term_getline(buf, 1))})
582 call term_sendkeys(buf, "0l")
583 call term_sendkeys(buf, "A<\<Esc>")
584 call WaitForAssert({-> assert_equal("some text<", term_getline(buf, 1))})
585
586 call StopVimInTerminal(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200587endfunc
588
589" Test for passing invalid arguments to terminal functions
590func Test_term_func_invalid_arg()
591 call assert_fails('let b = term_getaltscreen([])', 'E745:')
592 call assert_fails('let a = term_getattr(1, [])', 'E730:')
593 call assert_fails('let c = term_getcursor([])', 'E745:')
594 call assert_fails('let l = term_getline([], 1)', 'E745:')
595 call assert_fails('let l = term_getscrolled([])', 'E745:')
596 call assert_fails('let s = term_getsize([])', 'E745:')
597 call assert_fails('let s = term_getstatus([])', 'E745:')
598 call assert_fails('let s = term_scrape([], 1)', 'E745:')
599 call assert_fails('call term_sendkeys([], "a")', 'E745:')
600 call assert_fails('call term_setapi([], "")', 'E745:')
601 call assert_fails('call term_setrestore([], "")', 'E745:')
602 call assert_fails('call term_setkill([], "")', 'E745:')
603 if has('gui') || has('termguicolors')
604 call assert_fails('let p = term_getansicolors([])', 'E745:')
605 call assert_fails('call term_setansicolors([], [])', 'E745:')
606 endif
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100607 let buf = term_start('echo')
608 call assert_fails('call term_setapi(' .. buf .. ', {})', 'E731:')
609 call assert_fails('call term_setkill(' .. buf .. ', {})', 'E731:')
610 call assert_fails('call term_setrestore(' .. buf .. ', {})', 'E731:')
611 exe buf . "bwipe!"
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200612endfunc
613
614" Test for sending various special keycodes to a terminal
615func Test_term_keycode_translation()
616 CheckRunVimInTerminal
617
618 let buf = RunVimInTerminal('', {})
619 call term_sendkeys(buf, ":set nocompatible\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100620 call term_sendkeys(buf, ":set timeoutlen=20\<CR>")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200621
622 let keys = ["\<F1>", "\<F2>", "\<F3>", "\<F4>", "\<F5>", "\<F6>", "\<F7>",
623 \ "\<F8>", "\<F9>", "\<F10>", "\<F11>", "\<F12>", "\<Home>",
624 \ "\<S-Home>", "\<C-Home>", "\<End>", "\<S-End>", "\<C-End>",
625 \ "\<Ins>", "\<Del>", "\<Left>", "\<S-Left>", "\<C-Left>", "\<Right>",
626 \ "\<S-Right>", "\<C-Right>", "\<Up>", "\<S-Up>", "\<Down>",
627 \ "\<S-Down>"]
628 let output = ['<F1>', '<F2>', '<F3>', '<F4>', '<F5>', '<F6>', '<F7>',
629 \ '<F8>', '<F9>', '<F10>', '<F11>', '<F12>', '<Home>', '<S-Home>',
630 \ '<C-Home>', '<End>', '<S-End>', '<C-End>', '<Insert>', '<Del>',
631 \ '<Left>', '<S-Left>', '<C-Left>', '<Right>', '<S-Right>',
632 \ '<C-Right>', '<Up>', '<S-Up>', '<Down>', '<S-Down>']
633
634 call term_sendkeys(buf, "i")
635 for i in range(len(keys))
636 call term_sendkeys(buf, "\<C-U>\<C-K>" .. keys[i])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100637 call WaitForAssert({-> assert_equal(output[i], term_getline(buf, 1))}, 200)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200638 endfor
639
640 let keypad_keys = ["\<k0>", "\<k1>", "\<k2>", "\<k3>", "\<k4>", "\<k5>",
641 \ "\<k6>", "\<k7>", "\<k8>", "\<k9>", "\<kPoint>", "\<kPlus>",
642 \ "\<kMinus>", "\<kMultiply>", "\<kDivide>"]
643 let keypad_output = ['0', '1', '2', '3', '4', '5',
644 \ '6', '7', '8', '9', '.', '+',
645 \ '-', '*', '/']
646 for i in range(len(keypad_keys))
647 " TODO: Mysteriously keypad 3 and 9 do not work on some systems.
648 if keypad_output[i] == '3' || keypad_output[i] == '9'
649 continue
650 endif
651 call term_sendkeys(buf, "\<C-U>" .. keypad_keys[i])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100652 call WaitForAssert({-> assert_equal(keypad_output[i], term_getline(buf, 1))}, 100)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200653 endfor
654
655 call feedkeys("\<C-U>\<kEnter>\<BS>one\<C-W>.two", 'xt')
656 call WaitForAssert({-> assert_equal('two', term_getline(buf, 1))})
657
658 call StopVimInTerminal(buf)
659endfunc
660
661" Test for using the mouse in a terminal
662func Test_term_mouse()
663 CheckNotGui
664 CheckRunVimInTerminal
665
666 let save_mouse = &mouse
667 let save_term = &term
668 let save_ttymouse = &ttymouse
669 let save_clipboard = &clipboard
670 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
671
672 let lines =<< trim END
673 one two three four five
674 red green yellow red blue
675 vim emacs sublime nano
676 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100677 call writefile(lines, 'Xtest_mouse', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200678
679 " Create a terminal window running Vim for the test with mouse enabled
680 let prev_win = win_getid()
681 let buf = RunVimInTerminal('Xtest_mouse -n', {})
682 call term_sendkeys(buf, ":set nocompatible\<CR>")
683 call term_sendkeys(buf, ":set mouse=a term=xterm ttymouse=sgr\<CR>")
684 call term_sendkeys(buf, ":set clipboard=\<CR>")
685 call term_sendkeys(buf, ":set mousemodel=extend\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200686 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200687 redraw!
688
689 " Use the mouse to enter the terminal window
690 call win_gotoid(prev_win)
691 call feedkeys(MouseLeftClickCode(1, 1), 'x')
692 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
693 call assert_equal(1, getwininfo(win_getid())[0].terminal)
694
695 " Test for <LeftMouse> click/release
696 call test_setmouse(2, 5)
697 call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
698 call test_setmouse(3, 8)
699 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200700 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200701 call term_sendkeys(buf, ":call writefile([json_encode(getpos('.'))], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200702 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200703 let pos = json_decode(readfile('Xbuf')[0])
704 call assert_equal([3, 8], pos[1:2])
705
706 " Test for selecting text using mouse
707 call delete('Xbuf')
708 call test_setmouse(2, 11)
709 call term_sendkeys(buf, "\<LeftMouse>")
710 call test_setmouse(2, 16)
711 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200712 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200713 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200714 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200715 call assert_equal('yellow', readfile('Xbuf')[0])
716
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000717 " Test for selecting text using double click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200718 call delete('Xbuf')
719 call test_setmouse(1, 11)
720 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>")
721 call test_setmouse(1, 17)
722 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200723 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200724 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200725 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200726 call assert_equal('three four', readfile('Xbuf')[0])
727
728 " Test for selecting a line using triple click
729 call delete('Xbuf')
730 call test_setmouse(3, 2)
731 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200732 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200733 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200734 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200735 call assert_equal("vim emacs sublime nano\n", readfile('Xbuf')[0])
736
Bram Moolenaar87fd0922021-11-20 13:47:45 +0000737 " Test for selecting a block using quadruple click
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200738 call delete('Xbuf')
739 call test_setmouse(1, 11)
740 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>")
741 call test_setmouse(3, 13)
742 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200743 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200744 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200745 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200746 call assert_equal("ree\nyel\nsub", readfile('Xbuf')[0])
747
748 " Test for extending a selection using right click
749 call delete('Xbuf')
750 call test_setmouse(2, 9)
751 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
752 call test_setmouse(2, 16)
753 call term_sendkeys(buf, "\<RightMouse>\<RightRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200754 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200755 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200756 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200757 call assert_equal("n yellow", readfile('Xbuf')[0])
758
759 " Test for pasting text using middle click
760 call delete('Xbuf')
761 call term_sendkeys(buf, ":let @r='bright '\<CR>")
762 call test_setmouse(2, 22)
763 call term_sendkeys(buf, "\"r\<MiddleMouse>\<MiddleRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200764 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200765 call term_sendkeys(buf, ":call writefile([getline(2)], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200766 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200767 call assert_equal("red bright blue", readfile('Xbuf')[0][-15:])
768
769 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200770 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200771 call StopVimInTerminal(buf)
772 let &mouse = save_mouse
773 let &term = save_term
774 let &ttymouse = save_ttymouse
775 let &clipboard = save_clipboard
776 set mousetime&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200777 call delete('Xbuf')
778endfunc
779
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200780" Test for sync buffer cwd with shell's pwd
781func Test_terminal_sync_shell_dir()
782 CheckUnix
783 " The test always use sh (see src/testdir/unix.vim).
Bram Moolenaar7bfa6d62022-01-14 12:06:47 +0000784 " BSD's sh doesn't seem to play well with the OSC 7 escape sequence.
785 CheckNotBSD
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200786
787 set asd
788 " , is
789 " 1. a valid character for directory names
790 " 2. a reserved character in url-encoding
791 let chars = ",a"
792 " "," is url-encoded as '%2C'
793 let chars_url = "%2Ca"
Bram Moolenaarced2b382022-01-13 15:25:32 +0000794 let tmpfolder = fnamemodify(tempname(),':h') .. '/' .. chars
795 let tmpfolder_url = fnamemodify(tempname(),':h') .. '/' .. chars_url
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200796 call mkdir(tmpfolder, "p")
797 let buf = Run_shell_in_terminal({})
Bram Moolenaarced2b382022-01-13 15:25:32 +0000798 call term_sendkeys(buf, "echo $'\\e\]7;file://" .. tmpfolder_url .. "\\a'\<CR>")
799 "call term_sendkeys(buf, "cd " .. tmpfolder .. "\<CR>")
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200800 call TermWait(buf)
801 if has("mac")
Bram Moolenaarced2b382022-01-13 15:25:32 +0000802 let expected = "/private" .. tmpfolder
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200803 else
804 let expected = tmpfolder
805 endif
806 call assert_equal(expected, getcwd(winnr()))
Bram Moolenaar82820d92021-03-30 20:54:28 +0200807
808 set noasd
Bram Moolenaar8b9abfd2021-03-29 20:49:05 +0200809endfunc
810
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200811" Test for modeless selection in a terminal
812func Test_term_modeless_selection()
813 CheckUnix
814 CheckNotGui
815 CheckRunVimInTerminal
816 CheckFeature clipboard_working
817
818 let save_mouse = &mouse
819 let save_term = &term
820 let save_ttymouse = &ttymouse
821 set mouse=a term=xterm ttymouse=sgr mousetime=200
822 set clipboard=autoselectml
823
824 let lines =<< trim END
825 one two three four five
826 red green yellow red blue
827 vim emacs sublime nano
828 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100829 call writefile(lines, 'Xtest_modeless', 'D')
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200830
831 " Create a terminal window running Vim for the test with mouse disabled
832 let prev_win = win_getid()
833 let buf = RunVimInTerminal('Xtest_modeless -n', {})
834 call term_sendkeys(buf, ":set nocompatible\<CR>")
835 call term_sendkeys(buf, ":set mouse=\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200836 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200837 redraw!
838
839 " Use the mouse to enter the terminal window
840 call win_gotoid(prev_win)
841 call feedkeys(MouseLeftClickCode(1, 1), 'x')
842 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200843 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200844 call assert_equal(1, getwininfo(win_getid())[0].terminal)
845
846 " Test for copying a modeless selection to clipboard
847 let @* = 'clean'
848 " communicating with X server may take a little time
849 sleep 100m
850 call feedkeys(MouseLeftClickCode(2, 3), 'x')
851 call feedkeys(MouseLeftDragCode(2, 11), 'x')
852 call feedkeys(MouseLeftReleaseCode(2, 11), 'x')
853 call assert_equal("d green y", @*)
854
855 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200856 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200857 call StopVimInTerminal(buf)
858 let &mouse = save_mouse
859 let &term = save_term
860 let &ttymouse = save_ttymouse
861 set mousetime& clipboard&
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200862 new | only!
863endfunc
864
Bram Moolenaara4b44262020-07-12 21:38:29 +0200865func Test_terminal_getwinpos()
866 CheckRunVimInTerminal
867
868 " split, go to the bottom-right window
869 split
870 wincmd j
871 set splitright
872
Bram Moolenaar42095212020-07-21 21:48:58 +0200873 let buf = RunVimInTerminal('', {'cols': 60})
874 call TermWait(buf, 100)
875 call term_sendkeys(buf, ":echo getwinpos(500)\<CR>")
Bram Moolenaara4b44262020-07-12 21:38:29 +0200876
877 " Find the output of getwinpos() in the bottom line.
878 let rows = term_getsize(buf)[0]
879 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
880 let line = term_getline(buf, rows)
881 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
882 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
883
884 " Position must be bigger than the getwinpos() result of Vim itself.
885 " The calculation in the console assumes a 10 x 7 character cell.
886 " In the GUI it can be more, let's assume a 20 x 14 cell.
887 " And then add 100 / 200 tolerance.
888 let [xroot, yroot] = getwinpos()
889 let winpos = 50->getwinpos()
890 call assert_equal(xroot, winpos[0])
891 call assert_equal(yroot, winpos[1])
Bram Moolenaar7dfc5ce2020-09-05 15:05:30 +0200892 let [winrow, wincol] = win_screenpos(0)
Bram Moolenaara4b44262020-07-12 21:38:29 +0200893 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
894 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
895 call assert_inrange(xroot + 2, xroot + xoff, xpos)
896 call assert_inrange(yroot + 2, yroot + yoff, ypos)
897
898 call TermWait(buf)
899 call term_sendkeys(buf, ":q\<CR>")
900 call StopVimInTerminal(buf)
Bram Moolenaara4b44262020-07-12 21:38:29 +0200901 set splitright&
902 only!
903endfunc
904
ichizokc3f91c02021-12-17 09:44:33 +0000905func Test_terminal_term_start_error()
906 func s:term_start_error() abort
907 try
908 return term_start([[]])
909 catch
910 return v:exception
911 finally
912 "
913 endtry
914 endfunc
915 autocmd WinEnter * call type(0)
916
917 " Must not crash in s:term_start_error, nor the exception thrown.
918 let result = s:term_start_error()
919 call assert_match('^Vim(return):E730:', result)
920
921 autocmd! WinEnter
922 delfunc s:term_start_error
923endfunc
924
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200925
926" vim: shiftwidth=2 sts=2 expandtab