blob: e4f7cd23228cdd25f4c3f62d22ba1364a7bad74b [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,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200425 \ 'fixed': 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200426 \})
427 redraw
428 let res = popup_getoptions(winid)
429 call assert_equal(2, res.line)
430 call assert_equal(3, res.col)
431 call assert_equal(10, res.minwidth)
432 call assert_equal(11, res.minheight)
433 call assert_equal(20, res.maxwidth)
434 call assert_equal(21, res.maxheight)
435 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200436 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200437 if has('timers')
438 call assert_equal(5000, res.time)
439 endif
440 call popup_close(winid)
441
442 let winid = popup_create('hello', {})
443 redraw
444 let res = popup_getoptions(winid)
445 call assert_equal(0, res.line)
446 call assert_equal(0, res.col)
447 call assert_equal(0, res.minwidth)
448 call assert_equal(0, res.minheight)
449 call assert_equal(0, res.maxwidth)
450 call assert_equal(0, res.maxheight)
451 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200452 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200453 if has('timers')
454 call assert_equal(0, res.time)
455 endif
456 call popup_close(winid)
457 call assert_equal({}, popup_getoptions(winid))
458endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200459
460func Test_popup_option_values()
461 new
462 " window-local
463 setlocal number
464 setlocal nowrap
465 " buffer-local
466 setlocal omnifunc=Something
467 " global/buffer-local
468 setlocal path=/there
469 " global/window-local
470 setlocal scrolloff=9
471
472 let winid = popup_create('hello', {})
473 call assert_equal(0, getwinvar(winid, '&number'))
474 call assert_equal(1, getwinvar(winid, '&wrap'))
475 call assert_equal('', getwinvar(winid, '&omnifunc'))
476 call assert_equal(&g:path, getwinvar(winid, '&path'))
477 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
478
479 call popup_close(winid)
480 bwipe
481endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200482
483func Test_popup_atcursor()
484 topleft vnew
485 call setline(1, [
486 \ 'xxxxxxxxxxxxxxxxx',
487 \ 'xxxxxxxxxxxxxxxxx',
488 \ 'xxxxxxxxxxxxxxxxx',
489 \])
490
491 call cursor(2, 2)
492 redraw
493 let winid = popup_atcursor('vim', {})
494 redraw
495 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
496 call assert_equal('xvimxxxxxxxxxxxxx', line)
497 call popup_close(winid)
498
499 call cursor(3, 4)
500 redraw
501 let winid = popup_atcursor('vim', {})
502 redraw
503 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
504 call assert_equal('xxxvimxxxxxxxxxxx', line)
505 call popup_close(winid)
506
507 call cursor(1, 1)
508 redraw
509 let winid = popup_create('vim', {
510 \ 'line': 'cursor+2',
511 \ 'col': 'cursor+1',
512 \})
513 redraw
514 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
515 call assert_equal('xvimxxxxxxxxxxxxx', line)
516 call popup_close(winid)
517
518 call cursor(3, 3)
519 redraw
520 let winid = popup_create('vim', {
521 \ 'line': 'cursor-2',
522 \ 'col': 'cursor-1',
523 \})
524 redraw
525 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
526 call assert_equal('xvimxxxxxxxxxxxxx', line)
527 call popup_close(winid)
528
Bram Moolenaar402502d2019-05-30 22:07:36 +0200529 " just enough room above
530 call cursor(3, 3)
531 redraw
532 let winid = popup_atcursor(['vim', 'is great'], {})
533 redraw
534 let pos = popup_getpos(winid)
535 call assert_equal(1, pos.line)
536 call popup_close(winid)
537
538 " not enough room above, popup goes below the cursor
539 call cursor(3, 3)
540 redraw
541 let winid = popup_atcursor(['vim', 'is', 'great'], {})
542 redraw
543 let pos = popup_getpos(winid)
544 call assert_equal(4, pos.line)
545 call popup_close(winid)
546
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200547 bwipe!
548endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200549
550func Test_popup_filter()
551 new
552 call setline(1, 'some text')
553
554 func MyPopupFilter(winid, c)
555 if a:c == 'e'
556 let g:eaten = 'e'
557 return 1
558 endif
559 if a:c == '0'
560 let g:ignored = '0'
561 return 0
562 endif
563 if a:c == 'x'
564 call popup_close(a:winid)
565 return 1
566 endif
567 return 0
568 endfunc
569
570 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
571 redraw
572
573 " e is consumed by the filter
574 call feedkeys('e', 'xt')
575 call assert_equal('e', g:eaten)
576
577 " 0 is ignored by the filter
578 normal $
579 call assert_equal(9, getcurpos()[2])
580 call feedkeys('0', 'xt')
581 call assert_equal('0', g:ignored)
582 call assert_equal(1, getcurpos()[2])
583
584 " x closes the popup
585 call feedkeys('x', 'xt')
586 call assert_equal('e', g:eaten)
587 call assert_equal(-1, winbufnr(winid))
588
589 delfunc MyPopupFilter
590 popupclear
591endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200592
593func Test_popup_close_callback()
594 func PopupDone(id, result)
595 let g:result = a:result
596 endfunc
597 let winid = popup_create('something', {'callback': 'PopupDone'})
598 redraw
599 call popup_close(winid, 'done')
600 call assert_equal('done', g:result)
601endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200602
603func Test_popup_empty()
604 let winid = popup_create('', {'padding': [2,2,2,2]})
605 redraw
606 let pos = popup_getpos(winid)
607 call assert_equal(4, pos.width)
608 call assert_equal(5, pos.height)
609
610 let winid = popup_create([], {'border': []})
611 redraw
612 let pos = popup_getpos(winid)
613 call assert_equal(2, pos.width)
614 call assert_equal(3, pos.height)
615endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +0200616
617func Test_popup_never_behind()
618 if !CanRunVimInTerminal()
619 return
620 endif
621 " +-----------------------------+
622 " | | |
623 " | | |
624 " | | |
625 " | line1 |
626 " |------------line2------------|
627 " | line3 |
628 " | line4 |
629 " | |
630 " | |
631 " +-----------------------------+
632 let lines =<< trim END
633 only
634 split
635 vsplit
636 let info_window1 = getwininfo()[0]
637 let line = info_window1['height']
638 let col = info_window1['width']
639 call popup_create(['line1', 'line2', 'line3', 'line4'], {
640 \ 'line' : line,
641 \ 'col' : col,
642 \ })
643 END
644 call writefile(lines, 'XtestPopupBehind')
645 let buf = RunVimInTerminal('-S XtestPopupBehind', {'rows': 10})
646 call term_sendkeys(buf, "\<C-W>w")
647 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
648
649 " clean up
650 call StopVimInTerminal(buf)
651 call delete('XtestPopupBehind')
652endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200653
654func s:VerifyPosition( p, msg, line, col, width, height )
655 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
656 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
657 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
658 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
659endfunc
660
661func Test_popup_position_adjust()
662 " Anything placed past 2 cells from of the right of the screen is moved to the
663 " left.
664 "
665 " When wrapping is disabled, we also shift to the left to display on the
666 " screen, unless fixed is set.
667
668 " Entries for cases which don't vary based on wrapping.
669 " Format is per tests described below
670 let both_wrap_tests = [
671 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
672 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
673 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
674 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
675 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
676 \
677 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
678 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
679 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
680 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
681 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
682 \
683 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
684 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
685 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
686 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
687 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
688 \ ]
689
690 " these test groups are dicts with:
691 " - comment: something to identify the group of tests by
692 " - options: dict of options to merge with the row/col in tests
693 " - tests: list of cases. Each one is a list with elements:
694 " - text
695 " - row
696 " - col
697 " - expected row
698 " - expected col
699 " - expected width
700 " - expected height
701 let tests = [
702 \ {
703 \ 'comment': 'left-aligned with wrapping',
704 \ 'options': {
705 \ 'wrap': 1,
706 \ 'pos': 'botleft',
707 \ },
708 \ 'tests': both_wrap_tests + [
709 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
710 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
711 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
712 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
713 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
714 \ ],
715 \ },
716 \ {
717 \ 'comment': 'left aligned without wrapping',
718 \ 'options': {
719 \ 'wrap': 0,
720 \ 'pos': 'botleft',
721 \ },
722 \ 'tests': both_wrap_tests + [
723 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
724 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
725 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
726 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
727 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
728 \ ],
729 \ },
730 \ {
731 \ 'comment': 'left aligned with fixed position',
732 \ 'options': {
733 \ 'wrap': 0,
734 \ 'fixed': 1,
735 \ 'pos': 'botleft',
736 \ },
737 \ 'tests': both_wrap_tests + [
738 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
739 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
740 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
741 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
742 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
743 \ ],
744 \ },
745 \ ]
746
747 for test_group in tests
748 for test in test_group.tests
749 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
750 let options = {
751 \ 'line': line,
752 \ 'col': col,
753 \ }
754 call extend( options, test_group.options )
755
756 let p = popup_create( text, options )
757
758 let msg = string( extend( options, { 'text': text } ) )
759 call s:VerifyPosition( p, msg, e_line, e_col, e_width, e_height )
760 call popup_close( p )
761 endfor
762 endfor
763
764 popupclear
765 %bwipe!
766endfunc
767
768function Test_adjust_left_past_screen_width()
769 " width of screen
770 let X = join(map(range(&columns), {->'X'}), '')
771
772 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
773 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
774
775 redraw
776 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
777 call assert_equal(X, line)
778
779 call popup_close( p )
780 redraw
781
782 " Same if placed on the right hand side
783 let p = popup_create( X, { 'line': 1, 'col': &columns, 'wrap': 0 } )
784 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
785
786 redraw
787 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
788 call assert_equal(X, line)
789
790 call popup_close( p )
791 redraw
792
793 " Extend so > window width
794 let X .= 'x'
795
796 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
797 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
798
799 redraw
800 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
801 call assert_equal(X[ : -2 ], line)
802
803 call popup_close( p )
804 redraw
805
806 " Shifted then truncated (the x is not visible)
807 let p = popup_create( X, { 'line': 1, 'col': &columns - 3, 'wrap': 0 } )
808 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
809
810 redraw
811 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
812 call assert_equal(X[ : -2 ], line)
813
814 call popup_close( p )
815 redraw
816
817 " Not shifted, just truncated
818 let p = popup_create( X,
819 \ { 'line': 1, 'col': 2, 'wrap': 0, 'fixed': 1 } )
820 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
821
822 redraw
823 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
824 let e_line = ' ' . X[ 1 : -2 ]
825 call assert_equal(e_line, line)
826
827 call popup_close( p )
828 redraw
829
830 popupclear
831 %bwipe!
832endfunction