blob: 5a841e30ae580895a1518c5e95fc19e629fd31eb [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,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200357 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200358 \ border: [],
359 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200360 \ })
361 func Dragit()
362 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
363 endfunc
364 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
365 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200366 func Resize()
367 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
368 endfunc
369 map <silent> <F5> :call test_setmouse(6, 41)<CR>
370 map <silent> <F6> :call test_setmouse(7, 45)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200371 END
372 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200373 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200374 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
375
376 call term_sendkeys(buf, ":call Dragit()\<CR>")
377 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
378
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200379 call term_sendkeys(buf, ":call Resize()\<CR>")
380 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
381
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200382 " clean up
383 call StopVimInTerminal(buf)
384 call delete('XtestPopupDrag')
385endfunc
386
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200387func Test_popup_close_with_mouse()
388 if !CanRunVimInTerminal()
389 throw 'Skipped: cannot make screendumps'
390 endif
391 let lines =<< trim END
392 call setline(1, range(1, 20))
393 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200394 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200395 \ close: 'button',
396 \ border: [],
397 \ line: 1,
398 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200399 \ })
400 func CloseMsg(id, result)
401 echomsg 'Popup closed with ' .. a:result
402 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200403 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200404 \ close: 'click',
405 \ line: 3,
406 \ col: 15,
407 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200408 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200409 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200410 \ close: 'button',
411 \ line: 5,
412 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200413 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200414 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200415 \ close: 'button',
416 \ padding: [],
417 \ line: 5,
418 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200419 \ })
420 func CloseWithX()
421 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
422 endfunc
423 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
424 func CloseWithClick()
425 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
426 endfunc
427 map <silent> <F4> :call test_setmouse(3, 17)<CR>
428 END
429 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200430 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200431 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
432
433 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
434 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
435
436 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
437 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
438
439 " clean up
440 call StopVimInTerminal(buf)
441 call delete('XtestPopupClose')
442endfunction
443
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200444func Test_popup_with_mask()
445 if !CanRunVimInTerminal()
446 throw 'Skipped: cannot make screendumps'
447 endif
448 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200449 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200450 hi PopupColor ctermbg=lightgrey
451 let winid = popup_create([
452 \ 'some text',
453 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200454 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200455 \ line: 1,
456 \ col: 10,
457 \ wrap: 0,
458 \ fixed: 1,
459 \ zindex: 90,
460 \ padding: [],
461 \ highlight: 'PopupColor',
462 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200463 call popup_create([
464 \ 'xxxxxxxxx',
465 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200466 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200467 \ line: 3,
468 \ col: 18,
469 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200470 let winidb = popup_create([
471 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200472 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200473 \ line: 7,
474 \ col: 10,
475 \ wrap: 0,
476 \ fixed: 1,
477 \ close: 'button',
478 \ zindex: 90,
479 \ padding: [],
480 \ border: [],
481 \ 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 +0200482 END
483 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200484 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200485 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
486
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200487 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
488 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200489 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200490 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
491
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200492 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
493 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200494 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200495 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
496
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200497 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
498 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200499 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200500 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
501
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200502 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
503 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200504 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200505 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
506
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200507 " clean up
508 call StopVimInTerminal(buf)
509 call delete('XtestPopupMask')
510endfunc
511
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200512func Test_popup_select()
513 if !CanRunVimInTerminal()
514 throw 'Skipped: cannot make screendumps'
515 endif
Bram Moolenaar650a6372019-06-15 00:29:33 +0200516 if !has('clipboard')
517 throw 'Skipped: clipboard feature missing'
518 endif
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200519 " create a popup with some text to be selected
520 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200521 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200522 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200523 let winid = popup_create(['the word', 'some more', 'several words here'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200524 \ drag: 1,
525 \ border: [],
526 \ line: 3,
527 \ col: 10,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200528 \ })
529 func Select1()
530 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
531 endfunc
532 map <silent> <F3> :call test_setmouse(4, 15)<CR>
533 map <silent> <F4> :call test_setmouse(6, 23)<CR>
534 END
535 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200536 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200537 call term_sendkeys(buf, ":call Select1()\<CR>")
538 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
539
540 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
541 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200542 " clean the command line, sometimes it still shows a command
543 call term_sendkeys(buf, ":\<esc>")
544
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200545 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
546
547 " clean up
548 call StopVimInTerminal(buf)
549 call delete('XtestPopupSelect')
550endfunc
551
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200552func Test_popup_in_tab()
553 " default popup is local to tab, not visible when in other tab
554 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200555 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200556 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200557 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200558 tabnew
559 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200560 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200561 quit
562 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200563
564 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200565 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200566 " buffer is gone now
567 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200568
569 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200570 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200571 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200572 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200573 tabnew
574 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200575 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200576 quit
577 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200578 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200579
580 " create popup in other tab
581 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200582 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200583 call assert_equal(0, popup_getpos(winid).visible)
584 call assert_equal(1, popup_getoptions(winid).tabpage)
585 quit
586 call assert_equal(1, popup_getpos(winid).visible)
587 call assert_equal(0, popup_getoptions(winid).tabpage)
588 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200589endfunc
590
591func Test_popup_valid_arguments()
592 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200593 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200594 let pos = popup_getpos(winid)
595 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200596 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200597
598 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200599 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200600 let pos = popup_getpos(winid)
601 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200602 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200603
604 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200605 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200606 let pos = popup_getpos(winid)
607 let around = (&columns - pos.width) / 2
608 call assert_inrange(around - 1, around + 1, pos.col)
609 let around = (&lines - pos.height) / 2
610 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200611 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200612endfunc
613
614func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200615 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200616 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200617 call assert_fails('call popup_create("text", "none")', 'E715:')
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", #{col: "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", #{col: "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", #{col: "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", #{col: "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", #{line: "xxx"})', '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", #{line: "cursor8"})', 'E15:')
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", #{line: "cursor+x"})', 'E15:')
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", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200636 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200637
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200638 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200639 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200640 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
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", #{border: "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", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200645 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200646 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
647 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200648 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200649 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200650
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200651 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200652 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200653 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200654 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200655 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200656 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200657 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
658 call popup_clear()
659 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200660 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200661 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200662endfunc
663
Bram Moolenaareea16992019-05-31 17:34:48 +0200664func Test_win_execute_closing_curwin()
665 split
666 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200667 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200668 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200669endfunc
670
671func Test_win_execute_not_allowed()
672 let winid = popup_create('some text', {})
673 call assert_fails('call win_execute(winid, "split")', 'E994:')
674 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
675 call assert_fails('call win_execute(winid, "close")', 'E994:')
676 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200677 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200678 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
679 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
680 call assert_fails('call win_execute(winid, "next")', 'E994:')
681 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
682 call assert_fails('call win_execute(winid, "buf")', 'E994:')
683 call assert_fails('call win_execute(winid, "edit")', 'E994:')
684 call assert_fails('call win_execute(winid, "enew")', 'E994:')
685 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
686 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
687 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
688 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200689 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200690endfunc
691
Bram Moolenaar402502d2019-05-30 22:07:36 +0200692func Test_popup_with_wrap()
693 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200694 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200695 endif
696 let lines =<< trim END
697 call setline(1, range(1, 100))
698 let winid = popup_create(
699 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200700 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200701 END
702 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200703 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200704 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
705
706 " clean up
707 call StopVimInTerminal(buf)
708 call delete('XtestPopup')
709endfunc
710
711func Test_popup_without_wrap()
712 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200713 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200714 endif
715 let lines =<< trim END
716 call setline(1, range(1, 100))
717 let winid = popup_create(
718 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200719 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200720 END
721 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200722 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200723 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
724
725 " clean up
726 call StopVimInTerminal(buf)
727 call delete('XtestPopup')
728endfunc
729
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200730func Test_popup_with_showbreak()
731 if !CanRunVimInTerminal()
732 throw 'Skipped: cannot make screendumps'
733 endif
734 let lines =<< trim END
735 set showbreak=>>\
736 call setline(1, range(1, 20))
737 let winid = popup_dialog(
738 \ 'a long line here',
739 \ #{filter: 'popup_filter_yesno'})
740 END
741 call writefile(lines, 'XtestPopupShowbreak')
742 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
743 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
744
745 " clean up
746 call term_sendkeys(buf, "y")
747 call StopVimInTerminal(buf)
748 call delete('XtestPopupShowbreak')
749endfunc
750
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200751func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200752 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +0200753 throw 'Skipped: timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200754 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200755 topleft vnew
756 call setline(1, 'hello')
757
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200758 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200759 \ line: 1,
760 \ col: 1,
761 \ minwidth: 20,
762 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200763 \})
764 redraw
765 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
766 call assert_equal('world', line)
767
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200768 call assert_equal(winid, popup_locate(1, 1))
769 call assert_equal(winid, popup_locate(1, 20))
770 call assert_equal(0, popup_locate(1, 21))
771 call assert_equal(0, popup_locate(2, 1))
772
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200773 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200774 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200775 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
776 call assert_equal('hello', line)
777
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200778 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200779 \ line: &lines,
780 \ col: 10,
781 \ minwidth: 20,
782 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200783 \})
784 redraw
785 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
786 call assert_match('.*on the command line.*', line)
787
788 sleep 700m
789 redraw
790 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
791 call assert_notmatch('.*on the command line.*', line)
792
793 bwipe!
794endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200795
796func Test_popup_hide()
797 topleft vnew
798 call setline(1, 'hello')
799
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200800 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200801 \ line: 1,
802 \ col: 1,
803 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200804 \})
805 redraw
806 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
807 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200808 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200809 " buffer is still listed and active
810 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200811
812 call popup_hide(winid)
813 redraw
814 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
815 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200816 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200817 " buffer is still listed but hidden
818 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200819
820 call popup_show(winid)
821 redraw
822 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
823 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200824 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200825
826
827 call popup_close(winid)
828 redraw
829 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
830 call assert_equal('hello', line)
831
832 " error is given for existing non-popup window
833 call assert_fails('call popup_hide(win_getid())', 'E993:')
834
835 " no error non-existing window
836 call popup_hide(1234234)
837 call popup_show(41234234)
838
839 bwipe!
840endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200841
842func Test_popup_move()
843 topleft vnew
844 call setline(1, 'hello')
845
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200846 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200847 \ line: 1,
848 \ col: 1,
849 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200850 \})
851 redraw
852 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
853 call assert_equal('world ', line)
854
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200855 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200856 redraw
857 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
858 call assert_equal('hello ', line)
859 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
860 call assert_equal('~world', line)
861
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200862 call popup_move(winid, #{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200863 redraw
864 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
865 call assert_equal('hworld', line)
866
867 call popup_close(winid)
868
869 bwipe!
870endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200871
Bram Moolenaar402502d2019-05-30 22:07:36 +0200872func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200873 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200874 \ line: 2,
875 \ col: 3,
876 \ minwidth: 10,
877 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200878 \})
879 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200880 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200881 call assert_equal(2, res.line)
882 call assert_equal(3, res.col)
883 call assert_equal(10, res.width)
884 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200885 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200886
887 call popup_close(winid)
888endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200889
890func Test_popup_width_longest()
891 let tests = [
892 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
893 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
894 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
895 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
896 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
897 \ ]
898
899 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200900 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200901 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200902 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200903 call assert_equal(test[1], position.width)
904 call popup_close(winid)
905 endfor
906endfunc
907
908func Test_popup_wraps()
909 let tests = [
910 \ ['nowrap', 6, 1],
911 \ ['a line that wraps once', 12, 2],
912 \ ['a line that wraps two times', 12, 3],
913 \ ]
914 for test in tests
915 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200916 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200917 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200918 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200919 call assert_equal(test[1], position.width)
920 call assert_equal(test[2], position.height)
921
922 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200923 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200924 endfor
925endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200926
927func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200928 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200929 \ line: 2,
930 \ col: 3,
931 \ minwidth: 10,
932 \ minheight: 11,
933 \ maxwidth: 20,
934 \ maxheight: 21,
935 \ zindex: 100,
936 \ time: 5000,
937 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200938 \})
939 redraw
940 let res = popup_getoptions(winid)
941 call assert_equal(2, res.line)
942 call assert_equal(3, res.col)
943 call assert_equal(10, res.minwidth)
944 call assert_equal(11, res.minheight)
945 call assert_equal(20, res.maxwidth)
946 call assert_equal(21, res.maxheight)
947 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200948 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +0200949 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200950 if has('timers')
951 call assert_equal(5000, res.time)
952 endif
953 call popup_close(winid)
954
955 let winid = popup_create('hello', {})
956 redraw
957 let res = popup_getoptions(winid)
958 call assert_equal(0, res.line)
959 call assert_equal(0, res.col)
960 call assert_equal(0, res.minwidth)
961 call assert_equal(0, res.minheight)
962 call assert_equal(0, res.maxwidth)
963 call assert_equal(0, res.maxheight)
964 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200965 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200966 if has('timers')
967 call assert_equal(0, res.time)
968 endif
969 call popup_close(winid)
970 call assert_equal({}, popup_getoptions(winid))
971endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200972
973func Test_popup_option_values()
974 new
975 " window-local
976 setlocal number
977 setlocal nowrap
978 " buffer-local
979 setlocal omnifunc=Something
980 " global/buffer-local
981 setlocal path=/there
982 " global/window-local
983 setlocal scrolloff=9
984
985 let winid = popup_create('hello', {})
986 call assert_equal(0, getwinvar(winid, '&number'))
987 call assert_equal(1, getwinvar(winid, '&wrap'))
988 call assert_equal('', getwinvar(winid, '&omnifunc'))
989 call assert_equal(&g:path, getwinvar(winid, '&path'))
990 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
991
992 call popup_close(winid)
993 bwipe
994endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200995
996func Test_popup_atcursor()
997 topleft vnew
998 call setline(1, [
999 \ 'xxxxxxxxxxxxxxxxx',
1000 \ 'xxxxxxxxxxxxxxxxx',
1001 \ 'xxxxxxxxxxxxxxxxx',
1002 \])
1003
1004 call cursor(2, 2)
1005 redraw
1006 let winid = popup_atcursor('vim', {})
1007 redraw
1008 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1009 call assert_equal('xvimxxxxxxxxxxxxx', line)
1010 call popup_close(winid)
1011
1012 call cursor(3, 4)
1013 redraw
1014 let winid = popup_atcursor('vim', {})
1015 redraw
1016 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1017 call assert_equal('xxxvimxxxxxxxxxxx', line)
1018 call popup_close(winid)
1019
1020 call cursor(1, 1)
1021 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001022 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001023 \ line: 'cursor+2',
1024 \ col: 'cursor+1',
1025 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001026 redraw
1027 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1028 call assert_equal('xvimxxxxxxxxxxxxx', line)
1029 call popup_close(winid)
1030
1031 call cursor(3, 3)
1032 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001033 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001034 \ line: 'cursor-2',
1035 \ col: 'cursor-1',
1036 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001037 redraw
1038 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1039 call assert_equal('xvimxxxxxxxxxxxxx', line)
1040 call popup_close(winid)
1041
Bram Moolenaar402502d2019-05-30 22:07:36 +02001042 " just enough room above
1043 call cursor(3, 3)
1044 redraw
1045 let winid = popup_atcursor(['vim', 'is great'], {})
1046 redraw
1047 let pos = popup_getpos(winid)
1048 call assert_equal(1, pos.line)
1049 call popup_close(winid)
1050
1051 " not enough room above, popup goes below the cursor
1052 call cursor(3, 3)
1053 redraw
1054 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1055 redraw
1056 let pos = popup_getpos(winid)
1057 call assert_equal(4, pos.line)
1058 call popup_close(winid)
1059
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001060 " cursor in first line, popup in line 2
1061 call cursor(1, 1)
1062 redraw
1063 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1064 redraw
1065 let pos = popup_getpos(winid)
1066 call assert_equal(2, pos.line)
1067 call popup_close(winid)
1068
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001069 bwipe!
1070endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001071
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001072func Test_popup_beval()
1073 if !CanRunVimInTerminal()
1074 throw 'Skipped: cannot make screendumps'
1075 endif
1076
1077 let lines =<< trim END
1078 call setline(1, range(1, 20))
1079 call setline(5, 'here is some text to hover over')
1080 set balloonevalterm
1081 set balloonexpr=BalloonExpr()
1082 set balloondelay=100
1083 func BalloonExpr()
1084 let s:winid = popup_beval([v:beval_text], {})
1085 return ''
1086 endfunc
1087 func Hover()
1088 call test_setmouse(5, 15)
1089 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1090 sleep 100m
1091 endfunc
1092 func MoveOntoPopup()
1093 call test_setmouse(4, 17)
1094 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1095 endfunc
1096 func MoveAway()
1097 call test_setmouse(5, 13)
1098 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1099 endfunc
1100 END
1101 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001102 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001103 call term_wait(buf, 100)
1104 call term_sendkeys(buf, 'j')
1105 call term_sendkeys(buf, ":call Hover()\<CR>")
1106 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1107
1108 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1109 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1110
1111 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1112 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1113
1114 " clean up
1115 call StopVimInTerminal(buf)
1116 call delete('XtestPopupBeval')
1117endfunc
1118
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001119func Test_popup_filter()
1120 new
1121 call setline(1, 'some text')
1122
1123 func MyPopupFilter(winid, c)
1124 if a:c == 'e'
1125 let g:eaten = 'e'
1126 return 1
1127 endif
1128 if a:c == '0'
1129 let g:ignored = '0'
1130 return 0
1131 endif
1132 if a:c == 'x'
1133 call popup_close(a:winid)
1134 return 1
1135 endif
1136 return 0
1137 endfunc
1138
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001139 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001140 redraw
1141
1142 " e is consumed by the filter
1143 call feedkeys('e', 'xt')
1144 call assert_equal('e', g:eaten)
1145
1146 " 0 is ignored by the filter
1147 normal $
1148 call assert_equal(9, getcurpos()[2])
1149 call feedkeys('0', 'xt')
1150 call assert_equal('0', g:ignored)
1151 call assert_equal(1, getcurpos()[2])
1152
1153 " x closes the popup
1154 call feedkeys('x', 'xt')
1155 call assert_equal('e', g:eaten)
1156 call assert_equal(-1, winbufnr(winid))
1157
1158 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001159 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001160endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001161
Bram Moolenaara42d9452019-06-15 21:46:30 +02001162func ShowDialog(key, result)
1163 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001164 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001165 \ filter: 'popup_filter_yesno',
1166 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001167 \ })
1168 redraw
1169 call feedkeys(a:key, "xt")
1170 call assert_equal(winid, s:cb_winid)
1171 call assert_equal(a:result, s:cb_res)
1172endfunc
1173
1174func Test_popup_dialog()
1175 func QuitCallback(id, res)
1176 let s:cb_winid = a:id
1177 let s:cb_res = a:res
1178 endfunc
1179
1180 let winid = ShowDialog("y", 1)
1181 let winid = ShowDialog("Y", 1)
1182 let winid = ShowDialog("n", 0)
1183 let winid = ShowDialog("N", 0)
1184 let winid = ShowDialog("x", 0)
1185 let winid = ShowDialog("X", 0)
1186 let winid = ShowDialog("\<Esc>", 0)
1187 let winid = ShowDialog("\<C-C>", -1)
1188
1189 delfunc QuitCallback
1190endfunc
1191
Bram Moolenaara730e552019-06-16 19:05:31 +02001192func ShowMenu(key, result)
1193 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001194 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001195 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001196 \ })
1197 redraw
1198 call feedkeys(a:key, "xt")
1199 call assert_equal(winid, s:cb_winid)
1200 call assert_equal(a:result, s:cb_res)
1201endfunc
1202
1203func Test_popup_menu()
1204 func QuitCallback(id, res)
1205 let s:cb_winid = a:id
1206 let s:cb_res = a:res
1207 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001208 " mapping won't be used in popup
1209 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001210
1211 let winid = ShowMenu(" ", 1)
1212 let winid = ShowMenu("j \<CR>", 2)
1213 let winid = ShowMenu("JjK \<CR>", 2)
1214 let winid = ShowMenu("jjjjjj ", 3)
1215 let winid = ShowMenu("kkk ", 1)
1216 let winid = ShowMenu("x", -1)
1217 let winid = ShowMenu("X", -1)
1218 let winid = ShowMenu("\<Esc>", -1)
1219 let winid = ShowMenu("\<C-C>", -1)
1220
1221 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001222 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001223endfunc
1224
1225func Test_popup_menu_screenshot()
1226 if !CanRunVimInTerminal()
1227 throw 'Skipped: cannot make screendumps'
1228 endif
1229
1230 let lines =<< trim END
1231 call setline(1, range(1, 20))
1232 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001233 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001234 func MenuDone(id, res)
1235 echomsg "selected " .. a:res
1236 endfunc
1237 END
1238 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001239 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001240 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1241
1242 call term_sendkeys(buf, "jj")
1243 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1244
1245 call term_sendkeys(buf, " ")
1246 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1247
1248 " clean up
1249 call StopVimInTerminal(buf)
1250 call delete('XtestPopupMenu')
1251endfunc
1252
Bram Moolenaarf914a332019-07-20 15:09:56 +02001253func Test_popup_menu_narrow()
1254 if !CanRunVimInTerminal()
1255 throw 'Skipped: cannot make screendumps'
1256 endif
1257
1258 let lines =<< trim END
1259 call setline(1, range(1, 20))
1260 hi PopupSelected ctermbg=green
1261 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1262 func MenuDone(id, res)
1263 echomsg "selected " .. a:res
1264 endfunc
1265 END
1266 call writefile(lines, 'XtestPopupNarrowMenu')
1267 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1268 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1269
1270 " clean up
1271 call term_sendkeys(buf, "x")
1272 call StopVimInTerminal(buf)
1273 call delete('XtestPopupNarrowMenu')
1274endfunc
1275
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001276func Test_popup_title()
1277 if !CanRunVimInTerminal()
1278 throw 'Skipped: cannot make screendumps'
1279 endif
1280
1281 " Create a popup without title or border, a line of padding will be added to
1282 " put the title on.
1283 let lines =<< trim END
1284 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001285 call popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001286 END
1287 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001288 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001289 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1290
1291 " clean up
1292 call StopVimInTerminal(buf)
1293 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001294
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001295 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001296 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001297 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001298 call assert_equal('Another Title', popup_getoptions(winid).title)
1299
1300 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001301endfunc
1302
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001303func Test_popup_close_callback()
1304 func PopupDone(id, result)
1305 let g:result = a:result
1306 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001307 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001308 redraw
1309 call popup_close(winid, 'done')
1310 call assert_equal('done', g:result)
1311endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001312
1313func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001314 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001315 redraw
1316 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001317 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001318 call assert_equal(5, pos.height)
1319
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001320 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001321 redraw
1322 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001323 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001324 call assert_equal(3, pos.height)
1325endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001326
1327func Test_popup_never_behind()
1328 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001329 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +02001330 endif
1331 " +-----------------------------+
1332 " | | |
1333 " | | |
1334 " | | |
1335 " | line1 |
1336 " |------------line2------------|
1337 " | line3 |
1338 " | line4 |
1339 " | |
1340 " | |
1341 " +-----------------------------+
1342 let lines =<< trim END
1343 only
1344 split
1345 vsplit
1346 let info_window1 = getwininfo()[0]
1347 let line = info_window1['height']
1348 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001349 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001350 \ line : line,
1351 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001352 \ })
1353 END
1354 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001355 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001356 call term_sendkeys(buf, "\<C-W>w")
1357 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1358
1359 " clean up
1360 call StopVimInTerminal(buf)
1361 call delete('XtestPopupBehind')
1362endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001363
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001364func s:VerifyPosition(p, msg, line, col, width, height)
1365 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1366 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1367 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1368 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001369endfunc
1370
1371func Test_popup_position_adjust()
1372 " Anything placed past 2 cells from of the right of the screen is moved to the
1373 " left.
1374 "
1375 " When wrapping is disabled, we also shift to the left to display on the
1376 " screen, unless fixed is set.
1377
1378 " Entries for cases which don't vary based on wrapping.
1379 " Format is per tests described below
1380 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001381 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1382 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1383 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1384 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1385 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1386 \
1387 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1388 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1389 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1390 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1391 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1392 \
1393 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1394 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1395 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1396 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1397 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1398 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001399
1400 " these test groups are dicts with:
1401 " - comment: something to identify the group of tests by
1402 " - options: dict of options to merge with the row/col in tests
1403 " - tests: list of cases. Each one is a list with elements:
1404 " - text
1405 " - row
1406 " - col
1407 " - expected row
1408 " - expected col
1409 " - expected width
1410 " - expected height
1411 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001412 \ #{
1413 \ comment: 'left-aligned with wrapping',
1414 \ options: #{
1415 \ wrap: 1,
1416 \ pos: 'botleft',
1417 \ },
1418 \ tests: both_wrap_tests + [
1419 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1420 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1421 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1422 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1423 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1424 \ ],
1425 \ },
1426 \ #{
1427 \ comment: 'left aligned without wrapping',
1428 \ options: #{
1429 \ wrap: 0,
1430 \ pos: 'botleft',
1431 \ },
1432 \ tests: both_wrap_tests + [
1433 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1434 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1435 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1436 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1437 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1438 \ ],
1439 \ },
1440 \ #{
1441 \ comment: 'left aligned with fixed position',
1442 \ options: #{
1443 \ wrap: 0,
1444 \ fixed: 1,
1445 \ pos: 'botleft',
1446 \ },
1447 \ tests: both_wrap_tests + [
1448 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1449 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1450 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1451 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1452 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1453 \ ],
1454 \ },
1455 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001456
1457 for test_group in tests
1458 for test in test_group.tests
1459 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001460 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001461 \ line: line,
1462 \ col: col,
1463 \ }
1464 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001465
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001466 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001467
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001468 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001469 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1470 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001471 endfor
1472 endfor
1473
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001474 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001475 %bwipe!
1476endfunc
1477
Bram Moolenaar3397f742019-06-02 18:40:06 +02001478func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001479 " width of screen
1480 let X = join(map(range(&columns), {->'X'}), '')
1481
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001482 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1483 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001484
1485 redraw
1486 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1487 call assert_equal(X, line)
1488
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001489 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001490 redraw
1491
1492 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001493 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1494 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001495
1496 redraw
1497 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1498 call assert_equal(X, line)
1499
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001500 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001501 redraw
1502
1503 " Extend so > window width
1504 let X .= 'x'
1505
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001506 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1507 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001508
1509 redraw
1510 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1511 call assert_equal(X[ : -2 ], line)
1512
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001513 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001514 redraw
1515
1516 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001517 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1518 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001519
1520 redraw
1521 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1522 call assert_equal(X[ : -2 ], line)
1523
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001524 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001525 redraw
1526
1527 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001528 let p = popup_create(X,
1529 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1530 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001531
1532 redraw
1533 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1534 let e_line = ' ' . X[ 1 : -2 ]
1535 call assert_equal(e_line, line)
1536
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001537 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001538 redraw
1539
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001540 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001541 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001542endfunc
1543
1544func Test_popup_moved()
1545 new
1546 call test_override('char_avail', 1)
1547 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1548
1549 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001550 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001551 redraw
1552 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001553 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001554 " trigger the check for last_cursormoved by going into insert mode
1555 call feedkeys("li\<Esc>", 'xt')
1556 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001557 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001558
1559 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001560 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001561 redraw
1562 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001563 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001564 call feedkeys("hi\<Esc>", 'xt')
1565 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001566 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001567
1568 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001569 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001570 redraw
1571 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001572 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001573 call feedkeys("li\<Esc>", 'xt')
1574 call assert_equal(1, popup_getpos(winid).visible)
1575 call feedkeys("ei\<Esc>", 'xt')
1576 call assert_equal(1, popup_getpos(winid).visible)
1577 call feedkeys("eli\<Esc>", 'xt')
1578 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001579 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001580
Bram Moolenaar17627312019-06-02 19:53:44 +02001581 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001582 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001583 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001584 redraw
1585 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001586 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001587 call feedkeys("eli\<Esc>", 'xt')
1588 call assert_equal(1, popup_getpos(winid).visible)
1589 call feedkeys("wi\<Esc>", 'xt')
1590 call assert_equal(1, popup_getpos(winid).visible)
1591 call feedkeys("Eli\<Esc>", 'xt')
1592 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001593 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001594
1595 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001596 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001597 redraw
1598 call assert_equal(1, popup_getpos(winid).visible)
1599 call feedkeys("eli\<Esc>", 'xt')
1600 call feedkeys("ei\<Esc>", 'xt')
1601 call assert_equal(1, popup_getpos(winid).visible)
1602 call feedkeys("eli\<Esc>", 'xt')
1603 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001604 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001605
1606 bwipe!
1607 call test_override('ALL', 0)
1608endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001609
1610func Test_notifications()
1611 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02001612 throw 'Skipped: timer feature not supported'
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001613 endif
1614 if !CanRunVimInTerminal()
1615 throw 'Skipped: cannot make screendumps'
1616 endif
1617
1618 call writefile([
1619 \ "call setline(1, range(1, 20))",
1620 \ "hi Notification ctermbg=lightblue",
1621 \ "call popup_notification('first notification', {})",
1622 \], 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001623 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001624 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1625
1626 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001627 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1628 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001629 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1630
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001631 " clean up
1632 call StopVimInTerminal(buf)
1633 call delete('XtestNotifications')
1634endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001635
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001636func Test_popup_scrollbar()
1637 if !CanRunVimInTerminal()
1638 throw 'Skipped: cannot make screendumps'
1639 endif
1640
1641 let lines =<< trim END
1642 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001643 hi ScrollThumb ctermbg=blue
1644 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001645 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001646 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001647 \ minwidth: 8,
1648 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001649 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001650 func ScrollUp()
1651 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1652 endfunc
1653 func ScrollDown()
1654 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1655 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001656 func ClickTop()
1657 call feedkeys("\<F4>\<LeftMouse>", "xt")
1658 endfunc
1659 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001660 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001661 call feedkeys("\<F5>\<LeftMouse>", "xt")
1662 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001663 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001664 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1665 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001666 END
1667 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001668 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001669 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1670
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001671 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001672 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001673 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1674
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001675 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001676 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001677 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1678
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001679 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001680 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001681 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1682
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001683 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb'})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001684 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1685 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1686
1687 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1688 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1689
1690 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001691 " wait a bit, otherwise it fails sometimes (double click recognized?)
1692 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001693 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1694 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1695
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001696 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1697 sleep 100m
1698 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1699 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1700
1701 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1702 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1703
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001704 " clean up
1705 call StopVimInTerminal(buf)
1706 call delete('XtestPopupScroll')
1707endfunc
1708
Bram Moolenaar437a7462019-07-05 20:17:22 +02001709func Test_popup_fitting_scrollbar()
1710 " this was causing a crash, divide by zero
1711 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001712 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001713 \ scrollbar: 1,
1714 \ maxwidth: 10,
1715 \ maxheight: 5,
1716 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001717 redraw
1718 call popup_clear()
1719endfunc
1720
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001721func Test_popup_settext()
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001722 if !CanRunVimInTerminal()
1723 throw 'Skipped: cannot make screendumps'
1724 endif
1725
1726 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001727 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001728 let p = popup_create('test', opts)
1729 call popup_settext(p, 'this is a text')
1730 END
1731
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001732 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001733 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001734 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1735
1736 " Setting to empty string clears it
1737 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1738 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1739
1740 " Setting a list
1741 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1742 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1743
1744 " Shrinking with a list
1745 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1746 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1747
1748 " Growing with a list
1749 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1750 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1751
1752 " Empty list clears
1753 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1754 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1755
1756 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001757 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001758 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1759
1760 " clean up
1761 call StopVimInTerminal(buf)
1762 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001763endfunc
1764
1765func Test_popup_hidden()
1766 new
1767
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001768 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001769 redraw
1770 call assert_equal(0, popup_getpos(winid).visible)
1771 call popup_close(winid)
1772
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001773 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001774 redraw
1775 call assert_equal(0, popup_getpos(winid).visible)
1776 call popup_close(winid)
1777
1778 func QuitCallback(id, res)
1779 let s:cb_winid = a:id
1780 let s:cb_res = a:res
1781 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001782 let winid = popup_dialog('make a choice', #{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001783 \ filter: 'popup_filter_yesno',
1784 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001785 \ })
1786 redraw
1787 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001788 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1789 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001790 exe "normal anot used by filter\<Esc>"
1791 call assert_equal('not used by filter', getline(1))
1792
1793 call popup_show(winid)
1794 call feedkeys('y', "xt")
1795 call assert_equal(1, s:cb_res)
1796
1797 bwipe!
1798 delfunc QuitCallback
1799endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001800
1801" Test options not checked elsewhere
1802func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001803 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001804 let options = popup_getoptions(winid)
1805 call assert_equal(1, options.wrap)
1806 call assert_equal(0, options.drag)
1807 call assert_equal('Beautiful', options.highlight)
1808
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001809 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001810 let options = popup_getoptions(winid)
1811 call assert_equal(0, options.wrap)
1812 call assert_equal(1, options.drag)
1813 call assert_equal('Another', options.highlight)
1814
1815 call popup_close(winid)
1816endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001817
1818func Test_popupwin_garbage_collect()
1819 func MyPopupFilter(x, winid, c)
1820 " NOP
1821 endfunc
1822
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001823 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001824 call test_garbagecollect_now()
1825 redraw
1826 " Must not crach caused by invalid memory access
1827 call feedkeys('j', 'xt')
1828 call assert_true(v:true)
1829
1830 call popup_close(winid)
1831 delfunc MyPopupFilter
1832endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001833
1834func Test_popupwin_with_buffer()
1835 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1836 let buf = bufadd('XsomeFile')
1837 call assert_equal(0, bufloaded(buf))
1838 let winid = popup_create(buf, {})
1839 call assert_notequal(0, winid)
1840 let pos = popup_getpos(winid)
1841 call assert_equal(2, pos.height)
1842 call assert_equal(1, bufloaded(buf))
1843 call popup_close(winid)
1844 call assert_equal({}, popup_getpos(winid))
1845 call assert_equal(1, bufloaded(buf))
1846 exe 'bwipe! ' .. buf
Bram Moolenaar7866b872019-07-01 22:21:01 +02001847
1848 edit test_popupwin.vim
1849 let winid = popup_create(bufnr(''), {})
1850 redraw
1851 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001852 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001853endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02001854
1855func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001856 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001857 \ maxwidth: 40,
1858 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02001859 \ })
1860 for top in range(1, 20)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001861 call popup_setoptions(winid, #{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02001862 redraw
1863 call assert_equal(19, popup_getpos(winid).width)
1864 endfor
1865 call popup_clear()
1866endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02001867
1868func Test_popupwin_buf_close()
1869 let buf = bufadd('Xtestbuf')
1870 call bufload(buf)
1871 call setbufline(buf, 1, ['just', 'some', 'lines'])
1872 let winid = popup_create(buf, {})
1873 redraw
1874 call assert_equal(3, popup_getpos(winid).height)
1875 let bufinfo = getbufinfo(buf)[0]
1876 call assert_equal(1, bufinfo.changed)
1877 call assert_equal(0, bufinfo.hidden)
1878 call assert_equal(0, bufinfo.listed)
1879 call assert_equal(1, bufinfo.loaded)
1880 call assert_equal([], bufinfo.windows)
1881 call assert_equal([winid], bufinfo.popups)
1882
1883 call popup_close(winid)
1884 call assert_equal({}, popup_getpos(winid))
1885 let bufinfo = getbufinfo(buf)[0]
1886 call assert_equal(1, bufinfo.changed)
1887 call assert_equal(1, bufinfo.hidden)
1888 call assert_equal(0, bufinfo.listed)
1889 call assert_equal(1, bufinfo.loaded)
1890 call assert_equal([], bufinfo.windows)
1891 call assert_equal([], bufinfo.popups)
1892 exe 'bwipe! ' .. buf
1893endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02001894
1895func Test_popup_menu_with_maxwidth()
1896 if !CanRunVimInTerminal()
1897 throw 'Skipped: cannot make screendumps'
1898 endif
1899
1900 let lines =<< trim END
1901 call setline(1, range(1, 10))
1902 hi ScrollThumb ctermbg=blue
1903 hi ScrollBar ctermbg=red
1904 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001905 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001906 \ maxwidth: 10,
1907 \ maxheight: 3,
1908 \ pos : 'topleft',
1909 \ col : a:col,
1910 \ line : a:line,
1911 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02001912 \ })
1913 endfunc
1914 call PopupMenu(['x'], 1, 1)
1915 call PopupMenu(['123456789|'], 1, 16)
1916 call PopupMenu(['123456789|' .. ' '], 7, 1)
1917 call PopupMenu([repeat('123456789|', 100)], 7, 16)
1918 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
1919 END
1920 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001921 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02001922 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
1923
1924 " close the menu popupwin.
1925 call term_sendkeys(buf, " ")
1926 call term_sendkeys(buf, " ")
1927 call term_sendkeys(buf, " ")
1928 call term_sendkeys(buf, " ")
1929 call term_sendkeys(buf, " ")
1930
1931 " clean up
1932 call StopVimInTerminal(buf)
1933 call delete('XtestPopupMenuMaxWidth')
1934endfunc
1935
Bram Moolenaara901a372019-07-13 16:38:50 +02001936func Test_popup_menu_with_scrollbar()
1937 if !CanRunVimInTerminal()
1938 throw 'Skipped: cannot make screendumps'
1939 endif
1940
1941 let lines =<< trim END
1942 call setline(1, range(1, 20))
1943 hi ScrollThumb ctermbg=blue
1944 hi ScrollBar ctermbg=red
1945 call popup_menu(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001946 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001947 \ minwidth: 8,
1948 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02001949 \ })
1950 END
1951 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001952 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02001953
1954 call term_sendkeys(buf, "j")
1955 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
1956
1957 call term_sendkeys(buf, "jjj")
1958 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
1959
1960 " if the cursor is the bottom line, it stays at the bottom line.
1961 call term_sendkeys(buf, repeat("j", 20))
1962 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
1963
1964 call term_sendkeys(buf, "kk")
1965 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
1966
1967 call term_sendkeys(buf, "k")
1968 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
1969
1970 " if the cursor is in the top line, it stays in the top line.
1971 call term_sendkeys(buf, repeat("k", 20))
1972 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
1973
1974 " close the menu popupwin.
1975 call term_sendkeys(buf, " ")
1976
1977 " clean up
1978 call StopVimInTerminal(buf)
1979 call delete('XtestPopupMenuScroll')
1980endfunc
1981
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001982func Test_popup_menu_filter()
1983 if !CanRunVimInTerminal()
1984 throw 'Skipped: cannot make screendumps'
1985 endif
1986
1987 let lines =<< trim END
1988 function! MyFilter(winid, key) abort
1989 if a:key == "0"
1990 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
1991 return 1
1992 endif
1993 if a:key == "G"
1994 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
1995 return 1
1996 endif
1997 if a:key == "j"
1998 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
1999 return 1
2000 endif
2001 if a:key == "k"
2002 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2003 return 1
2004 endif
2005 if a:key == 'x'
2006 call popup_close(a:winid)
2007 return 1
2008 endif
2009 return 0
2010 endfunction
2011 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2012 \ maxheight : 3,
2013 \ filter : 'MyFilter'
2014 \ })
2015 END
2016 call writefile(lines, 'XtestPopupMenuFilter')
2017 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2018
2019 call term_sendkeys(buf, "j")
2020 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2021
2022 call term_sendkeys(buf, "k")
2023 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2024
2025 call term_sendkeys(buf, "G")
2026 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2027
2028 call term_sendkeys(buf, "0")
2029 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2030
2031 call term_sendkeys(buf, "x")
2032
2033 " clean up
2034 call StopVimInTerminal(buf)
2035 call delete('XtestPopupMenuFilter')
2036endfunc
2037
2038func Test_popup_cursorline()
2039 if !CanRunVimInTerminal()
2040 throw 'Skipped: cannot make screendumps'
2041 endif
2042
2043 let winid = popup_create('some text', {})
2044 call assert_equal(0, popup_getoptions(winid).cursorline)
2045 call popup_close(winid)
2046
2047 let winid = popup_create('some text', #{ cursorline: 1, })
2048 call assert_equal(1, popup_getoptions(winid).cursorline)
2049 call popup_close(winid)
2050
2051 let winid = popup_create('some text', #{ cursorline: 0, })
2052 call assert_equal(0, popup_getoptions(winid).cursorline)
2053 call popup_close(winid)
2054
2055 let winid = popup_menu('some text', {})
2056 call assert_equal(1, popup_getoptions(winid).cursorline)
2057 call popup_close(winid)
2058
2059 let winid = popup_menu('some text', #{ cursorline: 1, })
2060 call assert_equal(1, popup_getoptions(winid).cursorline)
2061 call popup_close(winid)
2062
2063 let winid = popup_menu('some text', #{ cursorline: 0, })
2064 call assert_equal(0, popup_getoptions(winid).cursorline)
2065 call popup_close(winid)
2066
2067 " ---------
2068 " Pattern 1
2069 " ---------
2070 let lines =<< trim END
2071 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2072 END
2073 call writefile(lines, 'XtestPopupCursorLine')
2074 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2075 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2076 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2077 call StopVimInTerminal(buf)
2078
2079 " ---------
2080 " Pattern 2
2081 " ---------
2082 let lines =<< trim END
2083 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2084 END
2085 call writefile(lines, 'XtestPopupCursorLine')
2086 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2087 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2088 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2089 call StopVimInTerminal(buf)
2090
2091 " ---------
2092 " Pattern 3
2093 " ---------
2094 let lines =<< trim END
2095 function! MyFilter(winid, key) abort
2096 if a:key == "j"
2097 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2098 return 1
2099 endif
2100 if a:key == 'x'
2101 call popup_close(a:winid)
2102 return 1
2103 endif
2104 return 0
2105 endfunction
2106 call popup_menu(['111', '222', '333'], #{
2107 \ cursorline : 0,
2108 \ maxheight : 2,
2109 \ filter : 'MyFilter',
2110 \ })
2111 END
2112 call writefile(lines, 'XtestPopupCursorLine')
2113 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2114 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2115 call term_sendkeys(buf, "j")
2116 call term_sendkeys(buf, "j")
2117 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2118 call term_sendkeys(buf, "x")
2119 call StopVimInTerminal(buf)
2120
2121 " ---------
2122 " Pattern 4
2123 " ---------
2124 let lines =<< trim END
2125 function! MyFilter(winid, key) abort
2126 if a:key == "j"
2127 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2128 return 1
2129 endif
2130 if a:key == 'x'
2131 call popup_close(a:winid)
2132 return 1
2133 endif
2134 return 0
2135 endfunction
2136 call popup_menu(['111', '222', '333'], #{
2137 \ cursorline : 1,
2138 \ maxheight : 2,
2139 \ filter : 'MyFilter',
2140 \ })
2141 END
2142 call writefile(lines, 'XtestPopupCursorLine')
2143 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2144 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2145 call term_sendkeys(buf, "j")
2146 call term_sendkeys(buf, "j")
2147 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2148 call term_sendkeys(buf, "x")
2149 call StopVimInTerminal(buf)
2150
2151 call delete('XtestPopupCursorLine')
2152endfunc
2153
Bram Moolenaarf914a332019-07-20 15:09:56 +02002154func Test_previewpopup()
2155 if !CanRunVimInTerminal()
2156 throw 'Skipped: cannot make screendumps'
2157 endif
2158 call writefile([
2159 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2160 \ "another\tXtagfile\t/^this is another",
2161 \ "theword\tXtagfile\t/^theword"],
2162 \ 'Xtags')
2163 call writefile(range(1,20)
2164 \ + ['theword is here']
2165 \ + range(22, 27)
2166 \ + ['this is another place']
2167 \ + range(29, 40),
2168 \ "Xtagfile")
2169 let lines =<< trim END
2170 set tags=Xtags
2171 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002172 \ 'one',
2173 \ 'two',
2174 \ 'three',
2175 \ 'four',
2176 \ 'five',
2177 \ 'six',
2178 \ 'seven',
2179 \ 'find theword somewhere',
2180 \ 'nine',
2181 \ 'this is another word',
2182 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002183 set previewpopup=height:4,width:40
2184 END
2185 call writefile(lines, 'XtestPreviewPopup')
2186 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2187
2188 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2189 call term_sendkeys(buf, ":\<CR>")
2190 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2191
2192 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2193 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2194
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002195 call term_sendkeys(buf, ":call popup_move(popup_getpreview(), #{col: 15})\<CR>")
2196 call term_sendkeys(buf, ":\<CR>")
2197 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2198
2199 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2200 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2201
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002202 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2203 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
2204
Bram Moolenaarf914a332019-07-20 15:09:56 +02002205 call StopVimInTerminal(buf)
2206 call delete('Xtags')
2207 call delete('Xtagfile')
2208 call delete('XtestPreviewPopup')
2209endfunc
2210
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002211" vim: shiftwidth=2 sts=2