blob: b4bfcef415e4944f4f3e3adeb881c6c6c384185c [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,
137 \ 'visible': 1}
138 let winid = popup_create('hello border', {'line': 2, 'col': 3, 'border': []})",
139 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200140 let options = popup_getoptions(winid)
141 call assert_equal([], options.border)
142 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200143
Bram Moolenaarae943152019-06-16 22:54:14 +0200144 let winid = popup_create('hello padding', {'line': 2, 'col': 3, 'padding': []})
145 let with_border_or_padding.width = 15
146 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200147 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200148 let options = popup_getoptions(winid)
149 call assert_false(has_key(options, "border"))
150 call assert_equal([], options.padding)
151
152 call popup_setoptions(winid, {
153 \ 'padding': [1, 2, 3, 4],
154 \ 'border': [4, 0, 7, 8],
155 \ 'borderhighlight': ['Top', 'Right', 'Bottom', 'Left'],
156 \ 'borderchars': ['1', '^', '2', '>', '3', 'v', '4', '<'],
157 \ })
158 let options = popup_getoptions(winid)
159 call assert_equal([1, 0, 1, 1], options.border)
160 call assert_equal([1, 2, 3, 4], options.padding)
161 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
162 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200163
164 let winid = popup_create('hello both', {'line': 3, 'col': 8, 'border': [], 'padding': []})
165 call assert_equal({
166 \ 'line': 3,
167 \ 'core_line': 5,
168 \ 'col': 8,
169 \ 'core_col': 10,
170 \ 'width': 14,
171 \ 'core_width': 10,
172 \ 'height': 5,
173 \ 'core_height': 1,
174 \ 'visible': 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200175
176 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200177endfunc
178
Bram Moolenaarb4230122019-05-30 18:40:53 +0200179func Test_popup_with_syntax_win_execute()
180 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200181 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200182 endif
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200183 let lines =<< trim END
184 call setline(1, range(1, 100))
185 hi PopupColor ctermbg=lightblue
186 let winid = popup_create([
187 \ '#include <stdio.h>',
188 \ 'int main(void)',
189 \ '{',
190 \ ' printf(123);',
191 \ '}',
192 \], {'line': 3, 'col': 25, 'highlight': 'PopupColor'})
193 call win_execute(winid, 'set syntax=cpp')
194 END
195 call writefile(lines, 'XtestPopup')
Bram Moolenaarb4230122019-05-30 18:40:53 +0200196 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
197 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
198
199 " clean up
200 call StopVimInTerminal(buf)
201 call delete('XtestPopup')
202endfunc
203
204func Test_popup_with_syntax_setbufvar()
205 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200206 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200207 endif
Bram Moolenaar402502d2019-05-30 22:07:36 +0200208 let lines =<< trim END
209 call setline(1, range(1, 100))
210 hi PopupColor ctermbg=lightgrey
211 let winid = popup_create([
212 \ '#include <stdio.h>',
213 \ 'int main(void)',
214 \ '{',
215 \ ' printf(567);',
216 \ '}',
217 \], {'line': 3, 'col': 21, 'highlight': 'PopupColor'})
218 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
219 END
220 call writefile(lines, 'XtestPopup')
Bram Moolenaarb4230122019-05-30 18:40:53 +0200221 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
222 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
223
224 " clean up
225 call StopVimInTerminal(buf)
226 call delete('XtestPopup')
227endfunc
228
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200229func Test_popup_with_matches()
230 if !CanRunVimInTerminal()
231 throw 'Skipped: cannot make screendumps'
232 endif
233 let lines =<< trim END
234 call setline(1, ['111 222 333', '444 555 666'])
235 let winid = popup_create([
236 \ '111 222 333',
237 \ '444 555 666',
238 \], {'line': 3, 'col': 10, 'border': []})
239 set hlsearch
240 /666
241 call matchadd('ErrorMsg', '111')
242 call matchadd('ErrorMsg', '444')
243 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
244 call win_execute(winid, "call matchadd('ErrorMsg', '555')")
245 END
246 call writefile(lines, 'XtestPopupMatches')
247 let buf = RunVimInTerminal('-S XtestPopupMatches', {'rows': 10})
248 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
249
250 " clean up
251 call StopVimInTerminal(buf)
252 call delete('XtestPopupMatches')
253endfunc
254
Bram Moolenaar399d8982019-06-02 15:34:29 +0200255func Test_popup_all_corners()
256 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200257 throw 'Skipped: cannot make screendumps'
Bram Moolenaar399d8982019-06-02 15:34:29 +0200258 endif
259 let lines =<< trim END
260 call setline(1, repeat([repeat('-', 60)], 15))
261 set so=0
262 normal 2G3|r#
263 let winid1 = popup_create(['first', 'second'], {
264 \ 'line': 'cursor+1',
265 \ 'col': 'cursor',
266 \ 'pos': 'topleft',
267 \ 'border': [],
268 \ 'padding': [],
269 \ })
270 normal 25|r@
271 let winid1 = popup_create(['First', 'SeconD'], {
272 \ 'line': 'cursor+1',
273 \ 'col': 'cursor',
274 \ 'pos': 'topright',
275 \ 'border': [],
276 \ 'padding': [],
277 \ })
278 normal 9G29|r%
279 let winid1 = popup_create(['fiRSt', 'seCOnd'], {
280 \ 'line': 'cursor-1',
281 \ 'col': 'cursor',
282 \ 'pos': 'botleft',
283 \ 'border': [],
284 \ 'padding': [],
285 \ })
286 normal 51|r&
287 let winid1 = popup_create(['FIrsT', 'SEcoND'], {
288 \ 'line': 'cursor-1',
289 \ 'col': 'cursor',
290 \ 'pos': 'botright',
291 \ 'border': [],
292 \ 'padding': [],
293 \ })
294 END
295 call writefile(lines, 'XtestPopupCorners')
296 let buf = RunVimInTerminal('-S XtestPopupCorners', {'rows': 12})
297 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
298
299 " clean up
300 call StopVimInTerminal(buf)
301 call delete('XtestPopupCorners')
302endfunc
303
Bram Moolenaar8d241042019-06-12 23:40:01 +0200304func Test_popup_firstline()
305 if !CanRunVimInTerminal()
306 throw 'Skipped: cannot make screendumps'
307 endif
308 let lines =<< trim END
309 call setline(1, range(1, 20))
310 call popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], {
311 \ 'maxheight': 4,
312 \ 'firstline': 3,
313 \ })
314 END
315 call writefile(lines, 'XtestPopupFirstline')
316 let buf = RunVimInTerminal('-S XtestPopupFirstline', {'rows': 10})
317 call VerifyScreenDump(buf, 'Test_popupwin_firstline', {})
318
319 " clean up
320 call StopVimInTerminal(buf)
321 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200322
323 let winid = popup_create(['1111', '222222', '33333', '44444'], {
324 \ 'maxheight': 2,
325 \ 'firstline': 3,
326 \ })
327 call assert_equal(3, popup_getoptions(winid).firstline)
328 call popup_setoptions(winid, {'firstline': 1})
329 call assert_equal(1, popup_getoptions(winid).firstline)
330
331 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200332endfunc
333
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200334func Test_popup_drag()
335 if !CanRunVimInTerminal()
336 throw 'Skipped: cannot make screendumps'
337 endif
338 " create a popup that covers the command line
339 let lines =<< trim END
340 call setline(1, range(1, 20))
341 let winid = popup_create(['1111', '222222', '33333'], {
342 \ 'drag': 1,
343 \ 'border': [],
344 \ 'line': &lines - 4,
345 \ })
346 func Dragit()
347 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
348 endfunc
349 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
350 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2)<CR>
351 END
352 call writefile(lines, 'XtestPopupDrag')
353 let buf = RunVimInTerminal('-S XtestPopupDrag', {'rows': 10})
354 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
355
356 call term_sendkeys(buf, ":call Dragit()\<CR>")
357 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
358
359 " clean up
360 call StopVimInTerminal(buf)
361 call delete('XtestPopupDrag')
362endfunc
363
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200364func Test_popup_with_mask()
365 if !CanRunVimInTerminal()
366 throw 'Skipped: cannot make screendumps'
367 endif
368 let lines =<< trim END
369 call setline(1, repeat([join(range(1, 40), '')], 10))
370 hi PopupColor ctermbg=lightgrey
371 let winid = popup_create([
372 \ 'some text',
373 \ 'another line',
374 \], {
375 \ 'line': 2,
376 \ 'col': 10,
377 \ 'zindex': 90,
378 \ 'padding': [],
379 \ 'highlight': 'PopupColor',
380 \ 'mask': [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
381 call popup_create([
382 \ 'xxxxxxxxx',
383 \ 'yyyyyyyyy',
384 \], {
385 \ 'line': 3,
386 \ 'col': 18,
387 \ 'zindex': 20})
388 END
389 call writefile(lines, 'XtestPopupMask')
390 let buf = RunVimInTerminal('-S XtestPopupMask', {'rows': 10})
391 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
392
393 call term_sendkeys(buf, ":call popup_move(winid, {'col': 11, 'line': 3})\<CR>")
394 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
395
396 " clean up
397 call StopVimInTerminal(buf)
398 call delete('XtestPopupMask')
399endfunc
400
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200401func Test_popup_select()
402 if !CanRunVimInTerminal()
403 throw 'Skipped: cannot make screendumps'
404 endif
Bram Moolenaar650a6372019-06-15 00:29:33 +0200405 if !has('clipboard')
406 throw 'Skipped: clipboard feature missing'
407 endif
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200408 " create a popup with some text to be selected
409 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200410 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200411 call setline(1, range(1, 20))
412 let winid = popup_create(['the word', 'some more', 'several words here'], {
413 \ 'drag': 1,
414 \ 'border': [],
415 \ 'line': 3,
416 \ 'col': 10,
417 \ })
418 func Select1()
419 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
420 endfunc
421 map <silent> <F3> :call test_setmouse(4, 15)<CR>
422 map <silent> <F4> :call test_setmouse(6, 23)<CR>
423 END
424 call writefile(lines, 'XtestPopupSelect')
425 let buf = RunVimInTerminal('-S XtestPopupSelect', {'rows': 10})
426 call term_sendkeys(buf, ":call Select1()\<CR>")
427 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
428
429 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
430 call term_sendkeys(buf, "\"*p")
431 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
432
433 " clean up
434 call StopVimInTerminal(buf)
435 call delete('XtestPopupSelect')
436endfunc
437
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200438func Test_popup_in_tab()
439 " default popup is local to tab, not visible when in other tab
440 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200441 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200442 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200443 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200444 tabnew
445 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200446 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200447 quit
448 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200449
450 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200451 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200452 " buffer is gone now
453 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200454
455 " global popup is visible in any tab
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200456 let winid = popup_create("text", {'tabpage': -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200457 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200458 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200459 tabnew
460 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200461 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200462 quit
463 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200464 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200465
466 " create popup in other tab
467 tabnew
468 let winid = popup_create("text", {'tabpage': 1})
469 call assert_equal(0, popup_getpos(winid).visible)
470 call assert_equal(1, popup_getoptions(winid).tabpage)
471 quit
472 call assert_equal(1, popup_getpos(winid).visible)
473 call assert_equal(0, popup_getoptions(winid).tabpage)
474 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200475endfunc
476
477func Test_popup_valid_arguments()
478 " Zero value is like the property wasn't there
479 let winid = popup_create("text", {"col": 0})
480 let pos = popup_getpos(winid)
481 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200482 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200483
484 " using cursor column has minimum value of 1
485 let winid = popup_create("text", {"col": 'cursor-100'})
486 let pos = popup_getpos(winid)
487 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200488 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200489
490 " center
491 let winid = popup_create("text", {"pos": 'center'})
492 let pos = popup_getpos(winid)
493 let around = (&columns - pos.width) / 2
494 call assert_inrange(around - 1, around + 1, pos.col)
495 let around = (&lines - pos.height) / 2
496 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200497 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200498endfunc
499
500func Test_popup_invalid_arguments()
501 call assert_fails('call popup_create(666, {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200502 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200503 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200504 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200505
506 call assert_fails('call popup_create("text", {"col": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200507 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200508 call assert_fails('call popup_create("text", {"col": "cursor8"})', 'E15:')
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": "cursor+x"})', '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+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200513 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200514
515 call assert_fails('call popup_create("text", {"line": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200516 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200517 call assert_fails('call popup_create("text", {"line": "cursor8"})', 'E15:')
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": "cursor+x"})', '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+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200522 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200523
524 call assert_fails('call popup_create("text", {"pos": "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200525 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200526 call assert_fails('call popup_create("text", {"padding": "none"})', 'E714:')
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", {"border": "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", {"borderhighlight": "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", {"borderchars": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200533 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200534
535 call assert_fails('call popup_create([{"text": "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200536 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200537 call assert_fails('call popup_create([{"text": "text", "props": "none"}], {})', 'E714:')
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"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200540 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200541endfunc
542
Bram Moolenaareea16992019-05-31 17:34:48 +0200543func Test_win_execute_closing_curwin()
544 split
545 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200546 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200547 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200548endfunc
549
550func Test_win_execute_not_allowed()
551 let winid = popup_create('some text', {})
552 call assert_fails('call win_execute(winid, "split")', 'E994:')
553 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
554 call assert_fails('call win_execute(winid, "close")', 'E994:')
555 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200556 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200557 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
558 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
559 call assert_fails('call win_execute(winid, "next")', 'E994:')
560 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
561 call assert_fails('call win_execute(winid, "buf")', 'E994:')
562 call assert_fails('call win_execute(winid, "edit")', 'E994:')
563 call assert_fails('call win_execute(winid, "enew")', 'E994:')
564 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
565 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
566 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
567 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200568 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200569endfunc
570
Bram Moolenaar402502d2019-05-30 22:07:36 +0200571func Test_popup_with_wrap()
572 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200573 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200574 endif
575 let lines =<< trim END
576 call setline(1, range(1, 100))
577 let winid = popup_create(
578 \ 'a long line that wont fit',
579 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
580 END
581 call writefile(lines, 'XtestPopup')
582 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
583 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
584
585 " clean up
586 call StopVimInTerminal(buf)
587 call delete('XtestPopup')
588endfunc
589
590func Test_popup_without_wrap()
591 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200592 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200593 endif
594 let lines =<< trim END
595 call setline(1, range(1, 100))
596 let winid = popup_create(
597 \ 'a long line that wont fit',
598 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
599 END
600 call writefile(lines, 'XtestPopup')
601 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
602 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
603
604 " clean up
605 call StopVimInTerminal(buf)
606 call delete('XtestPopup')
607endfunc
608
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200609func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200610 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +0200611 throw 'Skipped: timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200612 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200613 topleft vnew
614 call setline(1, 'hello')
615
616 call popup_create('world', {
617 \ 'line': 1,
618 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200619 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200620 \ 'time': 500,
621 \})
622 redraw
623 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
624 call assert_equal('world', line)
625
626 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200627 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200628 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
629 call assert_equal('hello', line)
630
631 call popup_create('on the command line', {
632 \ 'line': &lines,
633 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200634 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200635 \ 'time': 500,
636 \})
637 redraw
638 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
639 call assert_match('.*on the command line.*', line)
640
641 sleep 700m
642 redraw
643 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
644 call assert_notmatch('.*on the command line.*', line)
645
646 bwipe!
647endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200648
649func Test_popup_hide()
650 topleft vnew
651 call setline(1, 'hello')
652
653 let winid = popup_create('world', {
654 \ 'line': 1,
655 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200656 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200657 \})
658 redraw
659 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
660 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200661 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200662 " buffer is still listed and active
663 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200664
665 call popup_hide(winid)
666 redraw
667 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
668 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200669 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200670 " buffer is still listed but hidden
671 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200672
673 call popup_show(winid)
674 redraw
675 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
676 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200677 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200678
679
680 call popup_close(winid)
681 redraw
682 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
683 call assert_equal('hello', line)
684
685 " error is given for existing non-popup window
686 call assert_fails('call popup_hide(win_getid())', 'E993:')
687
688 " no error non-existing window
689 call popup_hide(1234234)
690 call popup_show(41234234)
691
692 bwipe!
693endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200694
695func Test_popup_move()
696 topleft vnew
697 call setline(1, 'hello')
698
699 let winid = popup_create('world', {
700 \ 'line': 1,
701 \ 'col': 1,
702 \ 'minwidth': 20,
703 \})
704 redraw
705 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
706 call assert_equal('world ', line)
707
708 call popup_move(winid, {'line': 2, 'col': 2})
709 redraw
710 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
711 call assert_equal('hello ', line)
712 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
713 call assert_equal('~world', line)
714
715 call popup_move(winid, {'line': 1})
716 redraw
717 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
718 call assert_equal('hworld', line)
719
720 call popup_close(winid)
721
722 bwipe!
723endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200724
Bram Moolenaar402502d2019-05-30 22:07:36 +0200725func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200726 let winid = popup_create('hello', {
727 \ 'line': 2,
728 \ 'col': 3,
729 \ 'minwidth': 10,
730 \ 'minheight': 11,
731 \})
732 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200733 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200734 call assert_equal(2, res.line)
735 call assert_equal(3, res.col)
736 call assert_equal(10, res.width)
737 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200738 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200739
740 call popup_close(winid)
741endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200742
743func Test_popup_width_longest()
744 let tests = [
745 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
746 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
747 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
748 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
749 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
750 \ ]
751
752 for test in tests
753 let winid = popup_create(test[0], {'line': 2, 'col': 3})
754 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200755 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200756 call assert_equal(test[1], position.width)
757 call popup_close(winid)
758 endfor
759endfunc
760
761func Test_popup_wraps()
762 let tests = [
763 \ ['nowrap', 6, 1],
764 \ ['a line that wraps once', 12, 2],
765 \ ['a line that wraps two times', 12, 3],
766 \ ]
767 for test in tests
768 let winid = popup_create(test[0],
769 \ {'line': 2, 'col': 3, 'maxwidth': 12})
770 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200771 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200772 call assert_equal(test[1], position.width)
773 call assert_equal(test[2], position.height)
774
775 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200776 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200777 endfor
778endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200779
780func Test_popup_getoptions()
781 let winid = popup_create('hello', {
782 \ 'line': 2,
783 \ 'col': 3,
784 \ 'minwidth': 10,
785 \ 'minheight': 11,
786 \ 'maxwidth': 20,
787 \ 'maxheight': 21,
788 \ 'zindex': 100,
789 \ 'time': 5000,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200790 \ 'fixed': 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200791 \})
792 redraw
793 let res = popup_getoptions(winid)
794 call assert_equal(2, res.line)
795 call assert_equal(3, res.col)
796 call assert_equal(10, res.minwidth)
797 call assert_equal(11, res.minheight)
798 call assert_equal(20, res.maxwidth)
799 call assert_equal(21, res.maxheight)
800 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200801 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200802 if has('timers')
803 call assert_equal(5000, res.time)
804 endif
805 call popup_close(winid)
806
807 let winid = popup_create('hello', {})
808 redraw
809 let res = popup_getoptions(winid)
810 call assert_equal(0, res.line)
811 call assert_equal(0, res.col)
812 call assert_equal(0, res.minwidth)
813 call assert_equal(0, res.minheight)
814 call assert_equal(0, res.maxwidth)
815 call assert_equal(0, res.maxheight)
816 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200817 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200818 if has('timers')
819 call assert_equal(0, res.time)
820 endif
821 call popup_close(winid)
822 call assert_equal({}, popup_getoptions(winid))
823endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200824
825func Test_popup_option_values()
826 new
827 " window-local
828 setlocal number
829 setlocal nowrap
830 " buffer-local
831 setlocal omnifunc=Something
832 " global/buffer-local
833 setlocal path=/there
834 " global/window-local
835 setlocal scrolloff=9
836
837 let winid = popup_create('hello', {})
838 call assert_equal(0, getwinvar(winid, '&number'))
839 call assert_equal(1, getwinvar(winid, '&wrap'))
840 call assert_equal('', getwinvar(winid, '&omnifunc'))
841 call assert_equal(&g:path, getwinvar(winid, '&path'))
842 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
843
844 call popup_close(winid)
845 bwipe
846endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200847
848func Test_popup_atcursor()
849 topleft vnew
850 call setline(1, [
851 \ 'xxxxxxxxxxxxxxxxx',
852 \ 'xxxxxxxxxxxxxxxxx',
853 \ 'xxxxxxxxxxxxxxxxx',
854 \])
855
856 call cursor(2, 2)
857 redraw
858 let winid = popup_atcursor('vim', {})
859 redraw
860 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
861 call assert_equal('xvimxxxxxxxxxxxxx', line)
862 call popup_close(winid)
863
864 call cursor(3, 4)
865 redraw
866 let winid = popup_atcursor('vim', {})
867 redraw
868 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
869 call assert_equal('xxxvimxxxxxxxxxxx', line)
870 call popup_close(winid)
871
872 call cursor(1, 1)
873 redraw
874 let winid = popup_create('vim', {
875 \ 'line': 'cursor+2',
876 \ 'col': 'cursor+1',
877 \})
878 redraw
879 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
880 call assert_equal('xvimxxxxxxxxxxxxx', line)
881 call popup_close(winid)
882
883 call cursor(3, 3)
884 redraw
885 let winid = popup_create('vim', {
886 \ 'line': 'cursor-2',
887 \ 'col': 'cursor-1',
888 \})
889 redraw
890 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
891 call assert_equal('xvimxxxxxxxxxxxxx', line)
892 call popup_close(winid)
893
Bram Moolenaar402502d2019-05-30 22:07:36 +0200894 " just enough room above
895 call cursor(3, 3)
896 redraw
897 let winid = popup_atcursor(['vim', 'is great'], {})
898 redraw
899 let pos = popup_getpos(winid)
900 call assert_equal(1, pos.line)
901 call popup_close(winid)
902
903 " not enough room above, popup goes below the cursor
904 call cursor(3, 3)
905 redraw
906 let winid = popup_atcursor(['vim', 'is', 'great'], {})
907 redraw
908 let pos = popup_getpos(winid)
909 call assert_equal(4, pos.line)
910 call popup_close(winid)
911
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200912 " cursor in first line, popup in line 2
913 call cursor(1, 1)
914 redraw
915 let winid = popup_atcursor(['vim', 'is', 'great'], {})
916 redraw
917 let pos = popup_getpos(winid)
918 call assert_equal(2, pos.line)
919 call popup_close(winid)
920
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200921 bwipe!
922endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200923
924func Test_popup_filter()
925 new
926 call setline(1, 'some text')
927
928 func MyPopupFilter(winid, c)
929 if a:c == 'e'
930 let g:eaten = 'e'
931 return 1
932 endif
933 if a:c == '0'
934 let g:ignored = '0'
935 return 0
936 endif
937 if a:c == 'x'
938 call popup_close(a:winid)
939 return 1
940 endif
941 return 0
942 endfunc
943
944 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
945 redraw
946
947 " e is consumed by the filter
948 call feedkeys('e', 'xt')
949 call assert_equal('e', g:eaten)
950
951 " 0 is ignored by the filter
952 normal $
953 call assert_equal(9, getcurpos()[2])
954 call feedkeys('0', 'xt')
955 call assert_equal('0', g:ignored)
956 call assert_equal(1, getcurpos()[2])
957
958 " x closes the popup
959 call feedkeys('x', 'xt')
960 call assert_equal('e', g:eaten)
961 call assert_equal(-1, winbufnr(winid))
962
963 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200964 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200965endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200966
Bram Moolenaara42d9452019-06-15 21:46:30 +0200967func ShowDialog(key, result)
968 let s:cb_res = 999
969 let winid = popup_dialog('do you want to quit (Yes/no)?', {
970 \ 'filter': 'popup_filter_yesno',
971 \ 'callback': 'QuitCallback',
972 \ })
973 redraw
974 call feedkeys(a:key, "xt")
975 call assert_equal(winid, s:cb_winid)
976 call assert_equal(a:result, s:cb_res)
977endfunc
978
979func Test_popup_dialog()
980 func QuitCallback(id, res)
981 let s:cb_winid = a:id
982 let s:cb_res = a:res
983 endfunc
984
985 let winid = ShowDialog("y", 1)
986 let winid = ShowDialog("Y", 1)
987 let winid = ShowDialog("n", 0)
988 let winid = ShowDialog("N", 0)
989 let winid = ShowDialog("x", 0)
990 let winid = ShowDialog("X", 0)
991 let winid = ShowDialog("\<Esc>", 0)
992 let winid = ShowDialog("\<C-C>", -1)
993
994 delfunc QuitCallback
995endfunc
996
Bram Moolenaara730e552019-06-16 19:05:31 +0200997func ShowMenu(key, result)
998 let s:cb_res = 999
999 let winid = popup_menu(['one', 'two', 'something else'], {
1000 \ 'callback': 'QuitCallback',
1001 \ })
1002 redraw
1003 call feedkeys(a:key, "xt")
1004 call assert_equal(winid, s:cb_winid)
1005 call assert_equal(a:result, s:cb_res)
1006endfunc
1007
1008func Test_popup_menu()
1009 func QuitCallback(id, res)
1010 let s:cb_winid = a:id
1011 let s:cb_res = a:res
1012 endfunc
1013
1014 let winid = ShowMenu(" ", 1)
1015 let winid = ShowMenu("j \<CR>", 2)
1016 let winid = ShowMenu("JjK \<CR>", 2)
1017 let winid = ShowMenu("jjjjjj ", 3)
1018 let winid = ShowMenu("kkk ", 1)
1019 let winid = ShowMenu("x", -1)
1020 let winid = ShowMenu("X", -1)
1021 let winid = ShowMenu("\<Esc>", -1)
1022 let winid = ShowMenu("\<C-C>", -1)
1023
1024 delfunc QuitCallback
1025endfunc
1026
1027func Test_popup_menu_screenshot()
1028 if !CanRunVimInTerminal()
1029 throw 'Skipped: cannot make screendumps'
1030 endif
1031
1032 let lines =<< trim END
1033 call setline(1, range(1, 20))
1034 hi PopupSelected ctermbg=lightblue
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001035 call popup_menu(['one', 'two', 'another'], {'callback': 'MenuDone', 'title': ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001036 func MenuDone(id, res)
1037 echomsg "selected " .. a:res
1038 endfunc
1039 END
1040 call writefile(lines, 'XtestPopupMenu')
1041 let buf = RunVimInTerminal('-S XtestPopupMenu', {'rows': 10})
1042 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1043
1044 call term_sendkeys(buf, "jj")
1045 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1046
1047 call term_sendkeys(buf, " ")
1048 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1049
1050 " clean up
1051 call StopVimInTerminal(buf)
1052 call delete('XtestPopupMenu')
1053endfunc
1054
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001055func Test_popup_title()
1056 if !CanRunVimInTerminal()
1057 throw 'Skipped: cannot make screendumps'
1058 endif
1059
1060 " Create a popup without title or border, a line of padding will be added to
1061 " put the title on.
1062 let lines =<< trim END
1063 call setline(1, range(1, 20))
1064 call popup_create(['one', 'two', 'another'], {'title': 'Title String'})
1065 END
1066 call writefile(lines, 'XtestPopupTitle')
1067 let buf = RunVimInTerminal('-S XtestPopupTitle', {'rows': 10})
1068 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1069
1070 " clean up
1071 call StopVimInTerminal(buf)
1072 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001073
1074 let winid = popup_create('something', {'title': 'Some Title'})
1075 call assert_equal('Some Title', popup_getoptions(winid).title)
1076 call popup_setoptions(winid, {'title': 'Another Title'})
1077 call assert_equal('Another Title', popup_getoptions(winid).title)
1078
1079 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001080endfunc
1081
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001082func Test_popup_close_callback()
1083 func PopupDone(id, result)
1084 let g:result = a:result
1085 endfunc
1086 let winid = popup_create('something', {'callback': 'PopupDone'})
1087 redraw
1088 call popup_close(winid, 'done')
1089 call assert_equal('done', g:result)
1090endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001091
1092func Test_popup_empty()
1093 let winid = popup_create('', {'padding': [2,2,2,2]})
1094 redraw
1095 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001096 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001097 call assert_equal(5, pos.height)
1098
1099 let winid = popup_create([], {'border': []})
1100 redraw
1101 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001102 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001103 call assert_equal(3, pos.height)
1104endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001105
1106func Test_popup_never_behind()
1107 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001108 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +02001109 endif
1110 " +-----------------------------+
1111 " | | |
1112 " | | |
1113 " | | |
1114 " | line1 |
1115 " |------------line2------------|
1116 " | line3 |
1117 " | line4 |
1118 " | |
1119 " | |
1120 " +-----------------------------+
1121 let lines =<< trim END
1122 only
1123 split
1124 vsplit
1125 let info_window1 = getwininfo()[0]
1126 let line = info_window1['height']
1127 let col = info_window1['width']
1128 call popup_create(['line1', 'line2', 'line3', 'line4'], {
1129 \ 'line' : line,
1130 \ 'col' : col,
1131 \ })
1132 END
1133 call writefile(lines, 'XtestPopupBehind')
1134 let buf = RunVimInTerminal('-S XtestPopupBehind', {'rows': 10})
1135 call term_sendkeys(buf, "\<C-W>w")
1136 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1137
1138 " clean up
1139 call StopVimInTerminal(buf)
1140 call delete('XtestPopupBehind')
1141endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001142
1143func s:VerifyPosition( p, msg, line, col, width, height )
1144 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
1145 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
1146 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
1147 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
1148endfunc
1149
1150func Test_popup_position_adjust()
1151 " Anything placed past 2 cells from of the right of the screen is moved to the
1152 " left.
1153 "
1154 " When wrapping is disabled, we also shift to the left to display on the
1155 " screen, unless fixed is set.
1156
1157 " Entries for cases which don't vary based on wrapping.
1158 " Format is per tests described below
1159 let both_wrap_tests = [
1160 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
1161 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
1162 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
1163 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
1164 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
1165 \
1166 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
1167 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
1168 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
1169 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
1170 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
1171 \
1172 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1173 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1174 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1175 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1176 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
1177 \ ]
1178
1179 " these test groups are dicts with:
1180 " - comment: something to identify the group of tests by
1181 " - options: dict of options to merge with the row/col in tests
1182 " - tests: list of cases. Each one is a list with elements:
1183 " - text
1184 " - row
1185 " - col
1186 " - expected row
1187 " - expected col
1188 " - expected width
1189 " - expected height
1190 let tests = [
1191 \ {
1192 \ 'comment': 'left-aligned with wrapping',
1193 \ 'options': {
1194 \ 'wrap': 1,
1195 \ 'pos': 'botleft',
1196 \ },
1197 \ 'tests': both_wrap_tests + [
1198 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
1199 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
1200 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
1201 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
1202 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1203 \ ],
1204 \ },
1205 \ {
1206 \ 'comment': 'left aligned without wrapping',
1207 \ 'options': {
1208 \ 'wrap': 0,
1209 \ 'pos': 'botleft',
1210 \ },
1211 \ 'tests': both_wrap_tests + [
1212 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
1213 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
1214 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
1215 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
1216 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1217 \ ],
1218 \ },
1219 \ {
1220 \ 'comment': 'left aligned with fixed position',
1221 \ 'options': {
1222 \ 'wrap': 0,
1223 \ 'fixed': 1,
1224 \ 'pos': 'botleft',
1225 \ },
1226 \ 'tests': both_wrap_tests + [
1227 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1228 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1229 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1230 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1231 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1232 \ ],
1233 \ },
1234 \ ]
1235
1236 for test_group in tests
1237 for test in test_group.tests
1238 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
1239 let options = {
1240 \ 'line': line,
1241 \ 'col': col,
1242 \ }
1243 call extend( options, test_group.options )
1244
1245 let p = popup_create( text, options )
1246
1247 let msg = string( extend( options, { 'text': text } ) )
1248 call s:VerifyPosition( p, msg, e_line, e_col, e_width, e_height )
1249 call popup_close( p )
1250 endfor
1251 endfor
1252
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001253 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001254 %bwipe!
1255endfunc
1256
Bram Moolenaar3397f742019-06-02 18:40:06 +02001257func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001258 " width of screen
1259 let X = join(map(range(&columns), {->'X'}), '')
1260
1261 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1262 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
1263
1264 redraw
1265 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1266 call assert_equal(X, line)
1267
1268 call popup_close( p )
1269 redraw
1270
1271 " Same if placed on the right hand side
1272 let p = popup_create( X, { 'line': 1, 'col': &columns, 'wrap': 0 } )
1273 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
1274
1275 redraw
1276 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1277 call assert_equal(X, line)
1278
1279 call popup_close( p )
1280 redraw
1281
1282 " Extend so > window width
1283 let X .= 'x'
1284
1285 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1286 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
1287
1288 redraw
1289 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1290 call assert_equal(X[ : -2 ], line)
1291
1292 call popup_close( p )
1293 redraw
1294
1295 " Shifted then truncated (the x is not visible)
1296 let p = popup_create( X, { 'line': 1, 'col': &columns - 3, 'wrap': 0 } )
1297 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
1298
1299 redraw
1300 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1301 call assert_equal(X[ : -2 ], line)
1302
1303 call popup_close( p )
1304 redraw
1305
1306 " Not shifted, just truncated
1307 let p = popup_create( X,
1308 \ { 'line': 1, 'col': 2, 'wrap': 0, 'fixed': 1 } )
1309 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
1310
1311 redraw
1312 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1313 let e_line = ' ' . X[ 1 : -2 ]
1314 call assert_equal(e_line, line)
1315
1316 call popup_close( p )
1317 redraw
1318
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001319 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001320 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001321endfunc
1322
1323func Test_popup_moved()
1324 new
1325 call test_override('char_avail', 1)
1326 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1327
1328 exe "normal gg0/word\<CR>"
1329 let winid = popup_atcursor('text', {'moved': 'any'})
1330 redraw
1331 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001332 call assert_equal([4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001333 " trigger the check for last_cursormoved by going into insert mode
1334 call feedkeys("li\<Esc>", 'xt')
1335 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001336 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001337
1338 exe "normal gg0/word\<CR>"
1339 let winid = popup_atcursor('text', {'moved': 'word'})
1340 redraw
1341 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001342 call assert_equal([4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001343 call feedkeys("hi\<Esc>", 'xt')
1344 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001345 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001346
1347 exe "normal gg0/word\<CR>"
1348 let winid = popup_atcursor('text', {'moved': 'word'})
1349 redraw
1350 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001351 call assert_equal([4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001352 call feedkeys("li\<Esc>", 'xt')
1353 call assert_equal(1, popup_getpos(winid).visible)
1354 call feedkeys("ei\<Esc>", 'xt')
1355 call assert_equal(1, popup_getpos(winid).visible)
1356 call feedkeys("eli\<Esc>", 'xt')
1357 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001358 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001359
Bram Moolenaar17627312019-06-02 19:53:44 +02001360 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001361 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001362 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001363 redraw
1364 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001365 call assert_equal([2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001366 call feedkeys("eli\<Esc>", 'xt')
1367 call assert_equal(1, popup_getpos(winid).visible)
1368 call feedkeys("wi\<Esc>", 'xt')
1369 call assert_equal(1, popup_getpos(winid).visible)
1370 call feedkeys("Eli\<Esc>", 'xt')
1371 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001372 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001373
1374 exe "normal gg0/word\<CR>"
1375 let winid = popup_atcursor('text', {'moved': [5, 10]})
1376 redraw
1377 call assert_equal(1, popup_getpos(winid).visible)
1378 call feedkeys("eli\<Esc>", 'xt')
1379 call feedkeys("ei\<Esc>", 'xt')
1380 call assert_equal(1, popup_getpos(winid).visible)
1381 call feedkeys("eli\<Esc>", 'xt')
1382 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001383 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001384
1385 bwipe!
1386 call test_override('ALL', 0)
1387endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001388
1389func Test_notifications()
1390 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02001391 throw 'Skipped: timer feature not supported'
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001392 endif
1393 if !CanRunVimInTerminal()
1394 throw 'Skipped: cannot make screendumps'
1395 endif
1396
1397 call writefile([
1398 \ "call setline(1, range(1, 20))",
1399 \ "hi Notification ctermbg=lightblue",
1400 \ "call popup_notification('first notification', {})",
1401 \], 'XtestNotifications')
1402 let buf = RunVimInTerminal('-S XtestNotifications', {'rows': 10})
1403 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1404
1405 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001406 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1407 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001408 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1409
1410
1411 " clean up
1412 call StopVimInTerminal(buf)
1413 call delete('XtestNotifications')
1414endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001415
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001416func Test_popup_settext()
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001417 if !CanRunVimInTerminal()
1418 throw 'Skipped: cannot make screendumps'
1419 endif
1420
1421 let lines =<< trim END
1422 let opts = {'wrap': 0}
1423 let p = popup_create('test', opts)
1424 call popup_settext(p, 'this is a text')
1425 END
1426
1427 call writefile( lines, 'XtestPopupSetText' )
1428 let buf = RunVimInTerminal('-S XtestPopupSetText', {'rows': 10})
1429 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1430
1431 " Setting to empty string clears it
1432 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1433 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1434
1435 " Setting a list
1436 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1437 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1438
1439 " Shrinking with a list
1440 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1441 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1442
1443 " Growing with a list
1444 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1445 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1446
1447 " Empty list clears
1448 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1449 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1450
1451 " Dicts
1452 call term_sendkeys(buf, ":call popup_settext(p, [{'text': 'aaaa'}, {'text': 'bbbb'}, {'text': 'cccc'}])\<CR>")
1453 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1454
1455 " clean up
1456 call StopVimInTerminal(buf)
1457 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001458endfunc
1459
1460func Test_popup_hidden()
1461 new
1462
1463 let winid = popup_atcursor('text', {'hidden': 1})
1464 redraw
1465 call assert_equal(0, popup_getpos(winid).visible)
1466 call popup_close(winid)
1467
1468 let winid = popup_create('text', {'hidden': 1})
1469 redraw
1470 call assert_equal(0, popup_getpos(winid).visible)
1471 call popup_close(winid)
1472
1473 func QuitCallback(id, res)
1474 let s:cb_winid = a:id
1475 let s:cb_res = a:res
1476 endfunc
1477 let winid = popup_dialog('make a choice', {'hidden': 1,
1478 \ 'filter': 'popup_filter_yesno',
1479 \ 'callback': 'QuitCallback',
1480 \ })
1481 redraw
1482 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001483 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1484 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001485 exe "normal anot used by filter\<Esc>"
1486 call assert_equal('not used by filter', getline(1))
1487
1488 call popup_show(winid)
1489 call feedkeys('y', "xt")
1490 call assert_equal(1, s:cb_res)
1491
1492 bwipe!
1493 delfunc QuitCallback
1494endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001495
1496" Test options not checked elsewhere
1497func Test_set_get_options()
1498 let winid = popup_create('some text', {'highlight': 'Beautiful'})
1499 let options = popup_getoptions(winid)
1500 call assert_equal(1, options.wrap)
1501 call assert_equal(0, options.drag)
1502 call assert_equal('Beautiful', options.highlight)
1503
1504 call popup_setoptions(winid, {'wrap': 0, 'drag': 1, 'highlight': 'Another'})
1505 let options = popup_getoptions(winid)
1506 call assert_equal(0, options.wrap)
1507 call assert_equal(1, options.drag)
1508 call assert_equal('Another', options.highlight)
1509
1510 call popup_close(winid)
1511endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001512
1513func Test_popupwin_garbage_collect()
1514 func MyPopupFilter(x, winid, c)
1515 " NOP
1516 endfunc
1517
1518 let winid = popup_create('something', {'filter': function('MyPopupFilter', [{}])})
1519 call test_garbagecollect_now()
1520 redraw
1521 " Must not crach caused by invalid memory access
1522 call feedkeys('j', 'xt')
1523 call assert_true(v:true)
1524
1525 call popup_close(winid)
1526 delfunc MyPopupFilter
1527endfunc