blob: 11e4323b9bf29c54b5a44b05c2e0d35804717ab6 [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 Moolenaarf914a332019-07-20 15:09:56 +02001218func Test_popup_menu_narrow()
1219 if !CanRunVimInTerminal()
1220 throw 'Skipped: cannot make screendumps'
1221 endif
1222
1223 let lines =<< trim END
1224 call setline(1, range(1, 20))
1225 hi PopupSelected ctermbg=green
1226 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1227 func MenuDone(id, res)
1228 echomsg "selected " .. a:res
1229 endfunc
1230 END
1231 call writefile(lines, 'XtestPopupNarrowMenu')
1232 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1233 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1234
1235 " clean up
1236 call term_sendkeys(buf, "x")
1237 call StopVimInTerminal(buf)
1238 call delete('XtestPopupNarrowMenu')
1239endfunc
1240
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001241func Test_popup_title()
1242 if !CanRunVimInTerminal()
1243 throw 'Skipped: cannot make screendumps'
1244 endif
1245
1246 " Create a popup without title or border, a line of padding will be added to
1247 " put the title on.
1248 let lines =<< trim END
1249 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001250 call popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001251 END
1252 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001253 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001254 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1255
1256 " clean up
1257 call StopVimInTerminal(buf)
1258 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001259
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001260 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001261 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001262 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001263 call assert_equal('Another Title', popup_getoptions(winid).title)
1264
1265 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001266endfunc
1267
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001268func Test_popup_close_callback()
1269 func PopupDone(id, result)
1270 let g:result = a:result
1271 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001272 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001273 redraw
1274 call popup_close(winid, 'done')
1275 call assert_equal('done', g:result)
1276endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001277
1278func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001279 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001280 redraw
1281 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001282 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001283 call assert_equal(5, pos.height)
1284
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001285 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001286 redraw
1287 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001288 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001289 call assert_equal(3, pos.height)
1290endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001291
1292func Test_popup_never_behind()
1293 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001294 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +02001295 endif
1296 " +-----------------------------+
1297 " | | |
1298 " | | |
1299 " | | |
1300 " | line1 |
1301 " |------------line2------------|
1302 " | line3 |
1303 " | line4 |
1304 " | |
1305 " | |
1306 " +-----------------------------+
1307 let lines =<< trim END
1308 only
1309 split
1310 vsplit
1311 let info_window1 = getwininfo()[0]
1312 let line = info_window1['height']
1313 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001314 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001315 \ line : line,
1316 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001317 \ })
1318 END
1319 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001320 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001321 call term_sendkeys(buf, "\<C-W>w")
1322 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1323
1324 " clean up
1325 call StopVimInTerminal(buf)
1326 call delete('XtestPopupBehind')
1327endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001328
1329func s:VerifyPosition( p, msg, line, col, width, height )
1330 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
1331 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
1332 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
1333 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
1334endfunc
1335
1336func Test_popup_position_adjust()
1337 " Anything placed past 2 cells from of the right of the screen is moved to the
1338 " left.
1339 "
1340 " When wrapping is disabled, we also shift to the left to display on the
1341 " screen, unless fixed is set.
1342
1343 " Entries for cases which don't vary based on wrapping.
1344 " Format is per tests described below
1345 let both_wrap_tests = [
1346 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
1347 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
1348 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
1349 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
1350 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
1351 \
1352 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
1353 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
1354 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
1355 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
1356 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
1357 \
1358 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1359 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1360 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1361 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1362 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
1363 \ ]
1364
1365 " these test groups are dicts with:
1366 " - comment: something to identify the group of tests by
1367 " - options: dict of options to merge with the row/col in tests
1368 " - tests: list of cases. Each one is a list with elements:
1369 " - text
1370 " - row
1371 " - col
1372 " - expected row
1373 " - expected col
1374 " - expected width
1375 " - expected height
1376 let tests = [
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001377 \ #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001378 \ comment: 'left-aligned with wrapping',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001379 \ options: #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001380 \ wrap: 1,
1381 \ pos: 'botleft',
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001382 \ },
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001383 \ tests: both_wrap_tests + [
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001384 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
1385 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
1386 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
1387 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
1388 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1389 \ ],
1390 \ },
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001391 \ #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001392 \ comment: 'left aligned without wrapping',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001393 \ options: #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001394 \ wrap: 0,
1395 \ pos: 'botleft',
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001396 \ },
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001397 \ tests: both_wrap_tests + [
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001398 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
1399 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
1400 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
1401 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
1402 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1403 \ ],
1404 \ },
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001405 \ #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001406 \ comment: 'left aligned with fixed position',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001407 \ options: #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001408 \ wrap: 0,
1409 \ fixed: 1,
1410 \ pos: 'botleft',
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001411 \ },
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001412 \ tests: both_wrap_tests + [
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001413 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1414 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1415 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1416 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1417 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1418 \ ],
1419 \ },
1420 \ ]
1421
1422 for test_group in tests
1423 for test in test_group.tests
1424 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001425 let options = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001426 \ line: line,
1427 \ col: col,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001428 \ }
1429 call extend( options, test_group.options )
1430
1431 let p = popup_create( text, options )
1432
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001433 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001434 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1435 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001436 endfor
1437 endfor
1438
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001439 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001440 %bwipe!
1441endfunc
1442
Bram Moolenaar3397f742019-06-02 18:40:06 +02001443func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001444 " width of screen
1445 let X = join(map(range(&columns), {->'X'}), '')
1446
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001447 let p = popup_create( X, #{line: 1, col: 1, wrap: 0})
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001448 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
1449
1450 redraw
1451 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1452 call assert_equal(X, line)
1453
1454 call popup_close( p )
1455 redraw
1456
1457 " Same if placed on the right hand side
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001458 let p = popup_create( X, #{line: 1, col: &columns, wrap: 0})
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001459 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
1460
1461 redraw
1462 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1463 call assert_equal(X, line)
1464
1465 call popup_close( p )
1466 redraw
1467
1468 " Extend so > window width
1469 let X .= 'x'
1470
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001471 let p = popup_create( X, #{line: 1, col: 1, wrap: 0})
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001472 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
1473
1474 redraw
1475 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1476 call assert_equal(X[ : -2 ], line)
1477
1478 call popup_close( p )
1479 redraw
1480
1481 " Shifted then truncated (the x is not visible)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001482 let p = popup_create( X, #{line: 1, col: &columns - 3, wrap: 0})
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001483 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
1484
1485 redraw
1486 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1487 call assert_equal(X[ : -2 ], line)
1488
1489 call popup_close( p )
1490 redraw
1491
1492 " Not shifted, just truncated
1493 let p = popup_create( X,
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001494 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001495 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
1496
1497 redraw
1498 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1499 let e_line = ' ' . X[ 1 : -2 ]
1500 call assert_equal(e_line, line)
1501
1502 call popup_close( p )
1503 redraw
1504
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001505 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001506 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001507endfunc
1508
1509func Test_popup_moved()
1510 new
1511 call test_override('char_avail', 1)
1512 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1513
1514 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001515 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001516 redraw
1517 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001518 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001519 " trigger the check for last_cursormoved by going into insert mode
1520 call feedkeys("li\<Esc>", 'xt')
1521 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001522 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001523
1524 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001525 let winid = popup_atcursor('text', #{moved: 'word'})
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([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001529 call feedkeys("hi\<Esc>", 'xt')
1530 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001531 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001532
1533 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001534 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001535 redraw
1536 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001537 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001538 call feedkeys("li\<Esc>", 'xt')
1539 call assert_equal(1, popup_getpos(winid).visible)
1540 call feedkeys("ei\<Esc>", 'xt')
1541 call assert_equal(1, popup_getpos(winid).visible)
1542 call feedkeys("eli\<Esc>", 'xt')
1543 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001544 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001545
Bram Moolenaar17627312019-06-02 19:53:44 +02001546 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001547 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001548 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001549 redraw
1550 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001551 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001552 call feedkeys("eli\<Esc>", 'xt')
1553 call assert_equal(1, popup_getpos(winid).visible)
1554 call feedkeys("wi\<Esc>", 'xt')
1555 call assert_equal(1, popup_getpos(winid).visible)
1556 call feedkeys("Eli\<Esc>", 'xt')
1557 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001558 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001559
1560 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001561 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001562 redraw
1563 call assert_equal(1, popup_getpos(winid).visible)
1564 call feedkeys("eli\<Esc>", 'xt')
1565 call feedkeys("ei\<Esc>", 'xt')
1566 call assert_equal(1, popup_getpos(winid).visible)
1567 call feedkeys("eli\<Esc>", 'xt')
1568 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001569 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001570
1571 bwipe!
1572 call test_override('ALL', 0)
1573endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001574
1575func Test_notifications()
1576 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02001577 throw 'Skipped: timer feature not supported'
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001578 endif
1579 if !CanRunVimInTerminal()
1580 throw 'Skipped: cannot make screendumps'
1581 endif
1582
1583 call writefile([
1584 \ "call setline(1, range(1, 20))",
1585 \ "hi Notification ctermbg=lightblue",
1586 \ "call popup_notification('first notification', {})",
1587 \], 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001588 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001589 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1590
1591 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001592 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1593 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001594 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1595
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001596 " clean up
1597 call StopVimInTerminal(buf)
1598 call delete('XtestNotifications')
1599endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001600
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001601func Test_popup_scrollbar()
1602 if !CanRunVimInTerminal()
1603 throw 'Skipped: cannot make screendumps'
1604 endif
1605
1606 let lines =<< trim END
1607 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001608 hi ScrollThumb ctermbg=blue
1609 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001610 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001611 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001612 \ minwidth: 8,
1613 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001614 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001615 func ScrollUp()
1616 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1617 endfunc
1618 func ScrollDown()
1619 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1620 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001621 func ClickTop()
1622 call feedkeys("\<F4>\<LeftMouse>", "xt")
1623 endfunc
1624 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001625 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001626 call feedkeys("\<F5>\<LeftMouse>", "xt")
1627 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001628 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001629 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1630 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001631 END
1632 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001633 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001634 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1635
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001636 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001637 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001638 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1639
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001640 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001641 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001642 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1643
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001644 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001645 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001646 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1647
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001648 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb'})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001649 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1650 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1651
1652 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1653 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1654
1655 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001656 " wait a bit, otherwise it fails sometimes (double click recognized?)
1657 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001658 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1659 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1660
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001661 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1662 sleep 100m
1663 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1664 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1665
1666 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1667 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1668
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001669 " clean up
1670 call StopVimInTerminal(buf)
1671 call delete('XtestPopupScroll')
1672endfunc
1673
Bram Moolenaar437a7462019-07-05 20:17:22 +02001674func Test_popup_fitting_scrollbar()
1675 " this was causing a crash, divide by zero
1676 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001677 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001678 \ scrollbar: 1,
1679 \ maxwidth: 10,
1680 \ maxheight: 5,
1681 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001682 redraw
1683 call popup_clear()
1684endfunc
1685
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001686func Test_popup_settext()
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001687 if !CanRunVimInTerminal()
1688 throw 'Skipped: cannot make screendumps'
1689 endif
1690
1691 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001692 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001693 let p = popup_create('test', opts)
1694 call popup_settext(p, 'this is a text')
1695 END
1696
1697 call writefile( lines, 'XtestPopupSetText' )
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001698 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001699 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1700
1701 " Setting to empty string clears it
1702 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1703 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1704
1705 " Setting a list
1706 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1707 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1708
1709 " Shrinking with a list
1710 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1711 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1712
1713 " Growing with a list
1714 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1715 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1716
1717 " Empty list clears
1718 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1719 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1720
1721 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001722 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001723 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1724
1725 " clean up
1726 call StopVimInTerminal(buf)
1727 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001728endfunc
1729
1730func Test_popup_hidden()
1731 new
1732
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001733 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001734 redraw
1735 call assert_equal(0, popup_getpos(winid).visible)
1736 call popup_close(winid)
1737
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001738 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001739 redraw
1740 call assert_equal(0, popup_getpos(winid).visible)
1741 call popup_close(winid)
1742
1743 func QuitCallback(id, res)
1744 let s:cb_winid = a:id
1745 let s:cb_res = a:res
1746 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001747 let winid = popup_dialog('make a choice', #{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001748 \ filter: 'popup_filter_yesno',
1749 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001750 \ })
1751 redraw
1752 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001753 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1754 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001755 exe "normal anot used by filter\<Esc>"
1756 call assert_equal('not used by filter', getline(1))
1757
1758 call popup_show(winid)
1759 call feedkeys('y', "xt")
1760 call assert_equal(1, s:cb_res)
1761
1762 bwipe!
1763 delfunc QuitCallback
1764endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001765
1766" Test options not checked elsewhere
1767func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001768 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001769 let options = popup_getoptions(winid)
1770 call assert_equal(1, options.wrap)
1771 call assert_equal(0, options.drag)
1772 call assert_equal('Beautiful', options.highlight)
1773
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001774 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001775 let options = popup_getoptions(winid)
1776 call assert_equal(0, options.wrap)
1777 call assert_equal(1, options.drag)
1778 call assert_equal('Another', options.highlight)
1779
1780 call popup_close(winid)
1781endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001782
1783func Test_popupwin_garbage_collect()
1784 func MyPopupFilter(x, winid, c)
1785 " NOP
1786 endfunc
1787
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001788 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001789 call test_garbagecollect_now()
1790 redraw
1791 " Must not crach caused by invalid memory access
1792 call feedkeys('j', 'xt')
1793 call assert_true(v:true)
1794
1795 call popup_close(winid)
1796 delfunc MyPopupFilter
1797endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001798
1799func Test_popupwin_with_buffer()
1800 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1801 let buf = bufadd('XsomeFile')
1802 call assert_equal(0, bufloaded(buf))
1803 let winid = popup_create(buf, {})
1804 call assert_notequal(0, winid)
1805 let pos = popup_getpos(winid)
1806 call assert_equal(2, pos.height)
1807 call assert_equal(1, bufloaded(buf))
1808 call popup_close(winid)
1809 call assert_equal({}, popup_getpos(winid))
1810 call assert_equal(1, bufloaded(buf))
1811 exe 'bwipe! ' .. buf
Bram Moolenaar7866b872019-07-01 22:21:01 +02001812
1813 edit test_popupwin.vim
1814 let winid = popup_create(bufnr(''), {})
1815 redraw
1816 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001817 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001818endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02001819
1820func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001821 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001822 \ maxwidth: 40,
1823 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02001824 \ })
1825 for top in range(1, 20)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001826 call popup_setoptions(winid, #{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02001827 redraw
1828 call assert_equal(19, popup_getpos(winid).width)
1829 endfor
1830 call popup_clear()
1831endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02001832
1833func Test_popupwin_buf_close()
1834 let buf = bufadd('Xtestbuf')
1835 call bufload(buf)
1836 call setbufline(buf, 1, ['just', 'some', 'lines'])
1837 let winid = popup_create(buf, {})
1838 redraw
1839 call assert_equal(3, popup_getpos(winid).height)
1840 let bufinfo = getbufinfo(buf)[0]
1841 call assert_equal(1, bufinfo.changed)
1842 call assert_equal(0, bufinfo.hidden)
1843 call assert_equal(0, bufinfo.listed)
1844 call assert_equal(1, bufinfo.loaded)
1845 call assert_equal([], bufinfo.windows)
1846 call assert_equal([winid], bufinfo.popups)
1847
1848 call popup_close(winid)
1849 call assert_equal({}, popup_getpos(winid))
1850 let bufinfo = getbufinfo(buf)[0]
1851 call assert_equal(1, bufinfo.changed)
1852 call assert_equal(1, bufinfo.hidden)
1853 call assert_equal(0, bufinfo.listed)
1854 call assert_equal(1, bufinfo.loaded)
1855 call assert_equal([], bufinfo.windows)
1856 call assert_equal([], bufinfo.popups)
1857 exe 'bwipe! ' .. buf
1858endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02001859
1860func Test_popup_menu_with_maxwidth()
1861 if !CanRunVimInTerminal()
1862 throw 'Skipped: cannot make screendumps'
1863 endif
1864
1865 let lines =<< trim END
1866 call setline(1, range(1, 10))
1867 hi ScrollThumb ctermbg=blue
1868 hi ScrollBar ctermbg=red
1869 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001870 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001871 \ maxwidth: 10,
1872 \ maxheight: 3,
1873 \ pos : 'topleft',
1874 \ col : a:col,
1875 \ line : a:line,
1876 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02001877 \ })
1878 endfunc
1879 call PopupMenu(['x'], 1, 1)
1880 call PopupMenu(['123456789|'], 1, 16)
1881 call PopupMenu(['123456789|' .. ' '], 7, 1)
1882 call PopupMenu([repeat('123456789|', 100)], 7, 16)
1883 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
1884 END
1885 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001886 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02001887 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
1888
1889 " close the menu popupwin.
1890 call term_sendkeys(buf, " ")
1891 call term_sendkeys(buf, " ")
1892 call term_sendkeys(buf, " ")
1893 call term_sendkeys(buf, " ")
1894 call term_sendkeys(buf, " ")
1895
1896 " clean up
1897 call StopVimInTerminal(buf)
1898 call delete('XtestPopupMenuMaxWidth')
1899endfunc
1900
Bram Moolenaara901a372019-07-13 16:38:50 +02001901func Test_popup_menu_with_scrollbar()
1902 if !CanRunVimInTerminal()
1903 throw 'Skipped: cannot make screendumps'
1904 endif
1905
1906 let lines =<< trim END
1907 call setline(1, range(1, 20))
1908 hi ScrollThumb ctermbg=blue
1909 hi ScrollBar ctermbg=red
1910 call popup_menu(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001911 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001912 \ minwidth: 8,
1913 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02001914 \ })
1915 END
1916 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001917 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02001918
1919 call term_sendkeys(buf, "j")
1920 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
1921
1922 call term_sendkeys(buf, "jjj")
1923 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
1924
1925 " if the cursor is the bottom line, it stays at the bottom line.
1926 call term_sendkeys(buf, repeat("j", 20))
1927 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
1928
1929 call term_sendkeys(buf, "kk")
1930 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
1931
1932 call term_sendkeys(buf, "k")
1933 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
1934
1935 " if the cursor is in the top line, it stays in the top line.
1936 call term_sendkeys(buf, repeat("k", 20))
1937 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
1938
1939 " close the menu popupwin.
1940 call term_sendkeys(buf, " ")
1941
1942 " clean up
1943 call StopVimInTerminal(buf)
1944 call delete('XtestPopupMenuScroll')
1945endfunc
1946
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001947func Test_popup_menu_filter()
1948 if !CanRunVimInTerminal()
1949 throw 'Skipped: cannot make screendumps'
1950 endif
1951
1952 let lines =<< trim END
1953 function! MyFilter(winid, key) abort
1954 if a:key == "0"
1955 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
1956 return 1
1957 endif
1958 if a:key == "G"
1959 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
1960 return 1
1961 endif
1962 if a:key == "j"
1963 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
1964 return 1
1965 endif
1966 if a:key == "k"
1967 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
1968 return 1
1969 endif
1970 if a:key == 'x'
1971 call popup_close(a:winid)
1972 return 1
1973 endif
1974 return 0
1975 endfunction
1976 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
1977 \ maxheight : 3,
1978 \ filter : 'MyFilter'
1979 \ })
1980 END
1981 call writefile(lines, 'XtestPopupMenuFilter')
1982 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
1983
1984 call term_sendkeys(buf, "j")
1985 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
1986
1987 call term_sendkeys(buf, "k")
1988 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
1989
1990 call term_sendkeys(buf, "G")
1991 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
1992
1993 call term_sendkeys(buf, "0")
1994 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
1995
1996 call term_sendkeys(buf, "x")
1997
1998 " clean up
1999 call StopVimInTerminal(buf)
2000 call delete('XtestPopupMenuFilter')
2001endfunc
2002
2003func Test_popup_cursorline()
2004 if !CanRunVimInTerminal()
2005 throw 'Skipped: cannot make screendumps'
2006 endif
2007
2008 let winid = popup_create('some text', {})
2009 call assert_equal(0, popup_getoptions(winid).cursorline)
2010 call popup_close(winid)
2011
2012 let winid = popup_create('some text', #{ cursorline: 1, })
2013 call assert_equal(1, popup_getoptions(winid).cursorline)
2014 call popup_close(winid)
2015
2016 let winid = popup_create('some text', #{ cursorline: 0, })
2017 call assert_equal(0, popup_getoptions(winid).cursorline)
2018 call popup_close(winid)
2019
2020 let winid = popup_menu('some text', {})
2021 call assert_equal(1, popup_getoptions(winid).cursorline)
2022 call popup_close(winid)
2023
2024 let winid = popup_menu('some text', #{ cursorline: 1, })
2025 call assert_equal(1, popup_getoptions(winid).cursorline)
2026 call popup_close(winid)
2027
2028 let winid = popup_menu('some text', #{ cursorline: 0, })
2029 call assert_equal(0, popup_getoptions(winid).cursorline)
2030 call popup_close(winid)
2031
2032 " ---------
2033 " Pattern 1
2034 " ---------
2035 let lines =<< trim END
2036 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2037 END
2038 call writefile(lines, 'XtestPopupCursorLine')
2039 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2040 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2041 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2042 call StopVimInTerminal(buf)
2043
2044 " ---------
2045 " Pattern 2
2046 " ---------
2047 let lines =<< trim END
2048 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2049 END
2050 call writefile(lines, 'XtestPopupCursorLine')
2051 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2052 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2053 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2054 call StopVimInTerminal(buf)
2055
2056 " ---------
2057 " Pattern 3
2058 " ---------
2059 let lines =<< trim END
2060 function! MyFilter(winid, key) abort
2061 if a:key == "j"
2062 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2063 return 1
2064 endif
2065 if a:key == 'x'
2066 call popup_close(a:winid)
2067 return 1
2068 endif
2069 return 0
2070 endfunction
2071 call popup_menu(['111', '222', '333'], #{
2072 \ cursorline : 0,
2073 \ maxheight : 2,
2074 \ filter : 'MyFilter',
2075 \ })
2076 END
2077 call writefile(lines, 'XtestPopupCursorLine')
2078 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2079 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2080 call term_sendkeys(buf, "j")
2081 call term_sendkeys(buf, "j")
2082 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2083 call term_sendkeys(buf, "x")
2084 call StopVimInTerminal(buf)
2085
2086 " ---------
2087 " Pattern 4
2088 " ---------
2089 let lines =<< trim END
2090 function! MyFilter(winid, key) abort
2091 if a:key == "j"
2092 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2093 return 1
2094 endif
2095 if a:key == 'x'
2096 call popup_close(a:winid)
2097 return 1
2098 endif
2099 return 0
2100 endfunction
2101 call popup_menu(['111', '222', '333'], #{
2102 \ cursorline : 1,
2103 \ maxheight : 2,
2104 \ filter : 'MyFilter',
2105 \ })
2106 END
2107 call writefile(lines, 'XtestPopupCursorLine')
2108 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2109 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2110 call term_sendkeys(buf, "j")
2111 call term_sendkeys(buf, "j")
2112 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2113 call term_sendkeys(buf, "x")
2114 call StopVimInTerminal(buf)
2115
2116 call delete('XtestPopupCursorLine')
2117endfunc
2118
Bram Moolenaarf914a332019-07-20 15:09:56 +02002119func Test_previewpopup()
2120 if !CanRunVimInTerminal()
2121 throw 'Skipped: cannot make screendumps'
2122 endif
2123 call writefile([
2124 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2125 \ "another\tXtagfile\t/^this is another",
2126 \ "theword\tXtagfile\t/^theword"],
2127 \ 'Xtags')
2128 call writefile(range(1,20)
2129 \ + ['theword is here']
2130 \ + range(22, 27)
2131 \ + ['this is another place']
2132 \ + range(29, 40),
2133 \ "Xtagfile")
2134 let lines =<< trim END
2135 set tags=Xtags
2136 call setline(1, [
2137 \ 'one',
2138 \ 'two',
2139 \ 'three',
2140 \ 'four',
2141 \ 'five',
2142 \ 'six',
2143 \ 'seven',
2144 \ 'find theword somewhere',
2145 \ 'nine',
2146 \ 'this is another word'])
2147 set previewpopup=height:4,width:40
2148 END
2149 call writefile(lines, 'XtestPreviewPopup')
2150 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2151
2152 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2153 call term_sendkeys(buf, ":\<CR>")
2154 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2155
2156 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2157 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2158
2159 call StopVimInTerminal(buf)
2160 call delete('Xtags')
2161 call delete('Xtagfile')
2162 call delete('XtestPreviewPopup')
2163endfunc
2164
Bram Moolenaar017c2692019-07-13 14:17:51 +02002165" vim: shiftwidth=2 sts=2 expandtab