blob: 13b0f1cc55023078ed64da1180b5d41cbf7f7511 [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 Moolenaard529ba52019-07-02 23:13:53 +0200430 call setline(1, repeat([join(range(1, 42), '')], 10))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200431 hi PopupColor ctermbg=lightgrey
432 let winid = popup_create([
433 \ 'some text',
434 \ 'another line',
435 \], {
436 \ 'line': 2,
437 \ '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})
451 END
452 call writefile(lines, 'XtestPopupMask')
453 let buf = RunVimInTerminal('-S XtestPopupMask', {'rows': 10})
454 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
455
456 call term_sendkeys(buf, ":call popup_move(winid, {'col': 11, 'line': 3})\<CR>")
457 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
458
Bram Moolenaard529ba52019-07-02 23:13:53 +0200459 call term_sendkeys(buf, ":call popup_move(winid, {'col': 65, 'line': 3})\<CR>")
460 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
461
462 call term_sendkeys(buf, ":call popup_move(winid, {'pos': 'topright', 'col': 12, 'line': 3})\<CR>")
463 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
464
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200465 " clean up
466 call StopVimInTerminal(buf)
467 call delete('XtestPopupMask')
468endfunc
469
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200470func Test_popup_select()
471 if !CanRunVimInTerminal()
472 throw 'Skipped: cannot make screendumps'
473 endif
Bram Moolenaar650a6372019-06-15 00:29:33 +0200474 if !has('clipboard')
475 throw 'Skipped: clipboard feature missing'
476 endif
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200477 " create a popup with some text to be selected
478 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200479 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200480 call setline(1, range(1, 20))
481 let winid = popup_create(['the word', 'some more', 'several words here'], {
482 \ 'drag': 1,
483 \ 'border': [],
484 \ 'line': 3,
485 \ 'col': 10,
486 \ })
487 func Select1()
488 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
489 endfunc
490 map <silent> <F3> :call test_setmouse(4, 15)<CR>
491 map <silent> <F4> :call test_setmouse(6, 23)<CR>
492 END
493 call writefile(lines, 'XtestPopupSelect')
494 let buf = RunVimInTerminal('-S XtestPopupSelect', {'rows': 10})
495 call term_sendkeys(buf, ":call Select1()\<CR>")
496 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
497
498 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
499 call term_sendkeys(buf, "\"*p")
500 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
501
502 " clean up
503 call StopVimInTerminal(buf)
504 call delete('XtestPopupSelect')
505endfunc
506
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200507func Test_popup_in_tab()
508 " default popup is local to tab, not visible when in other tab
509 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200510 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200511 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200512 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200513 tabnew
514 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200515 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200516 quit
517 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200518
519 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200520 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200521 " buffer is gone now
522 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200523
524 " global popup is visible in any tab
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200525 let winid = popup_create("text", {'tabpage': -1})
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(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200528 tabnew
529 call assert_equal(1, 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 Moolenaar3ff5f0f2019-06-10 13:11:22 +0200533 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200534
535 " create popup in other tab
536 tabnew
537 let winid = popup_create("text", {'tabpage': 1})
538 call assert_equal(0, popup_getpos(winid).visible)
539 call assert_equal(1, popup_getoptions(winid).tabpage)
540 quit
541 call assert_equal(1, popup_getpos(winid).visible)
542 call assert_equal(0, popup_getoptions(winid).tabpage)
543 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200544endfunc
545
546func Test_popup_valid_arguments()
547 " Zero value is like the property wasn't there
548 let winid = popup_create("text", {"col": 0})
549 let pos = popup_getpos(winid)
550 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200551 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200552
553 " using cursor column has minimum value of 1
554 let winid = popup_create("text", {"col": 'cursor-100'})
555 let pos = popup_getpos(winid)
556 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200557 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200558
559 " center
560 let winid = popup_create("text", {"pos": 'center'})
561 let pos = popup_getpos(winid)
562 let around = (&columns - pos.width) / 2
563 call assert_inrange(around - 1, around + 1, pos.col)
564 let around = (&lines - pos.height) / 2
565 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200566 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200567endfunc
568
569func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200570 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200571 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200572 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200573 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200574
575 call assert_fails('call popup_create("text", {"col": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200576 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200577 call assert_fails('call popup_create("text", {"col": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200578 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200579 call assert_fails('call popup_create("text", {"col": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200580 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200581 call assert_fails('call popup_create("text", {"col": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200582 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200583
584 call assert_fails('call popup_create("text", {"line": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200585 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200586 call assert_fails('call popup_create("text", {"line": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200587 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200588 call assert_fails('call popup_create("text", {"line": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200589 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200590 call assert_fails('call popup_create("text", {"line": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200591 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200592
593 call assert_fails('call popup_create("text", {"pos": "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200594 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200595 call assert_fails('call popup_create("text", {"padding": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200596 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200597 call assert_fails('call popup_create("text", {"border": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200598 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200599 call assert_fails('call popup_create("text", {"borderhighlight": "none"})', 'E714:')
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", {"borderchars": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200602 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200603
604 call assert_fails('call popup_create([{"text": "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200605 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200606 call assert_fails('call popup_create([{"text": "text", "props": "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200607 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200608 call assert_fails('call popup_create([{"text": "text", "props": ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200609 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200610endfunc
611
Bram Moolenaareea16992019-05-31 17:34:48 +0200612func Test_win_execute_closing_curwin()
613 split
614 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200615 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200616 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200617endfunc
618
619func Test_win_execute_not_allowed()
620 let winid = popup_create('some text', {})
621 call assert_fails('call win_execute(winid, "split")', 'E994:')
622 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
623 call assert_fails('call win_execute(winid, "close")', 'E994:')
624 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200625 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200626 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
627 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
628 call assert_fails('call win_execute(winid, "next")', 'E994:')
629 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
630 call assert_fails('call win_execute(winid, "buf")', 'E994:')
631 call assert_fails('call win_execute(winid, "edit")', 'E994:')
632 call assert_fails('call win_execute(winid, "enew")', 'E994:')
633 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
634 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
635 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
636 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200637 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200638endfunc
639
Bram Moolenaar402502d2019-05-30 22:07:36 +0200640func Test_popup_with_wrap()
641 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200642 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200643 endif
644 let lines =<< trim END
645 call setline(1, range(1, 100))
646 let winid = popup_create(
647 \ 'a long line that wont fit',
648 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
649 END
650 call writefile(lines, 'XtestPopup')
651 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
652 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
653
654 " clean up
655 call StopVimInTerminal(buf)
656 call delete('XtestPopup')
657endfunc
658
659func Test_popup_without_wrap()
660 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200661 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200662 endif
663 let lines =<< trim END
664 call setline(1, range(1, 100))
665 let winid = popup_create(
666 \ 'a long line that wont fit',
667 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
668 END
669 call writefile(lines, 'XtestPopup')
670 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
671 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
672
673 " clean up
674 call StopVimInTerminal(buf)
675 call delete('XtestPopup')
676endfunc
677
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200678func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200679 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +0200680 throw 'Skipped: timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200681 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200682 topleft vnew
683 call setline(1, 'hello')
684
685 call popup_create('world', {
686 \ 'line': 1,
687 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200688 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200689 \ 'time': 500,
690 \})
691 redraw
692 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
693 call assert_equal('world', line)
694
695 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200696 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200697 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
698 call assert_equal('hello', line)
699
700 call popup_create('on the command line', {
701 \ 'line': &lines,
702 \ 'col': 10,
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, 30), 'screenstring(&lines, v:val)'), '')
708 call assert_match('.*on the command line.*', line)
709
710 sleep 700m
711 redraw
712 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
713 call assert_notmatch('.*on the command line.*', line)
714
715 bwipe!
716endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200717
718func Test_popup_hide()
719 topleft vnew
720 call setline(1, 'hello')
721
722 let winid = popup_create('world', {
723 \ 'line': 1,
724 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200725 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200726 \})
727 redraw
728 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
729 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200730 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200731 " buffer is still listed and active
732 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200733
734 call popup_hide(winid)
735 redraw
736 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
737 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200738 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200739 " buffer is still listed but hidden
740 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200741
742 call popup_show(winid)
743 redraw
744 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
745 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200746 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200747
748
749 call popup_close(winid)
750 redraw
751 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
752 call assert_equal('hello', line)
753
754 " error is given for existing non-popup window
755 call assert_fails('call popup_hide(win_getid())', 'E993:')
756
757 " no error non-existing window
758 call popup_hide(1234234)
759 call popup_show(41234234)
760
761 bwipe!
762endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200763
764func Test_popup_move()
765 topleft vnew
766 call setline(1, 'hello')
767
768 let winid = popup_create('world', {
769 \ 'line': 1,
770 \ 'col': 1,
771 \ 'minwidth': 20,
772 \})
773 redraw
774 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
775 call assert_equal('world ', line)
776
777 call popup_move(winid, {'line': 2, 'col': 2})
778 redraw
779 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
780 call assert_equal('hello ', line)
781 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
782 call assert_equal('~world', line)
783
784 call popup_move(winid, {'line': 1})
785 redraw
786 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
787 call assert_equal('hworld', line)
788
789 call popup_close(winid)
790
791 bwipe!
792endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200793
Bram Moolenaar402502d2019-05-30 22:07:36 +0200794func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200795 let winid = popup_create('hello', {
796 \ 'line': 2,
797 \ 'col': 3,
798 \ 'minwidth': 10,
799 \ 'minheight': 11,
800 \})
801 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200802 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200803 call assert_equal(2, res.line)
804 call assert_equal(3, res.col)
805 call assert_equal(10, res.width)
806 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200807 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200808
809 call popup_close(winid)
810endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200811
812func Test_popup_width_longest()
813 let tests = [
814 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
815 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
816 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
817 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
818 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
819 \ ]
820
821 for test in tests
822 let winid = popup_create(test[0], {'line': 2, 'col': 3})
823 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200824 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200825 call assert_equal(test[1], position.width)
826 call popup_close(winid)
827 endfor
828endfunc
829
830func Test_popup_wraps()
831 let tests = [
832 \ ['nowrap', 6, 1],
833 \ ['a line that wraps once', 12, 2],
834 \ ['a line that wraps two times', 12, 3],
835 \ ]
836 for test in tests
837 let winid = popup_create(test[0],
838 \ {'line': 2, 'col': 3, 'maxwidth': 12})
839 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200840 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200841 call assert_equal(test[1], position.width)
842 call assert_equal(test[2], position.height)
843
844 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200845 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200846 endfor
847endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200848
849func Test_popup_getoptions()
850 let winid = popup_create('hello', {
851 \ 'line': 2,
852 \ 'col': 3,
853 \ 'minwidth': 10,
854 \ 'minheight': 11,
855 \ 'maxwidth': 20,
856 \ 'maxheight': 21,
857 \ 'zindex': 100,
858 \ 'time': 5000,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200859 \ 'fixed': 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200860 \})
861 redraw
862 let res = popup_getoptions(winid)
863 call assert_equal(2, res.line)
864 call assert_equal(3, res.col)
865 call assert_equal(10, res.minwidth)
866 call assert_equal(11, res.minheight)
867 call assert_equal(20, res.maxwidth)
868 call assert_equal(21, res.maxheight)
869 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200870 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200871 if has('timers')
872 call assert_equal(5000, res.time)
873 endif
874 call popup_close(winid)
875
876 let winid = popup_create('hello', {})
877 redraw
878 let res = popup_getoptions(winid)
879 call assert_equal(0, res.line)
880 call assert_equal(0, res.col)
881 call assert_equal(0, res.minwidth)
882 call assert_equal(0, res.minheight)
883 call assert_equal(0, res.maxwidth)
884 call assert_equal(0, res.maxheight)
885 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200886 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200887 if has('timers')
888 call assert_equal(0, res.time)
889 endif
890 call popup_close(winid)
891 call assert_equal({}, popup_getoptions(winid))
892endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200893
894func Test_popup_option_values()
895 new
896 " window-local
897 setlocal number
898 setlocal nowrap
899 " buffer-local
900 setlocal omnifunc=Something
901 " global/buffer-local
902 setlocal path=/there
903 " global/window-local
904 setlocal scrolloff=9
905
906 let winid = popup_create('hello', {})
907 call assert_equal(0, getwinvar(winid, '&number'))
908 call assert_equal(1, getwinvar(winid, '&wrap'))
909 call assert_equal('', getwinvar(winid, '&omnifunc'))
910 call assert_equal(&g:path, getwinvar(winid, '&path'))
911 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
912
913 call popup_close(winid)
914 bwipe
915endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200916
917func Test_popup_atcursor()
918 topleft vnew
919 call setline(1, [
920 \ 'xxxxxxxxxxxxxxxxx',
921 \ 'xxxxxxxxxxxxxxxxx',
922 \ 'xxxxxxxxxxxxxxxxx',
923 \])
924
925 call cursor(2, 2)
926 redraw
927 let winid = popup_atcursor('vim', {})
928 redraw
929 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
930 call assert_equal('xvimxxxxxxxxxxxxx', line)
931 call popup_close(winid)
932
933 call cursor(3, 4)
934 redraw
935 let winid = popup_atcursor('vim', {})
936 redraw
937 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
938 call assert_equal('xxxvimxxxxxxxxxxx', line)
939 call popup_close(winid)
940
941 call cursor(1, 1)
942 redraw
943 let winid = popup_create('vim', {
944 \ 'line': 'cursor+2',
945 \ 'col': 'cursor+1',
946 \})
947 redraw
948 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
949 call assert_equal('xvimxxxxxxxxxxxxx', line)
950 call popup_close(winid)
951
952 call cursor(3, 3)
953 redraw
954 let winid = popup_create('vim', {
955 \ 'line': 'cursor-2',
956 \ 'col': 'cursor-1',
957 \})
958 redraw
959 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
960 call assert_equal('xvimxxxxxxxxxxxxx', line)
961 call popup_close(winid)
962
Bram Moolenaar402502d2019-05-30 22:07:36 +0200963 " just enough room above
964 call cursor(3, 3)
965 redraw
966 let winid = popup_atcursor(['vim', 'is great'], {})
967 redraw
968 let pos = popup_getpos(winid)
969 call assert_equal(1, pos.line)
970 call popup_close(winid)
971
972 " not enough room above, popup goes below the cursor
973 call cursor(3, 3)
974 redraw
975 let winid = popup_atcursor(['vim', 'is', 'great'], {})
976 redraw
977 let pos = popup_getpos(winid)
978 call assert_equal(4, pos.line)
979 call popup_close(winid)
980
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200981 " cursor in first line, popup in line 2
982 call cursor(1, 1)
983 redraw
984 let winid = popup_atcursor(['vim', 'is', 'great'], {})
985 redraw
986 let pos = popup_getpos(winid)
987 call assert_equal(2, pos.line)
988 call popup_close(winid)
989
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200990 bwipe!
991endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200992
993func Test_popup_filter()
994 new
995 call setline(1, 'some text')
996
997 func MyPopupFilter(winid, c)
998 if a:c == 'e'
999 let g:eaten = 'e'
1000 return 1
1001 endif
1002 if a:c == '0'
1003 let g:ignored = '0'
1004 return 0
1005 endif
1006 if a:c == 'x'
1007 call popup_close(a:winid)
1008 return 1
1009 endif
1010 return 0
1011 endfunc
1012
1013 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
1014 redraw
1015
1016 " e is consumed by the filter
1017 call feedkeys('e', 'xt')
1018 call assert_equal('e', g:eaten)
1019
1020 " 0 is ignored by the filter
1021 normal $
1022 call assert_equal(9, getcurpos()[2])
1023 call feedkeys('0', 'xt')
1024 call assert_equal('0', g:ignored)
1025 call assert_equal(1, getcurpos()[2])
1026
1027 " x closes the popup
1028 call feedkeys('x', 'xt')
1029 call assert_equal('e', g:eaten)
1030 call assert_equal(-1, winbufnr(winid))
1031
1032 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001033 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001034endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001035
Bram Moolenaara42d9452019-06-15 21:46:30 +02001036func ShowDialog(key, result)
1037 let s:cb_res = 999
1038 let winid = popup_dialog('do you want to quit (Yes/no)?', {
1039 \ 'filter': 'popup_filter_yesno',
1040 \ 'callback': 'QuitCallback',
1041 \ })
1042 redraw
1043 call feedkeys(a:key, "xt")
1044 call assert_equal(winid, s:cb_winid)
1045 call assert_equal(a:result, s:cb_res)
1046endfunc
1047
1048func Test_popup_dialog()
1049 func QuitCallback(id, res)
1050 let s:cb_winid = a:id
1051 let s:cb_res = a:res
1052 endfunc
1053
1054 let winid = ShowDialog("y", 1)
1055 let winid = ShowDialog("Y", 1)
1056 let winid = ShowDialog("n", 0)
1057 let winid = ShowDialog("N", 0)
1058 let winid = ShowDialog("x", 0)
1059 let winid = ShowDialog("X", 0)
1060 let winid = ShowDialog("\<Esc>", 0)
1061 let winid = ShowDialog("\<C-C>", -1)
1062
1063 delfunc QuitCallback
1064endfunc
1065
Bram Moolenaara730e552019-06-16 19:05:31 +02001066func ShowMenu(key, result)
1067 let s:cb_res = 999
1068 let winid = popup_menu(['one', 'two', 'something else'], {
1069 \ 'callback': 'QuitCallback',
1070 \ })
1071 redraw
1072 call feedkeys(a:key, "xt")
1073 call assert_equal(winid, s:cb_winid)
1074 call assert_equal(a:result, s:cb_res)
1075endfunc
1076
1077func Test_popup_menu()
1078 func QuitCallback(id, res)
1079 let s:cb_winid = a:id
1080 let s:cb_res = a:res
1081 endfunc
1082
1083 let winid = ShowMenu(" ", 1)
1084 let winid = ShowMenu("j \<CR>", 2)
1085 let winid = ShowMenu("JjK \<CR>", 2)
1086 let winid = ShowMenu("jjjjjj ", 3)
1087 let winid = ShowMenu("kkk ", 1)
1088 let winid = ShowMenu("x", -1)
1089 let winid = ShowMenu("X", -1)
1090 let winid = ShowMenu("\<Esc>", -1)
1091 let winid = ShowMenu("\<C-C>", -1)
1092
1093 delfunc QuitCallback
1094endfunc
1095
1096func Test_popup_menu_screenshot()
1097 if !CanRunVimInTerminal()
1098 throw 'Skipped: cannot make screendumps'
1099 endif
1100
1101 let lines =<< trim END
1102 call setline(1, range(1, 20))
1103 hi PopupSelected ctermbg=lightblue
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001104 call popup_menu(['one', 'two', 'another'], {'callback': 'MenuDone', 'title': ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001105 func MenuDone(id, res)
1106 echomsg "selected " .. a:res
1107 endfunc
1108 END
1109 call writefile(lines, 'XtestPopupMenu')
1110 let buf = RunVimInTerminal('-S XtestPopupMenu', {'rows': 10})
1111 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1112
1113 call term_sendkeys(buf, "jj")
1114 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1115
1116 call term_sendkeys(buf, " ")
1117 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1118
1119 " clean up
1120 call StopVimInTerminal(buf)
1121 call delete('XtestPopupMenu')
1122endfunc
1123
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001124func Test_popup_title()
1125 if !CanRunVimInTerminal()
1126 throw 'Skipped: cannot make screendumps'
1127 endif
1128
1129 " Create a popup without title or border, a line of padding will be added to
1130 " put the title on.
1131 let lines =<< trim END
1132 call setline(1, range(1, 20))
1133 call popup_create(['one', 'two', 'another'], {'title': 'Title String'})
1134 END
1135 call writefile(lines, 'XtestPopupTitle')
1136 let buf = RunVimInTerminal('-S XtestPopupTitle', {'rows': 10})
1137 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1138
1139 " clean up
1140 call StopVimInTerminal(buf)
1141 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001142
1143 let winid = popup_create('something', {'title': 'Some Title'})
1144 call assert_equal('Some Title', popup_getoptions(winid).title)
1145 call popup_setoptions(winid, {'title': 'Another Title'})
1146 call assert_equal('Another Title', popup_getoptions(winid).title)
1147
1148 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001149endfunc
1150
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001151func Test_popup_close_callback()
1152 func PopupDone(id, result)
1153 let g:result = a:result
1154 endfunc
1155 let winid = popup_create('something', {'callback': 'PopupDone'})
1156 redraw
1157 call popup_close(winid, 'done')
1158 call assert_equal('done', g:result)
1159endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001160
1161func Test_popup_empty()
1162 let winid = popup_create('', {'padding': [2,2,2,2]})
1163 redraw
1164 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001165 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001166 call assert_equal(5, pos.height)
1167
1168 let winid = popup_create([], {'border': []})
1169 redraw
1170 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001171 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001172 call assert_equal(3, pos.height)
1173endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001174
1175func Test_popup_never_behind()
1176 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001177 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +02001178 endif
1179 " +-----------------------------+
1180 " | | |
1181 " | | |
1182 " | | |
1183 " | line1 |
1184 " |------------line2------------|
1185 " | line3 |
1186 " | line4 |
1187 " | |
1188 " | |
1189 " +-----------------------------+
1190 let lines =<< trim END
1191 only
1192 split
1193 vsplit
1194 let info_window1 = getwininfo()[0]
1195 let line = info_window1['height']
1196 let col = info_window1['width']
1197 call popup_create(['line1', 'line2', 'line3', 'line4'], {
1198 \ 'line' : line,
1199 \ 'col' : col,
1200 \ })
1201 END
1202 call writefile(lines, 'XtestPopupBehind')
1203 let buf = RunVimInTerminal('-S XtestPopupBehind', {'rows': 10})
1204 call term_sendkeys(buf, "\<C-W>w")
1205 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1206
1207 " clean up
1208 call StopVimInTerminal(buf)
1209 call delete('XtestPopupBehind')
1210endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001211
1212func s:VerifyPosition( p, msg, line, col, width, height )
1213 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
1214 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
1215 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
1216 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
1217endfunc
1218
1219func Test_popup_position_adjust()
1220 " Anything placed past 2 cells from of the right of the screen is moved to the
1221 " left.
1222 "
1223 " When wrapping is disabled, we also shift to the left to display on the
1224 " screen, unless fixed is set.
1225
1226 " Entries for cases which don't vary based on wrapping.
1227 " Format is per tests described below
1228 let both_wrap_tests = [
1229 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
1230 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
1231 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
1232 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
1233 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
1234 \
1235 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
1236 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
1237 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
1238 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
1239 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
1240 \
1241 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1242 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1243 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1244 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1245 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
1246 \ ]
1247
1248 " these test groups are dicts with:
1249 " - comment: something to identify the group of tests by
1250 " - options: dict of options to merge with the row/col in tests
1251 " - tests: list of cases. Each one is a list with elements:
1252 " - text
1253 " - row
1254 " - col
1255 " - expected row
1256 " - expected col
1257 " - expected width
1258 " - expected height
1259 let tests = [
1260 \ {
1261 \ 'comment': 'left-aligned with wrapping',
1262 \ 'options': {
1263 \ 'wrap': 1,
1264 \ 'pos': 'botleft',
1265 \ },
1266 \ 'tests': both_wrap_tests + [
1267 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
1268 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
1269 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
1270 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
1271 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1272 \ ],
1273 \ },
1274 \ {
1275 \ 'comment': 'left aligned without wrapping',
1276 \ 'options': {
1277 \ 'wrap': 0,
1278 \ 'pos': 'botleft',
1279 \ },
1280 \ 'tests': both_wrap_tests + [
1281 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
1282 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
1283 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
1284 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
1285 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1286 \ ],
1287 \ },
1288 \ {
1289 \ 'comment': 'left aligned with fixed position',
1290 \ 'options': {
1291 \ 'wrap': 0,
1292 \ 'fixed': 1,
1293 \ 'pos': 'botleft',
1294 \ },
1295 \ 'tests': both_wrap_tests + [
1296 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1297 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1298 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1299 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1300 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1301 \ ],
1302 \ },
1303 \ ]
1304
1305 for test_group in tests
1306 for test in test_group.tests
1307 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
1308 let options = {
1309 \ 'line': line,
1310 \ 'col': col,
1311 \ }
1312 call extend( options, test_group.options )
1313
1314 let p = popup_create( text, options )
1315
1316 let msg = string( extend( options, { 'text': text } ) )
1317 call s:VerifyPosition( p, msg, e_line, e_col, e_width, e_height )
1318 call popup_close( p )
1319 endfor
1320 endfor
1321
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001322 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001323 %bwipe!
1324endfunc
1325
Bram Moolenaar3397f742019-06-02 18:40:06 +02001326func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001327 " width of screen
1328 let X = join(map(range(&columns), {->'X'}), '')
1329
1330 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1331 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
1332
1333 redraw
1334 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1335 call assert_equal(X, line)
1336
1337 call popup_close( p )
1338 redraw
1339
1340 " Same if placed on the right hand side
1341 let p = popup_create( X, { 'line': 1, 'col': &columns, 'wrap': 0 } )
1342 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
1343
1344 redraw
1345 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1346 call assert_equal(X, line)
1347
1348 call popup_close( p )
1349 redraw
1350
1351 " Extend so > window width
1352 let X .= 'x'
1353
1354 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1355 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
1356
1357 redraw
1358 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1359 call assert_equal(X[ : -2 ], line)
1360
1361 call popup_close( p )
1362 redraw
1363
1364 " Shifted then truncated (the x is not visible)
1365 let p = popup_create( X, { 'line': 1, 'col': &columns - 3, 'wrap': 0 } )
1366 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
1367
1368 redraw
1369 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1370 call assert_equal(X[ : -2 ], line)
1371
1372 call popup_close( p )
1373 redraw
1374
1375 " Not shifted, just truncated
1376 let p = popup_create( X,
1377 \ { 'line': 1, 'col': 2, 'wrap': 0, 'fixed': 1 } )
1378 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
1379
1380 redraw
1381 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1382 let e_line = ' ' . X[ 1 : -2 ]
1383 call assert_equal(e_line, line)
1384
1385 call popup_close( p )
1386 redraw
1387
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001388 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001389 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001390endfunc
1391
1392func Test_popup_moved()
1393 new
1394 call test_override('char_avail', 1)
1395 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1396
1397 exe "normal gg0/word\<CR>"
1398 let winid = popup_atcursor('text', {'moved': 'any'})
1399 redraw
1400 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001401 call assert_equal([4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001402 " trigger the check for last_cursormoved by going into insert mode
1403 call feedkeys("li\<Esc>", 'xt')
1404 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001405 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001406
1407 exe "normal gg0/word\<CR>"
1408 let winid = popup_atcursor('text', {'moved': 'word'})
1409 redraw
1410 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001411 call assert_equal([4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001412 call feedkeys("hi\<Esc>", 'xt')
1413 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001414 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001415
1416 exe "normal gg0/word\<CR>"
1417 let winid = popup_atcursor('text', {'moved': 'word'})
1418 redraw
1419 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001420 call assert_equal([4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001421 call feedkeys("li\<Esc>", 'xt')
1422 call assert_equal(1, popup_getpos(winid).visible)
1423 call feedkeys("ei\<Esc>", 'xt')
1424 call assert_equal(1, popup_getpos(winid).visible)
1425 call feedkeys("eli\<Esc>", 'xt')
1426 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001427 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001428
Bram Moolenaar17627312019-06-02 19:53:44 +02001429 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001430 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001431 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001432 redraw
1433 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001434 call assert_equal([2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001435 call feedkeys("eli\<Esc>", 'xt')
1436 call assert_equal(1, popup_getpos(winid).visible)
1437 call feedkeys("wi\<Esc>", 'xt')
1438 call assert_equal(1, popup_getpos(winid).visible)
1439 call feedkeys("Eli\<Esc>", 'xt')
1440 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001441 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001442
1443 exe "normal gg0/word\<CR>"
1444 let winid = popup_atcursor('text', {'moved': [5, 10]})
1445 redraw
1446 call assert_equal(1, popup_getpos(winid).visible)
1447 call feedkeys("eli\<Esc>", 'xt')
1448 call feedkeys("ei\<Esc>", 'xt')
1449 call assert_equal(1, popup_getpos(winid).visible)
1450 call feedkeys("eli\<Esc>", 'xt')
1451 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001452 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001453
1454 bwipe!
1455 call test_override('ALL', 0)
1456endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001457
1458func Test_notifications()
1459 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02001460 throw 'Skipped: timer feature not supported'
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001461 endif
1462 if !CanRunVimInTerminal()
1463 throw 'Skipped: cannot make screendumps'
1464 endif
1465
1466 call writefile([
1467 \ "call setline(1, range(1, 20))",
1468 \ "hi Notification ctermbg=lightblue",
1469 \ "call popup_notification('first notification', {})",
1470 \], 'XtestNotifications')
1471 let buf = RunVimInTerminal('-S XtestNotifications', {'rows': 10})
1472 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1473
1474 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001475 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1476 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001477 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1478
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001479 " clean up
1480 call StopVimInTerminal(buf)
1481 call delete('XtestNotifications')
1482endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001483
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001484func Test_popup_scrollbar()
1485 if !CanRunVimInTerminal()
1486 throw 'Skipped: cannot make screendumps'
1487 endif
1488
1489 let lines =<< trim END
1490 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001491 hi ScrollThumb ctermbg=blue
1492 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001493 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
1494 \ 'six', 'seven', 'eight', 'nine'], {
1495 \ 'minwidth': 8,
1496 \ 'maxheight': 4,
1497 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001498 func ScrollUp()
1499 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1500 endfunc
1501 func ScrollDown()
1502 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1503 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001504 func ClickTop()
1505 call feedkeys("\<F4>\<LeftMouse>", "xt")
1506 endfunc
1507 func ClickBot()
1508 call feedkeys("\<F5>\<LeftMouse>", "xt")
1509 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001510 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001511 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1512 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001513 END
1514 call writefile(lines, 'XtestPopupScroll')
1515 let buf = RunVimInTerminal('-S XtestPopupScroll', {'rows': 10})
1516 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1517
1518 call term_sendkeys(buf, ":call popup_setoptions(winid, {'firstline': 2})\<CR>")
1519 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1520
1521 call term_sendkeys(buf, ":call popup_setoptions(winid, {'firstline': 6})\<CR>")
1522 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1523
1524 call term_sendkeys(buf, ":call popup_setoptions(winid, {'firstline': 9})\<CR>")
1525 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1526
Bram Moolenaar8da41812019-06-26 18:04:54 +02001527 call term_sendkeys(buf, ":call popup_setoptions(winid, {'scrollbarhighlight': 'ScrollBar', 'thumbhighlight': 'ScrollThumb'})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001528 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1529 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1530
1531 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1532 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1533
1534 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001535 " wait a bit, otherwise it fails sometimes (double click recognized?)
1536 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001537 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1538 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1539
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001540 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1541 sleep 100m
1542 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1543 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1544
1545 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1546 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1547
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001548 " clean up
1549 call StopVimInTerminal(buf)
1550 call delete('XtestPopupScroll')
1551endfunc
1552
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001553func Test_popup_settext()
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001554 if !CanRunVimInTerminal()
1555 throw 'Skipped: cannot make screendumps'
1556 endif
1557
1558 let lines =<< trim END
1559 let opts = {'wrap': 0}
1560 let p = popup_create('test', opts)
1561 call popup_settext(p, 'this is a text')
1562 END
1563
1564 call writefile( lines, 'XtestPopupSetText' )
1565 let buf = RunVimInTerminal('-S XtestPopupSetText', {'rows': 10})
1566 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1567
1568 " Setting to empty string clears it
1569 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1570 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1571
1572 " Setting a list
1573 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1574 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1575
1576 " Shrinking with a list
1577 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1578 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1579
1580 " Growing with a list
1581 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1582 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1583
1584 " Empty list clears
1585 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1586 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1587
1588 " Dicts
1589 call term_sendkeys(buf, ":call popup_settext(p, [{'text': 'aaaa'}, {'text': 'bbbb'}, {'text': 'cccc'}])\<CR>")
1590 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1591
1592 " clean up
1593 call StopVimInTerminal(buf)
1594 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001595endfunc
1596
1597func Test_popup_hidden()
1598 new
1599
1600 let winid = popup_atcursor('text', {'hidden': 1})
1601 redraw
1602 call assert_equal(0, popup_getpos(winid).visible)
1603 call popup_close(winid)
1604
1605 let winid = popup_create('text', {'hidden': 1})
1606 redraw
1607 call assert_equal(0, popup_getpos(winid).visible)
1608 call popup_close(winid)
1609
1610 func QuitCallback(id, res)
1611 let s:cb_winid = a:id
1612 let s:cb_res = a:res
1613 endfunc
1614 let winid = popup_dialog('make a choice', {'hidden': 1,
1615 \ 'filter': 'popup_filter_yesno',
1616 \ 'callback': 'QuitCallback',
1617 \ })
1618 redraw
1619 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001620 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1621 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001622 exe "normal anot used by filter\<Esc>"
1623 call assert_equal('not used by filter', getline(1))
1624
1625 call popup_show(winid)
1626 call feedkeys('y', "xt")
1627 call assert_equal(1, s:cb_res)
1628
1629 bwipe!
1630 delfunc QuitCallback
1631endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001632
1633" Test options not checked elsewhere
1634func Test_set_get_options()
1635 let winid = popup_create('some text', {'highlight': 'Beautiful'})
1636 let options = popup_getoptions(winid)
1637 call assert_equal(1, options.wrap)
1638 call assert_equal(0, options.drag)
1639 call assert_equal('Beautiful', options.highlight)
1640
1641 call popup_setoptions(winid, {'wrap': 0, 'drag': 1, 'highlight': 'Another'})
1642 let options = popup_getoptions(winid)
1643 call assert_equal(0, options.wrap)
1644 call assert_equal(1, options.drag)
1645 call assert_equal('Another', options.highlight)
1646
1647 call popup_close(winid)
1648endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001649
1650func Test_popupwin_garbage_collect()
1651 func MyPopupFilter(x, winid, c)
1652 " NOP
1653 endfunc
1654
1655 let winid = popup_create('something', {'filter': function('MyPopupFilter', [{}])})
1656 call test_garbagecollect_now()
1657 redraw
1658 " Must not crach caused by invalid memory access
1659 call feedkeys('j', 'xt')
1660 call assert_true(v:true)
1661
1662 call popup_close(winid)
1663 delfunc MyPopupFilter
1664endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001665
1666func Test_popupwin_with_buffer()
1667 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1668 let buf = bufadd('XsomeFile')
1669 call assert_equal(0, bufloaded(buf))
1670 let winid = popup_create(buf, {})
1671 call assert_notequal(0, winid)
1672 let pos = popup_getpos(winid)
1673 call assert_equal(2, pos.height)
1674 call assert_equal(1, bufloaded(buf))
1675 call popup_close(winid)
1676 call assert_equal({}, popup_getpos(winid))
1677 call assert_equal(1, bufloaded(buf))
1678 exe 'bwipe! ' .. buf
Bram Moolenaar7866b872019-07-01 22:21:01 +02001679
1680 edit test_popupwin.vim
1681 let winid = popup_create(bufnr(''), {})
1682 redraw
1683 call popup_close(winid)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001684endfunc