blob: 29d4c1531662e8cc86bce6e157d68054a5f1e3f8 [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 Moolenaarc6896e22019-05-30 22:32:34 +0200199 " buffer is still listed and active
200 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200201
202 call popup_hide(winid)
203 redraw
204 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
205 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200206 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200207 " buffer is still listed but hidden
208 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200209
210 call popup_show(winid)
211 redraw
212 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
213 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200214 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200215
216
217 call popup_close(winid)
218 redraw
219 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
220 call assert_equal('hello', line)
221
222 " error is given for existing non-popup window
223 call assert_fails('call popup_hide(win_getid())', 'E993:')
224
225 " no error non-existing window
226 call popup_hide(1234234)
227 call popup_show(41234234)
228
229 bwipe!
230endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200231
232func Test_popup_move()
233 topleft vnew
234 call setline(1, 'hello')
235
236 let winid = popup_create('world', {
237 \ 'line': 1,
238 \ 'col': 1,
239 \ 'minwidth': 20,
240 \})
241 redraw
242 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
243 call assert_equal('world ', line)
244
245 call popup_move(winid, {'line': 2, 'col': 2})
246 redraw
247 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
248 call assert_equal('hello ', line)
249 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
250 call assert_equal('~world', line)
251
252 call popup_move(winid, {'line': 1})
253 redraw
254 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
255 call assert_equal('hworld', line)
256
257 call popup_close(winid)
258
259 bwipe!
260endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200261
Bram Moolenaar402502d2019-05-30 22:07:36 +0200262func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200263 let winid = popup_create('hello', {
264 \ 'line': 2,
265 \ 'col': 3,
266 \ 'minwidth': 10,
267 \ 'minheight': 11,
268 \})
269 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200270 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200271 call assert_equal(2, res.line)
272 call assert_equal(3, res.col)
273 call assert_equal(10, res.width)
274 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200275 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200276
277 call popup_close(winid)
278endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200279
280func Test_popup_width_longest()
281 let tests = [
282 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
283 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
284 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
285 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
286 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
287 \ ]
288
289 for test in tests
290 let winid = popup_create(test[0], {'line': 2, 'col': 3})
291 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200292 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200293 call assert_equal(test[1], position.width)
294 call popup_close(winid)
295 endfor
296endfunc
297
298func Test_popup_wraps()
299 let tests = [
300 \ ['nowrap', 6, 1],
301 \ ['a line that wraps once', 12, 2],
302 \ ['a line that wraps two times', 12, 3],
303 \ ]
304 for test in tests
305 let winid = popup_create(test[0],
306 \ {'line': 2, 'col': 3, 'maxwidth': 12})
307 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200308 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200309 call assert_equal(test[1], position.width)
310 call assert_equal(test[2], position.height)
311
312 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200313 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200314 endfor
315endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200316
317func Test_popup_getoptions()
318 let winid = popup_create('hello', {
319 \ 'line': 2,
320 \ 'col': 3,
321 \ 'minwidth': 10,
322 \ 'minheight': 11,
323 \ 'maxwidth': 20,
324 \ 'maxheight': 21,
325 \ 'zindex': 100,
326 \ 'time': 5000,
327 \})
328 redraw
329 let res = popup_getoptions(winid)
330 call assert_equal(2, res.line)
331 call assert_equal(3, res.col)
332 call assert_equal(10, res.minwidth)
333 call assert_equal(11, res.minheight)
334 call assert_equal(20, res.maxwidth)
335 call assert_equal(21, res.maxheight)
336 call assert_equal(100, res.zindex)
337 if has('timers')
338 call assert_equal(5000, res.time)
339 endif
340 call popup_close(winid)
341
342 let winid = popup_create('hello', {})
343 redraw
344 let res = popup_getoptions(winid)
345 call assert_equal(0, res.line)
346 call assert_equal(0, res.col)
347 call assert_equal(0, res.minwidth)
348 call assert_equal(0, res.minheight)
349 call assert_equal(0, res.maxwidth)
350 call assert_equal(0, res.maxheight)
351 call assert_equal(50, res.zindex)
352 if has('timers')
353 call assert_equal(0, res.time)
354 endif
355 call popup_close(winid)
356 call assert_equal({}, popup_getoptions(winid))
357endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200358
359func Test_popup_option_values()
360 new
361 " window-local
362 setlocal number
363 setlocal nowrap
364 " buffer-local
365 setlocal omnifunc=Something
366 " global/buffer-local
367 setlocal path=/there
368 " global/window-local
369 setlocal scrolloff=9
370
371 let winid = popup_create('hello', {})
372 call assert_equal(0, getwinvar(winid, '&number'))
373 call assert_equal(1, getwinvar(winid, '&wrap'))
374 call assert_equal('', getwinvar(winid, '&omnifunc'))
375 call assert_equal(&g:path, getwinvar(winid, '&path'))
376 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
377
378 call popup_close(winid)
379 bwipe
380endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200381
382func Test_popup_atcursor()
383 topleft vnew
384 call setline(1, [
385 \ 'xxxxxxxxxxxxxxxxx',
386 \ 'xxxxxxxxxxxxxxxxx',
387 \ 'xxxxxxxxxxxxxxxxx',
388 \])
389
390 call cursor(2, 2)
391 redraw
392 let winid = popup_atcursor('vim', {})
393 redraw
394 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
395 call assert_equal('xvimxxxxxxxxxxxxx', line)
396 call popup_close(winid)
397
398 call cursor(3, 4)
399 redraw
400 let winid = popup_atcursor('vim', {})
401 redraw
402 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
403 call assert_equal('xxxvimxxxxxxxxxxx', line)
404 call popup_close(winid)
405
406 call cursor(1, 1)
407 redraw
408 let winid = popup_create('vim', {
409 \ 'line': 'cursor+2',
410 \ 'col': 'cursor+1',
411 \})
412 redraw
413 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
414 call assert_equal('xvimxxxxxxxxxxxxx', line)
415 call popup_close(winid)
416
417 call cursor(3, 3)
418 redraw
419 let winid = popup_create('vim', {
420 \ 'line': 'cursor-2',
421 \ 'col': 'cursor-1',
422 \})
423 redraw
424 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
425 call assert_equal('xvimxxxxxxxxxxxxx', line)
426 call popup_close(winid)
427
Bram Moolenaar402502d2019-05-30 22:07:36 +0200428 " just enough room above
429 call cursor(3, 3)
430 redraw
431 let winid = popup_atcursor(['vim', 'is great'], {})
432 redraw
433 let pos = popup_getpos(winid)
434 call assert_equal(1, pos.line)
435 call popup_close(winid)
436
437 " not enough room above, popup goes below the cursor
438 call cursor(3, 3)
439 redraw
440 let winid = popup_atcursor(['vim', 'is', 'great'], {})
441 redraw
442 let pos = popup_getpos(winid)
443 call assert_equal(4, pos.line)
444 call popup_close(winid)
445
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200446 bwipe!
447endfunc