blob: 26d1c94ae10845bb3d6b3e785543470a513cde70 [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 Moolenaar4c6d9042019-07-16 22:04:02 +0200637 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200638 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200639
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200640 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200641 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200642 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
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"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200645 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200646 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
647 call popup_clear()
648 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
649 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200650endfunc
651
Bram Moolenaareea16992019-05-31 17:34:48 +0200652func Test_win_execute_closing_curwin()
653 split
654 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200655 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200656 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200657endfunc
658
659func Test_win_execute_not_allowed()
660 let winid = popup_create('some text', {})
661 call assert_fails('call win_execute(winid, "split")', 'E994:')
662 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
663 call assert_fails('call win_execute(winid, "close")', 'E994:')
664 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200665 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200666 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
667 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
668 call assert_fails('call win_execute(winid, "next")', 'E994:')
669 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
670 call assert_fails('call win_execute(winid, "buf")', 'E994:')
671 call assert_fails('call win_execute(winid, "edit")', 'E994:')
672 call assert_fails('call win_execute(winid, "enew")', 'E994:')
673 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
674 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
675 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
676 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200677 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200678endfunc
679
Bram Moolenaar402502d2019-05-30 22:07:36 +0200680func Test_popup_with_wrap()
681 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200682 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200683 endif
684 let lines =<< trim END
685 call setline(1, range(1, 100))
686 let winid = popup_create(
687 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200688 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200689 END
690 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200691 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200692 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
693
694 " clean up
695 call StopVimInTerminal(buf)
696 call delete('XtestPopup')
697endfunc
698
699func Test_popup_without_wrap()
700 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200701 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200702 endif
703 let lines =<< trim END
704 call setline(1, range(1, 100))
705 let winid = popup_create(
706 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200707 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200708 END
709 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200710 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200711 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
712
713 " clean up
714 call StopVimInTerminal(buf)
715 call delete('XtestPopup')
716endfunc
717
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200718func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200719 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +0200720 throw 'Skipped: timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200721 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200722 topleft vnew
723 call setline(1, 'hello')
724
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200725 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200726 \ line: 1,
727 \ col: 1,
728 \ minwidth: 20,
729 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200730 \})
731 redraw
732 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
733 call assert_equal('world', line)
734
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200735 call assert_equal(winid, popup_locate(1, 1))
736 call assert_equal(winid, popup_locate(1, 20))
737 call assert_equal(0, popup_locate(1, 21))
738 call assert_equal(0, popup_locate(2, 1))
739
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200740 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200741 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200742 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
743 call assert_equal('hello', line)
744
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200745 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200746 \ line: &lines,
747 \ col: 10,
748 \ minwidth: 20,
749 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200750 \})
751 redraw
752 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
753 call assert_match('.*on the command line.*', line)
754
755 sleep 700m
756 redraw
757 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
758 call assert_notmatch('.*on the command line.*', line)
759
760 bwipe!
761endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200762
763func Test_popup_hide()
764 topleft vnew
765 call setline(1, 'hello')
766
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200767 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200768 \ line: 1,
769 \ col: 1,
770 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200771 \})
772 redraw
773 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
774 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200775 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200776 " buffer is still listed and active
777 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200778
779 call popup_hide(winid)
780 redraw
781 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
782 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200783 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200784 " buffer is still listed but hidden
785 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200786
787 call popup_show(winid)
788 redraw
789 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
790 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200791 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200792
793
794 call popup_close(winid)
795 redraw
796 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
797 call assert_equal('hello', line)
798
799 " error is given for existing non-popup window
800 call assert_fails('call popup_hide(win_getid())', 'E993:')
801
802 " no error non-existing window
803 call popup_hide(1234234)
804 call popup_show(41234234)
805
806 bwipe!
807endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200808
809func Test_popup_move()
810 topleft vnew
811 call setline(1, 'hello')
812
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200813 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200814 \ line: 1,
815 \ col: 1,
816 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200817 \})
818 redraw
819 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
820 call assert_equal('world ', line)
821
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200822 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200823 redraw
824 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
825 call assert_equal('hello ', line)
826 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
827 call assert_equal('~world', line)
828
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200829 call popup_move(winid, #{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200830 redraw
831 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
832 call assert_equal('hworld', line)
833
834 call popup_close(winid)
835
836 bwipe!
837endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200838
Bram Moolenaar402502d2019-05-30 22:07:36 +0200839func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200840 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200841 \ line: 2,
842 \ col: 3,
843 \ minwidth: 10,
844 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200845 \})
846 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200847 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200848 call assert_equal(2, res.line)
849 call assert_equal(3, res.col)
850 call assert_equal(10, res.width)
851 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200852 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200853
854 call popup_close(winid)
855endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200856
857func Test_popup_width_longest()
858 let tests = [
859 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
860 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
861 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
862 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
863 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
864 \ ]
865
866 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200867 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200868 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200869 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200870 call assert_equal(test[1], position.width)
871 call popup_close(winid)
872 endfor
873endfunc
874
875func Test_popup_wraps()
876 let tests = [
877 \ ['nowrap', 6, 1],
878 \ ['a line that wraps once', 12, 2],
879 \ ['a line that wraps two times', 12, 3],
880 \ ]
881 for test in tests
882 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200883 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200884 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200885 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200886 call assert_equal(test[1], position.width)
887 call assert_equal(test[2], position.height)
888
889 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200890 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200891 endfor
892endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200893
894func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200895 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200896 \ line: 2,
897 \ col: 3,
898 \ minwidth: 10,
899 \ minheight: 11,
900 \ maxwidth: 20,
901 \ maxheight: 21,
902 \ zindex: 100,
903 \ time: 5000,
904 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200905 \})
906 redraw
907 let res = popup_getoptions(winid)
908 call assert_equal(2, res.line)
909 call assert_equal(3, res.col)
910 call assert_equal(10, res.minwidth)
911 call assert_equal(11, res.minheight)
912 call assert_equal(20, res.maxwidth)
913 call assert_equal(21, res.maxheight)
914 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200915 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200916 if has('timers')
917 call assert_equal(5000, res.time)
918 endif
919 call popup_close(winid)
920
921 let winid = popup_create('hello', {})
922 redraw
923 let res = popup_getoptions(winid)
924 call assert_equal(0, res.line)
925 call assert_equal(0, res.col)
926 call assert_equal(0, res.minwidth)
927 call assert_equal(0, res.minheight)
928 call assert_equal(0, res.maxwidth)
929 call assert_equal(0, res.maxheight)
930 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200931 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200932 if has('timers')
933 call assert_equal(0, res.time)
934 endif
935 call popup_close(winid)
936 call assert_equal({}, popup_getoptions(winid))
937endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200938
939func Test_popup_option_values()
940 new
941 " window-local
942 setlocal number
943 setlocal nowrap
944 " buffer-local
945 setlocal omnifunc=Something
946 " global/buffer-local
947 setlocal path=/there
948 " global/window-local
949 setlocal scrolloff=9
950
951 let winid = popup_create('hello', {})
952 call assert_equal(0, getwinvar(winid, '&number'))
953 call assert_equal(1, getwinvar(winid, '&wrap'))
954 call assert_equal('', getwinvar(winid, '&omnifunc'))
955 call assert_equal(&g:path, getwinvar(winid, '&path'))
956 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
957
958 call popup_close(winid)
959 bwipe
960endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200961
962func Test_popup_atcursor()
963 topleft vnew
964 call setline(1, [
965 \ 'xxxxxxxxxxxxxxxxx',
966 \ 'xxxxxxxxxxxxxxxxx',
967 \ 'xxxxxxxxxxxxxxxxx',
968 \])
969
970 call cursor(2, 2)
971 redraw
972 let winid = popup_atcursor('vim', {})
973 redraw
974 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
975 call assert_equal('xvimxxxxxxxxxxxxx', line)
976 call popup_close(winid)
977
978 call cursor(3, 4)
979 redraw
980 let winid = popup_atcursor('vim', {})
981 redraw
982 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
983 call assert_equal('xxxvimxxxxxxxxxxx', line)
984 call popup_close(winid)
985
986 call cursor(1, 1)
987 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200988 let winid = popup_create('vim', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200989 \ line: 'cursor+2',
990 \ col: 'cursor+1',
991 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200992 redraw
993 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
994 call assert_equal('xvimxxxxxxxxxxxxx', line)
995 call popup_close(winid)
996
997 call cursor(3, 3)
998 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200999 let winid = popup_create('vim', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001000 \ line: 'cursor-2',
1001 \ col: 'cursor-1',
1002 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001003 redraw
1004 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1005 call assert_equal('xvimxxxxxxxxxxxxx', line)
1006 call popup_close(winid)
1007
Bram Moolenaar402502d2019-05-30 22:07:36 +02001008 " just enough room above
1009 call cursor(3, 3)
1010 redraw
1011 let winid = popup_atcursor(['vim', 'is great'], {})
1012 redraw
1013 let pos = popup_getpos(winid)
1014 call assert_equal(1, pos.line)
1015 call popup_close(winid)
1016
1017 " not enough room above, popup goes below the cursor
1018 call cursor(3, 3)
1019 redraw
1020 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1021 redraw
1022 let pos = popup_getpos(winid)
1023 call assert_equal(4, pos.line)
1024 call popup_close(winid)
1025
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001026 " cursor in first line, popup in line 2
1027 call cursor(1, 1)
1028 redraw
1029 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1030 redraw
1031 let pos = popup_getpos(winid)
1032 call assert_equal(2, pos.line)
1033 call popup_close(winid)
1034
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001035 bwipe!
1036endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001037
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001038func Test_popup_beval()
1039 if !CanRunVimInTerminal()
1040 throw 'Skipped: cannot make screendumps'
1041 endif
1042
1043 let lines =<< trim END
1044 call setline(1, range(1, 20))
1045 call setline(5, 'here is some text to hover over')
1046 set balloonevalterm
1047 set balloonexpr=BalloonExpr()
1048 set balloondelay=100
1049 func BalloonExpr()
1050 let s:winid = popup_beval([v:beval_text], {})
1051 return ''
1052 endfunc
1053 func Hover()
1054 call test_setmouse(5, 15)
1055 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1056 sleep 100m
1057 endfunc
1058 func MoveOntoPopup()
1059 call test_setmouse(4, 17)
1060 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1061 endfunc
1062 func MoveAway()
1063 call test_setmouse(5, 13)
1064 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1065 endfunc
1066 END
1067 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001068 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001069 call term_wait(buf, 100)
1070 call term_sendkeys(buf, 'j')
1071 call term_sendkeys(buf, ":call Hover()\<CR>")
1072 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1073
1074 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1075 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1076
1077 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1078 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1079
1080 " clean up
1081 call StopVimInTerminal(buf)
1082 call delete('XtestPopupBeval')
1083endfunc
1084
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001085func Test_popup_filter()
1086 new
1087 call setline(1, 'some text')
1088
1089 func MyPopupFilter(winid, c)
1090 if a:c == 'e'
1091 let g:eaten = 'e'
1092 return 1
1093 endif
1094 if a:c == '0'
1095 let g:ignored = '0'
1096 return 0
1097 endif
1098 if a:c == 'x'
1099 call popup_close(a:winid)
1100 return 1
1101 endif
1102 return 0
1103 endfunc
1104
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001105 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001106 redraw
1107
1108 " e is consumed by the filter
1109 call feedkeys('e', 'xt')
1110 call assert_equal('e', g:eaten)
1111
1112 " 0 is ignored by the filter
1113 normal $
1114 call assert_equal(9, getcurpos()[2])
1115 call feedkeys('0', 'xt')
1116 call assert_equal('0', g:ignored)
1117 call assert_equal(1, getcurpos()[2])
1118
1119 " x closes the popup
1120 call feedkeys('x', 'xt')
1121 call assert_equal('e', g:eaten)
1122 call assert_equal(-1, winbufnr(winid))
1123
1124 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001125 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001126endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001127
Bram Moolenaara42d9452019-06-15 21:46:30 +02001128func ShowDialog(key, result)
1129 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001130 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001131 \ filter: 'popup_filter_yesno',
1132 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001133 \ })
1134 redraw
1135 call feedkeys(a:key, "xt")
1136 call assert_equal(winid, s:cb_winid)
1137 call assert_equal(a:result, s:cb_res)
1138endfunc
1139
1140func Test_popup_dialog()
1141 func QuitCallback(id, res)
1142 let s:cb_winid = a:id
1143 let s:cb_res = a:res
1144 endfunc
1145
1146 let winid = ShowDialog("y", 1)
1147 let winid = ShowDialog("Y", 1)
1148 let winid = ShowDialog("n", 0)
1149 let winid = ShowDialog("N", 0)
1150 let winid = ShowDialog("x", 0)
1151 let winid = ShowDialog("X", 0)
1152 let winid = ShowDialog("\<Esc>", 0)
1153 let winid = ShowDialog("\<C-C>", -1)
1154
1155 delfunc QuitCallback
1156endfunc
1157
Bram Moolenaara730e552019-06-16 19:05:31 +02001158func ShowMenu(key, result)
1159 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001160 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001161 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001162 \ })
1163 redraw
1164 call feedkeys(a:key, "xt")
1165 call assert_equal(winid, s:cb_winid)
1166 call assert_equal(a:result, s:cb_res)
1167endfunc
1168
1169func Test_popup_menu()
1170 func QuitCallback(id, res)
1171 let s:cb_winid = a:id
1172 let s:cb_res = a:res
1173 endfunc
1174
1175 let winid = ShowMenu(" ", 1)
1176 let winid = ShowMenu("j \<CR>", 2)
1177 let winid = ShowMenu("JjK \<CR>", 2)
1178 let winid = ShowMenu("jjjjjj ", 3)
1179 let winid = ShowMenu("kkk ", 1)
1180 let winid = ShowMenu("x", -1)
1181 let winid = ShowMenu("X", -1)
1182 let winid = ShowMenu("\<Esc>", -1)
1183 let winid = ShowMenu("\<C-C>", -1)
1184
1185 delfunc QuitCallback
1186endfunc
1187
1188func Test_popup_menu_screenshot()
1189 if !CanRunVimInTerminal()
1190 throw 'Skipped: cannot make screendumps'
1191 endif
1192
1193 let lines =<< trim END
1194 call setline(1, range(1, 20))
1195 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001196 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001197 func MenuDone(id, res)
1198 echomsg "selected " .. a:res
1199 endfunc
1200 END
1201 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001202 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001203 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1204
1205 call term_sendkeys(buf, "jj")
1206 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1207
1208 call term_sendkeys(buf, " ")
1209 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1210
1211 " clean up
1212 call StopVimInTerminal(buf)
1213 call delete('XtestPopupMenu')
1214endfunc
1215
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001216func Test_popup_title()
1217 if !CanRunVimInTerminal()
1218 throw 'Skipped: cannot make screendumps'
1219 endif
1220
1221 " Create a popup without title or border, a line of padding will be added to
1222 " put the title on.
1223 let lines =<< trim END
1224 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001225 call popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001226 END
1227 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001228 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001229 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1230
1231 " clean up
1232 call StopVimInTerminal(buf)
1233 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001234
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001235 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001236 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001237 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001238 call assert_equal('Another Title', popup_getoptions(winid).title)
1239
1240 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001241endfunc
1242
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001243func Test_popup_close_callback()
1244 func PopupDone(id, result)
1245 let g:result = a:result
1246 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001247 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001248 redraw
1249 call popup_close(winid, 'done')
1250 call assert_equal('done', g:result)
1251endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001252
1253func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001254 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001255 redraw
1256 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001257 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001258 call assert_equal(5, pos.height)
1259
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001260 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001261 redraw
1262 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001263 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001264 call assert_equal(3, pos.height)
1265endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001266
1267func Test_popup_never_behind()
1268 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001269 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +02001270 endif
1271 " +-----------------------------+
1272 " | | |
1273 " | | |
1274 " | | |
1275 " | line1 |
1276 " |------------line2------------|
1277 " | line3 |
1278 " | line4 |
1279 " | |
1280 " | |
1281 " +-----------------------------+
1282 let lines =<< trim END
1283 only
1284 split
1285 vsplit
1286 let info_window1 = getwininfo()[0]
1287 let line = info_window1['height']
1288 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001289 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001290 \ line : line,
1291 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001292 \ })
1293 END
1294 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001295 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001296 call term_sendkeys(buf, "\<C-W>w")
1297 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1298
1299 " clean up
1300 call StopVimInTerminal(buf)
1301 call delete('XtestPopupBehind')
1302endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001303
1304func s:VerifyPosition( p, msg, line, col, width, height )
1305 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
1306 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
1307 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
1308 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
1309endfunc
1310
1311func Test_popup_position_adjust()
1312 " Anything placed past 2 cells from of the right of the screen is moved to the
1313 " left.
1314 "
1315 " When wrapping is disabled, we also shift to the left to display on the
1316 " screen, unless fixed is set.
1317
1318 " Entries for cases which don't vary based on wrapping.
1319 " Format is per tests described below
1320 let both_wrap_tests = [
1321 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
1322 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
1323 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
1324 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
1325 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
1326 \
1327 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
1328 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
1329 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
1330 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
1331 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
1332 \
1333 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1334 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1335 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1336 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1337 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
1338 \ ]
1339
1340 " these test groups are dicts with:
1341 " - comment: something to identify the group of tests by
1342 " - options: dict of options to merge with the row/col in tests
1343 " - tests: list of cases. Each one is a list with elements:
1344 " - text
1345 " - row
1346 " - col
1347 " - expected row
1348 " - expected col
1349 " - expected width
1350 " - expected height
1351 let tests = [
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001352 \ #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001353 \ comment: 'left-aligned with wrapping',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001354 \ options: #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001355 \ wrap: 1,
1356 \ pos: 'botleft',
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001357 \ },
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001358 \ tests: both_wrap_tests + [
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001359 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
1360 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
1361 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
1362 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
1363 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1364 \ ],
1365 \ },
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001366 \ #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001367 \ comment: 'left aligned without wrapping',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001368 \ options: #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001369 \ wrap: 0,
1370 \ pos: 'botleft',
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001371 \ },
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001372 \ tests: both_wrap_tests + [
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001373 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
1374 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
1375 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
1376 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
1377 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1378 \ ],
1379 \ },
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001380 \ #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001381 \ comment: 'left aligned with fixed position',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001382 \ options: #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001383 \ wrap: 0,
1384 \ fixed: 1,
1385 \ pos: 'botleft',
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001386 \ },
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001387 \ tests: both_wrap_tests + [
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001388 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
1389 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
1390 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
1391 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
1392 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
1393 \ ],
1394 \ },
1395 \ ]
1396
1397 for test_group in tests
1398 for test in test_group.tests
1399 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001400 let options = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001401 \ line: line,
1402 \ col: col,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001403 \ }
1404 call extend( options, test_group.options )
1405
1406 let p = popup_create( text, options )
1407
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001408 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001409 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1410 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001411 endfor
1412 endfor
1413
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001414 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001415 %bwipe!
1416endfunc
1417
Bram Moolenaar3397f742019-06-02 18:40:06 +02001418func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001419 " width of screen
1420 let X = join(map(range(&columns), {->'X'}), '')
1421
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001422 let p = popup_create( X, #{line: 1, col: 1, wrap: 0})
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001423 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
1424
1425 redraw
1426 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1427 call assert_equal(X, line)
1428
1429 call popup_close( p )
1430 redraw
1431
1432 " Same if placed on the right hand side
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001433 let p = popup_create( X, #{line: 1, col: &columns, wrap: 0})
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001434 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
1435
1436 redraw
1437 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1438 call assert_equal(X, line)
1439
1440 call popup_close( p )
1441 redraw
1442
1443 " Extend so > window width
1444 let X .= 'x'
1445
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001446 let p = popup_create( X, #{line: 1, col: 1, wrap: 0})
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001447 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
1448
1449 redraw
1450 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1451 call assert_equal(X[ : -2 ], line)
1452
1453 call popup_close( p )
1454 redraw
1455
1456 " Shifted then truncated (the x is not visible)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001457 let p = popup_create( X, #{line: 1, col: &columns - 3, wrap: 0})
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001458 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
1459
1460 redraw
1461 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1462 call assert_equal(X[ : -2 ], line)
1463
1464 call popup_close( p )
1465 redraw
1466
1467 " Not shifted, just truncated
1468 let p = popup_create( X,
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001469 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001470 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
1471
1472 redraw
1473 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1474 let e_line = ' ' . X[ 1 : -2 ]
1475 call assert_equal(e_line, line)
1476
1477 call popup_close( p )
1478 redraw
1479
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001480 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001481 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001482endfunc
1483
1484func Test_popup_moved()
1485 new
1486 call test_override('char_avail', 1)
1487 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1488
1489 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001490 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001491 redraw
1492 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001493 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001494 " trigger the check for last_cursormoved by going into insert mode
1495 call feedkeys("li\<Esc>", 'xt')
1496 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001497 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001498
1499 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001500 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001501 redraw
1502 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001503 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001504 call feedkeys("hi\<Esc>", 'xt')
1505 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001506 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001507
1508 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001509 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001510 redraw
1511 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001512 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001513 call feedkeys("li\<Esc>", 'xt')
1514 call assert_equal(1, popup_getpos(winid).visible)
1515 call feedkeys("ei\<Esc>", 'xt')
1516 call assert_equal(1, popup_getpos(winid).visible)
1517 call feedkeys("eli\<Esc>", 'xt')
1518 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001519 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001520
Bram Moolenaar17627312019-06-02 19:53:44 +02001521 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001522 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001523 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001524 redraw
1525 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001526 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001527 call feedkeys("eli\<Esc>", 'xt')
1528 call assert_equal(1, popup_getpos(winid).visible)
1529 call feedkeys("wi\<Esc>", 'xt')
1530 call assert_equal(1, popup_getpos(winid).visible)
1531 call feedkeys("Eli\<Esc>", 'xt')
1532 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001533 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001534
1535 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001536 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001537 redraw
1538 call assert_equal(1, popup_getpos(winid).visible)
1539 call feedkeys("eli\<Esc>", 'xt')
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
1546 bwipe!
1547 call test_override('ALL', 0)
1548endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001549
1550func Test_notifications()
1551 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02001552 throw 'Skipped: timer feature not supported'
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001553 endif
1554 if !CanRunVimInTerminal()
1555 throw 'Skipped: cannot make screendumps'
1556 endif
1557
1558 call writefile([
1559 \ "call setline(1, range(1, 20))",
1560 \ "hi Notification ctermbg=lightblue",
1561 \ "call popup_notification('first notification', {})",
1562 \], 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001563 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001564 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1565
1566 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001567 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1568 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001569 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1570
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001571 " clean up
1572 call StopVimInTerminal(buf)
1573 call delete('XtestNotifications')
1574endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001575
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001576func Test_popup_scrollbar()
1577 if !CanRunVimInTerminal()
1578 throw 'Skipped: cannot make screendumps'
1579 endif
1580
1581 let lines =<< trim END
1582 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001583 hi ScrollThumb ctermbg=blue
1584 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001585 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001586 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001587 \ minwidth: 8,
1588 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001589 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001590 func ScrollUp()
1591 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1592 endfunc
1593 func ScrollDown()
1594 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1595 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001596 func ClickTop()
1597 call feedkeys("\<F4>\<LeftMouse>", "xt")
1598 endfunc
1599 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001600 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001601 call feedkeys("\<F5>\<LeftMouse>", "xt")
1602 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001603 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001604 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1605 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001606 END
1607 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001608 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001609 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1610
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001611 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001612 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001613 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1614
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001615 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001616 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001617 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1618
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001619 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001620 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001621 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1622
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001623 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb'})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001624 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1625 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1626
1627 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1628 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1629
1630 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001631 " wait a bit, otherwise it fails sometimes (double click recognized?)
1632 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001633 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1634 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1635
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001636 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1637 sleep 100m
1638 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1639 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1640
1641 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1642 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1643
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001644 " clean up
1645 call StopVimInTerminal(buf)
1646 call delete('XtestPopupScroll')
1647endfunc
1648
Bram Moolenaar437a7462019-07-05 20:17:22 +02001649func Test_popup_fitting_scrollbar()
1650 " this was causing a crash, divide by zero
1651 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001652 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001653 \ scrollbar: 1,
1654 \ maxwidth: 10,
1655 \ maxheight: 5,
1656 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001657 redraw
1658 call popup_clear()
1659endfunc
1660
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001661func Test_popup_settext()
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001662 if !CanRunVimInTerminal()
1663 throw 'Skipped: cannot make screendumps'
1664 endif
1665
1666 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001667 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001668 let p = popup_create('test', opts)
1669 call popup_settext(p, 'this is a text')
1670 END
1671
1672 call writefile( lines, 'XtestPopupSetText' )
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001673 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001674 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1675
1676 " Setting to empty string clears it
1677 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1678 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1679
1680 " Setting a list
1681 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1682 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1683
1684 " Shrinking with a list
1685 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1686 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1687
1688 " Growing with a list
1689 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1690 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1691
1692 " Empty list clears
1693 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1694 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1695
1696 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001697 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001698 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1699
1700 " clean up
1701 call StopVimInTerminal(buf)
1702 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001703endfunc
1704
1705func Test_popup_hidden()
1706 new
1707
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001708 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001709 redraw
1710 call assert_equal(0, popup_getpos(winid).visible)
1711 call popup_close(winid)
1712
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001713 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001714 redraw
1715 call assert_equal(0, popup_getpos(winid).visible)
1716 call popup_close(winid)
1717
1718 func QuitCallback(id, res)
1719 let s:cb_winid = a:id
1720 let s:cb_res = a:res
1721 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001722 let winid = popup_dialog('make a choice', #{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001723 \ filter: 'popup_filter_yesno',
1724 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001725 \ })
1726 redraw
1727 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001728 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1729 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001730 exe "normal anot used by filter\<Esc>"
1731 call assert_equal('not used by filter', getline(1))
1732
1733 call popup_show(winid)
1734 call feedkeys('y', "xt")
1735 call assert_equal(1, s:cb_res)
1736
1737 bwipe!
1738 delfunc QuitCallback
1739endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001740
1741" Test options not checked elsewhere
1742func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001743 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001744 let options = popup_getoptions(winid)
1745 call assert_equal(1, options.wrap)
1746 call assert_equal(0, options.drag)
1747 call assert_equal('Beautiful', options.highlight)
1748
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001749 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001750 let options = popup_getoptions(winid)
1751 call assert_equal(0, options.wrap)
1752 call assert_equal(1, options.drag)
1753 call assert_equal('Another', options.highlight)
1754
1755 call popup_close(winid)
1756endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001757
1758func Test_popupwin_garbage_collect()
1759 func MyPopupFilter(x, winid, c)
1760 " NOP
1761 endfunc
1762
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001763 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001764 call test_garbagecollect_now()
1765 redraw
1766 " Must not crach caused by invalid memory access
1767 call feedkeys('j', 'xt')
1768 call assert_true(v:true)
1769
1770 call popup_close(winid)
1771 delfunc MyPopupFilter
1772endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001773
1774func Test_popupwin_with_buffer()
1775 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1776 let buf = bufadd('XsomeFile')
1777 call assert_equal(0, bufloaded(buf))
1778 let winid = popup_create(buf, {})
1779 call assert_notequal(0, winid)
1780 let pos = popup_getpos(winid)
1781 call assert_equal(2, pos.height)
1782 call assert_equal(1, bufloaded(buf))
1783 call popup_close(winid)
1784 call assert_equal({}, popup_getpos(winid))
1785 call assert_equal(1, bufloaded(buf))
1786 exe 'bwipe! ' .. buf
Bram Moolenaar7866b872019-07-01 22:21:01 +02001787
1788 edit test_popupwin.vim
1789 let winid = popup_create(bufnr(''), {})
1790 redraw
1791 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001792 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001793endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02001794
1795func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001796 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001797 \ maxwidth: 40,
1798 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02001799 \ })
1800 for top in range(1, 20)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001801 call popup_setoptions(winid, #{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02001802 redraw
1803 call assert_equal(19, popup_getpos(winid).width)
1804 endfor
1805 call popup_clear()
1806endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02001807
1808func Test_popupwin_buf_close()
1809 let buf = bufadd('Xtestbuf')
1810 call bufload(buf)
1811 call setbufline(buf, 1, ['just', 'some', 'lines'])
1812 let winid = popup_create(buf, {})
1813 redraw
1814 call assert_equal(3, popup_getpos(winid).height)
1815 let bufinfo = getbufinfo(buf)[0]
1816 call assert_equal(1, bufinfo.changed)
1817 call assert_equal(0, bufinfo.hidden)
1818 call assert_equal(0, bufinfo.listed)
1819 call assert_equal(1, bufinfo.loaded)
1820 call assert_equal([], bufinfo.windows)
1821 call assert_equal([winid], bufinfo.popups)
1822
1823 call popup_close(winid)
1824 call assert_equal({}, popup_getpos(winid))
1825 let bufinfo = getbufinfo(buf)[0]
1826 call assert_equal(1, bufinfo.changed)
1827 call assert_equal(1, bufinfo.hidden)
1828 call assert_equal(0, bufinfo.listed)
1829 call assert_equal(1, bufinfo.loaded)
1830 call assert_equal([], bufinfo.windows)
1831 call assert_equal([], bufinfo.popups)
1832 exe 'bwipe! ' .. buf
1833endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02001834
1835func Test_popup_menu_with_maxwidth()
1836 if !CanRunVimInTerminal()
1837 throw 'Skipped: cannot make screendumps'
1838 endif
1839
1840 let lines =<< trim END
1841 call setline(1, range(1, 10))
1842 hi ScrollThumb ctermbg=blue
1843 hi ScrollBar ctermbg=red
1844 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001845 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001846 \ maxwidth: 10,
1847 \ maxheight: 3,
1848 \ pos : 'topleft',
1849 \ col : a:col,
1850 \ line : a:line,
1851 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02001852 \ })
1853 endfunc
1854 call PopupMenu(['x'], 1, 1)
1855 call PopupMenu(['123456789|'], 1, 16)
1856 call PopupMenu(['123456789|' .. ' '], 7, 1)
1857 call PopupMenu([repeat('123456789|', 100)], 7, 16)
1858 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
1859 END
1860 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001861 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02001862 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
1863
1864 " close the menu popupwin.
1865 call term_sendkeys(buf, " ")
1866 call term_sendkeys(buf, " ")
1867 call term_sendkeys(buf, " ")
1868 call term_sendkeys(buf, " ")
1869 call term_sendkeys(buf, " ")
1870
1871 " clean up
1872 call StopVimInTerminal(buf)
1873 call delete('XtestPopupMenuMaxWidth')
1874endfunc
1875
Bram Moolenaara901a372019-07-13 16:38:50 +02001876func Test_popup_menu_with_scrollbar()
1877 if !CanRunVimInTerminal()
1878 throw 'Skipped: cannot make screendumps'
1879 endif
1880
1881 let lines =<< trim END
1882 call setline(1, range(1, 20))
1883 hi ScrollThumb ctermbg=blue
1884 hi ScrollBar ctermbg=red
1885 call popup_menu(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001886 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001887 \ minwidth: 8,
1888 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02001889 \ })
1890 END
1891 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001892 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02001893
1894 call term_sendkeys(buf, "j")
1895 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
1896
1897 call term_sendkeys(buf, "jjj")
1898 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
1899
1900 " if the cursor is the bottom line, it stays at the bottom line.
1901 call term_sendkeys(buf, repeat("j", 20))
1902 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
1903
1904 call term_sendkeys(buf, "kk")
1905 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
1906
1907 call term_sendkeys(buf, "k")
1908 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
1909
1910 " if the cursor is in the top line, it stays in the top line.
1911 call term_sendkeys(buf, repeat("k", 20))
1912 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
1913
1914 " close the menu popupwin.
1915 call term_sendkeys(buf, " ")
1916
1917 " clean up
1918 call StopVimInTerminal(buf)
1919 call delete('XtestPopupMenuScroll')
1920endfunc
1921
Bram Moolenaar017c2692019-07-13 14:17:51 +02001922" vim: shiftwidth=2 sts=2 expandtab