blob: d0d27b56181bc7342f172f8a27dcd139c9be52fc [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', {})
111 call win_execute(winid, winnr() .. "close")
112 call assert_equal(1, winnr())
113 popupclear
114endfunc
115
Bram Moolenaar402502d2019-05-30 22:07:36 +0200116func Test_popup_with_wrap()
117 if !CanRunVimInTerminal()
118 return
119 endif
120 let lines =<< trim END
121 call setline(1, range(1, 100))
122 let winid = popup_create(
123 \ 'a long line that wont fit',
124 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
125 END
126 call writefile(lines, 'XtestPopup')
127 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
128 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
129
130 " clean up
131 call StopVimInTerminal(buf)
132 call delete('XtestPopup')
133endfunc
134
135func Test_popup_without_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': 0})
144 END
145 call writefile(lines, 'XtestPopup')
146 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
147 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
148
149 " clean up
150 call StopVimInTerminal(buf)
151 call delete('XtestPopup')
152endfunc
153
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200154func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200155 if !has('timers')
156 return
157 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200158 topleft vnew
159 call setline(1, 'hello')
160
161 call popup_create('world', {
162 \ 'line': 1,
163 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200164 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200165 \ 'time': 500,
166 \})
167 redraw
168 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
169 call assert_equal('world', line)
170
171 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200172 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200173 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
174 call assert_equal('hello', line)
175
176 call popup_create('on the command line', {
177 \ 'line': &lines,
178 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200179 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200180 \ 'time': 500,
181 \})
182 redraw
183 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
184 call assert_match('.*on the command line.*', line)
185
186 sleep 700m
187 redraw
188 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
189 call assert_notmatch('.*on the command line.*', line)
190
191 bwipe!
192endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200193
194func Test_popup_hide()
195 topleft vnew
196 call setline(1, 'hello')
197
198 let winid = popup_create('world', {
199 \ 'line': 1,
200 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200201 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200202 \})
203 redraw
204 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
205 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200206 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200207 " buffer is still listed and active
208 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200209
210 call popup_hide(winid)
211 redraw
212 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
213 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200214 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200215 " buffer is still listed but hidden
216 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200217
218 call popup_show(winid)
219 redraw
220 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
221 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200222 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200223
224
225 call popup_close(winid)
226 redraw
227 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
228 call assert_equal('hello', line)
229
230 " error is given for existing non-popup window
231 call assert_fails('call popup_hide(win_getid())', 'E993:')
232
233 " no error non-existing window
234 call popup_hide(1234234)
235 call popup_show(41234234)
236
237 bwipe!
238endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200239
240func Test_popup_move()
241 topleft vnew
242 call setline(1, 'hello')
243
244 let winid = popup_create('world', {
245 \ 'line': 1,
246 \ 'col': 1,
247 \ 'minwidth': 20,
248 \})
249 redraw
250 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
251 call assert_equal('world ', line)
252
253 call popup_move(winid, {'line': 2, 'col': 2})
254 redraw
255 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
256 call assert_equal('hello ', line)
257 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
258 call assert_equal('~world', line)
259
260 call popup_move(winid, {'line': 1})
261 redraw
262 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
263 call assert_equal('hworld', line)
264
265 call popup_close(winid)
266
267 bwipe!
268endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200269
Bram Moolenaar402502d2019-05-30 22:07:36 +0200270func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200271 let winid = popup_create('hello', {
272 \ 'line': 2,
273 \ 'col': 3,
274 \ 'minwidth': 10,
275 \ 'minheight': 11,
276 \})
277 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200278 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200279 call assert_equal(2, res.line)
280 call assert_equal(3, res.col)
281 call assert_equal(10, res.width)
282 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200283 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200284
285 call popup_close(winid)
286endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200287
288func Test_popup_width_longest()
289 let tests = [
290 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
291 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
292 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
293 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
294 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
295 \ ]
296
297 for test in tests
298 let winid = popup_create(test[0], {'line': 2, 'col': 3})
299 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200300 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200301 call assert_equal(test[1], position.width)
302 call popup_close(winid)
303 endfor
304endfunc
305
306func Test_popup_wraps()
307 let tests = [
308 \ ['nowrap', 6, 1],
309 \ ['a line that wraps once', 12, 2],
310 \ ['a line that wraps two times', 12, 3],
311 \ ]
312 for test in tests
313 let winid = popup_create(test[0],
314 \ {'line': 2, 'col': 3, 'maxwidth': 12})
315 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200316 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200317 call assert_equal(test[1], position.width)
318 call assert_equal(test[2], position.height)
319
320 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200321 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200322 endfor
323endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200324
325func Test_popup_getoptions()
326 let winid = popup_create('hello', {
327 \ 'line': 2,
328 \ 'col': 3,
329 \ 'minwidth': 10,
330 \ 'minheight': 11,
331 \ 'maxwidth': 20,
332 \ 'maxheight': 21,
333 \ 'zindex': 100,
334 \ 'time': 5000,
335 \})
336 redraw
337 let res = popup_getoptions(winid)
338 call assert_equal(2, res.line)
339 call assert_equal(3, res.col)
340 call assert_equal(10, res.minwidth)
341 call assert_equal(11, res.minheight)
342 call assert_equal(20, res.maxwidth)
343 call assert_equal(21, res.maxheight)
344 call assert_equal(100, res.zindex)
345 if has('timers')
346 call assert_equal(5000, res.time)
347 endif
348 call popup_close(winid)
349
350 let winid = popup_create('hello', {})
351 redraw
352 let res = popup_getoptions(winid)
353 call assert_equal(0, res.line)
354 call assert_equal(0, res.col)
355 call assert_equal(0, res.minwidth)
356 call assert_equal(0, res.minheight)
357 call assert_equal(0, res.maxwidth)
358 call assert_equal(0, res.maxheight)
359 call assert_equal(50, res.zindex)
360 if has('timers')
361 call assert_equal(0, res.time)
362 endif
363 call popup_close(winid)
364 call assert_equal({}, popup_getoptions(winid))
365endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200366
367func Test_popup_option_values()
368 new
369 " window-local
370 setlocal number
371 setlocal nowrap
372 " buffer-local
373 setlocal omnifunc=Something
374 " global/buffer-local
375 setlocal path=/there
376 " global/window-local
377 setlocal scrolloff=9
378
379 let winid = popup_create('hello', {})
380 call assert_equal(0, getwinvar(winid, '&number'))
381 call assert_equal(1, getwinvar(winid, '&wrap'))
382 call assert_equal('', getwinvar(winid, '&omnifunc'))
383 call assert_equal(&g:path, getwinvar(winid, '&path'))
384 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
385
386 call popup_close(winid)
387 bwipe
388endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200389
390func Test_popup_atcursor()
391 topleft vnew
392 call setline(1, [
393 \ 'xxxxxxxxxxxxxxxxx',
394 \ 'xxxxxxxxxxxxxxxxx',
395 \ 'xxxxxxxxxxxxxxxxx',
396 \])
397
398 call cursor(2, 2)
399 redraw
400 let winid = popup_atcursor('vim', {})
401 redraw
402 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
403 call assert_equal('xvimxxxxxxxxxxxxx', line)
404 call popup_close(winid)
405
406 call cursor(3, 4)
407 redraw
408 let winid = popup_atcursor('vim', {})
409 redraw
410 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
411 call assert_equal('xxxvimxxxxxxxxxxx', line)
412 call popup_close(winid)
413
414 call cursor(1, 1)
415 redraw
416 let winid = popup_create('vim', {
417 \ 'line': 'cursor+2',
418 \ 'col': 'cursor+1',
419 \})
420 redraw
421 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
422 call assert_equal('xvimxxxxxxxxxxxxx', line)
423 call popup_close(winid)
424
425 call cursor(3, 3)
426 redraw
427 let winid = popup_create('vim', {
428 \ 'line': 'cursor-2',
429 \ 'col': 'cursor-1',
430 \})
431 redraw
432 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
433 call assert_equal('xvimxxxxxxxxxxxxx', line)
434 call popup_close(winid)
435
Bram Moolenaar402502d2019-05-30 22:07:36 +0200436 " just enough room above
437 call cursor(3, 3)
438 redraw
439 let winid = popup_atcursor(['vim', 'is great'], {})
440 redraw
441 let pos = popup_getpos(winid)
442 call assert_equal(1, pos.line)
443 call popup_close(winid)
444
445 " not enough room above, popup goes below the cursor
446 call cursor(3, 3)
447 redraw
448 let winid = popup_atcursor(['vim', 'is', 'great'], {})
449 redraw
450 let pos = popup_getpos(winid)
451 call assert_equal(4, pos.line)
452 call popup_close(winid)
453
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200454 bwipe!
455endfunc