blob: 6a2ef24a472380765754a30826272bc3f85c0da7 [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 Moolenaar2fd8e352019-06-01 20:16:48 +020059func Test_popup_with_border_and_padding()
60 if !CanRunVimInTerminal()
61 return
62 endif
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020063
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020064 for iter in range(0, 1)
65 call writefile([iter == 1 ? '' : 'set enc=latin1',
66 \ "call setline(1, range(1, 100))",
67 \ "call popup_create('hello border', {'line': 2, 'col': 3, 'border': []})",
68 \ "call popup_create('hello padding', {'line': 2, 'col': 23, 'padding': []})",
69 \ "call popup_create('hello both', {'line': 2, 'col': 43, 'border': [], 'padding': []})",
70 \ "call popup_create('border TL', {'line': 6, 'col': 3, 'border': [1, 0, 0, 4]})",
71 \ "call popup_create('paddings', {'line': 6, 'col': 23, 'padding': [1, 3, 2, 4]})",
72 \], 'XtestPopupBorder')
73 let buf = RunVimInTerminal('-S XtestPopupBorder', {'rows': 15})
74 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
75
76 call StopVimInTerminal(buf)
77 call delete('XtestPopupBorder')
78 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020079
Bram Moolenaar790498b2019-06-01 22:15:29 +020080 call writefile([
81 \ "call setline(1, range(1, 100))",
82 \ "hi BlueColor ctermbg=lightblue",
83 \ "hi TopColor ctermbg=253",
84 \ "hi RightColor ctermbg=245",
85 \ "hi BottomColor ctermbg=240",
86 \ "hi LeftColor ctermbg=248",
87 \ "call popup_create('hello border', {'line': 2, 'col': 3, 'border': [], 'borderhighlight': ['BlueColor']})",
88 \ "call popup_create(['hello border', 'and more'], {'line': 2, 'col': 23, 'border': [], 'borderhighlight': ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})",
89 \ "call popup_create(['hello border', 'lines only'], {'line': 2, 'col': 43, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['x']})",
90 \ "call popup_create(['hello border', 'with corners'], {'line': 2, 'col': 60, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['x', '#']})",
91 \ "call popup_create(['hello border', 'with numbers'], {'line': 6, 'col': 3, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['0', '1', '2', '3', '4', '5', '6', '7']})",
92 \ "call popup_create(['hello border', 'just blanks'], {'line': 7, 'col': 23, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': [' ']})",
93 \], 'XtestPopupBorder')
94 let buf = RunVimInTerminal('-S XtestPopupBorder', {'rows': 12})
95 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
96
97 call StopVimInTerminal(buf)
98 call delete('XtestPopupBorder')
99
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200100 let with_border_or_padding = {
101 \ 'line': 2,
102 \ 'core_line': 3,
103 \ 'col': 3,
104 \ 'core_col': 4,
105 \ 'width': 14,
106 \ 'core_width': 12,
107 \ 'height': 3,
108 \ 'core_height': 1,
109 \ 'visible': 1}
110 let winid = popup_create('hello border', {'line': 2, 'col': 3, 'border': []})",
111 call assert_equal(with_border_or_padding, popup_getpos(winid))
112
113 let winid = popup_create('hello paddng', {'line': 2, 'col': 3, 'padding': []})
114 call assert_equal(with_border_or_padding, popup_getpos(winid))
115
116 let winid = popup_create('hello both', {'line': 3, 'col': 8, 'border': [], 'padding': []})
117 call assert_equal({
118 \ 'line': 3,
119 \ 'core_line': 5,
120 \ 'col': 8,
121 \ 'core_col': 10,
122 \ 'width': 14,
123 \ 'core_width': 10,
124 \ 'height': 5,
125 \ 'core_height': 1,
126 \ 'visible': 1}, popup_getpos(winid))
127endfunc
128
Bram Moolenaarb4230122019-05-30 18:40:53 +0200129func Test_popup_with_syntax_win_execute()
130 if !CanRunVimInTerminal()
131 return
132 endif
133 call writefile([
134 \ "call setline(1, range(1, 100))",
135 \ "hi PopupColor ctermbg=lightblue",
136 \ "let winid = popup_create([",
137 \ "\\ '#include <stdio.h>',",
138 \ "\\ 'int main(void)',",
139 \ "\\ '{',",
140 \ "\\ ' printf(123);',",
141 \ "\\ '}',",
142 \ "\\], {'line': 3, 'col': 25, 'highlight': 'PopupColor'})",
143 \ "call win_execute(winid, 'set syntax=cpp')",
144 \], 'XtestPopup')
145 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
146 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
147
148 " clean up
149 call StopVimInTerminal(buf)
150 call delete('XtestPopup')
151endfunc
152
153func Test_popup_with_syntax_setbufvar()
154 if !CanRunVimInTerminal()
155 return
156 endif
Bram Moolenaar402502d2019-05-30 22:07:36 +0200157 let lines =<< trim END
158 call setline(1, range(1, 100))
159 hi PopupColor ctermbg=lightgrey
160 let winid = popup_create([
161 \ '#include <stdio.h>',
162 \ 'int main(void)',
163 \ '{',
164 \ ' printf(567);',
165 \ '}',
166 \], {'line': 3, 'col': 21, 'highlight': 'PopupColor'})
167 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
168 END
169 call writefile(lines, 'XtestPopup')
Bram Moolenaarb4230122019-05-30 18:40:53 +0200170 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
171 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
172
173 " clean up
174 call StopVimInTerminal(buf)
175 call delete('XtestPopup')
176endfunc
177
Bram Moolenaareea16992019-05-31 17:34:48 +0200178func Test_win_execute_closing_curwin()
179 split
180 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200181 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
182 popupclear
183endfunc
184
185func Test_win_execute_not_allowed()
186 let winid = popup_create('some text', {})
187 call assert_fails('call win_execute(winid, "split")', 'E994:')
188 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
189 call assert_fails('call win_execute(winid, "close")', 'E994:')
190 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200191 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200192 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
193 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
194 call assert_fails('call win_execute(winid, "next")', 'E994:')
195 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
196 call assert_fails('call win_execute(winid, "buf")', 'E994:')
197 call assert_fails('call win_execute(winid, "edit")', 'E994:')
198 call assert_fails('call win_execute(winid, "enew")', 'E994:')
199 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
200 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
201 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
202 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaareea16992019-05-31 17:34:48 +0200203 popupclear
204endfunc
205
Bram Moolenaar402502d2019-05-30 22:07:36 +0200206func Test_popup_with_wrap()
207 if !CanRunVimInTerminal()
208 return
209 endif
210 let lines =<< trim END
211 call setline(1, range(1, 100))
212 let winid = popup_create(
213 \ 'a long line that wont fit',
214 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
215 END
216 call writefile(lines, 'XtestPopup')
217 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
218 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
219
220 " clean up
221 call StopVimInTerminal(buf)
222 call delete('XtestPopup')
223endfunc
224
225func Test_popup_without_wrap()
226 if !CanRunVimInTerminal()
227 return
228 endif
229 let lines =<< trim END
230 call setline(1, range(1, 100))
231 let winid = popup_create(
232 \ 'a long line that wont fit',
233 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
234 END
235 call writefile(lines, 'XtestPopup')
236 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
237 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
238
239 " clean up
240 call StopVimInTerminal(buf)
241 call delete('XtestPopup')
242endfunc
243
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200244func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200245 if !has('timers')
246 return
247 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200248 topleft vnew
249 call setline(1, 'hello')
250
251 call popup_create('world', {
252 \ 'line': 1,
253 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200254 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200255 \ 'time': 500,
256 \})
257 redraw
258 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
259 call assert_equal('world', line)
260
261 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200262 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200263 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
264 call assert_equal('hello', line)
265
266 call popup_create('on the command line', {
267 \ 'line': &lines,
268 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200269 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200270 \ 'time': 500,
271 \})
272 redraw
273 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
274 call assert_match('.*on the command line.*', line)
275
276 sleep 700m
277 redraw
278 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
279 call assert_notmatch('.*on the command line.*', line)
280
281 bwipe!
282endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200283
284func Test_popup_hide()
285 topleft vnew
286 call setline(1, 'hello')
287
288 let winid = popup_create('world', {
289 \ 'line': 1,
290 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200291 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200292 \})
293 redraw
294 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
295 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200296 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200297 " buffer is still listed and active
298 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200299
300 call popup_hide(winid)
301 redraw
302 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
303 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200304 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200305 " buffer is still listed but hidden
306 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200307
308 call popup_show(winid)
309 redraw
310 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
311 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200312 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200313
314
315 call popup_close(winid)
316 redraw
317 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
318 call assert_equal('hello', line)
319
320 " error is given for existing non-popup window
321 call assert_fails('call popup_hide(win_getid())', 'E993:')
322
323 " no error non-existing window
324 call popup_hide(1234234)
325 call popup_show(41234234)
326
327 bwipe!
328endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200329
330func Test_popup_move()
331 topleft vnew
332 call setline(1, 'hello')
333
334 let winid = popup_create('world', {
335 \ 'line': 1,
336 \ 'col': 1,
337 \ 'minwidth': 20,
338 \})
339 redraw
340 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
341 call assert_equal('world ', line)
342
343 call popup_move(winid, {'line': 2, 'col': 2})
344 redraw
345 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
346 call assert_equal('hello ', line)
347 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
348 call assert_equal('~world', line)
349
350 call popup_move(winid, {'line': 1})
351 redraw
352 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
353 call assert_equal('hworld', line)
354
355 call popup_close(winid)
356
357 bwipe!
358endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200359
Bram Moolenaar402502d2019-05-30 22:07:36 +0200360func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200361 let winid = popup_create('hello', {
362 \ 'line': 2,
363 \ 'col': 3,
364 \ 'minwidth': 10,
365 \ 'minheight': 11,
366 \})
367 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200368 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200369 call assert_equal(2, res.line)
370 call assert_equal(3, res.col)
371 call assert_equal(10, res.width)
372 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200373 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200374
375 call popup_close(winid)
376endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200377
378func Test_popup_width_longest()
379 let tests = [
380 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
381 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
382 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
383 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
384 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
385 \ ]
386
387 for test in tests
388 let winid = popup_create(test[0], {'line': 2, 'col': 3})
389 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200390 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200391 call assert_equal(test[1], position.width)
392 call popup_close(winid)
393 endfor
394endfunc
395
396func Test_popup_wraps()
397 let tests = [
398 \ ['nowrap', 6, 1],
399 \ ['a line that wraps once', 12, 2],
400 \ ['a line that wraps two times', 12, 3],
401 \ ]
402 for test in tests
403 let winid = popup_create(test[0],
404 \ {'line': 2, 'col': 3, 'maxwidth': 12})
405 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200406 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200407 call assert_equal(test[1], position.width)
408 call assert_equal(test[2], position.height)
409
410 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200411 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200412 endfor
413endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200414
415func Test_popup_getoptions()
416 let winid = popup_create('hello', {
417 \ 'line': 2,
418 \ 'col': 3,
419 \ 'minwidth': 10,
420 \ 'minheight': 11,
421 \ 'maxwidth': 20,
422 \ 'maxheight': 21,
423 \ 'zindex': 100,
424 \ 'time': 5000,
425 \})
426 redraw
427 let res = popup_getoptions(winid)
428 call assert_equal(2, res.line)
429 call assert_equal(3, res.col)
430 call assert_equal(10, res.minwidth)
431 call assert_equal(11, res.minheight)
432 call assert_equal(20, res.maxwidth)
433 call assert_equal(21, res.maxheight)
434 call assert_equal(100, res.zindex)
435 if has('timers')
436 call assert_equal(5000, res.time)
437 endif
438 call popup_close(winid)
439
440 let winid = popup_create('hello', {})
441 redraw
442 let res = popup_getoptions(winid)
443 call assert_equal(0, res.line)
444 call assert_equal(0, res.col)
445 call assert_equal(0, res.minwidth)
446 call assert_equal(0, res.minheight)
447 call assert_equal(0, res.maxwidth)
448 call assert_equal(0, res.maxheight)
449 call assert_equal(50, res.zindex)
450 if has('timers')
451 call assert_equal(0, res.time)
452 endif
453 call popup_close(winid)
454 call assert_equal({}, popup_getoptions(winid))
455endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200456
457func Test_popup_option_values()
458 new
459 " window-local
460 setlocal number
461 setlocal nowrap
462 " buffer-local
463 setlocal omnifunc=Something
464 " global/buffer-local
465 setlocal path=/there
466 " global/window-local
467 setlocal scrolloff=9
468
469 let winid = popup_create('hello', {})
470 call assert_equal(0, getwinvar(winid, '&number'))
471 call assert_equal(1, getwinvar(winid, '&wrap'))
472 call assert_equal('', getwinvar(winid, '&omnifunc'))
473 call assert_equal(&g:path, getwinvar(winid, '&path'))
474 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
475
476 call popup_close(winid)
477 bwipe
478endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200479
480func Test_popup_atcursor()
481 topleft vnew
482 call setline(1, [
483 \ 'xxxxxxxxxxxxxxxxx',
484 \ 'xxxxxxxxxxxxxxxxx',
485 \ 'xxxxxxxxxxxxxxxxx',
486 \])
487
488 call cursor(2, 2)
489 redraw
490 let winid = popup_atcursor('vim', {})
491 redraw
492 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
493 call assert_equal('xvimxxxxxxxxxxxxx', line)
494 call popup_close(winid)
495
496 call cursor(3, 4)
497 redraw
498 let winid = popup_atcursor('vim', {})
499 redraw
500 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
501 call assert_equal('xxxvimxxxxxxxxxxx', line)
502 call popup_close(winid)
503
504 call cursor(1, 1)
505 redraw
506 let winid = popup_create('vim', {
507 \ 'line': 'cursor+2',
508 \ 'col': 'cursor+1',
509 \})
510 redraw
511 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
512 call assert_equal('xvimxxxxxxxxxxxxx', line)
513 call popup_close(winid)
514
515 call cursor(3, 3)
516 redraw
517 let winid = popup_create('vim', {
518 \ 'line': 'cursor-2',
519 \ 'col': 'cursor-1',
520 \})
521 redraw
522 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
523 call assert_equal('xvimxxxxxxxxxxxxx', line)
524 call popup_close(winid)
525
Bram Moolenaar402502d2019-05-30 22:07:36 +0200526 " just enough room above
527 call cursor(3, 3)
528 redraw
529 let winid = popup_atcursor(['vim', 'is great'], {})
530 redraw
531 let pos = popup_getpos(winid)
532 call assert_equal(1, pos.line)
533 call popup_close(winid)
534
535 " not enough room above, popup goes below the cursor
536 call cursor(3, 3)
537 redraw
538 let winid = popup_atcursor(['vim', 'is', 'great'], {})
539 redraw
540 let pos = popup_getpos(winid)
541 call assert_equal(4, pos.line)
542 call popup_close(winid)
543
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200544 bwipe!
545endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200546
547func Test_popup_filter()
548 new
549 call setline(1, 'some text')
550
551 func MyPopupFilter(winid, c)
552 if a:c == 'e'
553 let g:eaten = 'e'
554 return 1
555 endif
556 if a:c == '0'
557 let g:ignored = '0'
558 return 0
559 endif
560 if a:c == 'x'
561 call popup_close(a:winid)
562 return 1
563 endif
564 return 0
565 endfunc
566
567 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
568 redraw
569
570 " e is consumed by the filter
571 call feedkeys('e', 'xt')
572 call assert_equal('e', g:eaten)
573
574 " 0 is ignored by the filter
575 normal $
576 call assert_equal(9, getcurpos()[2])
577 call feedkeys('0', 'xt')
578 call assert_equal('0', g:ignored)
579 call assert_equal(1, getcurpos()[2])
580
581 " x closes the popup
582 call feedkeys('x', 'xt')
583 call assert_equal('e', g:eaten)
584 call assert_equal(-1, winbufnr(winid))
585
586 delfunc MyPopupFilter
587 popupclear
588endfunc