blob: 1cb0734ec91926bb337caa755da824262cf0edfe [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 Moolenaare7eb9272019-06-24 00:58:07 +0200368func Test_popup_with_mask()
369 if !CanRunVimInTerminal()
370 throw 'Skipped: cannot make screendumps'
371 endif
372 let lines =<< trim END
373 call setline(1, repeat([join(range(1, 40), '')], 10))
374 hi PopupColor ctermbg=lightgrey
375 let winid = popup_create([
376 \ 'some text',
377 \ 'another line',
378 \], {
379 \ 'line': 2,
380 \ 'col': 10,
381 \ 'zindex': 90,
382 \ 'padding': [],
383 \ 'highlight': 'PopupColor',
384 \ 'mask': [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
385 call popup_create([
386 \ 'xxxxxxxxx',
387 \ 'yyyyyyyyy',
388 \], {
389 \ 'line': 3,
390 \ 'col': 18,
391 \ 'zindex': 20})
392 END
393 call writefile(lines, 'XtestPopupMask')
394 let buf = RunVimInTerminal('-S XtestPopupMask', {'rows': 10})
395 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
396
397 call term_sendkeys(buf, ":call popup_move(winid, {'col': 11, 'line': 3})\<CR>")
398 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
399
400 " clean up
401 call StopVimInTerminal(buf)
402 call delete('XtestPopupMask')
403endfunc
404
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200405func Test_popup_select()
406 if !CanRunVimInTerminal()
407 throw 'Skipped: cannot make screendumps'
408 endif
Bram Moolenaar650a6372019-06-15 00:29:33 +0200409 if !has('clipboard')
410 throw 'Skipped: clipboard feature missing'
411 endif
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200412 " create a popup with some text to be selected
413 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200414 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200415 call setline(1, range(1, 20))
416 let winid = popup_create(['the word', 'some more', 'several words here'], {
417 \ 'drag': 1,
418 \ 'border': [],
419 \ 'line': 3,
420 \ 'col': 10,
421 \ })
422 func Select1()
423 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
424 endfunc
425 map <silent> <F3> :call test_setmouse(4, 15)<CR>
426 map <silent> <F4> :call test_setmouse(6, 23)<CR>
427 END
428 call writefile(lines, 'XtestPopupSelect')
429 let buf = RunVimInTerminal('-S XtestPopupSelect', {'rows': 10})
430 call term_sendkeys(buf, ":call Select1()\<CR>")
431 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
432
433 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
434 call term_sendkeys(buf, "\"*p")
435 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
436
437 " clean up
438 call StopVimInTerminal(buf)
439 call delete('XtestPopupSelect')
440endfunc
441
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200442func Test_popup_in_tab()
443 " default popup is local to tab, not visible when in other tab
444 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200445 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200446 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200447 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200448 tabnew
449 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200450 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200451 quit
452 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200453
454 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200455 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200456 " buffer is gone now
457 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200458
459 " global popup is visible in any tab
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200460 let winid = popup_create("text", {'tabpage': -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200461 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200462 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200463 tabnew
464 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200465 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200466 quit
467 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200468 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200469
470 " create popup in other tab
471 tabnew
472 let winid = popup_create("text", {'tabpage': 1})
473 call assert_equal(0, popup_getpos(winid).visible)
474 call assert_equal(1, popup_getoptions(winid).tabpage)
475 quit
476 call assert_equal(1, popup_getpos(winid).visible)
477 call assert_equal(0, popup_getoptions(winid).tabpage)
478 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200479endfunc
480
481func Test_popup_valid_arguments()
482 " Zero value is like the property wasn't there
483 let winid = popup_create("text", {"col": 0})
484 let pos = popup_getpos(winid)
485 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200486 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200487
488 " using cursor column has minimum value of 1
489 let winid = popup_create("text", {"col": 'cursor-100'})
490 let pos = popup_getpos(winid)
491 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200492 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200493
494 " center
495 let winid = popup_create("text", {"pos": 'center'})
496 let pos = popup_getpos(winid)
497 let around = (&columns - pos.width) / 2
498 call assert_inrange(around - 1, around + 1, pos.col)
499 let around = (&lines - pos.height) / 2
500 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200501 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200502endfunc
503
504func Test_popup_invalid_arguments()
505 call assert_fails('call popup_create(666, {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200506 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200507 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200508 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200509
510 call assert_fails('call popup_create("text", {"col": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200511 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200512 call assert_fails('call popup_create("text", {"col": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200513 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200514 call assert_fails('call popup_create("text", {"col": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200515 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200516 call assert_fails('call popup_create("text", {"col": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200517 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200518
519 call assert_fails('call popup_create("text", {"line": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200520 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200521 call assert_fails('call popup_create("text", {"line": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200522 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200523 call assert_fails('call popup_create("text", {"line": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200524 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200525 call assert_fails('call popup_create("text", {"line": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200526 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200527
528 call assert_fails('call popup_create("text", {"pos": "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200529 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200530 call assert_fails('call popup_create("text", {"padding": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200531 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200532 call assert_fails('call popup_create("text", {"border": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200533 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200534 call assert_fails('call popup_create("text", {"borderhighlight": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200535 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200536 call assert_fails('call popup_create("text", {"borderchars": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200537 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200538
539 call assert_fails('call popup_create([{"text": "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200540 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200541 call assert_fails('call popup_create([{"text": "text", "props": "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200542 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200543 call assert_fails('call popup_create([{"text": "text", "props": ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200544 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200545endfunc
546
Bram Moolenaareea16992019-05-31 17:34:48 +0200547func Test_win_execute_closing_curwin()
548 split
549 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200550 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200551 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200552endfunc
553
554func Test_win_execute_not_allowed()
555 let winid = popup_create('some text', {})
556 call assert_fails('call win_execute(winid, "split")', 'E994:')
557 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
558 call assert_fails('call win_execute(winid, "close")', 'E994:')
559 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200560 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200561 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
562 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
563 call assert_fails('call win_execute(winid, "next")', 'E994:')
564 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
565 call assert_fails('call win_execute(winid, "buf")', 'E994:')
566 call assert_fails('call win_execute(winid, "edit")', 'E994:')
567 call assert_fails('call win_execute(winid, "enew")', 'E994:')
568 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
569 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
570 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
571 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200572 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200573endfunc
574
Bram Moolenaar402502d2019-05-30 22:07:36 +0200575func Test_popup_with_wrap()
576 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200577 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200578 endif
579 let lines =<< trim END
580 call setline(1, range(1, 100))
581 let winid = popup_create(
582 \ 'a long line that wont fit',
583 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
584 END
585 call writefile(lines, 'XtestPopup')
586 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
587 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
588
589 " clean up
590 call StopVimInTerminal(buf)
591 call delete('XtestPopup')
592endfunc
593
594func Test_popup_without_wrap()
595 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200596 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200597 endif
598 let lines =<< trim END
599 call setline(1, range(1, 100))
600 let winid = popup_create(
601 \ 'a long line that wont fit',
602 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
603 END
604 call writefile(lines, 'XtestPopup')
605 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
606 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
607
608 " clean up
609 call StopVimInTerminal(buf)
610 call delete('XtestPopup')
611endfunc
612
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200613func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200614 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +0200615 throw 'Skipped: timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200616 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200617 topleft vnew
618 call setline(1, 'hello')
619
620 call popup_create('world', {
621 \ 'line': 1,
622 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200623 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200624 \ 'time': 500,
625 \})
626 redraw
627 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
628 call assert_equal('world', line)
629
630 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200631 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200632 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
633 call assert_equal('hello', line)
634
635 call popup_create('on the command line', {
636 \ 'line': &lines,
637 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200638 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200639 \ 'time': 500,
640 \})
641 redraw
642 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
643 call assert_match('.*on the command line.*', line)
644
645 sleep 700m
646 redraw
647 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
648 call assert_notmatch('.*on the command line.*', line)
649
650 bwipe!
651endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200652
653func Test_popup_hide()
654 topleft vnew
655 call setline(1, 'hello')
656
657 let winid = popup_create('world', {
658 \ 'line': 1,
659 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200660 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200661 \})
662 redraw
663 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
664 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200665 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200666 " buffer is still listed and active
667 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200668
669 call popup_hide(winid)
670 redraw
671 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
672 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200673 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200674 " buffer is still listed but hidden
675 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200676
677 call popup_show(winid)
678 redraw
679 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
680 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200681 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200682
683
684 call popup_close(winid)
685 redraw
686 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
687 call assert_equal('hello', line)
688
689 " error is given for existing non-popup window
690 call assert_fails('call popup_hide(win_getid())', 'E993:')
691
692 " no error non-existing window
693 call popup_hide(1234234)
694 call popup_show(41234234)
695
696 bwipe!
697endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200698
699func Test_popup_move()
700 topleft vnew
701 call setline(1, 'hello')
702
703 let winid = popup_create('world', {
704 \ 'line': 1,
705 \ 'col': 1,
706 \ 'minwidth': 20,
707 \})
708 redraw
709 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
710 call assert_equal('world ', line)
711
712 call popup_move(winid, {'line': 2, 'col': 2})
713 redraw
714 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
715 call assert_equal('hello ', line)
716 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
717 call assert_equal('~world', line)
718
719 call popup_move(winid, {'line': 1})
720 redraw
721 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
722 call assert_equal('hworld', line)
723
724 call popup_close(winid)
725
726 bwipe!
727endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200728
Bram Moolenaar402502d2019-05-30 22:07:36 +0200729func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200730 let winid = popup_create('hello', {
731 \ 'line': 2,
732 \ 'col': 3,
733 \ 'minwidth': 10,
734 \ 'minheight': 11,
735 \})
736 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200737 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200738 call assert_equal(2, res.line)
739 call assert_equal(3, res.col)
740 call assert_equal(10, res.width)
741 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200742 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200743
744 call popup_close(winid)
745endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200746
747func Test_popup_width_longest()
748 let tests = [
749 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
750 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
751 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
752 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
753 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
754 \ ]
755
756 for test in tests
757 let winid = popup_create(test[0], {'line': 2, 'col': 3})
758 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200759 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200760 call assert_equal(test[1], position.width)
761 call popup_close(winid)
762 endfor
763endfunc
764
765func Test_popup_wraps()
766 let tests = [
767 \ ['nowrap', 6, 1],
768 \ ['a line that wraps once', 12, 2],
769 \ ['a line that wraps two times', 12, 3],
770 \ ]
771 for test in tests
772 let winid = popup_create(test[0],
773 \ {'line': 2, 'col': 3, 'maxwidth': 12})
774 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200775 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200776 call assert_equal(test[1], position.width)
777 call assert_equal(test[2], position.height)
778
779 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200780 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200781 endfor
782endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200783
784func Test_popup_getoptions()
785 let winid = popup_create('hello', {
786 \ 'line': 2,
787 \ 'col': 3,
788 \ 'minwidth': 10,
789 \ 'minheight': 11,
790 \ 'maxwidth': 20,
791 \ 'maxheight': 21,
792 \ 'zindex': 100,
793 \ 'time': 5000,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200794 \ 'fixed': 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200795 \})
796 redraw
797 let res = popup_getoptions(winid)
798 call assert_equal(2, res.line)
799 call assert_equal(3, res.col)
800 call assert_equal(10, res.minwidth)
801 call assert_equal(11, res.minheight)
802 call assert_equal(20, res.maxwidth)
803 call assert_equal(21, res.maxheight)
804 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200805 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200806 if has('timers')
807 call assert_equal(5000, res.time)
808 endif
809 call popup_close(winid)
810
811 let winid = popup_create('hello', {})
812 redraw
813 let res = popup_getoptions(winid)
814 call assert_equal(0, res.line)
815 call assert_equal(0, res.col)
816 call assert_equal(0, res.minwidth)
817 call assert_equal(0, res.minheight)
818 call assert_equal(0, res.maxwidth)
819 call assert_equal(0, res.maxheight)
820 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200821 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200822 if has('timers')
823 call assert_equal(0, res.time)
824 endif
825 call popup_close(winid)
826 call assert_equal({}, popup_getoptions(winid))
827endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200828
829func Test_popup_option_values()
830 new
831 " window-local
832 setlocal number
833 setlocal nowrap
834 " buffer-local
835 setlocal omnifunc=Something
836 " global/buffer-local
837 setlocal path=/there
838 " global/window-local
839 setlocal scrolloff=9
840
841 let winid = popup_create('hello', {})
842 call assert_equal(0, getwinvar(winid, '&number'))
843 call assert_equal(1, getwinvar(winid, '&wrap'))
844 call assert_equal('', getwinvar(winid, '&omnifunc'))
845 call assert_equal(&g:path, getwinvar(winid, '&path'))
846 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
847
848 call popup_close(winid)
849 bwipe
850endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200851
852func Test_popup_atcursor()
853 topleft vnew
854 call setline(1, [
855 \ 'xxxxxxxxxxxxxxxxx',
856 \ 'xxxxxxxxxxxxxxxxx',
857 \ 'xxxxxxxxxxxxxxxxx',
858 \])
859
860 call cursor(2, 2)
861 redraw
862 let winid = popup_atcursor('vim', {})
863 redraw
864 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
865 call assert_equal('xvimxxxxxxxxxxxxx', line)
866 call popup_close(winid)
867
868 call cursor(3, 4)
869 redraw
870 let winid = popup_atcursor('vim', {})
871 redraw
872 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
873 call assert_equal('xxxvimxxxxxxxxxxx', line)
874 call popup_close(winid)
875
876 call cursor(1, 1)
877 redraw
878 let winid = popup_create('vim', {
879 \ 'line': 'cursor+2',
880 \ 'col': 'cursor+1',
881 \})
882 redraw
883 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
884 call assert_equal('xvimxxxxxxxxxxxxx', line)
885 call popup_close(winid)
886
887 call cursor(3, 3)
888 redraw
889 let winid = popup_create('vim', {
890 \ 'line': 'cursor-2',
891 \ 'col': 'cursor-1',
892 \})
893 redraw
894 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
895 call assert_equal('xvimxxxxxxxxxxxxx', line)
896 call popup_close(winid)
897
Bram Moolenaar402502d2019-05-30 22:07:36 +0200898 " just enough room above
899 call cursor(3, 3)
900 redraw
901 let winid = popup_atcursor(['vim', 'is great'], {})
902 redraw
903 let pos = popup_getpos(winid)
904 call assert_equal(1, pos.line)
905 call popup_close(winid)
906
907 " not enough room above, popup goes below the cursor
908 call cursor(3, 3)
909 redraw
910 let winid = popup_atcursor(['vim', 'is', 'great'], {})
911 redraw
912 let pos = popup_getpos(winid)
913 call assert_equal(4, pos.line)
914 call popup_close(winid)
915
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200916 " cursor in first line, popup in line 2
917 call cursor(1, 1)
918 redraw
919 let winid = popup_atcursor(['vim', 'is', 'great'], {})
920 redraw
921 let pos = popup_getpos(winid)
922 call assert_equal(2, pos.line)
923 call popup_close(winid)
924
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200925 bwipe!
926endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200927
928func Test_popup_filter()
929 new
930 call setline(1, 'some text')
931
932 func MyPopupFilter(winid, c)
933 if a:c == 'e'
934 let g:eaten = 'e'
935 return 1
936 endif
937 if a:c == '0'
938 let g:ignored = '0'
939 return 0
940 endif
941 if a:c == 'x'
942 call popup_close(a:winid)
943 return 1
944 endif
945 return 0
946 endfunc
947
948 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
949 redraw
950
951 " e is consumed by the filter
952 call feedkeys('e', 'xt')
953 call assert_equal('e', g:eaten)
954
955 " 0 is ignored by the filter
956 normal $
957 call assert_equal(9, getcurpos()[2])
958 call feedkeys('0', 'xt')
959 call assert_equal('0', g:ignored)
960 call assert_equal(1, getcurpos()[2])
961
962 " x closes the popup
963 call feedkeys('x', 'xt')
964 call assert_equal('e', g:eaten)
965 call assert_equal(-1, winbufnr(winid))
966
967 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200968 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200969endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200970
Bram Moolenaara42d9452019-06-15 21:46:30 +0200971func ShowDialog(key, result)
972 let s:cb_res = 999
973 let winid = popup_dialog('do you want to quit (Yes/no)?', {
974 \ 'filter': 'popup_filter_yesno',
975 \ 'callback': 'QuitCallback',
976 \ })
977 redraw
978 call feedkeys(a:key, "xt")
979 call assert_equal(winid, s:cb_winid)
980 call assert_equal(a:result, s:cb_res)
981endfunc
982
983func Test_popup_dialog()
984 func QuitCallback(id, res)
985 let s:cb_winid = a:id
986 let s:cb_res = a:res
987 endfunc
988
989 let winid = ShowDialog("y", 1)
990 let winid = ShowDialog("Y", 1)
991 let winid = ShowDialog("n", 0)
992 let winid = ShowDialog("N", 0)
993 let winid = ShowDialog("x", 0)
994 let winid = ShowDialog("X", 0)
995 let winid = ShowDialog("\<Esc>", 0)
996 let winid = ShowDialog("\<C-C>", -1)
997
998 delfunc QuitCallback
999endfunc
1000
Bram Moolenaara730e552019-06-16 19:05:31 +02001001func ShowMenu(key, result)
1002 let s:cb_res = 999
1003 let winid = popup_menu(['one', 'two', 'something else'], {
1004 \ 'callback': 'QuitCallback',
1005 \ })
1006 redraw
1007 call feedkeys(a:key, "xt")
1008 call assert_equal(winid, s:cb_winid)
1009 call assert_equal(a:result, s:cb_res)
1010endfunc
1011
1012func Test_popup_menu()
1013 func QuitCallback(id, res)
1014 let s:cb_winid = a:id
1015 let s:cb_res = a:res
1016 endfunc
1017
1018 let winid = ShowMenu(" ", 1)
1019 let winid = ShowMenu("j \<CR>", 2)
1020 let winid = ShowMenu("JjK \<CR>", 2)
1021 let winid = ShowMenu("jjjjjj ", 3)
1022 let winid = ShowMenu("kkk ", 1)
1023 let winid = ShowMenu("x", -1)
1024 let winid = ShowMenu("X", -1)
1025 let winid = ShowMenu("\<Esc>", -1)
1026 let winid = ShowMenu("\<C-C>", -1)
1027
1028 delfunc QuitCallback
1029endfunc
1030
1031func Test_popup_menu_screenshot()
1032 if !CanRunVimInTerminal()
1033 throw 'Skipped: cannot make screendumps'
1034 endif
1035
1036 let lines =<< trim END
1037 call setline(1, range(1, 20))
1038 hi PopupSelected ctermbg=lightblue
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001039 call popup_menu(['one', 'two', 'another'], {'callback': 'MenuDone', 'title': ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001040 func MenuDone(id, res)
1041 echomsg "selected " .. a:res
1042 endfunc
1043 END
1044 call writefile(lines, 'XtestPopupMenu')
1045 let buf = RunVimInTerminal('-S XtestPopupMenu', {'rows': 10})
1046 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1047
1048 call term_sendkeys(buf, "jj")
1049 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1050
1051 call term_sendkeys(buf, " ")
1052 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1053
1054 " clean up
1055 call StopVimInTerminal(buf)
1056 call delete('XtestPopupMenu')
1057endfunc
1058
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001059func Test_popup_title()
1060 if !CanRunVimInTerminal()
1061 throw 'Skipped: cannot make screendumps'
1062 endif
1063
1064 " Create a popup without title or border, a line of padding will be added to
1065 " put the title on.
1066 let lines =<< trim END
1067 call setline(1, range(1, 20))
1068 call popup_create(['one', 'two', 'another'], {'title': 'Title String'})
1069 END
1070 call writefile(lines, 'XtestPopupTitle')
1071 let buf = RunVimInTerminal('-S XtestPopupTitle', {'rows': 10})
1072 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1073
1074 " clean up
1075 call StopVimInTerminal(buf)
1076 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001077
1078 let winid = popup_create('something', {'title': 'Some Title'})
1079 call assert_equal('Some Title', popup_getoptions(winid).title)
1080 call popup_setoptions(winid, {'title': 'Another Title'})
1081 call assert_equal('Another Title', popup_getoptions(winid).title)
1082
1083 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001084endfunc
1085
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001086func Test_popup_close_callback()
1087 func PopupDone(id, result)
1088 let g:result = a:result
1089 endfunc
1090 let winid = popup_create('something', {'callback': 'PopupDone'})
1091 redraw
1092 call popup_close(winid, 'done')
1093 call assert_equal('done', g:result)
1094endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001095
1096func Test_popup_empty()
1097 let winid = popup_create('', {'padding': [2,2,2,2]})
1098 redraw
1099 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001100 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001101 call assert_equal(5, pos.height)
1102
1103 let winid = popup_create([], {'border': []})
1104 redraw
1105 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001106 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001107 call assert_equal(3, pos.height)
1108endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001109
1110func Test_popup_never_behind()
1111 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001112 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +02001113 endif
1114 " +-----------------------------+
1115 " | | |
1116 " | | |
1117 " | | |
1118 " | line1 |
1119 " |------------line2------------|
1120 " | line3 |
1121 " | line4 |
1122 " | |
1123 " | |
1124 " +-----------------------------+
1125 let lines =<< trim END
1126 only
1127 split
1128 vsplit
1129 let info_window1 = getwininfo()[0]
1130 let line = info_window1['height']
1131 let col = info_window1['width']
1132 call popup_create(['line1', 'line2', 'line3', 'line4'], {
1133 \ 'line' : line,
1134 \ 'col' : col,
1135 \ })
1136 END
1137 call writefile(lines, 'XtestPopupBehind')
1138 let buf = RunVimInTerminal('-S XtestPopupBehind', {'rows': 10})
1139 call term_sendkeys(buf, "\<C-W>w")
1140 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1141
1142 " clean up
1143 call StopVimInTerminal(buf)
1144 call delete('XtestPopupBehind')
1145endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001146
1147func s:VerifyPosition( p, msg, line, col, width, height )
1148 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
1149 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
1150 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
1151 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
1152endfunc
1153
1154func Test_popup_position_adjust()
1155 " Anything placed past 2 cells from of the right of the screen is moved to the
1156 " left.
1157 "
1158 " When wrapping is disabled, we also shift to the left to display on the
1159 " screen, unless fixed is set.
1160
1161 " Entries for cases which don't vary based on wrapping.
1162 " Format is per tests described below
1163 let both_wrap_tests = [
1164 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
1165 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
1166 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
1167 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
1168 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
1169 \
1170 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
1171 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
1172 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
1173 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
1174 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
1175 \
1176 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1177 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1178 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1179 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1180 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
1181 \ ]
1182
1183 " these test groups are dicts with:
1184 " - comment: something to identify the group of tests by
1185 " - options: dict of options to merge with the row/col in tests
1186 " - tests: list of cases. Each one is a list with elements:
1187 " - text
1188 " - row
1189 " - col
1190 " - expected row
1191 " - expected col
1192 " - expected width
1193 " - expected height
1194 let tests = [
1195 \ {
1196 \ 'comment': 'left-aligned with wrapping',
1197 \ 'options': {
1198 \ 'wrap': 1,
1199 \ 'pos': 'botleft',
1200 \ },
1201 \ 'tests': both_wrap_tests + [
1202 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
1203 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
1204 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
1205 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
1206 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1207 \ ],
1208 \ },
1209 \ {
1210 \ 'comment': 'left aligned without wrapping',
1211 \ 'options': {
1212 \ 'wrap': 0,
1213 \ 'pos': 'botleft',
1214 \ },
1215 \ 'tests': both_wrap_tests + [
1216 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
1217 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
1218 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
1219 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
1220 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1221 \ ],
1222 \ },
1223 \ {
1224 \ 'comment': 'left aligned with fixed position',
1225 \ 'options': {
1226 \ 'wrap': 0,
1227 \ 'fixed': 1,
1228 \ 'pos': 'botleft',
1229 \ },
1230 \ 'tests': both_wrap_tests + [
1231 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1232 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1233 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1234 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1235 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1236 \ ],
1237 \ },
1238 \ ]
1239
1240 for test_group in tests
1241 for test in test_group.tests
1242 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
1243 let options = {
1244 \ 'line': line,
1245 \ 'col': col,
1246 \ }
1247 call extend( options, test_group.options )
1248
1249 let p = popup_create( text, options )
1250
1251 let msg = string( extend( options, { 'text': text } ) )
1252 call s:VerifyPosition( p, msg, e_line, e_col, e_width, e_height )
1253 call popup_close( p )
1254 endfor
1255 endfor
1256
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001257 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001258 %bwipe!
1259endfunc
1260
Bram Moolenaar3397f742019-06-02 18:40:06 +02001261func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001262 " width of screen
1263 let X = join(map(range(&columns), {->'X'}), '')
1264
1265 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1266 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
1267
1268 redraw
1269 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1270 call assert_equal(X, line)
1271
1272 call popup_close( p )
1273 redraw
1274
1275 " Same if placed on the right hand side
1276 let p = popup_create( X, { 'line': 1, 'col': &columns, 'wrap': 0 } )
1277 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
1278
1279 redraw
1280 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1281 call assert_equal(X, line)
1282
1283 call popup_close( p )
1284 redraw
1285
1286 " Extend so > window width
1287 let X .= 'x'
1288
1289 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1290 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
1291
1292 redraw
1293 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1294 call assert_equal(X[ : -2 ], line)
1295
1296 call popup_close( p )
1297 redraw
1298
1299 " Shifted then truncated (the x is not visible)
1300 let p = popup_create( X, { 'line': 1, 'col': &columns - 3, 'wrap': 0 } )
1301 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
1302
1303 redraw
1304 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1305 call assert_equal(X[ : -2 ], line)
1306
1307 call popup_close( p )
1308 redraw
1309
1310 " Not shifted, just truncated
1311 let p = popup_create( X,
1312 \ { 'line': 1, 'col': 2, 'wrap': 0, 'fixed': 1 } )
1313 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
1314
1315 redraw
1316 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1317 let e_line = ' ' . X[ 1 : -2 ]
1318 call assert_equal(e_line, line)
1319
1320 call popup_close( p )
1321 redraw
1322
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001323 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001324 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001325endfunc
1326
1327func Test_popup_moved()
1328 new
1329 call test_override('char_avail', 1)
1330 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1331
1332 exe "normal gg0/word\<CR>"
1333 let winid = popup_atcursor('text', {'moved': 'any'})
1334 redraw
1335 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001336 call assert_equal([4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001337 " trigger the check for last_cursormoved by going into insert mode
1338 call feedkeys("li\<Esc>", 'xt')
1339 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001340 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001341
1342 exe "normal gg0/word\<CR>"
1343 let winid = popup_atcursor('text', {'moved': 'word'})
1344 redraw
1345 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001346 call assert_equal([4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001347 call feedkeys("hi\<Esc>", 'xt')
1348 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001349 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001350
1351 exe "normal gg0/word\<CR>"
1352 let winid = popup_atcursor('text', {'moved': 'word'})
1353 redraw
1354 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001355 call assert_equal([4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001356 call feedkeys("li\<Esc>", 'xt')
1357 call assert_equal(1, popup_getpos(winid).visible)
1358 call feedkeys("ei\<Esc>", 'xt')
1359 call assert_equal(1, popup_getpos(winid).visible)
1360 call feedkeys("eli\<Esc>", 'xt')
1361 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001362 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001363
Bram Moolenaar17627312019-06-02 19:53:44 +02001364 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001365 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001366 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001367 redraw
1368 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001369 call assert_equal([2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001370 call feedkeys("eli\<Esc>", 'xt')
1371 call assert_equal(1, popup_getpos(winid).visible)
1372 call feedkeys("wi\<Esc>", 'xt')
1373 call assert_equal(1, popup_getpos(winid).visible)
1374 call feedkeys("Eli\<Esc>", 'xt')
1375 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001376 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001377
1378 exe "normal gg0/word\<CR>"
1379 let winid = popup_atcursor('text', {'moved': [5, 10]})
1380 redraw
1381 call assert_equal(1, popup_getpos(winid).visible)
1382 call feedkeys("eli\<Esc>", 'xt')
1383 call feedkeys("ei\<Esc>", 'xt')
1384 call assert_equal(1, popup_getpos(winid).visible)
1385 call feedkeys("eli\<Esc>", 'xt')
1386 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001387 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001388
1389 bwipe!
1390 call test_override('ALL', 0)
1391endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001392
1393func Test_notifications()
1394 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02001395 throw 'Skipped: timer feature not supported'
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001396 endif
1397 if !CanRunVimInTerminal()
1398 throw 'Skipped: cannot make screendumps'
1399 endif
1400
1401 call writefile([
1402 \ "call setline(1, range(1, 20))",
1403 \ "hi Notification ctermbg=lightblue",
1404 \ "call popup_notification('first notification', {})",
1405 \], 'XtestNotifications')
1406 let buf = RunVimInTerminal('-S XtestNotifications', {'rows': 10})
1407 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1408
1409 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001410 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1411 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001412 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1413
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001414 " clean up
1415 call StopVimInTerminal(buf)
1416 call delete('XtestNotifications')
1417endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001418
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001419func Test_popup_scrollbar()
1420 if !CanRunVimInTerminal()
1421 throw 'Skipped: cannot make screendumps'
1422 endif
1423
1424 let lines =<< trim END
1425 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001426 hi ScrollThumb ctermbg=blue
1427 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001428 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
1429 \ 'six', 'seven', 'eight', 'nine'], {
1430 \ 'minwidth': 8,
1431 \ 'maxheight': 4,
1432 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001433 func ScrollUp()
1434 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1435 endfunc
1436 func ScrollDown()
1437 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1438 endfunc
1439 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001440 END
1441 call writefile(lines, 'XtestPopupScroll')
1442 let buf = RunVimInTerminal('-S XtestPopupScroll', {'rows': 10})
1443 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1444
1445 call term_sendkeys(buf, ":call popup_setoptions(winid, {'firstline': 2})\<CR>")
1446 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1447
1448 call term_sendkeys(buf, ":call popup_setoptions(winid, {'firstline': 6})\<CR>")
1449 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1450
1451 call term_sendkeys(buf, ":call popup_setoptions(winid, {'firstline': 9})\<CR>")
1452 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1453
Bram Moolenaar8da41812019-06-26 18:04:54 +02001454 call term_sendkeys(buf, ":call popup_setoptions(winid, {'scrollbarhighlight': 'ScrollBar', 'thumbhighlight': 'ScrollThumb'})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001455 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1456 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1457
1458 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1459 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1460
1461 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001462 " wait a bit, otherwise it fails sometimes (double click recognized?)
1463 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001464 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1465 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1466
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001467 " clean up
1468 call StopVimInTerminal(buf)
1469 call delete('XtestPopupScroll')
1470endfunc
1471
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001472func Test_popup_settext()
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001473 if !CanRunVimInTerminal()
1474 throw 'Skipped: cannot make screendumps'
1475 endif
1476
1477 let lines =<< trim END
1478 let opts = {'wrap': 0}
1479 let p = popup_create('test', opts)
1480 call popup_settext(p, 'this is a text')
1481 END
1482
1483 call writefile( lines, 'XtestPopupSetText' )
1484 let buf = RunVimInTerminal('-S XtestPopupSetText', {'rows': 10})
1485 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1486
1487 " Setting to empty string clears it
1488 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1489 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1490
1491 " Setting a list
1492 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1493 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1494
1495 " Shrinking with a list
1496 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1497 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1498
1499 " Growing with a list
1500 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1501 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1502
1503 " Empty list clears
1504 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1505 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1506
1507 " Dicts
1508 call term_sendkeys(buf, ":call popup_settext(p, [{'text': 'aaaa'}, {'text': 'bbbb'}, {'text': 'cccc'}])\<CR>")
1509 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1510
1511 " clean up
1512 call StopVimInTerminal(buf)
1513 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001514endfunc
1515
1516func Test_popup_hidden()
1517 new
1518
1519 let winid = popup_atcursor('text', {'hidden': 1})
1520 redraw
1521 call assert_equal(0, popup_getpos(winid).visible)
1522 call popup_close(winid)
1523
1524 let winid = popup_create('text', {'hidden': 1})
1525 redraw
1526 call assert_equal(0, popup_getpos(winid).visible)
1527 call popup_close(winid)
1528
1529 func QuitCallback(id, res)
1530 let s:cb_winid = a:id
1531 let s:cb_res = a:res
1532 endfunc
1533 let winid = popup_dialog('make a choice', {'hidden': 1,
1534 \ 'filter': 'popup_filter_yesno',
1535 \ 'callback': 'QuitCallback',
1536 \ })
1537 redraw
1538 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001539 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1540 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001541 exe "normal anot used by filter\<Esc>"
1542 call assert_equal('not used by filter', getline(1))
1543
1544 call popup_show(winid)
1545 call feedkeys('y', "xt")
1546 call assert_equal(1, s:cb_res)
1547
1548 bwipe!
1549 delfunc QuitCallback
1550endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001551
1552" Test options not checked elsewhere
1553func Test_set_get_options()
1554 let winid = popup_create('some text', {'highlight': 'Beautiful'})
1555 let options = popup_getoptions(winid)
1556 call assert_equal(1, options.wrap)
1557 call assert_equal(0, options.drag)
1558 call assert_equal('Beautiful', options.highlight)
1559
1560 call popup_setoptions(winid, {'wrap': 0, 'drag': 1, 'highlight': 'Another'})
1561 let options = popup_getoptions(winid)
1562 call assert_equal(0, options.wrap)
1563 call assert_equal(1, options.drag)
1564 call assert_equal('Another', options.highlight)
1565
1566 call popup_close(winid)
1567endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001568
1569func Test_popupwin_garbage_collect()
1570 func MyPopupFilter(x, winid, c)
1571 " NOP
1572 endfunc
1573
1574 let winid = popup_create('something', {'filter': function('MyPopupFilter', [{}])})
1575 call test_garbagecollect_now()
1576 redraw
1577 " Must not crach caused by invalid memory access
1578 call feedkeys('j', 'xt')
1579 call assert_true(v:true)
1580
1581 call popup_close(winid)
1582 delfunc MyPopupFilter
1583endfunc