blob: e982190168e94d256d744033aaf9c0d3ea8fb5b2 [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>")
104 call TermWait(buf, 100)
105 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>")
114 call TermWait(buf, 150)
115 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>")
141 call TermWait(buf, 150) " wait for terminal to vanish
142
143 call StopVimInTerminal(buf)
144 call delete('Xtext')
145 call delete('XtermPopup')
146endfunc
147
148" Check a terminal in popup window uses the default mininum size.
149func 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>")
312
313 let keys = ["\<F1>", "\<F2>", "\<F3>", "\<F4>", "\<F5>", "\<F6>", "\<F7>",
314 \ "\<F8>", "\<F9>", "\<F10>", "\<F11>", "\<F12>", "\<Home>",
315 \ "\<S-Home>", "\<C-Home>", "\<End>", "\<S-End>", "\<C-End>",
316 \ "\<Ins>", "\<Del>", "\<Left>", "\<S-Left>", "\<C-Left>", "\<Right>",
317 \ "\<S-Right>", "\<C-Right>", "\<Up>", "\<S-Up>", "\<Down>",
318 \ "\<S-Down>"]
319 let output = ['<F1>', '<F2>', '<F3>', '<F4>', '<F5>', '<F6>', '<F7>',
320 \ '<F8>', '<F9>', '<F10>', '<F11>', '<F12>', '<Home>', '<S-Home>',
321 \ '<C-Home>', '<End>', '<S-End>', '<C-End>', '<Insert>', '<Del>',
322 \ '<Left>', '<S-Left>', '<C-Left>', '<Right>', '<S-Right>',
323 \ '<C-Right>', '<Up>', '<S-Up>', '<Down>', '<S-Down>']
324
325 call term_sendkeys(buf, "i")
326 for i in range(len(keys))
327 call term_sendkeys(buf, "\<C-U>\<C-K>" .. keys[i])
328 call WaitForAssert({-> assert_equal(output[i], term_getline(buf, 1))})
329 endfor
330
331 let keypad_keys = ["\<k0>", "\<k1>", "\<k2>", "\<k3>", "\<k4>", "\<k5>",
332 \ "\<k6>", "\<k7>", "\<k8>", "\<k9>", "\<kPoint>", "\<kPlus>",
333 \ "\<kMinus>", "\<kMultiply>", "\<kDivide>"]
334 let keypad_output = ['0', '1', '2', '3', '4', '5',
335 \ '6', '7', '8', '9', '.', '+',
336 \ '-', '*', '/']
337 for i in range(len(keypad_keys))
338 " TODO: Mysteriously keypad 3 and 9 do not work on some systems.
339 if keypad_output[i] == '3' || keypad_output[i] == '9'
340 continue
341 endif
342 call term_sendkeys(buf, "\<C-U>" .. keypad_keys[i])
343 call WaitForAssert({-> assert_equal(keypad_output[i], term_getline(buf, 1))})
344 endfor
345
346 call feedkeys("\<C-U>\<kEnter>\<BS>one\<C-W>.two", 'xt')
347 call WaitForAssert({-> assert_equal('two', term_getline(buf, 1))})
348
349 call StopVimInTerminal(buf)
350endfunc
351
352" Test for using the mouse in a terminal
353func Test_term_mouse()
354 CheckNotGui
355 CheckRunVimInTerminal
356
357 let save_mouse = &mouse
358 let save_term = &term
359 let save_ttymouse = &ttymouse
360 let save_clipboard = &clipboard
361 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
362
363 let lines =<< trim END
364 one two three four five
365 red green yellow red blue
366 vim emacs sublime nano
367 END
368 call writefile(lines, 'Xtest_mouse')
369
370 " Create a terminal window running Vim for the test with mouse enabled
371 let prev_win = win_getid()
372 let buf = RunVimInTerminal('Xtest_mouse -n', {})
373 call term_sendkeys(buf, ":set nocompatible\<CR>")
374 call term_sendkeys(buf, ":set mouse=a term=xterm ttymouse=sgr\<CR>")
375 call term_sendkeys(buf, ":set clipboard=\<CR>")
376 call term_sendkeys(buf, ":set mousemodel=extend\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200377 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200378 redraw!
379
380 " Use the mouse to enter the terminal window
381 call win_gotoid(prev_win)
382 call feedkeys(MouseLeftClickCode(1, 1), 'x')
383 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
384 call assert_equal(1, getwininfo(win_getid())[0].terminal)
385
386 " Test for <LeftMouse> click/release
387 call test_setmouse(2, 5)
388 call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
389 call test_setmouse(3, 8)
390 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200391 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200392 call term_sendkeys(buf, ":call writefile([json_encode(getpos('.'))], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200393 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200394 let pos = json_decode(readfile('Xbuf')[0])
395 call assert_equal([3, 8], pos[1:2])
396
397 " Test for selecting text using mouse
398 call delete('Xbuf')
399 call test_setmouse(2, 11)
400 call term_sendkeys(buf, "\<LeftMouse>")
401 call test_setmouse(2, 16)
402 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200403 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200404 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200405 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200406 call assert_equal('yellow', readfile('Xbuf')[0])
407
408 " Test for selecting text using doubleclick
409 call delete('Xbuf')
410 call test_setmouse(1, 11)
411 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>")
412 call test_setmouse(1, 17)
413 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200414 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200415 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200416 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200417 call assert_equal('three four', readfile('Xbuf')[0])
418
419 " Test for selecting a line using triple click
420 call delete('Xbuf')
421 call test_setmouse(3, 2)
422 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200423 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200424 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200425 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200426 call assert_equal("vim emacs sublime nano\n", readfile('Xbuf')[0])
427
428 " Test for selecting a block using qudraple click
429 call delete('Xbuf')
430 call test_setmouse(1, 11)
431 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>")
432 call test_setmouse(3, 13)
433 call term_sendkeys(buf, "\<LeftRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200434 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200435 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200436 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200437 call assert_equal("ree\nyel\nsub", readfile('Xbuf')[0])
438
439 " Test for extending a selection using right click
440 call delete('Xbuf')
441 call test_setmouse(2, 9)
442 call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
443 call test_setmouse(2, 16)
444 call term_sendkeys(buf, "\<RightMouse>\<RightRelease>y")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200445 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200446 call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200447 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200448 call assert_equal("n yellow", readfile('Xbuf')[0])
449
450 " Test for pasting text using middle click
451 call delete('Xbuf')
452 call term_sendkeys(buf, ":let @r='bright '\<CR>")
453 call test_setmouse(2, 22)
454 call term_sendkeys(buf, "\"r\<MiddleMouse>\<MiddleRelease>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200455 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200456 call term_sendkeys(buf, ":call writefile([getline(2)], 'Xbuf')\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200457 call TermWait(buf, 50)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200458 call assert_equal("red bright blue", readfile('Xbuf')[0][-15:])
459
460 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200461 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200462 call StopVimInTerminal(buf)
463 let &mouse = save_mouse
464 let &term = save_term
465 let &ttymouse = save_ttymouse
466 let &clipboard = save_clipboard
467 set mousetime&
468 call delete('Xtest_mouse')
469 call delete('Xbuf')
470endfunc
471
472" Test for modeless selection in a terminal
473func Test_term_modeless_selection()
474 CheckUnix
475 CheckNotGui
476 CheckRunVimInTerminal
477 CheckFeature clipboard_working
478
479 let save_mouse = &mouse
480 let save_term = &term
481 let save_ttymouse = &ttymouse
482 set mouse=a term=xterm ttymouse=sgr mousetime=200
483 set clipboard=autoselectml
484
485 let lines =<< trim END
486 one two three four five
487 red green yellow red blue
488 vim emacs sublime nano
489 END
490 call writefile(lines, 'Xtest_modeless')
491
492 " Create a terminal window running Vim for the test with mouse disabled
493 let prev_win = win_getid()
494 let buf = RunVimInTerminal('Xtest_modeless -n', {})
495 call term_sendkeys(buf, ":set nocompatible\<CR>")
496 call term_sendkeys(buf, ":set mouse=\<CR>")
Bram Moolenaar733d2592020-08-20 18:59:06 +0200497 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200498 redraw!
499
500 " Use the mouse to enter the terminal window
501 call win_gotoid(prev_win)
502 call feedkeys(MouseLeftClickCode(1, 1), 'x')
503 call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
Bram Moolenaar733d2592020-08-20 18:59:06 +0200504 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200505 call assert_equal(1, getwininfo(win_getid())[0].terminal)
506
507 " Test for copying a modeless selection to clipboard
508 let @* = 'clean'
509 " communicating with X server may take a little time
510 sleep 100m
511 call feedkeys(MouseLeftClickCode(2, 3), 'x')
512 call feedkeys(MouseLeftDragCode(2, 11), 'x')
513 call feedkeys(MouseLeftReleaseCode(2, 11), 'x')
514 call assert_equal("d green y", @*)
515
516 " cleanup
Bram Moolenaar733d2592020-08-20 18:59:06 +0200517 call TermWait(buf)
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200518 call StopVimInTerminal(buf)
519 let &mouse = save_mouse
520 let &term = save_term
521 let &ttymouse = save_ttymouse
522 set mousetime& clipboard&
523 call delete('Xtest_modeless')
524 new | only!
525endfunc
526
Bram Moolenaara4b44262020-07-12 21:38:29 +0200527func Test_terminal_getwinpos()
528 CheckRunVimInTerminal
529
530 " split, go to the bottom-right window
531 split
532 wincmd j
533 set splitright
534
Bram Moolenaar42095212020-07-21 21:48:58 +0200535 let buf = RunVimInTerminal('', {'cols': 60})
536 call TermWait(buf, 100)
537 call term_sendkeys(buf, ":echo getwinpos(500)\<CR>")
Bram Moolenaara4b44262020-07-12 21:38:29 +0200538
539 " Find the output of getwinpos() in the bottom line.
540 let rows = term_getsize(buf)[0]
541 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
542 let line = term_getline(buf, rows)
543 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
544 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
545
546 " Position must be bigger than the getwinpos() result of Vim itself.
547 " The calculation in the console assumes a 10 x 7 character cell.
548 " In the GUI it can be more, let's assume a 20 x 14 cell.
549 " And then add 100 / 200 tolerance.
550 let [xroot, yroot] = getwinpos()
551 let winpos = 50->getwinpos()
552 call assert_equal(xroot, winpos[0])
553 call assert_equal(yroot, winpos[1])
554 let [winrow, wincol] = win_screenpos('.')
555 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
556 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
557 call assert_inrange(xroot + 2, xroot + xoff, xpos)
558 call assert_inrange(yroot + 2, yroot + yoff, ypos)
559
560 call TermWait(buf)
561 call term_sendkeys(buf, ":q\<CR>")
562 call StopVimInTerminal(buf)
Bram Moolenaara4b44262020-07-12 21:38:29 +0200563 exe buf . 'bwipe!'
564 set splitright&
565 only!
566endfunc
567
Bram Moolenaar18aa13d2020-07-11 13:09:36 +0200568
569" vim: shiftwidth=2 sts=2 expandtab