1" Tests for the terminal window.
2" This is split in two, because it can take a lot of time.
3" See test_terminal.vim and test_terminal2.vim for further tests.
4
5source check.vim
6CheckFeature terminal
7
8source shared.vim
9source screendump.vim
10source mouse.vim
11source term_util.vim
12
13let $PROMPT_COMMAND=''
14
15func Test_terminal_altscreen()
16  " somehow doesn't work on MS-Windows
17  CheckUnix
18  let cmd = "cat Xtext\<CR>"
19
20  let buf = term_start(&shell, {})
21  call writefile(["\<Esc>[?1047h"], 'Xtext')
22  call term_sendkeys(buf, cmd)
23  call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
24
25  call writefile(["\<Esc>[?1047l"], 'Xtext')
26  call term_sendkeys(buf, cmd)
27  call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
28
29  call term_sendkeys(buf, "exit\r")
30  exe buf . "bwipe!"
31  call delete('Xtext')
32endfunc
33
34func Test_terminal_shell_option()
35  if has('unix')
36    " exec is a shell builtin command, should fail without a shell.
37    term exec ls runtest.vim
38    call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
39    bwipe!
40
41    term ++shell exec ls runtest.vim
42    call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
43    bwipe!
44  elseif has('win32')
45    " dir is a shell builtin command, should fail without a shell.
46    " However, if dir.exe (which might be provided by Cygwin/MSYS2) exists in
47    " the %PATH%, "term dir" succeeds unintentionally.  Use dir.com instead.
48    try
49      term dir.com /b runtest.vim
50      call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
51    catch /CreateProcess/
52      " ignore
53    endtry
54    bwipe!
55
56    " This should execute the dir builtin command even with ".com".
57    term ++shell dir.com /b runtest.vim
58    call WaitForAssert({-> assert_match('runtest.vim', term_getline(bufnr(), 1))})
59    bwipe!
60  else
61    throw 'Skipped: does not work on this platform'
62  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, 500)
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, 500)
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, 250)  " 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 minimum 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
304  let buf = term_start('echo')
305  call assert_fails('call term_setapi(' .. buf .. ', {})', 'E731:')
306  call assert_fails('call term_setkill(' .. buf .. ', {})', 'E731:')
307  call assert_fails('call term_setrestore(' .. buf .. ', {})', 'E731:')
308  exe buf . "bwipe!"
309endfunc
310
311" Test for sending various special keycodes to a terminal
312func Test_term_keycode_translation()
313  CheckRunVimInTerminal
314
315  let buf = RunVimInTerminal('', {})
316  call term_sendkeys(buf, ":set nocompatible\<CR>")
317  call term_sendkeys(buf, ":set timeoutlen=20\<CR>")
318
319  let keys = ["\<F1>", "\<F2>", "\<F3>", "\<F4>", "\<F5>", "\<F6>", "\<F7>",
320        \ "\<F8>", "\<F9>", "\<F10>", "\<F11>", "\<F12>", "\<Home>",
321        \ "\<S-Home>", "\<C-Home>", "\<End>", "\<S-End>", "\<C-End>",
322	\ "\<Ins>", "\<Del>", "\<Left>", "\<S-Left>", "\<C-Left>", "\<Right>",
323        \ "\<S-Right>", "\<C-Right>", "\<Up>", "\<S-Up>", "\<Down>",
324        \ "\<S-Down>"]
325  let output = ['<F1>', '<F2>', '<F3>', '<F4>', '<F5>', '<F6>', '<F7>',
326        \ '<F8>', '<F9>', '<F10>', '<F11>', '<F12>', '<Home>', '<S-Home>',
327        \ '<C-Home>', '<End>', '<S-End>', '<C-End>', '<Insert>', '<Del>',
328        \ '<Left>', '<S-Left>', '<C-Left>', '<Right>', '<S-Right>',
329        \ '<C-Right>', '<Up>', '<S-Up>', '<Down>', '<S-Down>']
330
331  call term_sendkeys(buf, "i")
332  for i in range(len(keys))
333    call term_sendkeys(buf, "\<C-U>\<C-K>" .. keys[i])
334    call WaitForAssert({-> assert_equal(output[i], term_getline(buf, 1))}, 200)
335  endfor
336
337  let keypad_keys = ["\<k0>", "\<k1>", "\<k2>", "\<k3>", "\<k4>", "\<k5>",
338        \ "\<k6>", "\<k7>", "\<k8>", "\<k9>", "\<kPoint>", "\<kPlus>",
339        \ "\<kMinus>", "\<kMultiply>", "\<kDivide>"]
340  let keypad_output = ['0', '1', '2', '3', '4', '5',
341        \ '6', '7', '8', '9', '.', '+',
342        \ '-', '*', '/']
343  for i in range(len(keypad_keys))
344    " TODO: Mysteriously keypad 3 and 9 do not work on some systems.
345    if keypad_output[i] == '3' || keypad_output[i] == '9'
346      continue
347    endif
348    call term_sendkeys(buf, "\<C-U>" .. keypad_keys[i])
349    call WaitForAssert({-> assert_equal(keypad_output[i], term_getline(buf, 1))}, 100)
350  endfor
351
352  call feedkeys("\<C-U>\<kEnter>\<BS>one\<C-W>.two", 'xt')
353  call WaitForAssert({-> assert_equal('two', term_getline(buf, 1))})
354
355  call StopVimInTerminal(buf)
356endfunc
357
358" Test for using the mouse in a terminal
359func Test_term_mouse()
360  CheckNotGui
361  CheckRunVimInTerminal
362
363  let save_mouse = &mouse
364  let save_term = &term
365  let save_ttymouse = &ttymouse
366  let save_clipboard = &clipboard
367  set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
368
369  let lines =<< trim END
370    one two three four five
371    red green yellow red blue
372    vim emacs sublime nano
373  END
374  call writefile(lines, 'Xtest_mouse')
375
376  " Create a terminal window running Vim for the test with mouse enabled
377  let prev_win = win_getid()
378  let buf = RunVimInTerminal('Xtest_mouse -n', {})
379  call term_sendkeys(buf, ":set nocompatible\<CR>")
380  call term_sendkeys(buf, ":set mouse=a term=xterm ttymouse=sgr\<CR>")
381  call term_sendkeys(buf, ":set clipboard=\<CR>")
382  call term_sendkeys(buf, ":set mousemodel=extend\<CR>")
383  call TermWait(buf)
384  redraw!
385
386  " Use the mouse to enter the terminal window
387  call win_gotoid(prev_win)
388  call feedkeys(MouseLeftClickCode(1, 1), 'x')
389  call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
390  call assert_equal(1, getwininfo(win_getid())[0].terminal)
391
392  " Test for <LeftMouse> click/release
393  call test_setmouse(2, 5)
394  call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
395  call test_setmouse(3, 8)
396  call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
397  call TermWait(buf, 50)
398  call term_sendkeys(buf, ":call writefile([json_encode(getpos('.'))], 'Xbuf')\<CR>")
399  call TermWait(buf, 50)
400  let pos = json_decode(readfile('Xbuf')[0])
401  call assert_equal([3, 8], pos[1:2])
402
403  " Test for selecting text using mouse
404  call delete('Xbuf')
405  call test_setmouse(2, 11)
406  call term_sendkeys(buf, "\<LeftMouse>")
407  call test_setmouse(2, 16)
408  call term_sendkeys(buf, "\<LeftRelease>y")
409  call TermWait(buf, 50)
410  call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
411  call TermWait(buf, 50)
412  call assert_equal('yellow', readfile('Xbuf')[0])
413
414  " Test for selecting text using doubleclick
415  call delete('Xbuf')
416  call test_setmouse(1, 11)
417  call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>")
418  call test_setmouse(1, 17)
419  call term_sendkeys(buf, "\<LeftRelease>y")
420  call TermWait(buf, 50)
421  call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
422  call TermWait(buf, 50)
423  call assert_equal('three four', readfile('Xbuf')[0])
424
425  " Test for selecting a line using triple click
426  call delete('Xbuf')
427  call test_setmouse(3, 2)
428  call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>y")
429  call TermWait(buf, 50)
430  call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
431  call TermWait(buf, 50)
432  call assert_equal("vim emacs sublime nano\n", readfile('Xbuf')[0])
433
434  " Test for selecting a block using qudraple click
435  call delete('Xbuf')
436  call test_setmouse(1, 11)
437  call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>\<LeftRelease>\<LeftMouse>")
438  call test_setmouse(3, 13)
439  call term_sendkeys(buf, "\<LeftRelease>y")
440  call TermWait(buf, 50)
441  call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
442  call TermWait(buf, 50)
443  call assert_equal("ree\nyel\nsub", readfile('Xbuf')[0])
444
445  " Test for extending a selection using right click
446  call delete('Xbuf')
447  call test_setmouse(2, 9)
448  call term_sendkeys(buf, "\<LeftMouse>\<LeftRelease>")
449  call test_setmouse(2, 16)
450  call term_sendkeys(buf, "\<RightMouse>\<RightRelease>y")
451  call TermWait(buf, 50)
452  call term_sendkeys(buf, ":call writefile([@\"], 'Xbuf')\<CR>")
453  call TermWait(buf, 50)
454  call assert_equal("n yellow", readfile('Xbuf')[0])
455
456  " Test for pasting text using middle click
457  call delete('Xbuf')
458  call term_sendkeys(buf, ":let @r='bright '\<CR>")
459  call test_setmouse(2, 22)
460  call term_sendkeys(buf, "\"r\<MiddleMouse>\<MiddleRelease>")
461  call TermWait(buf, 50)
462  call term_sendkeys(buf, ":call writefile([getline(2)], 'Xbuf')\<CR>")
463  call TermWait(buf, 50)
464  call assert_equal("red bright blue", readfile('Xbuf')[0][-15:])
465
466  " cleanup
467  call TermWait(buf)
468  call StopVimInTerminal(buf)
469  let &mouse = save_mouse
470  let &term = save_term
471  let &ttymouse = save_ttymouse
472  let &clipboard = save_clipboard
473  set mousetime&
474  call delete('Xtest_mouse')
475  call delete('Xbuf')
476endfunc
477
478" Test for sync buffer cwd with shell's pwd
479func Test_terminal_sync_shell_dir()
480  CheckUnix
481  " The test always use sh (see src/testdir/unix.vim).
482  " However, BSD's sh doesn't seem to play well with OSC 7 escape sequence.
483  CheckNotBSD
484
485  set asd
486  " , is
487  "  1. a valid character for directory names
488  "  2. a reserved character in url-encoding
489  let chars = ",a"
490  " "," is url-encoded as '%2C'
491  let chars_url = "%2Ca"
492  let tmpfolder = fnamemodify(tempname(),':h').'/'.chars
493  let tmpfolder_url = fnamemodify(tempname(),':h').'/'.chars_url
494  call mkdir(tmpfolder, "p")
495  let buf = Run_shell_in_terminal({})
496  call term_sendkeys(buf, "echo -ne $'\\e\]7;file://".tmpfolder_url."\\a'\<CR>")
497  "call term_sendkeys(buf, "cd ".tmpfolder."\<CR>")
498  call TermWait(buf)
499  if has("mac")
500    let expected = "/private".tmpfolder
501  else
502    let expected = tmpfolder
503  endif
504  call assert_equal(expected, getcwd(winnr()))
505
506  set noasd
507endfunc
508
509" Test for modeless selection in a terminal
510func Test_term_modeless_selection()
511  CheckUnix
512  CheckNotGui
513  CheckRunVimInTerminal
514  CheckFeature clipboard_working
515
516  let save_mouse = &mouse
517  let save_term = &term
518  let save_ttymouse = &ttymouse
519  set mouse=a term=xterm ttymouse=sgr mousetime=200
520  set clipboard=autoselectml
521
522  let lines =<< trim END
523    one two three four five
524    red green yellow red blue
525    vim emacs sublime nano
526  END
527  call writefile(lines, 'Xtest_modeless')
528
529  " Create a terminal window running Vim for the test with mouse disabled
530  let prev_win = win_getid()
531  let buf = RunVimInTerminal('Xtest_modeless -n', {})
532  call term_sendkeys(buf, ":set nocompatible\<CR>")
533  call term_sendkeys(buf, ":set mouse=\<CR>")
534  call TermWait(buf)
535  redraw!
536
537  " Use the mouse to enter the terminal window
538  call win_gotoid(prev_win)
539  call feedkeys(MouseLeftClickCode(1, 1), 'x')
540  call feedkeys(MouseLeftReleaseCode(1, 1), 'x')
541  call TermWait(buf)
542  call assert_equal(1, getwininfo(win_getid())[0].terminal)
543
544  " Test for copying a modeless selection to clipboard
545  let @* = 'clean'
546  " communicating with X server may take a little time
547  sleep 100m
548  call feedkeys(MouseLeftClickCode(2, 3), 'x')
549  call feedkeys(MouseLeftDragCode(2, 11), 'x')
550  call feedkeys(MouseLeftReleaseCode(2, 11), 'x')
551  call assert_equal("d green y", @*)
552
553  " cleanup
554  call TermWait(buf)
555  call StopVimInTerminal(buf)
556  let &mouse = save_mouse
557  let &term = save_term
558  let &ttymouse = save_ttymouse
559  set mousetime& clipboard&
560  call delete('Xtest_modeless')
561  new | only!
562endfunc
563
564func Test_terminal_getwinpos()
565  CheckRunVimInTerminal
566
567  " split, go to the bottom-right window
568  split
569  wincmd j
570  set splitright
571
572  let buf = RunVimInTerminal('', {'cols': 60})
573  call TermWait(buf, 100)
574  call term_sendkeys(buf, ":echo getwinpos(500)\<CR>")
575
576  " Find the output of getwinpos() in the bottom line.
577  let rows = term_getsize(buf)[0]
578  call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
579  let line = term_getline(buf, rows)
580  let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
581  let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
582
583  " Position must be bigger than the getwinpos() result of Vim itself.
584  " The calculation in the console assumes a 10 x 7 character cell.
585  " In the GUI it can be more, let's assume a 20 x 14 cell.
586  " And then add 100 / 200 tolerance.
587  let [xroot, yroot] = getwinpos()
588  let winpos = 50->getwinpos()
589  call assert_equal(xroot, winpos[0])
590  call assert_equal(yroot, winpos[1])
591  let [winrow, wincol] = win_screenpos(0)
592  let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
593  let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
594  call assert_inrange(xroot + 2, xroot + xoff, xpos)
595  call assert_inrange(yroot + 2, yroot + yoff, ypos)
596
597  call TermWait(buf)
598  call term_sendkeys(buf, ":q\<CR>")
599  call StopVimInTerminal(buf)
600  set splitright&
601  only!
602endfunc
603
604
605" vim: shiftwidth=2 sts=2 expandtab
606