blob: eaf37e6745ec7681e58316e15e8b62dc0cd19488 [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
69func Test_terminal_in_popup()
70 CheckRunVimInTerminal
71
72 let text =<< trim END
73 some text
74 to edit
75 in a popup window
76 END
77 call writefile(text, 'Xtext')
78 let cmd = GetVimCommandCleanTerm()
79 let lines = [
80 \ 'call setline(1, range(20))',
81 \ 'hi PopTerm ctermbg=grey',
82 \ 'func OpenTerm(setColor)',
83 \ " set noruler",
84 \ " let s:buf = term_start('" .. cmd .. " Xtext', #{hidden: 1, term_finish: 'close'})",
85 \ ' let g:winid = popup_create(s:buf, #{minwidth: 45, minheight: 7, border: [], drag: 1, resize: 1})',
86 \ ' if a:setColor',
87 \ ' call win_execute(g:winid, "set wincolor=PopTerm")',
88 \ ' endif',
89 \ 'endfunc',
90 \ 'func HidePopup()',
91 \ ' call popup_hide(g:winid)',
92 \ 'endfunc',
93 \ 'func ClosePopup()',
94 \ ' call popup_close(g:winid)',
95 \ 'endfunc',
96 \ 'func ReopenPopup()',
97 \ ' call popup_create(s:buf, #{minwidth: 40, minheight: 6, border: []})',
98 \ 'endfunc',
99 \ ]
100 call writefile(lines, 'XtermPopup')
101 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
102 call TermWait(buf, 100)
103 call term_sendkeys(buf, ":call OpenTerm(0)\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100104 call TermWait(buf, 500)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200105 call term_sendkeys(buf, ":\<CR>")
106 call TermWait(buf, 100)
107 call term_sendkeys(buf, "\<C-W>:echo getwinvar(g:winid, \"&buftype\") win_gettype(g:winid)\<CR>")
108 call VerifyScreenDump(buf, 'Test_terminal_popup_1', {})
109
110 call term_sendkeys(buf, ":q\<CR>")
111 call VerifyScreenDump(buf, 'Test_terminal_popup_2', {})
112
113 call term_sendkeys(buf, ":call OpenTerm(1)\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100114 call TermWait(buf, 500)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200115 call term_sendkeys(buf, ":set hlsearch\<CR>")
116 call TermWait(buf, 100)
117 call term_sendkeys(buf, "/edit\<CR>")
118 call VerifyScreenDump(buf, 'Test_terminal_popup_3', {})
119
120 call term_sendkeys(buf, "\<C-W>:call HidePopup()\<CR>")
121 call VerifyScreenDump(buf, 'Test_terminal_popup_4', {})
122 call term_sendkeys(buf, "\<CR>")
123 call TermWait(buf, 50)
124
125 call term_sendkeys(buf, "\<C-W>:call ClosePopup()\<CR>")
126 call VerifyScreenDump(buf, 'Test_terminal_popup_5', {})
127
128 call term_sendkeys(buf, "\<C-W>:call ReopenPopup()\<CR>")
129 call VerifyScreenDump(buf, 'Test_terminal_popup_6', {})
130
131 " Go to terminal-Normal mode and visually select text.
132 call term_sendkeys(buf, "\<C-W>Ngg/in\<CR>vww")
133 call VerifyScreenDump(buf, 'Test_terminal_popup_7', {})
134
135 " Back to job mode, redraws
136 call term_sendkeys(buf, "A")
137 call VerifyScreenDump(buf, 'Test_terminal_popup_8', {})
138
139 call TermWait(buf, 50)
140 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100141 call TermWait(buf, 250) " wait for terminal to vanish
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200142
143 call StopVimInTerminal(buf)
144 call delete('Xtext')
145 call delete('XtermPopup')
146endfunc
147
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100148" Check a terminal in popup window uses the default minimum size.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200149func Test_terminal_in_popup_min_size()
150 CheckRunVimInTerminal
151
152 let text =<< trim END
153 another text
154 to show
155 in a popup window
156 END
157 call writefile(text, 'Xtext')
158 let lines = [
159 \ 'call setline(1, range(20))',
160 \ 'func OpenTerm()',
161 \ " let s:buf = term_start('cat Xtext', #{hidden: 1})",
162 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
163 \ 'endfunc',
164 \ ]
165 call writefile(lines, 'XtermPopup')
166 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
167 call TermWait(buf, 100)
168 call term_sendkeys(buf, ":set noruler\<CR>")
169 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
170 call TermWait(buf, 50)
171 call term_sendkeys(buf, ":\<CR>")
172 call VerifyScreenDump(buf, 'Test_terminal_popup_m1', {})
173
174 call TermWait(buf, 50)
175 call term_sendkeys(buf, ":q\<CR>")
176 call TermWait(buf, 50) " wait for terminal to vanish
177 call StopVimInTerminal(buf)
178 call delete('Xtext')
179 call delete('XtermPopup')
180endfunc
181
182" Check a terminal in popup window with different colors
183func Terminal_in_popup_colored(group_name, highlight_cmd, highlight_opt)
184 CheckRunVimInTerminal
185 CheckUnix
186
187 let lines = [
188 \ 'call setline(1, range(20))',
189 \ 'func OpenTerm()',
190 \ " let s:buf = term_start('cat', #{hidden: 1, "
191 \ .. a:highlight_opt .. "})",
192 \ ' let g:winid = popup_create(s:buf, #{ border: []})',
193 \ 'endfunc',
194 \ a:highlight_cmd,
195 \ ]
196 call writefile(lines, 'XtermPopup')
197 let buf = RunVimInTerminal('-S XtermPopup', #{rows: 15})
198 call TermWait(buf, 100)
199 call term_sendkeys(buf, ":set noruler\<CR>")
200 call term_sendkeys(buf, ":call OpenTerm()\<CR>")
201 call TermWait(buf, 50)
202 call term_sendkeys(buf, "hello\<CR>")
203 call VerifyScreenDump(buf, 'Test_terminal_popup_' .. a:group_name, {})
204
205 call term_sendkeys(buf, "\<C-D>")
206 call TermWait(buf, 50)
207 call term_sendkeys(buf, ":q\<CR>")
208 call TermWait(buf, 50) " wait for terminal to vanish
209 call StopVimInTerminal(buf)
210 call delete('XtermPopup')
211endfunc
212
213func Test_terminal_in_popup_colored_Terminal()
214 call Terminal_in_popup_colored("Terminal", "highlight Terminal ctermfg=blue ctermbg=yellow", "")
215endfunc
216
217func Test_terminal_in_popup_colored_group()
218 call Terminal_in_popup_colored("MyTermCol", "highlight MyTermCol ctermfg=darkgreen ctermbg=lightblue", "term_highlight: 'MyTermCol',")
219endfunc
220
221func Test_double_popup_terminal()
222 let buf1 = term_start(&shell, #{hidden: 1})
223 let win1 = popup_create(buf1, {})
224 let buf2 = term_start(&shell, #{hidden: 1})
225 call assert_fails('call popup_create(buf2, {})', 'E861:')
226 call popup_close(win1)
227 exe buf1 .. 'bwipe!'
228 exe buf2 .. 'bwipe!'
229endfunc
230
231func Test_issue_5607()
232 let wincount = winnr('$')
233 exe 'terminal' &shell &shellcmdflag 'exit'
234 let job = term_getjob(bufnr())
235 call WaitForAssert({-> assert_equal("dead", job_status(job))})
236
237 let old_wincolor = &wincolor
238 try
239 set wincolor=
240 finally
241 let &wincolor = old_wincolor
242 bw!
243 endtry
244endfunc
245
246func Test_hidden_terminal()
247 let buf = term_start(&shell, #{hidden: 1})
248 call assert_equal('', bufname('^$'))
249 call StopShellInTerminal(buf)
250endfunc
251
252func Test_term_nasty_callback()
253 CheckExecutable sh
254
255 set hidden
256 let g:buf0 = term_start('sh', #{hidden: 1, term_finish: 'close'})
257 call popup_create(g:buf0, {})
258 call assert_fails("call term_start(['sh', '-c'], #{curwin: 1})", 'E863:')
259
260 call popup_clear(1)
261 set hidden&
262endfunc
263
264func Test_term_and_startinsert()
265 CheckRunVimInTerminal
266 CheckUnix
267
268 let lines =<< trim EOL
269 put='some text'
270 term
271 startinsert
272 EOL
273 call writefile(lines, 'XTest_startinsert')
274 let buf = RunVimInTerminal('-S XTest_startinsert', {})
275
276 call term_sendkeys(buf, "exit\r")
277 call WaitForAssert({-> assert_equal("some text", term_getline(buf, 1))})
278 call term_sendkeys(buf, "0l")
279 call term_sendkeys(buf, "A<\<Esc>")
280 call WaitForAssert({-> assert_equal("some text<", term_getline(buf, 1))})
281
282 call StopVimInTerminal(buf)
283 call delete('XTest_startinsert')
284endfunc
285
286" Test for passing invalid arguments to terminal functions
287func Test_term_func_invalid_arg()
288 call assert_fails('let b = term_getaltscreen([])', 'E745:')
289 call assert_fails('let a = term_getattr(1, [])', 'E730:')
290 call assert_fails('let c = term_getcursor([])', 'E745:')
291 call assert_fails('let l = term_getline([], 1)', 'E745:')
292 call assert_fails('let l = term_getscrolled([])', 'E745:')
293 call assert_fails('let s = term_getsize([])', 'E745:')
294 call assert_fails('let s = term_getstatus([])', 'E745:')
295 call assert_fails('let s = term_scrape([], 1)', 'E745:')
296 call assert_fails('call term_sendkeys([], "a")', 'E745:')
297 call assert_fails('call term_setapi([], "")', 'E745:')
298 call assert_fails('call term_setrestore([], "")', 'E745:')
299 call assert_fails('call term_setkill([], "")', 'E745:')
300 if has('gui') || has('termguicolors')
301 call assert_fails('let p = term_getansicolors([])', 'E745:')
302 call assert_fails('call term_setansicolors([], [])', 'E745:')
303 endif
304endfunc
305
306" Test for sending various special keycodes to a terminal
307func Test_term_keycode_translation()
308 CheckRunVimInTerminal
309
310 let buf = RunVimInTerminal('', {})
311 call term_sendkeys(buf, ":set nocompatible\<CR>")
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100312 call term_sendkeys(buf, ":set timeoutlen=20\<CR>")
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200313
314 let keys = ["\<F1>", "\<F2>", "\<F3>", "\<F4>", "\<F5>", "\<F6>", "\<F7>",
315 \ "\<F8>", "\<F9>", "\<F10>", "\<F11>", "\<F12>", "\<Home>",
316 \ "\<S-Home>", "\<C-Home>", "\<End>", "\<S-End>", "\<C-End>",
317 \ "\<Ins>", "\<Del>", "\<Left>", "\<S-Left>", "\<C-Left>", "\<Right>",
318 \ "\<S-Right>", "\<C-Right>", "\<Up>", "\<S-Up>", "\<Down>",
319 \ "\<S-Down>"]
320 let output = ['<F1>', '<F2>', '<F3>', '<F4>', '<F5>', '<F6>', '<F7>',
321 \ '<F8>', '<F9>', '<F10>', '<F11>', '<F12>', '<Home>', '<S-Home>',
322 \ '<C-Home>', '<End>', '<S-End>', '<C-End>', '<Insert>', '<Del>',
323 \ '<Left>', '<S-Left>', '<C-Left>', '<Right>', '<S-Right>',
324 \ '<C-Right>', '<Up>', '<S-Up>', '<Down>', '<S-Down>']
325
326 call term_sendkeys(buf, "i")
327 for i in range(len(keys))
328 call term_sendkeys(buf, "\<C-U>\<C-K>" .. keys[i])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100329 call WaitForAssert({-> assert_equal(output[i], term_getline(buf, 1))}, 200)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200330 endfor
331
332 let keypad_keys = ["\<k0>", "\<k1>", "\<k2>", "\<k3>", "\<k4>", "\<k5>",
333 \ "\<k6>", "\<k7>", "\<k8>", "\<k9>", "\<kPoint>", "\<kPlus>",
334 \ "\<kMinus>", "\<kMultiply>", "\<kDivide>"]
335 let keypad_output = ['0', '1', '2', '3', '4', '5',
336 \ '6', '7', '8', '9', '.', '+',
337 \ '-', '*', '/']
338 for i in range(len(keypad_keys))
339 " TODO: Mysteriously keypad 3 and 9 do not work on some systems.
340 if keypad_output[i] == '3' || keypad_output[i] == '9'
341 continue
342 endif
343 call term_sendkeys(buf, "\<C-U>" .. keypad_keys[i])
Bram Moolenaar4d8c96d2020-12-29 20:53:33 +0100344 call WaitForAssert({-> assert_equal(keypad_output[i], term_getline(buf, 1))}, 100)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200345 endfor
346
347 call feedkeys("\<C-U>\<kEnter>\<BS>one\<C-W>.two", 'xt')
348 call WaitForAssert({-> assert_equal('two', term_getline(buf, 1))})
349
350 call StopVimInTerminal(buf)
351endfunc
352
353" Test for using the mouse in a terminal
354func Test_term_mouse()
355 CheckNotGui
356 CheckRunVimInTerminal
357
358 let save_mouse = &mouse
359 let save_term = &term
360 let save_ttymouse = &ttymouse
361 let save_clipboard = &clipboard
362 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
363
364 let lines =<< trim END
365 one two three four five
366 red green yellow red blue
367 vim emacs sublime nano
368 END
369 call writefile(lines, 'Xtest_mouse')
370
371 " Create a terminal window running Vim for the test with mouse enabled
372 let prev_win = win_getid()
373 let buf = RunVimInTerminal('Xtest_mouse -n', {})
374 call term_sendkeys(buf, ":set nocompatible\<CR>")
375 call term_sendkeys(buf, ":set mouse=a term=xterm ttymouse=sgr\<CR>")
376 call term_sendkeys(buf, ":set clipboard=\<CR>")
377 call term_sendkeys(buf, ":set mousemodel=extend\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200378 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200379 redraw!
380
381 " Use the mouse to enter the terminal window
382 call win_gotoid(prev_win)
383 call feedkeys(MouseLeftClickCode(1, 1), 'x')
384 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
385 call assert_equal(1, getwininfo(win_getid())[0].terminal)
386
387 " Test for <LeftMouse> click/release
388 call test_setmouse(2, 5)
389 call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
390 call test_setmouse(3, 8)
391 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200392 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200393 call term_sendkeys(buf, ":call writefile([json_encode(getpos('.'))], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200394 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200395 let pos = json_decode(readfile('Xbuf')[0])
396 call assert_equal([3, 8], pos[1:2])
397
398 " Test for selecting text using mouse
399 call delete('Xbuf')
400 call test_setmouse(2, 11)
401 call term_sendkeys(buf, "\<LeftMouse>")
402 call test_setmouse(2, 16)
403 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200404 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200405 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200406 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200407 call assert_equal('yellow', readfile('Xbuf')[0])
408
409 " Test for selecting text using doubleclick
410 call delete('Xbuf')
411 call test_setmouse(1, 11)
412 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>")
413 call test_setmouse(1, 17)
414 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200415 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200416 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200417 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200418 call assert_equal('three four', readfile('Xbuf')[0])
419
420 " Test for selecting a line using triple click
421 call delete('Xbuf')
422 call test_setmouse(3, 2)
423 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200424 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200425 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200426 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200427 call assert_equal("vim emacs sublime nano\n", readfile('Xbuf')[0])
428
429 " Test for selecting a block using qudraple click
430 call delete('Xbuf')
431 call test_setmouse(1, 11)
432 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>")
433 call test_setmouse(3, 13)
434 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200435 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200436 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200437 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200438 call assert_equal("ree\nyel\nsub", readfile('Xbuf')[0])
439
440 " Test for extending a selection using right click
441 call delete('Xbuf')
442 call test_setmouse(2, 9)
443 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
444 call test_setmouse(2, 16)
445 call term_sendkeys(buf, "\<RightMouse>\<RightRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200446 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200447 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200448 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200449 call assert_equal("n yellow", readfile('Xbuf')[0])
450
451 " Test for pasting text using middle click
452 call delete('Xbuf')
453 call term_sendkeys(buf, ":let @r='bright '\<CR>")
454 call test_setmouse(2, 22)
455 call term_sendkeys(buf, "\"r\<MiddleMouse>\<MiddleRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200456 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200457 call term_sendkeys(buf, ":call writefile([getline(2)], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200458 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200459 call assert_equal("red bright blue", readfile('Xbuf')[0][-15:])
460
461 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200462 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200463 call StopVimInTerminal(buf)
464 let &mouse = save_mouse
465 let &term = save_term
466 let &ttymouse = save_ttymouse
467 let &clipboard = save_clipboard
468 set mousetime&
469 call delete('Xtest_mouse')
470 call delete('Xbuf')
471endfunc
472
473" Test for modeless selection in a terminal
474func Test_term_modeless_selection()
475 CheckUnix
476 CheckNotGui
477 CheckRunVimInTerminal
478 CheckFeature clipboard_working
479
480 let save_mouse = &mouse
481 let save_term = &term
482 let save_ttymouse = &ttymouse
483 set mouse=a term=xterm ttymouse=sgr mousetime=200
484 set clipboard=autoselectml
485
486 let lines =<< trim END
487 one two three four five
488 red green yellow red blue
489 vim emacs sublime nano
490 END
491 call writefile(lines, 'Xtest_modeless')
492
493 " Create a terminal window running Vim for the test with mouse disabled
494 let prev_win = win_getid()
495 let buf = RunVimInTerminal('Xtest_modeless -n', {})
496 call term_sendkeys(buf, ":set nocompatible\<CR>")
497 call term_sendkeys(buf, ":set mouse=\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200498 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200499 redraw!
500
501 " Use the mouse to enter the terminal window
502 call win_gotoid(prev_win)
503 call feedkeys(MouseLeftClickCode(1, 1), 'x')
504 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200505 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200506 call assert_equal(1, getwininfo(win_getid())[0].terminal)
507
508 " Test for copying a modeless selection to clipboard
509 let @* = 'clean'
510 " communicating with X server may take a little time
511 sleep 100m
512 call feedkeys(MouseLeftClickCode(2, 3), 'x')
513 call feedkeys(MouseLeftDragCode(2, 11), 'x')
514 call feedkeys(MouseLeftReleaseCode(2, 11), 'x')
515 call assert_equal("d green y", @*)
516
517 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200518 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200519 call StopVimInTerminal(buf)
520 let &mouse = save_mouse
521 let &term = save_term
522 let &ttymouse = save_ttymouse
523 set mousetime& clipboard&
524 call delete('Xtest_modeless')
525 new | only!
526endfunc
527
Bram Moolenaara4b44262020-07-12 21:38:29 +0200528func Test_terminal_getwinpos()
529 CheckRunVimInTerminal
530
531 " split, go to the bottom-right window
532 split
533 wincmd j
534 set splitright
535
Bram Moolenaar42095212020-07-21 21:48:58 +0200536 let buf = RunVimInTerminal('', {'cols': 60})
537 call TermWait(buf, 100)
538 call term_sendkeys(buf, ":echo getwinpos(500)\<CR>")
Bram Moolenaara4b44262020-07-12 21:38:29 +0200539
540 " Find the output of getwinpos() in the bottom line.
541 let rows = term_getsize(buf)[0]
542 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
543 let line = term_getline(buf, rows)
544 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
545 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
546
547 " Position must be bigger than the getwinpos() result of Vim itself.
548 " The calculation in the console assumes a 10 x 7 character cell.
549 " In the GUI it can be more, let's assume a 20 x 14 cell.
550 " And then add 100 / 200 tolerance.
551 let [xroot, yroot] = getwinpos()
552 let winpos = 50->getwinpos()
553 call assert_equal(xroot, winpos[0])
554 call assert_equal(yroot, winpos[1])
Bram Moolenaar7dfc5ce2020-09-05 15:05:30 +0200555 let [winrow, wincol] = win_screenpos(0)
Bram Moolenaara4b44262020-07-12 21:38:29 +0200556 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
557 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
558 call assert_inrange(xroot + 2, xroot + xoff, xpos)
559 call assert_inrange(yroot + 2, yroot + yoff, ypos)
560
561 call TermWait(buf)
562 call term_sendkeys(buf, ":q\<CR>")
563 call StopVimInTerminal(buf)
Bram Moolenaara4b44262020-07-12 21:38:29 +0200564 set splitright&
565 only!
566endfunc
567
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200568
569" vim: shiftwidth=2 sts=2 expandtab