blob: d9b0b295f742465816b2178009eda43f0ce93f11 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
3if !has('textprop')
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02004 throw 'Skipped: textprop feature missing'
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005endif
6
7source screendump.vim
8
9func Test_simple_popup()
10 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020011 throw 'Skipped: cannot make screendumps'
Bram Moolenaar4d784b22019-05-25 19:51:39 +020012 endif
13 call writefile([
14 \ "call setline(1, range(1, 100))",
Bram Moolenaar20c023a2019-05-26 21:03:24 +020015 \ "hi PopupColor1 ctermbg=lightblue",
16 \ "hi PopupColor2 ctermbg=lightcyan",
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020017 \ "hi Comment ctermfg=red",
18 \ "call prop_type_add('comment', {'highlight': 'Comment'})",
Bram Moolenaar60cdb302019-05-27 21:54:10 +020019 \ "let winid = popup_create('hello there', {'line': 3, 'col': 11, 'minwidth': 20, 'highlight': 'PopupColor1'})",
20 \ "let winid2 = popup_create(['another one', 'another two', 'another three'], {'line': 3, 'col': 25, 'minwidth': 20})",
Bram Moolenaar20c023a2019-05-26 21:03:24 +020021 \ "call setwinvar(winid2, '&wincolor', 'PopupColor2')",
Bram Moolenaar4d784b22019-05-25 19:51:39 +020022 \], 'XtestPopup')
23 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
24 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020029 \ .. "{'text': 'other tab'},"
30 \ .. "{'text': 'a comment line', 'props': [{"
Bram Moolenaar60cdb302019-05-27 21:54:10 +020031 \ .. "'col': 3, 'length': 7, 'minwidth': 20, 'type': 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar60cdb302019-05-27 21:54:10 +020033 \ .. "], {'line': 4, 'col': 9, 'minwidth': 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
35
36 " switch back to first tabpage
37 call term_sendkeys(buf, "gt")
38 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
39
40 " close that tabpage
41 call term_sendkeys(buf, ":quit!\<CR>")
42 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
43
Bram Moolenaar202d9822019-06-11 21:56:30 +020044 " set 'columns' to a small value, size must be recomputed
45 call term_sendkeys(buf, ":let cols = &columns\<CR>")
46 call term_sendkeys(buf, ":set columns=12\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
48 call term_sendkeys(buf, ":let &columns = cols\<CR>")
49
Bram Moolenaar17146962019-05-30 00:12:11 +020050 " resize popup, show empty line at bottom
Bram Moolenaar60cdb302019-05-27 21:54:10 +020051 call term_sendkeys(buf, ":call popup_move(popupwin, {'minwidth': 15, 'maxwidth': 25, 'minheight': 3, 'maxheight': 5})\<CR>")
52 call term_sendkeys(buf, ":redraw\<CR>")
53 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
54
Bram Moolenaar17146962019-05-30 00:12:11 +020055 " show not fitting line at bottom
56 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
57 call term_sendkeys(buf, ":redraw\<CR>")
58 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
59
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020060 " move popup over ruler
61 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
62 call term_sendkeys(buf, ":call popup_move(popupwin, {'line': 7, 'col': 55})\<CR>")
63 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
64
65 " clear all popups after moving the cursor a bit, so that ruler is updated
66 call term_sendkeys(buf, "axxx\<Esc>")
67 call term_wait(buf)
68 call term_sendkeys(buf, "0")
69 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020070 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020071 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
72
Bram Moolenaar4d784b22019-05-25 19:51:39 +020073 " clean up
74 call StopVimInTerminal(buf)
75 call delete('XtestPopup')
76endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020077
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020078func Test_popup_with_border_and_padding()
79 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020080 throw 'Skipped: cannot make screendumps'
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081 endif
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020083 for iter in range(0, 1)
84 call writefile([iter == 1 ? '' : 'set enc=latin1',
85 \ "call setline(1, range(1, 100))",
86 \ "call popup_create('hello border', {'line': 2, 'col': 3, 'border': []})",
87 \ "call popup_create('hello padding', {'line': 2, 'col': 23, 'padding': []})",
88 \ "call popup_create('hello both', {'line': 2, 'col': 43, 'border': [], 'padding': []})",
89 \ "call popup_create('border TL', {'line': 6, 'col': 3, 'border': [1, 0, 0, 4]})",
90 \ "call popup_create('paddings', {'line': 6, 'col': 23, 'padding': [1, 3, 2, 4]})",
91 \], 'XtestPopupBorder')
92 let buf = RunVimInTerminal('-S XtestPopupBorder', {'rows': 15})
93 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
94
95 call StopVimInTerminal(buf)
96 call delete('XtestPopupBorder')
97 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020098
Bram Moolenaar790498b2019-06-01 22:15:29 +020099 call writefile([
100 \ "call setline(1, range(1, 100))",
101 \ "hi BlueColor ctermbg=lightblue",
102 \ "hi TopColor ctermbg=253",
103 \ "hi RightColor ctermbg=245",
104 \ "hi BottomColor ctermbg=240",
105 \ "hi LeftColor ctermbg=248",
106 \ "call popup_create('hello border', {'line': 2, 'col': 3, 'border': [], 'borderhighlight': ['BlueColor']})",
107 \ "call popup_create(['hello border', 'and more'], {'line': 2, 'col': 23, 'border': [], 'borderhighlight': ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})",
108 \ "call popup_create(['hello border', 'lines only'], {'line': 2, 'col': 43, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['x']})",
109 \ "call popup_create(['hello border', 'with corners'], {'line': 2, 'col': 60, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['x', '#']})",
110 \ "call popup_create(['hello border', 'with numbers'], {'line': 6, 'col': 3, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['0', '1', '2', '3', '4', '5', '6', '7']})",
111 \ "call popup_create(['hello border', 'just blanks'], {'line': 7, 'col': 23, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': [' ']})",
112 \], 'XtestPopupBorder')
113 let buf = RunVimInTerminal('-S XtestPopupBorder', {'rows': 12})
114 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
115
116 call StopVimInTerminal(buf)
117 call delete('XtestPopupBorder')
118
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200119 let with_border_or_padding = {
120 \ 'line': 2,
121 \ 'core_line': 3,
122 \ 'col': 3,
123 \ 'core_col': 4,
124 \ 'width': 14,
125 \ 'core_width': 12,
126 \ 'height': 3,
127 \ 'core_height': 1,
128 \ 'visible': 1}
129 let winid = popup_create('hello border', {'line': 2, 'col': 3, 'border': []})",
130 call assert_equal(with_border_or_padding, popup_getpos(winid))
131
132 let winid = popup_create('hello paddng', {'line': 2, 'col': 3, 'padding': []})
133 call assert_equal(with_border_or_padding, popup_getpos(winid))
134
135 let winid = popup_create('hello both', {'line': 3, 'col': 8, 'border': [], 'padding': []})
136 call assert_equal({
137 \ 'line': 3,
138 \ 'core_line': 5,
139 \ 'col': 8,
140 \ 'core_col': 10,
141 \ 'width': 14,
142 \ 'core_width': 10,
143 \ 'height': 5,
144 \ 'core_height': 1,
145 \ 'visible': 1}, popup_getpos(winid))
146endfunc
147
Bram Moolenaarb4230122019-05-30 18:40:53 +0200148func Test_popup_with_syntax_win_execute()
149 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200150 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200151 endif
152 call writefile([
153 \ "call setline(1, range(1, 100))",
154 \ "hi PopupColor ctermbg=lightblue",
155 \ "let winid = popup_create([",
156 \ "\\ '#include <stdio.h>',",
157 \ "\\ 'int main(void)',",
158 \ "\\ '{',",
159 \ "\\ ' printf(123);',",
160 \ "\\ '}',",
161 \ "\\], {'line': 3, 'col': 25, 'highlight': 'PopupColor'})",
162 \ "call win_execute(winid, 'set syntax=cpp')",
163 \], 'XtestPopup')
164 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
165 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
166
167 " clean up
168 call StopVimInTerminal(buf)
169 call delete('XtestPopup')
170endfunc
171
172func Test_popup_with_syntax_setbufvar()
173 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200174 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200175 endif
Bram Moolenaar402502d2019-05-30 22:07:36 +0200176 let lines =<< trim END
177 call setline(1, range(1, 100))
178 hi PopupColor ctermbg=lightgrey
179 let winid = popup_create([
180 \ '#include <stdio.h>',
181 \ 'int main(void)',
182 \ '{',
183 \ ' printf(567);',
184 \ '}',
185 \], {'line': 3, 'col': 21, 'highlight': 'PopupColor'})
186 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
187 END
188 call writefile(lines, 'XtestPopup')
Bram Moolenaarb4230122019-05-30 18:40:53 +0200189 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
190 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
191
192 " clean up
193 call StopVimInTerminal(buf)
194 call delete('XtestPopup')
195endfunc
196
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200197func Test_popup_with_matches()
198 if !CanRunVimInTerminal()
199 throw 'Skipped: cannot make screendumps'
200 endif
201 let lines =<< trim END
202 call setline(1, ['111 222 333', '444 555 666'])
203 let winid = popup_create([
204 \ '111 222 333',
205 \ '444 555 666',
206 \], {'line': 3, 'col': 10, 'border': []})
207 set hlsearch
208 /666
209 call matchadd('ErrorMsg', '111')
210 call matchadd('ErrorMsg', '444')
211 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
212 call win_execute(winid, "call matchadd('ErrorMsg', '555')")
213 END
214 call writefile(lines, 'XtestPopupMatches')
215 let buf = RunVimInTerminal('-S XtestPopupMatches', {'rows': 10})
216 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
217
218 " clean up
219 call StopVimInTerminal(buf)
220 call delete('XtestPopupMatches')
221endfunc
222
Bram Moolenaar399d8982019-06-02 15:34:29 +0200223func Test_popup_all_corners()
224 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200225 throw 'Skipped: cannot make screendumps'
Bram Moolenaar399d8982019-06-02 15:34:29 +0200226 endif
227 let lines =<< trim END
228 call setline(1, repeat([repeat('-', 60)], 15))
229 set so=0
230 normal 2G3|r#
231 let winid1 = popup_create(['first', 'second'], {
232 \ 'line': 'cursor+1',
233 \ 'col': 'cursor',
234 \ 'pos': 'topleft',
235 \ 'border': [],
236 \ 'padding': [],
237 \ })
238 normal 25|r@
239 let winid1 = popup_create(['First', 'SeconD'], {
240 \ 'line': 'cursor+1',
241 \ 'col': 'cursor',
242 \ 'pos': 'topright',
243 \ 'border': [],
244 \ 'padding': [],
245 \ })
246 normal 9G29|r%
247 let winid1 = popup_create(['fiRSt', 'seCOnd'], {
248 \ 'line': 'cursor-1',
249 \ 'col': 'cursor',
250 \ 'pos': 'botleft',
251 \ 'border': [],
252 \ 'padding': [],
253 \ })
254 normal 51|r&
255 let winid1 = popup_create(['FIrsT', 'SEcoND'], {
256 \ 'line': 'cursor-1',
257 \ 'col': 'cursor',
258 \ 'pos': 'botright',
259 \ 'border': [],
260 \ 'padding': [],
261 \ })
262 END
263 call writefile(lines, 'XtestPopupCorners')
264 let buf = RunVimInTerminal('-S XtestPopupCorners', {'rows': 12})
265 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
266
267 " clean up
268 call StopVimInTerminal(buf)
269 call delete('XtestPopupCorners')
270endfunc
271
Bram Moolenaar8d241042019-06-12 23:40:01 +0200272func Test_popup_firstline()
273 if !CanRunVimInTerminal()
274 throw 'Skipped: cannot make screendumps'
275 endif
276 let lines =<< trim END
277 call setline(1, range(1, 20))
278 call popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], {
279 \ 'maxheight': 4,
280 \ 'firstline': 3,
281 \ })
282 END
283 call writefile(lines, 'XtestPopupFirstline')
284 let buf = RunVimInTerminal('-S XtestPopupFirstline', {'rows': 10})
285 call VerifyScreenDump(buf, 'Test_popupwin_firstline', {})
286
287 " clean up
288 call StopVimInTerminal(buf)
289 call delete('XtestPopupFirstline')
290endfunc
291
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200292func Test_popup_drag()
293 if !CanRunVimInTerminal()
294 throw 'Skipped: cannot make screendumps'
295 endif
296 " create a popup that covers the command line
297 let lines =<< trim END
298 call setline(1, range(1, 20))
299 let winid = popup_create(['1111', '222222', '33333'], {
300 \ 'drag': 1,
301 \ 'border': [],
302 \ 'line': &lines - 4,
303 \ })
304 func Dragit()
305 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
306 endfunc
307 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
308 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2)<CR>
309 END
310 call writefile(lines, 'XtestPopupDrag')
311 let buf = RunVimInTerminal('-S XtestPopupDrag', {'rows': 10})
312 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
313
314 call term_sendkeys(buf, ":call Dragit()\<CR>")
315 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
316
317 " clean up
318 call StopVimInTerminal(buf)
319 call delete('XtestPopupDrag')
320endfunc
321
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200322func Test_popup_select()
323 if !CanRunVimInTerminal()
324 throw 'Skipped: cannot make screendumps'
325 endif
Bram Moolenaar650a6372019-06-15 00:29:33 +0200326 if !has('clipboard')
327 throw 'Skipped: clipboard feature missing'
328 endif
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200329 " create a popup with some text to be selected
330 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200331 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200332 call setline(1, range(1, 20))
333 let winid = popup_create(['the word', 'some more', 'several words here'], {
334 \ 'drag': 1,
335 \ 'border': [],
336 \ 'line': 3,
337 \ 'col': 10,
338 \ })
339 func Select1()
340 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
341 endfunc
342 map <silent> <F3> :call test_setmouse(4, 15)<CR>
343 map <silent> <F4> :call test_setmouse(6, 23)<CR>
344 END
345 call writefile(lines, 'XtestPopupSelect')
346 let buf = RunVimInTerminal('-S XtestPopupSelect', {'rows': 10})
347 call term_sendkeys(buf, ":call Select1()\<CR>")
348 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
349
350 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
351 call term_sendkeys(buf, "\"*p")
352 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
353
354 " clean up
355 call StopVimInTerminal(buf)
356 call delete('XtestPopupSelect')
357endfunc
358
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200359func Test_popup_in_tab()
360 " default popup is local to tab, not visible when in other tab
361 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200362 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200363 call assert_equal(1, popup_getpos(winid).visible)
364 tabnew
365 call assert_equal(0, popup_getpos(winid).visible)
366 quit
367 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200368
369 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200370 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200371 " buffer is gone now
372 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200373
374 " global popup is visible in any tab
Bram Moolenaarfc06cbb2019-06-15 14:14:31 +0200375 let winid = popup_create("text", {'tabpage': -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200376 call assert_equal(1, popup_getpos(winid).visible)
377 tabnew
378 call assert_equal(1, popup_getpos(winid).visible)
379 quit
380 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200381 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200382endfunc
383
384func Test_popup_valid_arguments()
385 " Zero value is like the property wasn't there
386 let winid = popup_create("text", {"col": 0})
387 let pos = popup_getpos(winid)
388 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200389 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200390
391 " using cursor column has minimum value of 1
392 let winid = popup_create("text", {"col": 'cursor-100'})
393 let pos = popup_getpos(winid)
394 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200395 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200396
397 " center
398 let winid = popup_create("text", {"pos": 'center'})
399 let pos = popup_getpos(winid)
400 let around = (&columns - pos.width) / 2
401 call assert_inrange(around - 1, around + 1, pos.col)
402 let around = (&lines - pos.height) / 2
403 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200404 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200405endfunc
406
407func Test_popup_invalid_arguments()
408 call assert_fails('call popup_create(666, {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200409 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200410 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200411 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200412
413 call assert_fails('call popup_create("text", {"col": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200414 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200415 call assert_fails('call popup_create("text", {"col": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200416 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200417 call assert_fails('call popup_create("text", {"col": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200418 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200419 call assert_fails('call popup_create("text", {"col": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200420 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200421
422 call assert_fails('call popup_create("text", {"line": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200423 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200424 call assert_fails('call popup_create("text", {"line": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200425 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200426 call assert_fails('call popup_create("text", {"line": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200427 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200428 call assert_fails('call popup_create("text", {"line": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200429 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200430
431 call assert_fails('call popup_create("text", {"pos": "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200432 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200433 call assert_fails('call popup_create("text", {"padding": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200434 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200435 call assert_fails('call popup_create("text", {"border": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200436 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200437 call assert_fails('call popup_create("text", {"borderhighlight": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200438 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200439 call assert_fails('call popup_create("text", {"borderchars": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200440 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200441
442 call assert_fails('call popup_create([{"text": "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200443 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200444 call assert_fails('call popup_create([{"text": "text", "props": "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200445 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200446 call assert_fails('call popup_create([{"text": "text", "props": ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200447 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200448endfunc
449
Bram Moolenaareea16992019-05-31 17:34:48 +0200450func Test_win_execute_closing_curwin()
451 split
452 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200453 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200454 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200455endfunc
456
457func Test_win_execute_not_allowed()
458 let winid = popup_create('some text', {})
459 call assert_fails('call win_execute(winid, "split")', 'E994:')
460 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
461 call assert_fails('call win_execute(winid, "close")', 'E994:')
462 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200463 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200464 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
465 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
466 call assert_fails('call win_execute(winid, "next")', 'E994:')
467 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
468 call assert_fails('call win_execute(winid, "buf")', 'E994:')
469 call assert_fails('call win_execute(winid, "edit")', 'E994:')
470 call assert_fails('call win_execute(winid, "enew")', 'E994:')
471 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
472 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
473 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
474 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200475 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200476endfunc
477
Bram Moolenaar402502d2019-05-30 22:07:36 +0200478func Test_popup_with_wrap()
479 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200480 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200481 endif
482 let lines =<< trim END
483 call setline(1, range(1, 100))
484 let winid = popup_create(
485 \ 'a long line that wont fit',
486 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
487 END
488 call writefile(lines, 'XtestPopup')
489 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
490 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
491
492 " clean up
493 call StopVimInTerminal(buf)
494 call delete('XtestPopup')
495endfunc
496
497func Test_popup_without_wrap()
498 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200499 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200500 endif
501 let lines =<< trim END
502 call setline(1, range(1, 100))
503 let winid = popup_create(
504 \ 'a long line that wont fit',
505 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
506 END
507 call writefile(lines, 'XtestPopup')
508 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
509 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
510
511 " clean up
512 call StopVimInTerminal(buf)
513 call delete('XtestPopup')
514endfunc
515
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200516func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200517 if !has('timers')
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200518 throw 'Skipped, timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200519 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200520 topleft vnew
521 call setline(1, 'hello')
522
523 call popup_create('world', {
524 \ 'line': 1,
525 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200526 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200527 \ 'time': 500,
528 \})
529 redraw
530 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
531 call assert_equal('world', line)
532
533 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200534 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200535 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
536 call assert_equal('hello', line)
537
538 call popup_create('on the command line', {
539 \ 'line': &lines,
540 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200541 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200542 \ 'time': 500,
543 \})
544 redraw
545 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
546 call assert_match('.*on the command line.*', line)
547
548 sleep 700m
549 redraw
550 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
551 call assert_notmatch('.*on the command line.*', line)
552
553 bwipe!
554endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200555
556func Test_popup_hide()
557 topleft vnew
558 call setline(1, 'hello')
559
560 let winid = popup_create('world', {
561 \ 'line': 1,
562 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200563 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200564 \})
565 redraw
566 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
567 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200568 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200569 " buffer is still listed and active
570 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200571
572 call popup_hide(winid)
573 redraw
574 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
575 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200576 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200577 " buffer is still listed but hidden
578 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200579
580 call popup_show(winid)
581 redraw
582 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
583 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200584 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200585
586
587 call popup_close(winid)
588 redraw
589 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
590 call assert_equal('hello', line)
591
592 " error is given for existing non-popup window
593 call assert_fails('call popup_hide(win_getid())', 'E993:')
594
595 " no error non-existing window
596 call popup_hide(1234234)
597 call popup_show(41234234)
598
599 bwipe!
600endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200601
602func Test_popup_move()
603 topleft vnew
604 call setline(1, 'hello')
605
606 let winid = popup_create('world', {
607 \ 'line': 1,
608 \ 'col': 1,
609 \ 'minwidth': 20,
610 \})
611 redraw
612 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
613 call assert_equal('world ', line)
614
615 call popup_move(winid, {'line': 2, 'col': 2})
616 redraw
617 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
618 call assert_equal('hello ', line)
619 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
620 call assert_equal('~world', line)
621
622 call popup_move(winid, {'line': 1})
623 redraw
624 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
625 call assert_equal('hworld', line)
626
627 call popup_close(winid)
628
629 bwipe!
630endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200631
Bram Moolenaar402502d2019-05-30 22:07:36 +0200632func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200633 let winid = popup_create('hello', {
634 \ 'line': 2,
635 \ 'col': 3,
636 \ 'minwidth': 10,
637 \ 'minheight': 11,
638 \})
639 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200640 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200641 call assert_equal(2, res.line)
642 call assert_equal(3, res.col)
643 call assert_equal(10, res.width)
644 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200645 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200646
647 call popup_close(winid)
648endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200649
650func Test_popup_width_longest()
651 let tests = [
652 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
653 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
654 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
655 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
656 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
657 \ ]
658
659 for test in tests
660 let winid = popup_create(test[0], {'line': 2, 'col': 3})
661 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200662 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200663 call assert_equal(test[1], position.width)
664 call popup_close(winid)
665 endfor
666endfunc
667
668func Test_popup_wraps()
669 let tests = [
670 \ ['nowrap', 6, 1],
671 \ ['a line that wraps once', 12, 2],
672 \ ['a line that wraps two times', 12, 3],
673 \ ]
674 for test in tests
675 let winid = popup_create(test[0],
676 \ {'line': 2, 'col': 3, 'maxwidth': 12})
677 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200678 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200679 call assert_equal(test[1], position.width)
680 call assert_equal(test[2], position.height)
681
682 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200683 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200684 endfor
685endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200686
687func Test_popup_getoptions()
688 let winid = popup_create('hello', {
689 \ 'line': 2,
690 \ 'col': 3,
691 \ 'minwidth': 10,
692 \ 'minheight': 11,
693 \ 'maxwidth': 20,
694 \ 'maxheight': 21,
695 \ 'zindex': 100,
696 \ 'time': 5000,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200697 \ 'fixed': 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200698 \})
699 redraw
700 let res = popup_getoptions(winid)
701 call assert_equal(2, res.line)
702 call assert_equal(3, res.col)
703 call assert_equal(10, res.minwidth)
704 call assert_equal(11, res.minheight)
705 call assert_equal(20, res.maxwidth)
706 call assert_equal(21, res.maxheight)
707 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200708 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200709 if has('timers')
710 call assert_equal(5000, res.time)
711 endif
712 call popup_close(winid)
713
714 let winid = popup_create('hello', {})
715 redraw
716 let res = popup_getoptions(winid)
717 call assert_equal(0, res.line)
718 call assert_equal(0, res.col)
719 call assert_equal(0, res.minwidth)
720 call assert_equal(0, res.minheight)
721 call assert_equal(0, res.maxwidth)
722 call assert_equal(0, res.maxheight)
723 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200724 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200725 if has('timers')
726 call assert_equal(0, res.time)
727 endif
728 call popup_close(winid)
729 call assert_equal({}, popup_getoptions(winid))
730endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200731
732func Test_popup_option_values()
733 new
734 " window-local
735 setlocal number
736 setlocal nowrap
737 " buffer-local
738 setlocal omnifunc=Something
739 " global/buffer-local
740 setlocal path=/there
741 " global/window-local
742 setlocal scrolloff=9
743
744 let winid = popup_create('hello', {})
745 call assert_equal(0, getwinvar(winid, '&number'))
746 call assert_equal(1, getwinvar(winid, '&wrap'))
747 call assert_equal('', getwinvar(winid, '&omnifunc'))
748 call assert_equal(&g:path, getwinvar(winid, '&path'))
749 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
750
751 call popup_close(winid)
752 bwipe
753endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200754
755func Test_popup_atcursor()
756 topleft vnew
757 call setline(1, [
758 \ 'xxxxxxxxxxxxxxxxx',
759 \ 'xxxxxxxxxxxxxxxxx',
760 \ 'xxxxxxxxxxxxxxxxx',
761 \])
762
763 call cursor(2, 2)
764 redraw
765 let winid = popup_atcursor('vim', {})
766 redraw
767 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
768 call assert_equal('xvimxxxxxxxxxxxxx', line)
769 call popup_close(winid)
770
771 call cursor(3, 4)
772 redraw
773 let winid = popup_atcursor('vim', {})
774 redraw
775 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
776 call assert_equal('xxxvimxxxxxxxxxxx', line)
777 call popup_close(winid)
778
779 call cursor(1, 1)
780 redraw
781 let winid = popup_create('vim', {
782 \ 'line': 'cursor+2',
783 \ 'col': 'cursor+1',
784 \})
785 redraw
786 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
787 call assert_equal('xvimxxxxxxxxxxxxx', line)
788 call popup_close(winid)
789
790 call cursor(3, 3)
791 redraw
792 let winid = popup_create('vim', {
793 \ 'line': 'cursor-2',
794 \ 'col': 'cursor-1',
795 \})
796 redraw
797 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
798 call assert_equal('xvimxxxxxxxxxxxxx', line)
799 call popup_close(winid)
800
Bram Moolenaar402502d2019-05-30 22:07:36 +0200801 " just enough room above
802 call cursor(3, 3)
803 redraw
804 let winid = popup_atcursor(['vim', 'is great'], {})
805 redraw
806 let pos = popup_getpos(winid)
807 call assert_equal(1, pos.line)
808 call popup_close(winid)
809
810 " not enough room above, popup goes below the cursor
811 call cursor(3, 3)
812 redraw
813 let winid = popup_atcursor(['vim', 'is', 'great'], {})
814 redraw
815 let pos = popup_getpos(winid)
816 call assert_equal(4, pos.line)
817 call popup_close(winid)
818
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200819 " cursor in first line, popup in line 2
820 call cursor(1, 1)
821 redraw
822 let winid = popup_atcursor(['vim', 'is', 'great'], {})
823 redraw
824 let pos = popup_getpos(winid)
825 call assert_equal(2, pos.line)
826 call popup_close(winid)
827
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200828 bwipe!
829endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200830
831func Test_popup_filter()
832 new
833 call setline(1, 'some text')
834
835 func MyPopupFilter(winid, c)
836 if a:c == 'e'
837 let g:eaten = 'e'
838 return 1
839 endif
840 if a:c == '0'
841 let g:ignored = '0'
842 return 0
843 endif
844 if a:c == 'x'
845 call popup_close(a:winid)
846 return 1
847 endif
848 return 0
849 endfunc
850
851 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
852 redraw
853
854 " e is consumed by the filter
855 call feedkeys('e', 'xt')
856 call assert_equal('e', g:eaten)
857
858 " 0 is ignored by the filter
859 normal $
860 call assert_equal(9, getcurpos()[2])
861 call feedkeys('0', 'xt')
862 call assert_equal('0', g:ignored)
863 call assert_equal(1, getcurpos()[2])
864
865 " x closes the popup
866 call feedkeys('x', 'xt')
867 call assert_equal('e', g:eaten)
868 call assert_equal(-1, winbufnr(winid))
869
870 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200871 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200872endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200873
874func Test_popup_close_callback()
875 func PopupDone(id, result)
876 let g:result = a:result
877 endfunc
878 let winid = popup_create('something', {'callback': 'PopupDone'})
879 redraw
880 call popup_close(winid, 'done')
881 call assert_equal('done', g:result)
882endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200883
884func Test_popup_empty()
885 let winid = popup_create('', {'padding': [2,2,2,2]})
886 redraw
887 let pos = popup_getpos(winid)
888 call assert_equal(4, pos.width)
889 call assert_equal(5, pos.height)
890
891 let winid = popup_create([], {'border': []})
892 redraw
893 let pos = popup_getpos(winid)
894 call assert_equal(2, pos.width)
895 call assert_equal(3, pos.height)
896endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +0200897
898func Test_popup_never_behind()
899 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200900 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +0200901 endif
902 " +-----------------------------+
903 " | | |
904 " | | |
905 " | | |
906 " | line1 |
907 " |------------line2------------|
908 " | line3 |
909 " | line4 |
910 " | |
911 " | |
912 " +-----------------------------+
913 let lines =<< trim END
914 only
915 split
916 vsplit
917 let info_window1 = getwininfo()[0]
918 let line = info_window1['height']
919 let col = info_window1['width']
920 call popup_create(['line1', 'line2', 'line3', 'line4'], {
921 \ 'line' : line,
922 \ 'col' : col,
923 \ })
924 END
925 call writefile(lines, 'XtestPopupBehind')
926 let buf = RunVimInTerminal('-S XtestPopupBehind', {'rows': 10})
927 call term_sendkeys(buf, "\<C-W>w")
928 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
929
930 " clean up
931 call StopVimInTerminal(buf)
932 call delete('XtestPopupBehind')
933endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200934
935func s:VerifyPosition( p, msg, line, col, width, height )
936 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
937 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
938 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
939 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
940endfunc
941
942func Test_popup_position_adjust()
943 " Anything placed past 2 cells from of the right of the screen is moved to the
944 " left.
945 "
946 " When wrapping is disabled, we also shift to the left to display on the
947 " screen, unless fixed is set.
948
949 " Entries for cases which don't vary based on wrapping.
950 " Format is per tests described below
951 let both_wrap_tests = [
952 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
953 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
954 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
955 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
956 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
957 \
958 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
959 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
960 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
961 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
962 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
963 \
964 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
965 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
966 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
967 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
968 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
969 \ ]
970
971 " these test groups are dicts with:
972 " - comment: something to identify the group of tests by
973 " - options: dict of options to merge with the row/col in tests
974 " - tests: list of cases. Each one is a list with elements:
975 " - text
976 " - row
977 " - col
978 " - expected row
979 " - expected col
980 " - expected width
981 " - expected height
982 let tests = [
983 \ {
984 \ 'comment': 'left-aligned with wrapping',
985 \ 'options': {
986 \ 'wrap': 1,
987 \ 'pos': 'botleft',
988 \ },
989 \ 'tests': both_wrap_tests + [
990 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
991 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
992 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
993 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
994 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
995 \ ],
996 \ },
997 \ {
998 \ 'comment': 'left aligned without wrapping',
999 \ 'options': {
1000 \ 'wrap': 0,
1001 \ 'pos': 'botleft',
1002 \ },
1003 \ 'tests': both_wrap_tests + [
1004 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
1005 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
1006 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
1007 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
1008 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1009 \ ],
1010 \ },
1011 \ {
1012 \ 'comment': 'left aligned with fixed position',
1013 \ 'options': {
1014 \ 'wrap': 0,
1015 \ 'fixed': 1,
1016 \ 'pos': 'botleft',
1017 \ },
1018 \ 'tests': both_wrap_tests + [
1019 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1020 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1021 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1022 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1023 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1024 \ ],
1025 \ },
1026 \ ]
1027
1028 for test_group in tests
1029 for test in test_group.tests
1030 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
1031 let options = {
1032 \ 'line': line,
1033 \ 'col': col,
1034 \ }
1035 call extend( options, test_group.options )
1036
1037 let p = popup_create( text, options )
1038
1039 let msg = string( extend( options, { 'text': text } ) )
1040 call s:VerifyPosition( p, msg, e_line, e_col, e_width, e_height )
1041 call popup_close( p )
1042 endfor
1043 endfor
1044
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001045 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001046 %bwipe!
1047endfunc
1048
Bram Moolenaar3397f742019-06-02 18:40:06 +02001049func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001050 " width of screen
1051 let X = join(map(range(&columns), {->'X'}), '')
1052
1053 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1054 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
1055
1056 redraw
1057 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1058 call assert_equal(X, line)
1059
1060 call popup_close( p )
1061 redraw
1062
1063 " Same if placed on the right hand side
1064 let p = popup_create( X, { 'line': 1, 'col': &columns, 'wrap': 0 } )
1065 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
1066
1067 redraw
1068 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1069 call assert_equal(X, line)
1070
1071 call popup_close( p )
1072 redraw
1073
1074 " Extend so > window width
1075 let X .= 'x'
1076
1077 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1078 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
1079
1080 redraw
1081 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1082 call assert_equal(X[ : -2 ], line)
1083
1084 call popup_close( p )
1085 redraw
1086
1087 " Shifted then truncated (the x is not visible)
1088 let p = popup_create( X, { 'line': 1, 'col': &columns - 3, 'wrap': 0 } )
1089 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
1090
1091 redraw
1092 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1093 call assert_equal(X[ : -2 ], line)
1094
1095 call popup_close( p )
1096 redraw
1097
1098 " Not shifted, just truncated
1099 let p = popup_create( X,
1100 \ { 'line': 1, 'col': 2, 'wrap': 0, 'fixed': 1 } )
1101 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
1102
1103 redraw
1104 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1105 let e_line = ' ' . X[ 1 : -2 ]
1106 call assert_equal(e_line, line)
1107
1108 call popup_close( p )
1109 redraw
1110
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001111 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001112 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001113endfunc
1114
1115func Test_popup_moved()
1116 new
1117 call test_override('char_avail', 1)
1118 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1119
1120 exe "normal gg0/word\<CR>"
1121 let winid = popup_atcursor('text', {'moved': 'any'})
1122 redraw
1123 call assert_equal(1, popup_getpos(winid).visible)
1124 " trigger the check for last_cursormoved by going into insert mode
1125 call feedkeys("li\<Esc>", 'xt')
1126 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001127 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001128
1129 exe "normal gg0/word\<CR>"
1130 let winid = popup_atcursor('text', {'moved': 'word'})
1131 redraw
1132 call assert_equal(1, popup_getpos(winid).visible)
1133 call feedkeys("hi\<Esc>", 'xt')
1134 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001135 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001136
1137 exe "normal gg0/word\<CR>"
1138 let winid = popup_atcursor('text', {'moved': 'word'})
1139 redraw
1140 call assert_equal(1, popup_getpos(winid).visible)
1141 call feedkeys("li\<Esc>", 'xt')
1142 call assert_equal(1, popup_getpos(winid).visible)
1143 call feedkeys("ei\<Esc>", 'xt')
1144 call assert_equal(1, popup_getpos(winid).visible)
1145 call feedkeys("eli\<Esc>", 'xt')
1146 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001147 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001148
Bram Moolenaar17627312019-06-02 19:53:44 +02001149 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001150 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001151 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001152 redraw
1153 call assert_equal(1, popup_getpos(winid).visible)
1154 call feedkeys("eli\<Esc>", 'xt')
1155 call assert_equal(1, popup_getpos(winid).visible)
1156 call feedkeys("wi\<Esc>", 'xt')
1157 call assert_equal(1, popup_getpos(winid).visible)
1158 call feedkeys("Eli\<Esc>", 'xt')
1159 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001160 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001161
1162 exe "normal gg0/word\<CR>"
1163 let winid = popup_atcursor('text', {'moved': [5, 10]})
1164 redraw
1165 call assert_equal(1, popup_getpos(winid).visible)
1166 call feedkeys("eli\<Esc>", 'xt')
1167 call feedkeys("ei\<Esc>", 'xt')
1168 call assert_equal(1, popup_getpos(winid).visible)
1169 call feedkeys("eli\<Esc>", 'xt')
1170 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001171 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001172
1173 bwipe!
1174 call test_override('ALL', 0)
1175endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001176
1177func Test_notifications()
1178 if !has('timers')
1179 throw 'Skipped, timer feature not supported'
1180 endif
1181 if !CanRunVimInTerminal()
1182 throw 'Skipped: cannot make screendumps'
1183 endif
1184
1185 call writefile([
1186 \ "call setline(1, range(1, 20))",
1187 \ "hi Notification ctermbg=lightblue",
1188 \ "call popup_notification('first notification', {})",
1189 \], 'XtestNotifications')
1190 let buf = RunVimInTerminal('-S XtestNotifications', {'rows': 10})
1191 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1192
1193 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001194 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1195 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001196 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1197
1198
1199 " clean up
1200 call StopVimInTerminal(buf)
1201 call delete('XtestNotifications')
1202endfunc