blob: 3983b5761a417e00ccdfae84c5b1deea228cc469 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature textprop
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
9 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020010 throw 'Skipped: cannot make screendumps'
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011 endif
Bram Moolenaare7eb9272019-06-24 00:58:07 +020012 let lines =<< trim END
13 call setline(1, range(1, 100))
14 hi PopupColor1 ctermbg=lightblue
15 hi PopupColor2 ctermbg=lightcyan
16 hi Comment ctermfg=red
17 call prop_type_add('comment', {'highlight': 'Comment'})
18 let winid = popup_create('hello there', {'line': 3, 'col': 11, 'minwidth': 20, 'highlight': 'PopupColor1'})
19 let winid2 = popup_create(['another one', 'another two', 'another three'], {'line': 3, 'col': 25, 'minwidth': 20})
20 call setwinvar(winid2, '&wincolor', 'PopupColor2')
21 END
22 call writefile(lines, 'XtestPopup')
Bram Moolenaar4d784b22019-05-25 19:51:39 +020023 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 Moolenaar202d9822019-06-11 21:56:30 +020044 " set 'columns' to a small value, size must be recomputed
45 call term_sendkeys(buf, ":let cols = &columns\<CR>")
46 call term_sendkeys(buf, ":set columns=12\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
48 call term_sendkeys(buf, ":let &columns = cols\<CR>")
49
Bram Moolenaar17146962019-05-30 00:12:11 +020050 " resize popup, show empty line at bottom
Bram Moolenaar60cdb302019-05-27 21:54:10 +020051 call term_sendkeys(buf, ":call popup_move(popupwin, {'minwidth': 15, 'maxwidth': 25, 'minheight': 3, 'maxheight': 5})\<CR>")
52 call term_sendkeys(buf, ":redraw\<CR>")
53 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
54
Bram Moolenaar17146962019-05-30 00:12:11 +020055 " show not fitting line at bottom
56 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
57 call term_sendkeys(buf, ":redraw\<CR>")
58 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
59
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020060 " move popup over ruler
61 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
62 call term_sendkeys(buf, ":call popup_move(popupwin, {'line': 7, 'col': 55})\<CR>")
63 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
64
65 " clear all popups after moving the cursor a bit, so that ruler is updated
66 call term_sendkeys(buf, "axxx\<Esc>")
67 call term_wait(buf)
68 call term_sendkeys(buf, "0")
69 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020070 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020071 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
72
Bram Moolenaar4d784b22019-05-25 19:51:39 +020073 " clean up
74 call StopVimInTerminal(buf)
75 call delete('XtestPopup')
76endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020077
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020078func Test_popup_with_border_and_padding()
79 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020080 throw 'Skipped: cannot make screendumps'
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081 endif
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020083 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020084 let lines =<< trim END
85 call setline(1, range(1, 100))
86 call popup_create('hello border', {'line': 2, 'col': 3, 'border': []})
87 call popup_create('hello padding', {'line': 2, 'col': 23, 'padding': []})
88 call popup_create('hello both', {'line': 2, 'col': 43, 'border': [], 'padding': []})
89 call popup_create('border TL', {'line': 6, 'col': 3, 'border': [1, 0, 0, 4]})
90 call popup_create('paddings', {'line': 6, 'col': 23, 'padding': [1, 3, 2, 4]})
91 call popup_create('wrapped longer text', {'line': 8, 'col': 55, 'padding': [0, 3, 0, 3], 'border': [0, 1, 0, 1]})
92 call popup_create('right aligned text', {'line': 11, 'col': 56, 'wrap': 0, 'padding': [0, 3, 0, 3], 'border': [0, 1, 0, 1]})
93 END
94 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
95 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020096 let buf = RunVimInTerminal('-S XtestPopupBorder', {'rows': 15})
97 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
98
99 call StopVimInTerminal(buf)
100 call delete('XtestPopupBorder')
101 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200102
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200103 let lines =<< trim END
104 call setline(1, range(1, 100))
105 hi BlueColor ctermbg=lightblue
106 hi TopColor ctermbg=253
107 hi RightColor ctermbg=245
108 hi BottomColor ctermbg=240
109 hi LeftColor ctermbg=248
110 call popup_create('hello border', {'line': 2, 'col': 3, 'border': [], 'borderhighlight': ['BlueColor']})
111 call popup_create(['hello border', 'and more'], {'line': 2, 'col': 23, 'border': [], 'borderhighlight': ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
112 call popup_create(['hello border', 'lines only'], {'line': 2, 'col': 43, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['x']})
113 call popup_create(['hello border', 'with corners'], {'line': 2, 'col': 60, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['x', '#']})
114 let winid = popup_create(['hello border', 'with numbers'], {'line': 6, 'col': 3, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['0', '1', '2', '3', '4', '5', '6', '7']})
115 call popup_create(['hello border', 'just blanks'], {'line': 7, 'col': 23, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': [' ']})
116 END
117 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar790498b2019-06-01 22:15:29 +0200118 let buf = RunVimInTerminal('-S XtestPopupBorder', {'rows': 12})
119 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
120
Bram Moolenaarad24a712019-06-17 20:05:45 +0200121 " check that changing borderchars triggers a redraw
122 call term_sendkeys(buf, ":call popup_setoptions(winid, {'borderchars': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
123 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
124
Bram Moolenaar790498b2019-06-01 22:15:29 +0200125 call StopVimInTerminal(buf)
126 call delete('XtestPopupBorder')
127
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200128 let with_border_or_padding = {
129 \ 'line': 2,
130 \ 'core_line': 3,
131 \ 'col': 3,
132 \ 'core_col': 4,
133 \ 'width': 14,
134 \ 'core_width': 12,
135 \ 'height': 3,
136 \ 'core_height': 1,
Bram Moolenaar53a95d62019-06-26 03:54:08 +0200137 \ 'firstline': 1,
Bram Moolenaar6c6a6032019-06-25 05:33:36 +0200138 \ 'scrollbar': 0,
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200139 \ 'visible': 1}
140 let winid = popup_create('hello border', {'line': 2, 'col': 3, 'border': []})",
141 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200142 let options = popup_getoptions(winid)
143 call assert_equal([], options.border)
144 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200145
Bram Moolenaarae943152019-06-16 22:54:14 +0200146 let winid = popup_create('hello padding', {'line': 2, 'col': 3, 'padding': []})
147 let with_border_or_padding.width = 15
148 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200149 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200150 let options = popup_getoptions(winid)
151 call assert_false(has_key(options, "border"))
152 call assert_equal([], options.padding)
153
154 call popup_setoptions(winid, {
155 \ 'padding': [1, 2, 3, 4],
156 \ 'border': [4, 0, 7, 8],
157 \ 'borderhighlight': ['Top', 'Right', 'Bottom', 'Left'],
158 \ 'borderchars': ['1', '^', '2', '>', '3', 'v', '4', '<'],
159 \ })
160 let options = popup_getoptions(winid)
161 call assert_equal([1, 0, 1, 1], options.border)
162 call assert_equal([1, 2, 3, 4], options.padding)
163 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
164 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200165
166 let winid = popup_create('hello both', {'line': 3, 'col': 8, 'border': [], 'padding': []})
167 call assert_equal({
168 \ 'line': 3,
169 \ 'core_line': 5,
170 \ 'col': 8,
171 \ 'core_col': 10,
172 \ 'width': 14,
173 \ 'core_width': 10,
174 \ 'height': 5,
Bram Moolenaar6c6a6032019-06-25 05:33:36 +0200175 \ 'scrollbar': 0,
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200176 \ 'core_height': 1,
Bram Moolenaar53a95d62019-06-26 03:54:08 +0200177 \ 'firstline': 1,
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200178 \ 'visible': 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200179
180 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200181endfunc
182
Bram Moolenaarb4230122019-05-30 18:40:53 +0200183func Test_popup_with_syntax_win_execute()
184 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200185 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200186 endif
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200187 let lines =<< trim END
188 call setline(1, range(1, 100))
189 hi PopupColor ctermbg=lightblue
190 let winid = popup_create([
191 \ '#include <stdio.h>',
192 \ 'int main(void)',
193 \ '{',
194 \ ' printf(123);',
195 \ '}',
196 \], {'line': 3, 'col': 25, 'highlight': 'PopupColor'})
197 call win_execute(winid, 'set syntax=cpp')
198 END
199 call writefile(lines, 'XtestPopup')
Bram Moolenaarb4230122019-05-30 18:40:53 +0200200 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
201 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
202
203 " clean up
204 call StopVimInTerminal(buf)
205 call delete('XtestPopup')
206endfunc
207
208func Test_popup_with_syntax_setbufvar()
209 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200210 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200211 endif
Bram Moolenaar402502d2019-05-30 22:07:36 +0200212 let lines =<< trim END
213 call setline(1, range(1, 100))
214 hi PopupColor ctermbg=lightgrey
215 let winid = popup_create([
216 \ '#include <stdio.h>',
217 \ 'int main(void)',
218 \ '{',
219 \ ' printf(567);',
220 \ '}',
221 \], {'line': 3, 'col': 21, 'highlight': 'PopupColor'})
222 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
223 END
224 call writefile(lines, 'XtestPopup')
Bram Moolenaarb4230122019-05-30 18:40:53 +0200225 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
226 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
227
228 " clean up
229 call StopVimInTerminal(buf)
230 call delete('XtestPopup')
231endfunc
232
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200233func Test_popup_with_matches()
234 if !CanRunVimInTerminal()
235 throw 'Skipped: cannot make screendumps'
236 endif
237 let lines =<< trim END
238 call setline(1, ['111 222 333', '444 555 666'])
239 let winid = popup_create([
240 \ '111 222 333',
241 \ '444 555 666',
242 \], {'line': 3, 'col': 10, 'border': []})
243 set hlsearch
244 /666
245 call matchadd('ErrorMsg', '111')
246 call matchadd('ErrorMsg', '444')
247 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
248 call win_execute(winid, "call matchadd('ErrorMsg', '555')")
249 END
250 call writefile(lines, 'XtestPopupMatches')
251 let buf = RunVimInTerminal('-S XtestPopupMatches', {'rows': 10})
252 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
253
254 " clean up
255 call StopVimInTerminal(buf)
256 call delete('XtestPopupMatches')
257endfunc
258
Bram Moolenaar399d8982019-06-02 15:34:29 +0200259func Test_popup_all_corners()
260 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200261 throw 'Skipped: cannot make screendumps'
Bram Moolenaar399d8982019-06-02 15:34:29 +0200262 endif
263 let lines =<< trim END
264 call setline(1, repeat([repeat('-', 60)], 15))
265 set so=0
266 normal 2G3|r#
267 let winid1 = popup_create(['first', 'second'], {
268 \ 'line': 'cursor+1',
269 \ 'col': 'cursor',
270 \ 'pos': 'topleft',
271 \ 'border': [],
272 \ 'padding': [],
273 \ })
274 normal 25|r@
275 let winid1 = popup_create(['First', 'SeconD'], {
276 \ 'line': 'cursor+1',
277 \ 'col': 'cursor',
278 \ 'pos': 'topright',
279 \ 'border': [],
280 \ 'padding': [],
281 \ })
282 normal 9G29|r%
283 let winid1 = popup_create(['fiRSt', 'seCOnd'], {
284 \ 'line': 'cursor-1',
285 \ 'col': 'cursor',
286 \ 'pos': 'botleft',
287 \ 'border': [],
288 \ 'padding': [],
289 \ })
290 normal 51|r&
291 let winid1 = popup_create(['FIrsT', 'SEcoND'], {
292 \ 'line': 'cursor-1',
293 \ 'col': 'cursor',
294 \ 'pos': 'botright',
295 \ 'border': [],
296 \ 'padding': [],
297 \ })
298 END
299 call writefile(lines, 'XtestPopupCorners')
300 let buf = RunVimInTerminal('-S XtestPopupCorners', {'rows': 12})
301 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
302
303 " clean up
304 call StopVimInTerminal(buf)
305 call delete('XtestPopupCorners')
306endfunc
307
Bram Moolenaar8d241042019-06-12 23:40:01 +0200308func Test_popup_firstline()
309 if !CanRunVimInTerminal()
310 throw 'Skipped: cannot make screendumps'
311 endif
312 let lines =<< trim END
313 call setline(1, range(1, 20))
314 call popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], {
315 \ 'maxheight': 4,
316 \ 'firstline': 3,
317 \ })
318 END
319 call writefile(lines, 'XtestPopupFirstline')
320 let buf = RunVimInTerminal('-S XtestPopupFirstline', {'rows': 10})
321 call VerifyScreenDump(buf, 'Test_popupwin_firstline', {})
322
323 " clean up
324 call StopVimInTerminal(buf)
325 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200326
327 let winid = popup_create(['1111', '222222', '33333', '44444'], {
328 \ 'maxheight': 2,
329 \ 'firstline': 3,
330 \ })
331 call assert_equal(3, popup_getoptions(winid).firstline)
332 call popup_setoptions(winid, {'firstline': 1})
333 call assert_equal(1, popup_getoptions(winid).firstline)
334
335 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200336endfunc
337
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200338func Test_popup_drag()
339 if !CanRunVimInTerminal()
340 throw 'Skipped: cannot make screendumps'
341 endif
342 " create a popup that covers the command line
343 let lines =<< trim END
344 call setline(1, range(1, 20))
345 let winid = popup_create(['1111', '222222', '33333'], {
346 \ 'drag': 1,
347 \ 'border': [],
348 \ 'line': &lines - 4,
349 \ })
350 func Dragit()
351 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
352 endfunc
353 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
354 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2)<CR>
355 END
356 call writefile(lines, 'XtestPopupDrag')
357 let buf = RunVimInTerminal('-S XtestPopupDrag', {'rows': 10})
358 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
359
360 call term_sendkeys(buf, ":call Dragit()\<CR>")
361 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
362
363 " clean up
364 call StopVimInTerminal(buf)
365 call delete('XtestPopupDrag')
366endfunc
367
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200368func Test_popup_close_with_mouse()
369 if !CanRunVimInTerminal()
370 throw 'Skipped: cannot make screendumps'
371 endif
372 let lines =<< trim END
373 call setline(1, range(1, 20))
374 " With border, can click on X
375 let winid = popup_create('foobar', {
376 \ 'close': 'button',
377 \ 'border': [],
378 \ 'line': 1,
379 \ 'col': 1,
380 \ })
381 func CloseMsg(id, result)
382 echomsg 'Popup closed with ' .. a:result
383 endfunc
384 let winid = popup_create('notification', {
385 \ 'close': 'click',
386 \ 'line': 3,
387 \ 'col': 15,
388 \ 'callback': 'CloseMsg',
389 \ })
390 let winid = popup_create('no border here', {
391 \ 'close': 'button',
392 \ 'line': 5,
393 \ 'col': 3,
394 \ })
395 let winid = popup_create('only padding', {
396 \ 'close': 'button',
397 \ 'padding': [],
398 \ 'line': 5,
399 \ 'col': 23,
400 \ })
401 func CloseWithX()
402 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
403 endfunc
404 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
405 func CloseWithClick()
406 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
407 endfunc
408 map <silent> <F4> :call test_setmouse(3, 17)<CR>
409 END
410 call writefile(lines, 'XtestPopupClose')
411 let buf = RunVimInTerminal('-S XtestPopupClose', {'rows': 10})
412 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
413
414 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
415 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
416
417 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
418 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
419
420 " clean up
421 call StopVimInTerminal(buf)
422 call delete('XtestPopupClose')
423endfunction
424
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200425func Test_popup_with_mask()
426 if !CanRunVimInTerminal()
427 throw 'Skipped: cannot make screendumps'
428 endif
429 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200430 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200431 hi PopupColor ctermbg=lightgrey
432 let winid = popup_create([
433 \ 'some text',
434 \ 'another line',
435 \], {
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200436 \ 'line': 1,
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200437 \ 'col': 10,
Bram Moolenaard529ba52019-07-02 23:13:53 +0200438 \ 'wrap': 0,
439 \ 'fixed': 1,
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200440 \ 'zindex': 90,
441 \ 'padding': [],
442 \ 'highlight': 'PopupColor',
443 \ 'mask': [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
444 call popup_create([
445 \ 'xxxxxxxxx',
446 \ 'yyyyyyyyy',
447 \], {
448 \ 'line': 3,
449 \ 'col': 18,
450 \ 'zindex': 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200451 let winidb = popup_create([
452 \ 'just one line',
453 \], {
454 \ 'line': 7,
455 \ 'col': 10,
456 \ 'wrap': 0,
457 \ 'fixed': 1,
458 \ 'close': 'button',
459 \ 'zindex': 90,
460 \ 'padding': [],
461 \ 'border': [],
462 \ 'mask': [[1,2,1,1], [-5,-1,4,4], [7,9,2,3], [3,5,5,5],[-7,-4,5,5]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200463 END
464 call writefile(lines, 'XtestPopupMask')
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200465 let buf = RunVimInTerminal('-S XtestPopupMask', {'rows': 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200466 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
467
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200468 call term_sendkeys(buf, ":call popup_move(winid, {'col': 11, 'line': 2})\<CR>")
469 call term_sendkeys(buf, ":call popup_move(winidb, {'col': 12})\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200470 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
471
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200472 call term_sendkeys(buf, ":call popup_move(winid, {'col': 65, 'line': 2})\<CR>")
473 call term_sendkeys(buf, ":call popup_move(winidb, {'col': 63})\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200474 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
475
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200476 call term_sendkeys(buf, ":call popup_move(winid, {'pos': 'topright', 'col': 12, 'line': 2})\<CR>")
477 call term_sendkeys(buf, ":call popup_move(winidb, {'pos': 'topright', 'col': 12})\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200478 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
479
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200480 " clean up
481 call StopVimInTerminal(buf)
482 call delete('XtestPopupMask')
483endfunc
484
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200485func Test_popup_select()
486 if !CanRunVimInTerminal()
487 throw 'Skipped: cannot make screendumps'
488 endif
Bram Moolenaar650a6372019-06-15 00:29:33 +0200489 if !has('clipboard')
490 throw 'Skipped: clipboard feature missing'
491 endif
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200492 " create a popup with some text to be selected
493 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200494 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200495 call setline(1, range(1, 20))
496 let winid = popup_create(['the word', 'some more', 'several words here'], {
497 \ 'drag': 1,
498 \ 'border': [],
499 \ 'line': 3,
500 \ 'col': 10,
501 \ })
502 func Select1()
503 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
504 endfunc
505 map <silent> <F3> :call test_setmouse(4, 15)<CR>
506 map <silent> <F4> :call test_setmouse(6, 23)<CR>
507 END
508 call writefile(lines, 'XtestPopupSelect')
509 let buf = RunVimInTerminal('-S XtestPopupSelect', {'rows': 10})
510 call term_sendkeys(buf, ":call Select1()\<CR>")
511 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
512
513 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
514 call term_sendkeys(buf, "\"*p")
515 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
516
517 " clean up
518 call StopVimInTerminal(buf)
519 call delete('XtestPopupSelect')
520endfunc
521
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200522func Test_popup_in_tab()
523 " default popup is local to tab, not visible when in other tab
524 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200525 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200526 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200527 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200528 tabnew
529 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200530 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200531 quit
532 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200533
534 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200535 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200536 " buffer is gone now
537 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200538
539 " global popup is visible in any tab
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200540 let winid = popup_create("text", {'tabpage': -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200541 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200542 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200543 tabnew
544 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200545 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200546 quit
547 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200548 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200549
550 " create popup in other tab
551 tabnew
552 let winid = popup_create("text", {'tabpage': 1})
553 call assert_equal(0, popup_getpos(winid).visible)
554 call assert_equal(1, popup_getoptions(winid).tabpage)
555 quit
556 call assert_equal(1, popup_getpos(winid).visible)
557 call assert_equal(0, popup_getoptions(winid).tabpage)
558 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200559endfunc
560
561func Test_popup_valid_arguments()
562 " Zero value is like the property wasn't there
563 let winid = popup_create("text", {"col": 0})
564 let pos = popup_getpos(winid)
565 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200566 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200567
568 " using cursor column has minimum value of 1
569 let winid = popup_create("text", {"col": 'cursor-100'})
570 let pos = popup_getpos(winid)
571 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200572 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200573
574 " center
575 let winid = popup_create("text", {"pos": 'center'})
576 let pos = popup_getpos(winid)
577 let around = (&columns - pos.width) / 2
578 call assert_inrange(around - 1, around + 1, pos.col)
579 let around = (&lines - pos.height) / 2
580 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200581 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200582endfunc
583
584func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200585 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200586 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200587 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200588 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200589
590 call assert_fails('call popup_create("text", {"col": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200591 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200592 call assert_fails('call popup_create("text", {"col": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200593 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200594 call assert_fails('call popup_create("text", {"col": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200595 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200596 call assert_fails('call popup_create("text", {"col": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200597 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200598
599 call assert_fails('call popup_create("text", {"line": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200600 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200601 call assert_fails('call popup_create("text", {"line": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200602 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200603 call assert_fails('call popup_create("text", {"line": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200604 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200605 call assert_fails('call popup_create("text", {"line": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200606 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200607
608 call assert_fails('call popup_create("text", {"pos": "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200609 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200610 call assert_fails('call popup_create("text", {"padding": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200611 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200612 call assert_fails('call popup_create("text", {"border": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200613 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200614 call assert_fails('call popup_create("text", {"borderhighlight": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200615 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200616 call assert_fails('call popup_create("text", {"borderchars": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200617 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200618
619 call assert_fails('call popup_create([{"text": "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200620 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200621 call assert_fails('call popup_create([{"text": "text", "props": "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200622 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200623 call assert_fails('call popup_create([{"text": "text", "props": ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200624 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200625endfunc
626
Bram Moolenaareea16992019-05-31 17:34:48 +0200627func Test_win_execute_closing_curwin()
628 split
629 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200630 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200631 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200632endfunc
633
634func Test_win_execute_not_allowed()
635 let winid = popup_create('some text', {})
636 call assert_fails('call win_execute(winid, "split")', 'E994:')
637 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
638 call assert_fails('call win_execute(winid, "close")', 'E994:')
639 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200640 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200641 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
642 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
643 call assert_fails('call win_execute(winid, "next")', 'E994:')
644 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
645 call assert_fails('call win_execute(winid, "buf")', 'E994:')
646 call assert_fails('call win_execute(winid, "edit")', 'E994:')
647 call assert_fails('call win_execute(winid, "enew")', 'E994:')
648 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
649 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
650 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
651 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200652 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200653endfunc
654
Bram Moolenaar402502d2019-05-30 22:07:36 +0200655func Test_popup_with_wrap()
656 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200657 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200658 endif
659 let lines =<< trim END
660 call setline(1, range(1, 100))
661 let winid = popup_create(
662 \ 'a long line that wont fit',
663 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
664 END
665 call writefile(lines, 'XtestPopup')
666 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
667 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
668
669 " clean up
670 call StopVimInTerminal(buf)
671 call delete('XtestPopup')
672endfunc
673
674func Test_popup_without_wrap()
675 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200676 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200677 endif
678 let lines =<< trim END
679 call setline(1, range(1, 100))
680 let winid = popup_create(
681 \ 'a long line that wont fit',
682 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
683 END
684 call writefile(lines, 'XtestPopup')
685 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
686 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
687
688 " clean up
689 call StopVimInTerminal(buf)
690 call delete('XtestPopup')
691endfunc
692
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200693func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200694 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +0200695 throw 'Skipped: timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200696 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200697 topleft vnew
698 call setline(1, 'hello')
699
700 call popup_create('world', {
701 \ 'line': 1,
702 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200703 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200704 \ 'time': 500,
705 \})
706 redraw
707 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
708 call assert_equal('world', line)
709
710 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200711 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200712 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
713 call assert_equal('hello', line)
714
715 call popup_create('on the command line', {
716 \ 'line': &lines,
717 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200718 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200719 \ 'time': 500,
720 \})
721 redraw
722 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
723 call assert_match('.*on the command line.*', line)
724
725 sleep 700m
726 redraw
727 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
728 call assert_notmatch('.*on the command line.*', line)
729
730 bwipe!
731endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200732
733func Test_popup_hide()
734 topleft vnew
735 call setline(1, 'hello')
736
737 let winid = popup_create('world', {
738 \ 'line': 1,
739 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200740 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200741 \})
742 redraw
743 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
744 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200745 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200746 " buffer is still listed and active
747 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200748
749 call popup_hide(winid)
750 redraw
751 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
752 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200753 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200754 " buffer is still listed but hidden
755 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200756
757 call popup_show(winid)
758 redraw
759 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
760 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200761 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200762
763
764 call popup_close(winid)
765 redraw
766 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
767 call assert_equal('hello', line)
768
769 " error is given for existing non-popup window
770 call assert_fails('call popup_hide(win_getid())', 'E993:')
771
772 " no error non-existing window
773 call popup_hide(1234234)
774 call popup_show(41234234)
775
776 bwipe!
777endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200778
779func Test_popup_move()
780 topleft vnew
781 call setline(1, 'hello')
782
783 let winid = popup_create('world', {
784 \ 'line': 1,
785 \ 'col': 1,
786 \ 'minwidth': 20,
787 \})
788 redraw
789 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
790 call assert_equal('world ', line)
791
792 call popup_move(winid, {'line': 2, 'col': 2})
793 redraw
794 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
795 call assert_equal('hello ', line)
796 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
797 call assert_equal('~world', line)
798
799 call popup_move(winid, {'line': 1})
800 redraw
801 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
802 call assert_equal('hworld', line)
803
804 call popup_close(winid)
805
806 bwipe!
807endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200808
Bram Moolenaar402502d2019-05-30 22:07:36 +0200809func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200810 let winid = popup_create('hello', {
811 \ 'line': 2,
812 \ 'col': 3,
813 \ 'minwidth': 10,
814 \ 'minheight': 11,
815 \})
816 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200817 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200818 call assert_equal(2, res.line)
819 call assert_equal(3, res.col)
820 call assert_equal(10, res.width)
821 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200822 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200823
824 call popup_close(winid)
825endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200826
827func Test_popup_width_longest()
828 let tests = [
829 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
830 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
831 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
832 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
833 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
834 \ ]
835
836 for test in tests
837 let winid = popup_create(test[0], {'line': 2, 'col': 3})
838 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200839 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200840 call assert_equal(test[1], position.width)
841 call popup_close(winid)
842 endfor
843endfunc
844
845func Test_popup_wraps()
846 let tests = [
847 \ ['nowrap', 6, 1],
848 \ ['a line that wraps once', 12, 2],
849 \ ['a line that wraps two times', 12, 3],
850 \ ]
851 for test in tests
852 let winid = popup_create(test[0],
853 \ {'line': 2, 'col': 3, 'maxwidth': 12})
854 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200855 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200856 call assert_equal(test[1], position.width)
857 call assert_equal(test[2], position.height)
858
859 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200860 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200861 endfor
862endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200863
864func Test_popup_getoptions()
865 let winid = popup_create('hello', {
866 \ 'line': 2,
867 \ 'col': 3,
868 \ 'minwidth': 10,
869 \ 'minheight': 11,
870 \ 'maxwidth': 20,
871 \ 'maxheight': 21,
872 \ 'zindex': 100,
873 \ 'time': 5000,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200874 \ 'fixed': 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200875 \})
876 redraw
877 let res = popup_getoptions(winid)
878 call assert_equal(2, res.line)
879 call assert_equal(3, res.col)
880 call assert_equal(10, res.minwidth)
881 call assert_equal(11, res.minheight)
882 call assert_equal(20, res.maxwidth)
883 call assert_equal(21, res.maxheight)
884 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200885 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200886 if has('timers')
887 call assert_equal(5000, res.time)
888 endif
889 call popup_close(winid)
890
891 let winid = popup_create('hello', {})
892 redraw
893 let res = popup_getoptions(winid)
894 call assert_equal(0, res.line)
895 call assert_equal(0, res.col)
896 call assert_equal(0, res.minwidth)
897 call assert_equal(0, res.minheight)
898 call assert_equal(0, res.maxwidth)
899 call assert_equal(0, res.maxheight)
900 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200901 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200902 if has('timers')
903 call assert_equal(0, res.time)
904 endif
905 call popup_close(winid)
906 call assert_equal({}, popup_getoptions(winid))
907endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200908
909func Test_popup_option_values()
910 new
911 " window-local
912 setlocal number
913 setlocal nowrap
914 " buffer-local
915 setlocal omnifunc=Something
916 " global/buffer-local
917 setlocal path=/there
918 " global/window-local
919 setlocal scrolloff=9
920
921 let winid = popup_create('hello', {})
922 call assert_equal(0, getwinvar(winid, '&number'))
923 call assert_equal(1, getwinvar(winid, '&wrap'))
924 call assert_equal('', getwinvar(winid, '&omnifunc'))
925 call assert_equal(&g:path, getwinvar(winid, '&path'))
926 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
927
928 call popup_close(winid)
929 bwipe
930endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200931
932func Test_popup_atcursor()
933 topleft vnew
934 call setline(1, [
935 \ 'xxxxxxxxxxxxxxxxx',
936 \ 'xxxxxxxxxxxxxxxxx',
937 \ 'xxxxxxxxxxxxxxxxx',
938 \])
939
940 call cursor(2, 2)
941 redraw
942 let winid = popup_atcursor('vim', {})
943 redraw
944 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
945 call assert_equal('xvimxxxxxxxxxxxxx', line)
946 call popup_close(winid)
947
948 call cursor(3, 4)
949 redraw
950 let winid = popup_atcursor('vim', {})
951 redraw
952 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
953 call assert_equal('xxxvimxxxxxxxxxxx', line)
954 call popup_close(winid)
955
956 call cursor(1, 1)
957 redraw
958 let winid = popup_create('vim', {
959 \ 'line': 'cursor+2',
960 \ 'col': 'cursor+1',
961 \})
962 redraw
963 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
964 call assert_equal('xvimxxxxxxxxxxxxx', line)
965 call popup_close(winid)
966
967 call cursor(3, 3)
968 redraw
969 let winid = popup_create('vim', {
970 \ 'line': 'cursor-2',
971 \ 'col': 'cursor-1',
972 \})
973 redraw
974 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
975 call assert_equal('xvimxxxxxxxxxxxxx', line)
976 call popup_close(winid)
977
Bram Moolenaar402502d2019-05-30 22:07:36 +0200978 " just enough room above
979 call cursor(3, 3)
980 redraw
981 let winid = popup_atcursor(['vim', 'is great'], {})
982 redraw
983 let pos = popup_getpos(winid)
984 call assert_equal(1, pos.line)
985 call popup_close(winid)
986
987 " not enough room above, popup goes below the cursor
988 call cursor(3, 3)
989 redraw
990 let winid = popup_atcursor(['vim', 'is', 'great'], {})
991 redraw
992 let pos = popup_getpos(winid)
993 call assert_equal(4, pos.line)
994 call popup_close(winid)
995
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200996 " cursor in first line, popup in line 2
997 call cursor(1, 1)
998 redraw
999 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1000 redraw
1001 let pos = popup_getpos(winid)
1002 call assert_equal(2, pos.line)
1003 call popup_close(winid)
1004
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001005 bwipe!
1006endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001007
1008func Test_popup_filter()
1009 new
1010 call setline(1, 'some text')
1011
1012 func MyPopupFilter(winid, c)
1013 if a:c == 'e'
1014 let g:eaten = 'e'
1015 return 1
1016 endif
1017 if a:c == '0'
1018 let g:ignored = '0'
1019 return 0
1020 endif
1021 if a:c == 'x'
1022 call popup_close(a:winid)
1023 return 1
1024 endif
1025 return 0
1026 endfunc
1027
1028 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
1029 redraw
1030
1031 " e is consumed by the filter
1032 call feedkeys('e', 'xt')
1033 call assert_equal('e', g:eaten)
1034
1035 " 0 is ignored by the filter
1036 normal $
1037 call assert_equal(9, getcurpos()[2])
1038 call feedkeys('0', 'xt')
1039 call assert_equal('0', g:ignored)
1040 call assert_equal(1, getcurpos()[2])
1041
1042 " x closes the popup
1043 call feedkeys('x', 'xt')
1044 call assert_equal('e', g:eaten)
1045 call assert_equal(-1, winbufnr(winid))
1046
1047 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001048 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001049endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001050
Bram Moolenaara42d9452019-06-15 21:46:30 +02001051func ShowDialog(key, result)
1052 let s:cb_res = 999
1053 let winid = popup_dialog('do you want to quit (Yes/no)?', {
1054 \ 'filter': 'popup_filter_yesno',
1055 \ 'callback': 'QuitCallback',
1056 \ })
1057 redraw
1058 call feedkeys(a:key, "xt")
1059 call assert_equal(winid, s:cb_winid)
1060 call assert_equal(a:result, s:cb_res)
1061endfunc
1062
1063func Test_popup_dialog()
1064 func QuitCallback(id, res)
1065 let s:cb_winid = a:id
1066 let s:cb_res = a:res
1067 endfunc
1068
1069 let winid = ShowDialog("y", 1)
1070 let winid = ShowDialog("Y", 1)
1071 let winid = ShowDialog("n", 0)
1072 let winid = ShowDialog("N", 0)
1073 let winid = ShowDialog("x", 0)
1074 let winid = ShowDialog("X", 0)
1075 let winid = ShowDialog("\<Esc>", 0)
1076 let winid = ShowDialog("\<C-C>", -1)
1077
1078 delfunc QuitCallback
1079endfunc
1080
Bram Moolenaara730e552019-06-16 19:05:31 +02001081func ShowMenu(key, result)
1082 let s:cb_res = 999
1083 let winid = popup_menu(['one', 'two', 'something else'], {
1084 \ 'callback': 'QuitCallback',
1085 \ })
1086 redraw
1087 call feedkeys(a:key, "xt")
1088 call assert_equal(winid, s:cb_winid)
1089 call assert_equal(a:result, s:cb_res)
1090endfunc
1091
1092func Test_popup_menu()
1093 func QuitCallback(id, res)
1094 let s:cb_winid = a:id
1095 let s:cb_res = a:res
1096 endfunc
1097
1098 let winid = ShowMenu(" ", 1)
1099 let winid = ShowMenu("j \<CR>", 2)
1100 let winid = ShowMenu("JjK \<CR>", 2)
1101 let winid = ShowMenu("jjjjjj ", 3)
1102 let winid = ShowMenu("kkk ", 1)
1103 let winid = ShowMenu("x", -1)
1104 let winid = ShowMenu("X", -1)
1105 let winid = ShowMenu("\<Esc>", -1)
1106 let winid = ShowMenu("\<C-C>", -1)
1107
1108 delfunc QuitCallback
1109endfunc
1110
1111func Test_popup_menu_screenshot()
1112 if !CanRunVimInTerminal()
1113 throw 'Skipped: cannot make screendumps'
1114 endif
1115
1116 let lines =<< trim END
1117 call setline(1, range(1, 20))
1118 hi PopupSelected ctermbg=lightblue
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001119 call popup_menu(['one', 'two', 'another'], {'callback': 'MenuDone', 'title': ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001120 func MenuDone(id, res)
1121 echomsg "selected " .. a:res
1122 endfunc
1123 END
1124 call writefile(lines, 'XtestPopupMenu')
1125 let buf = RunVimInTerminal('-S XtestPopupMenu', {'rows': 10})
1126 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1127
1128 call term_sendkeys(buf, "jj")
1129 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1130
1131 call term_sendkeys(buf, " ")
1132 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1133
1134 " clean up
1135 call StopVimInTerminal(buf)
1136 call delete('XtestPopupMenu')
1137endfunc
1138
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001139func Test_popup_title()
1140 if !CanRunVimInTerminal()
1141 throw 'Skipped: cannot make screendumps'
1142 endif
1143
1144 " Create a popup without title or border, a line of padding will be added to
1145 " put the title on.
1146 let lines =<< trim END
1147 call setline(1, range(1, 20))
1148 call popup_create(['one', 'two', 'another'], {'title': 'Title String'})
1149 END
1150 call writefile(lines, 'XtestPopupTitle')
1151 let buf = RunVimInTerminal('-S XtestPopupTitle', {'rows': 10})
1152 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1153
1154 " clean up
1155 call StopVimInTerminal(buf)
1156 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001157
1158 let winid = popup_create('something', {'title': 'Some Title'})
1159 call assert_equal('Some Title', popup_getoptions(winid).title)
1160 call popup_setoptions(winid, {'title': 'Another Title'})
1161 call assert_equal('Another Title', popup_getoptions(winid).title)
1162
1163 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001164endfunc
1165
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001166func Test_popup_close_callback()
1167 func PopupDone(id, result)
1168 let g:result = a:result
1169 endfunc
1170 let winid = popup_create('something', {'callback': 'PopupDone'})
1171 redraw
1172 call popup_close(winid, 'done')
1173 call assert_equal('done', g:result)
1174endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001175
1176func Test_popup_empty()
1177 let winid = popup_create('', {'padding': [2,2,2,2]})
1178 redraw
1179 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001180 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001181 call assert_equal(5, pos.height)
1182
1183 let winid = popup_create([], {'border': []})
1184 redraw
1185 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001186 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001187 call assert_equal(3, pos.height)
1188endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001189
1190func Test_popup_never_behind()
1191 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001192 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +02001193 endif
1194 " +-----------------------------+
1195 " | | |
1196 " | | |
1197 " | | |
1198 " | line1 |
1199 " |------------line2------------|
1200 " | line3 |
1201 " | line4 |
1202 " | |
1203 " | |
1204 " +-----------------------------+
1205 let lines =<< trim END
1206 only
1207 split
1208 vsplit
1209 let info_window1 = getwininfo()[0]
1210 let line = info_window1['height']
1211 let col = info_window1['width']
1212 call popup_create(['line1', 'line2', 'line3', 'line4'], {
1213 \ 'line' : line,
1214 \ 'col' : col,
1215 \ })
1216 END
1217 call writefile(lines, 'XtestPopupBehind')
1218 let buf = RunVimInTerminal('-S XtestPopupBehind', {'rows': 10})
1219 call term_sendkeys(buf, "\<C-W>w")
1220 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1221
1222 " clean up
1223 call StopVimInTerminal(buf)
1224 call delete('XtestPopupBehind')
1225endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001226
1227func s:VerifyPosition( p, msg, line, col, width, height )
1228 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
1229 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
1230 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
1231 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
1232endfunc
1233
1234func Test_popup_position_adjust()
1235 " Anything placed past 2 cells from of the right of the screen is moved to the
1236 " left.
1237 "
1238 " When wrapping is disabled, we also shift to the left to display on the
1239 " screen, unless fixed is set.
1240
1241 " Entries for cases which don't vary based on wrapping.
1242 " Format is per tests described below
1243 let both_wrap_tests = [
1244 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
1245 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
1246 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
1247 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
1248 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
1249 \
1250 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
1251 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
1252 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
1253 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
1254 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
1255 \
1256 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1257 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1258 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1259 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1260 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
1261 \ ]
1262
1263 " these test groups are dicts with:
1264 " - comment: something to identify the group of tests by
1265 " - options: dict of options to merge with the row/col in tests
1266 " - tests: list of cases. Each one is a list with elements:
1267 " - text
1268 " - row
1269 " - col
1270 " - expected row
1271 " - expected col
1272 " - expected width
1273 " - expected height
1274 let tests = [
1275 \ {
1276 \ 'comment': 'left-aligned with wrapping',
1277 \ 'options': {
1278 \ 'wrap': 1,
1279 \ 'pos': 'botleft',
1280 \ },
1281 \ 'tests': both_wrap_tests + [
1282 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
1283 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
1284 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
1285 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
1286 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1287 \ ],
1288 \ },
1289 \ {
1290 \ 'comment': 'left aligned without wrapping',
1291 \ 'options': {
1292 \ 'wrap': 0,
1293 \ 'pos': 'botleft',
1294 \ },
1295 \ 'tests': both_wrap_tests + [
1296 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
1297 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
1298 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
1299 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
1300 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1301 \ ],
1302 \ },
1303 \ {
1304 \ 'comment': 'left aligned with fixed position',
1305 \ 'options': {
1306 \ 'wrap': 0,
1307 \ 'fixed': 1,
1308 \ 'pos': 'botleft',
1309 \ },
1310 \ 'tests': both_wrap_tests + [
1311 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1312 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1313 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1314 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1315 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1316 \ ],
1317 \ },
1318 \ ]
1319
1320 for test_group in tests
1321 for test in test_group.tests
1322 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
1323 let options = {
1324 \ 'line': line,
1325 \ 'col': col,
1326 \ }
1327 call extend( options, test_group.options )
1328
1329 let p = popup_create( text, options )
1330
1331 let msg = string( extend( options, { 'text': text } ) )
1332 call s:VerifyPosition( p, msg, e_line, e_col, e_width, e_height )
1333 call popup_close( p )
1334 endfor
1335 endfor
1336
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001337 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001338 %bwipe!
1339endfunc
1340
Bram Moolenaar3397f742019-06-02 18:40:06 +02001341func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001342 " width of screen
1343 let X = join(map(range(&columns), {->'X'}), '')
1344
1345 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1346 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
1347
1348 redraw
1349 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1350 call assert_equal(X, line)
1351
1352 call popup_close( p )
1353 redraw
1354
1355 " Same if placed on the right hand side
1356 let p = popup_create( X, { 'line': 1, 'col': &columns, 'wrap': 0 } )
1357 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
1358
1359 redraw
1360 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1361 call assert_equal(X, line)
1362
1363 call popup_close( p )
1364 redraw
1365
1366 " Extend so > window width
1367 let X .= 'x'
1368
1369 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1370 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
1371
1372 redraw
1373 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1374 call assert_equal(X[ : -2 ], line)
1375
1376 call popup_close( p )
1377 redraw
1378
1379 " Shifted then truncated (the x is not visible)
1380 let p = popup_create( X, { 'line': 1, 'col': &columns - 3, 'wrap': 0 } )
1381 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
1382
1383 redraw
1384 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1385 call assert_equal(X[ : -2 ], line)
1386
1387 call popup_close( p )
1388 redraw
1389
1390 " Not shifted, just truncated
1391 let p = popup_create( X,
1392 \ { 'line': 1, 'col': 2, 'wrap': 0, 'fixed': 1 } )
1393 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
1394
1395 redraw
1396 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1397 let e_line = ' ' . X[ 1 : -2 ]
1398 call assert_equal(e_line, line)
1399
1400 call popup_close( p )
1401 redraw
1402
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001403 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001404 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001405endfunc
1406
1407func Test_popup_moved()
1408 new
1409 call test_override('char_avail', 1)
1410 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1411
1412 exe "normal gg0/word\<CR>"
1413 let winid = popup_atcursor('text', {'moved': 'any'})
1414 redraw
1415 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001416 call assert_equal([4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001417 " trigger the check for last_cursormoved by going into insert mode
1418 call feedkeys("li\<Esc>", 'xt')
1419 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001420 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001421
1422 exe "normal gg0/word\<CR>"
1423 let winid = popup_atcursor('text', {'moved': 'word'})
1424 redraw
1425 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001426 call assert_equal([4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001427 call feedkeys("hi\<Esc>", 'xt')
1428 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001429 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001430
1431 exe "normal gg0/word\<CR>"
1432 let winid = popup_atcursor('text', {'moved': 'word'})
1433 redraw
1434 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001435 call assert_equal([4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001436 call feedkeys("li\<Esc>", 'xt')
1437 call assert_equal(1, popup_getpos(winid).visible)
1438 call feedkeys("ei\<Esc>", 'xt')
1439 call assert_equal(1, popup_getpos(winid).visible)
1440 call feedkeys("eli\<Esc>", 'xt')
1441 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001442 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001443
Bram Moolenaar17627312019-06-02 19:53:44 +02001444 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001445 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001446 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001447 redraw
1448 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001449 call assert_equal([2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001450 call feedkeys("eli\<Esc>", 'xt')
1451 call assert_equal(1, popup_getpos(winid).visible)
1452 call feedkeys("wi\<Esc>", 'xt')
1453 call assert_equal(1, popup_getpos(winid).visible)
1454 call feedkeys("Eli\<Esc>", 'xt')
1455 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001456 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001457
1458 exe "normal gg0/word\<CR>"
1459 let winid = popup_atcursor('text', {'moved': [5, 10]})
1460 redraw
1461 call assert_equal(1, popup_getpos(winid).visible)
1462 call feedkeys("eli\<Esc>", 'xt')
1463 call feedkeys("ei\<Esc>", 'xt')
1464 call assert_equal(1, popup_getpos(winid).visible)
1465 call feedkeys("eli\<Esc>", 'xt')
1466 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001467 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001468
1469 bwipe!
1470 call test_override('ALL', 0)
1471endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001472
1473func Test_notifications()
1474 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02001475 throw 'Skipped: timer feature not supported'
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001476 endif
1477 if !CanRunVimInTerminal()
1478 throw 'Skipped: cannot make screendumps'
1479 endif
1480
1481 call writefile([
1482 \ "call setline(1, range(1, 20))",
1483 \ "hi Notification ctermbg=lightblue",
1484 \ "call popup_notification('first notification', {})",
1485 \], 'XtestNotifications')
1486 let buf = RunVimInTerminal('-S XtestNotifications', {'rows': 10})
1487 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1488
1489 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001490 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1491 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001492 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1493
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001494 " clean up
1495 call StopVimInTerminal(buf)
1496 call delete('XtestNotifications')
1497endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001498
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001499func Test_popup_scrollbar()
1500 if !CanRunVimInTerminal()
1501 throw 'Skipped: cannot make screendumps'
1502 endif
1503
1504 let lines =<< trim END
1505 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001506 hi ScrollThumb ctermbg=blue
1507 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001508 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
1509 \ 'six', 'seven', 'eight', 'nine'], {
1510 \ 'minwidth': 8,
1511 \ 'maxheight': 4,
1512 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001513 func ScrollUp()
1514 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1515 endfunc
1516 func ScrollDown()
1517 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1518 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001519 func ClickTop()
1520 call feedkeys("\<F4>\<LeftMouse>", "xt")
1521 endfunc
1522 func ClickBot()
1523 call feedkeys("\<F5>\<LeftMouse>", "xt")
1524 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001525 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001526 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1527 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001528 END
1529 call writefile(lines, 'XtestPopupScroll')
1530 let buf = RunVimInTerminal('-S XtestPopupScroll', {'rows': 10})
1531 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1532
1533 call term_sendkeys(buf, ":call popup_setoptions(winid, {'firstline': 2})\<CR>")
1534 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1535
1536 call term_sendkeys(buf, ":call popup_setoptions(winid, {'firstline': 6})\<CR>")
1537 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1538
1539 call term_sendkeys(buf, ":call popup_setoptions(winid, {'firstline': 9})\<CR>")
1540 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1541
Bram Moolenaar8da41812019-06-26 18:04:54 +02001542 call term_sendkeys(buf, ":call popup_setoptions(winid, {'scrollbarhighlight': 'ScrollBar', 'thumbhighlight': 'ScrollThumb'})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001543 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1544 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1545
1546 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1547 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1548
1549 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001550 " wait a bit, otherwise it fails sometimes (double click recognized?)
1551 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001552 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1553 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1554
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001555 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1556 sleep 100m
1557 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1558 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1559
1560 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1561 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1562
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001563 " clean up
1564 call StopVimInTerminal(buf)
1565 call delete('XtestPopupScroll')
1566endfunc
1567
Bram Moolenaar437a7462019-07-05 20:17:22 +02001568func Test_popup_fitting_scrollbar()
1569 " this was causing a crash, divide by zero
1570 let winid = popup_create([
1571 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], {
1572 \ 'scrollbar': 1,
1573 \ 'maxwidth': 10,
1574 \ 'maxheight': 5,
1575 \ 'firstline': 2})
1576 redraw
1577 call popup_clear()
1578endfunc
1579
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001580func Test_popup_settext()
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001581 if !CanRunVimInTerminal()
1582 throw 'Skipped: cannot make screendumps'
1583 endif
1584
1585 let lines =<< trim END
1586 let opts = {'wrap': 0}
1587 let p = popup_create('test', opts)
1588 call popup_settext(p, 'this is a text')
1589 END
1590
1591 call writefile( lines, 'XtestPopupSetText' )
1592 let buf = RunVimInTerminal('-S XtestPopupSetText', {'rows': 10})
1593 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1594
1595 " Setting to empty string clears it
1596 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1597 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1598
1599 " Setting a list
1600 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1601 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1602
1603 " Shrinking with a list
1604 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1605 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1606
1607 " Growing with a list
1608 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1609 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1610
1611 " Empty list clears
1612 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1613 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1614
1615 " Dicts
1616 call term_sendkeys(buf, ":call popup_settext(p, [{'text': 'aaaa'}, {'text': 'bbbb'}, {'text': 'cccc'}])\<CR>")
1617 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1618
1619 " clean up
1620 call StopVimInTerminal(buf)
1621 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001622endfunc
1623
1624func Test_popup_hidden()
1625 new
1626
1627 let winid = popup_atcursor('text', {'hidden': 1})
1628 redraw
1629 call assert_equal(0, popup_getpos(winid).visible)
1630 call popup_close(winid)
1631
1632 let winid = popup_create('text', {'hidden': 1})
1633 redraw
1634 call assert_equal(0, popup_getpos(winid).visible)
1635 call popup_close(winid)
1636
1637 func QuitCallback(id, res)
1638 let s:cb_winid = a:id
1639 let s:cb_res = a:res
1640 endfunc
1641 let winid = popup_dialog('make a choice', {'hidden': 1,
1642 \ 'filter': 'popup_filter_yesno',
1643 \ 'callback': 'QuitCallback',
1644 \ })
1645 redraw
1646 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001647 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1648 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001649 exe "normal anot used by filter\<Esc>"
1650 call assert_equal('not used by filter', getline(1))
1651
1652 call popup_show(winid)
1653 call feedkeys('y', "xt")
1654 call assert_equal(1, s:cb_res)
1655
1656 bwipe!
1657 delfunc QuitCallback
1658endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001659
1660" Test options not checked elsewhere
1661func Test_set_get_options()
1662 let winid = popup_create('some text', {'highlight': 'Beautiful'})
1663 let options = popup_getoptions(winid)
1664 call assert_equal(1, options.wrap)
1665 call assert_equal(0, options.drag)
1666 call assert_equal('Beautiful', options.highlight)
1667
1668 call popup_setoptions(winid, {'wrap': 0, 'drag': 1, 'highlight': 'Another'})
1669 let options = popup_getoptions(winid)
1670 call assert_equal(0, options.wrap)
1671 call assert_equal(1, options.drag)
1672 call assert_equal('Another', options.highlight)
1673
1674 call popup_close(winid)
1675endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001676
1677func Test_popupwin_garbage_collect()
1678 func MyPopupFilter(x, winid, c)
1679 " NOP
1680 endfunc
1681
1682 let winid = popup_create('something', {'filter': function('MyPopupFilter', [{}])})
1683 call test_garbagecollect_now()
1684 redraw
1685 " Must not crach caused by invalid memory access
1686 call feedkeys('j', 'xt')
1687 call assert_true(v:true)
1688
1689 call popup_close(winid)
1690 delfunc MyPopupFilter
1691endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001692
1693func Test_popupwin_with_buffer()
1694 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1695 let buf = bufadd('XsomeFile')
1696 call assert_equal(0, bufloaded(buf))
1697 let winid = popup_create(buf, {})
1698 call assert_notequal(0, winid)
1699 let pos = popup_getpos(winid)
1700 call assert_equal(2, pos.height)
1701 call assert_equal(1, bufloaded(buf))
1702 call popup_close(winid)
1703 call assert_equal({}, popup_getpos(winid))
1704 call assert_equal(1, bufloaded(buf))
1705 exe 'bwipe! ' .. buf
Bram Moolenaar7866b872019-07-01 22:21:01 +02001706
1707 edit test_popupwin.vim
1708 let winid = popup_create(bufnr(''), {})
1709 redraw
1710 call popup_close(winid)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001711endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02001712
1713func Test_popupwin_width()
1714 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), {
1715 \ 'maxwidth': 40,
1716 \ 'maxheight': 10,
1717 \ })
1718 for top in range(1, 20)
1719 call popup_setoptions(winid, {'firstline': top})
1720 redraw
1721 call assert_equal(19, popup_getpos(winid).width)
1722 endfor
1723 call popup_clear()
1724endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02001725
1726func Test_popupwin_buf_close()
1727 let buf = bufadd('Xtestbuf')
1728 call bufload(buf)
1729 call setbufline(buf, 1, ['just', 'some', 'lines'])
1730 let winid = popup_create(buf, {})
1731 redraw
1732 call assert_equal(3, popup_getpos(winid).height)
1733 let bufinfo = getbufinfo(buf)[0]
1734 call assert_equal(1, bufinfo.changed)
1735 call assert_equal(0, bufinfo.hidden)
1736 call assert_equal(0, bufinfo.listed)
1737 call assert_equal(1, bufinfo.loaded)
1738 call assert_equal([], bufinfo.windows)
1739 call assert_equal([winid], bufinfo.popups)
1740
1741 call popup_close(winid)
1742 call assert_equal({}, popup_getpos(winid))
1743 let bufinfo = getbufinfo(buf)[0]
1744 call assert_equal(1, bufinfo.changed)
1745 call assert_equal(1, bufinfo.hidden)
1746 call assert_equal(0, bufinfo.listed)
1747 call assert_equal(1, bufinfo.loaded)
1748 call assert_equal([], bufinfo.windows)
1749 call assert_equal([], bufinfo.popups)
1750 exe 'bwipe! ' .. buf
1751endfunc