blob: 0ec901cb594124943875a66fea0db57e3d033fbb [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature textprop
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
9 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020010 throw 'Skipped: cannot make screendumps'
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011 endif
Bram Moolenaare7eb9272019-06-24 00:58:07 +020012 let lines =<< trim END
13 call setline(1, range(1, 100))
14 hi PopupColor1 ctermbg=lightblue
15 hi PopupColor2 ctermbg=lightcyan
16 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020017 call prop_type_add('comment', #{highlight: 'Comment'})
18 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
19 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020020 call setwinvar(winid2, '&wincolor', 'PopupColor2')
21 END
22 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020023 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020024 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 Moolenaar4c6d9042019-07-16 22:04:02 +020029 \ .. "#{text: 'other tab'},"
30 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020031 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +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 Moolenaar4c6d9042019-07-16 22:04:02 +020051 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020052 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>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020062 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020063 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020064 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
65
66 " clear all popups after moving the cursor a bit, so that ruler is updated
67 call term_sendkeys(buf, "axxx\<Esc>")
68 call term_wait(buf)
69 call term_sendkeys(buf, "0")
70 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020071 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020072 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
73
Bram Moolenaar4d784b22019-05-25 19:51:39 +020074 " clean up
75 call StopVimInTerminal(buf)
76 call delete('XtestPopup')
77endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020078
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020079func Test_popup_with_border_and_padding()
80 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020081 throw 'Skipped: cannot make screendumps'
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082 endif
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020083
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020084 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020085 let lines =<< trim END
86 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020087 call popup_create('hello border', #{line: 2, col: 3, border: []})
88 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
89 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: []})
90 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
91 call popup_create('paddings', #{line: 6, col: 23, padding: [1, 3, 2, 4]})
92 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
93 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020094 END
95 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
96 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020097 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020098 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
99
100 call StopVimInTerminal(buf)
101 call delete('XtestPopupBorder')
102 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200103
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200104 let lines =<< trim END
105 call setline(1, range(1, 100))
106 hi BlueColor ctermbg=lightblue
107 hi TopColor ctermbg=253
108 hi RightColor ctermbg=245
109 hi BottomColor ctermbg=240
110 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200111 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
112 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
113 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
114 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
115 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
116 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200117 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200118 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200119 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200120 END
121 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200122 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200123 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
124
Bram Moolenaarad24a712019-06-17 20:05:45 +0200125 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200126 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200127 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
128
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200129 " check multi-byte border only with 'ambiwidth' single
130 if &ambiwidth == 'single'
131 call term_sendkeys(buf, ":call MultiByte()\<CR>")
132 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
133 endif
134
Bram Moolenaar790498b2019-06-01 22:15:29 +0200135 call StopVimInTerminal(buf)
136 call delete('XtestPopupBorder')
137
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200138 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200139 \ line: 2,
140 \ core_line: 3,
141 \ col: 3,
142 \ core_col: 4,
143 \ width: 14,
144 \ core_width: 12,
145 \ height: 3,
146 \ core_height: 1,
147 \ firstline: 1,
148 \ scrollbar: 0,
149 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200150 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200151 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200152 let options = popup_getoptions(winid)
153 call assert_equal([], options.border)
154 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200155
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200156 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200157 let with_border_or_padding.width = 15
158 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200159 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200160 let options = popup_getoptions(winid)
161 call assert_false(has_key(options, "border"))
162 call assert_equal([], options.padding)
163
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200164 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200165 \ padding: [1, 2, 3, 4],
166 \ border: [4, 0, 7, 8],
167 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
168 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200169 \ })
170 let options = popup_getoptions(winid)
171 call assert_equal([1, 0, 1, 1], options.border)
172 call assert_equal([1, 2, 3, 4], options.padding)
173 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
174 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200175
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200176 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
177 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200178 \ line: 3,
179 \ core_line: 5,
180 \ col: 8,
181 \ core_col: 10,
182 \ width: 14,
183 \ core_width: 10,
184 \ height: 5,
185 \ scrollbar: 0,
186 \ core_height: 1,
187 \ firstline: 1,
188 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200189
190 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200191endfunc
192
Bram Moolenaarb4230122019-05-30 18:40:53 +0200193func Test_popup_with_syntax_win_execute()
194 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200195 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200196 endif
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200197 let lines =<< trim END
198 call setline(1, range(1, 100))
199 hi PopupColor ctermbg=lightblue
200 let winid = popup_create([
201 \ '#include <stdio.h>',
202 \ 'int main(void)',
203 \ '{',
204 \ ' printf(123);',
205 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200206 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200207 call win_execute(winid, 'set syntax=cpp')
208 END
209 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200210 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200211 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
212
213 " clean up
214 call StopVimInTerminal(buf)
215 call delete('XtestPopup')
216endfunc
217
218func Test_popup_with_syntax_setbufvar()
219 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200220 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200221 endif
Bram Moolenaar402502d2019-05-30 22:07:36 +0200222 let lines =<< trim END
223 call setline(1, range(1, 100))
224 hi PopupColor ctermbg=lightgrey
225 let winid = popup_create([
226 \ '#include <stdio.h>',
227 \ 'int main(void)',
228 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200229 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200230 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200231 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200232 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
233 END
234 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200235 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200236 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
237
238 " clean up
239 call StopVimInTerminal(buf)
240 call delete('XtestPopup')
241endfunc
242
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200243func Test_popup_with_matches()
244 if !CanRunVimInTerminal()
245 throw 'Skipped: cannot make screendumps'
246 endif
247 let lines =<< trim END
248 call setline(1, ['111 222 333', '444 555 666'])
249 let winid = popup_create([
250 \ '111 222 333',
251 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200252 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200253 set hlsearch
254 /666
255 call matchadd('ErrorMsg', '111')
256 call matchadd('ErrorMsg', '444')
257 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
258 call win_execute(winid, "call matchadd('ErrorMsg', '555')")
259 END
260 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200261 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200262 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
263
264 " clean up
265 call StopVimInTerminal(buf)
266 call delete('XtestPopupMatches')
267endfunc
268
Bram Moolenaar399d8982019-06-02 15:34:29 +0200269func Test_popup_all_corners()
270 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200271 throw 'Skipped: cannot make screendumps'
Bram Moolenaar399d8982019-06-02 15:34:29 +0200272 endif
273 let lines =<< trim END
274 call setline(1, repeat([repeat('-', 60)], 15))
275 set so=0
276 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200277 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200278 \ line: 'cursor+1',
279 \ col: 'cursor',
280 \ pos: 'topleft',
281 \ border: [],
282 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200283 \ })
284 normal 25|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200285 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200286 \ line: 'cursor+1',
287 \ col: 'cursor',
288 \ pos: 'topright',
289 \ border: [],
290 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200291 \ })
292 normal 9G29|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200293 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200294 \ line: 'cursor-1',
295 \ col: 'cursor',
296 \ pos: 'botleft',
297 \ border: [],
298 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200299 \ })
300 normal 51|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200301 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200302 \ line: 'cursor-1',
303 \ col: 'cursor',
304 \ pos: 'botright',
305 \ border: [],
306 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200307 \ })
308 END
309 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200310 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200311 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
312
313 " clean up
314 call StopVimInTerminal(buf)
315 call delete('XtestPopupCorners')
316endfunc
317
Bram Moolenaar8d241042019-06-12 23:40:01 +0200318func Test_popup_firstline()
319 if !CanRunVimInTerminal()
320 throw 'Skipped: cannot make screendumps'
321 endif
322 let lines =<< trim END
323 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200324 call popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200325 \ maxheight: 4,
326 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200327 \ })
328 END
329 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200330 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200331 call VerifyScreenDump(buf, 'Test_popupwin_firstline', {})
332
333 " clean up
334 call StopVimInTerminal(buf)
335 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200336
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200337 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200338 \ maxheight: 2,
339 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200340 \ })
341 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200342 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200343 call assert_equal(1, popup_getoptions(winid).firstline)
344
345 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200346endfunc
347
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200348func Test_popup_drag()
349 if !CanRunVimInTerminal()
350 throw 'Skipped: cannot make screendumps'
351 endif
352 " create a popup that covers the command line
353 let lines =<< trim END
354 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200355 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200356 \ drag: 1,
357 \ border: [],
358 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200359 \ })
360 func Dragit()
361 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
362 endfunc
363 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
364 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2)<CR>
365 END
366 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200367 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200368 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
369
370 call term_sendkeys(buf, ":call Dragit()\<CR>")
371 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
372
373 " clean up
374 call StopVimInTerminal(buf)
375 call delete('XtestPopupDrag')
376endfunc
377
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200378func Test_popup_close_with_mouse()
379 if !CanRunVimInTerminal()
380 throw 'Skipped: cannot make screendumps'
381 endif
382 let lines =<< trim END
383 call setline(1, range(1, 20))
384 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200385 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200386 \ close: 'button',
387 \ border: [],
388 \ line: 1,
389 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200390 \ })
391 func CloseMsg(id, result)
392 echomsg 'Popup closed with ' .. a:result
393 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200394 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200395 \ close: 'click',
396 \ line: 3,
397 \ col: 15,
398 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200399 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200400 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200401 \ close: 'button',
402 \ line: 5,
403 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200404 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200405 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200406 \ close: 'button',
407 \ padding: [],
408 \ line: 5,
409 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200410 \ })
411 func CloseWithX()
412 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
413 endfunc
414 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
415 func CloseWithClick()
416 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
417 endfunc
418 map <silent> <F4> :call test_setmouse(3, 17)<CR>
419 END
420 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200421 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200422 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
423
424 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
425 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
426
427 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
428 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
429
430 " clean up
431 call StopVimInTerminal(buf)
432 call delete('XtestPopupClose')
433endfunction
434
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200435func Test_popup_with_mask()
436 if !CanRunVimInTerminal()
437 throw 'Skipped: cannot make screendumps'
438 endif
439 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200440 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200441 hi PopupColor ctermbg=lightgrey
442 let winid = popup_create([
443 \ 'some text',
444 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200445 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200446 \ line: 1,
447 \ col: 10,
448 \ wrap: 0,
449 \ fixed: 1,
450 \ zindex: 90,
451 \ padding: [],
452 \ highlight: 'PopupColor',
453 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200454 call popup_create([
455 \ 'xxxxxxxxx',
456 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200457 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200458 \ line: 3,
459 \ col: 18,
460 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200461 let winidb = popup_create([
462 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200463 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200464 \ line: 7,
465 \ col: 10,
466 \ wrap: 0,
467 \ fixed: 1,
468 \ close: 'button',
469 \ zindex: 90,
470 \ padding: [],
471 \ border: [],
472 \ mask: [[1,2,1,1], [-5,-1,4,4], [7,9,2,3], [3,5,5,5],[-7,-4,5,5]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200473 END
474 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200475 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200476 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
477
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200478 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
479 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200480 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200481 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
482
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200483 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
484 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200485 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200486 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
487
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200488 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
489 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200490 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200491 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
492
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200493 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
494 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200495 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200496 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
497
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200498 " clean up
499 call StopVimInTerminal(buf)
500 call delete('XtestPopupMask')
501endfunc
502
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200503func Test_popup_select()
504 if !CanRunVimInTerminal()
505 throw 'Skipped: cannot make screendumps'
506 endif
Bram Moolenaar650a6372019-06-15 00:29:33 +0200507 if !has('clipboard')
508 throw 'Skipped: clipboard feature missing'
509 endif
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200510 " create a popup with some text to be selected
511 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200512 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200513 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200514 let winid = popup_create(['the word', 'some more', 'several words here'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200515 \ drag: 1,
516 \ border: [],
517 \ line: 3,
518 \ col: 10,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200519 \ })
520 func Select1()
521 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
522 endfunc
523 map <silent> <F3> :call test_setmouse(4, 15)<CR>
524 map <silent> <F4> :call test_setmouse(6, 23)<CR>
525 END
526 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200527 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200528 call term_sendkeys(buf, ":call Select1()\<CR>")
529 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
530
531 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
532 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200533 " clean the command line, sometimes it still shows a command
534 call term_sendkeys(buf, ":\<esc>")
535
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200536 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
537
538 " clean up
539 call StopVimInTerminal(buf)
540 call delete('XtestPopupSelect')
541endfunc
542
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200543func Test_popup_in_tab()
544 " default popup is local to tab, not visible when in other tab
545 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200546 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200547 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200548 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200549 tabnew
550 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200551 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200552 quit
553 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200554
555 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200556 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200557 " buffer is gone now
558 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200559
560 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200561 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200562 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200563 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200564 tabnew
565 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200566 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200567 quit
568 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200569 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200570
571 " create popup in other tab
572 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200573 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200574 call assert_equal(0, popup_getpos(winid).visible)
575 call assert_equal(1, popup_getoptions(winid).tabpage)
576 quit
577 call assert_equal(1, popup_getpos(winid).visible)
578 call assert_equal(0, popup_getoptions(winid).tabpage)
579 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200580endfunc
581
582func Test_popup_valid_arguments()
583 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200584 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200585 let pos = popup_getpos(winid)
586 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200587 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200588
589 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200590 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200591 let pos = popup_getpos(winid)
592 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200593 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200594
595 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200596 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200597 let pos = popup_getpos(winid)
598 let around = (&columns - pos.width) / 2
599 call assert_inrange(around - 1, around + 1, pos.col)
600 let around = (&lines - pos.height) / 2
601 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200602 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200603endfunc
604
605func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200606 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200607 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200608 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200609 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200610
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200611 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200612 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200613 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200614 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200615 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200616 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200617 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200618 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200619
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200620 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200621 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200622 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200623 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200624 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200625 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200626 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200627 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200628
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200629 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200630 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200631 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200632 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200633 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200634 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200635 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200636 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200637 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
638 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200639 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200640 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200641
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200642 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200643 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200644 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200645 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200646 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200647 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200648 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
649 call popup_clear()
650 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
651 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200652endfunc
653
Bram Moolenaareea16992019-05-31 17:34:48 +0200654func Test_win_execute_closing_curwin()
655 split
656 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200657 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200658 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200659endfunc
660
661func Test_win_execute_not_allowed()
662 let winid = popup_create('some text', {})
663 call assert_fails('call win_execute(winid, "split")', 'E994:')
664 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
665 call assert_fails('call win_execute(winid, "close")', 'E994:')
666 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200667 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200668 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
669 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
670 call assert_fails('call win_execute(winid, "next")', 'E994:')
671 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
672 call assert_fails('call win_execute(winid, "buf")', 'E994:')
673 call assert_fails('call win_execute(winid, "edit")', 'E994:')
674 call assert_fails('call win_execute(winid, "enew")', 'E994:')
675 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
676 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
677 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
678 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200679 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200680endfunc
681
Bram Moolenaar402502d2019-05-30 22:07:36 +0200682func Test_popup_with_wrap()
683 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200684 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200685 endif
686 let lines =<< trim END
687 call setline(1, range(1, 100))
688 let winid = popup_create(
689 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200690 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200691 END
692 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200693 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200694 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
695
696 " clean up
697 call StopVimInTerminal(buf)
698 call delete('XtestPopup')
699endfunc
700
701func Test_popup_without_wrap()
702 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200703 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200704 endif
705 let lines =<< trim END
706 call setline(1, range(1, 100))
707 let winid = popup_create(
708 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200709 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200710 END
711 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200712 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200713 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
714
715 " clean up
716 call StopVimInTerminal(buf)
717 call delete('XtestPopup')
718endfunc
719
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200720func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200721 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +0200722 throw 'Skipped: timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200723 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200724 topleft vnew
725 call setline(1, 'hello')
726
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200727 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200728 \ line: 1,
729 \ col: 1,
730 \ minwidth: 20,
731 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200732 \})
733 redraw
734 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
735 call assert_equal('world', line)
736
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200737 call assert_equal(winid, popup_locate(1, 1))
738 call assert_equal(winid, popup_locate(1, 20))
739 call assert_equal(0, popup_locate(1, 21))
740 call assert_equal(0, popup_locate(2, 1))
741
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200742 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200743 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200744 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
745 call assert_equal('hello', line)
746
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200747 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200748 \ line: &lines,
749 \ col: 10,
750 \ minwidth: 20,
751 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200752 \})
753 redraw
754 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
755 call assert_match('.*on the command line.*', line)
756
757 sleep 700m
758 redraw
759 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
760 call assert_notmatch('.*on the command line.*', line)
761
762 bwipe!
763endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200764
765func Test_popup_hide()
766 topleft vnew
767 call setline(1, 'hello')
768
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200769 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200770 \ line: 1,
771 \ col: 1,
772 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200773 \})
774 redraw
775 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
776 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200777 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200778 " buffer is still listed and active
779 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200780
781 call popup_hide(winid)
782 redraw
783 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
784 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200785 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200786 " buffer is still listed but hidden
787 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200788
789 call popup_show(winid)
790 redraw
791 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
792 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200793 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200794
795
796 call popup_close(winid)
797 redraw
798 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
799 call assert_equal('hello', line)
800
801 " error is given for existing non-popup window
802 call assert_fails('call popup_hide(win_getid())', 'E993:')
803
804 " no error non-existing window
805 call popup_hide(1234234)
806 call popup_show(41234234)
807
808 bwipe!
809endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200810
811func Test_popup_move()
812 topleft vnew
813 call setline(1, 'hello')
814
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200815 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200816 \ line: 1,
817 \ col: 1,
818 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200819 \})
820 redraw
821 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
822 call assert_equal('world ', line)
823
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200824 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200825 redraw
826 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
827 call assert_equal('hello ', line)
828 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
829 call assert_equal('~world', line)
830
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200831 call popup_move(winid, #{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200832 redraw
833 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
834 call assert_equal('hworld', line)
835
836 call popup_close(winid)
837
838 bwipe!
839endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200840
Bram Moolenaar402502d2019-05-30 22:07:36 +0200841func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200842 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200843 \ line: 2,
844 \ col: 3,
845 \ minwidth: 10,
846 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200847 \})
848 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200849 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200850 call assert_equal(2, res.line)
851 call assert_equal(3, res.col)
852 call assert_equal(10, res.width)
853 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200854 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200855
856 call popup_close(winid)
857endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200858
859func Test_popup_width_longest()
860 let tests = [
861 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
862 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
863 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
864 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
865 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
866 \ ]
867
868 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200869 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200870 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200871 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200872 call assert_equal(test[1], position.width)
873 call popup_close(winid)
874 endfor
875endfunc
876
877func Test_popup_wraps()
878 let tests = [
879 \ ['nowrap', 6, 1],
880 \ ['a line that wraps once', 12, 2],
881 \ ['a line that wraps two times', 12, 3],
882 \ ]
883 for test in tests
884 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200885 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200886 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200887 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200888 call assert_equal(test[1], position.width)
889 call assert_equal(test[2], position.height)
890
891 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200892 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200893 endfor
894endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200895
896func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200897 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200898 \ line: 2,
899 \ col: 3,
900 \ minwidth: 10,
901 \ minheight: 11,
902 \ maxwidth: 20,
903 \ maxheight: 21,
904 \ zindex: 100,
905 \ time: 5000,
906 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200907 \})
908 redraw
909 let res = popup_getoptions(winid)
910 call assert_equal(2, res.line)
911 call assert_equal(3, res.col)
912 call assert_equal(10, res.minwidth)
913 call assert_equal(11, res.minheight)
914 call assert_equal(20, res.maxwidth)
915 call assert_equal(21, res.maxheight)
916 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200917 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200918 if has('timers')
919 call assert_equal(5000, res.time)
920 endif
921 call popup_close(winid)
922
923 let winid = popup_create('hello', {})
924 redraw
925 let res = popup_getoptions(winid)
926 call assert_equal(0, res.line)
927 call assert_equal(0, res.col)
928 call assert_equal(0, res.minwidth)
929 call assert_equal(0, res.minheight)
930 call assert_equal(0, res.maxwidth)
931 call assert_equal(0, res.maxheight)
932 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200933 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200934 if has('timers')
935 call assert_equal(0, res.time)
936 endif
937 call popup_close(winid)
938 call assert_equal({}, popup_getoptions(winid))
939endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200940
941func Test_popup_option_values()
942 new
943 " window-local
944 setlocal number
945 setlocal nowrap
946 " buffer-local
947 setlocal omnifunc=Something
948 " global/buffer-local
949 setlocal path=/there
950 " global/window-local
951 setlocal scrolloff=9
952
953 let winid = popup_create('hello', {})
954 call assert_equal(0, getwinvar(winid, '&number'))
955 call assert_equal(1, getwinvar(winid, '&wrap'))
956 call assert_equal('', getwinvar(winid, '&omnifunc'))
957 call assert_equal(&g:path, getwinvar(winid, '&path'))
958 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
959
960 call popup_close(winid)
961 bwipe
962endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200963
964func Test_popup_atcursor()
965 topleft vnew
966 call setline(1, [
967 \ 'xxxxxxxxxxxxxxxxx',
968 \ 'xxxxxxxxxxxxxxxxx',
969 \ 'xxxxxxxxxxxxxxxxx',
970 \])
971
972 call cursor(2, 2)
973 redraw
974 let winid = popup_atcursor('vim', {})
975 redraw
976 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
977 call assert_equal('xvimxxxxxxxxxxxxx', line)
978 call popup_close(winid)
979
980 call cursor(3, 4)
981 redraw
982 let winid = popup_atcursor('vim', {})
983 redraw
984 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
985 call assert_equal('xxxvimxxxxxxxxxxx', line)
986 call popup_close(winid)
987
988 call cursor(1, 1)
989 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200990 let winid = popup_create('vim', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200991 \ line: 'cursor+2',
992 \ col: 'cursor+1',
993 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200994 redraw
995 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
996 call assert_equal('xvimxxxxxxxxxxxxx', line)
997 call popup_close(winid)
998
999 call cursor(3, 3)
1000 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001001 let winid = popup_create('vim', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001002 \ line: 'cursor-2',
1003 \ col: 'cursor-1',
1004 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001005 redraw
1006 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1007 call assert_equal('xvimxxxxxxxxxxxxx', line)
1008 call popup_close(winid)
1009
Bram Moolenaar402502d2019-05-30 22:07:36 +02001010 " just enough room above
1011 call cursor(3, 3)
1012 redraw
1013 let winid = popup_atcursor(['vim', 'is great'], {})
1014 redraw
1015 let pos = popup_getpos(winid)
1016 call assert_equal(1, pos.line)
1017 call popup_close(winid)
1018
1019 " not enough room above, popup goes below the cursor
1020 call cursor(3, 3)
1021 redraw
1022 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1023 redraw
1024 let pos = popup_getpos(winid)
1025 call assert_equal(4, pos.line)
1026 call popup_close(winid)
1027
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001028 " cursor in first line, popup in line 2
1029 call cursor(1, 1)
1030 redraw
1031 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1032 redraw
1033 let pos = popup_getpos(winid)
1034 call assert_equal(2, pos.line)
1035 call popup_close(winid)
1036
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001037 bwipe!
1038endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001039
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001040func Test_popup_beval()
1041 if !CanRunVimInTerminal()
1042 throw 'Skipped: cannot make screendumps'
1043 endif
1044
1045 let lines =<< trim END
1046 call setline(1, range(1, 20))
1047 call setline(5, 'here is some text to hover over')
1048 set balloonevalterm
1049 set balloonexpr=BalloonExpr()
1050 set balloondelay=100
1051 func BalloonExpr()
1052 let s:winid = popup_beval([v:beval_text], {})
1053 return ''
1054 endfunc
1055 func Hover()
1056 call test_setmouse(5, 15)
1057 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1058 sleep 100m
1059 endfunc
1060 func MoveOntoPopup()
1061 call test_setmouse(4, 17)
1062 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1063 endfunc
1064 func MoveAway()
1065 call test_setmouse(5, 13)
1066 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1067 endfunc
1068 END
1069 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001070 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001071 call term_wait(buf, 100)
1072 call term_sendkeys(buf, 'j')
1073 call term_sendkeys(buf, ":call Hover()\<CR>")
1074 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1075
1076 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1077 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1078
1079 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1080 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1081
1082 " clean up
1083 call StopVimInTerminal(buf)
1084 call delete('XtestPopupBeval')
1085endfunc
1086
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001087func Test_popup_filter()
1088 new
1089 call setline(1, 'some text')
1090
1091 func MyPopupFilter(winid, c)
1092 if a:c == 'e'
1093 let g:eaten = 'e'
1094 return 1
1095 endif
1096 if a:c == '0'
1097 let g:ignored = '0'
1098 return 0
1099 endif
1100 if a:c == 'x'
1101 call popup_close(a:winid)
1102 return 1
1103 endif
1104 return 0
1105 endfunc
1106
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001107 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001108 redraw
1109
1110 " e is consumed by the filter
1111 call feedkeys('e', 'xt')
1112 call assert_equal('e', g:eaten)
1113
1114 " 0 is ignored by the filter
1115 normal $
1116 call assert_equal(9, getcurpos()[2])
1117 call feedkeys('0', 'xt')
1118 call assert_equal('0', g:ignored)
1119 call assert_equal(1, getcurpos()[2])
1120
1121 " x closes the popup
1122 call feedkeys('x', 'xt')
1123 call assert_equal('e', g:eaten)
1124 call assert_equal(-1, winbufnr(winid))
1125
1126 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001127 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001128endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001129
Bram Moolenaara42d9452019-06-15 21:46:30 +02001130func ShowDialog(key, result)
1131 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001132 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001133 \ filter: 'popup_filter_yesno',
1134 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001135 \ })
1136 redraw
1137 call feedkeys(a:key, "xt")
1138 call assert_equal(winid, s:cb_winid)
1139 call assert_equal(a:result, s:cb_res)
1140endfunc
1141
1142func Test_popup_dialog()
1143 func QuitCallback(id, res)
1144 let s:cb_winid = a:id
1145 let s:cb_res = a:res
1146 endfunc
1147
1148 let winid = ShowDialog("y", 1)
1149 let winid = ShowDialog("Y", 1)
1150 let winid = ShowDialog("n", 0)
1151 let winid = ShowDialog("N", 0)
1152 let winid = ShowDialog("x", 0)
1153 let winid = ShowDialog("X", 0)
1154 let winid = ShowDialog("\<Esc>", 0)
1155 let winid = ShowDialog("\<C-C>", -1)
1156
1157 delfunc QuitCallback
1158endfunc
1159
Bram Moolenaara730e552019-06-16 19:05:31 +02001160func ShowMenu(key, result)
1161 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001162 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001163 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001164 \ })
1165 redraw
1166 call feedkeys(a:key, "xt")
1167 call assert_equal(winid, s:cb_winid)
1168 call assert_equal(a:result, s:cb_res)
1169endfunc
1170
1171func Test_popup_menu()
1172 func QuitCallback(id, res)
1173 let s:cb_winid = a:id
1174 let s:cb_res = a:res
1175 endfunc
1176
1177 let winid = ShowMenu(" ", 1)
1178 let winid = ShowMenu("j \<CR>", 2)
1179 let winid = ShowMenu("JjK \<CR>", 2)
1180 let winid = ShowMenu("jjjjjj ", 3)
1181 let winid = ShowMenu("kkk ", 1)
1182 let winid = ShowMenu("x", -1)
1183 let winid = ShowMenu("X", -1)
1184 let winid = ShowMenu("\<Esc>", -1)
1185 let winid = ShowMenu("\<C-C>", -1)
1186
1187 delfunc QuitCallback
1188endfunc
1189
1190func Test_popup_menu_screenshot()
1191 if !CanRunVimInTerminal()
1192 throw 'Skipped: cannot make screendumps'
1193 endif
1194
1195 let lines =<< trim END
1196 call setline(1, range(1, 20))
1197 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001198 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001199 func MenuDone(id, res)
1200 echomsg "selected " .. a:res
1201 endfunc
1202 END
1203 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001204 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001205 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1206
1207 call term_sendkeys(buf, "jj")
1208 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1209
1210 call term_sendkeys(buf, " ")
1211 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1212
1213 " clean up
1214 call StopVimInTerminal(buf)
1215 call delete('XtestPopupMenu')
1216endfunc
1217
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001218func Test_popup_title()
1219 if !CanRunVimInTerminal()
1220 throw 'Skipped: cannot make screendumps'
1221 endif
1222
1223 " Create a popup without title or border, a line of padding will be added to
1224 " put the title on.
1225 let lines =<< trim END
1226 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001227 call popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001228 END
1229 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001230 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001231 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1232
1233 " clean up
1234 call StopVimInTerminal(buf)
1235 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001236
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001237 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001238 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001239 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001240 call assert_equal('Another Title', popup_getoptions(winid).title)
1241
1242 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001243endfunc
1244
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001245func Test_popup_close_callback()
1246 func PopupDone(id, result)
1247 let g:result = a:result
1248 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001249 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001250 redraw
1251 call popup_close(winid, 'done')
1252 call assert_equal('done', g:result)
1253endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001254
1255func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001256 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001257 redraw
1258 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001259 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001260 call assert_equal(5, pos.height)
1261
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001262 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001263 redraw
1264 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001265 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001266 call assert_equal(3, pos.height)
1267endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001268
1269func Test_popup_never_behind()
1270 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001271 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +02001272 endif
1273 " +-----------------------------+
1274 " | | |
1275 " | | |
1276 " | | |
1277 " | line1 |
1278 " |------------line2------------|
1279 " | line3 |
1280 " | line4 |
1281 " | |
1282 " | |
1283 " +-----------------------------+
1284 let lines =<< trim END
1285 only
1286 split
1287 vsplit
1288 let info_window1 = getwininfo()[0]
1289 let line = info_window1['height']
1290 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001291 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001292 \ line : line,
1293 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001294 \ })
1295 END
1296 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001297 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001298 call term_sendkeys(buf, "\<C-W>w")
1299 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1300
1301 " clean up
1302 call StopVimInTerminal(buf)
1303 call delete('XtestPopupBehind')
1304endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001305
1306func s:VerifyPosition( p, msg, line, col, width, height )
1307 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
1308 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
1309 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
1310 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
1311endfunc
1312
1313func Test_popup_position_adjust()
1314 " Anything placed past 2 cells from of the right of the screen is moved to the
1315 " left.
1316 "
1317 " When wrapping is disabled, we also shift to the left to display on the
1318 " screen, unless fixed is set.
1319
1320 " Entries for cases which don't vary based on wrapping.
1321 " Format is per tests described below
1322 let both_wrap_tests = [
1323 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
1324 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
1325 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
1326 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
1327 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
1328 \
1329 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
1330 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
1331 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
1332 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
1333 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
1334 \
1335 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1336 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1337 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1338 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1339 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
1340 \ ]
1341
1342 " these test groups are dicts with:
1343 " - comment: something to identify the group of tests by
1344 " - options: dict of options to merge with the row/col in tests
1345 " - tests: list of cases. Each one is a list with elements:
1346 " - text
1347 " - row
1348 " - col
1349 " - expected row
1350 " - expected col
1351 " - expected width
1352 " - expected height
1353 let tests = [
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001354 \ #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001355 \ comment: 'left-aligned with wrapping',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001356 \ options: #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001357 \ wrap: 1,
1358 \ pos: 'botleft',
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001359 \ },
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001360 \ tests: both_wrap_tests + [
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001361 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
1362 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
1363 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
1364 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
1365 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1366 \ ],
1367 \ },
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001368 \ #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001369 \ comment: 'left aligned without wrapping',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001370 \ options: #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001371 \ wrap: 0,
1372 \ pos: 'botleft',
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001373 \ },
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001374 \ tests: both_wrap_tests + [
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001375 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
1376 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
1377 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
1378 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
1379 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1380 \ ],
1381 \ },
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001382 \ #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001383 \ comment: 'left aligned with fixed position',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001384 \ options: #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001385 \ wrap: 0,
1386 \ fixed: 1,
1387 \ pos: 'botleft',
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001388 \ },
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001389 \ tests: both_wrap_tests + [
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001390 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1391 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1392 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1393 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1394 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1395 \ ],
1396 \ },
1397 \ ]
1398
1399 for test_group in tests
1400 for test in test_group.tests
1401 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001402 let options = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001403 \ line: line,
1404 \ col: col,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001405 \ }
1406 call extend( options, test_group.options )
1407
1408 let p = popup_create( text, options )
1409
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001410 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001411 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1412 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001413 endfor
1414 endfor
1415
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001416 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001417 %bwipe!
1418endfunc
1419
Bram Moolenaar3397f742019-06-02 18:40:06 +02001420func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001421 " width of screen
1422 let X = join(map(range(&columns), {->'X'}), '')
1423
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001424 let p = popup_create( X, #{line: 1, col: 1, wrap: 0})
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001425 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
1426
1427 redraw
1428 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1429 call assert_equal(X, line)
1430
1431 call popup_close( p )
1432 redraw
1433
1434 " Same if placed on the right hand side
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001435 let p = popup_create( X, #{line: 1, col: &columns, wrap: 0})
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001436 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
1437
1438 redraw
1439 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1440 call assert_equal(X, line)
1441
1442 call popup_close( p )
1443 redraw
1444
1445 " Extend so > window width
1446 let X .= 'x'
1447
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001448 let p = popup_create( X, #{line: 1, col: 1, wrap: 0})
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001449 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
1450
1451 redraw
1452 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1453 call assert_equal(X[ : -2 ], line)
1454
1455 call popup_close( p )
1456 redraw
1457
1458 " Shifted then truncated (the x is not visible)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001459 let p = popup_create( X, #{line: 1, col: &columns - 3, wrap: 0})
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001460 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
1461
1462 redraw
1463 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1464 call assert_equal(X[ : -2 ], line)
1465
1466 call popup_close( p )
1467 redraw
1468
1469 " Not shifted, just truncated
1470 let p = popup_create( X,
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001471 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001472 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
1473
1474 redraw
1475 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1476 let e_line = ' ' . X[ 1 : -2 ]
1477 call assert_equal(e_line, line)
1478
1479 call popup_close( p )
1480 redraw
1481
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001482 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001483 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001484endfunc
1485
1486func Test_popup_moved()
1487 new
1488 call test_override('char_avail', 1)
1489 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1490
1491 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001492 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001493 redraw
1494 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001495 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001496 " trigger the check for last_cursormoved by going into insert mode
1497 call feedkeys("li\<Esc>", 'xt')
1498 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001499 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001500
1501 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001502 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001503 redraw
1504 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001505 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001506 call feedkeys("hi\<Esc>", 'xt')
1507 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001508 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001509
1510 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001511 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001512 redraw
1513 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001514 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001515 call feedkeys("li\<Esc>", 'xt')
1516 call assert_equal(1, popup_getpos(winid).visible)
1517 call feedkeys("ei\<Esc>", 'xt')
1518 call assert_equal(1, popup_getpos(winid).visible)
1519 call feedkeys("eli\<Esc>", 'xt')
1520 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001521 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001522
Bram Moolenaar17627312019-06-02 19:53:44 +02001523 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001524 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001525 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001526 redraw
1527 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001528 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001529 call feedkeys("eli\<Esc>", 'xt')
1530 call assert_equal(1, popup_getpos(winid).visible)
1531 call feedkeys("wi\<Esc>", 'xt')
1532 call assert_equal(1, popup_getpos(winid).visible)
1533 call feedkeys("Eli\<Esc>", 'xt')
1534 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001535 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001536
1537 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001538 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001539 redraw
1540 call assert_equal(1, popup_getpos(winid).visible)
1541 call feedkeys("eli\<Esc>", 'xt')
1542 call feedkeys("ei\<Esc>", 'xt')
1543 call assert_equal(1, popup_getpos(winid).visible)
1544 call feedkeys("eli\<Esc>", 'xt')
1545 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001546 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001547
1548 bwipe!
1549 call test_override('ALL', 0)
1550endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001551
1552func Test_notifications()
1553 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02001554 throw 'Skipped: timer feature not supported'
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001555 endif
1556 if !CanRunVimInTerminal()
1557 throw 'Skipped: cannot make screendumps'
1558 endif
1559
1560 call writefile([
1561 \ "call setline(1, range(1, 20))",
1562 \ "hi Notification ctermbg=lightblue",
1563 \ "call popup_notification('first notification', {})",
1564 \], 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001565 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001566 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1567
1568 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001569 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1570 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001571 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1572
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001573 " clean up
1574 call StopVimInTerminal(buf)
1575 call delete('XtestNotifications')
1576endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001577
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001578func Test_popup_scrollbar()
1579 if !CanRunVimInTerminal()
1580 throw 'Skipped: cannot make screendumps'
1581 endif
1582
1583 let lines =<< trim END
1584 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001585 hi ScrollThumb ctermbg=blue
1586 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001587 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001588 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001589 \ minwidth: 8,
1590 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001591 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001592 func ScrollUp()
1593 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1594 endfunc
1595 func ScrollDown()
1596 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1597 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001598 func ClickTop()
1599 call feedkeys("\<F4>\<LeftMouse>", "xt")
1600 endfunc
1601 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001602 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001603 call feedkeys("\<F5>\<LeftMouse>", "xt")
1604 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001605 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001606 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1607 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001608 END
1609 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001610 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001611 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1612
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001613 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001614 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001615 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1616
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001617 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001618 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001619 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1620
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001621 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001622 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001623 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1624
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001625 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb'})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001626 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1627 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1628
1629 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1630 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1631
1632 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001633 " wait a bit, otherwise it fails sometimes (double click recognized?)
1634 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001635 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1636 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1637
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001638 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1639 sleep 100m
1640 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1641 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1642
1643 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1644 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1645
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001646 " clean up
1647 call StopVimInTerminal(buf)
1648 call delete('XtestPopupScroll')
1649endfunc
1650
Bram Moolenaar437a7462019-07-05 20:17:22 +02001651func Test_popup_fitting_scrollbar()
1652 " this was causing a crash, divide by zero
1653 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001654 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001655 \ scrollbar: 1,
1656 \ maxwidth: 10,
1657 \ maxheight: 5,
1658 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001659 redraw
1660 call popup_clear()
1661endfunc
1662
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001663func Test_popup_settext()
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001664 if !CanRunVimInTerminal()
1665 throw 'Skipped: cannot make screendumps'
1666 endif
1667
1668 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001669 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001670 let p = popup_create('test', opts)
1671 call popup_settext(p, 'this is a text')
1672 END
1673
1674 call writefile( lines, 'XtestPopupSetText' )
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001675 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001676 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1677
1678 " Setting to empty string clears it
1679 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1680 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1681
1682 " Setting a list
1683 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1684 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1685
1686 " Shrinking with a list
1687 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1688 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1689
1690 " Growing with a list
1691 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1692 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1693
1694 " Empty list clears
1695 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1696 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1697
1698 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001699 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001700 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1701
1702 " clean up
1703 call StopVimInTerminal(buf)
1704 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001705endfunc
1706
1707func Test_popup_hidden()
1708 new
1709
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001710 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001711 redraw
1712 call assert_equal(0, popup_getpos(winid).visible)
1713 call popup_close(winid)
1714
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001715 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001716 redraw
1717 call assert_equal(0, popup_getpos(winid).visible)
1718 call popup_close(winid)
1719
1720 func QuitCallback(id, res)
1721 let s:cb_winid = a:id
1722 let s:cb_res = a:res
1723 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001724 let winid = popup_dialog('make a choice', #{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001725 \ filter: 'popup_filter_yesno',
1726 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001727 \ })
1728 redraw
1729 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001730 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1731 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001732 exe "normal anot used by filter\<Esc>"
1733 call assert_equal('not used by filter', getline(1))
1734
1735 call popup_show(winid)
1736 call feedkeys('y', "xt")
1737 call assert_equal(1, s:cb_res)
1738
1739 bwipe!
1740 delfunc QuitCallback
1741endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001742
1743" Test options not checked elsewhere
1744func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001745 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001746 let options = popup_getoptions(winid)
1747 call assert_equal(1, options.wrap)
1748 call assert_equal(0, options.drag)
1749 call assert_equal('Beautiful', options.highlight)
1750
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001751 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001752 let options = popup_getoptions(winid)
1753 call assert_equal(0, options.wrap)
1754 call assert_equal(1, options.drag)
1755 call assert_equal('Another', options.highlight)
1756
1757 call popup_close(winid)
1758endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001759
1760func Test_popupwin_garbage_collect()
1761 func MyPopupFilter(x, winid, c)
1762 " NOP
1763 endfunc
1764
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001765 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001766 call test_garbagecollect_now()
1767 redraw
1768 " Must not crach caused by invalid memory access
1769 call feedkeys('j', 'xt')
1770 call assert_true(v:true)
1771
1772 call popup_close(winid)
1773 delfunc MyPopupFilter
1774endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001775
1776func Test_popupwin_with_buffer()
1777 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1778 let buf = bufadd('XsomeFile')
1779 call assert_equal(0, bufloaded(buf))
1780 let winid = popup_create(buf, {})
1781 call assert_notequal(0, winid)
1782 let pos = popup_getpos(winid)
1783 call assert_equal(2, pos.height)
1784 call assert_equal(1, bufloaded(buf))
1785 call popup_close(winid)
1786 call assert_equal({}, popup_getpos(winid))
1787 call assert_equal(1, bufloaded(buf))
1788 exe 'bwipe! ' .. buf
Bram Moolenaar7866b872019-07-01 22:21:01 +02001789
1790 edit test_popupwin.vim
1791 let winid = popup_create(bufnr(''), {})
1792 redraw
1793 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001794 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001795endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02001796
1797func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001798 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001799 \ maxwidth: 40,
1800 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02001801 \ })
1802 for top in range(1, 20)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001803 call popup_setoptions(winid, #{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02001804 redraw
1805 call assert_equal(19, popup_getpos(winid).width)
1806 endfor
1807 call popup_clear()
1808endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02001809
1810func Test_popupwin_buf_close()
1811 let buf = bufadd('Xtestbuf')
1812 call bufload(buf)
1813 call setbufline(buf, 1, ['just', 'some', 'lines'])
1814 let winid = popup_create(buf, {})
1815 redraw
1816 call assert_equal(3, popup_getpos(winid).height)
1817 let bufinfo = getbufinfo(buf)[0]
1818 call assert_equal(1, bufinfo.changed)
1819 call assert_equal(0, bufinfo.hidden)
1820 call assert_equal(0, bufinfo.listed)
1821 call assert_equal(1, bufinfo.loaded)
1822 call assert_equal([], bufinfo.windows)
1823 call assert_equal([winid], bufinfo.popups)
1824
1825 call popup_close(winid)
1826 call assert_equal({}, popup_getpos(winid))
1827 let bufinfo = getbufinfo(buf)[0]
1828 call assert_equal(1, bufinfo.changed)
1829 call assert_equal(1, bufinfo.hidden)
1830 call assert_equal(0, bufinfo.listed)
1831 call assert_equal(1, bufinfo.loaded)
1832 call assert_equal([], bufinfo.windows)
1833 call assert_equal([], bufinfo.popups)
1834 exe 'bwipe! ' .. buf
1835endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02001836
1837func Test_popup_menu_with_maxwidth()
1838 if !CanRunVimInTerminal()
1839 throw 'Skipped: cannot make screendumps'
1840 endif
1841
1842 let lines =<< trim END
1843 call setline(1, range(1, 10))
1844 hi ScrollThumb ctermbg=blue
1845 hi ScrollBar ctermbg=red
1846 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001847 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001848 \ maxwidth: 10,
1849 \ maxheight: 3,
1850 \ pos : 'topleft',
1851 \ col : a:col,
1852 \ line : a:line,
1853 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02001854 \ })
1855 endfunc
1856 call PopupMenu(['x'], 1, 1)
1857 call PopupMenu(['123456789|'], 1, 16)
1858 call PopupMenu(['123456789|' .. ' '], 7, 1)
1859 call PopupMenu([repeat('123456789|', 100)], 7, 16)
1860 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
1861 END
1862 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001863 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02001864 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
1865
1866 " close the menu popupwin.
1867 call term_sendkeys(buf, " ")
1868 call term_sendkeys(buf, " ")
1869 call term_sendkeys(buf, " ")
1870 call term_sendkeys(buf, " ")
1871 call term_sendkeys(buf, " ")
1872
1873 " clean up
1874 call StopVimInTerminal(buf)
1875 call delete('XtestPopupMenuMaxWidth')
1876endfunc
1877
Bram Moolenaara901a372019-07-13 16:38:50 +02001878func Test_popup_menu_with_scrollbar()
1879 if !CanRunVimInTerminal()
1880 throw 'Skipped: cannot make screendumps'
1881 endif
1882
1883 let lines =<< trim END
1884 call setline(1, range(1, 20))
1885 hi ScrollThumb ctermbg=blue
1886 hi ScrollBar ctermbg=red
1887 call popup_menu(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001888 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001889 \ minwidth: 8,
1890 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02001891 \ })
1892 END
1893 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001894 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02001895
1896 call term_sendkeys(buf, "j")
1897 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
1898
1899 call term_sendkeys(buf, "jjj")
1900 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
1901
1902 " if the cursor is the bottom line, it stays at the bottom line.
1903 call term_sendkeys(buf, repeat("j", 20))
1904 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
1905
1906 call term_sendkeys(buf, "kk")
1907 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
1908
1909 call term_sendkeys(buf, "k")
1910 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
1911
1912 " if the cursor is in the top line, it stays in the top line.
1913 call term_sendkeys(buf, repeat("k", 20))
1914 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
1915
1916 " close the menu popupwin.
1917 call term_sendkeys(buf, " ")
1918
1919 " clean up
1920 call StopVimInTerminal(buf)
1921 call delete('XtestPopupMenuScroll')
1922endfunc
1923
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001924func Test_popup_menu_filter()
1925 if !CanRunVimInTerminal()
1926 throw 'Skipped: cannot make screendumps'
1927 endif
1928
1929 let lines =<< trim END
1930 function! MyFilter(winid, key) abort
1931 if a:key == "0"
1932 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
1933 return 1
1934 endif
1935 if a:key == "G"
1936 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
1937 return 1
1938 endif
1939 if a:key == "j"
1940 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
1941 return 1
1942 endif
1943 if a:key == "k"
1944 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
1945 return 1
1946 endif
1947 if a:key == 'x'
1948 call popup_close(a:winid)
1949 return 1
1950 endif
1951 return 0
1952 endfunction
1953 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
1954 \ maxheight : 3,
1955 \ filter : 'MyFilter'
1956 \ })
1957 END
1958 call writefile(lines, 'XtestPopupMenuFilter')
1959 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
1960
1961 call term_sendkeys(buf, "j")
1962 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
1963
1964 call term_sendkeys(buf, "k")
1965 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
1966
1967 call term_sendkeys(buf, "G")
1968 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
1969
1970 call term_sendkeys(buf, "0")
1971 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
1972
1973 call term_sendkeys(buf, "x")
1974
1975 " clean up
1976 call StopVimInTerminal(buf)
1977 call delete('XtestPopupMenuFilter')
1978endfunc
1979
1980func Test_popup_cursorline()
1981 if !CanRunVimInTerminal()
1982 throw 'Skipped: cannot make screendumps'
1983 endif
1984
1985 let winid = popup_create('some text', {})
1986 call assert_equal(0, popup_getoptions(winid).cursorline)
1987 call popup_close(winid)
1988
1989 let winid = popup_create('some text', #{ cursorline: 1, })
1990 call assert_equal(1, popup_getoptions(winid).cursorline)
1991 call popup_close(winid)
1992
1993 let winid = popup_create('some text', #{ cursorline: 0, })
1994 call assert_equal(0, popup_getoptions(winid).cursorline)
1995 call popup_close(winid)
1996
1997 let winid = popup_menu('some text', {})
1998 call assert_equal(1, popup_getoptions(winid).cursorline)
1999 call popup_close(winid)
2000
2001 let winid = popup_menu('some text', #{ cursorline: 1, })
2002 call assert_equal(1, popup_getoptions(winid).cursorline)
2003 call popup_close(winid)
2004
2005 let winid = popup_menu('some text', #{ cursorline: 0, })
2006 call assert_equal(0, popup_getoptions(winid).cursorline)
2007 call popup_close(winid)
2008
2009 " ---------
2010 " Pattern 1
2011 " ---------
2012 let lines =<< trim END
2013 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2014 END
2015 call writefile(lines, 'XtestPopupCursorLine')
2016 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2017 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2018 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2019 call StopVimInTerminal(buf)
2020
2021 " ---------
2022 " Pattern 2
2023 " ---------
2024 let lines =<< trim END
2025 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2026 END
2027 call writefile(lines, 'XtestPopupCursorLine')
2028 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2029 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2030 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2031 call StopVimInTerminal(buf)
2032
2033 " ---------
2034 " Pattern 3
2035 " ---------
2036 let lines =<< trim END
2037 function! MyFilter(winid, key) abort
2038 if a:key == "j"
2039 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2040 return 1
2041 endif
2042 if a:key == 'x'
2043 call popup_close(a:winid)
2044 return 1
2045 endif
2046 return 0
2047 endfunction
2048 call popup_menu(['111', '222', '333'], #{
2049 \ cursorline : 0,
2050 \ maxheight : 2,
2051 \ filter : 'MyFilter',
2052 \ })
2053 END
2054 call writefile(lines, 'XtestPopupCursorLine')
2055 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2056 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2057 call term_sendkeys(buf, "j")
2058 call term_sendkeys(buf, "j")
2059 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2060 call term_sendkeys(buf, "x")
2061 call StopVimInTerminal(buf)
2062
2063 " ---------
2064 " Pattern 4
2065 " ---------
2066 let lines =<< trim END
2067 function! MyFilter(winid, key) abort
2068 if a:key == "j"
2069 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2070 return 1
2071 endif
2072 if a:key == 'x'
2073 call popup_close(a:winid)
2074 return 1
2075 endif
2076 return 0
2077 endfunction
2078 call popup_menu(['111', '222', '333'], #{
2079 \ cursorline : 1,
2080 \ maxheight : 2,
2081 \ filter : 'MyFilter',
2082 \ })
2083 END
2084 call writefile(lines, 'XtestPopupCursorLine')
2085 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2086 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2087 call term_sendkeys(buf, "j")
2088 call term_sendkeys(buf, "j")
2089 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2090 call term_sendkeys(buf, "x")
2091 call StopVimInTerminal(buf)
2092
2093 call delete('XtestPopupCursorLine')
2094endfunc
2095
Bram Moolenaar017c2692019-07-13 14:17:51 +02002096" vim: shiftwidth=2 sts=2 expandtab