blob: b9d6a062c36103be3a73769a020dad0226d31150 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
3if !has('textprop')
4 finish
5endif
6
7source screendump.vim
8
9func Test_simple_popup()
10 if !CanRunVimInTerminal()
11 return
12 endif
13 call writefile([
14 \ "call setline(1, range(1, 100))",
Bram Moolenaar20c023a2019-05-26 21:03:24 +020015 \ "hi PopupColor1 ctermbg=lightblue",
16 \ "hi PopupColor2 ctermbg=lightcyan",
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020017 \ "hi Comment ctermfg=red",
18 \ "call prop_type_add('comment', {'highlight': 'Comment'})",
Bram Moolenaar60cdb302019-05-27 21:54:10 +020019 \ "let winid = popup_create('hello there', {'line': 3, 'col': 11, 'minwidth': 20, 'highlight': 'PopupColor1'})",
20 \ "let winid2 = popup_create(['another one', 'another two', 'another three'], {'line': 3, 'col': 25, 'minwidth': 20})",
Bram Moolenaar20c023a2019-05-26 21:03:24 +020021 \ "call setwinvar(winid2, '&wincolor', 'PopupColor2')",
Bram Moolenaar4d784b22019-05-25 19:51:39 +020022 \], 'XtestPopup')
23 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
24 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020029 \ .. "{'text': 'other tab'},"
30 \ .. "{'text': 'a comment line', 'props': [{"
Bram Moolenaar60cdb302019-05-27 21:54:10 +020031 \ .. "'col': 3, 'length': 7, 'minwidth': 20, 'type': 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar60cdb302019-05-27 21:54:10 +020033 \ .. "], {'line': 4, 'col': 9, 'minwidth': 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
35
36 " switch back to first tabpage
37 call term_sendkeys(buf, "gt")
38 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
39
40 " close that tabpage
41 call term_sendkeys(buf, ":quit!\<CR>")
42 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
43
Bram Moolenaar17146962019-05-30 00:12:11 +020044 " resize popup, show empty line at bottom
Bram Moolenaar60cdb302019-05-27 21:54:10 +020045 call term_sendkeys(buf, ":call popup_move(popupwin, {'minwidth': 15, 'maxwidth': 25, 'minheight': 3, 'maxheight': 5})\<CR>")
46 call term_sendkeys(buf, ":redraw\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
48
Bram Moolenaar17146962019-05-30 00:12:11 +020049 " show not fitting line at bottom
50 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
51 call term_sendkeys(buf, ":redraw\<CR>")
52 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
53
Bram Moolenaar4d784b22019-05-25 19:51:39 +020054 " clean up
55 call StopVimInTerminal(buf)
56 call delete('XtestPopup')
57endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020058
Bram Moolenaarb4230122019-05-30 18:40:53 +020059func Test_popup_with_syntax_win_execute()
60 if !CanRunVimInTerminal()
61 return
62 endif
63 call writefile([
64 \ "call setline(1, range(1, 100))",
65 \ "hi PopupColor ctermbg=lightblue",
66 \ "let winid = popup_create([",
67 \ "\\ '#include <stdio.h>',",
68 \ "\\ 'int main(void)',",
69 \ "\\ '{',",
70 \ "\\ ' printf(123);',",
71 \ "\\ '}',",
72 \ "\\], {'line': 3, 'col': 25, 'highlight': 'PopupColor'})",
73 \ "call win_execute(winid, 'set syntax=cpp')",
74 \], 'XtestPopup')
75 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
76 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
77
78 " clean up
79 call StopVimInTerminal(buf)
80 call delete('XtestPopup')
81endfunc
82
83func Test_popup_with_syntax_setbufvar()
84 if !CanRunVimInTerminal()
85 return
86 endif
Bram Moolenaar402502d2019-05-30 22:07:36 +020087 let lines =<< trim END
88 call setline(1, range(1, 100))
89 hi PopupColor ctermbg=lightgrey
90 let winid = popup_create([
91 \ '#include <stdio.h>',
92 \ 'int main(void)',
93 \ '{',
94 \ ' printf(567);',
95 \ '}',
96 \], {'line': 3, 'col': 21, 'highlight': 'PopupColor'})
97 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
98 END
99 call writefile(lines, 'XtestPopup')
Bram Moolenaarb4230122019-05-30 18:40:53 +0200100 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
101 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
102
103 " clean up
104 call StopVimInTerminal(buf)
105 call delete('XtestPopup')
106endfunc
107
Bram Moolenaareea16992019-05-31 17:34:48 +0200108func Test_win_execute_closing_curwin()
109 split
110 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200111 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
112 popupclear
113endfunc
114
115func Test_win_execute_not_allowed()
116 let winid = popup_create('some text', {})
117 call assert_fails('call win_execute(winid, "split")', 'E994:')
118 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
119 call assert_fails('call win_execute(winid, "close")', 'E994:')
120 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200121 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200122 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
123 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
124 call assert_fails('call win_execute(winid, "next")', 'E994:')
125 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
126 call assert_fails('call win_execute(winid, "buf")', 'E994:')
127 call assert_fails('call win_execute(winid, "edit")', 'E994:')
128 call assert_fails('call win_execute(winid, "enew")', 'E994:')
129 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
130 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
131 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
132 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaareea16992019-05-31 17:34:48 +0200133 popupclear
134endfunc
135
Bram Moolenaar402502d2019-05-30 22:07:36 +0200136func Test_popup_with_wrap()
137 if !CanRunVimInTerminal()
138 return
139 endif
140 let lines =<< trim END
141 call setline(1, range(1, 100))
142 let winid = popup_create(
143 \ 'a long line that wont fit',
144 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
145 END
146 call writefile(lines, 'XtestPopup')
147 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
148 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
149
150 " clean up
151 call StopVimInTerminal(buf)
152 call delete('XtestPopup')
153endfunc
154
155func Test_popup_without_wrap()
156 if !CanRunVimInTerminal()
157 return
158 endif
159 let lines =<< trim END
160 call setline(1, range(1, 100))
161 let winid = popup_create(
162 \ 'a long line that wont fit',
163 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
164 END
165 call writefile(lines, 'XtestPopup')
166 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
167 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
168
169 " clean up
170 call StopVimInTerminal(buf)
171 call delete('XtestPopup')
172endfunc
173
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200174func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200175 if !has('timers')
176 return
177 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200178 topleft vnew
179 call setline(1, 'hello')
180
181 call popup_create('world', {
182 \ 'line': 1,
183 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200184 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200185 \ 'time': 500,
186 \})
187 redraw
188 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
189 call assert_equal('world', line)
190
191 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200192 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200193 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
194 call assert_equal('hello', line)
195
196 call popup_create('on the command line', {
197 \ 'line': &lines,
198 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200199 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200200 \ 'time': 500,
201 \})
202 redraw
203 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
204 call assert_match('.*on the command line.*', line)
205
206 sleep 700m
207 redraw
208 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
209 call assert_notmatch('.*on the command line.*', line)
210
211 bwipe!
212endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200213
214func Test_popup_hide()
215 topleft vnew
216 call setline(1, 'hello')
217
218 let winid = popup_create('world', {
219 \ 'line': 1,
220 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200221 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200222 \})
223 redraw
224 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
225 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200226 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200227 " buffer is still listed and active
228 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200229
230 call popup_hide(winid)
231 redraw
232 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
233 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200234 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200235 " buffer is still listed but hidden
236 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200237
238 call popup_show(winid)
239 redraw
240 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
241 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200242 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200243
244
245 call popup_close(winid)
246 redraw
247 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
248 call assert_equal('hello', line)
249
250 " error is given for existing non-popup window
251 call assert_fails('call popup_hide(win_getid())', 'E993:')
252
253 " no error non-existing window
254 call popup_hide(1234234)
255 call popup_show(41234234)
256
257 bwipe!
258endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200259
260func Test_popup_move()
261 topleft vnew
262 call setline(1, 'hello')
263
264 let winid = popup_create('world', {
265 \ 'line': 1,
266 \ 'col': 1,
267 \ 'minwidth': 20,
268 \})
269 redraw
270 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
271 call assert_equal('world ', line)
272
273 call popup_move(winid, {'line': 2, 'col': 2})
274 redraw
275 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
276 call assert_equal('hello ', line)
277 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
278 call assert_equal('~world', line)
279
280 call popup_move(winid, {'line': 1})
281 redraw
282 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
283 call assert_equal('hworld', line)
284
285 call popup_close(winid)
286
287 bwipe!
288endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200289
Bram Moolenaar402502d2019-05-30 22:07:36 +0200290func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200291 let winid = popup_create('hello', {
292 \ 'line': 2,
293 \ 'col': 3,
294 \ 'minwidth': 10,
295 \ 'minheight': 11,
296 \})
297 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200298 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200299 call assert_equal(2, res.line)
300 call assert_equal(3, res.col)
301 call assert_equal(10, res.width)
302 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200303 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200304
305 call popup_close(winid)
306endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200307
308func Test_popup_width_longest()
309 let tests = [
310 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
311 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
312 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
313 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
314 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
315 \ ]
316
317 for test in tests
318 let winid = popup_create(test[0], {'line': 2, 'col': 3})
319 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200320 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200321 call assert_equal(test[1], position.width)
322 call popup_close(winid)
323 endfor
324endfunc
325
326func Test_popup_wraps()
327 let tests = [
328 \ ['nowrap', 6, 1],
329 \ ['a line that wraps once', 12, 2],
330 \ ['a line that wraps two times', 12, 3],
331 \ ]
332 for test in tests
333 let winid = popup_create(test[0],
334 \ {'line': 2, 'col': 3, 'maxwidth': 12})
335 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200336 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200337 call assert_equal(test[1], position.width)
338 call assert_equal(test[2], position.height)
339
340 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200341 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200342 endfor
343endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200344
345func Test_popup_getoptions()
346 let winid = popup_create('hello', {
347 \ 'line': 2,
348 \ 'col': 3,
349 \ 'minwidth': 10,
350 \ 'minheight': 11,
351 \ 'maxwidth': 20,
352 \ 'maxheight': 21,
353 \ 'zindex': 100,
354 \ 'time': 5000,
355 \})
356 redraw
357 let res = popup_getoptions(winid)
358 call assert_equal(2, res.line)
359 call assert_equal(3, res.col)
360 call assert_equal(10, res.minwidth)
361 call assert_equal(11, res.minheight)
362 call assert_equal(20, res.maxwidth)
363 call assert_equal(21, res.maxheight)
364 call assert_equal(100, res.zindex)
365 if has('timers')
366 call assert_equal(5000, res.time)
367 endif
368 call popup_close(winid)
369
370 let winid = popup_create('hello', {})
371 redraw
372 let res = popup_getoptions(winid)
373 call assert_equal(0, res.line)
374 call assert_equal(0, res.col)
375 call assert_equal(0, res.minwidth)
376 call assert_equal(0, res.minheight)
377 call assert_equal(0, res.maxwidth)
378 call assert_equal(0, res.maxheight)
379 call assert_equal(50, res.zindex)
380 if has('timers')
381 call assert_equal(0, res.time)
382 endif
383 call popup_close(winid)
384 call assert_equal({}, popup_getoptions(winid))
385endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200386
387func Test_popup_option_values()
388 new
389 " window-local
390 setlocal number
391 setlocal nowrap
392 " buffer-local
393 setlocal omnifunc=Something
394 " global/buffer-local
395 setlocal path=/there
396 " global/window-local
397 setlocal scrolloff=9
398
399 let winid = popup_create('hello', {})
400 call assert_equal(0, getwinvar(winid, '&number'))
401 call assert_equal(1, getwinvar(winid, '&wrap'))
402 call assert_equal('', getwinvar(winid, '&omnifunc'))
403 call assert_equal(&g:path, getwinvar(winid, '&path'))
404 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
405
406 call popup_close(winid)
407 bwipe
408endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200409
410func Test_popup_atcursor()
411 topleft vnew
412 call setline(1, [
413 \ 'xxxxxxxxxxxxxxxxx',
414 \ 'xxxxxxxxxxxxxxxxx',
415 \ 'xxxxxxxxxxxxxxxxx',
416 \])
417
418 call cursor(2, 2)
419 redraw
420 let winid = popup_atcursor('vim', {})
421 redraw
422 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
423 call assert_equal('xvimxxxxxxxxxxxxx', line)
424 call popup_close(winid)
425
426 call cursor(3, 4)
427 redraw
428 let winid = popup_atcursor('vim', {})
429 redraw
430 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
431 call assert_equal('xxxvimxxxxxxxxxxx', line)
432 call popup_close(winid)
433
434 call cursor(1, 1)
435 redraw
436 let winid = popup_create('vim', {
437 \ 'line': 'cursor+2',
438 \ 'col': 'cursor+1',
439 \})
440 redraw
441 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
442 call assert_equal('xvimxxxxxxxxxxxxx', line)
443 call popup_close(winid)
444
445 call cursor(3, 3)
446 redraw
447 let winid = popup_create('vim', {
448 \ 'line': 'cursor-2',
449 \ 'col': 'cursor-1',
450 \})
451 redraw
452 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
453 call assert_equal('xvimxxxxxxxxxxxxx', line)
454 call popup_close(winid)
455
Bram Moolenaar402502d2019-05-30 22:07:36 +0200456 " just enough room above
457 call cursor(3, 3)
458 redraw
459 let winid = popup_atcursor(['vim', 'is great'], {})
460 redraw
461 let pos = popup_getpos(winid)
462 call assert_equal(1, pos.line)
463 call popup_close(winid)
464
465 " not enough room above, popup goes below the cursor
466 call cursor(3, 3)
467 redraw
468 let winid = popup_atcursor(['vim', 'is', 'great'], {})
469 redraw
470 let pos = popup_getpos(winid)
471 call assert_equal(4, pos.line)
472 call popup_close(winid)
473
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200474 bwipe!
475endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200476
477func Test_popup_filter()
478 new
479 call setline(1, 'some text')
480
481 func MyPopupFilter(winid, c)
482 if a:c == 'e'
483 let g:eaten = 'e'
484 return 1
485 endif
486 if a:c == '0'
487 let g:ignored = '0'
488 return 0
489 endif
490 if a:c == 'x'
491 call popup_close(a:winid)
492 return 1
493 endif
494 return 0
495 endfunc
496
497 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
498 redraw
499
500 " e is consumed by the filter
501 call feedkeys('e', 'xt')
502 call assert_equal('e', g:eaten)
503
504 " 0 is ignored by the filter
505 normal $
506 call assert_equal(9, getcurpos()[2])
507 call feedkeys('0', 'xt')
508 call assert_equal('0', g:ignored)
509 call assert_equal(1, getcurpos()[2])
510
511 " x closes the popup
512 call feedkeys('x', 'xt')
513 call assert_equal('e', g:eaten)
514 call assert_equal(-1, winbufnr(winid))
515
516 delfunc MyPopupFilter
517 popupclear
518endfunc