blob: 7c3cd94f0814b99013741e892e0bf2e45297faae [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:')
121 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
122 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
123 call assert_fails('call win_execute(winid, "next")', 'E994:')
124 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
125 call assert_fails('call win_execute(winid, "buf")', 'E994:')
126 call assert_fails('call win_execute(winid, "edit")', 'E994:')
127 call assert_fails('call win_execute(winid, "enew")', 'E994:')
128 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
129 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
130 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
131 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaareea16992019-05-31 17:34:48 +0200132 popupclear
133endfunc
134
Bram Moolenaar402502d2019-05-30 22:07:36 +0200135func Test_popup_with_wrap()
136 if !CanRunVimInTerminal()
137 return
138 endif
139 let lines =<< trim END
140 call setline(1, range(1, 100))
141 let winid = popup_create(
142 \ 'a long line that wont fit',
143 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
144 END
145 call writefile(lines, 'XtestPopup')
146 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
147 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
148
149 " clean up
150 call StopVimInTerminal(buf)
151 call delete('XtestPopup')
152endfunc
153
154func Test_popup_without_wrap()
155 if !CanRunVimInTerminal()
156 return
157 endif
158 let lines =<< trim END
159 call setline(1, range(1, 100))
160 let winid = popup_create(
161 \ 'a long line that wont fit',
162 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
163 END
164 call writefile(lines, 'XtestPopup')
165 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
166 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
167
168 " clean up
169 call StopVimInTerminal(buf)
170 call delete('XtestPopup')
171endfunc
172
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200173func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200174 if !has('timers')
175 return
176 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200177 topleft vnew
178 call setline(1, 'hello')
179
180 call popup_create('world', {
181 \ 'line': 1,
182 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200183 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200184 \ 'time': 500,
185 \})
186 redraw
187 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
188 call assert_equal('world', line)
189
190 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200191 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200192 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
193 call assert_equal('hello', line)
194
195 call popup_create('on the command line', {
196 \ 'line': &lines,
197 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200198 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200199 \ 'time': 500,
200 \})
201 redraw
202 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
203 call assert_match('.*on the command line.*', line)
204
205 sleep 700m
206 redraw
207 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
208 call assert_notmatch('.*on the command line.*', line)
209
210 bwipe!
211endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200212
213func Test_popup_hide()
214 topleft vnew
215 call setline(1, 'hello')
216
217 let winid = popup_create('world', {
218 \ 'line': 1,
219 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200220 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200221 \})
222 redraw
223 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
224 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200225 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200226 " buffer is still listed and active
227 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200228
229 call popup_hide(winid)
230 redraw
231 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
232 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200233 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200234 " buffer is still listed but hidden
235 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200236
237 call popup_show(winid)
238 redraw
239 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
240 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200241 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200242
243
244 call popup_close(winid)
245 redraw
246 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
247 call assert_equal('hello', line)
248
249 " error is given for existing non-popup window
250 call assert_fails('call popup_hide(win_getid())', 'E993:')
251
252 " no error non-existing window
253 call popup_hide(1234234)
254 call popup_show(41234234)
255
256 bwipe!
257endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200258
259func Test_popup_move()
260 topleft vnew
261 call setline(1, 'hello')
262
263 let winid = popup_create('world', {
264 \ 'line': 1,
265 \ 'col': 1,
266 \ 'minwidth': 20,
267 \})
268 redraw
269 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
270 call assert_equal('world ', line)
271
272 call popup_move(winid, {'line': 2, 'col': 2})
273 redraw
274 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
275 call assert_equal('hello ', line)
276 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
277 call assert_equal('~world', line)
278
279 call popup_move(winid, {'line': 1})
280 redraw
281 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
282 call assert_equal('hworld', line)
283
284 call popup_close(winid)
285
286 bwipe!
287endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200288
Bram Moolenaar402502d2019-05-30 22:07:36 +0200289func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200290 let winid = popup_create('hello', {
291 \ 'line': 2,
292 \ 'col': 3,
293 \ 'minwidth': 10,
294 \ 'minheight': 11,
295 \})
296 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200297 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200298 call assert_equal(2, res.line)
299 call assert_equal(3, res.col)
300 call assert_equal(10, res.width)
301 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200302 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200303
304 call popup_close(winid)
305endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200306
307func Test_popup_width_longest()
308 let tests = [
309 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
310 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
311 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
312 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
313 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
314 \ ]
315
316 for test in tests
317 let winid = popup_create(test[0], {'line': 2, 'col': 3})
318 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200319 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200320 call assert_equal(test[1], position.width)
321 call popup_close(winid)
322 endfor
323endfunc
324
325func Test_popup_wraps()
326 let tests = [
327 \ ['nowrap', 6, 1],
328 \ ['a line that wraps once', 12, 2],
329 \ ['a line that wraps two times', 12, 3],
330 \ ]
331 for test in tests
332 let winid = popup_create(test[0],
333 \ {'line': 2, 'col': 3, 'maxwidth': 12})
334 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200335 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200336 call assert_equal(test[1], position.width)
337 call assert_equal(test[2], position.height)
338
339 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200340 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200341 endfor
342endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200343
344func Test_popup_getoptions()
345 let winid = popup_create('hello', {
346 \ 'line': 2,
347 \ 'col': 3,
348 \ 'minwidth': 10,
349 \ 'minheight': 11,
350 \ 'maxwidth': 20,
351 \ 'maxheight': 21,
352 \ 'zindex': 100,
353 \ 'time': 5000,
354 \})
355 redraw
356 let res = popup_getoptions(winid)
357 call assert_equal(2, res.line)
358 call assert_equal(3, res.col)
359 call assert_equal(10, res.minwidth)
360 call assert_equal(11, res.minheight)
361 call assert_equal(20, res.maxwidth)
362 call assert_equal(21, res.maxheight)
363 call assert_equal(100, res.zindex)
364 if has('timers')
365 call assert_equal(5000, res.time)
366 endif
367 call popup_close(winid)
368
369 let winid = popup_create('hello', {})
370 redraw
371 let res = popup_getoptions(winid)
372 call assert_equal(0, res.line)
373 call assert_equal(0, res.col)
374 call assert_equal(0, res.minwidth)
375 call assert_equal(0, res.minheight)
376 call assert_equal(0, res.maxwidth)
377 call assert_equal(0, res.maxheight)
378 call assert_equal(50, res.zindex)
379 if has('timers')
380 call assert_equal(0, res.time)
381 endif
382 call popup_close(winid)
383 call assert_equal({}, popup_getoptions(winid))
384endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200385
386func Test_popup_option_values()
387 new
388 " window-local
389 setlocal number
390 setlocal nowrap
391 " buffer-local
392 setlocal omnifunc=Something
393 " global/buffer-local
394 setlocal path=/there
395 " global/window-local
396 setlocal scrolloff=9
397
398 let winid = popup_create('hello', {})
399 call assert_equal(0, getwinvar(winid, '&number'))
400 call assert_equal(1, getwinvar(winid, '&wrap'))
401 call assert_equal('', getwinvar(winid, '&omnifunc'))
402 call assert_equal(&g:path, getwinvar(winid, '&path'))
403 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
404
405 call popup_close(winid)
406 bwipe
407endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200408
409func Test_popup_atcursor()
410 topleft vnew
411 call setline(1, [
412 \ 'xxxxxxxxxxxxxxxxx',
413 \ 'xxxxxxxxxxxxxxxxx',
414 \ 'xxxxxxxxxxxxxxxxx',
415 \])
416
417 call cursor(2, 2)
418 redraw
419 let winid = popup_atcursor('vim', {})
420 redraw
421 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
422 call assert_equal('xvimxxxxxxxxxxxxx', line)
423 call popup_close(winid)
424
425 call cursor(3, 4)
426 redraw
427 let winid = popup_atcursor('vim', {})
428 redraw
429 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
430 call assert_equal('xxxvimxxxxxxxxxxx', line)
431 call popup_close(winid)
432
433 call cursor(1, 1)
434 redraw
435 let winid = popup_create('vim', {
436 \ 'line': 'cursor+2',
437 \ 'col': 'cursor+1',
438 \})
439 redraw
440 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
441 call assert_equal('xvimxxxxxxxxxxxxx', line)
442 call popup_close(winid)
443
444 call cursor(3, 3)
445 redraw
446 let winid = popup_create('vim', {
447 \ 'line': 'cursor-2',
448 \ 'col': 'cursor-1',
449 \})
450 redraw
451 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
452 call assert_equal('xvimxxxxxxxxxxxxx', line)
453 call popup_close(winid)
454
Bram Moolenaar402502d2019-05-30 22:07:36 +0200455 " just enough room above
456 call cursor(3, 3)
457 redraw
458 let winid = popup_atcursor(['vim', 'is great'], {})
459 redraw
460 let pos = popup_getpos(winid)
461 call assert_equal(1, pos.line)
462 call popup_close(winid)
463
464 " not enough room above, popup goes below the cursor
465 call cursor(3, 3)
466 redraw
467 let winid = popup_atcursor(['vim', 'is', 'great'], {})
468 redraw
469 let pos = popup_getpos(winid)
470 call assert_equal(4, pos.line)
471 call popup_close(winid)
472
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200473 bwipe!
474endfunc