blob: 6d3279b4bb7c369a2172771252d13b11d585545d [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 Moolenaar399d8982019-06-02 15:34:29 +0200178func Test_popup_all_corners()
179 if !CanRunVimInTerminal()
180 return
181 endif
182 let lines =<< trim END
183 call setline(1, repeat([repeat('-', 60)], 15))
184 set so=0
185 normal 2G3|r#
186 let winid1 = popup_create(['first', 'second'], {
187 \ 'line': 'cursor+1',
188 \ 'col': 'cursor',
189 \ 'pos': 'topleft',
190 \ 'border': [],
191 \ 'padding': [],
192 \ })
193 normal 25|r@
194 let winid1 = popup_create(['First', 'SeconD'], {
195 \ 'line': 'cursor+1',
196 \ 'col': 'cursor',
197 \ 'pos': 'topright',
198 \ 'border': [],
199 \ 'padding': [],
200 \ })
201 normal 9G29|r%
202 let winid1 = popup_create(['fiRSt', 'seCOnd'], {
203 \ 'line': 'cursor-1',
204 \ 'col': 'cursor',
205 \ 'pos': 'botleft',
206 \ 'border': [],
207 \ 'padding': [],
208 \ })
209 normal 51|r&
210 let winid1 = popup_create(['FIrsT', 'SEcoND'], {
211 \ 'line': 'cursor-1',
212 \ 'col': 'cursor',
213 \ 'pos': 'botright',
214 \ 'border': [],
215 \ 'padding': [],
216 \ })
217 END
218 call writefile(lines, 'XtestPopupCorners')
219 let buf = RunVimInTerminal('-S XtestPopupCorners', {'rows': 12})
220 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
221
222 " clean up
223 call StopVimInTerminal(buf)
224 call delete('XtestPopupCorners')
225endfunc
226
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200227func Test_popup_in_tab()
228 " default popup is local to tab, not visible when in other tab
229 let winid = popup_create("text", {})
230 call assert_equal(1, popup_getpos(winid).visible)
231 tabnew
232 call assert_equal(0, popup_getpos(winid).visible)
233 quit
234 call assert_equal(1, popup_getpos(winid).visible)
235 popupclear
236
237 " global popup is visible in any tab
238 let winid = popup_create("text", {'tab': -1})
239 call assert_equal(1, popup_getpos(winid).visible)
240 tabnew
241 call assert_equal(1, popup_getpos(winid).visible)
242 quit
243 call assert_equal(1, popup_getpos(winid).visible)
244 popupclear
245endfunc
246
247func Test_popup_valid_arguments()
248 " Zero value is like the property wasn't there
249 let winid = popup_create("text", {"col": 0})
250 let pos = popup_getpos(winid)
251 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
252 popupclear
253
254 " using cursor column has minimum value of 1
255 let winid = popup_create("text", {"col": 'cursor-100'})
256 let pos = popup_getpos(winid)
257 call assert_equal(1, pos.col)
258 popupclear
259
260 " center
261 let winid = popup_create("text", {"pos": 'center'})
262 let pos = popup_getpos(winid)
263 let around = (&columns - pos.width) / 2
264 call assert_inrange(around - 1, around + 1, pos.col)
265 let around = (&lines - pos.height) / 2
266 call assert_inrange(around - 1, around + 1, pos.line)
267 popupclear
268endfunc
269
270func Test_popup_invalid_arguments()
271 call assert_fails('call popup_create(666, {})', 'E714:')
272 popupclear
273 call assert_fails('call popup_create("text", "none")', 'E715:')
274 popupclear
275
276 call assert_fails('call popup_create("text", {"col": "xxx"})', 'E475:')
277 popupclear
278 call assert_fails('call popup_create("text", {"col": "cursor8"})', 'E15:')
279 popupclear
280 call assert_fails('call popup_create("text", {"col": "cursor+x"})', 'E15:')
281 popupclear
282 call assert_fails('call popup_create("text", {"col": "cursor+8x"})', 'E15:')
283 popupclear
284
285 call assert_fails('call popup_create("text", {"line": "xxx"})', 'E475:')
286 popupclear
287 call assert_fails('call popup_create("text", {"line": "cursor8"})', 'E15:')
288 popupclear
289 call assert_fails('call popup_create("text", {"line": "cursor+x"})', 'E15:')
290 popupclear
291 call assert_fails('call popup_create("text", {"line": "cursor+8x"})', 'E15:')
292 popupclear
293
294 call assert_fails('call popup_create("text", {"pos": "there"})', 'E475:')
295 popupclear
296 call assert_fails('call popup_create("text", {"padding": "none"})', 'E714:')
297 popupclear
298 call assert_fails('call popup_create("text", {"border": "none"})', 'E714:')
299 popupclear
300 call assert_fails('call popup_create("text", {"borderhighlight": "none"})', 'E714:')
301 popupclear
302 call assert_fails('call popup_create("text", {"borderchars": "none"})', 'E714:')
303 popupclear
304
305 call assert_fails('call popup_create([{"text": "text"}, 666], {})', 'E715:')
306 popupclear
307 call assert_fails('call popup_create([{"text": "text", "props": "none"}], {})', 'E714:')
308 popupclear
309 call assert_fails('call popup_create([{"text": "text", "props": ["none"]}], {})', 'E715:')
310 popupclear
311endfunc
312
Bram Moolenaareea16992019-05-31 17:34:48 +0200313func Test_win_execute_closing_curwin()
314 split
315 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200316 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
317 popupclear
318endfunc
319
320func Test_win_execute_not_allowed()
321 let winid = popup_create('some text', {})
322 call assert_fails('call win_execute(winid, "split")', 'E994:')
323 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
324 call assert_fails('call win_execute(winid, "close")', 'E994:')
325 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200326 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200327 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
328 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
329 call assert_fails('call win_execute(winid, "next")', 'E994:')
330 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
331 call assert_fails('call win_execute(winid, "buf")', 'E994:')
332 call assert_fails('call win_execute(winid, "edit")', 'E994:')
333 call assert_fails('call win_execute(winid, "enew")', 'E994:')
334 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
335 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
336 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
337 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaareea16992019-05-31 17:34:48 +0200338 popupclear
339endfunc
340
Bram Moolenaar402502d2019-05-30 22:07:36 +0200341func Test_popup_with_wrap()
342 if !CanRunVimInTerminal()
343 return
344 endif
345 let lines =<< trim END
346 call setline(1, range(1, 100))
347 let winid = popup_create(
348 \ 'a long line that wont fit',
349 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
350 END
351 call writefile(lines, 'XtestPopup')
352 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
353 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
354
355 " clean up
356 call StopVimInTerminal(buf)
357 call delete('XtestPopup')
358endfunc
359
360func Test_popup_without_wrap()
361 if !CanRunVimInTerminal()
362 return
363 endif
364 let lines =<< trim END
365 call setline(1, range(1, 100))
366 let winid = popup_create(
367 \ 'a long line that wont fit',
368 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
369 END
370 call writefile(lines, 'XtestPopup')
371 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
372 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
373
374 " clean up
375 call StopVimInTerminal(buf)
376 call delete('XtestPopup')
377endfunc
378
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200379func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200380 if !has('timers')
381 return
382 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200383 topleft vnew
384 call setline(1, 'hello')
385
386 call popup_create('world', {
387 \ 'line': 1,
388 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200389 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200390 \ 'time': 500,
391 \})
392 redraw
393 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
394 call assert_equal('world', line)
395
396 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200397 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200398 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
399 call assert_equal('hello', line)
400
401 call popup_create('on the command line', {
402 \ 'line': &lines,
403 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200404 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200405 \ 'time': 500,
406 \})
407 redraw
408 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
409 call assert_match('.*on the command line.*', line)
410
411 sleep 700m
412 redraw
413 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
414 call assert_notmatch('.*on the command line.*', line)
415
416 bwipe!
417endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200418
419func Test_popup_hide()
420 topleft vnew
421 call setline(1, 'hello')
422
423 let winid = popup_create('world', {
424 \ 'line': 1,
425 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200426 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200427 \})
428 redraw
429 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
430 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200431 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200432 " buffer is still listed and active
433 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200434
435 call popup_hide(winid)
436 redraw
437 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
438 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200439 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200440 " buffer is still listed but hidden
441 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200442
443 call popup_show(winid)
444 redraw
445 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
446 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200447 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200448
449
450 call popup_close(winid)
451 redraw
452 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
453 call assert_equal('hello', line)
454
455 " error is given for existing non-popup window
456 call assert_fails('call popup_hide(win_getid())', 'E993:')
457
458 " no error non-existing window
459 call popup_hide(1234234)
460 call popup_show(41234234)
461
462 bwipe!
463endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200464
465func Test_popup_move()
466 topleft vnew
467 call setline(1, 'hello')
468
469 let winid = popup_create('world', {
470 \ 'line': 1,
471 \ 'col': 1,
472 \ 'minwidth': 20,
473 \})
474 redraw
475 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
476 call assert_equal('world ', line)
477
478 call popup_move(winid, {'line': 2, 'col': 2})
479 redraw
480 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
481 call assert_equal('hello ', line)
482 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
483 call assert_equal('~world', line)
484
485 call popup_move(winid, {'line': 1})
486 redraw
487 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
488 call assert_equal('hworld', line)
489
490 call popup_close(winid)
491
492 bwipe!
493endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200494
Bram Moolenaar402502d2019-05-30 22:07:36 +0200495func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200496 let winid = popup_create('hello', {
497 \ 'line': 2,
498 \ 'col': 3,
499 \ 'minwidth': 10,
500 \ 'minheight': 11,
501 \})
502 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200503 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200504 call assert_equal(2, res.line)
505 call assert_equal(3, res.col)
506 call assert_equal(10, res.width)
507 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200508 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200509
510 call popup_close(winid)
511endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200512
513func Test_popup_width_longest()
514 let tests = [
515 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
516 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
517 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
518 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
519 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
520 \ ]
521
522 for test in tests
523 let winid = popup_create(test[0], {'line': 2, 'col': 3})
524 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200525 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200526 call assert_equal(test[1], position.width)
527 call popup_close(winid)
528 endfor
529endfunc
530
531func Test_popup_wraps()
532 let tests = [
533 \ ['nowrap', 6, 1],
534 \ ['a line that wraps once', 12, 2],
535 \ ['a line that wraps two times', 12, 3],
536 \ ]
537 for test in tests
538 let winid = popup_create(test[0],
539 \ {'line': 2, 'col': 3, 'maxwidth': 12})
540 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200541 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200542 call assert_equal(test[1], position.width)
543 call assert_equal(test[2], position.height)
544
545 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200546 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200547 endfor
548endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200549
550func Test_popup_getoptions()
551 let winid = popup_create('hello', {
552 \ 'line': 2,
553 \ 'col': 3,
554 \ 'minwidth': 10,
555 \ 'minheight': 11,
556 \ 'maxwidth': 20,
557 \ 'maxheight': 21,
558 \ 'zindex': 100,
559 \ 'time': 5000,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200560 \ 'fixed': 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200561 \})
562 redraw
563 let res = popup_getoptions(winid)
564 call assert_equal(2, res.line)
565 call assert_equal(3, res.col)
566 call assert_equal(10, res.minwidth)
567 call assert_equal(11, res.minheight)
568 call assert_equal(20, res.maxwidth)
569 call assert_equal(21, res.maxheight)
570 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200571 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200572 if has('timers')
573 call assert_equal(5000, res.time)
574 endif
575 call popup_close(winid)
576
577 let winid = popup_create('hello', {})
578 redraw
579 let res = popup_getoptions(winid)
580 call assert_equal(0, res.line)
581 call assert_equal(0, res.col)
582 call assert_equal(0, res.minwidth)
583 call assert_equal(0, res.minheight)
584 call assert_equal(0, res.maxwidth)
585 call assert_equal(0, res.maxheight)
586 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200587 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200588 if has('timers')
589 call assert_equal(0, res.time)
590 endif
591 call popup_close(winid)
592 call assert_equal({}, popup_getoptions(winid))
593endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200594
595func Test_popup_option_values()
596 new
597 " window-local
598 setlocal number
599 setlocal nowrap
600 " buffer-local
601 setlocal omnifunc=Something
602 " global/buffer-local
603 setlocal path=/there
604 " global/window-local
605 setlocal scrolloff=9
606
607 let winid = popup_create('hello', {})
608 call assert_equal(0, getwinvar(winid, '&number'))
609 call assert_equal(1, getwinvar(winid, '&wrap'))
610 call assert_equal('', getwinvar(winid, '&omnifunc'))
611 call assert_equal(&g:path, getwinvar(winid, '&path'))
612 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
613
614 call popup_close(winid)
615 bwipe
616endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200617
618func Test_popup_atcursor()
619 topleft vnew
620 call setline(1, [
621 \ 'xxxxxxxxxxxxxxxxx',
622 \ 'xxxxxxxxxxxxxxxxx',
623 \ 'xxxxxxxxxxxxxxxxx',
624 \])
625
626 call cursor(2, 2)
627 redraw
628 let winid = popup_atcursor('vim', {})
629 redraw
630 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
631 call assert_equal('xvimxxxxxxxxxxxxx', line)
632 call popup_close(winid)
633
634 call cursor(3, 4)
635 redraw
636 let winid = popup_atcursor('vim', {})
637 redraw
638 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
639 call assert_equal('xxxvimxxxxxxxxxxx', line)
640 call popup_close(winid)
641
642 call cursor(1, 1)
643 redraw
644 let winid = popup_create('vim', {
645 \ 'line': 'cursor+2',
646 \ 'col': 'cursor+1',
647 \})
648 redraw
649 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
650 call assert_equal('xvimxxxxxxxxxxxxx', line)
651 call popup_close(winid)
652
653 call cursor(3, 3)
654 redraw
655 let winid = popup_create('vim', {
656 \ 'line': 'cursor-2',
657 \ 'col': 'cursor-1',
658 \})
659 redraw
660 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
661 call assert_equal('xvimxxxxxxxxxxxxx', line)
662 call popup_close(winid)
663
Bram Moolenaar402502d2019-05-30 22:07:36 +0200664 " just enough room above
665 call cursor(3, 3)
666 redraw
667 let winid = popup_atcursor(['vim', 'is great'], {})
668 redraw
669 let pos = popup_getpos(winid)
670 call assert_equal(1, pos.line)
671 call popup_close(winid)
672
673 " not enough room above, popup goes below the cursor
674 call cursor(3, 3)
675 redraw
676 let winid = popup_atcursor(['vim', 'is', 'great'], {})
677 redraw
678 let pos = popup_getpos(winid)
679 call assert_equal(4, pos.line)
680 call popup_close(winid)
681
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200682 " cursor in first line, popup in line 2
683 call cursor(1, 1)
684 redraw
685 let winid = popup_atcursor(['vim', 'is', 'great'], {})
686 redraw
687 let pos = popup_getpos(winid)
688 call assert_equal(2, pos.line)
689 call popup_close(winid)
690
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200691 bwipe!
692endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200693
694func Test_popup_filter()
695 new
696 call setline(1, 'some text')
697
698 func MyPopupFilter(winid, c)
699 if a:c == 'e'
700 let g:eaten = 'e'
701 return 1
702 endif
703 if a:c == '0'
704 let g:ignored = '0'
705 return 0
706 endif
707 if a:c == 'x'
708 call popup_close(a:winid)
709 return 1
710 endif
711 return 0
712 endfunc
713
714 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
715 redraw
716
717 " e is consumed by the filter
718 call feedkeys('e', 'xt')
719 call assert_equal('e', g:eaten)
720
721 " 0 is ignored by the filter
722 normal $
723 call assert_equal(9, getcurpos()[2])
724 call feedkeys('0', 'xt')
725 call assert_equal('0', g:ignored)
726 call assert_equal(1, getcurpos()[2])
727
728 " x closes the popup
729 call feedkeys('x', 'xt')
730 call assert_equal('e', g:eaten)
731 call assert_equal(-1, winbufnr(winid))
732
733 delfunc MyPopupFilter
734 popupclear
735endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200736
737func Test_popup_close_callback()
738 func PopupDone(id, result)
739 let g:result = a:result
740 endfunc
741 let winid = popup_create('something', {'callback': 'PopupDone'})
742 redraw
743 call popup_close(winid, 'done')
744 call assert_equal('done', g:result)
745endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200746
747func Test_popup_empty()
748 let winid = popup_create('', {'padding': [2,2,2,2]})
749 redraw
750 let pos = popup_getpos(winid)
751 call assert_equal(4, pos.width)
752 call assert_equal(5, pos.height)
753
754 let winid = popup_create([], {'border': []})
755 redraw
756 let pos = popup_getpos(winid)
757 call assert_equal(2, pos.width)
758 call assert_equal(3, pos.height)
759endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +0200760
761func Test_popup_never_behind()
762 if !CanRunVimInTerminal()
763 return
764 endif
765 " +-----------------------------+
766 " | | |
767 " | | |
768 " | | |
769 " | line1 |
770 " |------------line2------------|
771 " | line3 |
772 " | line4 |
773 " | |
774 " | |
775 " +-----------------------------+
776 let lines =<< trim END
777 only
778 split
779 vsplit
780 let info_window1 = getwininfo()[0]
781 let line = info_window1['height']
782 let col = info_window1['width']
783 call popup_create(['line1', 'line2', 'line3', 'line4'], {
784 \ 'line' : line,
785 \ 'col' : col,
786 \ })
787 END
788 call writefile(lines, 'XtestPopupBehind')
789 let buf = RunVimInTerminal('-S XtestPopupBehind', {'rows': 10})
790 call term_sendkeys(buf, "\<C-W>w")
791 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
792
793 " clean up
794 call StopVimInTerminal(buf)
795 call delete('XtestPopupBehind')
796endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200797
798func s:VerifyPosition( p, msg, line, col, width, height )
799 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
800 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
801 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
802 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
803endfunc
804
805func Test_popup_position_adjust()
806 " Anything placed past 2 cells from of the right of the screen is moved to the
807 " left.
808 "
809 " When wrapping is disabled, we also shift to the left to display on the
810 " screen, unless fixed is set.
811
812 " Entries for cases which don't vary based on wrapping.
813 " Format is per tests described below
814 let both_wrap_tests = [
815 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
816 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
817 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
818 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
819 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
820 \
821 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
822 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
823 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
824 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
825 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
826 \
827 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
828 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
829 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
830 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
831 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
832 \ ]
833
834 " these test groups are dicts with:
835 " - comment: something to identify the group of tests by
836 " - options: dict of options to merge with the row/col in tests
837 " - tests: list of cases. Each one is a list with elements:
838 " - text
839 " - row
840 " - col
841 " - expected row
842 " - expected col
843 " - expected width
844 " - expected height
845 let tests = [
846 \ {
847 \ 'comment': 'left-aligned with wrapping',
848 \ 'options': {
849 \ 'wrap': 1,
850 \ 'pos': 'botleft',
851 \ },
852 \ 'tests': both_wrap_tests + [
853 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
854 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
855 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
856 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
857 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
858 \ ],
859 \ },
860 \ {
861 \ 'comment': 'left aligned without wrapping',
862 \ 'options': {
863 \ 'wrap': 0,
864 \ 'pos': 'botleft',
865 \ },
866 \ 'tests': both_wrap_tests + [
867 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
868 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
869 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
870 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
871 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
872 \ ],
873 \ },
874 \ {
875 \ 'comment': 'left aligned with fixed position',
876 \ 'options': {
877 \ 'wrap': 0,
878 \ 'fixed': 1,
879 \ 'pos': 'botleft',
880 \ },
881 \ 'tests': both_wrap_tests + [
882 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
883 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
884 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
885 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
886 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
887 \ ],
888 \ },
889 \ ]
890
891 for test_group in tests
892 for test in test_group.tests
893 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
894 let options = {
895 \ 'line': line,
896 \ 'col': col,
897 \ }
898 call extend( options, test_group.options )
899
900 let p = popup_create( text, options )
901
902 let msg = string( extend( options, { 'text': text } ) )
903 call s:VerifyPosition( p, msg, e_line, e_col, e_width, e_height )
904 call popup_close( p )
905 endfor
906 endfor
907
908 popupclear
909 %bwipe!
910endfunc
911
Bram Moolenaar3397f742019-06-02 18:40:06 +0200912func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200913 " width of screen
914 let X = join(map(range(&columns), {->'X'}), '')
915
916 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
917 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
918
919 redraw
920 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
921 call assert_equal(X, line)
922
923 call popup_close( p )
924 redraw
925
926 " Same if placed on the right hand side
927 let p = popup_create( X, { 'line': 1, 'col': &columns, 'wrap': 0 } )
928 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
929
930 redraw
931 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
932 call assert_equal(X, line)
933
934 call popup_close( p )
935 redraw
936
937 " Extend so > window width
938 let X .= 'x'
939
940 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
941 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
942
943 redraw
944 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
945 call assert_equal(X[ : -2 ], line)
946
947 call popup_close( p )
948 redraw
949
950 " Shifted then truncated (the x is not visible)
951 let p = popup_create( X, { 'line': 1, 'col': &columns - 3, 'wrap': 0 } )
952 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
953
954 redraw
955 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
956 call assert_equal(X[ : -2 ], line)
957
958 call popup_close( p )
959 redraw
960
961 " Not shifted, just truncated
962 let p = popup_create( X,
963 \ { 'line': 1, 'col': 2, 'wrap': 0, 'fixed': 1 } )
964 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
965
966 redraw
967 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
968 let e_line = ' ' . X[ 1 : -2 ]
969 call assert_equal(e_line, line)
970
971 call popup_close( p )
972 redraw
973
974 popupclear
975 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +0200976endfunc
977
978func Test_popup_moved()
979 new
980 call test_override('char_avail', 1)
981 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
982
983 exe "normal gg0/word\<CR>"
984 let winid = popup_atcursor('text', {'moved': 'any'})
985 redraw
986 call assert_equal(1, popup_getpos(winid).visible)
987 " trigger the check for last_cursormoved by going into insert mode
988 call feedkeys("li\<Esc>", 'xt')
989 call assert_equal({}, popup_getpos(winid))
990 popupclear
991
992 exe "normal gg0/word\<CR>"
993 let winid = popup_atcursor('text', {'moved': 'word'})
994 redraw
995 call assert_equal(1, popup_getpos(winid).visible)
996 call feedkeys("hi\<Esc>", 'xt')
997 call assert_equal({}, popup_getpos(winid))
998 popupclear
999
1000 exe "normal gg0/word\<CR>"
1001 let winid = popup_atcursor('text', {'moved': 'word'})
1002 redraw
1003 call assert_equal(1, popup_getpos(winid).visible)
1004 call feedkeys("li\<Esc>", 'xt')
1005 call assert_equal(1, popup_getpos(winid).visible)
1006 call feedkeys("ei\<Esc>", 'xt')
1007 call assert_equal(1, popup_getpos(winid).visible)
1008 call feedkeys("eli\<Esc>", 'xt')
1009 call assert_equal({}, popup_getpos(winid))
1010 popupclear
1011
1012 exe "normal gg0/WORD\<CR>"
1013 let winid = popup_atcursor('text', {'moved': 'WORD'})
1014 redraw
1015 call assert_equal(1, popup_getpos(winid).visible)
1016 call feedkeys("eli\<Esc>", 'xt')
1017 call assert_equal(1, popup_getpos(winid).visible)
1018 call feedkeys("wi\<Esc>", 'xt')
1019 call assert_equal(1, popup_getpos(winid).visible)
1020 call feedkeys("Eli\<Esc>", 'xt')
1021 call assert_equal({}, popup_getpos(winid))
1022 popupclear
1023
1024 exe "normal gg0/word\<CR>"
1025 let winid = popup_atcursor('text', {'moved': [5, 10]})
1026 redraw
1027 call assert_equal(1, popup_getpos(winid).visible)
1028 call feedkeys("eli\<Esc>", 'xt')
1029 call feedkeys("ei\<Esc>", 'xt')
1030 call assert_equal(1, popup_getpos(winid).visible)
1031 call feedkeys("eli\<Esc>", 'xt')
1032 call assert_equal({}, popup_getpos(winid))
1033 popupclear
1034
1035 bwipe!
1036 call test_override('ALL', 0)
1037endfunc