blob: b6cef00370799780ad259b87bd827683c8145b86 [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 Moolenaar6c6a6032019-06-25 05:33:36 +0200137 \ 'scrollbar': 0,
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200138 \ 'visible': 1}
139 let winid = popup_create('hello border', {'line': 2, 'col': 3, 'border': []})",
140 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200141 let options = popup_getoptions(winid)
142 call assert_equal([], options.border)
143 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200144
Bram Moolenaarae943152019-06-16 22:54:14 +0200145 let winid = popup_create('hello padding', {'line': 2, 'col': 3, 'padding': []})
146 let with_border_or_padding.width = 15
147 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200148 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200149 let options = popup_getoptions(winid)
150 call assert_false(has_key(options, "border"))
151 call assert_equal([], options.padding)
152
153 call popup_setoptions(winid, {
154 \ 'padding': [1, 2, 3, 4],
155 \ 'border': [4, 0, 7, 8],
156 \ 'borderhighlight': ['Top', 'Right', 'Bottom', 'Left'],
157 \ 'borderchars': ['1', '^', '2', '>', '3', 'v', '4', '<'],
158 \ })
159 let options = popup_getoptions(winid)
160 call assert_equal([1, 0, 1, 1], options.border)
161 call assert_equal([1, 2, 3, 4], options.padding)
162 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
163 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200164
165 let winid = popup_create('hello both', {'line': 3, 'col': 8, 'border': [], 'padding': []})
166 call assert_equal({
167 \ 'line': 3,
168 \ 'core_line': 5,
169 \ 'col': 8,
170 \ 'core_col': 10,
171 \ 'width': 14,
172 \ 'core_width': 10,
173 \ 'height': 5,
Bram Moolenaar6c6a6032019-06-25 05:33:36 +0200174 \ 'scrollbar': 0,
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200175 \ 'core_height': 1,
176 \ 'visible': 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200177
178 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200179endfunc
180
Bram Moolenaarb4230122019-05-30 18:40:53 +0200181func Test_popup_with_syntax_win_execute()
182 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200183 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200184 endif
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200185 let lines =<< trim END
186 call setline(1, range(1, 100))
187 hi PopupColor ctermbg=lightblue
188 let winid = popup_create([
189 \ '#include <stdio.h>',
190 \ 'int main(void)',
191 \ '{',
192 \ ' printf(123);',
193 \ '}',
194 \], {'line': 3, 'col': 25, 'highlight': 'PopupColor'})
195 call win_execute(winid, 'set syntax=cpp')
196 END
197 call writefile(lines, 'XtestPopup')
Bram Moolenaarb4230122019-05-30 18:40:53 +0200198 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
199 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
200
201 " clean up
202 call StopVimInTerminal(buf)
203 call delete('XtestPopup')
204endfunc
205
206func Test_popup_with_syntax_setbufvar()
207 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200208 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200209 endif
Bram Moolenaar402502d2019-05-30 22:07:36 +0200210 let lines =<< trim END
211 call setline(1, range(1, 100))
212 hi PopupColor ctermbg=lightgrey
213 let winid = popup_create([
214 \ '#include <stdio.h>',
215 \ 'int main(void)',
216 \ '{',
217 \ ' printf(567);',
218 \ '}',
219 \], {'line': 3, 'col': 21, 'highlight': 'PopupColor'})
220 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
221 END
222 call writefile(lines, 'XtestPopup')
Bram Moolenaarb4230122019-05-30 18:40:53 +0200223 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
224 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
225
226 " clean up
227 call StopVimInTerminal(buf)
228 call delete('XtestPopup')
229endfunc
230
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200231func Test_popup_with_matches()
232 if !CanRunVimInTerminal()
233 throw 'Skipped: cannot make screendumps'
234 endif
235 let lines =<< trim END
236 call setline(1, ['111 222 333', '444 555 666'])
237 let winid = popup_create([
238 \ '111 222 333',
239 \ '444 555 666',
240 \], {'line': 3, 'col': 10, 'border': []})
241 set hlsearch
242 /666
243 call matchadd('ErrorMsg', '111')
244 call matchadd('ErrorMsg', '444')
245 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
246 call win_execute(winid, "call matchadd('ErrorMsg', '555')")
247 END
248 call writefile(lines, 'XtestPopupMatches')
249 let buf = RunVimInTerminal('-S XtestPopupMatches', {'rows': 10})
250 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
251
252 " clean up
253 call StopVimInTerminal(buf)
254 call delete('XtestPopupMatches')
255endfunc
256
Bram Moolenaar399d8982019-06-02 15:34:29 +0200257func Test_popup_all_corners()
258 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200259 throw 'Skipped: cannot make screendumps'
Bram Moolenaar399d8982019-06-02 15:34:29 +0200260 endif
261 let lines =<< trim END
262 call setline(1, repeat([repeat('-', 60)], 15))
263 set so=0
264 normal 2G3|r#
265 let winid1 = popup_create(['first', 'second'], {
266 \ 'line': 'cursor+1',
267 \ 'col': 'cursor',
268 \ 'pos': 'topleft',
269 \ 'border': [],
270 \ 'padding': [],
271 \ })
272 normal 25|r@
273 let winid1 = popup_create(['First', 'SeconD'], {
274 \ 'line': 'cursor+1',
275 \ 'col': 'cursor',
276 \ 'pos': 'topright',
277 \ 'border': [],
278 \ 'padding': [],
279 \ })
280 normal 9G29|r%
281 let winid1 = popup_create(['fiRSt', 'seCOnd'], {
282 \ 'line': 'cursor-1',
283 \ 'col': 'cursor',
284 \ 'pos': 'botleft',
285 \ 'border': [],
286 \ 'padding': [],
287 \ })
288 normal 51|r&
289 let winid1 = popup_create(['FIrsT', 'SEcoND'], {
290 \ 'line': 'cursor-1',
291 \ 'col': 'cursor',
292 \ 'pos': 'botright',
293 \ 'border': [],
294 \ 'padding': [],
295 \ })
296 END
297 call writefile(lines, 'XtestPopupCorners')
298 let buf = RunVimInTerminal('-S XtestPopupCorners', {'rows': 12})
299 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
300
301 " clean up
302 call StopVimInTerminal(buf)
303 call delete('XtestPopupCorners')
304endfunc
305
Bram Moolenaar8d241042019-06-12 23:40:01 +0200306func Test_popup_firstline()
307 if !CanRunVimInTerminal()
308 throw 'Skipped: cannot make screendumps'
309 endif
310 let lines =<< trim END
311 call setline(1, range(1, 20))
312 call popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], {
313 \ 'maxheight': 4,
314 \ 'firstline': 3,
315 \ })
316 END
317 call writefile(lines, 'XtestPopupFirstline')
318 let buf = RunVimInTerminal('-S XtestPopupFirstline', {'rows': 10})
319 call VerifyScreenDump(buf, 'Test_popupwin_firstline', {})
320
321 " clean up
322 call StopVimInTerminal(buf)
323 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200324
325 let winid = popup_create(['1111', '222222', '33333', '44444'], {
326 \ 'maxheight': 2,
327 \ 'firstline': 3,
328 \ })
329 call assert_equal(3, popup_getoptions(winid).firstline)
330 call popup_setoptions(winid, {'firstline': 1})
331 call assert_equal(1, popup_getoptions(winid).firstline)
332
333 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200334endfunc
335
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200336func Test_popup_drag()
337 if !CanRunVimInTerminal()
338 throw 'Skipped: cannot make screendumps'
339 endif
340 " create a popup that covers the command line
341 let lines =<< trim END
342 call setline(1, range(1, 20))
343 let winid = popup_create(['1111', '222222', '33333'], {
344 \ 'drag': 1,
345 \ 'border': [],
346 \ 'line': &lines - 4,
347 \ })
348 func Dragit()
349 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
350 endfunc
351 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
352 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2)<CR>
353 END
354 call writefile(lines, 'XtestPopupDrag')
355 let buf = RunVimInTerminal('-S XtestPopupDrag', {'rows': 10})
356 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
357
358 call term_sendkeys(buf, ":call Dragit()\<CR>")
359 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
360
361 " clean up
362 call StopVimInTerminal(buf)
363 call delete('XtestPopupDrag')
364endfunc
365
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200366func Test_popup_with_mask()
367 if !CanRunVimInTerminal()
368 throw 'Skipped: cannot make screendumps'
369 endif
370 let lines =<< trim END
371 call setline(1, repeat([join(range(1, 40), '')], 10))
372 hi PopupColor ctermbg=lightgrey
373 let winid = popup_create([
374 \ 'some text',
375 \ 'another line',
376 \], {
377 \ 'line': 2,
378 \ 'col': 10,
379 \ 'zindex': 90,
380 \ 'padding': [],
381 \ 'highlight': 'PopupColor',
382 \ 'mask': [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
383 call popup_create([
384 \ 'xxxxxxxxx',
385 \ 'yyyyyyyyy',
386 \], {
387 \ 'line': 3,
388 \ 'col': 18,
389 \ 'zindex': 20})
390 END
391 call writefile(lines, 'XtestPopupMask')
392 let buf = RunVimInTerminal('-S XtestPopupMask', {'rows': 10})
393 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
394
395 call term_sendkeys(buf, ":call popup_move(winid, {'col': 11, 'line': 3})\<CR>")
396 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
397
398 " clean up
399 call StopVimInTerminal(buf)
400 call delete('XtestPopupMask')
401endfunc
402
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200403func Test_popup_select()
404 if !CanRunVimInTerminal()
405 throw 'Skipped: cannot make screendumps'
406 endif
Bram Moolenaar650a6372019-06-15 00:29:33 +0200407 if !has('clipboard')
408 throw 'Skipped: clipboard feature missing'
409 endif
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200410 " create a popup with some text to be selected
411 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200412 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200413 call setline(1, range(1, 20))
414 let winid = popup_create(['the word', 'some more', 'several words here'], {
415 \ 'drag': 1,
416 \ 'border': [],
417 \ 'line': 3,
418 \ 'col': 10,
419 \ })
420 func Select1()
421 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
422 endfunc
423 map <silent> <F3> :call test_setmouse(4, 15)<CR>
424 map <silent> <F4> :call test_setmouse(6, 23)<CR>
425 END
426 call writefile(lines, 'XtestPopupSelect')
427 let buf = RunVimInTerminal('-S XtestPopupSelect', {'rows': 10})
428 call term_sendkeys(buf, ":call Select1()\<CR>")
429 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
430
431 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
432 call term_sendkeys(buf, "\"*p")
433 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
434
435 " clean up
436 call StopVimInTerminal(buf)
437 call delete('XtestPopupSelect')
438endfunc
439
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200440func Test_popup_in_tab()
441 " default popup is local to tab, not visible when in other tab
442 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200443 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200444 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200445 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200446 tabnew
447 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200448 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200449 quit
450 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200451
452 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200453 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200454 " buffer is gone now
455 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200456
457 " global popup is visible in any tab
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200458 let winid = popup_create("text", {'tabpage': -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200459 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200460 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200461 tabnew
462 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200463 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200464 quit
465 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200466 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200467
468 " create popup in other tab
469 tabnew
470 let winid = popup_create("text", {'tabpage': 1})
471 call assert_equal(0, popup_getpos(winid).visible)
472 call assert_equal(1, popup_getoptions(winid).tabpage)
473 quit
474 call assert_equal(1, popup_getpos(winid).visible)
475 call assert_equal(0, popup_getoptions(winid).tabpage)
476 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200477endfunc
478
479func Test_popup_valid_arguments()
480 " Zero value is like the property wasn't there
481 let winid = popup_create("text", {"col": 0})
482 let pos = popup_getpos(winid)
483 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200484 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200485
486 " using cursor column has minimum value of 1
487 let winid = popup_create("text", {"col": 'cursor-100'})
488 let pos = popup_getpos(winid)
489 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200490 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200491
492 " center
493 let winid = popup_create("text", {"pos": 'center'})
494 let pos = popup_getpos(winid)
495 let around = (&columns - pos.width) / 2
496 call assert_inrange(around - 1, around + 1, pos.col)
497 let around = (&lines - pos.height) / 2
498 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200499 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200500endfunc
501
502func Test_popup_invalid_arguments()
503 call assert_fails('call popup_create(666, {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200504 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200505 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200506 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200507
508 call assert_fails('call popup_create("text", {"col": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200509 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200510 call assert_fails('call popup_create("text", {"col": "cursor8"})', 'E15:')
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": "cursor+x"})', '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+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200515 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200516
517 call assert_fails('call popup_create("text", {"line": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200518 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200519 call assert_fails('call popup_create("text", {"line": "cursor8"})', 'E15:')
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": "cursor+x"})', '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+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200524 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200525
526 call assert_fails('call popup_create("text", {"pos": "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200527 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200528 call assert_fails('call popup_create("text", {"padding": "none"})', 'E714:')
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", {"border": "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", {"borderhighlight": "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", {"borderchars": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200535 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200536
537 call assert_fails('call popup_create([{"text": "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200538 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200539 call assert_fails('call popup_create([{"text": "text", "props": "none"}], {})', 'E714:')
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"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200542 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200543endfunc
544
Bram Moolenaareea16992019-05-31 17:34:48 +0200545func Test_win_execute_closing_curwin()
546 split
547 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200548 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200549 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200550endfunc
551
552func Test_win_execute_not_allowed()
553 let winid = popup_create('some text', {})
554 call assert_fails('call win_execute(winid, "split")', 'E994:')
555 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
556 call assert_fails('call win_execute(winid, "close")', 'E994:')
557 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200558 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200559 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
560 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
561 call assert_fails('call win_execute(winid, "next")', 'E994:')
562 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
563 call assert_fails('call win_execute(winid, "buf")', 'E994:')
564 call assert_fails('call win_execute(winid, "edit")', 'E994:')
565 call assert_fails('call win_execute(winid, "enew")', 'E994:')
566 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
567 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
568 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
569 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200570 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200571endfunc
572
Bram Moolenaar402502d2019-05-30 22:07:36 +0200573func Test_popup_with_wrap()
574 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200575 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200576 endif
577 let lines =<< trim END
578 call setline(1, range(1, 100))
579 let winid = popup_create(
580 \ 'a long line that wont fit',
581 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
582 END
583 call writefile(lines, 'XtestPopup')
584 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
585 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
586
587 " clean up
588 call StopVimInTerminal(buf)
589 call delete('XtestPopup')
590endfunc
591
592func Test_popup_without_wrap()
593 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200594 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200595 endif
596 let lines =<< trim END
597 call setline(1, range(1, 100))
598 let winid = popup_create(
599 \ 'a long line that wont fit',
600 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
601 END
602 call writefile(lines, 'XtestPopup')
603 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
604 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
605
606 " clean up
607 call StopVimInTerminal(buf)
608 call delete('XtestPopup')
609endfunc
610
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200611func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200612 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +0200613 throw 'Skipped: timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200614 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200615 topleft vnew
616 call setline(1, 'hello')
617
618 call popup_create('world', {
619 \ 'line': 1,
620 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200621 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200622 \ 'time': 500,
623 \})
624 redraw
625 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
626 call assert_equal('world', line)
627
628 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200629 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200630 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
631 call assert_equal('hello', line)
632
633 call popup_create('on the command line', {
634 \ 'line': &lines,
635 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200636 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200637 \ 'time': 500,
638 \})
639 redraw
640 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
641 call assert_match('.*on the command line.*', line)
642
643 sleep 700m
644 redraw
645 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
646 call assert_notmatch('.*on the command line.*', line)
647
648 bwipe!
649endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200650
651func Test_popup_hide()
652 topleft vnew
653 call setline(1, 'hello')
654
655 let winid = popup_create('world', {
656 \ 'line': 1,
657 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200658 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200659 \})
660 redraw
661 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
662 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200663 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200664 " buffer is still listed and active
665 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200666
667 call popup_hide(winid)
668 redraw
669 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
670 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200671 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200672 " buffer is still listed but hidden
673 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200674
675 call popup_show(winid)
676 redraw
677 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
678 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200679 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200680
681
682 call popup_close(winid)
683 redraw
684 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
685 call assert_equal('hello', line)
686
687 " error is given for existing non-popup window
688 call assert_fails('call popup_hide(win_getid())', 'E993:')
689
690 " no error non-existing window
691 call popup_hide(1234234)
692 call popup_show(41234234)
693
694 bwipe!
695endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200696
697func Test_popup_move()
698 topleft vnew
699 call setline(1, 'hello')
700
701 let winid = popup_create('world', {
702 \ 'line': 1,
703 \ 'col': 1,
704 \ 'minwidth': 20,
705 \})
706 redraw
707 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
708 call assert_equal('world ', line)
709
710 call popup_move(winid, {'line': 2, 'col': 2})
711 redraw
712 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
713 call assert_equal('hello ', line)
714 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
715 call assert_equal('~world', line)
716
717 call popup_move(winid, {'line': 1})
718 redraw
719 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
720 call assert_equal('hworld', line)
721
722 call popup_close(winid)
723
724 bwipe!
725endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200726
Bram Moolenaar402502d2019-05-30 22:07:36 +0200727func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200728 let winid = popup_create('hello', {
729 \ 'line': 2,
730 \ 'col': 3,
731 \ 'minwidth': 10,
732 \ 'minheight': 11,
733 \})
734 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200735 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200736 call assert_equal(2, res.line)
737 call assert_equal(3, res.col)
738 call assert_equal(10, res.width)
739 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200740 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200741
742 call popup_close(winid)
743endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200744
745func Test_popup_width_longest()
746 let tests = [
747 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
748 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
749 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
750 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
751 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
752 \ ]
753
754 for test in tests
755 let winid = popup_create(test[0], {'line': 2, 'col': 3})
756 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200757 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200758 call assert_equal(test[1], position.width)
759 call popup_close(winid)
760 endfor
761endfunc
762
763func Test_popup_wraps()
764 let tests = [
765 \ ['nowrap', 6, 1],
766 \ ['a line that wraps once', 12, 2],
767 \ ['a line that wraps two times', 12, 3],
768 \ ]
769 for test in tests
770 let winid = popup_create(test[0],
771 \ {'line': 2, 'col': 3, 'maxwidth': 12})
772 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200773 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200774 call assert_equal(test[1], position.width)
775 call assert_equal(test[2], position.height)
776
777 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200778 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200779 endfor
780endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200781
782func Test_popup_getoptions()
783 let winid = popup_create('hello', {
784 \ 'line': 2,
785 \ 'col': 3,
786 \ 'minwidth': 10,
787 \ 'minheight': 11,
788 \ 'maxwidth': 20,
789 \ 'maxheight': 21,
790 \ 'zindex': 100,
791 \ 'time': 5000,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200792 \ 'fixed': 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200793 \})
794 redraw
795 let res = popup_getoptions(winid)
796 call assert_equal(2, res.line)
797 call assert_equal(3, res.col)
798 call assert_equal(10, res.minwidth)
799 call assert_equal(11, res.minheight)
800 call assert_equal(20, res.maxwidth)
801 call assert_equal(21, res.maxheight)
802 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200803 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200804 if has('timers')
805 call assert_equal(5000, res.time)
806 endif
807 call popup_close(winid)
808
809 let winid = popup_create('hello', {})
810 redraw
811 let res = popup_getoptions(winid)
812 call assert_equal(0, res.line)
813 call assert_equal(0, res.col)
814 call assert_equal(0, res.minwidth)
815 call assert_equal(0, res.minheight)
816 call assert_equal(0, res.maxwidth)
817 call assert_equal(0, res.maxheight)
818 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200819 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200820 if has('timers')
821 call assert_equal(0, res.time)
822 endif
823 call popup_close(winid)
824 call assert_equal({}, popup_getoptions(winid))
825endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200826
827func Test_popup_option_values()
828 new
829 " window-local
830 setlocal number
831 setlocal nowrap
832 " buffer-local
833 setlocal omnifunc=Something
834 " global/buffer-local
835 setlocal path=/there
836 " global/window-local
837 setlocal scrolloff=9
838
839 let winid = popup_create('hello', {})
840 call assert_equal(0, getwinvar(winid, '&number'))
841 call assert_equal(1, getwinvar(winid, '&wrap'))
842 call assert_equal('', getwinvar(winid, '&omnifunc'))
843 call assert_equal(&g:path, getwinvar(winid, '&path'))
844 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
845
846 call popup_close(winid)
847 bwipe
848endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200849
850func Test_popup_atcursor()
851 topleft vnew
852 call setline(1, [
853 \ 'xxxxxxxxxxxxxxxxx',
854 \ 'xxxxxxxxxxxxxxxxx',
855 \ 'xxxxxxxxxxxxxxxxx',
856 \])
857
858 call cursor(2, 2)
859 redraw
860 let winid = popup_atcursor('vim', {})
861 redraw
862 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
863 call assert_equal('xvimxxxxxxxxxxxxx', line)
864 call popup_close(winid)
865
866 call cursor(3, 4)
867 redraw
868 let winid = popup_atcursor('vim', {})
869 redraw
870 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
871 call assert_equal('xxxvimxxxxxxxxxxx', line)
872 call popup_close(winid)
873
874 call cursor(1, 1)
875 redraw
876 let winid = popup_create('vim', {
877 \ 'line': 'cursor+2',
878 \ 'col': 'cursor+1',
879 \})
880 redraw
881 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
882 call assert_equal('xvimxxxxxxxxxxxxx', line)
883 call popup_close(winid)
884
885 call cursor(3, 3)
886 redraw
887 let winid = popup_create('vim', {
888 \ 'line': 'cursor-2',
889 \ 'col': 'cursor-1',
890 \})
891 redraw
892 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
893 call assert_equal('xvimxxxxxxxxxxxxx', line)
894 call popup_close(winid)
895
Bram Moolenaar402502d2019-05-30 22:07:36 +0200896 " just enough room above
897 call cursor(3, 3)
898 redraw
899 let winid = popup_atcursor(['vim', 'is great'], {})
900 redraw
901 let pos = popup_getpos(winid)
902 call assert_equal(1, pos.line)
903 call popup_close(winid)
904
905 " not enough room above, popup goes below the cursor
906 call cursor(3, 3)
907 redraw
908 let winid = popup_atcursor(['vim', 'is', 'great'], {})
909 redraw
910 let pos = popup_getpos(winid)
911 call assert_equal(4, pos.line)
912 call popup_close(winid)
913
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200914 " cursor in first line, popup in line 2
915 call cursor(1, 1)
916 redraw
917 let winid = popup_atcursor(['vim', 'is', 'great'], {})
918 redraw
919 let pos = popup_getpos(winid)
920 call assert_equal(2, pos.line)
921 call popup_close(winid)
922
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200923 bwipe!
924endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200925
926func Test_popup_filter()
927 new
928 call setline(1, 'some text')
929
930 func MyPopupFilter(winid, c)
931 if a:c == 'e'
932 let g:eaten = 'e'
933 return 1
934 endif
935 if a:c == '0'
936 let g:ignored = '0'
937 return 0
938 endif
939 if a:c == 'x'
940 call popup_close(a:winid)
941 return 1
942 endif
943 return 0
944 endfunc
945
946 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
947 redraw
948
949 " e is consumed by the filter
950 call feedkeys('e', 'xt')
951 call assert_equal('e', g:eaten)
952
953 " 0 is ignored by the filter
954 normal $
955 call assert_equal(9, getcurpos()[2])
956 call feedkeys('0', 'xt')
957 call assert_equal('0', g:ignored)
958 call assert_equal(1, getcurpos()[2])
959
960 " x closes the popup
961 call feedkeys('x', 'xt')
962 call assert_equal('e', g:eaten)
963 call assert_equal(-1, winbufnr(winid))
964
965 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200966 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200967endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200968
Bram Moolenaara42d9452019-06-15 21:46:30 +0200969func ShowDialog(key, result)
970 let s:cb_res = 999
971 let winid = popup_dialog('do you want to quit (Yes/no)?', {
972 \ 'filter': 'popup_filter_yesno',
973 \ 'callback': 'QuitCallback',
974 \ })
975 redraw
976 call feedkeys(a:key, "xt")
977 call assert_equal(winid, s:cb_winid)
978 call assert_equal(a:result, s:cb_res)
979endfunc
980
981func Test_popup_dialog()
982 func QuitCallback(id, res)
983 let s:cb_winid = a:id
984 let s:cb_res = a:res
985 endfunc
986
987 let winid = ShowDialog("y", 1)
988 let winid = ShowDialog("Y", 1)
989 let winid = ShowDialog("n", 0)
990 let winid = ShowDialog("N", 0)
991 let winid = ShowDialog("x", 0)
992 let winid = ShowDialog("X", 0)
993 let winid = ShowDialog("\<Esc>", 0)
994 let winid = ShowDialog("\<C-C>", -1)
995
996 delfunc QuitCallback
997endfunc
998
Bram Moolenaara730e552019-06-16 19:05:31 +0200999func ShowMenu(key, result)
1000 let s:cb_res = 999
1001 let winid = popup_menu(['one', 'two', 'something else'], {
1002 \ 'callback': 'QuitCallback',
1003 \ })
1004 redraw
1005 call feedkeys(a:key, "xt")
1006 call assert_equal(winid, s:cb_winid)
1007 call assert_equal(a:result, s:cb_res)
1008endfunc
1009
1010func Test_popup_menu()
1011 func QuitCallback(id, res)
1012 let s:cb_winid = a:id
1013 let s:cb_res = a:res
1014 endfunc
1015
1016 let winid = ShowMenu(" ", 1)
1017 let winid = ShowMenu("j \<CR>", 2)
1018 let winid = ShowMenu("JjK \<CR>", 2)
1019 let winid = ShowMenu("jjjjjj ", 3)
1020 let winid = ShowMenu("kkk ", 1)
1021 let winid = ShowMenu("x", -1)
1022 let winid = ShowMenu("X", -1)
1023 let winid = ShowMenu("\<Esc>", -1)
1024 let winid = ShowMenu("\<C-C>", -1)
1025
1026 delfunc QuitCallback
1027endfunc
1028
1029func Test_popup_menu_screenshot()
1030 if !CanRunVimInTerminal()
1031 throw 'Skipped: cannot make screendumps'
1032 endif
1033
1034 let lines =<< trim END
1035 call setline(1, range(1, 20))
1036 hi PopupSelected ctermbg=lightblue
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001037 call popup_menu(['one', 'two', 'another'], {'callback': 'MenuDone', 'title': ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001038 func MenuDone(id, res)
1039 echomsg "selected " .. a:res
1040 endfunc
1041 END
1042 call writefile(lines, 'XtestPopupMenu')
1043 let buf = RunVimInTerminal('-S XtestPopupMenu', {'rows': 10})
1044 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1045
1046 call term_sendkeys(buf, "jj")
1047 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1048
1049 call term_sendkeys(buf, " ")
1050 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1051
1052 " clean up
1053 call StopVimInTerminal(buf)
1054 call delete('XtestPopupMenu')
1055endfunc
1056
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001057func Test_popup_title()
1058 if !CanRunVimInTerminal()
1059 throw 'Skipped: cannot make screendumps'
1060 endif
1061
1062 " Create a popup without title or border, a line of padding will be added to
1063 " put the title on.
1064 let lines =<< trim END
1065 call setline(1, range(1, 20))
1066 call popup_create(['one', 'two', 'another'], {'title': 'Title String'})
1067 END
1068 call writefile(lines, 'XtestPopupTitle')
1069 let buf = RunVimInTerminal('-S XtestPopupTitle', {'rows': 10})
1070 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1071
1072 " clean up
1073 call StopVimInTerminal(buf)
1074 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001075
1076 let winid = popup_create('something', {'title': 'Some Title'})
1077 call assert_equal('Some Title', popup_getoptions(winid).title)
1078 call popup_setoptions(winid, {'title': 'Another Title'})
1079 call assert_equal('Another Title', popup_getoptions(winid).title)
1080
1081 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001082endfunc
1083
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001084func Test_popup_close_callback()
1085 func PopupDone(id, result)
1086 let g:result = a:result
1087 endfunc
1088 let winid = popup_create('something', {'callback': 'PopupDone'})
1089 redraw
1090 call popup_close(winid, 'done')
1091 call assert_equal('done', g:result)
1092endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001093
1094func Test_popup_empty()
1095 let winid = popup_create('', {'padding': [2,2,2,2]})
1096 redraw
1097 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001098 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001099 call assert_equal(5, pos.height)
1100
1101 let winid = popup_create([], {'border': []})
1102 redraw
1103 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001104 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001105 call assert_equal(3, pos.height)
1106endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001107
1108func Test_popup_never_behind()
1109 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001110 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +02001111 endif
1112 " +-----------------------------+
1113 " | | |
1114 " | | |
1115 " | | |
1116 " | line1 |
1117 " |------------line2------------|
1118 " | line3 |
1119 " | line4 |
1120 " | |
1121 " | |
1122 " +-----------------------------+
1123 let lines =<< trim END
1124 only
1125 split
1126 vsplit
1127 let info_window1 = getwininfo()[0]
1128 let line = info_window1['height']
1129 let col = info_window1['width']
1130 call popup_create(['line1', 'line2', 'line3', 'line4'], {
1131 \ 'line' : line,
1132 \ 'col' : col,
1133 \ })
1134 END
1135 call writefile(lines, 'XtestPopupBehind')
1136 let buf = RunVimInTerminal('-S XtestPopupBehind', {'rows': 10})
1137 call term_sendkeys(buf, "\<C-W>w")
1138 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1139
1140 " clean up
1141 call StopVimInTerminal(buf)
1142 call delete('XtestPopupBehind')
1143endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001144
1145func s:VerifyPosition( p, msg, line, col, width, height )
1146 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
1147 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
1148 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
1149 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
1150endfunc
1151
1152func Test_popup_position_adjust()
1153 " Anything placed past 2 cells from of the right of the screen is moved to the
1154 " left.
1155 "
1156 " When wrapping is disabled, we also shift to the left to display on the
1157 " screen, unless fixed is set.
1158
1159 " Entries for cases which don't vary based on wrapping.
1160 " Format is per tests described below
1161 let both_wrap_tests = [
1162 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
1163 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
1164 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
1165 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
1166 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
1167 \
1168 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
1169 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
1170 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
1171 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
1172 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
1173 \
1174 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1175 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1176 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1177 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1178 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
1179 \ ]
1180
1181 " these test groups are dicts with:
1182 " - comment: something to identify the group of tests by
1183 " - options: dict of options to merge with the row/col in tests
1184 " - tests: list of cases. Each one is a list with elements:
1185 " - text
1186 " - row
1187 " - col
1188 " - expected row
1189 " - expected col
1190 " - expected width
1191 " - expected height
1192 let tests = [
1193 \ {
1194 \ 'comment': 'left-aligned with wrapping',
1195 \ 'options': {
1196 \ 'wrap': 1,
1197 \ 'pos': 'botleft',
1198 \ },
1199 \ 'tests': both_wrap_tests + [
1200 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
1201 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
1202 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
1203 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
1204 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1205 \ ],
1206 \ },
1207 \ {
1208 \ 'comment': 'left aligned without wrapping',
1209 \ 'options': {
1210 \ 'wrap': 0,
1211 \ 'pos': 'botleft',
1212 \ },
1213 \ 'tests': both_wrap_tests + [
1214 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
1215 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
1216 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
1217 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
1218 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1219 \ ],
1220 \ },
1221 \ {
1222 \ 'comment': 'left aligned with fixed position',
1223 \ 'options': {
1224 \ 'wrap': 0,
1225 \ 'fixed': 1,
1226 \ 'pos': 'botleft',
1227 \ },
1228 \ 'tests': both_wrap_tests + [
1229 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1230 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1231 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1232 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1233 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1234 \ ],
1235 \ },
1236 \ ]
1237
1238 for test_group in tests
1239 for test in test_group.tests
1240 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
1241 let options = {
1242 \ 'line': line,
1243 \ 'col': col,
1244 \ }
1245 call extend( options, test_group.options )
1246
1247 let p = popup_create( text, options )
1248
1249 let msg = string( extend( options, { 'text': text } ) )
1250 call s:VerifyPosition( p, msg, e_line, e_col, e_width, e_height )
1251 call popup_close( p )
1252 endfor
1253 endfor
1254
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001255 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001256 %bwipe!
1257endfunc
1258
Bram Moolenaar3397f742019-06-02 18:40:06 +02001259func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001260 " width of screen
1261 let X = join(map(range(&columns), {->'X'}), '')
1262
1263 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1264 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
1265
1266 redraw
1267 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1268 call assert_equal(X, line)
1269
1270 call popup_close( p )
1271 redraw
1272
1273 " Same if placed on the right hand side
1274 let p = popup_create( X, { 'line': 1, 'col': &columns, 'wrap': 0 } )
1275 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
1276
1277 redraw
1278 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1279 call assert_equal(X, line)
1280
1281 call popup_close( p )
1282 redraw
1283
1284 " Extend so > window width
1285 let X .= 'x'
1286
1287 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1288 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
1289
1290 redraw
1291 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1292 call assert_equal(X[ : -2 ], line)
1293
1294 call popup_close( p )
1295 redraw
1296
1297 " Shifted then truncated (the x is not visible)
1298 let p = popup_create( X, { 'line': 1, 'col': &columns - 3, 'wrap': 0 } )
1299 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
1300
1301 redraw
1302 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1303 call assert_equal(X[ : -2 ], line)
1304
1305 call popup_close( p )
1306 redraw
1307
1308 " Not shifted, just truncated
1309 let p = popup_create( X,
1310 \ { 'line': 1, 'col': 2, 'wrap': 0, 'fixed': 1 } )
1311 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
1312
1313 redraw
1314 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1315 let e_line = ' ' . X[ 1 : -2 ]
1316 call assert_equal(e_line, line)
1317
1318 call popup_close( p )
1319 redraw
1320
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001321 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001322 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001323endfunc
1324
1325func Test_popup_moved()
1326 new
1327 call test_override('char_avail', 1)
1328 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1329
1330 exe "normal gg0/word\<CR>"
1331 let winid = popup_atcursor('text', {'moved': 'any'})
1332 redraw
1333 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001334 call assert_equal([4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001335 " trigger the check for last_cursormoved by going into insert mode
1336 call feedkeys("li\<Esc>", 'xt')
1337 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001338 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001339
1340 exe "normal gg0/word\<CR>"
1341 let winid = popup_atcursor('text', {'moved': 'word'})
1342 redraw
1343 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001344 call assert_equal([4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001345 call feedkeys("hi\<Esc>", 'xt')
1346 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001347 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001348
1349 exe "normal gg0/word\<CR>"
1350 let winid = popup_atcursor('text', {'moved': 'word'})
1351 redraw
1352 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001353 call assert_equal([4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001354 call feedkeys("li\<Esc>", 'xt')
1355 call assert_equal(1, popup_getpos(winid).visible)
1356 call feedkeys("ei\<Esc>", 'xt')
1357 call assert_equal(1, popup_getpos(winid).visible)
1358 call feedkeys("eli\<Esc>", 'xt')
1359 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001360 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001361
Bram Moolenaar17627312019-06-02 19:53:44 +02001362 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001363 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001364 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001365 redraw
1366 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001367 call assert_equal([2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001368 call feedkeys("eli\<Esc>", 'xt')
1369 call assert_equal(1, popup_getpos(winid).visible)
1370 call feedkeys("wi\<Esc>", 'xt')
1371 call assert_equal(1, popup_getpos(winid).visible)
1372 call feedkeys("Eli\<Esc>", 'xt')
1373 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001374 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001375
1376 exe "normal gg0/word\<CR>"
1377 let winid = popup_atcursor('text', {'moved': [5, 10]})
1378 redraw
1379 call assert_equal(1, popup_getpos(winid).visible)
1380 call feedkeys("eli\<Esc>", 'xt')
1381 call feedkeys("ei\<Esc>", 'xt')
1382 call assert_equal(1, popup_getpos(winid).visible)
1383 call feedkeys("eli\<Esc>", 'xt')
1384 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001385 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001386
1387 bwipe!
1388 call test_override('ALL', 0)
1389endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001390
1391func Test_notifications()
1392 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02001393 throw 'Skipped: timer feature not supported'
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001394 endif
1395 if !CanRunVimInTerminal()
1396 throw 'Skipped: cannot make screendumps'
1397 endif
1398
1399 call writefile([
1400 \ "call setline(1, range(1, 20))",
1401 \ "hi Notification ctermbg=lightblue",
1402 \ "call popup_notification('first notification', {})",
1403 \], 'XtestNotifications')
1404 let buf = RunVimInTerminal('-S XtestNotifications', {'rows': 10})
1405 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1406
1407 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001408 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1409 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001410 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1411
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001412 " clean up
1413 call StopVimInTerminal(buf)
1414 call delete('XtestNotifications')
1415endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001416
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001417func Test_popup_scrollbar()
1418 if !CanRunVimInTerminal()
1419 throw 'Skipped: cannot make screendumps'
1420 endif
1421
1422 let lines =<< trim END
1423 call setline(1, range(1, 20))
1424 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
1425 \ 'six', 'seven', 'eight', 'nine'], {
1426 \ 'minwidth': 8,
1427 \ 'maxheight': 4,
1428 \ })
1429 END
1430 call writefile(lines, 'XtestPopupScroll')
1431 let buf = RunVimInTerminal('-S XtestPopupScroll', {'rows': 10})
1432 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1433
1434 call term_sendkeys(buf, ":call popup_setoptions(winid, {'firstline': 2})\<CR>")
1435 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1436
1437 call term_sendkeys(buf, ":call popup_setoptions(winid, {'firstline': 6})\<CR>")
1438 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1439
1440 call term_sendkeys(buf, ":call popup_setoptions(winid, {'firstline': 9})\<CR>")
1441 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1442
1443 " clean up
1444 call StopVimInTerminal(buf)
1445 call delete('XtestPopupScroll')
1446endfunc
1447
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001448func Test_popup_settext()
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001449 if !CanRunVimInTerminal()
1450 throw 'Skipped: cannot make screendumps'
1451 endif
1452
1453 let lines =<< trim END
1454 let opts = {'wrap': 0}
1455 let p = popup_create('test', opts)
1456 call popup_settext(p, 'this is a text')
1457 END
1458
1459 call writefile( lines, 'XtestPopupSetText' )
1460 let buf = RunVimInTerminal('-S XtestPopupSetText', {'rows': 10})
1461 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1462
1463 " Setting to empty string clears it
1464 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1465 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1466
1467 " Setting a list
1468 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1469 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1470
1471 " Shrinking with a list
1472 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1473 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1474
1475 " Growing with a list
1476 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1477 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1478
1479 " Empty list clears
1480 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1481 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1482
1483 " Dicts
1484 call term_sendkeys(buf, ":call popup_settext(p, [{'text': 'aaaa'}, {'text': 'bbbb'}, {'text': 'cccc'}])\<CR>")
1485 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1486
1487 " clean up
1488 call StopVimInTerminal(buf)
1489 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001490endfunc
1491
1492func Test_popup_hidden()
1493 new
1494
1495 let winid = popup_atcursor('text', {'hidden': 1})
1496 redraw
1497 call assert_equal(0, popup_getpos(winid).visible)
1498 call popup_close(winid)
1499
1500 let winid = popup_create('text', {'hidden': 1})
1501 redraw
1502 call assert_equal(0, popup_getpos(winid).visible)
1503 call popup_close(winid)
1504
1505 func QuitCallback(id, res)
1506 let s:cb_winid = a:id
1507 let s:cb_res = a:res
1508 endfunc
1509 let winid = popup_dialog('make a choice', {'hidden': 1,
1510 \ 'filter': 'popup_filter_yesno',
1511 \ 'callback': 'QuitCallback',
1512 \ })
1513 redraw
1514 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001515 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1516 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001517 exe "normal anot used by filter\<Esc>"
1518 call assert_equal('not used by filter', getline(1))
1519
1520 call popup_show(winid)
1521 call feedkeys('y', "xt")
1522 call assert_equal(1, s:cb_res)
1523
1524 bwipe!
1525 delfunc QuitCallback
1526endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001527
1528" Test options not checked elsewhere
1529func Test_set_get_options()
1530 let winid = popup_create('some text', {'highlight': 'Beautiful'})
1531 let options = popup_getoptions(winid)
1532 call assert_equal(1, options.wrap)
1533 call assert_equal(0, options.drag)
1534 call assert_equal('Beautiful', options.highlight)
1535
1536 call popup_setoptions(winid, {'wrap': 0, 'drag': 1, 'highlight': 'Another'})
1537 let options = popup_getoptions(winid)
1538 call assert_equal(0, options.wrap)
1539 call assert_equal(1, options.drag)
1540 call assert_equal('Another', options.highlight)
1541
1542 call popup_close(winid)
1543endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001544
1545func Test_popupwin_garbage_collect()
1546 func MyPopupFilter(x, winid, c)
1547 " NOP
1548 endfunc
1549
1550 let winid = popup_create('something', {'filter': function('MyPopupFilter', [{}])})
1551 call test_garbagecollect_now()
1552 redraw
1553 " Must not crach caused by invalid memory access
1554 call feedkeys('j', 'xt')
1555 call assert_true(v:true)
1556
1557 call popup_close(winid)
1558 delfunc MyPopupFilter
1559endfunc