blob: 5983dfb47d82266c57b8de67be0b08c3e10a96a1 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
3if !has('textprop')
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02004 throw 'Skipped: textprop feature missing'
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005endif
6
7source screendump.vim
8
9func Test_simple_popup()
10 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020011 throw 'Skipped: cannot make screendumps'
Bram Moolenaar4d784b22019-05-25 19:51:39 +020012 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 Moolenaar24a5ac52019-06-08 19:01:18 +020054 " move popup over ruler
55 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
56 call term_sendkeys(buf, ":call popup_move(popupwin, {'line': 7, 'col': 55})\<CR>")
57 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
58
59 " clear all popups after moving the cursor a bit, so that ruler is updated
60 call term_sendkeys(buf, "axxx\<Esc>")
61 call term_wait(buf)
62 call term_sendkeys(buf, "0")
63 call term_wait(buf)
64 call term_sendkeys(buf, ":popupclear\<CR>")
65 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
66
Bram Moolenaar4d784b22019-05-25 19:51:39 +020067 " clean up
68 call StopVimInTerminal(buf)
69 call delete('XtestPopup')
70endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020071
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020072func Test_popup_with_border_and_padding()
73 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020074 throw 'Skipped: cannot make screendumps'
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020075 endif
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020076
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020077 for iter in range(0, 1)
78 call writefile([iter == 1 ? '' : 'set enc=latin1',
79 \ "call setline(1, range(1, 100))",
80 \ "call popup_create('hello border', {'line': 2, 'col': 3, 'border': []})",
81 \ "call popup_create('hello padding', {'line': 2, 'col': 23, 'padding': []})",
82 \ "call popup_create('hello both', {'line': 2, 'col': 43, 'border': [], 'padding': []})",
83 \ "call popup_create('border TL', {'line': 6, 'col': 3, 'border': [1, 0, 0, 4]})",
84 \ "call popup_create('paddings', {'line': 6, 'col': 23, 'padding': [1, 3, 2, 4]})",
85 \], 'XtestPopupBorder')
86 let buf = RunVimInTerminal('-S XtestPopupBorder', {'rows': 15})
87 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
88
89 call StopVimInTerminal(buf)
90 call delete('XtestPopupBorder')
91 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020092
Bram Moolenaar790498b2019-06-01 22:15:29 +020093 call writefile([
94 \ "call setline(1, range(1, 100))",
95 \ "hi BlueColor ctermbg=lightblue",
96 \ "hi TopColor ctermbg=253",
97 \ "hi RightColor ctermbg=245",
98 \ "hi BottomColor ctermbg=240",
99 \ "hi LeftColor ctermbg=248",
100 \ "call popup_create('hello border', {'line': 2, 'col': 3, 'border': [], 'borderhighlight': ['BlueColor']})",
101 \ "call popup_create(['hello border', 'and more'], {'line': 2, 'col': 23, 'border': [], 'borderhighlight': ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})",
102 \ "call popup_create(['hello border', 'lines only'], {'line': 2, 'col': 43, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['x']})",
103 \ "call popup_create(['hello border', 'with corners'], {'line': 2, 'col': 60, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['x', '#']})",
104 \ "call popup_create(['hello border', 'with numbers'], {'line': 6, 'col': 3, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['0', '1', '2', '3', '4', '5', '6', '7']})",
105 \ "call popup_create(['hello border', 'just blanks'], {'line': 7, 'col': 23, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': [' ']})",
106 \], 'XtestPopupBorder')
107 let buf = RunVimInTerminal('-S XtestPopupBorder', {'rows': 12})
108 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
109
110 call StopVimInTerminal(buf)
111 call delete('XtestPopupBorder')
112
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200113 let with_border_or_padding = {
114 \ 'line': 2,
115 \ 'core_line': 3,
116 \ 'col': 3,
117 \ 'core_col': 4,
118 \ 'width': 14,
119 \ 'core_width': 12,
120 \ 'height': 3,
121 \ 'core_height': 1,
122 \ 'visible': 1}
123 let winid = popup_create('hello border', {'line': 2, 'col': 3, 'border': []})",
124 call assert_equal(with_border_or_padding, popup_getpos(winid))
125
126 let winid = popup_create('hello paddng', {'line': 2, 'col': 3, 'padding': []})
127 call assert_equal(with_border_or_padding, popup_getpos(winid))
128
129 let winid = popup_create('hello both', {'line': 3, 'col': 8, 'border': [], 'padding': []})
130 call assert_equal({
131 \ 'line': 3,
132 \ 'core_line': 5,
133 \ 'col': 8,
134 \ 'core_col': 10,
135 \ 'width': 14,
136 \ 'core_width': 10,
137 \ 'height': 5,
138 \ 'core_height': 1,
139 \ 'visible': 1}, popup_getpos(winid))
140endfunc
141
Bram Moolenaarb4230122019-05-30 18:40:53 +0200142func Test_popup_with_syntax_win_execute()
143 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200144 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200145 endif
146 call writefile([
147 \ "call setline(1, range(1, 100))",
148 \ "hi PopupColor ctermbg=lightblue",
149 \ "let winid = popup_create([",
150 \ "\\ '#include <stdio.h>',",
151 \ "\\ 'int main(void)',",
152 \ "\\ '{',",
153 \ "\\ ' printf(123);',",
154 \ "\\ '}',",
155 \ "\\], {'line': 3, 'col': 25, 'highlight': 'PopupColor'})",
156 \ "call win_execute(winid, 'set syntax=cpp')",
157 \], 'XtestPopup')
158 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
159 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
160
161 " clean up
162 call StopVimInTerminal(buf)
163 call delete('XtestPopup')
164endfunc
165
166func Test_popup_with_syntax_setbufvar()
167 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200168 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200169 endif
Bram Moolenaar402502d2019-05-30 22:07:36 +0200170 let lines =<< trim END
171 call setline(1, range(1, 100))
172 hi PopupColor ctermbg=lightgrey
173 let winid = popup_create([
174 \ '#include <stdio.h>',
175 \ 'int main(void)',
176 \ '{',
177 \ ' printf(567);',
178 \ '}',
179 \], {'line': 3, 'col': 21, 'highlight': 'PopupColor'})
180 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
181 END
182 call writefile(lines, 'XtestPopup')
Bram Moolenaarb4230122019-05-30 18:40:53 +0200183 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
184 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
185
186 " clean up
187 call StopVimInTerminal(buf)
188 call delete('XtestPopup')
189endfunc
190
Bram Moolenaar399d8982019-06-02 15:34:29 +0200191func Test_popup_all_corners()
192 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200193 throw 'Skipped: cannot make screendumps'
Bram Moolenaar399d8982019-06-02 15:34:29 +0200194 endif
195 let lines =<< trim END
196 call setline(1, repeat([repeat('-', 60)], 15))
197 set so=0
198 normal 2G3|r#
199 let winid1 = popup_create(['first', 'second'], {
200 \ 'line': 'cursor+1',
201 \ 'col': 'cursor',
202 \ 'pos': 'topleft',
203 \ 'border': [],
204 \ 'padding': [],
205 \ })
206 normal 25|r@
207 let winid1 = popup_create(['First', 'SeconD'], {
208 \ 'line': 'cursor+1',
209 \ 'col': 'cursor',
210 \ 'pos': 'topright',
211 \ 'border': [],
212 \ 'padding': [],
213 \ })
214 normal 9G29|r%
215 let winid1 = popup_create(['fiRSt', 'seCOnd'], {
216 \ 'line': 'cursor-1',
217 \ 'col': 'cursor',
218 \ 'pos': 'botleft',
219 \ 'border': [],
220 \ 'padding': [],
221 \ })
222 normal 51|r&
223 let winid1 = popup_create(['FIrsT', 'SEcoND'], {
224 \ 'line': 'cursor-1',
225 \ 'col': 'cursor',
226 \ 'pos': 'botright',
227 \ 'border': [],
228 \ 'padding': [],
229 \ })
230 END
231 call writefile(lines, 'XtestPopupCorners')
232 let buf = RunVimInTerminal('-S XtestPopupCorners', {'rows': 12})
233 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
234
235 " clean up
236 call StopVimInTerminal(buf)
237 call delete('XtestPopupCorners')
238endfunc
239
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200240func Test_popup_in_tab()
241 " default popup is local to tab, not visible when in other tab
242 let winid = popup_create("text", {})
243 call assert_equal(1, popup_getpos(winid).visible)
244 tabnew
245 call assert_equal(0, popup_getpos(winid).visible)
246 quit
247 call assert_equal(1, popup_getpos(winid).visible)
248 popupclear
249
250 " global popup is visible in any tab
251 let winid = popup_create("text", {'tab': -1})
252 call assert_equal(1, popup_getpos(winid).visible)
253 tabnew
254 call assert_equal(1, popup_getpos(winid).visible)
255 quit
256 call assert_equal(1, popup_getpos(winid).visible)
257 popupclear
258endfunc
259
260func Test_popup_valid_arguments()
261 " Zero value is like the property wasn't there
262 let winid = popup_create("text", {"col": 0})
263 let pos = popup_getpos(winid)
264 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
265 popupclear
266
267 " using cursor column has minimum value of 1
268 let winid = popup_create("text", {"col": 'cursor-100'})
269 let pos = popup_getpos(winid)
270 call assert_equal(1, pos.col)
271 popupclear
272
273 " center
274 let winid = popup_create("text", {"pos": 'center'})
275 let pos = popup_getpos(winid)
276 let around = (&columns - pos.width) / 2
277 call assert_inrange(around - 1, around + 1, pos.col)
278 let around = (&lines - pos.height) / 2
279 call assert_inrange(around - 1, around + 1, pos.line)
280 popupclear
281endfunc
282
283func Test_popup_invalid_arguments()
284 call assert_fails('call popup_create(666, {})', 'E714:')
285 popupclear
286 call assert_fails('call popup_create("text", "none")', 'E715:')
287 popupclear
288
289 call assert_fails('call popup_create("text", {"col": "xxx"})', 'E475:')
290 popupclear
291 call assert_fails('call popup_create("text", {"col": "cursor8"})', 'E15:')
292 popupclear
293 call assert_fails('call popup_create("text", {"col": "cursor+x"})', 'E15:')
294 popupclear
295 call assert_fails('call popup_create("text", {"col": "cursor+8x"})', 'E15:')
296 popupclear
297
298 call assert_fails('call popup_create("text", {"line": "xxx"})', 'E475:')
299 popupclear
300 call assert_fails('call popup_create("text", {"line": "cursor8"})', 'E15:')
301 popupclear
302 call assert_fails('call popup_create("text", {"line": "cursor+x"})', 'E15:')
303 popupclear
304 call assert_fails('call popup_create("text", {"line": "cursor+8x"})', 'E15:')
305 popupclear
306
307 call assert_fails('call popup_create("text", {"pos": "there"})', 'E475:')
308 popupclear
309 call assert_fails('call popup_create("text", {"padding": "none"})', 'E714:')
310 popupclear
311 call assert_fails('call popup_create("text", {"border": "none"})', 'E714:')
312 popupclear
313 call assert_fails('call popup_create("text", {"borderhighlight": "none"})', 'E714:')
314 popupclear
315 call assert_fails('call popup_create("text", {"borderchars": "none"})', 'E714:')
316 popupclear
317
318 call assert_fails('call popup_create([{"text": "text"}, 666], {})', 'E715:')
319 popupclear
320 call assert_fails('call popup_create([{"text": "text", "props": "none"}], {})', 'E714:')
321 popupclear
322 call assert_fails('call popup_create([{"text": "text", "props": ["none"]}], {})', 'E715:')
323 popupclear
324endfunc
325
Bram Moolenaareea16992019-05-31 17:34:48 +0200326func Test_win_execute_closing_curwin()
327 split
328 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200329 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
330 popupclear
331endfunc
332
333func Test_win_execute_not_allowed()
334 let winid = popup_create('some text', {})
335 call assert_fails('call win_execute(winid, "split")', 'E994:')
336 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
337 call assert_fails('call win_execute(winid, "close")', 'E994:')
338 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200339 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200340 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
341 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
342 call assert_fails('call win_execute(winid, "next")', 'E994:')
343 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
344 call assert_fails('call win_execute(winid, "buf")', 'E994:')
345 call assert_fails('call win_execute(winid, "edit")', 'E994:')
346 call assert_fails('call win_execute(winid, "enew")', 'E994:')
347 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
348 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
349 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
350 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaareea16992019-05-31 17:34:48 +0200351 popupclear
352endfunc
353
Bram Moolenaar402502d2019-05-30 22:07:36 +0200354func Test_popup_with_wrap()
355 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200356 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200357 endif
358 let lines =<< trim END
359 call setline(1, range(1, 100))
360 let winid = popup_create(
361 \ 'a long line that wont fit',
362 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
363 END
364 call writefile(lines, 'XtestPopup')
365 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
366 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
367
368 " clean up
369 call StopVimInTerminal(buf)
370 call delete('XtestPopup')
371endfunc
372
373func Test_popup_without_wrap()
374 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200375 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200376 endif
377 let lines =<< trim END
378 call setline(1, range(1, 100))
379 let winid = popup_create(
380 \ 'a long line that wont fit',
381 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
382 END
383 call writefile(lines, 'XtestPopup')
384 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
385 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
386
387 " clean up
388 call StopVimInTerminal(buf)
389 call delete('XtestPopup')
390endfunc
391
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200392func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200393 if !has('timers')
394 return
395 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200396 topleft vnew
397 call setline(1, 'hello')
398
399 call popup_create('world', {
400 \ 'line': 1,
401 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200402 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200403 \ 'time': 500,
404 \})
405 redraw
406 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
407 call assert_equal('world', line)
408
409 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200410 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200411 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
412 call assert_equal('hello', line)
413
414 call popup_create('on the command line', {
415 \ 'line': &lines,
416 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200417 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200418 \ 'time': 500,
419 \})
420 redraw
421 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
422 call assert_match('.*on the command line.*', line)
423
424 sleep 700m
425 redraw
426 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
427 call assert_notmatch('.*on the command line.*', line)
428
429 bwipe!
430endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200431
432func Test_popup_hide()
433 topleft vnew
434 call setline(1, 'hello')
435
436 let winid = popup_create('world', {
437 \ 'line': 1,
438 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200439 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200440 \})
441 redraw
442 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
443 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200444 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200445 " buffer is still listed and active
446 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200447
448 call popup_hide(winid)
449 redraw
450 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
451 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200452 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200453 " buffer is still listed but hidden
454 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200455
456 call popup_show(winid)
457 redraw
458 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
459 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200460 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200461
462
463 call popup_close(winid)
464 redraw
465 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
466 call assert_equal('hello', line)
467
468 " error is given for existing non-popup window
469 call assert_fails('call popup_hide(win_getid())', 'E993:')
470
471 " no error non-existing window
472 call popup_hide(1234234)
473 call popup_show(41234234)
474
475 bwipe!
476endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200477
478func Test_popup_move()
479 topleft vnew
480 call setline(1, 'hello')
481
482 let winid = popup_create('world', {
483 \ 'line': 1,
484 \ 'col': 1,
485 \ 'minwidth': 20,
486 \})
487 redraw
488 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
489 call assert_equal('world ', line)
490
491 call popup_move(winid, {'line': 2, 'col': 2})
492 redraw
493 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
494 call assert_equal('hello ', line)
495 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
496 call assert_equal('~world', line)
497
498 call popup_move(winid, {'line': 1})
499 redraw
500 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
501 call assert_equal('hworld', line)
502
503 call popup_close(winid)
504
505 bwipe!
506endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200507
Bram Moolenaar402502d2019-05-30 22:07:36 +0200508func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200509 let winid = popup_create('hello', {
510 \ 'line': 2,
511 \ 'col': 3,
512 \ 'minwidth': 10,
513 \ 'minheight': 11,
514 \})
515 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200516 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200517 call assert_equal(2, res.line)
518 call assert_equal(3, res.col)
519 call assert_equal(10, res.width)
520 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200521 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200522
523 call popup_close(winid)
524endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200525
526func Test_popup_width_longest()
527 let tests = [
528 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
529 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
530 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
531 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
532 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
533 \ ]
534
535 for test in tests
536 let winid = popup_create(test[0], {'line': 2, 'col': 3})
537 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200538 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200539 call assert_equal(test[1], position.width)
540 call popup_close(winid)
541 endfor
542endfunc
543
544func Test_popup_wraps()
545 let tests = [
546 \ ['nowrap', 6, 1],
547 \ ['a line that wraps once', 12, 2],
548 \ ['a line that wraps two times', 12, 3],
549 \ ]
550 for test in tests
551 let winid = popup_create(test[0],
552 \ {'line': 2, 'col': 3, 'maxwidth': 12})
553 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200554 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200555 call assert_equal(test[1], position.width)
556 call assert_equal(test[2], position.height)
557
558 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200559 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200560 endfor
561endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200562
563func Test_popup_getoptions()
564 let winid = popup_create('hello', {
565 \ 'line': 2,
566 \ 'col': 3,
567 \ 'minwidth': 10,
568 \ 'minheight': 11,
569 \ 'maxwidth': 20,
570 \ 'maxheight': 21,
571 \ 'zindex': 100,
572 \ 'time': 5000,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200573 \ 'fixed': 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200574 \})
575 redraw
576 let res = popup_getoptions(winid)
577 call assert_equal(2, res.line)
578 call assert_equal(3, res.col)
579 call assert_equal(10, res.minwidth)
580 call assert_equal(11, res.minheight)
581 call assert_equal(20, res.maxwidth)
582 call assert_equal(21, res.maxheight)
583 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200584 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200585 if has('timers')
586 call assert_equal(5000, res.time)
587 endif
588 call popup_close(winid)
589
590 let winid = popup_create('hello', {})
591 redraw
592 let res = popup_getoptions(winid)
593 call assert_equal(0, res.line)
594 call assert_equal(0, res.col)
595 call assert_equal(0, res.minwidth)
596 call assert_equal(0, res.minheight)
597 call assert_equal(0, res.maxwidth)
598 call assert_equal(0, res.maxheight)
599 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200600 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200601 if has('timers')
602 call assert_equal(0, res.time)
603 endif
604 call popup_close(winid)
605 call assert_equal({}, popup_getoptions(winid))
606endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200607
608func Test_popup_option_values()
609 new
610 " window-local
611 setlocal number
612 setlocal nowrap
613 " buffer-local
614 setlocal omnifunc=Something
615 " global/buffer-local
616 setlocal path=/there
617 " global/window-local
618 setlocal scrolloff=9
619
620 let winid = popup_create('hello', {})
621 call assert_equal(0, getwinvar(winid, '&number'))
622 call assert_equal(1, getwinvar(winid, '&wrap'))
623 call assert_equal('', getwinvar(winid, '&omnifunc'))
624 call assert_equal(&g:path, getwinvar(winid, '&path'))
625 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
626
627 call popup_close(winid)
628 bwipe
629endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200630
631func Test_popup_atcursor()
632 topleft vnew
633 call setline(1, [
634 \ 'xxxxxxxxxxxxxxxxx',
635 \ 'xxxxxxxxxxxxxxxxx',
636 \ 'xxxxxxxxxxxxxxxxx',
637 \])
638
639 call cursor(2, 2)
640 redraw
641 let winid = popup_atcursor('vim', {})
642 redraw
643 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
644 call assert_equal('xvimxxxxxxxxxxxxx', line)
645 call popup_close(winid)
646
647 call cursor(3, 4)
648 redraw
649 let winid = popup_atcursor('vim', {})
650 redraw
651 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
652 call assert_equal('xxxvimxxxxxxxxxxx', line)
653 call popup_close(winid)
654
655 call cursor(1, 1)
656 redraw
657 let winid = popup_create('vim', {
658 \ 'line': 'cursor+2',
659 \ 'col': 'cursor+1',
660 \})
661 redraw
662 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
663 call assert_equal('xvimxxxxxxxxxxxxx', line)
664 call popup_close(winid)
665
666 call cursor(3, 3)
667 redraw
668 let winid = popup_create('vim', {
669 \ 'line': 'cursor-2',
670 \ 'col': 'cursor-1',
671 \})
672 redraw
673 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
674 call assert_equal('xvimxxxxxxxxxxxxx', line)
675 call popup_close(winid)
676
Bram Moolenaar402502d2019-05-30 22:07:36 +0200677 " just enough room above
678 call cursor(3, 3)
679 redraw
680 let winid = popup_atcursor(['vim', 'is great'], {})
681 redraw
682 let pos = popup_getpos(winid)
683 call assert_equal(1, pos.line)
684 call popup_close(winid)
685
686 " not enough room above, popup goes below the cursor
687 call cursor(3, 3)
688 redraw
689 let winid = popup_atcursor(['vim', 'is', 'great'], {})
690 redraw
691 let pos = popup_getpos(winid)
692 call assert_equal(4, pos.line)
693 call popup_close(winid)
694
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200695 " cursor in first line, popup in line 2
696 call cursor(1, 1)
697 redraw
698 let winid = popup_atcursor(['vim', 'is', 'great'], {})
699 redraw
700 let pos = popup_getpos(winid)
701 call assert_equal(2, pos.line)
702 call popup_close(winid)
703
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200704 bwipe!
705endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200706
707func Test_popup_filter()
708 new
709 call setline(1, 'some text')
710
711 func MyPopupFilter(winid, c)
712 if a:c == 'e'
713 let g:eaten = 'e'
714 return 1
715 endif
716 if a:c == '0'
717 let g:ignored = '0'
718 return 0
719 endif
720 if a:c == 'x'
721 call popup_close(a:winid)
722 return 1
723 endif
724 return 0
725 endfunc
726
727 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
728 redraw
729
730 " e is consumed by the filter
731 call feedkeys('e', 'xt')
732 call assert_equal('e', g:eaten)
733
734 " 0 is ignored by the filter
735 normal $
736 call assert_equal(9, getcurpos()[2])
737 call feedkeys('0', 'xt')
738 call assert_equal('0', g:ignored)
739 call assert_equal(1, getcurpos()[2])
740
741 " x closes the popup
742 call feedkeys('x', 'xt')
743 call assert_equal('e', g:eaten)
744 call assert_equal(-1, winbufnr(winid))
745
746 delfunc MyPopupFilter
747 popupclear
748endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200749
750func Test_popup_close_callback()
751 func PopupDone(id, result)
752 let g:result = a:result
753 endfunc
754 let winid = popup_create('something', {'callback': 'PopupDone'})
755 redraw
756 call popup_close(winid, 'done')
757 call assert_equal('done', g:result)
758endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200759
760func Test_popup_empty()
761 let winid = popup_create('', {'padding': [2,2,2,2]})
762 redraw
763 let pos = popup_getpos(winid)
764 call assert_equal(4, pos.width)
765 call assert_equal(5, pos.height)
766
767 let winid = popup_create([], {'border': []})
768 redraw
769 let pos = popup_getpos(winid)
770 call assert_equal(2, pos.width)
771 call assert_equal(3, pos.height)
772endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +0200773
774func Test_popup_never_behind()
775 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200776 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +0200777 endif
778 " +-----------------------------+
779 " | | |
780 " | | |
781 " | | |
782 " | line1 |
783 " |------------line2------------|
784 " | line3 |
785 " | line4 |
786 " | |
787 " | |
788 " +-----------------------------+
789 let lines =<< trim END
790 only
791 split
792 vsplit
793 let info_window1 = getwininfo()[0]
794 let line = info_window1['height']
795 let col = info_window1['width']
796 call popup_create(['line1', 'line2', 'line3', 'line4'], {
797 \ 'line' : line,
798 \ 'col' : col,
799 \ })
800 END
801 call writefile(lines, 'XtestPopupBehind')
802 let buf = RunVimInTerminal('-S XtestPopupBehind', {'rows': 10})
803 call term_sendkeys(buf, "\<C-W>w")
804 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
805
806 " clean up
807 call StopVimInTerminal(buf)
808 call delete('XtestPopupBehind')
809endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200810
811func s:VerifyPosition( p, msg, line, col, width, height )
812 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
813 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
814 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
815 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
816endfunc
817
818func Test_popup_position_adjust()
819 " Anything placed past 2 cells from of the right of the screen is moved to the
820 " left.
821 "
822 " When wrapping is disabled, we also shift to the left to display on the
823 " screen, unless fixed is set.
824
825 " Entries for cases which don't vary based on wrapping.
826 " Format is per tests described below
827 let both_wrap_tests = [
828 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
829 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
830 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
831 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
832 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
833 \
834 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
835 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
836 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
837 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
838 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
839 \
840 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
841 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
842 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
843 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
844 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
845 \ ]
846
847 " these test groups are dicts with:
848 " - comment: something to identify the group of tests by
849 " - options: dict of options to merge with the row/col in tests
850 " - tests: list of cases. Each one is a list with elements:
851 " - text
852 " - row
853 " - col
854 " - expected row
855 " - expected col
856 " - expected width
857 " - expected height
858 let tests = [
859 \ {
860 \ 'comment': 'left-aligned with wrapping',
861 \ 'options': {
862 \ 'wrap': 1,
863 \ 'pos': 'botleft',
864 \ },
865 \ 'tests': both_wrap_tests + [
866 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
867 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
868 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
869 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
870 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
871 \ ],
872 \ },
873 \ {
874 \ 'comment': 'left aligned without wrapping',
875 \ 'options': {
876 \ 'wrap': 0,
877 \ 'pos': 'botleft',
878 \ },
879 \ 'tests': both_wrap_tests + [
880 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
881 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
882 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
883 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
884 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
885 \ ],
886 \ },
887 \ {
888 \ 'comment': 'left aligned with fixed position',
889 \ 'options': {
890 \ 'wrap': 0,
891 \ 'fixed': 1,
892 \ 'pos': 'botleft',
893 \ },
894 \ 'tests': both_wrap_tests + [
895 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
896 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
897 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
898 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
899 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
900 \ ],
901 \ },
902 \ ]
903
904 for test_group in tests
905 for test in test_group.tests
906 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
907 let options = {
908 \ 'line': line,
909 \ 'col': col,
910 \ }
911 call extend( options, test_group.options )
912
913 let p = popup_create( text, options )
914
915 let msg = string( extend( options, { 'text': text } ) )
916 call s:VerifyPosition( p, msg, e_line, e_col, e_width, e_height )
917 call popup_close( p )
918 endfor
919 endfor
920
921 popupclear
922 %bwipe!
923endfunc
924
Bram Moolenaar3397f742019-06-02 18:40:06 +0200925func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200926 " width of screen
927 let X = join(map(range(&columns), {->'X'}), '')
928
929 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
930 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
931
932 redraw
933 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
934 call assert_equal(X, line)
935
936 call popup_close( p )
937 redraw
938
939 " Same if placed on the right hand side
940 let p = popup_create( X, { 'line': 1, 'col': &columns, 'wrap': 0 } )
941 call s:VerifyPosition( p, 'full width topright', 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, line)
946
947 call popup_close( p )
948 redraw
949
950 " Extend so > window width
951 let X .= 'x'
952
953 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
954 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
955
956 redraw
957 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
958 call assert_equal(X[ : -2 ], line)
959
960 call popup_close( p )
961 redraw
962
963 " Shifted then truncated (the x is not visible)
964 let p = popup_create( X, { 'line': 1, 'col': &columns - 3, 'wrap': 0 } )
965 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
966
967 redraw
968 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
969 call assert_equal(X[ : -2 ], line)
970
971 call popup_close( p )
972 redraw
973
974 " Not shifted, just truncated
975 let p = popup_create( X,
976 \ { 'line': 1, 'col': 2, 'wrap': 0, 'fixed': 1 } )
977 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
978
979 redraw
980 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
981 let e_line = ' ' . X[ 1 : -2 ]
982 call assert_equal(e_line, line)
983
984 call popup_close( p )
985 redraw
986
987 popupclear
988 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +0200989endfunc
990
991func Test_popup_moved()
992 new
993 call test_override('char_avail', 1)
994 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
995
996 exe "normal gg0/word\<CR>"
997 let winid = popup_atcursor('text', {'moved': 'any'})
998 redraw
999 call assert_equal(1, popup_getpos(winid).visible)
1000 " trigger the check for last_cursormoved by going into insert mode
1001 call feedkeys("li\<Esc>", 'xt')
1002 call assert_equal({}, popup_getpos(winid))
1003 popupclear
1004
1005 exe "normal gg0/word\<CR>"
1006 let winid = popup_atcursor('text', {'moved': 'word'})
1007 redraw
1008 call assert_equal(1, popup_getpos(winid).visible)
1009 call feedkeys("hi\<Esc>", 'xt')
1010 call assert_equal({}, popup_getpos(winid))
1011 popupclear
1012
1013 exe "normal gg0/word\<CR>"
1014 let winid = popup_atcursor('text', {'moved': 'word'})
1015 redraw
1016 call assert_equal(1, popup_getpos(winid).visible)
1017 call feedkeys("li\<Esc>", 'xt')
1018 call assert_equal(1, popup_getpos(winid).visible)
1019 call feedkeys("ei\<Esc>", 'xt')
1020 call assert_equal(1, popup_getpos(winid).visible)
1021 call feedkeys("eli\<Esc>", 'xt')
1022 call assert_equal({}, popup_getpos(winid))
1023 popupclear
1024
Bram Moolenaar17627312019-06-02 19:53:44 +02001025 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001026 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001027 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001028 redraw
1029 call assert_equal(1, popup_getpos(winid).visible)
1030 call feedkeys("eli\<Esc>", 'xt')
1031 call assert_equal(1, popup_getpos(winid).visible)
1032 call feedkeys("wi\<Esc>", 'xt')
1033 call assert_equal(1, popup_getpos(winid).visible)
1034 call feedkeys("Eli\<Esc>", 'xt')
1035 call assert_equal({}, popup_getpos(winid))
1036 popupclear
1037
1038 exe "normal gg0/word\<CR>"
1039 let winid = popup_atcursor('text', {'moved': [5, 10]})
1040 redraw
1041 call assert_equal(1, popup_getpos(winid).visible)
1042 call feedkeys("eli\<Esc>", 'xt')
1043 call feedkeys("ei\<Esc>", 'xt')
1044 call assert_equal(1, popup_getpos(winid).visible)
1045 call feedkeys("eli\<Esc>", 'xt')
1046 call assert_equal({}, popup_getpos(winid))
1047 popupclear
1048
1049 bwipe!
1050 call test_override('ALL', 0)
1051endfunc