blob: f7a319c23f1abae5769b7359bafec18643912c45 [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]})",
90 \], 'XtestPopupBorder')
91 let buf = RunVimInTerminal('-S XtestPopupBorder', {'rows': 15})
92 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
93
94 call StopVimInTerminal(buf)
95 call delete('XtestPopupBorder')
96 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020097
Bram Moolenaar790498b2019-06-01 22:15:29 +020098 call writefile([
99 \ "call setline(1, range(1, 100))",
100 \ "hi BlueColor ctermbg=lightblue",
101 \ "hi TopColor ctermbg=253",
102 \ "hi RightColor ctermbg=245",
103 \ "hi BottomColor ctermbg=240",
104 \ "hi LeftColor ctermbg=248",
105 \ "call popup_create('hello border', {'line': 2, 'col': 3, 'border': [], 'borderhighlight': ['BlueColor']})",
106 \ "call popup_create(['hello border', 'and more'], {'line': 2, 'col': 23, 'border': [], 'borderhighlight': ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})",
107 \ "call popup_create(['hello border', 'lines only'], {'line': 2, 'col': 43, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['x']})",
108 \ "call popup_create(['hello border', 'with corners'], {'line': 2, 'col': 60, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['x', '#']})",
109 \ "call popup_create(['hello border', 'with numbers'], {'line': 6, 'col': 3, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['0', '1', '2', '3', '4', '5', '6', '7']})",
110 \ "call popup_create(['hello border', 'just blanks'], {'line': 7, 'col': 23, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': [' ']})",
111 \], 'XtestPopupBorder')
112 let buf = RunVimInTerminal('-S XtestPopupBorder', {'rows': 12})
113 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
114
115 call StopVimInTerminal(buf)
116 call delete('XtestPopupBorder')
117
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200118 let with_border_or_padding = {
119 \ 'line': 2,
120 \ 'core_line': 3,
121 \ 'col': 3,
122 \ 'core_col': 4,
123 \ 'width': 14,
124 \ 'core_width': 12,
125 \ 'height': 3,
126 \ 'core_height': 1,
127 \ 'visible': 1}
128 let winid = popup_create('hello border', {'line': 2, 'col': 3, 'border': []})",
129 call assert_equal(with_border_or_padding, popup_getpos(winid))
130
131 let winid = popup_create('hello paddng', {'line': 2, 'col': 3, 'padding': []})
132 call assert_equal(with_border_or_padding, popup_getpos(winid))
133
134 let winid = popup_create('hello both', {'line': 3, 'col': 8, 'border': [], 'padding': []})
135 call assert_equal({
136 \ 'line': 3,
137 \ 'core_line': 5,
138 \ 'col': 8,
139 \ 'core_col': 10,
140 \ 'width': 14,
141 \ 'core_width': 10,
142 \ 'height': 5,
143 \ 'core_height': 1,
144 \ 'visible': 1}, popup_getpos(winid))
145endfunc
146
Bram Moolenaarb4230122019-05-30 18:40:53 +0200147func Test_popup_with_syntax_win_execute()
148 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200149 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200150 endif
151 call writefile([
152 \ "call setline(1, range(1, 100))",
153 \ "hi PopupColor ctermbg=lightblue",
154 \ "let winid = popup_create([",
155 \ "\\ '#include <stdio.h>',",
156 \ "\\ 'int main(void)',",
157 \ "\\ '{',",
158 \ "\\ ' printf(123);',",
159 \ "\\ '}',",
160 \ "\\], {'line': 3, 'col': 25, 'highlight': 'PopupColor'})",
161 \ "call win_execute(winid, 'set syntax=cpp')",
162 \], 'XtestPopup')
163 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
164 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
165
166 " clean up
167 call StopVimInTerminal(buf)
168 call delete('XtestPopup')
169endfunc
170
171func Test_popup_with_syntax_setbufvar()
172 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200173 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200174 endif
Bram Moolenaar402502d2019-05-30 22:07:36 +0200175 let lines =<< trim END
176 call setline(1, range(1, 100))
177 hi PopupColor ctermbg=lightgrey
178 let winid = popup_create([
179 \ '#include <stdio.h>',
180 \ 'int main(void)',
181 \ '{',
182 \ ' printf(567);',
183 \ '}',
184 \], {'line': 3, 'col': 21, 'highlight': 'PopupColor'})
185 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
186 END
187 call writefile(lines, 'XtestPopup')
Bram Moolenaarb4230122019-05-30 18:40:53 +0200188 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
189 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
190
191 " clean up
192 call StopVimInTerminal(buf)
193 call delete('XtestPopup')
194endfunc
195
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200196func Test_popup_with_matches()
197 if !CanRunVimInTerminal()
198 throw 'Skipped: cannot make screendumps'
199 endif
200 let lines =<< trim END
201 call setline(1, ['111 222 333', '444 555 666'])
202 let winid = popup_create([
203 \ '111 222 333',
204 \ '444 555 666',
205 \], {'line': 3, 'col': 10, 'border': []})
206 set hlsearch
207 /666
208 call matchadd('ErrorMsg', '111')
209 call matchadd('ErrorMsg', '444')
210 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
211 call win_execute(winid, "call matchadd('ErrorMsg', '555')")
212 END
213 call writefile(lines, 'XtestPopupMatches')
214 let buf = RunVimInTerminal('-S XtestPopupMatches', {'rows': 10})
215 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
216
217 " clean up
218 call StopVimInTerminal(buf)
219 call delete('XtestPopupMatches')
220endfunc
221
Bram Moolenaar399d8982019-06-02 15:34:29 +0200222func Test_popup_all_corners()
223 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200224 throw 'Skipped: cannot make screendumps'
Bram Moolenaar399d8982019-06-02 15:34:29 +0200225 endif
226 let lines =<< trim END
227 call setline(1, repeat([repeat('-', 60)], 15))
228 set so=0
229 normal 2G3|r#
230 let winid1 = popup_create(['first', 'second'], {
231 \ 'line': 'cursor+1',
232 \ 'col': 'cursor',
233 \ 'pos': 'topleft',
234 \ 'border': [],
235 \ 'padding': [],
236 \ })
237 normal 25|r@
238 let winid1 = popup_create(['First', 'SeconD'], {
239 \ 'line': 'cursor+1',
240 \ 'col': 'cursor',
241 \ 'pos': 'topright',
242 \ 'border': [],
243 \ 'padding': [],
244 \ })
245 normal 9G29|r%
246 let winid1 = popup_create(['fiRSt', 'seCOnd'], {
247 \ 'line': 'cursor-1',
248 \ 'col': 'cursor',
249 \ 'pos': 'botleft',
250 \ 'border': [],
251 \ 'padding': [],
252 \ })
253 normal 51|r&
254 let winid1 = popup_create(['FIrsT', 'SEcoND'], {
255 \ 'line': 'cursor-1',
256 \ 'col': 'cursor',
257 \ 'pos': 'botright',
258 \ 'border': [],
259 \ 'padding': [],
260 \ })
261 END
262 call writefile(lines, 'XtestPopupCorners')
263 let buf = RunVimInTerminal('-S XtestPopupCorners', {'rows': 12})
264 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
265
266 " clean up
267 call StopVimInTerminal(buf)
268 call delete('XtestPopupCorners')
269endfunc
270
Bram Moolenaar8d241042019-06-12 23:40:01 +0200271func Test_popup_firstline()
272 if !CanRunVimInTerminal()
273 throw 'Skipped: cannot make screendumps'
274 endif
275 let lines =<< trim END
276 call setline(1, range(1, 20))
277 call popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], {
278 \ 'maxheight': 4,
279 \ 'firstline': 3,
280 \ })
281 END
282 call writefile(lines, 'XtestPopupFirstline')
283 let buf = RunVimInTerminal('-S XtestPopupFirstline', {'rows': 10})
284 call VerifyScreenDump(buf, 'Test_popupwin_firstline', {})
285
286 " clean up
287 call StopVimInTerminal(buf)
288 call delete('XtestPopupFirstline')
289endfunc
290
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200291func Test_popup_drag()
292 if !CanRunVimInTerminal()
293 throw 'Skipped: cannot make screendumps'
294 endif
295 " create a popup that covers the command line
296 let lines =<< trim END
297 call setline(1, range(1, 20))
298 let winid = popup_create(['1111', '222222', '33333'], {
299 \ 'drag': 1,
300 \ 'border': [],
301 \ 'line': &lines - 4,
302 \ })
303 func Dragit()
304 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
305 endfunc
306 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
307 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2)<CR>
308 END
309 call writefile(lines, 'XtestPopupDrag')
310 let buf = RunVimInTerminal('-S XtestPopupDrag', {'rows': 10})
311 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
312
313 call term_sendkeys(buf, ":call Dragit()\<CR>")
314 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
315
316 " clean up
317 call StopVimInTerminal(buf)
318 call delete('XtestPopupDrag')
319endfunc
320
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200321func Test_popup_select()
322 if !CanRunVimInTerminal()
323 throw 'Skipped: cannot make screendumps'
324 endif
Bram Moolenaar650a6372019-06-15 00:29:33 +0200325 if !has('clipboard')
326 throw 'Skipped: clipboard feature missing'
327 endif
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200328 " create a popup with some text to be selected
329 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200330 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200331 call setline(1, range(1, 20))
332 let winid = popup_create(['the word', 'some more', 'several words here'], {
333 \ 'drag': 1,
334 \ 'border': [],
335 \ 'line': 3,
336 \ 'col': 10,
337 \ })
338 func Select1()
339 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
340 endfunc
341 map <silent> <F3> :call test_setmouse(4, 15)<CR>
342 map <silent> <F4> :call test_setmouse(6, 23)<CR>
343 END
344 call writefile(lines, 'XtestPopupSelect')
345 let buf = RunVimInTerminal('-S XtestPopupSelect', {'rows': 10})
346 call term_sendkeys(buf, ":call Select1()\<CR>")
347 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
348
349 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
350 call term_sendkeys(buf, "\"*p")
351 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
352
353 " clean up
354 call StopVimInTerminal(buf)
355 call delete('XtestPopupSelect')
356endfunc
357
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200358func Test_popup_in_tab()
359 " default popup is local to tab, not visible when in other tab
360 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200361 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200362 call assert_equal(1, popup_getpos(winid).visible)
363 tabnew
364 call assert_equal(0, popup_getpos(winid).visible)
365 quit
366 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200367
368 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200369 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200370 " buffer is gone now
371 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200372
373 " global popup is visible in any tab
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200374 let winid = popup_create("text", {'tabpage': -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200375 call assert_equal(1, popup_getpos(winid).visible)
376 tabnew
377 call assert_equal(1, popup_getpos(winid).visible)
378 quit
379 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200380 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200381endfunc
382
383func Test_popup_valid_arguments()
384 " Zero value is like the property wasn't there
385 let winid = popup_create("text", {"col": 0})
386 let pos = popup_getpos(winid)
387 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200388 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200389
390 " using cursor column has minimum value of 1
391 let winid = popup_create("text", {"col": 'cursor-100'})
392 let pos = popup_getpos(winid)
393 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200394 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200395
396 " center
397 let winid = popup_create("text", {"pos": 'center'})
398 let pos = popup_getpos(winid)
399 let around = (&columns - pos.width) / 2
400 call assert_inrange(around - 1, around + 1, pos.col)
401 let around = (&lines - pos.height) / 2
402 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200403 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200404endfunc
405
406func Test_popup_invalid_arguments()
407 call assert_fails('call popup_create(666, {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200408 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200409 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200410 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200411
412 call assert_fails('call popup_create("text", {"col": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200413 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200414 call assert_fails('call popup_create("text", {"col": "cursor8"})', 'E15:')
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": "cursor+x"})', '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+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200419 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200420
421 call assert_fails('call popup_create("text", {"line": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200422 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200423 call assert_fails('call popup_create("text", {"line": "cursor8"})', 'E15:')
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": "cursor+x"})', '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+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200428 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200429
430 call assert_fails('call popup_create("text", {"pos": "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200431 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200432 call assert_fails('call popup_create("text", {"padding": "none"})', 'E714:')
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", {"border": "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", {"borderhighlight": "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", {"borderchars": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200439 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200440
441 call assert_fails('call popup_create([{"text": "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200442 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200443 call assert_fails('call popup_create([{"text": "text", "props": "none"}], {})', 'E714:')
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"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200446 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200447endfunc
448
Bram Moolenaareea16992019-05-31 17:34:48 +0200449func Test_win_execute_closing_curwin()
450 split
451 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200452 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200453 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200454endfunc
455
456func Test_win_execute_not_allowed()
457 let winid = popup_create('some text', {})
458 call assert_fails('call win_execute(winid, "split")', 'E994:')
459 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
460 call assert_fails('call win_execute(winid, "close")', 'E994:')
461 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200462 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200463 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
464 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
465 call assert_fails('call win_execute(winid, "next")', 'E994:')
466 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
467 call assert_fails('call win_execute(winid, "buf")', 'E994:')
468 call assert_fails('call win_execute(winid, "edit")', 'E994:')
469 call assert_fails('call win_execute(winid, "enew")', 'E994:')
470 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
471 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
472 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
473 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200474 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200475endfunc
476
Bram Moolenaar402502d2019-05-30 22:07:36 +0200477func Test_popup_with_wrap()
478 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200479 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200480 endif
481 let lines =<< trim END
482 call setline(1, range(1, 100))
483 let winid = popup_create(
484 \ 'a long line that wont fit',
485 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
486 END
487 call writefile(lines, 'XtestPopup')
488 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
489 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
490
491 " clean up
492 call StopVimInTerminal(buf)
493 call delete('XtestPopup')
494endfunc
495
496func Test_popup_without_wrap()
497 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200498 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200499 endif
500 let lines =<< trim END
501 call setline(1, range(1, 100))
502 let winid = popup_create(
503 \ 'a long line that wont fit',
504 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
505 END
506 call writefile(lines, 'XtestPopup')
507 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
508 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
509
510 " clean up
511 call StopVimInTerminal(buf)
512 call delete('XtestPopup')
513endfunc
514
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200515func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200516 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +0200517 throw 'Skipped: timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200518 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200519 topleft vnew
520 call setline(1, 'hello')
521
522 call popup_create('world', {
523 \ 'line': 1,
524 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200525 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200526 \ 'time': 500,
527 \})
528 redraw
529 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
530 call assert_equal('world', line)
531
532 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200533 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200534 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
535 call assert_equal('hello', line)
536
537 call popup_create('on the command line', {
538 \ 'line': &lines,
539 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200540 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200541 \ 'time': 500,
542 \})
543 redraw
544 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
545 call assert_match('.*on the command line.*', line)
546
547 sleep 700m
548 redraw
549 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
550 call assert_notmatch('.*on the command line.*', line)
551
552 bwipe!
553endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200554
555func Test_popup_hide()
556 topleft vnew
557 call setline(1, 'hello')
558
559 let winid = popup_create('world', {
560 \ 'line': 1,
561 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200562 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200563 \})
564 redraw
565 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
566 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200567 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200568 " buffer is still listed and active
569 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200570
571 call popup_hide(winid)
572 redraw
573 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
574 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200575 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200576 " buffer is still listed but hidden
577 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200578
579 call popup_show(winid)
580 redraw
581 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
582 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200583 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200584
585
586 call popup_close(winid)
587 redraw
588 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
589 call assert_equal('hello', line)
590
591 " error is given for existing non-popup window
592 call assert_fails('call popup_hide(win_getid())', 'E993:')
593
594 " no error non-existing window
595 call popup_hide(1234234)
596 call popup_show(41234234)
597
598 bwipe!
599endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200600
601func Test_popup_move()
602 topleft vnew
603 call setline(1, 'hello')
604
605 let winid = popup_create('world', {
606 \ 'line': 1,
607 \ 'col': 1,
608 \ 'minwidth': 20,
609 \})
610 redraw
611 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
612 call assert_equal('world ', line)
613
614 call popup_move(winid, {'line': 2, 'col': 2})
615 redraw
616 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
617 call assert_equal('hello ', line)
618 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
619 call assert_equal('~world', line)
620
621 call popup_move(winid, {'line': 1})
622 redraw
623 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
624 call assert_equal('hworld', line)
625
626 call popup_close(winid)
627
628 bwipe!
629endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200630
Bram Moolenaar402502d2019-05-30 22:07:36 +0200631func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200632 let winid = popup_create('hello', {
633 \ 'line': 2,
634 \ 'col': 3,
635 \ 'minwidth': 10,
636 \ 'minheight': 11,
637 \})
638 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200639 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200640 call assert_equal(2, res.line)
641 call assert_equal(3, res.col)
642 call assert_equal(10, res.width)
643 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200644 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200645
646 call popup_close(winid)
647endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200648
649func Test_popup_width_longest()
650 let tests = [
651 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
652 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
653 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
654 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
655 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
656 \ ]
657
658 for test in tests
659 let winid = popup_create(test[0], {'line': 2, 'col': 3})
660 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200661 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200662 call assert_equal(test[1], position.width)
663 call popup_close(winid)
664 endfor
665endfunc
666
667func Test_popup_wraps()
668 let tests = [
669 \ ['nowrap', 6, 1],
670 \ ['a line that wraps once', 12, 2],
671 \ ['a line that wraps two times', 12, 3],
672 \ ]
673 for test in tests
674 let winid = popup_create(test[0],
675 \ {'line': 2, 'col': 3, 'maxwidth': 12})
676 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200677 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200678 call assert_equal(test[1], position.width)
679 call assert_equal(test[2], position.height)
680
681 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200682 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200683 endfor
684endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200685
686func Test_popup_getoptions()
687 let winid = popup_create('hello', {
688 \ 'line': 2,
689 \ 'col': 3,
690 \ 'minwidth': 10,
691 \ 'minheight': 11,
692 \ 'maxwidth': 20,
693 \ 'maxheight': 21,
694 \ 'zindex': 100,
695 \ 'time': 5000,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200696 \ 'fixed': 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200697 \})
698 redraw
699 let res = popup_getoptions(winid)
700 call assert_equal(2, res.line)
701 call assert_equal(3, res.col)
702 call assert_equal(10, res.minwidth)
703 call assert_equal(11, res.minheight)
704 call assert_equal(20, res.maxwidth)
705 call assert_equal(21, res.maxheight)
706 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200707 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200708 if has('timers')
709 call assert_equal(5000, res.time)
710 endif
711 call popup_close(winid)
712
713 let winid = popup_create('hello', {})
714 redraw
715 let res = popup_getoptions(winid)
716 call assert_equal(0, res.line)
717 call assert_equal(0, res.col)
718 call assert_equal(0, res.minwidth)
719 call assert_equal(0, res.minheight)
720 call assert_equal(0, res.maxwidth)
721 call assert_equal(0, res.maxheight)
722 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200723 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200724 if has('timers')
725 call assert_equal(0, res.time)
726 endif
727 call popup_close(winid)
728 call assert_equal({}, popup_getoptions(winid))
729endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200730
731func Test_popup_option_values()
732 new
733 " window-local
734 setlocal number
735 setlocal nowrap
736 " buffer-local
737 setlocal omnifunc=Something
738 " global/buffer-local
739 setlocal path=/there
740 " global/window-local
741 setlocal scrolloff=9
742
743 let winid = popup_create('hello', {})
744 call assert_equal(0, getwinvar(winid, '&number'))
745 call assert_equal(1, getwinvar(winid, '&wrap'))
746 call assert_equal('', getwinvar(winid, '&omnifunc'))
747 call assert_equal(&g:path, getwinvar(winid, '&path'))
748 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
749
750 call popup_close(winid)
751 bwipe
752endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200753
754func Test_popup_atcursor()
755 topleft vnew
756 call setline(1, [
757 \ 'xxxxxxxxxxxxxxxxx',
758 \ 'xxxxxxxxxxxxxxxxx',
759 \ 'xxxxxxxxxxxxxxxxx',
760 \])
761
762 call cursor(2, 2)
763 redraw
764 let winid = popup_atcursor('vim', {})
765 redraw
766 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
767 call assert_equal('xvimxxxxxxxxxxxxx', line)
768 call popup_close(winid)
769
770 call cursor(3, 4)
771 redraw
772 let winid = popup_atcursor('vim', {})
773 redraw
774 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
775 call assert_equal('xxxvimxxxxxxxxxxx', line)
776 call popup_close(winid)
777
778 call cursor(1, 1)
779 redraw
780 let winid = popup_create('vim', {
781 \ 'line': 'cursor+2',
782 \ 'col': 'cursor+1',
783 \})
784 redraw
785 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
786 call assert_equal('xvimxxxxxxxxxxxxx', line)
787 call popup_close(winid)
788
789 call cursor(3, 3)
790 redraw
791 let winid = popup_create('vim', {
792 \ 'line': 'cursor-2',
793 \ 'col': 'cursor-1',
794 \})
795 redraw
796 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
797 call assert_equal('xvimxxxxxxxxxxxxx', line)
798 call popup_close(winid)
799
Bram Moolenaar402502d2019-05-30 22:07:36 +0200800 " just enough room above
801 call cursor(3, 3)
802 redraw
803 let winid = popup_atcursor(['vim', 'is great'], {})
804 redraw
805 let pos = popup_getpos(winid)
806 call assert_equal(1, pos.line)
807 call popup_close(winid)
808
809 " not enough room above, popup goes below the cursor
810 call cursor(3, 3)
811 redraw
812 let winid = popup_atcursor(['vim', 'is', 'great'], {})
813 redraw
814 let pos = popup_getpos(winid)
815 call assert_equal(4, pos.line)
816 call popup_close(winid)
817
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200818 " cursor in first line, popup in line 2
819 call cursor(1, 1)
820 redraw
821 let winid = popup_atcursor(['vim', 'is', 'great'], {})
822 redraw
823 let pos = popup_getpos(winid)
824 call assert_equal(2, pos.line)
825 call popup_close(winid)
826
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200827 bwipe!
828endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200829
830func Test_popup_filter()
831 new
832 call setline(1, 'some text')
833
834 func MyPopupFilter(winid, c)
835 if a:c == 'e'
836 let g:eaten = 'e'
837 return 1
838 endif
839 if a:c == '0'
840 let g:ignored = '0'
841 return 0
842 endif
843 if a:c == 'x'
844 call popup_close(a:winid)
845 return 1
846 endif
847 return 0
848 endfunc
849
850 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
851 redraw
852
853 " e is consumed by the filter
854 call feedkeys('e', 'xt')
855 call assert_equal('e', g:eaten)
856
857 " 0 is ignored by the filter
858 normal $
859 call assert_equal(9, getcurpos()[2])
860 call feedkeys('0', 'xt')
861 call assert_equal('0', g:ignored)
862 call assert_equal(1, getcurpos()[2])
863
864 " x closes the popup
865 call feedkeys('x', 'xt')
866 call assert_equal('e', g:eaten)
867 call assert_equal(-1, winbufnr(winid))
868
869 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200870 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200871endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200872
Bram Moolenaara42d9452019-06-15 21:46:30 +0200873func ShowDialog(key, result)
874 let s:cb_res = 999
875 let winid = popup_dialog('do you want to quit (Yes/no)?', {
876 \ 'filter': 'popup_filter_yesno',
877 \ 'callback': 'QuitCallback',
878 \ })
879 redraw
880 call feedkeys(a:key, "xt")
881 call assert_equal(winid, s:cb_winid)
882 call assert_equal(a:result, s:cb_res)
883endfunc
884
885func Test_popup_dialog()
886 func QuitCallback(id, res)
887 let s:cb_winid = a:id
888 let s:cb_res = a:res
889 endfunc
890
891 let winid = ShowDialog("y", 1)
892 let winid = ShowDialog("Y", 1)
893 let winid = ShowDialog("n", 0)
894 let winid = ShowDialog("N", 0)
895 let winid = ShowDialog("x", 0)
896 let winid = ShowDialog("X", 0)
897 let winid = ShowDialog("\<Esc>", 0)
898 let winid = ShowDialog("\<C-C>", -1)
899
900 delfunc QuitCallback
901endfunc
902
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200903func Test_popup_close_callback()
904 func PopupDone(id, result)
905 let g:result = a:result
906 endfunc
907 let winid = popup_create('something', {'callback': 'PopupDone'})
908 redraw
909 call popup_close(winid, 'done')
910 call assert_equal('done', g:result)
911endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200912
913func Test_popup_empty()
914 let winid = popup_create('', {'padding': [2,2,2,2]})
915 redraw
916 let pos = popup_getpos(winid)
917 call assert_equal(4, pos.width)
918 call assert_equal(5, pos.height)
919
920 let winid = popup_create([], {'border': []})
921 redraw
922 let pos = popup_getpos(winid)
923 call assert_equal(2, pos.width)
924 call assert_equal(3, pos.height)
925endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +0200926
927func Test_popup_never_behind()
928 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200929 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +0200930 endif
931 " +-----------------------------+
932 " | | |
933 " | | |
934 " | | |
935 " | line1 |
936 " |------------line2------------|
937 " | line3 |
938 " | line4 |
939 " | |
940 " | |
941 " +-----------------------------+
942 let lines =<< trim END
943 only
944 split
945 vsplit
946 let info_window1 = getwininfo()[0]
947 let line = info_window1['height']
948 let col = info_window1['width']
949 call popup_create(['line1', 'line2', 'line3', 'line4'], {
950 \ 'line' : line,
951 \ 'col' : col,
952 \ })
953 END
954 call writefile(lines, 'XtestPopupBehind')
955 let buf = RunVimInTerminal('-S XtestPopupBehind', {'rows': 10})
956 call term_sendkeys(buf, "\<C-W>w")
957 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
958
959 " clean up
960 call StopVimInTerminal(buf)
961 call delete('XtestPopupBehind')
962endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200963
964func s:VerifyPosition( p, msg, line, col, width, height )
965 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
966 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
967 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
968 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
969endfunc
970
971func Test_popup_position_adjust()
972 " Anything placed past 2 cells from of the right of the screen is moved to the
973 " left.
974 "
975 " When wrapping is disabled, we also shift to the left to display on the
976 " screen, unless fixed is set.
977
978 " Entries for cases which don't vary based on wrapping.
979 " Format is per tests described below
980 let both_wrap_tests = [
981 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
982 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
983 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
984 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
985 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
986 \
987 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
988 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
989 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
990 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
991 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
992 \
993 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
994 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
995 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
996 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
997 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
998 \ ]
999
1000 " these test groups are dicts with:
1001 " - comment: something to identify the group of tests by
1002 " - options: dict of options to merge with the row/col in tests
1003 " - tests: list of cases. Each one is a list with elements:
1004 " - text
1005 " - row
1006 " - col
1007 " - expected row
1008 " - expected col
1009 " - expected width
1010 " - expected height
1011 let tests = [
1012 \ {
1013 \ 'comment': 'left-aligned with wrapping',
1014 \ 'options': {
1015 \ 'wrap': 1,
1016 \ 'pos': 'botleft',
1017 \ },
1018 \ 'tests': both_wrap_tests + [
1019 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
1020 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
1021 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
1022 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
1023 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1024 \ ],
1025 \ },
1026 \ {
1027 \ 'comment': 'left aligned without wrapping',
1028 \ 'options': {
1029 \ 'wrap': 0,
1030 \ 'pos': 'botleft',
1031 \ },
1032 \ 'tests': both_wrap_tests + [
1033 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
1034 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
1035 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
1036 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
1037 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1038 \ ],
1039 \ },
1040 \ {
1041 \ 'comment': 'left aligned with fixed position',
1042 \ 'options': {
1043 \ 'wrap': 0,
1044 \ 'fixed': 1,
1045 \ 'pos': 'botleft',
1046 \ },
1047 \ 'tests': both_wrap_tests + [
1048 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1049 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1050 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1051 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1052 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1053 \ ],
1054 \ },
1055 \ ]
1056
1057 for test_group in tests
1058 for test in test_group.tests
1059 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
1060 let options = {
1061 \ 'line': line,
1062 \ 'col': col,
1063 \ }
1064 call extend( options, test_group.options )
1065
1066 let p = popup_create( text, options )
1067
1068 let msg = string( extend( options, { 'text': text } ) )
1069 call s:VerifyPosition( p, msg, e_line, e_col, e_width, e_height )
1070 call popup_close( p )
1071 endfor
1072 endfor
1073
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001074 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001075 %bwipe!
1076endfunc
1077
Bram Moolenaar3397f742019-06-02 18:40:06 +02001078func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001079 " width of screen
1080 let X = join(map(range(&columns), {->'X'}), '')
1081
1082 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1083 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
1084
1085 redraw
1086 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1087 call assert_equal(X, line)
1088
1089 call popup_close( p )
1090 redraw
1091
1092 " Same if placed on the right hand side
1093 let p = popup_create( X, { 'line': 1, 'col': &columns, 'wrap': 0 } )
1094 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
1095
1096 redraw
1097 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1098 call assert_equal(X, line)
1099
1100 call popup_close( p )
1101 redraw
1102
1103 " Extend so > window width
1104 let X .= 'x'
1105
1106 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1107 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
1108
1109 redraw
1110 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1111 call assert_equal(X[ : -2 ], line)
1112
1113 call popup_close( p )
1114 redraw
1115
1116 " Shifted then truncated (the x is not visible)
1117 let p = popup_create( X, { 'line': 1, 'col': &columns - 3, 'wrap': 0 } )
1118 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
1119
1120 redraw
1121 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1122 call assert_equal(X[ : -2 ], line)
1123
1124 call popup_close( p )
1125 redraw
1126
1127 " Not shifted, just truncated
1128 let p = popup_create( X,
1129 \ { 'line': 1, 'col': 2, 'wrap': 0, 'fixed': 1 } )
1130 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
1131
1132 redraw
1133 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1134 let e_line = ' ' . X[ 1 : -2 ]
1135 call assert_equal(e_line, line)
1136
1137 call popup_close( p )
1138 redraw
1139
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001140 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001141 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001142endfunc
1143
1144func Test_popup_moved()
1145 new
1146 call test_override('char_avail', 1)
1147 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1148
1149 exe "normal gg0/word\<CR>"
1150 let winid = popup_atcursor('text', {'moved': 'any'})
1151 redraw
1152 call assert_equal(1, popup_getpos(winid).visible)
1153 " trigger the check for last_cursormoved by going into insert mode
1154 call feedkeys("li\<Esc>", 'xt')
1155 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001156 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001157
1158 exe "normal gg0/word\<CR>"
1159 let winid = popup_atcursor('text', {'moved': 'word'})
1160 redraw
1161 call assert_equal(1, popup_getpos(winid).visible)
1162 call feedkeys("hi\<Esc>", 'xt')
1163 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001164 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001165
1166 exe "normal gg0/word\<CR>"
1167 let winid = popup_atcursor('text', {'moved': 'word'})
1168 redraw
1169 call assert_equal(1, popup_getpos(winid).visible)
1170 call feedkeys("li\<Esc>", 'xt')
1171 call assert_equal(1, popup_getpos(winid).visible)
1172 call feedkeys("ei\<Esc>", 'xt')
1173 call assert_equal(1, popup_getpos(winid).visible)
1174 call feedkeys("eli\<Esc>", 'xt')
1175 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001176 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001177
Bram Moolenaar17627312019-06-02 19:53:44 +02001178 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001179 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001180 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001181 redraw
1182 call assert_equal(1, popup_getpos(winid).visible)
1183 call feedkeys("eli\<Esc>", 'xt')
1184 call assert_equal(1, popup_getpos(winid).visible)
1185 call feedkeys("wi\<Esc>", 'xt')
1186 call assert_equal(1, popup_getpos(winid).visible)
1187 call feedkeys("Eli\<Esc>", 'xt')
1188 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001189 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001190
1191 exe "normal gg0/word\<CR>"
1192 let winid = popup_atcursor('text', {'moved': [5, 10]})
1193 redraw
1194 call assert_equal(1, popup_getpos(winid).visible)
1195 call feedkeys("eli\<Esc>", 'xt')
1196 call feedkeys("ei\<Esc>", 'xt')
1197 call assert_equal(1, popup_getpos(winid).visible)
1198 call feedkeys("eli\<Esc>", 'xt')
1199 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001200 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001201
1202 bwipe!
1203 call test_override('ALL', 0)
1204endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001205
1206func Test_notifications()
1207 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02001208 throw 'Skipped: timer feature not supported'
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001209 endif
1210 if !CanRunVimInTerminal()
1211 throw 'Skipped: cannot make screendumps'
1212 endif
1213
1214 call writefile([
1215 \ "call setline(1, range(1, 20))",
1216 \ "hi Notification ctermbg=lightblue",
1217 \ "call popup_notification('first notification', {})",
1218 \], 'XtestNotifications')
1219 let buf = RunVimInTerminal('-S XtestNotifications', {'rows': 10})
1220 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1221
1222 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001223 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1224 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001225 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1226
1227
1228 " clean up
1229 call StopVimInTerminal(buf)
1230 call delete('XtestNotifications')
1231endfunc