blob: f89d5660231eda5a54633acb7354989aec16c2c1 [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
430 call setline(1, repeat([join(range(1, 40), '')], 10))
431 hi PopupColor ctermbg=lightgrey
432 let winid = popup_create([
433 \ 'some text',
434 \ 'another line',
435 \], {
436 \ 'line': 2,
437 \ 'col': 10,
438 \ 'zindex': 90,
439 \ 'padding': [],
440 \ 'highlight': 'PopupColor',
441 \ 'mask': [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
442 call popup_create([
443 \ 'xxxxxxxxx',
444 \ 'yyyyyyyyy',
445 \], {
446 \ 'line': 3,
447 \ 'col': 18,
448 \ 'zindex': 20})
449 END
450 call writefile(lines, 'XtestPopupMask')
451 let buf = RunVimInTerminal('-S XtestPopupMask', {'rows': 10})
452 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
453
454 call term_sendkeys(buf, ":call popup_move(winid, {'col': 11, 'line': 3})\<CR>")
455 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
456
457 " clean up
458 call StopVimInTerminal(buf)
459 call delete('XtestPopupMask')
460endfunc
461
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200462func Test_popup_select()
463 if !CanRunVimInTerminal()
464 throw 'Skipped: cannot make screendumps'
465 endif
Bram Moolenaar650a6372019-06-15 00:29:33 +0200466 if !has('clipboard')
467 throw 'Skipped: clipboard feature missing'
468 endif
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200469 " create a popup with some text to be selected
470 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200471 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200472 call setline(1, range(1, 20))
473 let winid = popup_create(['the word', 'some more', 'several words here'], {
474 \ 'drag': 1,
475 \ 'border': [],
476 \ 'line': 3,
477 \ 'col': 10,
478 \ })
479 func Select1()
480 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
481 endfunc
482 map <silent> <F3> :call test_setmouse(4, 15)<CR>
483 map <silent> <F4> :call test_setmouse(6, 23)<CR>
484 END
485 call writefile(lines, 'XtestPopupSelect')
486 let buf = RunVimInTerminal('-S XtestPopupSelect', {'rows': 10})
487 call term_sendkeys(buf, ":call Select1()\<CR>")
488 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
489
490 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
491 call term_sendkeys(buf, "\"*p")
492 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
493
494 " clean up
495 call StopVimInTerminal(buf)
496 call delete('XtestPopupSelect')
497endfunc
498
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200499func Test_popup_in_tab()
500 " default popup is local to tab, not visible when in other tab
501 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200502 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200503 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200504 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200505 tabnew
506 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200507 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200508 quit
509 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200510
511 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200512 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200513 " buffer is gone now
514 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200515
516 " global popup is visible in any tab
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200517 let winid = popup_create("text", {'tabpage': -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200518 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200519 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200520 tabnew
521 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200522 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200523 quit
524 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200525 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200526
527 " create popup in other tab
528 tabnew
529 let winid = popup_create("text", {'tabpage': 1})
530 call assert_equal(0, popup_getpos(winid).visible)
531 call assert_equal(1, popup_getoptions(winid).tabpage)
532 quit
533 call assert_equal(1, popup_getpos(winid).visible)
534 call assert_equal(0, popup_getoptions(winid).tabpage)
535 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200536endfunc
537
538func Test_popup_valid_arguments()
539 " Zero value is like the property wasn't there
540 let winid = popup_create("text", {"col": 0})
541 let pos = popup_getpos(winid)
542 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200543 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200544
545 " using cursor column has minimum value of 1
546 let winid = popup_create("text", {"col": 'cursor-100'})
547 let pos = popup_getpos(winid)
548 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200549 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200550
551 " center
552 let winid = popup_create("text", {"pos": 'center'})
553 let pos = popup_getpos(winid)
554 let around = (&columns - pos.width) / 2
555 call assert_inrange(around - 1, around + 1, pos.col)
556 let around = (&lines - pos.height) / 2
557 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200558 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200559endfunc
560
561func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200562 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200563 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200564 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200565 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200566
567 call assert_fails('call popup_create("text", {"col": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200568 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200569 call assert_fails('call popup_create("text", {"col": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200570 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200571 call assert_fails('call popup_create("text", {"col": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200572 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200573 call assert_fails('call popup_create("text", {"col": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200574 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200575
576 call assert_fails('call popup_create("text", {"line": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200577 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200578 call assert_fails('call popup_create("text", {"line": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200579 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200580 call assert_fails('call popup_create("text", {"line": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200581 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200582 call assert_fails('call popup_create("text", {"line": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200583 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200584
585 call assert_fails('call popup_create("text", {"pos": "there"})', 'E475:')
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", {"padding": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200588 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200589 call assert_fails('call popup_create("text", {"border": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200590 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200591 call assert_fails('call popup_create("text", {"borderhighlight": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200592 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200593 call assert_fails('call popup_create("text", {"borderchars": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200594 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200595
596 call assert_fails('call popup_create([{"text": "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200597 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200598 call assert_fails('call popup_create([{"text": "text", "props": "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200599 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200600 call assert_fails('call popup_create([{"text": "text", "props": ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200601 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200602endfunc
603
Bram Moolenaareea16992019-05-31 17:34:48 +0200604func Test_win_execute_closing_curwin()
605 split
606 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200607 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200608 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200609endfunc
610
611func Test_win_execute_not_allowed()
612 let winid = popup_create('some text', {})
613 call assert_fails('call win_execute(winid, "split")', 'E994:')
614 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
615 call assert_fails('call win_execute(winid, "close")', 'E994:')
616 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200617 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200618 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
619 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
620 call assert_fails('call win_execute(winid, "next")', 'E994:')
621 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
622 call assert_fails('call win_execute(winid, "buf")', 'E994:')
623 call assert_fails('call win_execute(winid, "edit")', 'E994:')
624 call assert_fails('call win_execute(winid, "enew")', 'E994:')
625 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
626 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
627 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
628 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200629 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200630endfunc
631
Bram Moolenaar402502d2019-05-30 22:07:36 +0200632func Test_popup_with_wrap()
633 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200634 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200635 endif
636 let lines =<< trim END
637 call setline(1, range(1, 100))
638 let winid = popup_create(
639 \ 'a long line that wont fit',
640 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
641 END
642 call writefile(lines, 'XtestPopup')
643 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
644 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
645
646 " clean up
647 call StopVimInTerminal(buf)
648 call delete('XtestPopup')
649endfunc
650
651func Test_popup_without_wrap()
652 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200653 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200654 endif
655 let lines =<< trim END
656 call setline(1, range(1, 100))
657 let winid = popup_create(
658 \ 'a long line that wont fit',
659 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
660 END
661 call writefile(lines, 'XtestPopup')
662 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
663 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
664
665 " clean up
666 call StopVimInTerminal(buf)
667 call delete('XtestPopup')
668endfunc
669
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200670func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200671 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +0200672 throw 'Skipped: timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200673 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200674 topleft vnew
675 call setline(1, 'hello')
676
677 call popup_create('world', {
678 \ 'line': 1,
679 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200680 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200681 \ 'time': 500,
682 \})
683 redraw
684 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
685 call assert_equal('world', line)
686
687 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200688 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200689 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
690 call assert_equal('hello', line)
691
692 call popup_create('on the command line', {
693 \ 'line': &lines,
694 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200695 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200696 \ 'time': 500,
697 \})
698 redraw
699 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
700 call assert_match('.*on the command line.*', line)
701
702 sleep 700m
703 redraw
704 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
705 call assert_notmatch('.*on the command line.*', line)
706
707 bwipe!
708endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200709
710func Test_popup_hide()
711 topleft vnew
712 call setline(1, 'hello')
713
714 let winid = popup_create('world', {
715 \ 'line': 1,
716 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200717 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200718 \})
719 redraw
720 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
721 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200722 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200723 " buffer is still listed and active
724 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200725
726 call popup_hide(winid)
727 redraw
728 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
729 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200730 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200731 " buffer is still listed but hidden
732 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200733
734 call popup_show(winid)
735 redraw
736 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
737 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200738 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200739
740
741 call popup_close(winid)
742 redraw
743 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
744 call assert_equal('hello', line)
745
746 " error is given for existing non-popup window
747 call assert_fails('call popup_hide(win_getid())', 'E993:')
748
749 " no error non-existing window
750 call popup_hide(1234234)
751 call popup_show(41234234)
752
753 bwipe!
754endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200755
756func Test_popup_move()
757 topleft vnew
758 call setline(1, 'hello')
759
760 let winid = popup_create('world', {
761 \ 'line': 1,
762 \ 'col': 1,
763 \ 'minwidth': 20,
764 \})
765 redraw
766 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
767 call assert_equal('world ', line)
768
769 call popup_move(winid, {'line': 2, 'col': 2})
770 redraw
771 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
772 call assert_equal('hello ', line)
773 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
774 call assert_equal('~world', line)
775
776 call popup_move(winid, {'line': 1})
777 redraw
778 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
779 call assert_equal('hworld', line)
780
781 call popup_close(winid)
782
783 bwipe!
784endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200785
Bram Moolenaar402502d2019-05-30 22:07:36 +0200786func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200787 let winid = popup_create('hello', {
788 \ 'line': 2,
789 \ 'col': 3,
790 \ 'minwidth': 10,
791 \ 'minheight': 11,
792 \})
793 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200794 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200795 call assert_equal(2, res.line)
796 call assert_equal(3, res.col)
797 call assert_equal(10, res.width)
798 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200799 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200800
801 call popup_close(winid)
802endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200803
804func Test_popup_width_longest()
805 let tests = [
806 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
807 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
808 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
809 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
810 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
811 \ ]
812
813 for test in tests
814 let winid = popup_create(test[0], {'line': 2, 'col': 3})
815 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200816 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200817 call assert_equal(test[1], position.width)
818 call popup_close(winid)
819 endfor
820endfunc
821
822func Test_popup_wraps()
823 let tests = [
824 \ ['nowrap', 6, 1],
825 \ ['a line that wraps once', 12, 2],
826 \ ['a line that wraps two times', 12, 3],
827 \ ]
828 for test in tests
829 let winid = popup_create(test[0],
830 \ {'line': 2, 'col': 3, 'maxwidth': 12})
831 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200832 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200833 call assert_equal(test[1], position.width)
834 call assert_equal(test[2], position.height)
835
836 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200837 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200838 endfor
839endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200840
841func Test_popup_getoptions()
842 let winid = popup_create('hello', {
843 \ 'line': 2,
844 \ 'col': 3,
845 \ 'minwidth': 10,
846 \ 'minheight': 11,
847 \ 'maxwidth': 20,
848 \ 'maxheight': 21,
849 \ 'zindex': 100,
850 \ 'time': 5000,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200851 \ 'fixed': 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200852 \})
853 redraw
854 let res = popup_getoptions(winid)
855 call assert_equal(2, res.line)
856 call assert_equal(3, res.col)
857 call assert_equal(10, res.minwidth)
858 call assert_equal(11, res.minheight)
859 call assert_equal(20, res.maxwidth)
860 call assert_equal(21, res.maxheight)
861 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200862 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200863 if has('timers')
864 call assert_equal(5000, res.time)
865 endif
866 call popup_close(winid)
867
868 let winid = popup_create('hello', {})
869 redraw
870 let res = popup_getoptions(winid)
871 call assert_equal(0, res.line)
872 call assert_equal(0, res.col)
873 call assert_equal(0, res.minwidth)
874 call assert_equal(0, res.minheight)
875 call assert_equal(0, res.maxwidth)
876 call assert_equal(0, res.maxheight)
877 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200878 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200879 if has('timers')
880 call assert_equal(0, res.time)
881 endif
882 call popup_close(winid)
883 call assert_equal({}, popup_getoptions(winid))
884endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200885
886func Test_popup_option_values()
887 new
888 " window-local
889 setlocal number
890 setlocal nowrap
891 " buffer-local
892 setlocal omnifunc=Something
893 " global/buffer-local
894 setlocal path=/there
895 " global/window-local
896 setlocal scrolloff=9
897
898 let winid = popup_create('hello', {})
899 call assert_equal(0, getwinvar(winid, '&number'))
900 call assert_equal(1, getwinvar(winid, '&wrap'))
901 call assert_equal('', getwinvar(winid, '&omnifunc'))
902 call assert_equal(&g:path, getwinvar(winid, '&path'))
903 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
904
905 call popup_close(winid)
906 bwipe
907endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200908
909func Test_popup_atcursor()
910 topleft vnew
911 call setline(1, [
912 \ 'xxxxxxxxxxxxxxxxx',
913 \ 'xxxxxxxxxxxxxxxxx',
914 \ 'xxxxxxxxxxxxxxxxx',
915 \])
916
917 call cursor(2, 2)
918 redraw
919 let winid = popup_atcursor('vim', {})
920 redraw
921 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
922 call assert_equal('xvimxxxxxxxxxxxxx', line)
923 call popup_close(winid)
924
925 call cursor(3, 4)
926 redraw
927 let winid = popup_atcursor('vim', {})
928 redraw
929 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
930 call assert_equal('xxxvimxxxxxxxxxxx', line)
931 call popup_close(winid)
932
933 call cursor(1, 1)
934 redraw
935 let winid = popup_create('vim', {
936 \ 'line': 'cursor+2',
937 \ 'col': 'cursor+1',
938 \})
939 redraw
940 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
941 call assert_equal('xvimxxxxxxxxxxxxx', line)
942 call popup_close(winid)
943
944 call cursor(3, 3)
945 redraw
946 let winid = popup_create('vim', {
947 \ 'line': 'cursor-2',
948 \ 'col': 'cursor-1',
949 \})
950 redraw
951 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
952 call assert_equal('xvimxxxxxxxxxxxxx', line)
953 call popup_close(winid)
954
Bram Moolenaar402502d2019-05-30 22:07:36 +0200955 " just enough room above
956 call cursor(3, 3)
957 redraw
958 let winid = popup_atcursor(['vim', 'is great'], {})
959 redraw
960 let pos = popup_getpos(winid)
961 call assert_equal(1, pos.line)
962 call popup_close(winid)
963
964 " not enough room above, popup goes below the cursor
965 call cursor(3, 3)
966 redraw
967 let winid = popup_atcursor(['vim', 'is', 'great'], {})
968 redraw
969 let pos = popup_getpos(winid)
970 call assert_equal(4, pos.line)
971 call popup_close(winid)
972
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200973 " cursor in first line, popup in line 2
974 call cursor(1, 1)
975 redraw
976 let winid = popup_atcursor(['vim', 'is', 'great'], {})
977 redraw
978 let pos = popup_getpos(winid)
979 call assert_equal(2, pos.line)
980 call popup_close(winid)
981
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200982 bwipe!
983endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200984
985func Test_popup_filter()
986 new
987 call setline(1, 'some text')
988
989 func MyPopupFilter(winid, c)
990 if a:c == 'e'
991 let g:eaten = 'e'
992 return 1
993 endif
994 if a:c == '0'
995 let g:ignored = '0'
996 return 0
997 endif
998 if a:c == 'x'
999 call popup_close(a:winid)
1000 return 1
1001 endif
1002 return 0
1003 endfunc
1004
1005 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
1006 redraw
1007
1008 " e is consumed by the filter
1009 call feedkeys('e', 'xt')
1010 call assert_equal('e', g:eaten)
1011
1012 " 0 is ignored by the filter
1013 normal $
1014 call assert_equal(9, getcurpos()[2])
1015 call feedkeys('0', 'xt')
1016 call assert_equal('0', g:ignored)
1017 call assert_equal(1, getcurpos()[2])
1018
1019 " x closes the popup
1020 call feedkeys('x', 'xt')
1021 call assert_equal('e', g:eaten)
1022 call assert_equal(-1, winbufnr(winid))
1023
1024 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001025 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001026endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001027
Bram Moolenaara42d9452019-06-15 21:46:30 +02001028func ShowDialog(key, result)
1029 let s:cb_res = 999
1030 let winid = popup_dialog('do you want to quit (Yes/no)?', {
1031 \ 'filter': 'popup_filter_yesno',
1032 \ 'callback': 'QuitCallback',
1033 \ })
1034 redraw
1035 call feedkeys(a:key, "xt")
1036 call assert_equal(winid, s:cb_winid)
1037 call assert_equal(a:result, s:cb_res)
1038endfunc
1039
1040func Test_popup_dialog()
1041 func QuitCallback(id, res)
1042 let s:cb_winid = a:id
1043 let s:cb_res = a:res
1044 endfunc
1045
1046 let winid = ShowDialog("y", 1)
1047 let winid = ShowDialog("Y", 1)
1048 let winid = ShowDialog("n", 0)
1049 let winid = ShowDialog("N", 0)
1050 let winid = ShowDialog("x", 0)
1051 let winid = ShowDialog("X", 0)
1052 let winid = ShowDialog("\<Esc>", 0)
1053 let winid = ShowDialog("\<C-C>", -1)
1054
1055 delfunc QuitCallback
1056endfunc
1057
Bram Moolenaara730e552019-06-16 19:05:31 +02001058func ShowMenu(key, result)
1059 let s:cb_res = 999
1060 let winid = popup_menu(['one', 'two', 'something else'], {
1061 \ 'callback': 'QuitCallback',
1062 \ })
1063 redraw
1064 call feedkeys(a:key, "xt")
1065 call assert_equal(winid, s:cb_winid)
1066 call assert_equal(a:result, s:cb_res)
1067endfunc
1068
1069func Test_popup_menu()
1070 func QuitCallback(id, res)
1071 let s:cb_winid = a:id
1072 let s:cb_res = a:res
1073 endfunc
1074
1075 let winid = ShowMenu(" ", 1)
1076 let winid = ShowMenu("j \<CR>", 2)
1077 let winid = ShowMenu("JjK \<CR>", 2)
1078 let winid = ShowMenu("jjjjjj ", 3)
1079 let winid = ShowMenu("kkk ", 1)
1080 let winid = ShowMenu("x", -1)
1081 let winid = ShowMenu("X", -1)
1082 let winid = ShowMenu("\<Esc>", -1)
1083 let winid = ShowMenu("\<C-C>", -1)
1084
1085 delfunc QuitCallback
1086endfunc
1087
1088func Test_popup_menu_screenshot()
1089 if !CanRunVimInTerminal()
1090 throw 'Skipped: cannot make screendumps'
1091 endif
1092
1093 let lines =<< trim END
1094 call setline(1, range(1, 20))
1095 hi PopupSelected ctermbg=lightblue
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001096 call popup_menu(['one', 'two', 'another'], {'callback': 'MenuDone', 'title': ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001097 func MenuDone(id, res)
1098 echomsg "selected " .. a:res
1099 endfunc
1100 END
1101 call writefile(lines, 'XtestPopupMenu')
1102 let buf = RunVimInTerminal('-S XtestPopupMenu', {'rows': 10})
1103 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1104
1105 call term_sendkeys(buf, "jj")
1106 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1107
1108 call term_sendkeys(buf, " ")
1109 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1110
1111 " clean up
1112 call StopVimInTerminal(buf)
1113 call delete('XtestPopupMenu')
1114endfunc
1115
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001116func Test_popup_title()
1117 if !CanRunVimInTerminal()
1118 throw 'Skipped: cannot make screendumps'
1119 endif
1120
1121 " Create a popup without title or border, a line of padding will be added to
1122 " put the title on.
1123 let lines =<< trim END
1124 call setline(1, range(1, 20))
1125 call popup_create(['one', 'two', 'another'], {'title': 'Title String'})
1126 END
1127 call writefile(lines, 'XtestPopupTitle')
1128 let buf = RunVimInTerminal('-S XtestPopupTitle', {'rows': 10})
1129 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1130
1131 " clean up
1132 call StopVimInTerminal(buf)
1133 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001134
1135 let winid = popup_create('something', {'title': 'Some Title'})
1136 call assert_equal('Some Title', popup_getoptions(winid).title)
1137 call popup_setoptions(winid, {'title': 'Another Title'})
1138 call assert_equal('Another Title', popup_getoptions(winid).title)
1139
1140 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001141endfunc
1142
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001143func Test_popup_close_callback()
1144 func PopupDone(id, result)
1145 let g:result = a:result
1146 endfunc
1147 let winid = popup_create('something', {'callback': 'PopupDone'})
1148 redraw
1149 call popup_close(winid, 'done')
1150 call assert_equal('done', g:result)
1151endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001152
1153func Test_popup_empty()
1154 let winid = popup_create('', {'padding': [2,2,2,2]})
1155 redraw
1156 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001157 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001158 call assert_equal(5, pos.height)
1159
1160 let winid = popup_create([], {'border': []})
1161 redraw
1162 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001163 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001164 call assert_equal(3, pos.height)
1165endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001166
1167func Test_popup_never_behind()
1168 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001169 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +02001170 endif
1171 " +-----------------------------+
1172 " | | |
1173 " | | |
1174 " | | |
1175 " | line1 |
1176 " |------------line2------------|
1177 " | line3 |
1178 " | line4 |
1179 " | |
1180 " | |
1181 " +-----------------------------+
1182 let lines =<< trim END
1183 only
1184 split
1185 vsplit
1186 let info_window1 = getwininfo()[0]
1187 let line = info_window1['height']
1188 let col = info_window1['width']
1189 call popup_create(['line1', 'line2', 'line3', 'line4'], {
1190 \ 'line' : line,
1191 \ 'col' : col,
1192 \ })
1193 END
1194 call writefile(lines, 'XtestPopupBehind')
1195 let buf = RunVimInTerminal('-S XtestPopupBehind', {'rows': 10})
1196 call term_sendkeys(buf, "\<C-W>w")
1197 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1198
1199 " clean up
1200 call StopVimInTerminal(buf)
1201 call delete('XtestPopupBehind')
1202endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001203
1204func s:VerifyPosition( p, msg, line, col, width, height )
1205 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
1206 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
1207 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
1208 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
1209endfunc
1210
1211func Test_popup_position_adjust()
1212 " Anything placed past 2 cells from of the right of the screen is moved to the
1213 " left.
1214 "
1215 " When wrapping is disabled, we also shift to the left to display on the
1216 " screen, unless fixed is set.
1217
1218 " Entries for cases which don't vary based on wrapping.
1219 " Format is per tests described below
1220 let both_wrap_tests = [
1221 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
1222 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
1223 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
1224 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
1225 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
1226 \
1227 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
1228 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
1229 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
1230 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
1231 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
1232 \
1233 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1234 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1235 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1236 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1237 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
1238 \ ]
1239
1240 " these test groups are dicts with:
1241 " - comment: something to identify the group of tests by
1242 " - options: dict of options to merge with the row/col in tests
1243 " - tests: list of cases. Each one is a list with elements:
1244 " - text
1245 " - row
1246 " - col
1247 " - expected row
1248 " - expected col
1249 " - expected width
1250 " - expected height
1251 let tests = [
1252 \ {
1253 \ 'comment': 'left-aligned with wrapping',
1254 \ 'options': {
1255 \ 'wrap': 1,
1256 \ 'pos': 'botleft',
1257 \ },
1258 \ 'tests': both_wrap_tests + [
1259 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
1260 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
1261 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
1262 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
1263 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1264 \ ],
1265 \ },
1266 \ {
1267 \ 'comment': 'left aligned without wrapping',
1268 \ 'options': {
1269 \ 'wrap': 0,
1270 \ 'pos': 'botleft',
1271 \ },
1272 \ 'tests': both_wrap_tests + [
1273 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
1274 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
1275 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
1276 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
1277 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1278 \ ],
1279 \ },
1280 \ {
1281 \ 'comment': 'left aligned with fixed position',
1282 \ 'options': {
1283 \ 'wrap': 0,
1284 \ 'fixed': 1,
1285 \ 'pos': 'botleft',
1286 \ },
1287 \ 'tests': both_wrap_tests + [
1288 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1289 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1290 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1291 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1292 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1293 \ ],
1294 \ },
1295 \ ]
1296
1297 for test_group in tests
1298 for test in test_group.tests
1299 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
1300 let options = {
1301 \ 'line': line,
1302 \ 'col': col,
1303 \ }
1304 call extend( options, test_group.options )
1305
1306 let p = popup_create( text, options )
1307
1308 let msg = string( extend( options, { 'text': text } ) )
1309 call s:VerifyPosition( p, msg, e_line, e_col, e_width, e_height )
1310 call popup_close( p )
1311 endfor
1312 endfor
1313
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001314 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001315 %bwipe!
1316endfunc
1317
Bram Moolenaar3397f742019-06-02 18:40:06 +02001318func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001319 " width of screen
1320 let X = join(map(range(&columns), {->'X'}), '')
1321
1322 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1323 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
1324
1325 redraw
1326 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1327 call assert_equal(X, line)
1328
1329 call popup_close( p )
1330 redraw
1331
1332 " Same if placed on the right hand side
1333 let p = popup_create( X, { 'line': 1, 'col': &columns, 'wrap': 0 } )
1334 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
1335
1336 redraw
1337 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1338 call assert_equal(X, line)
1339
1340 call popup_close( p )
1341 redraw
1342
1343 " Extend so > window width
1344 let X .= 'x'
1345
1346 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1347 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
1348
1349 redraw
1350 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1351 call assert_equal(X[ : -2 ], line)
1352
1353 call popup_close( p )
1354 redraw
1355
1356 " Shifted then truncated (the x is not visible)
1357 let p = popup_create( X, { 'line': 1, 'col': &columns - 3, 'wrap': 0 } )
1358 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
1359
1360 redraw
1361 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1362 call assert_equal(X[ : -2 ], line)
1363
1364 call popup_close( p )
1365 redraw
1366
1367 " Not shifted, just truncated
1368 let p = popup_create( X,
1369 \ { 'line': 1, 'col': 2, 'wrap': 0, 'fixed': 1 } )
1370 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
1371
1372 redraw
1373 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1374 let e_line = ' ' . X[ 1 : -2 ]
1375 call assert_equal(e_line, line)
1376
1377 call popup_close( p )
1378 redraw
1379
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001380 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001381 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001382endfunc
1383
1384func Test_popup_moved()
1385 new
1386 call test_override('char_avail', 1)
1387 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1388
1389 exe "normal gg0/word\<CR>"
1390 let winid = popup_atcursor('text', {'moved': 'any'})
1391 redraw
1392 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001393 call assert_equal([4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001394 " trigger the check for last_cursormoved by going into insert mode
1395 call feedkeys("li\<Esc>", 'xt')
1396 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001397 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001398
1399 exe "normal gg0/word\<CR>"
1400 let winid = popup_atcursor('text', {'moved': 'word'})
1401 redraw
1402 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001403 call assert_equal([4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001404 call feedkeys("hi\<Esc>", 'xt')
1405 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001406 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001407
1408 exe "normal gg0/word\<CR>"
1409 let winid = popup_atcursor('text', {'moved': 'word'})
1410 redraw
1411 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001412 call assert_equal([4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001413 call feedkeys("li\<Esc>", 'xt')
1414 call assert_equal(1, popup_getpos(winid).visible)
1415 call feedkeys("ei\<Esc>", 'xt')
1416 call assert_equal(1, popup_getpos(winid).visible)
1417 call feedkeys("eli\<Esc>", 'xt')
1418 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001419 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001420
Bram Moolenaar17627312019-06-02 19:53:44 +02001421 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001422 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001423 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001424 redraw
1425 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001426 call assert_equal([2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001427 call feedkeys("eli\<Esc>", 'xt')
1428 call assert_equal(1, popup_getpos(winid).visible)
1429 call feedkeys("wi\<Esc>", 'xt')
1430 call assert_equal(1, popup_getpos(winid).visible)
1431 call feedkeys("Eli\<Esc>", 'xt')
1432 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001433 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001434
1435 exe "normal gg0/word\<CR>"
1436 let winid = popup_atcursor('text', {'moved': [5, 10]})
1437 redraw
1438 call assert_equal(1, popup_getpos(winid).visible)
1439 call feedkeys("eli\<Esc>", 'xt')
1440 call feedkeys("ei\<Esc>", 'xt')
1441 call assert_equal(1, popup_getpos(winid).visible)
1442 call feedkeys("eli\<Esc>", 'xt')
1443 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001444 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001445
1446 bwipe!
1447 call test_override('ALL', 0)
1448endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001449
1450func Test_notifications()
1451 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02001452 throw 'Skipped: timer feature not supported'
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001453 endif
1454 if !CanRunVimInTerminal()
1455 throw 'Skipped: cannot make screendumps'
1456 endif
1457
1458 call writefile([
1459 \ "call setline(1, range(1, 20))",
1460 \ "hi Notification ctermbg=lightblue",
1461 \ "call popup_notification('first notification', {})",
1462 \], 'XtestNotifications')
1463 let buf = RunVimInTerminal('-S XtestNotifications', {'rows': 10})
1464 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1465
1466 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001467 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1468 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001469 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1470
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001471 " clean up
1472 call StopVimInTerminal(buf)
1473 call delete('XtestNotifications')
1474endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001475
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001476func Test_popup_scrollbar()
1477 if !CanRunVimInTerminal()
1478 throw 'Skipped: cannot make screendumps'
1479 endif
1480
1481 let lines =<< trim END
1482 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001483 hi ScrollThumb ctermbg=blue
1484 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001485 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
1486 \ 'six', 'seven', 'eight', 'nine'], {
1487 \ 'minwidth': 8,
1488 \ 'maxheight': 4,
1489 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001490 func ScrollUp()
1491 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1492 endfunc
1493 func ScrollDown()
1494 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1495 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001496 func ClickTop()
1497 call feedkeys("\<F4>\<LeftMouse>", "xt")
1498 endfunc
1499 func ClickBot()
1500 call feedkeys("\<F5>\<LeftMouse>", "xt")
1501 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001502 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001503 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1504 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001505 END
1506 call writefile(lines, 'XtestPopupScroll')
1507 let buf = RunVimInTerminal('-S XtestPopupScroll', {'rows': 10})
1508 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1509
1510 call term_sendkeys(buf, ":call popup_setoptions(winid, {'firstline': 2})\<CR>")
1511 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1512
1513 call term_sendkeys(buf, ":call popup_setoptions(winid, {'firstline': 6})\<CR>")
1514 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1515
1516 call term_sendkeys(buf, ":call popup_setoptions(winid, {'firstline': 9})\<CR>")
1517 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1518
Bram Moolenaar8da41812019-06-26 18:04:54 +02001519 call term_sendkeys(buf, ":call popup_setoptions(winid, {'scrollbarhighlight': 'ScrollBar', 'thumbhighlight': 'ScrollThumb'})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001520 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1521 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1522
1523 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1524 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1525
1526 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001527 " wait a bit, otherwise it fails sometimes (double click recognized?)
1528 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001529 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1530 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1531
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001532 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1533 sleep 100m
1534 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1535 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1536
1537 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1538 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1539
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001540 " clean up
1541 call StopVimInTerminal(buf)
1542 call delete('XtestPopupScroll')
1543endfunc
1544
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001545func Test_popup_settext()
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001546 if !CanRunVimInTerminal()
1547 throw 'Skipped: cannot make screendumps'
1548 endif
1549
1550 let lines =<< trim END
1551 let opts = {'wrap': 0}
1552 let p = popup_create('test', opts)
1553 call popup_settext(p, 'this is a text')
1554 END
1555
1556 call writefile( lines, 'XtestPopupSetText' )
1557 let buf = RunVimInTerminal('-S XtestPopupSetText', {'rows': 10})
1558 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1559
1560 " Setting to empty string clears it
1561 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1562 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1563
1564 " Setting a list
1565 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1566 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1567
1568 " Shrinking with a list
1569 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1570 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1571
1572 " Growing with 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 " Empty list clears
1577 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1578 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1579
1580 " Dicts
1581 call term_sendkeys(buf, ":call popup_settext(p, [{'text': 'aaaa'}, {'text': 'bbbb'}, {'text': 'cccc'}])\<CR>")
1582 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1583
1584 " clean up
1585 call StopVimInTerminal(buf)
1586 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001587endfunc
1588
1589func Test_popup_hidden()
1590 new
1591
1592 let winid = popup_atcursor('text', {'hidden': 1})
1593 redraw
1594 call assert_equal(0, popup_getpos(winid).visible)
1595 call popup_close(winid)
1596
1597 let winid = popup_create('text', {'hidden': 1})
1598 redraw
1599 call assert_equal(0, popup_getpos(winid).visible)
1600 call popup_close(winid)
1601
1602 func QuitCallback(id, res)
1603 let s:cb_winid = a:id
1604 let s:cb_res = a:res
1605 endfunc
1606 let winid = popup_dialog('make a choice', {'hidden': 1,
1607 \ 'filter': 'popup_filter_yesno',
1608 \ 'callback': 'QuitCallback',
1609 \ })
1610 redraw
1611 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001612 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1613 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001614 exe "normal anot used by filter\<Esc>"
1615 call assert_equal('not used by filter', getline(1))
1616
1617 call popup_show(winid)
1618 call feedkeys('y', "xt")
1619 call assert_equal(1, s:cb_res)
1620
1621 bwipe!
1622 delfunc QuitCallback
1623endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001624
1625" Test options not checked elsewhere
1626func Test_set_get_options()
1627 let winid = popup_create('some text', {'highlight': 'Beautiful'})
1628 let options = popup_getoptions(winid)
1629 call assert_equal(1, options.wrap)
1630 call assert_equal(0, options.drag)
1631 call assert_equal('Beautiful', options.highlight)
1632
1633 call popup_setoptions(winid, {'wrap': 0, 'drag': 1, 'highlight': 'Another'})
1634 let options = popup_getoptions(winid)
1635 call assert_equal(0, options.wrap)
1636 call assert_equal(1, options.drag)
1637 call assert_equal('Another', options.highlight)
1638
1639 call popup_close(winid)
1640endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001641
1642func Test_popupwin_garbage_collect()
1643 func MyPopupFilter(x, winid, c)
1644 " NOP
1645 endfunc
1646
1647 let winid = popup_create('something', {'filter': function('MyPopupFilter', [{}])})
1648 call test_garbagecollect_now()
1649 redraw
1650 " Must not crach caused by invalid memory access
1651 call feedkeys('j', 'xt')
1652 call assert_true(v:true)
1653
1654 call popup_close(winid)
1655 delfunc MyPopupFilter
1656endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001657
1658func Test_popupwin_with_buffer()
1659 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1660 let buf = bufadd('XsomeFile')
1661 call assert_equal(0, bufloaded(buf))
1662 let winid = popup_create(buf, {})
1663 call assert_notequal(0, winid)
1664 let pos = popup_getpos(winid)
1665 call assert_equal(2, pos.height)
1666 call assert_equal(1, bufloaded(buf))
1667 call popup_close(winid)
1668 call assert_equal({}, popup_getpos(winid))
1669 call assert_equal(1, bufloaded(buf))
1670 exe 'bwipe! ' .. buf
Bram Moolenaar7866b872019-07-01 22:21:01 +02001671
1672 edit test_popupwin.vim
1673 let winid = popup_create(bufnr(''), {})
1674 redraw
1675 call popup_close(winid)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001676endfunc