blob: 79207c8b26bb307895033848dbddcfe52db376c5 [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
331 call setline(1, range(1, 20))
332 let winid = popup_create(['the word', 'some more', 'several words here'], {
333 \ 'drag': 1,
334 \ 'border': [],
335 \ 'line': 3,
336 \ 'col': 10,
337 \ })
338 func Select1()
339 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
340 endfunc
341 map <silent> <F3> :call test_setmouse(4, 15)<CR>
342 map <silent> <F4> :call test_setmouse(6, 23)<CR>
343 END
344 call writefile(lines, 'XtestPopupSelect')
345 let buf = RunVimInTerminal('-S XtestPopupSelect', {'rows': 10})
346 call term_sendkeys(buf, ":call Select1()\<CR>")
347 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
348
349 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
350 call term_sendkeys(buf, "\"*p")
351 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
352
353 " clean up
354 call StopVimInTerminal(buf)
355 call delete('XtestPopupSelect')
356endfunc
357
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200358func Test_popup_in_tab()
359 " default popup is local to tab, not visible when in other tab
360 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200361 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200362 call assert_equal(1, popup_getpos(winid).visible)
363 tabnew
364 call assert_equal(0, popup_getpos(winid).visible)
365 quit
366 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200367
368 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200369 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200370 " buffer is gone now
371 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200372
373 " global popup is visible in any tab
374 let winid = popup_create("text", {'tab': -1})
375 call assert_equal(1, popup_getpos(winid).visible)
376 tabnew
377 call assert_equal(1, popup_getpos(winid).visible)
378 quit
379 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200380 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200381endfunc
382
383func Test_popup_valid_arguments()
384 " Zero value is like the property wasn't there
385 let winid = popup_create("text", {"col": 0})
386 let pos = popup_getpos(winid)
387 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200388 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200389
390 " using cursor column has minimum value of 1
391 let winid = popup_create("text", {"col": 'cursor-100'})
392 let pos = popup_getpos(winid)
393 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200394 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200395
396 " center
397 let winid = popup_create("text", {"pos": 'center'})
398 let pos = popup_getpos(winid)
399 let around = (&columns - pos.width) / 2
400 call assert_inrange(around - 1, around + 1, pos.col)
401 let around = (&lines - pos.height) / 2
402 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200403 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200404endfunc
405
406func Test_popup_invalid_arguments()
407 call assert_fails('call popup_create(666, {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200408 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200409 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200410 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200411
412 call assert_fails('call popup_create("text", {"col": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200413 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200414 call assert_fails('call popup_create("text", {"col": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200415 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200416 call assert_fails('call popup_create("text", {"col": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200417 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200418 call assert_fails('call popup_create("text", {"col": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200419 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200420
421 call assert_fails('call popup_create("text", {"line": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200422 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200423 call assert_fails('call popup_create("text", {"line": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200424 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200425 call assert_fails('call popup_create("text", {"line": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200426 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200427 call assert_fails('call popup_create("text", {"line": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200428 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200429
430 call assert_fails('call popup_create("text", {"pos": "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200431 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200432 call assert_fails('call popup_create("text", {"padding": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200433 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200434 call assert_fails('call popup_create("text", {"border": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200435 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200436 call assert_fails('call popup_create("text", {"borderhighlight": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200437 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200438 call assert_fails('call popup_create("text", {"borderchars": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200439 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200440
441 call assert_fails('call popup_create([{"text": "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200442 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200443 call assert_fails('call popup_create([{"text": "text", "props": "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200444 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200445 call assert_fails('call popup_create([{"text": "text", "props": ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200446 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200447endfunc
448
Bram Moolenaareea16992019-05-31 17:34:48 +0200449func Test_win_execute_closing_curwin()
450 split
451 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200452 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200453 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200454endfunc
455
456func Test_win_execute_not_allowed()
457 let winid = popup_create('some text', {})
458 call assert_fails('call win_execute(winid, "split")', 'E994:')
459 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
460 call assert_fails('call win_execute(winid, "close")', 'E994:')
461 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200462 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200463 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
464 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
465 call assert_fails('call win_execute(winid, "next")', 'E994:')
466 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
467 call assert_fails('call win_execute(winid, "buf")', 'E994:')
468 call assert_fails('call win_execute(winid, "edit")', 'E994:')
469 call assert_fails('call win_execute(winid, "enew")', 'E994:')
470 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
471 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
472 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
473 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200474 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200475endfunc
476
Bram Moolenaar402502d2019-05-30 22:07:36 +0200477func Test_popup_with_wrap()
478 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200479 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200480 endif
481 let lines =<< trim END
482 call setline(1, range(1, 100))
483 let winid = popup_create(
484 \ 'a long line that wont fit',
485 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
486 END
487 call writefile(lines, 'XtestPopup')
488 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
489 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
490
491 " clean up
492 call StopVimInTerminal(buf)
493 call delete('XtestPopup')
494endfunc
495
496func Test_popup_without_wrap()
497 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200498 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200499 endif
500 let lines =<< trim END
501 call setline(1, range(1, 100))
502 let winid = popup_create(
503 \ 'a long line that wont fit',
504 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
505 END
506 call writefile(lines, 'XtestPopup')
507 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
508 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
509
510 " clean up
511 call StopVimInTerminal(buf)
512 call delete('XtestPopup')
513endfunc
514
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200515func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200516 if !has('timers')
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200517 throw 'Skipped, timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200518 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200519 topleft vnew
520 call setline(1, 'hello')
521
522 call popup_create('world', {
523 \ 'line': 1,
524 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200525 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200526 \ 'time': 500,
527 \})
528 redraw
529 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
530 call assert_equal('world', line)
531
532 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200533 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200534 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
535 call assert_equal('hello', line)
536
537 call popup_create('on the command line', {
538 \ 'line': &lines,
539 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200540 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200541 \ 'time': 500,
542 \})
543 redraw
544 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
545 call assert_match('.*on the command line.*', line)
546
547 sleep 700m
548 redraw
549 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
550 call assert_notmatch('.*on the command line.*', line)
551
552 bwipe!
553endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200554
555func Test_popup_hide()
556 topleft vnew
557 call setline(1, 'hello')
558
559 let winid = popup_create('world', {
560 \ 'line': 1,
561 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200562 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200563 \})
564 redraw
565 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
566 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200567 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200568 " buffer is still listed and active
569 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200570
571 call popup_hide(winid)
572 redraw
573 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
574 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200575 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200576 " buffer is still listed but hidden
577 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200578
579 call popup_show(winid)
580 redraw
581 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
582 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200583 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200584
585
586 call popup_close(winid)
587 redraw
588 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
589 call assert_equal('hello', line)
590
591 " error is given for existing non-popup window
592 call assert_fails('call popup_hide(win_getid())', 'E993:')
593
594 " no error non-existing window
595 call popup_hide(1234234)
596 call popup_show(41234234)
597
598 bwipe!
599endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200600
601func Test_popup_move()
602 topleft vnew
603 call setline(1, 'hello')
604
605 let winid = popup_create('world', {
606 \ 'line': 1,
607 \ 'col': 1,
608 \ 'minwidth': 20,
609 \})
610 redraw
611 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
612 call assert_equal('world ', line)
613
614 call popup_move(winid, {'line': 2, 'col': 2})
615 redraw
616 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
617 call assert_equal('hello ', line)
618 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
619 call assert_equal('~world', line)
620
621 call popup_move(winid, {'line': 1})
622 redraw
623 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
624 call assert_equal('hworld', line)
625
626 call popup_close(winid)
627
628 bwipe!
629endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200630
Bram Moolenaar402502d2019-05-30 22:07:36 +0200631func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200632 let winid = popup_create('hello', {
633 \ 'line': 2,
634 \ 'col': 3,
635 \ 'minwidth': 10,
636 \ 'minheight': 11,
637 \})
638 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200639 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200640 call assert_equal(2, res.line)
641 call assert_equal(3, res.col)
642 call assert_equal(10, res.width)
643 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200644 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200645
646 call popup_close(winid)
647endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200648
649func Test_popup_width_longest()
650 let tests = [
651 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
652 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
653 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
654 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
655 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
656 \ ]
657
658 for test in tests
659 let winid = popup_create(test[0], {'line': 2, 'col': 3})
660 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200661 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200662 call assert_equal(test[1], position.width)
663 call popup_close(winid)
664 endfor
665endfunc
666
667func Test_popup_wraps()
668 let tests = [
669 \ ['nowrap', 6, 1],
670 \ ['a line that wraps once', 12, 2],
671 \ ['a line that wraps two times', 12, 3],
672 \ ]
673 for test in tests
674 let winid = popup_create(test[0],
675 \ {'line': 2, 'col': 3, 'maxwidth': 12})
676 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200677 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200678 call assert_equal(test[1], position.width)
679 call assert_equal(test[2], position.height)
680
681 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200682 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200683 endfor
684endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200685
686func Test_popup_getoptions()
687 let winid = popup_create('hello', {
688 \ 'line': 2,
689 \ 'col': 3,
690 \ 'minwidth': 10,
691 \ 'minheight': 11,
692 \ 'maxwidth': 20,
693 \ 'maxheight': 21,
694 \ 'zindex': 100,
695 \ 'time': 5000,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200696 \ 'fixed': 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200697 \})
698 redraw
699 let res = popup_getoptions(winid)
700 call assert_equal(2, res.line)
701 call assert_equal(3, res.col)
702 call assert_equal(10, res.minwidth)
703 call assert_equal(11, res.minheight)
704 call assert_equal(20, res.maxwidth)
705 call assert_equal(21, res.maxheight)
706 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200707 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200708 if has('timers')
709 call assert_equal(5000, res.time)
710 endif
711 call popup_close(winid)
712
713 let winid = popup_create('hello', {})
714 redraw
715 let res = popup_getoptions(winid)
716 call assert_equal(0, res.line)
717 call assert_equal(0, res.col)
718 call assert_equal(0, res.minwidth)
719 call assert_equal(0, res.minheight)
720 call assert_equal(0, res.maxwidth)
721 call assert_equal(0, res.maxheight)
722 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200723 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200724 if has('timers')
725 call assert_equal(0, res.time)
726 endif
727 call popup_close(winid)
728 call assert_equal({}, popup_getoptions(winid))
729endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200730
731func Test_popup_option_values()
732 new
733 " window-local
734 setlocal number
735 setlocal nowrap
736 " buffer-local
737 setlocal omnifunc=Something
738 " global/buffer-local
739 setlocal path=/there
740 " global/window-local
741 setlocal scrolloff=9
742
743 let winid = popup_create('hello', {})
744 call assert_equal(0, getwinvar(winid, '&number'))
745 call assert_equal(1, getwinvar(winid, '&wrap'))
746 call assert_equal('', getwinvar(winid, '&omnifunc'))
747 call assert_equal(&g:path, getwinvar(winid, '&path'))
748 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
749
750 call popup_close(winid)
751 bwipe
752endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200753
754func Test_popup_atcursor()
755 topleft vnew
756 call setline(1, [
757 \ 'xxxxxxxxxxxxxxxxx',
758 \ 'xxxxxxxxxxxxxxxxx',
759 \ 'xxxxxxxxxxxxxxxxx',
760 \])
761
762 call cursor(2, 2)
763 redraw
764 let winid = popup_atcursor('vim', {})
765 redraw
766 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
767 call assert_equal('xvimxxxxxxxxxxxxx', line)
768 call popup_close(winid)
769
770 call cursor(3, 4)
771 redraw
772 let winid = popup_atcursor('vim', {})
773 redraw
774 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
775 call assert_equal('xxxvimxxxxxxxxxxx', line)
776 call popup_close(winid)
777
778 call cursor(1, 1)
779 redraw
780 let winid = popup_create('vim', {
781 \ 'line': 'cursor+2',
782 \ 'col': 'cursor+1',
783 \})
784 redraw
785 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
786 call assert_equal('xvimxxxxxxxxxxxxx', line)
787 call popup_close(winid)
788
789 call cursor(3, 3)
790 redraw
791 let winid = popup_create('vim', {
792 \ 'line': 'cursor-2',
793 \ 'col': 'cursor-1',
794 \})
795 redraw
796 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
797 call assert_equal('xvimxxxxxxxxxxxxx', line)
798 call popup_close(winid)
799
Bram Moolenaar402502d2019-05-30 22:07:36 +0200800 " just enough room above
801 call cursor(3, 3)
802 redraw
803 let winid = popup_atcursor(['vim', 'is great'], {})
804 redraw
805 let pos = popup_getpos(winid)
806 call assert_equal(1, pos.line)
807 call popup_close(winid)
808
809 " not enough room above, popup goes below the cursor
810 call cursor(3, 3)
811 redraw
812 let winid = popup_atcursor(['vim', 'is', 'great'], {})
813 redraw
814 let pos = popup_getpos(winid)
815 call assert_equal(4, pos.line)
816 call popup_close(winid)
817
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200818 " cursor in first line, popup in line 2
819 call cursor(1, 1)
820 redraw
821 let winid = popup_atcursor(['vim', 'is', 'great'], {})
822 redraw
823 let pos = popup_getpos(winid)
824 call assert_equal(2, pos.line)
825 call popup_close(winid)
826
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200827 bwipe!
828endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200829
830func Test_popup_filter()
831 new
832 call setline(1, 'some text')
833
834 func MyPopupFilter(winid, c)
835 if a:c == 'e'
836 let g:eaten = 'e'
837 return 1
838 endif
839 if a:c == '0'
840 let g:ignored = '0'
841 return 0
842 endif
843 if a:c == 'x'
844 call popup_close(a:winid)
845 return 1
846 endif
847 return 0
848 endfunc
849
850 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
851 redraw
852
853 " e is consumed by the filter
854 call feedkeys('e', 'xt')
855 call assert_equal('e', g:eaten)
856
857 " 0 is ignored by the filter
858 normal $
859 call assert_equal(9, getcurpos()[2])
860 call feedkeys('0', 'xt')
861 call assert_equal('0', g:ignored)
862 call assert_equal(1, getcurpos()[2])
863
864 " x closes the popup
865 call feedkeys('x', 'xt')
866 call assert_equal('e', g:eaten)
867 call assert_equal(-1, winbufnr(winid))
868
869 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200870 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200871endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200872
873func Test_popup_close_callback()
874 func PopupDone(id, result)
875 let g:result = a:result
876 endfunc
877 let winid = popup_create('something', {'callback': 'PopupDone'})
878 redraw
879 call popup_close(winid, 'done')
880 call assert_equal('done', g:result)
881endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200882
883func Test_popup_empty()
884 let winid = popup_create('', {'padding': [2,2,2,2]})
885 redraw
886 let pos = popup_getpos(winid)
887 call assert_equal(4, pos.width)
888 call assert_equal(5, pos.height)
889
890 let winid = popup_create([], {'border': []})
891 redraw
892 let pos = popup_getpos(winid)
893 call assert_equal(2, pos.width)
894 call assert_equal(3, pos.height)
895endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +0200896
897func Test_popup_never_behind()
898 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200899 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +0200900 endif
901 " +-----------------------------+
902 " | | |
903 " | | |
904 " | | |
905 " | line1 |
906 " |------------line2------------|
907 " | line3 |
908 " | line4 |
909 " | |
910 " | |
911 " +-----------------------------+
912 let lines =<< trim END
913 only
914 split
915 vsplit
916 let info_window1 = getwininfo()[0]
917 let line = info_window1['height']
918 let col = info_window1['width']
919 call popup_create(['line1', 'line2', 'line3', 'line4'], {
920 \ 'line' : line,
921 \ 'col' : col,
922 \ })
923 END
924 call writefile(lines, 'XtestPopupBehind')
925 let buf = RunVimInTerminal('-S XtestPopupBehind', {'rows': 10})
926 call term_sendkeys(buf, "\<C-W>w")
927 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
928
929 " clean up
930 call StopVimInTerminal(buf)
931 call delete('XtestPopupBehind')
932endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200933
934func s:VerifyPosition( p, msg, line, col, width, height )
935 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
936 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
937 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
938 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
939endfunc
940
941func Test_popup_position_adjust()
942 " Anything placed past 2 cells from of the right of the screen is moved to the
943 " left.
944 "
945 " When wrapping is disabled, we also shift to the left to display on the
946 " screen, unless fixed is set.
947
948 " Entries for cases which don't vary based on wrapping.
949 " Format is per tests described below
950 let both_wrap_tests = [
951 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
952 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
953 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
954 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
955 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
956 \
957 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
958 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
959 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
960 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
961 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
962 \
963 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
964 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
965 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
966 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
967 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
968 \ ]
969
970 " these test groups are dicts with:
971 " - comment: something to identify the group of tests by
972 " - options: dict of options to merge with the row/col in tests
973 " - tests: list of cases. Each one is a list with elements:
974 " - text
975 " - row
976 " - col
977 " - expected row
978 " - expected col
979 " - expected width
980 " - expected height
981 let tests = [
982 \ {
983 \ 'comment': 'left-aligned with wrapping',
984 \ 'options': {
985 \ 'wrap': 1,
986 \ 'pos': 'botleft',
987 \ },
988 \ 'tests': both_wrap_tests + [
989 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
990 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
991 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
992 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
993 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
994 \ ],
995 \ },
996 \ {
997 \ 'comment': 'left aligned without wrapping',
998 \ 'options': {
999 \ 'wrap': 0,
1000 \ 'pos': 'botleft',
1001 \ },
1002 \ 'tests': both_wrap_tests + [
1003 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
1004 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
1005 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
1006 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
1007 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1008 \ ],
1009 \ },
1010 \ {
1011 \ 'comment': 'left aligned with fixed position',
1012 \ 'options': {
1013 \ 'wrap': 0,
1014 \ 'fixed': 1,
1015 \ 'pos': 'botleft',
1016 \ },
1017 \ 'tests': both_wrap_tests + [
1018 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1019 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1020 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1021 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1022 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1023 \ ],
1024 \ },
1025 \ ]
1026
1027 for test_group in tests
1028 for test in test_group.tests
1029 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
1030 let options = {
1031 \ 'line': line,
1032 \ 'col': col,
1033 \ }
1034 call extend( options, test_group.options )
1035
1036 let p = popup_create( text, options )
1037
1038 let msg = string( extend( options, { 'text': text } ) )
1039 call s:VerifyPosition( p, msg, e_line, e_col, e_width, e_height )
1040 call popup_close( p )
1041 endfor
1042 endfor
1043
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001044 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001045 %bwipe!
1046endfunc
1047
Bram Moolenaar3397f742019-06-02 18:40:06 +02001048func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001049 " width of screen
1050 let X = join(map(range(&columns), {->'X'}), '')
1051
1052 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1053 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
1054
1055 redraw
1056 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1057 call assert_equal(X, line)
1058
1059 call popup_close( p )
1060 redraw
1061
1062 " Same if placed on the right hand side
1063 let p = popup_create( X, { 'line': 1, 'col': &columns, 'wrap': 0 } )
1064 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
1065
1066 redraw
1067 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1068 call assert_equal(X, line)
1069
1070 call popup_close( p )
1071 redraw
1072
1073 " Extend so > window width
1074 let X .= 'x'
1075
1076 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
1077 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
1078
1079 redraw
1080 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1081 call assert_equal(X[ : -2 ], line)
1082
1083 call popup_close( p )
1084 redraw
1085
1086 " Shifted then truncated (the x is not visible)
1087 let p = popup_create( X, { 'line': 1, 'col': &columns - 3, 'wrap': 0 } )
1088 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
1089
1090 redraw
1091 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1092 call assert_equal(X[ : -2 ], line)
1093
1094 call popup_close( p )
1095 redraw
1096
1097 " Not shifted, just truncated
1098 let p = popup_create( X,
1099 \ { 'line': 1, 'col': 2, 'wrap': 0, 'fixed': 1 } )
1100 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
1101
1102 redraw
1103 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1104 let e_line = ' ' . X[ 1 : -2 ]
1105 call assert_equal(e_line, line)
1106
1107 call popup_close( p )
1108 redraw
1109
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001110 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001111 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001112endfunc
1113
1114func Test_popup_moved()
1115 new
1116 call test_override('char_avail', 1)
1117 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1118
1119 exe "normal gg0/word\<CR>"
1120 let winid = popup_atcursor('text', {'moved': 'any'})
1121 redraw
1122 call assert_equal(1, popup_getpos(winid).visible)
1123 " trigger the check for last_cursormoved by going into insert mode
1124 call feedkeys("li\<Esc>", 'xt')
1125 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001126 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001127
1128 exe "normal gg0/word\<CR>"
1129 let winid = popup_atcursor('text', {'moved': 'word'})
1130 redraw
1131 call assert_equal(1, popup_getpos(winid).visible)
1132 call feedkeys("hi\<Esc>", 'xt')
1133 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001134 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001135
1136 exe "normal gg0/word\<CR>"
1137 let winid = popup_atcursor('text', {'moved': 'word'})
1138 redraw
1139 call assert_equal(1, popup_getpos(winid).visible)
1140 call feedkeys("li\<Esc>", 'xt')
1141 call assert_equal(1, popup_getpos(winid).visible)
1142 call feedkeys("ei\<Esc>", 'xt')
1143 call assert_equal(1, popup_getpos(winid).visible)
1144 call feedkeys("eli\<Esc>", 'xt')
1145 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001146 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001147
Bram Moolenaar17627312019-06-02 19:53:44 +02001148 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001149 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001150 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001151 redraw
1152 call assert_equal(1, popup_getpos(winid).visible)
1153 call feedkeys("eli\<Esc>", 'xt')
1154 call assert_equal(1, popup_getpos(winid).visible)
1155 call feedkeys("wi\<Esc>", 'xt')
1156 call assert_equal(1, popup_getpos(winid).visible)
1157 call feedkeys("Eli\<Esc>", 'xt')
1158 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001159 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001160
1161 exe "normal gg0/word\<CR>"
1162 let winid = popup_atcursor('text', {'moved': [5, 10]})
1163 redraw
1164 call assert_equal(1, popup_getpos(winid).visible)
1165 call feedkeys("eli\<Esc>", 'xt')
1166 call feedkeys("ei\<Esc>", 'xt')
1167 call assert_equal(1, popup_getpos(winid).visible)
1168 call feedkeys("eli\<Esc>", 'xt')
1169 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001170 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001171
1172 bwipe!
1173 call test_override('ALL', 0)
1174endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001175
1176func Test_notifications()
1177 if !has('timers')
1178 throw 'Skipped, timer feature not supported'
1179 endif
1180 if !CanRunVimInTerminal()
1181 throw 'Skipped: cannot make screendumps'
1182 endif
1183
1184 call writefile([
1185 \ "call setline(1, range(1, 20))",
1186 \ "hi Notification ctermbg=lightblue",
1187 \ "call popup_notification('first notification', {})",
1188 \], 'XtestNotifications')
1189 let buf = RunVimInTerminal('-S XtestNotifications', {'rows': 10})
1190 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1191
1192 " second one goes below the first one
1193 call term_sendkeys(buf, ":call popup_notification('another important notification', {'highlight': 'Notification'})\<CR>")
1194 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1195
1196
1197 " clean up
1198 call StopVimInTerminal(buf)
1199 call delete('XtestNotifications')
1200endfunc