blob: c5fdb6e875d29c40ff8ec5ef4c3fe55504b5615e [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 Moolenaar402502d2019-05-30 22:07:36 +0200108func Test_popup_with_wrap()
109 if !CanRunVimInTerminal()
110 return
111 endif
112 let lines =<< trim END
113 call setline(1, range(1, 100))
114 let winid = popup_create(
115 \ 'a long line that wont fit',
116 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
117 END
118 call writefile(lines, 'XtestPopup')
119 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
120 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
121
122 " clean up
123 call StopVimInTerminal(buf)
124 call delete('XtestPopup')
125endfunc
126
127func Test_popup_without_wrap()
128 if !CanRunVimInTerminal()
129 return
130 endif
131 let lines =<< trim END
132 call setline(1, range(1, 100))
133 let winid = popup_create(
134 \ 'a long line that wont fit',
135 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
136 END
137 call writefile(lines, 'XtestPopup')
138 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
139 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
140
141 " clean up
142 call StopVimInTerminal(buf)
143 call delete('XtestPopup')
144endfunc
145
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200146func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200147 if !has('timers')
148 return
149 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200150 topleft vnew
151 call setline(1, 'hello')
152
153 call popup_create('world', {
154 \ 'line': 1,
155 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200156 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200157 \ 'time': 500,
158 \})
159 redraw
160 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
161 call assert_equal('world', line)
162
163 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200164 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200165 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
166 call assert_equal('hello', line)
167
168 call popup_create('on the command line', {
169 \ 'line': &lines,
170 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200171 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200172 \ 'time': 500,
173 \})
174 redraw
175 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
176 call assert_match('.*on the command line.*', line)
177
178 sleep 700m
179 redraw
180 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
181 call assert_notmatch('.*on the command line.*', line)
182
183 bwipe!
184endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200185
186func Test_popup_hide()
187 topleft vnew
188 call setline(1, 'hello')
189
190 let winid = popup_create('world', {
191 \ 'line': 1,
192 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200193 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200194 \})
195 redraw
196 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
197 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200198 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200199
200 call popup_hide(winid)
201 redraw
202 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
203 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200204 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200205
206 call popup_show(winid)
207 redraw
208 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
209 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200210 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200211
212
213 call popup_close(winid)
214 redraw
215 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
216 call assert_equal('hello', line)
217
218 " error is given for existing non-popup window
219 call assert_fails('call popup_hide(win_getid())', 'E993:')
220
221 " no error non-existing window
222 call popup_hide(1234234)
223 call popup_show(41234234)
224
225 bwipe!
226endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200227
228func Test_popup_move()
229 topleft vnew
230 call setline(1, 'hello')
231
232 let winid = popup_create('world', {
233 \ 'line': 1,
234 \ 'col': 1,
235 \ 'minwidth': 20,
236 \})
237 redraw
238 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
239 call assert_equal('world ', line)
240
241 call popup_move(winid, {'line': 2, 'col': 2})
242 redraw
243 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
244 call assert_equal('hello ', line)
245 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
246 call assert_equal('~world', line)
247
248 call popup_move(winid, {'line': 1})
249 redraw
250 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
251 call assert_equal('hworld', line)
252
253 call popup_close(winid)
254
255 bwipe!
256endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200257
Bram Moolenaar402502d2019-05-30 22:07:36 +0200258func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200259 let winid = popup_create('hello', {
260 \ 'line': 2,
261 \ 'col': 3,
262 \ 'minwidth': 10,
263 \ 'minheight': 11,
264 \})
265 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200266 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200267 call assert_equal(2, res.line)
268 call assert_equal(3, res.col)
269 call assert_equal(10, res.width)
270 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200271 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200272
273 call popup_close(winid)
274endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200275
276func Test_popup_width_longest()
277 let tests = [
278 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
279 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
280 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
281 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
282 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
283 \ ]
284
285 for test in tests
286 let winid = popup_create(test[0], {'line': 2, 'col': 3})
287 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200288 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200289 call assert_equal(test[1], position.width)
290 call popup_close(winid)
291 endfor
292endfunc
293
294func Test_popup_wraps()
295 let tests = [
296 \ ['nowrap', 6, 1],
297 \ ['a line that wraps once', 12, 2],
298 \ ['a line that wraps two times', 12, 3],
299 \ ]
300 for test in tests
301 let winid = popup_create(test[0],
302 \ {'line': 2, 'col': 3, 'maxwidth': 12})
303 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200304 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200305 call assert_equal(test[1], position.width)
306 call assert_equal(test[2], position.height)
307
308 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200309 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200310 endfor
311endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200312
313func Test_popup_getoptions()
314 let winid = popup_create('hello', {
315 \ 'line': 2,
316 \ 'col': 3,
317 \ 'minwidth': 10,
318 \ 'minheight': 11,
319 \ 'maxwidth': 20,
320 \ 'maxheight': 21,
321 \ 'zindex': 100,
322 \ 'time': 5000,
323 \})
324 redraw
325 let res = popup_getoptions(winid)
326 call assert_equal(2, res.line)
327 call assert_equal(3, res.col)
328 call assert_equal(10, res.minwidth)
329 call assert_equal(11, res.minheight)
330 call assert_equal(20, res.maxwidth)
331 call assert_equal(21, res.maxheight)
332 call assert_equal(100, res.zindex)
333 if has('timers')
334 call assert_equal(5000, res.time)
335 endif
336 call popup_close(winid)
337
338 let winid = popup_create('hello', {})
339 redraw
340 let res = popup_getoptions(winid)
341 call assert_equal(0, res.line)
342 call assert_equal(0, res.col)
343 call assert_equal(0, res.minwidth)
344 call assert_equal(0, res.minheight)
345 call assert_equal(0, res.maxwidth)
346 call assert_equal(0, res.maxheight)
347 call assert_equal(50, res.zindex)
348 if has('timers')
349 call assert_equal(0, res.time)
350 endif
351 call popup_close(winid)
352 call assert_equal({}, popup_getoptions(winid))
353endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200354
355func Test_popup_option_values()
356 new
357 " window-local
358 setlocal number
359 setlocal nowrap
360 " buffer-local
361 setlocal omnifunc=Something
362 " global/buffer-local
363 setlocal path=/there
364 " global/window-local
365 setlocal scrolloff=9
366
367 let winid = popup_create('hello', {})
368 call assert_equal(0, getwinvar(winid, '&number'))
369 call assert_equal(1, getwinvar(winid, '&wrap'))
370 call assert_equal('', getwinvar(winid, '&omnifunc'))
371 call assert_equal(&g:path, getwinvar(winid, '&path'))
372 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
373
374 call popup_close(winid)
375 bwipe
376endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200377
378func Test_popup_atcursor()
379 topleft vnew
380 call setline(1, [
381 \ 'xxxxxxxxxxxxxxxxx',
382 \ 'xxxxxxxxxxxxxxxxx',
383 \ 'xxxxxxxxxxxxxxxxx',
384 \])
385
386 call cursor(2, 2)
387 redraw
388 let winid = popup_atcursor('vim', {})
389 redraw
390 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
391 call assert_equal('xvimxxxxxxxxxxxxx', line)
392 call popup_close(winid)
393
394 call cursor(3, 4)
395 redraw
396 let winid = popup_atcursor('vim', {})
397 redraw
398 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
399 call assert_equal('xxxvimxxxxxxxxxxx', line)
400 call popup_close(winid)
401
402 call cursor(1, 1)
403 redraw
404 let winid = popup_create('vim', {
405 \ 'line': 'cursor+2',
406 \ 'col': 'cursor+1',
407 \})
408 redraw
409 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
410 call assert_equal('xvimxxxxxxxxxxxxx', line)
411 call popup_close(winid)
412
413 call cursor(3, 3)
414 redraw
415 let winid = popup_create('vim', {
416 \ 'line': 'cursor-2',
417 \ 'col': 'cursor-1',
418 \})
419 redraw
420 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
421 call assert_equal('xvimxxxxxxxxxxxxx', line)
422 call popup_close(winid)
423
Bram Moolenaar402502d2019-05-30 22:07:36 +0200424 " just enough room above
425 call cursor(3, 3)
426 redraw
427 let winid = popup_atcursor(['vim', 'is great'], {})
428 redraw
429 let pos = popup_getpos(winid)
430 call assert_equal(1, pos.line)
431 call popup_close(winid)
432
433 " not enough room above, popup goes below the cursor
434 call cursor(3, 3)
435 redraw
436 let winid = popup_atcursor(['vim', 'is', 'great'], {})
437 redraw
438 let pos = popup_getpos(winid)
439 call assert_equal(4, pos.line)
440 call popup_close(winid)
441
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200442 bwipe!
443endfunc