blob: 477cda38ae52da5e87410e6798d12b040dfb2e4b [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
12 call writefile([
13 \ "call setline(1, range(1, 100))",
Bram Moolenaar20c023a2019-05-26 21:03:24 +020014 \ "hi PopupColor1 ctermbg=lightblue",
15 \ "hi PopupColor2 ctermbg=lightcyan",
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020016 \ "hi Comment ctermfg=red",
17 \ "call prop_type_add('comment', {'highlight': 'Comment'})",
Bram Moolenaar60cdb302019-05-27 21:54:10 +020018 \ "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})",
Bram Moolenaar20c023a2019-05-26 21:03:24 +020020 \ "call setwinvar(winid2, '&wincolor', 'PopupColor2')",
Bram Moolenaar4d784b22019-05-25 19:51:39 +020021 \], 'XtestPopup')
22 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
23 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
24
Bram Moolenaarec583842019-05-26 14:11:23 +020025 " Add a tabpage
26 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020027 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020028 \ .. "{'text': 'other tab'},"
29 \ .. "{'text': 'a comment line', 'props': [{"
Bram Moolenaar60cdb302019-05-27 21:54:10 +020030 \ .. "'col': 3, 'length': 7, 'minwidth': 20, 'type': 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020031 \ .. "}]},"
Bram Moolenaar60cdb302019-05-27 21:54:10 +020032 \ .. "], {'line': 4, 'col': 9, 'minwidth': 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020033 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
34
35 " switch back to first tabpage
36 call term_sendkeys(buf, "gt")
37 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
38
39 " close that tabpage
40 call term_sendkeys(buf, ":quit!\<CR>")
41 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
42
Bram Moolenaar202d9822019-06-11 21:56:30 +020043 " set 'columns' to a small value, size must be recomputed
44 call term_sendkeys(buf, ":let cols = &columns\<CR>")
45 call term_sendkeys(buf, ":set columns=12\<CR>")
46 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
47 call term_sendkeys(buf, ":let &columns = cols\<CR>")
48
Bram Moolenaar17146962019-05-30 00:12:11 +020049 " resize popup, show empty line at bottom
Bram Moolenaar60cdb302019-05-27 21:54:10 +020050 call term_sendkeys(buf, ":call popup_move(popupwin, {'minwidth': 15, 'maxwidth': 25, 'minheight': 3, 'maxheight': 5})\<CR>")
51 call term_sendkeys(buf, ":redraw\<CR>")
52 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
53
Bram Moolenaar17146962019-05-30 00:12:11 +020054 " show not fitting line at bottom
55 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
56 call term_sendkeys(buf, ":redraw\<CR>")
57 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
58
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020059 " move popup over ruler
60 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
61 call term_sendkeys(buf, ":call popup_move(popupwin, {'line': 7, 'col': 55})\<CR>")
62 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
63
64 " clear all popups after moving the cursor a bit, so that ruler is updated
65 call term_sendkeys(buf, "axxx\<Esc>")
66 call term_wait(buf)
67 call term_sendkeys(buf, "0")
68 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020069 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020070 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
71
Bram Moolenaar4d784b22019-05-25 19:51:39 +020072 " clean up
73 call StopVimInTerminal(buf)
74 call delete('XtestPopup')
75endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020076
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020077func Test_popup_with_border_and_padding()
78 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020079 throw 'Skipped: cannot make screendumps'
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020080 endif
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020082 for iter in range(0, 1)
83 call writefile([iter == 1 ? '' : 'set enc=latin1',
84 \ "call setline(1, range(1, 100))",
85 \ "call popup_create('hello border', {'line': 2, 'col': 3, 'border': []})",
86 \ "call popup_create('hello padding', {'line': 2, 'col': 23, 'padding': []})",
87 \ "call popup_create('hello both', {'line': 2, 'col': 43, 'border': [], 'padding': []})",
88 \ "call popup_create('border TL', {'line': 6, 'col': 3, 'border': [1, 0, 0, 4]})",
89 \ "call popup_create('paddings', {'line': 6, 'col': 23, 'padding': [1, 3, 2, 4]})",
Bram Moolenaar51c31312019-06-15 22:27:23 +020090 \ "call popup_create('wrapped longer text', {'line': 8, 'col': 55, 'padding': [0, 3, 0, 3], 'border': [0, 1, 0, 1]})",
91 \ "call popup_create('right aligned text', {'line': 11, 'col': 56, 'wrap': 0, 'padding': [0, 3, 0, 3], 'border': [0, 1, 0, 1]})",
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020092 \], 'XtestPopupBorder')
93 let buf = RunVimInTerminal('-S XtestPopupBorder', {'rows': 15})
94 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
95
96 call StopVimInTerminal(buf)
97 call delete('XtestPopupBorder')
98 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020099
Bram Moolenaar790498b2019-06-01 22:15:29 +0200100 call writefile([
101 \ "call setline(1, range(1, 100))",
102 \ "hi BlueColor ctermbg=lightblue",
103 \ "hi TopColor ctermbg=253",
104 \ "hi RightColor ctermbg=245",
105 \ "hi BottomColor ctermbg=240",
106 \ "hi LeftColor ctermbg=248",
107 \ "call popup_create('hello border', {'line': 2, 'col': 3, 'border': [], 'borderhighlight': ['BlueColor']})",
108 \ "call popup_create(['hello border', 'and more'], {'line': 2, 'col': 23, 'border': [], 'borderhighlight': ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})",
109 \ "call popup_create(['hello border', 'lines only'], {'line': 2, 'col': 43, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['x']})",
110 \ "call popup_create(['hello border', 'with corners'], {'line': 2, 'col': 60, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['x', '#']})",
111 \ "call popup_create(['hello border', 'with numbers'], {'line': 6, 'col': 3, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['0', '1', '2', '3', '4', '5', '6', '7']})",
112 \ "call popup_create(['hello border', 'just blanks'], {'line': 7, 'col': 23, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': [' ']})",
113 \], 'XtestPopupBorder')
114 let buf = RunVimInTerminal('-S XtestPopupBorder', {'rows': 12})
115 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
116
117 call StopVimInTerminal(buf)
118 call delete('XtestPopupBorder')
119
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200120 let with_border_or_padding = {
121 \ 'line': 2,
122 \ 'core_line': 3,
123 \ 'col': 3,
124 \ 'core_col': 4,
125 \ 'width': 14,
126 \ 'core_width': 12,
127 \ 'height': 3,
128 \ 'core_height': 1,
129 \ 'visible': 1}
130 let winid = popup_create('hello border', {'line': 2, 'col': 3, 'border': []})",
131 call assert_equal(with_border_or_padding, popup_getpos(winid))
132
133 let winid = popup_create('hello paddng', {'line': 2, 'col': 3, 'padding': []})
134 call assert_equal(with_border_or_padding, popup_getpos(winid))
135
136 let winid = popup_create('hello both', {'line': 3, 'col': 8, 'border': [], 'padding': []})
137 call assert_equal({
138 \ 'line': 3,
139 \ 'core_line': 5,
140 \ 'col': 8,
141 \ 'core_col': 10,
142 \ 'width': 14,
143 \ 'core_width': 10,
144 \ 'height': 5,
145 \ 'core_height': 1,
146 \ 'visible': 1}, popup_getpos(winid))
147endfunc
148
Bram Moolenaarb4230122019-05-30 18:40:53 +0200149func Test_popup_with_syntax_win_execute()
150 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200151 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200152 endif
153 call writefile([
154 \ "call setline(1, range(1, 100))",
155 \ "hi PopupColor ctermbg=lightblue",
156 \ "let winid = popup_create([",
157 \ "\\ '#include <stdio.h>',",
158 \ "\\ 'int main(void)',",
159 \ "\\ '{',",
160 \ "\\ ' printf(123);',",
161 \ "\\ '}',",
162 \ "\\], {'line': 3, 'col': 25, 'highlight': 'PopupColor'})",
163 \ "call win_execute(winid, 'set syntax=cpp')",
164 \], 'XtestPopup')
165 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
166 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
167
168 " clean up
169 call StopVimInTerminal(buf)
170 call delete('XtestPopup')
171endfunc
172
173func Test_popup_with_syntax_setbufvar()
174 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200175 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200176 endif
Bram Moolenaar402502d2019-05-30 22:07:36 +0200177 let lines =<< trim END
178 call setline(1, range(1, 100))
179 hi PopupColor ctermbg=lightgrey
180 let winid = popup_create([
181 \ '#include <stdio.h>',
182 \ 'int main(void)',
183 \ '{',
184 \ ' printf(567);',
185 \ '}',
186 \], {'line': 3, 'col': 21, 'highlight': 'PopupColor'})
187 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
188 END
189 call writefile(lines, 'XtestPopup')
Bram Moolenaarb4230122019-05-30 18:40:53 +0200190 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
191 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
192
193 " clean up
194 call StopVimInTerminal(buf)
195 call delete('XtestPopup')
196endfunc
197
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200198func Test_popup_with_matches()
199 if !CanRunVimInTerminal()
200 throw 'Skipped: cannot make screendumps'
201 endif
202 let lines =<< trim END
203 call setline(1, ['111 222 333', '444 555 666'])
204 let winid = popup_create([
205 \ '111 222 333',
206 \ '444 555 666',
207 \], {'line': 3, 'col': 10, 'border': []})
208 set hlsearch
209 /666
210 call matchadd('ErrorMsg', '111')
211 call matchadd('ErrorMsg', '444')
212 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
213 call win_execute(winid, "call matchadd('ErrorMsg', '555')")
214 END
215 call writefile(lines, 'XtestPopupMatches')
216 let buf = RunVimInTerminal('-S XtestPopupMatches', {'rows': 10})
217 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
218
219 " clean up
220 call StopVimInTerminal(buf)
221 call delete('XtestPopupMatches')
222endfunc
223
Bram Moolenaar399d8982019-06-02 15:34:29 +0200224func Test_popup_all_corners()
225 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200226 throw 'Skipped: cannot make screendumps'
Bram Moolenaar399d8982019-06-02 15:34:29 +0200227 endif
228 let lines =<< trim END
229 call setline(1, repeat([repeat('-', 60)], 15))
230 set so=0
231 normal 2G3|r#
232 let winid1 = popup_create(['first', 'second'], {
233 \ 'line': 'cursor+1',
234 \ 'col': 'cursor',
235 \ 'pos': 'topleft',
236 \ 'border': [],
237 \ 'padding': [],
238 \ })
239 normal 25|r@
240 let winid1 = popup_create(['First', 'SeconD'], {
241 \ 'line': 'cursor+1',
242 \ 'col': 'cursor',
243 \ 'pos': 'topright',
244 \ 'border': [],
245 \ 'padding': [],
246 \ })
247 normal 9G29|r%
248 let winid1 = popup_create(['fiRSt', 'seCOnd'], {
249 \ 'line': 'cursor-1',
250 \ 'col': 'cursor',
251 \ 'pos': 'botleft',
252 \ 'border': [],
253 \ 'padding': [],
254 \ })
255 normal 51|r&
256 let winid1 = popup_create(['FIrsT', 'SEcoND'], {
257 \ 'line': 'cursor-1',
258 \ 'col': 'cursor',
259 \ 'pos': 'botright',
260 \ 'border': [],
261 \ 'padding': [],
262 \ })
263 END
264 call writefile(lines, 'XtestPopupCorners')
265 let buf = RunVimInTerminal('-S XtestPopupCorners', {'rows': 12})
266 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
267
268 " clean up
269 call StopVimInTerminal(buf)
270 call delete('XtestPopupCorners')
271endfunc
272
Bram Moolenaar8d241042019-06-12 23:40:01 +0200273func Test_popup_firstline()
274 if !CanRunVimInTerminal()
275 throw 'Skipped: cannot make screendumps'
276 endif
277 let lines =<< trim END
278 call setline(1, range(1, 20))
279 call popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], {
280 \ 'maxheight': 4,
281 \ 'firstline': 3,
282 \ })
283 END
284 call writefile(lines, 'XtestPopupFirstline')
285 let buf = RunVimInTerminal('-S XtestPopupFirstline', {'rows': 10})
286 call VerifyScreenDump(buf, 'Test_popupwin_firstline', {})
287
288 " clean up
289 call StopVimInTerminal(buf)
290 call delete('XtestPopupFirstline')
291endfunc
292
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200293func Test_popup_drag()
294 if !CanRunVimInTerminal()
295 throw 'Skipped: cannot make screendumps'
296 endif
297 " create a popup that covers the command line
298 let lines =<< trim END
299 call setline(1, range(1, 20))
300 let winid = popup_create(['1111', '222222', '33333'], {
301 \ 'drag': 1,
302 \ 'border': [],
303 \ 'line': &lines - 4,
304 \ })
305 func Dragit()
306 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
307 endfunc
308 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
309 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2)<CR>
310 END
311 call writefile(lines, 'XtestPopupDrag')
312 let buf = RunVimInTerminal('-S XtestPopupDrag', {'rows': 10})
313 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
314
315 call term_sendkeys(buf, ":call Dragit()\<CR>")
316 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
317
318 " clean up
319 call StopVimInTerminal(buf)
320 call delete('XtestPopupDrag')
321endfunc
322
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200323func Test_popup_select()
324 if !CanRunVimInTerminal()
325 throw 'Skipped: cannot make screendumps'
326 endif
Bram Moolenaar650a6372019-06-15 00:29:33 +0200327 if !has('clipboard')
328 throw 'Skipped: clipboard feature missing'
329 endif
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200330 " create a popup with some text to be selected
331 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200332 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200333 call setline(1, range(1, 20))
334 let winid = popup_create(['the word', 'some more', 'several words here'], {
335 \ 'drag': 1,
336 \ 'border': [],
337 \ 'line': 3,
338 \ 'col': 10,
339 \ })
340 func Select1()
341 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
342 endfunc
343 map <silent> <F3> :call test_setmouse(4, 15)<CR>
344 map <silent> <F4> :call test_setmouse(6, 23)<CR>
345 END
346 call writefile(lines, 'XtestPopupSelect')
347 let buf = RunVimInTerminal('-S XtestPopupSelect', {'rows': 10})
348 call term_sendkeys(buf, ":call Select1()\<CR>")
349 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
350
351 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
352 call term_sendkeys(buf, "\"*p")
353 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
354
355 " clean up
356 call StopVimInTerminal(buf)
357 call delete('XtestPopupSelect')
358endfunc
359
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200360func Test_popup_in_tab()
361 " default popup is local to tab, not visible when in other tab
362 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200363 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200364 call assert_equal(1, popup_getpos(winid).visible)
365 tabnew
366 call assert_equal(0, popup_getpos(winid).visible)
367 quit
368 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200369
370 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200371 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200372 " buffer is gone now
373 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200374
375 " global popup is visible in any tab
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200376 let winid = popup_create("text", {'tabpage': -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200377 call assert_equal(1, popup_getpos(winid).visible)
378 tabnew
379 call assert_equal(1, popup_getpos(winid).visible)
380 quit
381 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200382 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200383endfunc
384
385func Test_popup_valid_arguments()
386 " Zero value is like the property wasn't there
387 let winid = popup_create("text", {"col": 0})
388 let pos = popup_getpos(winid)
389 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200390 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200391
392 " using cursor column has minimum value of 1
393 let winid = popup_create("text", {"col": 'cursor-100'})
394 let pos = popup_getpos(winid)
395 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200396 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200397
398 " center
399 let winid = popup_create("text", {"pos": 'center'})
400 let pos = popup_getpos(winid)
401 let around = (&columns - pos.width) / 2
402 call assert_inrange(around - 1, around + 1, pos.col)
403 let around = (&lines - pos.height) / 2
404 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200405 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200406endfunc
407
408func Test_popup_invalid_arguments()
409 call assert_fails('call popup_create(666, {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200410 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200411 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200412 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200413
414 call assert_fails('call popup_create("text", {"col": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200415 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200416 call assert_fails('call popup_create("text", {"col": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200417 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200418 call assert_fails('call popup_create("text", {"col": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200419 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200420 call assert_fails('call popup_create("text", {"col": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200421 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200422
423 call assert_fails('call popup_create("text", {"line": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200424 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200425 call assert_fails('call popup_create("text", {"line": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200426 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200427 call assert_fails('call popup_create("text", {"line": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200428 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200429 call assert_fails('call popup_create("text", {"line": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200430 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200431
432 call assert_fails('call popup_create("text", {"pos": "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200433 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200434 call assert_fails('call popup_create("text", {"padding": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200435 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200436 call assert_fails('call popup_create("text", {"border": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200437 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200438 call assert_fails('call popup_create("text", {"borderhighlight": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200439 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200440 call assert_fails('call popup_create("text", {"borderchars": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200441 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200442
443 call assert_fails('call popup_create([{"text": "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200444 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200445 call assert_fails('call popup_create([{"text": "text", "props": "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200446 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200447 call assert_fails('call popup_create([{"text": "text", "props": ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200448 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200449endfunc
450
Bram Moolenaareea16992019-05-31 17:34:48 +0200451func Test_win_execute_closing_curwin()
452 split
453 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200454 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200455 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200456endfunc
457
458func Test_win_execute_not_allowed()
459 let winid = popup_create('some text', {})
460 call assert_fails('call win_execute(winid, "split")', 'E994:')
461 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
462 call assert_fails('call win_execute(winid, "close")', 'E994:')
463 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200464 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200465 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
466 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
467 call assert_fails('call win_execute(winid, "next")', 'E994:')
468 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
469 call assert_fails('call win_execute(winid, "buf")', 'E994:')
470 call assert_fails('call win_execute(winid, "edit")', 'E994:')
471 call assert_fails('call win_execute(winid, "enew")', 'E994:')
472 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
473 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
474 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
475 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200476 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200477endfunc
478
Bram Moolenaar402502d2019-05-30 22:07:36 +0200479func Test_popup_with_wrap()
480 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200481 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200482 endif
483 let lines =<< trim END
484 call setline(1, range(1, 100))
485 let winid = popup_create(
486 \ 'a long line that wont fit',
487 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
488 END
489 call writefile(lines, 'XtestPopup')
490 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
491 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
492
493 " clean up
494 call StopVimInTerminal(buf)
495 call delete('XtestPopup')
496endfunc
497
498func Test_popup_without_wrap()
499 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200500 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200501 endif
502 let lines =<< trim END
503 call setline(1, range(1, 100))
504 let winid = popup_create(
505 \ 'a long line that wont fit',
506 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
507 END
508 call writefile(lines, 'XtestPopup')
509 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
510 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
511
512 " clean up
513 call StopVimInTerminal(buf)
514 call delete('XtestPopup')
515endfunc
516
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200517func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200518 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +0200519 throw 'Skipped: timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200520 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200521 topleft vnew
522 call setline(1, 'hello')
523
524 call popup_create('world', {
525 \ 'line': 1,
526 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200527 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200528 \ 'time': 500,
529 \})
530 redraw
531 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
532 call assert_equal('world', line)
533
534 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200535 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200536 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
537 call assert_equal('hello', line)
538
539 call popup_create('on the command line', {
540 \ 'line': &lines,
541 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200542 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200543 \ 'time': 500,
544 \})
545 redraw
546 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
547 call assert_match('.*on the command line.*', line)
548
549 sleep 700m
550 redraw
551 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
552 call assert_notmatch('.*on the command line.*', line)
553
554 bwipe!
555endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200556
557func Test_popup_hide()
558 topleft vnew
559 call setline(1, 'hello')
560
561 let winid = popup_create('world', {
562 \ 'line': 1,
563 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200564 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200565 \})
566 redraw
567 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
568 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200569 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200570 " buffer is still listed and active
571 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200572
573 call popup_hide(winid)
574 redraw
575 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
576 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200577 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200578 " buffer is still listed but hidden
579 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200580
581 call popup_show(winid)
582 redraw
583 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
584 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200585 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200586
587
588 call popup_close(winid)
589 redraw
590 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
591 call assert_equal('hello', line)
592
593 " error is given for existing non-popup window
594 call assert_fails('call popup_hide(win_getid())', 'E993:')
595
596 " no error non-existing window
597 call popup_hide(1234234)
598 call popup_show(41234234)
599
600 bwipe!
601endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200602
603func Test_popup_move()
604 topleft vnew
605 call setline(1, 'hello')
606
607 let winid = popup_create('world', {
608 \ 'line': 1,
609 \ 'col': 1,
610 \ 'minwidth': 20,
611 \})
612 redraw
613 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
614 call assert_equal('world ', line)
615
616 call popup_move(winid, {'line': 2, 'col': 2})
617 redraw
618 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
619 call assert_equal('hello ', line)
620 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
621 call assert_equal('~world', line)
622
623 call popup_move(winid, {'line': 1})
624 redraw
625 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
626 call assert_equal('hworld', line)
627
628 call popup_close(winid)
629
630 bwipe!
631endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200632
Bram Moolenaar402502d2019-05-30 22:07:36 +0200633func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200634 let winid = popup_create('hello', {
635 \ 'line': 2,
636 \ 'col': 3,
637 \ 'minwidth': 10,
638 \ 'minheight': 11,
639 \})
640 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200641 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200642 call assert_equal(2, res.line)
643 call assert_equal(3, res.col)
644 call assert_equal(10, res.width)
645 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200646 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200647
648 call popup_close(winid)
649endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200650
651func Test_popup_width_longest()
652 let tests = [
653 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
654 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
655 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
656 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
657 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
658 \ ]
659
660 for test in tests
661 let winid = popup_create(test[0], {'line': 2, 'col': 3})
662 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200663 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200664 call assert_equal(test[1], position.width)
665 call popup_close(winid)
666 endfor
667endfunc
668
669func Test_popup_wraps()
670 let tests = [
671 \ ['nowrap', 6, 1],
672 \ ['a line that wraps once', 12, 2],
673 \ ['a line that wraps two times', 12, 3],
674 \ ]
675 for test in tests
676 let winid = popup_create(test[0],
677 \ {'line': 2, 'col': 3, 'maxwidth': 12})
678 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200679 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200680 call assert_equal(test[1], position.width)
681 call assert_equal(test[2], position.height)
682
683 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200684 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200685 endfor
686endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200687
688func Test_popup_getoptions()
689 let winid = popup_create('hello', {
690 \ 'line': 2,
691 \ 'col': 3,
692 \ 'minwidth': 10,
693 \ 'minheight': 11,
694 \ 'maxwidth': 20,
695 \ 'maxheight': 21,
696 \ 'zindex': 100,
697 \ 'time': 5000,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200698 \ 'fixed': 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200699 \})
700 redraw
701 let res = popup_getoptions(winid)
702 call assert_equal(2, res.line)
703 call assert_equal(3, res.col)
704 call assert_equal(10, res.minwidth)
705 call assert_equal(11, res.minheight)
706 call assert_equal(20, res.maxwidth)
707 call assert_equal(21, res.maxheight)
708 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200709 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200710 if has('timers')
711 call assert_equal(5000, res.time)
712 endif
713 call popup_close(winid)
714
715 let winid = popup_create('hello', {})
716 redraw
717 let res = popup_getoptions(winid)
718 call assert_equal(0, res.line)
719 call assert_equal(0, res.col)
720 call assert_equal(0, res.minwidth)
721 call assert_equal(0, res.minheight)
722 call assert_equal(0, res.maxwidth)
723 call assert_equal(0, res.maxheight)
724 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200725 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200726 if has('timers')
727 call assert_equal(0, res.time)
728 endif
729 call popup_close(winid)
730 call assert_equal({}, popup_getoptions(winid))
731endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200732
733func Test_popup_option_values()
734 new
735 " window-local
736 setlocal number
737 setlocal nowrap
738 " buffer-local
739 setlocal omnifunc=Something
740 " global/buffer-local
741 setlocal path=/there
742 " global/window-local
743 setlocal scrolloff=9
744
745 let winid = popup_create('hello', {})
746 call assert_equal(0, getwinvar(winid, '&number'))
747 call assert_equal(1, getwinvar(winid, '&wrap'))
748 call assert_equal('', getwinvar(winid, '&omnifunc'))
749 call assert_equal(&g:path, getwinvar(winid, '&path'))
750 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
751
752 call popup_close(winid)
753 bwipe
754endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200755
756func Test_popup_atcursor()
757 topleft vnew
758 call setline(1, [
759 \ 'xxxxxxxxxxxxxxxxx',
760 \ 'xxxxxxxxxxxxxxxxx',
761 \ 'xxxxxxxxxxxxxxxxx',
762 \])
763
764 call cursor(2, 2)
765 redraw
766 let winid = popup_atcursor('vim', {})
767 redraw
768 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
769 call assert_equal('xvimxxxxxxxxxxxxx', line)
770 call popup_close(winid)
771
772 call cursor(3, 4)
773 redraw
774 let winid = popup_atcursor('vim', {})
775 redraw
776 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
777 call assert_equal('xxxvimxxxxxxxxxxx', line)
778 call popup_close(winid)
779
780 call cursor(1, 1)
781 redraw
782 let winid = popup_create('vim', {
783 \ 'line': 'cursor+2',
784 \ 'col': 'cursor+1',
785 \})
786 redraw
787 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
788 call assert_equal('xvimxxxxxxxxxxxxx', line)
789 call popup_close(winid)
790
791 call cursor(3, 3)
792 redraw
793 let winid = popup_create('vim', {
794 \ 'line': 'cursor-2',
795 \ 'col': 'cursor-1',
796 \})
797 redraw
798 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
799 call assert_equal('xvimxxxxxxxxxxxxx', line)
800 call popup_close(winid)
801
Bram Moolenaar402502d2019-05-30 22:07:36 +0200802 " just enough room above
803 call cursor(3, 3)
804 redraw
805 let winid = popup_atcursor(['vim', 'is great'], {})
806 redraw
807 let pos = popup_getpos(winid)
808 call assert_equal(1, pos.line)
809 call popup_close(winid)
810
811 " not enough room above, popup goes below the cursor
812 call cursor(3, 3)
813 redraw
814 let winid = popup_atcursor(['vim', 'is', 'great'], {})
815 redraw
816 let pos = popup_getpos(winid)
817 call assert_equal(4, pos.line)
818 call popup_close(winid)
819
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200820 " cursor in first line, popup in line 2
821 call cursor(1, 1)
822 redraw
823 let winid = popup_atcursor(['vim', 'is', 'great'], {})
824 redraw
825 let pos = popup_getpos(winid)
826 call assert_equal(2, pos.line)
827 call popup_close(winid)
828
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200829 bwipe!
830endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200831
832func Test_popup_filter()
833 new
834 call setline(1, 'some text')
835
836 func MyPopupFilter(winid, c)
837 if a:c == 'e'
838 let g:eaten = 'e'
839 return 1
840 endif
841 if a:c == '0'
842 let g:ignored = '0'
843 return 0
844 endif
845 if a:c == 'x'
846 call popup_close(a:winid)
847 return 1
848 endif
849 return 0
850 endfunc
851
852 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
853 redraw
854
855 " e is consumed by the filter
856 call feedkeys('e', 'xt')
857 call assert_equal('e', g:eaten)
858
859 " 0 is ignored by the filter
860 normal $
861 call assert_equal(9, getcurpos()[2])
862 call feedkeys('0', 'xt')
863 call assert_equal('0', g:ignored)
864 call assert_equal(1, getcurpos()[2])
865
866 " x closes the popup
867 call feedkeys('x', 'xt')
868 call assert_equal('e', g:eaten)
869 call assert_equal(-1, winbufnr(winid))
870
871 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200872 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200873endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200874
Bram Moolenaara42d9452019-06-15 21:46:30 +0200875func ShowDialog(key, result)
876 let s:cb_res = 999
877 let winid = popup_dialog('do you want to quit (Yes/no)?', {
878 \ 'filter': 'popup_filter_yesno',
879 \ 'callback': 'QuitCallback',
880 \ })
881 redraw
882 call feedkeys(a:key, "xt")
883 call assert_equal(winid, s:cb_winid)
884 call assert_equal(a:result, s:cb_res)
885endfunc
886
887func Test_popup_dialog()
888 func QuitCallback(id, res)
889 let s:cb_winid = a:id
890 let s:cb_res = a:res
891 endfunc
892
893 let winid = ShowDialog("y", 1)
894 let winid = ShowDialog("Y", 1)
895 let winid = ShowDialog("n", 0)
896 let winid = ShowDialog("N", 0)
897 let winid = ShowDialog("x", 0)
898 let winid = ShowDialog("X", 0)
899 let winid = ShowDialog("\<Esc>", 0)
900 let winid = ShowDialog("\<C-C>", -1)
901
902 delfunc QuitCallback
903endfunc
904
Bram Moolenaara730e552019-06-16 19:05:31 +0200905func ShowMenu(key, result)
906 let s:cb_res = 999
907 let winid = popup_menu(['one', 'two', 'something else'], {
908 \ 'callback': 'QuitCallback',
909 \ })
910 redraw
911 call feedkeys(a:key, "xt")
912 call assert_equal(winid, s:cb_winid)
913 call assert_equal(a:result, s:cb_res)
914endfunc
915
916func Test_popup_menu()
917 func QuitCallback(id, res)
918 let s:cb_winid = a:id
919 let s:cb_res = a:res
920 endfunc
921
922 let winid = ShowMenu(" ", 1)
923 let winid = ShowMenu("j \<CR>", 2)
924 let winid = ShowMenu("JjK \<CR>", 2)
925 let winid = ShowMenu("jjjjjj ", 3)
926 let winid = ShowMenu("kkk ", 1)
927 let winid = ShowMenu("x", -1)
928 let winid = ShowMenu("X", -1)
929 let winid = ShowMenu("\<Esc>", -1)
930 let winid = ShowMenu("\<C-C>", -1)
931
932 delfunc QuitCallback
933endfunc
934
935func Test_popup_menu_screenshot()
936 if !CanRunVimInTerminal()
937 throw 'Skipped: cannot make screendumps'
938 endif
939
940 let lines =<< trim END
941 call setline(1, range(1, 20))
942 hi PopupSelected ctermbg=lightblue
943 call popup_menu(['one', 'two', 'another'], {'callback': 'MenuDone'})
944 func MenuDone(id, res)
945 echomsg "selected " .. a:res
946 endfunc
947 END
948 call writefile(lines, 'XtestPopupMenu')
949 let buf = RunVimInTerminal('-S XtestPopupMenu', {'rows': 10})
950 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
951
952 call term_sendkeys(buf, "jj")
953 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
954
955 call term_sendkeys(buf, " ")
956 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
957
958 " clean up
959 call StopVimInTerminal(buf)
960 call delete('XtestPopupMenu')
961endfunc
962
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200963func Test_popup_close_callback()
964 func PopupDone(id, result)
965 let g:result = a:result
966 endfunc
967 let winid = popup_create('something', {'callback': 'PopupDone'})
968 redraw
969 call popup_close(winid, 'done')
970 call assert_equal('done', g:result)
971endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200972
973func Test_popup_empty()
974 let winid = popup_create('', {'padding': [2,2,2,2]})
975 redraw
976 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200977 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200978 call assert_equal(5, pos.height)
979
980 let winid = popup_create([], {'border': []})
981 redraw
982 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200983 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200984 call assert_equal(3, pos.height)
985endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +0200986
987func Test_popup_never_behind()
988 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200989 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +0200990 endif
991 " +-----------------------------+
992 " | | |
993 " | | |
994 " | | |
995 " | line1 |
996 " |------------line2------------|
997 " | line3 |
998 " | line4 |
999 " | |
1000 " | |
1001 " +-----------------------------+
1002 let lines =<< trim END
1003 only
1004 split
1005 vsplit
1006 let info_window1 = getwininfo()[0]
1007 let line = info_window1['height']
1008 let col = info_window1['width']
1009 call popup_create(['line1', 'line2', 'line3', 'line4'], {
1010 \ 'line' : line,
1011 \ 'col' : col,
1012 \ })
1013 END
1014 call writefile(lines, 'XtestPopupBehind')
1015 let buf = RunVimInTerminal('-S XtestPopupBehind', {'rows': 10})
1016 call term_sendkeys(buf, "\<C-W>w")
1017 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1018
1019 " clean up
1020 call StopVimInTerminal(buf)
1021 call delete('XtestPopupBehind')
1022endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001023
1024func s:VerifyPosition( p, msg, line, col, width, height )
1025 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
1026 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
1027 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
1028 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
1029endfunc
1030
1031func Test_popup_position_adjust()
1032 " Anything placed past 2 cells from of the right of the screen is moved to the
1033 " left.
1034 "
1035 " When wrapping is disabled, we also shift to the left to display on the
1036 " screen, unless fixed is set.
1037
1038 " Entries for cases which don't vary based on wrapping.
1039 " Format is per tests described below
1040 let both_wrap_tests = [
1041 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
1042 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
1043 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
1044 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
1045 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
1046 \
1047 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
1048 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
1049 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
1050 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
1051 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
1052 \
1053 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1054 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1055 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1056 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1057 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
1058 \ ]
1059
1060 " these test groups are dicts with:
1061 " - comment: something to identify the group of tests by
1062 " - options: dict of options to merge with the row/col in tests
1063 " - tests: list of cases. Each one is a list with elements:
1064 " - text
1065 " - row
1066 " - col
1067 " - expected row
1068 " - expected col
1069 " - expected width
1070 " - expected height
1071 let tests = [
1072 \ {
1073 \ 'comment': 'left-aligned with wrapping',
1074 \ 'options': {
1075 \ 'wrap': 1,
1076 \ 'pos': 'botleft',
1077 \ },
1078 \ 'tests': both_wrap_tests + [
1079 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
1080 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
1081 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
1082 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
1083 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1084 \ ],
1085 \ },
1086 \ {
1087 \ 'comment': 'left aligned without wrapping',
1088 \ 'options': {
1089 \ 'wrap': 0,
1090 \ 'pos': 'botleft',
1091 \ },
1092 \ 'tests': both_wrap_tests + [
1093 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
1094 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
1095 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
1096 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
1097 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1098 \ ],
1099 \ },
1100 \ {
1101 \ 'comment': 'left aligned with fixed position',
1102 \ 'options': {
1103 \ 'wrap': 0,
1104 \ 'fixed': 1,
1105 \ 'pos': 'botleft',
1106 \ },
1107 \ 'tests': both_wrap_tests + [
1108 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1109 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1110 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1111 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1112 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1113 \ ],
1114 \ },
1115 \ ]
1116
1117 for test_group in tests
1118 for test in test_group.tests
1119 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
1120 let options = {
1121 \ 'line': line,
1122 \ 'col': col,
1123 \ }
1124 call extend( options, test_group.options )
1125
1126 let p = popup_create( text, options )
1127
1128 let msg = string( extend( options, { 'text': text } ) )
1129 call s:VerifyPosition( p, msg, e_line, e_col, e_width, e_height )
1130 call popup_close( p )
1131 endfor
1132 endfor
1133
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001134 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001135 %bwipe!
1136endfunc
1137
Bram Moolenaar3397f742019-06-02 18:40:06 +02001138func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001139 " width of screen
1140 let X = join(map(range(&columns), {->'X'}), '')
1141
1142 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1143 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
1144
1145 redraw
1146 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1147 call assert_equal(X, line)
1148
1149 call popup_close( p )
1150 redraw
1151
1152 " Same if placed on the right hand side
1153 let p = popup_create( X, { 'line': 1, 'col': &columns, 'wrap': 0 } )
1154 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
1155
1156 redraw
1157 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1158 call assert_equal(X, line)
1159
1160 call popup_close( p )
1161 redraw
1162
1163 " Extend so > window width
1164 let X .= 'x'
1165
1166 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1167 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
1168
1169 redraw
1170 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1171 call assert_equal(X[ : -2 ], line)
1172
1173 call popup_close( p )
1174 redraw
1175
1176 " Shifted then truncated (the x is not visible)
1177 let p = popup_create( X, { 'line': 1, 'col': &columns - 3, 'wrap': 0 } )
1178 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
1179
1180 redraw
1181 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1182 call assert_equal(X[ : -2 ], line)
1183
1184 call popup_close( p )
1185 redraw
1186
1187 " Not shifted, just truncated
1188 let p = popup_create( X,
1189 \ { 'line': 1, 'col': 2, 'wrap': 0, 'fixed': 1 } )
1190 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
1191
1192 redraw
1193 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1194 let e_line = ' ' . X[ 1 : -2 ]
1195 call assert_equal(e_line, line)
1196
1197 call popup_close( p )
1198 redraw
1199
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001200 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001201 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001202endfunc
1203
1204func Test_popup_moved()
1205 new
1206 call test_override('char_avail', 1)
1207 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1208
1209 exe "normal gg0/word\<CR>"
1210 let winid = popup_atcursor('text', {'moved': 'any'})
1211 redraw
1212 call assert_equal(1, popup_getpos(winid).visible)
1213 " trigger the check for last_cursormoved by going into insert mode
1214 call feedkeys("li\<Esc>", 'xt')
1215 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001216 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001217
1218 exe "normal gg0/word\<CR>"
1219 let winid = popup_atcursor('text', {'moved': 'word'})
1220 redraw
1221 call assert_equal(1, popup_getpos(winid).visible)
1222 call feedkeys("hi\<Esc>", 'xt')
1223 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001224 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001225
1226 exe "normal gg0/word\<CR>"
1227 let winid = popup_atcursor('text', {'moved': 'word'})
1228 redraw
1229 call assert_equal(1, popup_getpos(winid).visible)
1230 call feedkeys("li\<Esc>", 'xt')
1231 call assert_equal(1, popup_getpos(winid).visible)
1232 call feedkeys("ei\<Esc>", 'xt')
1233 call assert_equal(1, popup_getpos(winid).visible)
1234 call feedkeys("eli\<Esc>", 'xt')
1235 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001236 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001237
Bram Moolenaar17627312019-06-02 19:53:44 +02001238 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001239 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001240 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001241 redraw
1242 call assert_equal(1, popup_getpos(winid).visible)
1243 call feedkeys("eli\<Esc>", 'xt')
1244 call assert_equal(1, popup_getpos(winid).visible)
1245 call feedkeys("wi\<Esc>", 'xt')
1246 call assert_equal(1, popup_getpos(winid).visible)
1247 call feedkeys("Eli\<Esc>", 'xt')
1248 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001249 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001250
1251 exe "normal gg0/word\<CR>"
1252 let winid = popup_atcursor('text', {'moved': [5, 10]})
1253 redraw
1254 call assert_equal(1, popup_getpos(winid).visible)
1255 call feedkeys("eli\<Esc>", 'xt')
1256 call feedkeys("ei\<Esc>", 'xt')
1257 call assert_equal(1, popup_getpos(winid).visible)
1258 call feedkeys("eli\<Esc>", 'xt')
1259 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001260 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001261
1262 bwipe!
1263 call test_override('ALL', 0)
1264endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001265
1266func Test_notifications()
1267 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02001268 throw 'Skipped: timer feature not supported'
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001269 endif
1270 if !CanRunVimInTerminal()
1271 throw 'Skipped: cannot make screendumps'
1272 endif
1273
1274 call writefile([
1275 \ "call setline(1, range(1, 20))",
1276 \ "hi Notification ctermbg=lightblue",
1277 \ "call popup_notification('first notification', {})",
1278 \], 'XtestNotifications')
1279 let buf = RunVimInTerminal('-S XtestNotifications', {'rows': 10})
1280 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1281
1282 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001283 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1284 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001285 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1286
1287
1288 " clean up
1289 call StopVimInTerminal(buf)
1290 call delete('XtestNotifications')
1291endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001292
1293function Test_popup_settext()
1294 if !CanRunVimInTerminal()
1295 throw 'Skipped: cannot make screendumps'
1296 endif
1297
1298 let lines =<< trim END
1299 let opts = {'wrap': 0}
1300 let p = popup_create('test', opts)
1301 call popup_settext(p, 'this is a text')
1302 END
1303
1304 call writefile( lines, 'XtestPopupSetText' )
1305 let buf = RunVimInTerminal('-S XtestPopupSetText', {'rows': 10})
1306 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1307
1308 " Setting to empty string clears it
1309 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1310 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1311
1312 " Setting a list
1313 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1314 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1315
1316 " Shrinking with a list
1317 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1318 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1319
1320 " Growing with a list
1321 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1322 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1323
1324 " Empty list clears
1325 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1326 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1327
1328 " Dicts
1329 call term_sendkeys(buf, ":call popup_settext(p, [{'text': 'aaaa'}, {'text': 'bbbb'}, {'text': 'cccc'}])\<CR>")
1330 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1331
1332 " clean up
1333 call StopVimInTerminal(buf)
1334 call delete('XtestPopupSetText')
1335endfunction