blob: 6e11bba73a452c49620d69cb9dc68d4a341a8939 [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
326 " create a popup with some text to be selected
327 let lines =<< trim END
328 call setline(1, range(1, 20))
329 let winid = popup_create(['the word', 'some more', 'several words here'], {
330 \ 'drag': 1,
331 \ 'border': [],
332 \ 'line': 3,
333 \ 'col': 10,
334 \ })
335 func Select1()
336 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
337 endfunc
338 map <silent> <F3> :call test_setmouse(4, 15)<CR>
339 map <silent> <F4> :call test_setmouse(6, 23)<CR>
340 END
341 call writefile(lines, 'XtestPopupSelect')
342 let buf = RunVimInTerminal('-S XtestPopupSelect', {'rows': 10})
343 call term_sendkeys(buf, ":call Select1()\<CR>")
344 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
345
346 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
347 call term_sendkeys(buf, "\"*p")
348 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
349
350 " clean up
351 call StopVimInTerminal(buf)
352 call delete('XtestPopupSelect')
353endfunc
354
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200355func Test_popup_in_tab()
356 " default popup is local to tab, not visible when in other tab
357 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200358 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200359 call assert_equal(1, popup_getpos(winid).visible)
360 tabnew
361 call assert_equal(0, popup_getpos(winid).visible)
362 quit
363 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200364
365 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200366 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200367 " buffer is gone now
368 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200369
370 " global popup is visible in any tab
371 let winid = popup_create("text", {'tab': -1})
372 call assert_equal(1, popup_getpos(winid).visible)
373 tabnew
374 call assert_equal(1, popup_getpos(winid).visible)
375 quit
376 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200377 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200378endfunc
379
380func Test_popup_valid_arguments()
381 " Zero value is like the property wasn't there
382 let winid = popup_create("text", {"col": 0})
383 let pos = popup_getpos(winid)
384 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200385 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200386
387 " using cursor column has minimum value of 1
388 let winid = popup_create("text", {"col": 'cursor-100'})
389 let pos = popup_getpos(winid)
390 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200391 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200392
393 " center
394 let winid = popup_create("text", {"pos": 'center'})
395 let pos = popup_getpos(winid)
396 let around = (&columns - pos.width) / 2
397 call assert_inrange(around - 1, around + 1, pos.col)
398 let around = (&lines - pos.height) / 2
399 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200400 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200401endfunc
402
403func Test_popup_invalid_arguments()
404 call assert_fails('call popup_create(666, {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200405 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200406 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200407 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200408
409 call assert_fails('call popup_create("text", {"col": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200410 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200411 call assert_fails('call popup_create("text", {"col": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200412 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200413 call assert_fails('call popup_create("text", {"col": "cursor+x"})', 'E15:')
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": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200416 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200417
418 call assert_fails('call popup_create("text", {"line": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200419 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200420 call assert_fails('call popup_create("text", {"line": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200421 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200422 call assert_fails('call popup_create("text", {"line": "cursor+x"})', 'E15:')
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": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200425 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200426
427 call assert_fails('call popup_create("text", {"pos": "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200428 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200429 call assert_fails('call popup_create("text", {"padding": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200430 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200431 call assert_fails('call popup_create("text", {"border": "none"})', 'E714:')
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", {"borderhighlight": "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", {"borderchars": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200436 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200437
438 call assert_fails('call popup_create([{"text": "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200439 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200440 call assert_fails('call popup_create([{"text": "text", "props": "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200441 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200442 call assert_fails('call popup_create([{"text": "text", "props": ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200443 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200444endfunc
445
Bram Moolenaareea16992019-05-31 17:34:48 +0200446func Test_win_execute_closing_curwin()
447 split
448 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200449 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200450 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200451endfunc
452
453func Test_win_execute_not_allowed()
454 let winid = popup_create('some text', {})
455 call assert_fails('call win_execute(winid, "split")', 'E994:')
456 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
457 call assert_fails('call win_execute(winid, "close")', 'E994:')
458 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200459 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200460 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
461 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
462 call assert_fails('call win_execute(winid, "next")', 'E994:')
463 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
464 call assert_fails('call win_execute(winid, "buf")', 'E994:')
465 call assert_fails('call win_execute(winid, "edit")', 'E994:')
466 call assert_fails('call win_execute(winid, "enew")', 'E994:')
467 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
468 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
469 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
470 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200471 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200472endfunc
473
Bram Moolenaar402502d2019-05-30 22:07:36 +0200474func Test_popup_with_wrap()
475 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200476 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200477 endif
478 let lines =<< trim END
479 call setline(1, range(1, 100))
480 let winid = popup_create(
481 \ 'a long line that wont fit',
482 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
483 END
484 call writefile(lines, 'XtestPopup')
485 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
486 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
487
488 " clean up
489 call StopVimInTerminal(buf)
490 call delete('XtestPopup')
491endfunc
492
493func Test_popup_without_wrap()
494 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200495 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200496 endif
497 let lines =<< trim END
498 call setline(1, range(1, 100))
499 let winid = popup_create(
500 \ 'a long line that wont fit',
501 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
502 END
503 call writefile(lines, 'XtestPopup')
504 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
505 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
506
507 " clean up
508 call StopVimInTerminal(buf)
509 call delete('XtestPopup')
510endfunc
511
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200512func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200513 if !has('timers')
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200514 throw 'Skipped, timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200515 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200516 topleft vnew
517 call setline(1, 'hello')
518
519 call popup_create('world', {
520 \ 'line': 1,
521 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200522 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200523 \ 'time': 500,
524 \})
525 redraw
526 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
527 call assert_equal('world', line)
528
529 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200530 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200531 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
532 call assert_equal('hello', line)
533
534 call popup_create('on the command line', {
535 \ 'line': &lines,
536 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200537 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200538 \ 'time': 500,
539 \})
540 redraw
541 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
542 call assert_match('.*on the command line.*', line)
543
544 sleep 700m
545 redraw
546 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
547 call assert_notmatch('.*on the command line.*', line)
548
549 bwipe!
550endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200551
552func Test_popup_hide()
553 topleft vnew
554 call setline(1, 'hello')
555
556 let winid = popup_create('world', {
557 \ 'line': 1,
558 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200559 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200560 \})
561 redraw
562 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
563 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200564 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200565 " buffer is still listed and active
566 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200567
568 call popup_hide(winid)
569 redraw
570 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
571 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200572 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200573 " buffer is still listed but hidden
574 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200575
576 call popup_show(winid)
577 redraw
578 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
579 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200580 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200581
582
583 call popup_close(winid)
584 redraw
585 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
586 call assert_equal('hello', line)
587
588 " error is given for existing non-popup window
589 call assert_fails('call popup_hide(win_getid())', 'E993:')
590
591 " no error non-existing window
592 call popup_hide(1234234)
593 call popup_show(41234234)
594
595 bwipe!
596endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200597
598func Test_popup_move()
599 topleft vnew
600 call setline(1, 'hello')
601
602 let winid = popup_create('world', {
603 \ 'line': 1,
604 \ 'col': 1,
605 \ 'minwidth': 20,
606 \})
607 redraw
608 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
609 call assert_equal('world ', line)
610
611 call popup_move(winid, {'line': 2, 'col': 2})
612 redraw
613 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
614 call assert_equal('hello ', line)
615 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
616 call assert_equal('~world', line)
617
618 call popup_move(winid, {'line': 1})
619 redraw
620 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
621 call assert_equal('hworld', line)
622
623 call popup_close(winid)
624
625 bwipe!
626endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200627
Bram Moolenaar402502d2019-05-30 22:07:36 +0200628func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200629 let winid = popup_create('hello', {
630 \ 'line': 2,
631 \ 'col': 3,
632 \ 'minwidth': 10,
633 \ 'minheight': 11,
634 \})
635 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200636 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200637 call assert_equal(2, res.line)
638 call assert_equal(3, res.col)
639 call assert_equal(10, res.width)
640 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200641 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200642
643 call popup_close(winid)
644endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200645
646func Test_popup_width_longest()
647 let tests = [
648 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
649 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
650 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
651 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
652 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
653 \ ]
654
655 for test in tests
656 let winid = popup_create(test[0], {'line': 2, 'col': 3})
657 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200658 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200659 call assert_equal(test[1], position.width)
660 call popup_close(winid)
661 endfor
662endfunc
663
664func Test_popup_wraps()
665 let tests = [
666 \ ['nowrap', 6, 1],
667 \ ['a line that wraps once', 12, 2],
668 \ ['a line that wraps two times', 12, 3],
669 \ ]
670 for test in tests
671 let winid = popup_create(test[0],
672 \ {'line': 2, 'col': 3, 'maxwidth': 12})
673 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200674 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200675 call assert_equal(test[1], position.width)
676 call assert_equal(test[2], position.height)
677
678 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200679 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200680 endfor
681endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200682
683func Test_popup_getoptions()
684 let winid = popup_create('hello', {
685 \ 'line': 2,
686 \ 'col': 3,
687 \ 'minwidth': 10,
688 \ 'minheight': 11,
689 \ 'maxwidth': 20,
690 \ 'maxheight': 21,
691 \ 'zindex': 100,
692 \ 'time': 5000,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200693 \ 'fixed': 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200694 \})
695 redraw
696 let res = popup_getoptions(winid)
697 call assert_equal(2, res.line)
698 call assert_equal(3, res.col)
699 call assert_equal(10, res.minwidth)
700 call assert_equal(11, res.minheight)
701 call assert_equal(20, res.maxwidth)
702 call assert_equal(21, res.maxheight)
703 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200704 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200705 if has('timers')
706 call assert_equal(5000, res.time)
707 endif
708 call popup_close(winid)
709
710 let winid = popup_create('hello', {})
711 redraw
712 let res = popup_getoptions(winid)
713 call assert_equal(0, res.line)
714 call assert_equal(0, res.col)
715 call assert_equal(0, res.minwidth)
716 call assert_equal(0, res.minheight)
717 call assert_equal(0, res.maxwidth)
718 call assert_equal(0, res.maxheight)
719 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200720 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200721 if has('timers')
722 call assert_equal(0, res.time)
723 endif
724 call popup_close(winid)
725 call assert_equal({}, popup_getoptions(winid))
726endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200727
728func Test_popup_option_values()
729 new
730 " window-local
731 setlocal number
732 setlocal nowrap
733 " buffer-local
734 setlocal omnifunc=Something
735 " global/buffer-local
736 setlocal path=/there
737 " global/window-local
738 setlocal scrolloff=9
739
740 let winid = popup_create('hello', {})
741 call assert_equal(0, getwinvar(winid, '&number'))
742 call assert_equal(1, getwinvar(winid, '&wrap'))
743 call assert_equal('', getwinvar(winid, '&omnifunc'))
744 call assert_equal(&g:path, getwinvar(winid, '&path'))
745 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
746
747 call popup_close(winid)
748 bwipe
749endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200750
751func Test_popup_atcursor()
752 topleft vnew
753 call setline(1, [
754 \ 'xxxxxxxxxxxxxxxxx',
755 \ 'xxxxxxxxxxxxxxxxx',
756 \ 'xxxxxxxxxxxxxxxxx',
757 \])
758
759 call cursor(2, 2)
760 redraw
761 let winid = popup_atcursor('vim', {})
762 redraw
763 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
764 call assert_equal('xvimxxxxxxxxxxxxx', line)
765 call popup_close(winid)
766
767 call cursor(3, 4)
768 redraw
769 let winid = popup_atcursor('vim', {})
770 redraw
771 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
772 call assert_equal('xxxvimxxxxxxxxxxx', line)
773 call popup_close(winid)
774
775 call cursor(1, 1)
776 redraw
777 let winid = popup_create('vim', {
778 \ 'line': 'cursor+2',
779 \ 'col': 'cursor+1',
780 \})
781 redraw
782 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
783 call assert_equal('xvimxxxxxxxxxxxxx', line)
784 call popup_close(winid)
785
786 call cursor(3, 3)
787 redraw
788 let winid = popup_create('vim', {
789 \ 'line': 'cursor-2',
790 \ 'col': 'cursor-1',
791 \})
792 redraw
793 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
794 call assert_equal('xvimxxxxxxxxxxxxx', line)
795 call popup_close(winid)
796
Bram Moolenaar402502d2019-05-30 22:07:36 +0200797 " just enough room above
798 call cursor(3, 3)
799 redraw
800 let winid = popup_atcursor(['vim', 'is great'], {})
801 redraw
802 let pos = popup_getpos(winid)
803 call assert_equal(1, pos.line)
804 call popup_close(winid)
805
806 " not enough room above, popup goes below the cursor
807 call cursor(3, 3)
808 redraw
809 let winid = popup_atcursor(['vim', 'is', 'great'], {})
810 redraw
811 let pos = popup_getpos(winid)
812 call assert_equal(4, pos.line)
813 call popup_close(winid)
814
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200815 " cursor in first line, popup in line 2
816 call cursor(1, 1)
817 redraw
818 let winid = popup_atcursor(['vim', 'is', 'great'], {})
819 redraw
820 let pos = popup_getpos(winid)
821 call assert_equal(2, pos.line)
822 call popup_close(winid)
823
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200824 bwipe!
825endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200826
827func Test_popup_filter()
828 new
829 call setline(1, 'some text')
830
831 func MyPopupFilter(winid, c)
832 if a:c == 'e'
833 let g:eaten = 'e'
834 return 1
835 endif
836 if a:c == '0'
837 let g:ignored = '0'
838 return 0
839 endif
840 if a:c == 'x'
841 call popup_close(a:winid)
842 return 1
843 endif
844 return 0
845 endfunc
846
847 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
848 redraw
849
850 " e is consumed by the filter
851 call feedkeys('e', 'xt')
852 call assert_equal('e', g:eaten)
853
854 " 0 is ignored by the filter
855 normal $
856 call assert_equal(9, getcurpos()[2])
857 call feedkeys('0', 'xt')
858 call assert_equal('0', g:ignored)
859 call assert_equal(1, getcurpos()[2])
860
861 " x closes the popup
862 call feedkeys('x', 'xt')
863 call assert_equal('e', g:eaten)
864 call assert_equal(-1, winbufnr(winid))
865
866 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200867 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200868endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200869
870func Test_popup_close_callback()
871 func PopupDone(id, result)
872 let g:result = a:result
873 endfunc
874 let winid = popup_create('something', {'callback': 'PopupDone'})
875 redraw
876 call popup_close(winid, 'done')
877 call assert_equal('done', g:result)
878endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200879
880func Test_popup_empty()
881 let winid = popup_create('', {'padding': [2,2,2,2]})
882 redraw
883 let pos = popup_getpos(winid)
884 call assert_equal(4, pos.width)
885 call assert_equal(5, pos.height)
886
887 let winid = popup_create([], {'border': []})
888 redraw
889 let pos = popup_getpos(winid)
890 call assert_equal(2, pos.width)
891 call assert_equal(3, pos.height)
892endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +0200893
894func Test_popup_never_behind()
895 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200896 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +0200897 endif
898 " +-----------------------------+
899 " | | |
900 " | | |
901 " | | |
902 " | line1 |
903 " |------------line2------------|
904 " | line3 |
905 " | line4 |
906 " | |
907 " | |
908 " +-----------------------------+
909 let lines =<< trim END
910 only
911 split
912 vsplit
913 let info_window1 = getwininfo()[0]
914 let line = info_window1['height']
915 let col = info_window1['width']
916 call popup_create(['line1', 'line2', 'line3', 'line4'], {
917 \ 'line' : line,
918 \ 'col' : col,
919 \ })
920 END
921 call writefile(lines, 'XtestPopupBehind')
922 let buf = RunVimInTerminal('-S XtestPopupBehind', {'rows': 10})
923 call term_sendkeys(buf, "\<C-W>w")
924 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
925
926 " clean up
927 call StopVimInTerminal(buf)
928 call delete('XtestPopupBehind')
929endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200930
931func s:VerifyPosition( p, msg, line, col, width, height )
932 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
933 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
934 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
935 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
936endfunc
937
938func Test_popup_position_adjust()
939 " Anything placed past 2 cells from of the right of the screen is moved to the
940 " left.
941 "
942 " When wrapping is disabled, we also shift to the left to display on the
943 " screen, unless fixed is set.
944
945 " Entries for cases which don't vary based on wrapping.
946 " Format is per tests described below
947 let both_wrap_tests = [
948 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
949 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
950 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
951 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
952 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
953 \
954 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
955 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
956 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
957 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
958 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
959 \
960 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
961 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
962 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
963 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
964 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
965 \ ]
966
967 " these test groups are dicts with:
968 " - comment: something to identify the group of tests by
969 " - options: dict of options to merge with the row/col in tests
970 " - tests: list of cases. Each one is a list with elements:
971 " - text
972 " - row
973 " - col
974 " - expected row
975 " - expected col
976 " - expected width
977 " - expected height
978 let tests = [
979 \ {
980 \ 'comment': 'left-aligned with wrapping',
981 \ 'options': {
982 \ 'wrap': 1,
983 \ 'pos': 'botleft',
984 \ },
985 \ 'tests': both_wrap_tests + [
986 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
987 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
988 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
989 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
990 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
991 \ ],
992 \ },
993 \ {
994 \ 'comment': 'left aligned without wrapping',
995 \ 'options': {
996 \ 'wrap': 0,
997 \ 'pos': 'botleft',
998 \ },
999 \ 'tests': both_wrap_tests + [
1000 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
1001 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
1002 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
1003 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
1004 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1005 \ ],
1006 \ },
1007 \ {
1008 \ 'comment': 'left aligned with fixed position',
1009 \ 'options': {
1010 \ 'wrap': 0,
1011 \ 'fixed': 1,
1012 \ 'pos': 'botleft',
1013 \ },
1014 \ 'tests': both_wrap_tests + [
1015 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1016 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1017 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1018 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1019 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1020 \ ],
1021 \ },
1022 \ ]
1023
1024 for test_group in tests
1025 for test in test_group.tests
1026 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
1027 let options = {
1028 \ 'line': line,
1029 \ 'col': col,
1030 \ }
1031 call extend( options, test_group.options )
1032
1033 let p = popup_create( text, options )
1034
1035 let msg = string( extend( options, { 'text': text } ) )
1036 call s:VerifyPosition( p, msg, e_line, e_col, e_width, e_height )
1037 call popup_close( p )
1038 endfor
1039 endfor
1040
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001041 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001042 %bwipe!
1043endfunc
1044
Bram Moolenaar3397f742019-06-02 18:40:06 +02001045func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001046 " width of screen
1047 let X = join(map(range(&columns), {->'X'}), '')
1048
1049 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1050 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
1051
1052 redraw
1053 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1054 call assert_equal(X, line)
1055
1056 call popup_close( p )
1057 redraw
1058
1059 " Same if placed on the right hand side
1060 let p = popup_create( X, { 'line': 1, 'col': &columns, 'wrap': 0 } )
1061 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
1062
1063 redraw
1064 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1065 call assert_equal(X, line)
1066
1067 call popup_close( p )
1068 redraw
1069
1070 " Extend so > window width
1071 let X .= 'x'
1072
1073 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1074 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
1075
1076 redraw
1077 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1078 call assert_equal(X[ : -2 ], line)
1079
1080 call popup_close( p )
1081 redraw
1082
1083 " Shifted then truncated (the x is not visible)
1084 let p = popup_create( X, { 'line': 1, 'col': &columns - 3, 'wrap': 0 } )
1085 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
1086
1087 redraw
1088 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1089 call assert_equal(X[ : -2 ], line)
1090
1091 call popup_close( p )
1092 redraw
1093
1094 " Not shifted, just truncated
1095 let p = popup_create( X,
1096 \ { 'line': 1, 'col': 2, 'wrap': 0, 'fixed': 1 } )
1097 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
1098
1099 redraw
1100 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1101 let e_line = ' ' . X[ 1 : -2 ]
1102 call assert_equal(e_line, line)
1103
1104 call popup_close( p )
1105 redraw
1106
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001107 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001108 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001109endfunc
1110
1111func Test_popup_moved()
1112 new
1113 call test_override('char_avail', 1)
1114 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1115
1116 exe "normal gg0/word\<CR>"
1117 let winid = popup_atcursor('text', {'moved': 'any'})
1118 redraw
1119 call assert_equal(1, popup_getpos(winid).visible)
1120 " trigger the check for last_cursormoved by going into insert mode
1121 call feedkeys("li\<Esc>", 'xt')
1122 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001123 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001124
1125 exe "normal gg0/word\<CR>"
1126 let winid = popup_atcursor('text', {'moved': 'word'})
1127 redraw
1128 call assert_equal(1, popup_getpos(winid).visible)
1129 call feedkeys("hi\<Esc>", 'xt')
1130 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001131 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001132
1133 exe "normal gg0/word\<CR>"
1134 let winid = popup_atcursor('text', {'moved': 'word'})
1135 redraw
1136 call assert_equal(1, popup_getpos(winid).visible)
1137 call feedkeys("li\<Esc>", 'xt')
1138 call assert_equal(1, popup_getpos(winid).visible)
1139 call feedkeys("ei\<Esc>", 'xt')
1140 call assert_equal(1, popup_getpos(winid).visible)
1141 call feedkeys("eli\<Esc>", 'xt')
1142 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001143 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001144
Bram Moolenaar17627312019-06-02 19:53:44 +02001145 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001146 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001147 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001148 redraw
1149 call assert_equal(1, popup_getpos(winid).visible)
1150 call feedkeys("eli\<Esc>", 'xt')
1151 call assert_equal(1, popup_getpos(winid).visible)
1152 call feedkeys("wi\<Esc>", 'xt')
1153 call assert_equal(1, popup_getpos(winid).visible)
1154 call feedkeys("Eli\<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': [5, 10]})
1160 redraw
1161 call assert_equal(1, popup_getpos(winid).visible)
1162 call feedkeys("eli\<Esc>", 'xt')
1163 call feedkeys("ei\<Esc>", 'xt')
1164 call assert_equal(1, popup_getpos(winid).visible)
1165 call feedkeys("eli\<Esc>", 'xt')
1166 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001167 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001168
1169 bwipe!
1170 call test_override('ALL', 0)
1171endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001172
1173func Test_notifications()
1174 if !has('timers')
1175 throw 'Skipped, timer feature not supported'
1176 endif
1177 if !CanRunVimInTerminal()
1178 throw 'Skipped: cannot make screendumps'
1179 endif
1180
1181 call writefile([
1182 \ "call setline(1, range(1, 20))",
1183 \ "hi Notification ctermbg=lightblue",
1184 \ "call popup_notification('first notification', {})",
1185 \], 'XtestNotifications')
1186 let buf = RunVimInTerminal('-S XtestNotifications', {'rows': 10})
1187 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1188
1189 " second one goes below the first one
1190 call term_sendkeys(buf, ":call popup_notification('another important notification', {'highlight': 'Notification'})\<CR>")
1191 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1192
1193
1194 " clean up
1195 call StopVimInTerminal(buf)
1196 call delete('XtestNotifications')
1197endfunc