blob: 6033be68a59f8a8a92eba58b3383d5eefa502042 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature textprop
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
9 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020010 throw 'Skipped: cannot make screendumps'
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011 endif
Bram Moolenaare7eb9272019-06-24 00:58:07 +020012 let lines =<< trim END
13 call setline(1, range(1, 100))
14 hi PopupColor1 ctermbg=lightblue
15 hi PopupColor2 ctermbg=lightcyan
16 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020017 call prop_type_add('comment', #{highlight: 'Comment'})
18 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
19 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020020 call setwinvar(winid2, '&wincolor', 'PopupColor2')
21 END
22 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020023 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020024 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020029 \ .. "#{text: 'other tab'},"
30 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020031 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020033 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
35
36 " switch back to first tabpage
37 call term_sendkeys(buf, "gt")
38 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
39
40 " close that tabpage
41 call term_sendkeys(buf, ":quit!\<CR>")
42 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
43
Bram Moolenaar202d9822019-06-11 21:56:30 +020044 " set 'columns' to a small value, size must be recomputed
45 call term_sendkeys(buf, ":let cols = &columns\<CR>")
46 call term_sendkeys(buf, ":set columns=12\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
48 call term_sendkeys(buf, ":let &columns = cols\<CR>")
49
Bram Moolenaar17146962019-05-30 00:12:11 +020050 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020051 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020052 call term_sendkeys(buf, ":redraw\<CR>")
53 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
54
Bram Moolenaar17146962019-05-30 00:12:11 +020055 " show not fitting line at bottom
56 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
57 call term_sendkeys(buf, ":redraw\<CR>")
58 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
59
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020060 " move popup over ruler
61 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020062 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020063 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020064 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
65
66 " clear all popups after moving the cursor a bit, so that ruler is updated
67 call term_sendkeys(buf, "axxx\<Esc>")
68 call term_wait(buf)
69 call term_sendkeys(buf, "0")
70 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020071 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020072 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
73
Bram Moolenaar4d784b22019-05-25 19:51:39 +020074 " clean up
75 call StopVimInTerminal(buf)
76 call delete('XtestPopup')
77endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020078
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020079func Test_popup_with_border_and_padding()
80 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020081 throw 'Skipped: cannot make screendumps'
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082 endif
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020083
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020084 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020085 let lines =<< trim END
86 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020087 call popup_create('hello border', #{line: 2, col: 3, border: []})
88 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
89 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: []})
90 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
91 call popup_create('paddings', #{line: 6, col: 23, padding: [1, 3, 2, 4]})
92 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
93 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020094 END
95 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
96 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020097 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020098 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
99
100 call StopVimInTerminal(buf)
101 call delete('XtestPopupBorder')
102 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200103
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200104 let lines =<< trim END
105 call setline(1, range(1, 100))
106 hi BlueColor ctermbg=lightblue
107 hi TopColor ctermbg=253
108 hi RightColor ctermbg=245
109 hi BottomColor ctermbg=240
110 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200111 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
112 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
113 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
114 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
115 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
116 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200117 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200118 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200119 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200120 END
121 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200122 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200123 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
124
Bram Moolenaarad24a712019-06-17 20:05:45 +0200125 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200126 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200127 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
128
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200129 " check multi-byte border only with 'ambiwidth' single
130 if &ambiwidth == 'single'
131 call term_sendkeys(buf, ":call MultiByte()\<CR>")
132 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
133 endif
134
Bram Moolenaar790498b2019-06-01 22:15:29 +0200135 call StopVimInTerminal(buf)
136 call delete('XtestPopupBorder')
137
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200138 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200139 \ line: 2,
140 \ core_line: 3,
141 \ col: 3,
142 \ core_col: 4,
143 \ width: 14,
144 \ core_width: 12,
145 \ height: 3,
146 \ core_height: 1,
147 \ firstline: 1,
148 \ scrollbar: 0,
149 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200150 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200151 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200152 let options = popup_getoptions(winid)
153 call assert_equal([], options.border)
154 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200155
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200156 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200157 let with_border_or_padding.width = 15
158 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200159 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200160 let options = popup_getoptions(winid)
161 call assert_false(has_key(options, "border"))
162 call assert_equal([], options.padding)
163
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200164 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200165 \ padding: [1, 2, 3, 4],
166 \ border: [4, 0, 7, 8],
167 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
168 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200169 \ })
170 let options = popup_getoptions(winid)
171 call assert_equal([1, 0, 1, 1], options.border)
172 call assert_equal([1, 2, 3, 4], options.padding)
173 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
174 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200175
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200176 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
177 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200178 \ line: 3,
179 \ core_line: 5,
180 \ col: 8,
181 \ core_col: 10,
182 \ width: 14,
183 \ core_width: 10,
184 \ height: 5,
185 \ scrollbar: 0,
186 \ core_height: 1,
187 \ firstline: 1,
188 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200189
190 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200191endfunc
192
Bram Moolenaarb4230122019-05-30 18:40:53 +0200193func Test_popup_with_syntax_win_execute()
194 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200195 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200196 endif
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200197 let lines =<< trim END
198 call setline(1, range(1, 100))
199 hi PopupColor ctermbg=lightblue
200 let winid = popup_create([
201 \ '#include <stdio.h>',
202 \ 'int main(void)',
203 \ '{',
204 \ ' printf(123);',
205 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200206 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200207 call win_execute(winid, 'set syntax=cpp')
208 END
209 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200210 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200211 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
212
213 " clean up
214 call StopVimInTerminal(buf)
215 call delete('XtestPopup')
216endfunc
217
218func Test_popup_with_syntax_setbufvar()
219 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200220 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200221 endif
Bram Moolenaar402502d2019-05-30 22:07:36 +0200222 let lines =<< trim END
223 call setline(1, range(1, 100))
224 hi PopupColor ctermbg=lightgrey
225 let winid = popup_create([
226 \ '#include <stdio.h>',
227 \ 'int main(void)',
228 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200229 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200230 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200231 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200232 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
233 END
234 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200235 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200236 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
237
238 " clean up
239 call StopVimInTerminal(buf)
240 call delete('XtestPopup')
241endfunc
242
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200243func Test_popup_with_matches()
244 if !CanRunVimInTerminal()
245 throw 'Skipped: cannot make screendumps'
246 endif
247 let lines =<< trim END
248 call setline(1, ['111 222 333', '444 555 666'])
249 let winid = popup_create([
250 \ '111 222 333',
251 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200252 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200253 set hlsearch
254 /666
255 call matchadd('ErrorMsg', '111')
256 call matchadd('ErrorMsg', '444')
257 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
258 call win_execute(winid, "call matchadd('ErrorMsg', '555')")
259 END
260 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200261 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200262 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
263
264 " clean up
265 call StopVimInTerminal(buf)
266 call delete('XtestPopupMatches')
267endfunc
268
Bram Moolenaar399d8982019-06-02 15:34:29 +0200269func Test_popup_all_corners()
270 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200271 throw 'Skipped: cannot make screendumps'
Bram Moolenaar399d8982019-06-02 15:34:29 +0200272 endif
273 let lines =<< trim END
274 call setline(1, repeat([repeat('-', 60)], 15))
275 set so=0
276 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200277 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200278 \ line: 'cursor+1',
279 \ col: 'cursor',
280 \ pos: 'topleft',
281 \ border: [],
282 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200283 \ })
284 normal 25|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200285 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200286 \ line: 'cursor+1',
287 \ col: 'cursor',
288 \ pos: 'topright',
289 \ border: [],
290 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200291 \ })
292 normal 9G29|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200293 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200294 \ line: 'cursor-1',
295 \ col: 'cursor',
296 \ pos: 'botleft',
297 \ border: [],
298 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200299 \ })
300 normal 51|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200301 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200302 \ line: 'cursor-1',
303 \ col: 'cursor',
304 \ pos: 'botright',
305 \ border: [],
306 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200307 \ })
308 END
309 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200310 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200311 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
312
313 " clean up
314 call StopVimInTerminal(buf)
315 call delete('XtestPopupCorners')
316endfunc
317
Bram Moolenaar8d241042019-06-12 23:40:01 +0200318func Test_popup_firstline()
319 if !CanRunVimInTerminal()
320 throw 'Skipped: cannot make screendumps'
321 endif
322 let lines =<< trim END
323 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200324 call popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200325 \ maxheight: 4,
326 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200327 \ })
328 END
329 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200330 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200331 call VerifyScreenDump(buf, 'Test_popupwin_firstline', {})
332
333 " clean up
334 call StopVimInTerminal(buf)
335 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200336
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200337 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200338 \ maxheight: 2,
339 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200340 \ })
341 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200342 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200343 call assert_equal(1, popup_getoptions(winid).firstline)
344
345 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200346endfunc
347
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200348func Test_popup_drag()
349 if !CanRunVimInTerminal()
350 throw 'Skipped: cannot make screendumps'
351 endif
352 " create a popup that covers the command line
353 let lines =<< trim END
354 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200355 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200356 \ drag: 1,
357 \ border: [],
358 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200359 \ })
360 func Dragit()
361 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
362 endfunc
363 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
364 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2)<CR>
365 END
366 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200367 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200368 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
369
370 call term_sendkeys(buf, ":call Dragit()\<CR>")
371 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
372
373 " clean up
374 call StopVimInTerminal(buf)
375 call delete('XtestPopupDrag')
376endfunc
377
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200378func Test_popup_close_with_mouse()
379 if !CanRunVimInTerminal()
380 throw 'Skipped: cannot make screendumps'
381 endif
382 let lines =<< trim END
383 call setline(1, range(1, 20))
384 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200385 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200386 \ close: 'button',
387 \ border: [],
388 \ line: 1,
389 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200390 \ })
391 func CloseMsg(id, result)
392 echomsg 'Popup closed with ' .. a:result
393 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200394 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200395 \ close: 'click',
396 \ line: 3,
397 \ col: 15,
398 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200399 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200400 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200401 \ close: 'button',
402 \ line: 5,
403 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200404 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200405 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200406 \ close: 'button',
407 \ padding: [],
408 \ line: 5,
409 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200410 \ })
411 func CloseWithX()
412 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
413 endfunc
414 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
415 func CloseWithClick()
416 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
417 endfunc
418 map <silent> <F4> :call test_setmouse(3, 17)<CR>
419 END
420 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200421 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200422 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
423
424 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
425 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
426
427 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
428 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
429
430 " clean up
431 call StopVimInTerminal(buf)
432 call delete('XtestPopupClose')
433endfunction
434
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200435func Test_popup_with_mask()
436 if !CanRunVimInTerminal()
437 throw 'Skipped: cannot make screendumps'
438 endif
439 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200440 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200441 hi PopupColor ctermbg=lightgrey
442 let winid = popup_create([
443 \ 'some text',
444 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200445 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200446 \ line: 1,
447 \ col: 10,
448 \ wrap: 0,
449 \ fixed: 1,
450 \ zindex: 90,
451 \ padding: [],
452 \ highlight: 'PopupColor',
453 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200454 call popup_create([
455 \ 'xxxxxxxxx',
456 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200457 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200458 \ line: 3,
459 \ col: 18,
460 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200461 let winidb = popup_create([
462 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200463 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200464 \ line: 7,
465 \ col: 10,
466 \ wrap: 0,
467 \ fixed: 1,
468 \ close: 'button',
469 \ zindex: 90,
470 \ padding: [],
471 \ border: [],
472 \ mask: [[1,2,1,1], [-5,-1,4,4], [7,9,2,3], [3,5,5,5],[-7,-4,5,5]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200473 END
474 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200475 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200476 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
477
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200478 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
479 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200480 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200481 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
482
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200483 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
484 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200485 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200486 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
487
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200488 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
489 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200490 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200491 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
492
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200493 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
494 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200495 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200496 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
497
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200498 " clean up
499 call StopVimInTerminal(buf)
500 call delete('XtestPopupMask')
501endfunc
502
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200503func Test_popup_select()
504 if !CanRunVimInTerminal()
505 throw 'Skipped: cannot make screendumps'
506 endif
Bram Moolenaar650a6372019-06-15 00:29:33 +0200507 if !has('clipboard')
508 throw 'Skipped: clipboard feature missing'
509 endif
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200510 " create a popup with some text to be selected
511 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200512 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200513 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200514 let winid = popup_create(['the word', 'some more', 'several words here'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200515 \ drag: 1,
516 \ border: [],
517 \ line: 3,
518 \ col: 10,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200519 \ })
520 func Select1()
521 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
522 endfunc
523 map <silent> <F3> :call test_setmouse(4, 15)<CR>
524 map <silent> <F4> :call test_setmouse(6, 23)<CR>
525 END
526 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200527 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200528 call term_sendkeys(buf, ":call Select1()\<CR>")
529 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
530
531 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
532 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200533 " clean the command line, sometimes it still shows a command
534 call term_sendkeys(buf, ":\<esc>")
535
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200536 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
537
538 " clean up
539 call StopVimInTerminal(buf)
540 call delete('XtestPopupSelect')
541endfunc
542
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200543func Test_popup_in_tab()
544 " default popup is local to tab, not visible when in other tab
545 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200546 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200547 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200548 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200549 tabnew
550 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200551 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200552 quit
553 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200554
555 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200556 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200557 " buffer is gone now
558 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200559
560 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200561 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200562 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200563 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200564 tabnew
565 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200566 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200567 quit
568 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200569 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200570
571 " create popup in other tab
572 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200573 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200574 call assert_equal(0, popup_getpos(winid).visible)
575 call assert_equal(1, popup_getoptions(winid).tabpage)
576 quit
577 call assert_equal(1, popup_getpos(winid).visible)
578 call assert_equal(0, popup_getoptions(winid).tabpage)
579 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200580endfunc
581
582func Test_popup_valid_arguments()
583 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200584 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200585 let pos = popup_getpos(winid)
586 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200587 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200588
589 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200590 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200591 let pos = popup_getpos(winid)
592 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200593 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200594
595 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200596 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200597 let pos = popup_getpos(winid)
598 let around = (&columns - pos.width) / 2
599 call assert_inrange(around - 1, around + 1, pos.col)
600 let around = (&lines - pos.height) / 2
601 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200602 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200603endfunc
604
605func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200606 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200607 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200608 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200609 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200610
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200611 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200612 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200613 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200614 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200615 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200616 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200617 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200618 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200619
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200620 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200621 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200622 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200623 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200624 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200625 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200626 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200627 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200628
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200629 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200630 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200631 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200632 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200633 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200634 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200635 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200636 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200637 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
638 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200639 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200640 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200641
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200642 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200643 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200644 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200645 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200646 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200647 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200648 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
649 call popup_clear()
650 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
651 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200652endfunc
653
Bram Moolenaareea16992019-05-31 17:34:48 +0200654func Test_win_execute_closing_curwin()
655 split
656 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200657 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200658 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200659endfunc
660
661func Test_win_execute_not_allowed()
662 let winid = popup_create('some text', {})
663 call assert_fails('call win_execute(winid, "split")', 'E994:')
664 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
665 call assert_fails('call win_execute(winid, "close")', 'E994:')
666 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200667 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200668 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
669 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
670 call assert_fails('call win_execute(winid, "next")', 'E994:')
671 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
672 call assert_fails('call win_execute(winid, "buf")', 'E994:')
673 call assert_fails('call win_execute(winid, "edit")', 'E994:')
674 call assert_fails('call win_execute(winid, "enew")', 'E994:')
675 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
676 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
677 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
678 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200679 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200680endfunc
681
Bram Moolenaar402502d2019-05-30 22:07:36 +0200682func Test_popup_with_wrap()
683 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200684 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200685 endif
686 let lines =<< trim END
687 call setline(1, range(1, 100))
688 let winid = popup_create(
689 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200690 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200691 END
692 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200693 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200694 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
695
696 " clean up
697 call StopVimInTerminal(buf)
698 call delete('XtestPopup')
699endfunc
700
701func Test_popup_without_wrap()
702 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200703 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200704 endif
705 let lines =<< trim END
706 call setline(1, range(1, 100))
707 let winid = popup_create(
708 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200709 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200710 END
711 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200712 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200713 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
714
715 " clean up
716 call StopVimInTerminal(buf)
717 call delete('XtestPopup')
718endfunc
719
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200720func Test_popup_with_showbreak()
721 if !CanRunVimInTerminal()
722 throw 'Skipped: cannot make screendumps'
723 endif
724 let lines =<< trim END
725 set showbreak=>>\
726 call setline(1, range(1, 20))
727 let winid = popup_dialog(
728 \ 'a long line here',
729 \ #{filter: 'popup_filter_yesno'})
730 END
731 call writefile(lines, 'XtestPopupShowbreak')
732 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
733 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
734
735 " clean up
736 call term_sendkeys(buf, "y")
737 call StopVimInTerminal(buf)
738 call delete('XtestPopupShowbreak')
739endfunc
740
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200741func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200742 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +0200743 throw 'Skipped: timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200744 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200745 topleft vnew
746 call setline(1, 'hello')
747
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200748 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200749 \ line: 1,
750 \ col: 1,
751 \ minwidth: 20,
752 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200753 \})
754 redraw
755 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
756 call assert_equal('world', line)
757
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200758 call assert_equal(winid, popup_locate(1, 1))
759 call assert_equal(winid, popup_locate(1, 20))
760 call assert_equal(0, popup_locate(1, 21))
761 call assert_equal(0, popup_locate(2, 1))
762
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200763 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200764 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200765 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
766 call assert_equal('hello', line)
767
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200768 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200769 \ line: &lines,
770 \ col: 10,
771 \ minwidth: 20,
772 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200773 \})
774 redraw
775 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
776 call assert_match('.*on the command line.*', line)
777
778 sleep 700m
779 redraw
780 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
781 call assert_notmatch('.*on the command line.*', line)
782
783 bwipe!
784endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200785
786func Test_popup_hide()
787 topleft vnew
788 call setline(1, 'hello')
789
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200790 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200791 \ line: 1,
792 \ col: 1,
793 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200794 \})
795 redraw
796 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
797 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200798 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200799 " buffer is still listed and active
800 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200801
802 call popup_hide(winid)
803 redraw
804 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
805 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200806 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200807 " buffer is still listed but hidden
808 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200809
810 call popup_show(winid)
811 redraw
812 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
813 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200814 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200815
816
817 call popup_close(winid)
818 redraw
819 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
820 call assert_equal('hello', line)
821
822 " error is given for existing non-popup window
823 call assert_fails('call popup_hide(win_getid())', 'E993:')
824
825 " no error non-existing window
826 call popup_hide(1234234)
827 call popup_show(41234234)
828
829 bwipe!
830endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200831
832func Test_popup_move()
833 topleft vnew
834 call setline(1, 'hello')
835
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200836 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200837 \ line: 1,
838 \ col: 1,
839 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200840 \})
841 redraw
842 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
843 call assert_equal('world ', line)
844
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200845 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200846 redraw
847 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
848 call assert_equal('hello ', line)
849 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
850 call assert_equal('~world', line)
851
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200852 call popup_move(winid, #{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200853 redraw
854 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
855 call assert_equal('hworld', line)
856
857 call popup_close(winid)
858
859 bwipe!
860endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200861
Bram Moolenaar402502d2019-05-30 22:07:36 +0200862func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200863 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200864 \ line: 2,
865 \ col: 3,
866 \ minwidth: 10,
867 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200868 \})
869 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200870 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200871 call assert_equal(2, res.line)
872 call assert_equal(3, res.col)
873 call assert_equal(10, res.width)
874 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200875 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200876
877 call popup_close(winid)
878endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200879
880func Test_popup_width_longest()
881 let tests = [
882 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
883 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
884 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
885 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
886 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
887 \ ]
888
889 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200890 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200891 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200892 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200893 call assert_equal(test[1], position.width)
894 call popup_close(winid)
895 endfor
896endfunc
897
898func Test_popup_wraps()
899 let tests = [
900 \ ['nowrap', 6, 1],
901 \ ['a line that wraps once', 12, 2],
902 \ ['a line that wraps two times', 12, 3],
903 \ ]
904 for test in tests
905 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200906 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200907 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200908 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200909 call assert_equal(test[1], position.width)
910 call assert_equal(test[2], position.height)
911
912 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200913 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200914 endfor
915endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200916
917func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200918 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200919 \ line: 2,
920 \ col: 3,
921 \ minwidth: 10,
922 \ minheight: 11,
923 \ maxwidth: 20,
924 \ maxheight: 21,
925 \ zindex: 100,
926 \ time: 5000,
927 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200928 \})
929 redraw
930 let res = popup_getoptions(winid)
931 call assert_equal(2, res.line)
932 call assert_equal(3, res.col)
933 call assert_equal(10, res.minwidth)
934 call assert_equal(11, res.minheight)
935 call assert_equal(20, res.maxwidth)
936 call assert_equal(21, res.maxheight)
937 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200938 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200939 if has('timers')
940 call assert_equal(5000, res.time)
941 endif
942 call popup_close(winid)
943
944 let winid = popup_create('hello', {})
945 redraw
946 let res = popup_getoptions(winid)
947 call assert_equal(0, res.line)
948 call assert_equal(0, res.col)
949 call assert_equal(0, res.minwidth)
950 call assert_equal(0, res.minheight)
951 call assert_equal(0, res.maxwidth)
952 call assert_equal(0, res.maxheight)
953 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200954 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200955 if has('timers')
956 call assert_equal(0, res.time)
957 endif
958 call popup_close(winid)
959 call assert_equal({}, popup_getoptions(winid))
960endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200961
962func Test_popup_option_values()
963 new
964 " window-local
965 setlocal number
966 setlocal nowrap
967 " buffer-local
968 setlocal omnifunc=Something
969 " global/buffer-local
970 setlocal path=/there
971 " global/window-local
972 setlocal scrolloff=9
973
974 let winid = popup_create('hello', {})
975 call assert_equal(0, getwinvar(winid, '&number'))
976 call assert_equal(1, getwinvar(winid, '&wrap'))
977 call assert_equal('', getwinvar(winid, '&omnifunc'))
978 call assert_equal(&g:path, getwinvar(winid, '&path'))
979 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
980
981 call popup_close(winid)
982 bwipe
983endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200984
985func Test_popup_atcursor()
986 topleft vnew
987 call setline(1, [
988 \ 'xxxxxxxxxxxxxxxxx',
989 \ 'xxxxxxxxxxxxxxxxx',
990 \ 'xxxxxxxxxxxxxxxxx',
991 \])
992
993 call cursor(2, 2)
994 redraw
995 let winid = popup_atcursor('vim', {})
996 redraw
997 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
998 call assert_equal('xvimxxxxxxxxxxxxx', line)
999 call popup_close(winid)
1000
1001 call cursor(3, 4)
1002 redraw
1003 let winid = popup_atcursor('vim', {})
1004 redraw
1005 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1006 call assert_equal('xxxvimxxxxxxxxxxx', line)
1007 call popup_close(winid)
1008
1009 call cursor(1, 1)
1010 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001011 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001012 \ line: 'cursor+2',
1013 \ col: 'cursor+1',
1014 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001015 redraw
1016 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1017 call assert_equal('xvimxxxxxxxxxxxxx', line)
1018 call popup_close(winid)
1019
1020 call cursor(3, 3)
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(1, v:val)'), '')
1028 call assert_equal('xvimxxxxxxxxxxxxx', line)
1029 call popup_close(winid)
1030
Bram Moolenaar402502d2019-05-30 22:07:36 +02001031 " just enough room above
1032 call cursor(3, 3)
1033 redraw
1034 let winid = popup_atcursor(['vim', 'is great'], {})
1035 redraw
1036 let pos = popup_getpos(winid)
1037 call assert_equal(1, pos.line)
1038 call popup_close(winid)
1039
1040 " not enough room above, popup goes below the cursor
1041 call cursor(3, 3)
1042 redraw
1043 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1044 redraw
1045 let pos = popup_getpos(winid)
1046 call assert_equal(4, pos.line)
1047 call popup_close(winid)
1048
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001049 " cursor in first line, popup in line 2
1050 call cursor(1, 1)
1051 redraw
1052 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1053 redraw
1054 let pos = popup_getpos(winid)
1055 call assert_equal(2, pos.line)
1056 call popup_close(winid)
1057
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001058 bwipe!
1059endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001060
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001061func Test_popup_beval()
1062 if !CanRunVimInTerminal()
1063 throw 'Skipped: cannot make screendumps'
1064 endif
1065
1066 let lines =<< trim END
1067 call setline(1, range(1, 20))
1068 call setline(5, 'here is some text to hover over')
1069 set balloonevalterm
1070 set balloonexpr=BalloonExpr()
1071 set balloondelay=100
1072 func BalloonExpr()
1073 let s:winid = popup_beval([v:beval_text], {})
1074 return ''
1075 endfunc
1076 func Hover()
1077 call test_setmouse(5, 15)
1078 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1079 sleep 100m
1080 endfunc
1081 func MoveOntoPopup()
1082 call test_setmouse(4, 17)
1083 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1084 endfunc
1085 func MoveAway()
1086 call test_setmouse(5, 13)
1087 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1088 endfunc
1089 END
1090 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001091 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001092 call term_wait(buf, 100)
1093 call term_sendkeys(buf, 'j')
1094 call term_sendkeys(buf, ":call Hover()\<CR>")
1095 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1096
1097 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1098 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1099
1100 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1101 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1102
1103 " clean up
1104 call StopVimInTerminal(buf)
1105 call delete('XtestPopupBeval')
1106endfunc
1107
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001108func Test_popup_filter()
1109 new
1110 call setline(1, 'some text')
1111
1112 func MyPopupFilter(winid, c)
1113 if a:c == 'e'
1114 let g:eaten = 'e'
1115 return 1
1116 endif
1117 if a:c == '0'
1118 let g:ignored = '0'
1119 return 0
1120 endif
1121 if a:c == 'x'
1122 call popup_close(a:winid)
1123 return 1
1124 endif
1125 return 0
1126 endfunc
1127
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001128 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001129 redraw
1130
1131 " e is consumed by the filter
1132 call feedkeys('e', 'xt')
1133 call assert_equal('e', g:eaten)
1134
1135 " 0 is ignored by the filter
1136 normal $
1137 call assert_equal(9, getcurpos()[2])
1138 call feedkeys('0', 'xt')
1139 call assert_equal('0', g:ignored)
1140 call assert_equal(1, getcurpos()[2])
1141
1142 " x closes the popup
1143 call feedkeys('x', 'xt')
1144 call assert_equal('e', g:eaten)
1145 call assert_equal(-1, winbufnr(winid))
1146
1147 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001148 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001149endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001150
Bram Moolenaara42d9452019-06-15 21:46:30 +02001151func ShowDialog(key, result)
1152 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001153 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001154 \ filter: 'popup_filter_yesno',
1155 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001156 \ })
1157 redraw
1158 call feedkeys(a:key, "xt")
1159 call assert_equal(winid, s:cb_winid)
1160 call assert_equal(a:result, s:cb_res)
1161endfunc
1162
1163func Test_popup_dialog()
1164 func QuitCallback(id, res)
1165 let s:cb_winid = a:id
1166 let s:cb_res = a:res
1167 endfunc
1168
1169 let winid = ShowDialog("y", 1)
1170 let winid = ShowDialog("Y", 1)
1171 let winid = ShowDialog("n", 0)
1172 let winid = ShowDialog("N", 0)
1173 let winid = ShowDialog("x", 0)
1174 let winid = ShowDialog("X", 0)
1175 let winid = ShowDialog("\<Esc>", 0)
1176 let winid = ShowDialog("\<C-C>", -1)
1177
1178 delfunc QuitCallback
1179endfunc
1180
Bram Moolenaara730e552019-06-16 19:05:31 +02001181func ShowMenu(key, result)
1182 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001183 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001184 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001185 \ })
1186 redraw
1187 call feedkeys(a:key, "xt")
1188 call assert_equal(winid, s:cb_winid)
1189 call assert_equal(a:result, s:cb_res)
1190endfunc
1191
1192func Test_popup_menu()
1193 func QuitCallback(id, res)
1194 let s:cb_winid = a:id
1195 let s:cb_res = a:res
1196 endfunc
1197
1198 let winid = ShowMenu(" ", 1)
1199 let winid = ShowMenu("j \<CR>", 2)
1200 let winid = ShowMenu("JjK \<CR>", 2)
1201 let winid = ShowMenu("jjjjjj ", 3)
1202 let winid = ShowMenu("kkk ", 1)
1203 let winid = ShowMenu("x", -1)
1204 let winid = ShowMenu("X", -1)
1205 let winid = ShowMenu("\<Esc>", -1)
1206 let winid = ShowMenu("\<C-C>", -1)
1207
1208 delfunc QuitCallback
1209endfunc
1210
1211func Test_popup_menu_screenshot()
1212 if !CanRunVimInTerminal()
1213 throw 'Skipped: cannot make screendumps'
1214 endif
1215
1216 let lines =<< trim END
1217 call setline(1, range(1, 20))
1218 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001219 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001220 func MenuDone(id, res)
1221 echomsg "selected " .. a:res
1222 endfunc
1223 END
1224 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001225 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001226 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1227
1228 call term_sendkeys(buf, "jj")
1229 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1230
1231 call term_sendkeys(buf, " ")
1232 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1233
1234 " clean up
1235 call StopVimInTerminal(buf)
1236 call delete('XtestPopupMenu')
1237endfunc
1238
Bram Moolenaarf914a332019-07-20 15:09:56 +02001239func Test_popup_menu_narrow()
1240 if !CanRunVimInTerminal()
1241 throw 'Skipped: cannot make screendumps'
1242 endif
1243
1244 let lines =<< trim END
1245 call setline(1, range(1, 20))
1246 hi PopupSelected ctermbg=green
1247 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1248 func MenuDone(id, res)
1249 echomsg "selected " .. a:res
1250 endfunc
1251 END
1252 call writefile(lines, 'XtestPopupNarrowMenu')
1253 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1254 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1255
1256 " clean up
1257 call term_sendkeys(buf, "x")
1258 call StopVimInTerminal(buf)
1259 call delete('XtestPopupNarrowMenu')
1260endfunc
1261
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001262func Test_popup_title()
1263 if !CanRunVimInTerminal()
1264 throw 'Skipped: cannot make screendumps'
1265 endif
1266
1267 " Create a popup without title or border, a line of padding will be added to
1268 " put the title on.
1269 let lines =<< trim END
1270 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001271 call popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001272 END
1273 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001274 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001275 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1276
1277 " clean up
1278 call StopVimInTerminal(buf)
1279 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001280
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001281 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001282 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001283 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001284 call assert_equal('Another Title', popup_getoptions(winid).title)
1285
1286 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001287endfunc
1288
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001289func Test_popup_close_callback()
1290 func PopupDone(id, result)
1291 let g:result = a:result
1292 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001293 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001294 redraw
1295 call popup_close(winid, 'done')
1296 call assert_equal('done', g:result)
1297endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001298
1299func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001300 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001301 redraw
1302 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001303 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001304 call assert_equal(5, pos.height)
1305
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001306 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001307 redraw
1308 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001309 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001310 call assert_equal(3, pos.height)
1311endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001312
1313func Test_popup_never_behind()
1314 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001315 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +02001316 endif
1317 " +-----------------------------+
1318 " | | |
1319 " | | |
1320 " | | |
1321 " | line1 |
1322 " |------------line2------------|
1323 " | line3 |
1324 " | line4 |
1325 " | |
1326 " | |
1327 " +-----------------------------+
1328 let lines =<< trim END
1329 only
1330 split
1331 vsplit
1332 let info_window1 = getwininfo()[0]
1333 let line = info_window1['height']
1334 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001335 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001336 \ line : line,
1337 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001338 \ })
1339 END
1340 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001341 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001342 call term_sendkeys(buf, "\<C-W>w")
1343 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1344
1345 " clean up
1346 call StopVimInTerminal(buf)
1347 call delete('XtestPopupBehind')
1348endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001349
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001350func s:VerifyPosition(p, msg, line, col, width, height)
1351 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1352 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1353 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1354 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001355endfunc
1356
1357func Test_popup_position_adjust()
1358 " Anything placed past 2 cells from of the right of the screen is moved to the
1359 " left.
1360 "
1361 " When wrapping is disabled, we also shift to the left to display on the
1362 " screen, unless fixed is set.
1363
1364 " Entries for cases which don't vary based on wrapping.
1365 " Format is per tests described below
1366 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001367 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1368 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1369 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1370 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1371 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1372 \
1373 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1374 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1375 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1376 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1377 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1378 \
1379 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1380 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1381 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1382 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1383 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1384 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001385
1386 " these test groups are dicts with:
1387 " - comment: something to identify the group of tests by
1388 " - options: dict of options to merge with the row/col in tests
1389 " - tests: list of cases. Each one is a list with elements:
1390 " - text
1391 " - row
1392 " - col
1393 " - expected row
1394 " - expected col
1395 " - expected width
1396 " - expected height
1397 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001398 \ #{
1399 \ comment: 'left-aligned with wrapping',
1400 \ options: #{
1401 \ wrap: 1,
1402 \ pos: 'botleft',
1403 \ },
1404 \ tests: both_wrap_tests + [
1405 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1406 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1407 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1408 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1409 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1410 \ ],
1411 \ },
1412 \ #{
1413 \ comment: 'left aligned without wrapping',
1414 \ options: #{
1415 \ wrap: 0,
1416 \ pos: 'botleft',
1417 \ },
1418 \ tests: both_wrap_tests + [
1419 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1420 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1421 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1422 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1423 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1424 \ ],
1425 \ },
1426 \ #{
1427 \ comment: 'left aligned with fixed position',
1428 \ options: #{
1429 \ wrap: 0,
1430 \ fixed: 1,
1431 \ pos: 'botleft',
1432 \ },
1433 \ tests: both_wrap_tests + [
1434 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1435 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1436 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1437 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1438 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1439 \ ],
1440 \ },
1441 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001442
1443 for test_group in tests
1444 for test in test_group.tests
1445 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001446 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001447 \ line: line,
1448 \ col: col,
1449 \ }
1450 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001451
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001452 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001453
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001454 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001455 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1456 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001457 endfor
1458 endfor
1459
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001460 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001461 %bwipe!
1462endfunc
1463
Bram Moolenaar3397f742019-06-02 18:40:06 +02001464func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001465 " width of screen
1466 let X = join(map(range(&columns), {->'X'}), '')
1467
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001468 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1469 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001470
1471 redraw
1472 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1473 call assert_equal(X, line)
1474
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001475 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001476 redraw
1477
1478 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001479 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1480 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001481
1482 redraw
1483 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1484 call assert_equal(X, line)
1485
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001486 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001487 redraw
1488
1489 " Extend so > window width
1490 let X .= 'x'
1491
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001492 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1493 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001494
1495 redraw
1496 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1497 call assert_equal(X[ : -2 ], line)
1498
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001499 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001500 redraw
1501
1502 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001503 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1504 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001505
1506 redraw
1507 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1508 call assert_equal(X[ : -2 ], line)
1509
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001510 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001511 redraw
1512
1513 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001514 let p = popup_create(X,
1515 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1516 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001517
1518 redraw
1519 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1520 let e_line = ' ' . X[ 1 : -2 ]
1521 call assert_equal(e_line, line)
1522
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001523 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001524 redraw
1525
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001526 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001527 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001528endfunc
1529
1530func Test_popup_moved()
1531 new
1532 call test_override('char_avail', 1)
1533 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1534
1535 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001536 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001537 redraw
1538 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001539 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001540 " trigger the check for last_cursormoved by going into insert mode
1541 call feedkeys("li\<Esc>", 'xt')
1542 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001543 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001544
1545 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001546 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001547 redraw
1548 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001549 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001550 call feedkeys("hi\<Esc>", 'xt')
1551 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001552 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001553
1554 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001555 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001556 redraw
1557 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001558 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001559 call feedkeys("li\<Esc>", 'xt')
1560 call assert_equal(1, popup_getpos(winid).visible)
1561 call feedkeys("ei\<Esc>", 'xt')
1562 call assert_equal(1, popup_getpos(winid).visible)
1563 call feedkeys("eli\<Esc>", 'xt')
1564 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001565 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001566
Bram Moolenaar17627312019-06-02 19:53:44 +02001567 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001568 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001569 let winid = popup_atcursor('text', {})
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([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001573 call feedkeys("eli\<Esc>", 'xt')
1574 call assert_equal(1, popup_getpos(winid).visible)
1575 call feedkeys("wi\<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
1581 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001582 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001583 redraw
1584 call assert_equal(1, popup_getpos(winid).visible)
1585 call feedkeys("eli\<Esc>", 'xt')
1586 call feedkeys("ei\<Esc>", 'xt')
1587 call assert_equal(1, popup_getpos(winid).visible)
1588 call feedkeys("eli\<Esc>", 'xt')
1589 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001590 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001591
1592 bwipe!
1593 call test_override('ALL', 0)
1594endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001595
1596func Test_notifications()
1597 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02001598 throw 'Skipped: timer feature not supported'
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001599 endif
1600 if !CanRunVimInTerminal()
1601 throw 'Skipped: cannot make screendumps'
1602 endif
1603
1604 call writefile([
1605 \ "call setline(1, range(1, 20))",
1606 \ "hi Notification ctermbg=lightblue",
1607 \ "call popup_notification('first notification', {})",
1608 \], 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001609 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001610 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1611
1612 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001613 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1614 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001615 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1616
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001617 " clean up
1618 call StopVimInTerminal(buf)
1619 call delete('XtestNotifications')
1620endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001621
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001622func Test_popup_scrollbar()
1623 if !CanRunVimInTerminal()
1624 throw 'Skipped: cannot make screendumps'
1625 endif
1626
1627 let lines =<< trim END
1628 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001629 hi ScrollThumb ctermbg=blue
1630 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001631 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001632 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001633 \ minwidth: 8,
1634 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001635 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001636 func ScrollUp()
1637 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1638 endfunc
1639 func ScrollDown()
1640 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1641 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001642 func ClickTop()
1643 call feedkeys("\<F4>\<LeftMouse>", "xt")
1644 endfunc
1645 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001646 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001647 call feedkeys("\<F5>\<LeftMouse>", "xt")
1648 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001649 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001650 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1651 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001652 END
1653 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001654 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001655 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1656
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001657 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001658 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001659 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1660
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001661 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001662 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001663 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1664
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001665 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001666 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001667 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1668
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001669 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb'})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001670 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1671 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1672
1673 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1674 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1675
1676 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001677 " wait a bit, otherwise it fails sometimes (double click recognized?)
1678 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001679 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1680 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1681
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001682 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1683 sleep 100m
1684 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1685 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1686
1687 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1688 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1689
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001690 " clean up
1691 call StopVimInTerminal(buf)
1692 call delete('XtestPopupScroll')
1693endfunc
1694
Bram Moolenaar437a7462019-07-05 20:17:22 +02001695func Test_popup_fitting_scrollbar()
1696 " this was causing a crash, divide by zero
1697 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001698 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001699 \ scrollbar: 1,
1700 \ maxwidth: 10,
1701 \ maxheight: 5,
1702 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001703 redraw
1704 call popup_clear()
1705endfunc
1706
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001707func Test_popup_settext()
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001708 if !CanRunVimInTerminal()
1709 throw 'Skipped: cannot make screendumps'
1710 endif
1711
1712 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001713 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001714 let p = popup_create('test', opts)
1715 call popup_settext(p, 'this is a text')
1716 END
1717
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001718 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001719 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001720 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1721
1722 " Setting to empty string clears it
1723 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1724 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1725
1726 " Setting a list
1727 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1728 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1729
1730 " Shrinking with a list
1731 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1732 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1733
1734 " Growing with a list
1735 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1736 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1737
1738 " Empty list clears
1739 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1740 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1741
1742 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001743 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001744 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1745
1746 " clean up
1747 call StopVimInTerminal(buf)
1748 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001749endfunc
1750
1751func Test_popup_hidden()
1752 new
1753
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001754 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001755 redraw
1756 call assert_equal(0, popup_getpos(winid).visible)
1757 call popup_close(winid)
1758
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001759 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001760 redraw
1761 call assert_equal(0, popup_getpos(winid).visible)
1762 call popup_close(winid)
1763
1764 func QuitCallback(id, res)
1765 let s:cb_winid = a:id
1766 let s:cb_res = a:res
1767 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001768 let winid = popup_dialog('make a choice', #{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001769 \ filter: 'popup_filter_yesno',
1770 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001771 \ })
1772 redraw
1773 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001774 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1775 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001776 exe "normal anot used by filter\<Esc>"
1777 call assert_equal('not used by filter', getline(1))
1778
1779 call popup_show(winid)
1780 call feedkeys('y', "xt")
1781 call assert_equal(1, s:cb_res)
1782
1783 bwipe!
1784 delfunc QuitCallback
1785endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001786
1787" Test options not checked elsewhere
1788func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001789 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001790 let options = popup_getoptions(winid)
1791 call assert_equal(1, options.wrap)
1792 call assert_equal(0, options.drag)
1793 call assert_equal('Beautiful', options.highlight)
1794
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001795 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001796 let options = popup_getoptions(winid)
1797 call assert_equal(0, options.wrap)
1798 call assert_equal(1, options.drag)
1799 call assert_equal('Another', options.highlight)
1800
1801 call popup_close(winid)
1802endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001803
1804func Test_popupwin_garbage_collect()
1805 func MyPopupFilter(x, winid, c)
1806 " NOP
1807 endfunc
1808
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001809 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001810 call test_garbagecollect_now()
1811 redraw
1812 " Must not crach caused by invalid memory access
1813 call feedkeys('j', 'xt')
1814 call assert_true(v:true)
1815
1816 call popup_close(winid)
1817 delfunc MyPopupFilter
1818endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001819
1820func Test_popupwin_with_buffer()
1821 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1822 let buf = bufadd('XsomeFile')
1823 call assert_equal(0, bufloaded(buf))
1824 let winid = popup_create(buf, {})
1825 call assert_notequal(0, winid)
1826 let pos = popup_getpos(winid)
1827 call assert_equal(2, pos.height)
1828 call assert_equal(1, bufloaded(buf))
1829 call popup_close(winid)
1830 call assert_equal({}, popup_getpos(winid))
1831 call assert_equal(1, bufloaded(buf))
1832 exe 'bwipe! ' .. buf
Bram Moolenaar7866b872019-07-01 22:21:01 +02001833
1834 edit test_popupwin.vim
1835 let winid = popup_create(bufnr(''), {})
1836 redraw
1837 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001838 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001839endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02001840
1841func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001842 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001843 \ maxwidth: 40,
1844 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02001845 \ })
1846 for top in range(1, 20)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001847 call popup_setoptions(winid, #{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02001848 redraw
1849 call assert_equal(19, popup_getpos(winid).width)
1850 endfor
1851 call popup_clear()
1852endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02001853
1854func Test_popupwin_buf_close()
1855 let buf = bufadd('Xtestbuf')
1856 call bufload(buf)
1857 call setbufline(buf, 1, ['just', 'some', 'lines'])
1858 let winid = popup_create(buf, {})
1859 redraw
1860 call assert_equal(3, popup_getpos(winid).height)
1861 let bufinfo = getbufinfo(buf)[0]
1862 call assert_equal(1, bufinfo.changed)
1863 call assert_equal(0, bufinfo.hidden)
1864 call assert_equal(0, bufinfo.listed)
1865 call assert_equal(1, bufinfo.loaded)
1866 call assert_equal([], bufinfo.windows)
1867 call assert_equal([winid], bufinfo.popups)
1868
1869 call popup_close(winid)
1870 call assert_equal({}, popup_getpos(winid))
1871 let bufinfo = getbufinfo(buf)[0]
1872 call assert_equal(1, bufinfo.changed)
1873 call assert_equal(1, bufinfo.hidden)
1874 call assert_equal(0, bufinfo.listed)
1875 call assert_equal(1, bufinfo.loaded)
1876 call assert_equal([], bufinfo.windows)
1877 call assert_equal([], bufinfo.popups)
1878 exe 'bwipe! ' .. buf
1879endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02001880
1881func Test_popup_menu_with_maxwidth()
1882 if !CanRunVimInTerminal()
1883 throw 'Skipped: cannot make screendumps'
1884 endif
1885
1886 let lines =<< trim END
1887 call setline(1, range(1, 10))
1888 hi ScrollThumb ctermbg=blue
1889 hi ScrollBar ctermbg=red
1890 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001891 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001892 \ maxwidth: 10,
1893 \ maxheight: 3,
1894 \ pos : 'topleft',
1895 \ col : a:col,
1896 \ line : a:line,
1897 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02001898 \ })
1899 endfunc
1900 call PopupMenu(['x'], 1, 1)
1901 call PopupMenu(['123456789|'], 1, 16)
1902 call PopupMenu(['123456789|' .. ' '], 7, 1)
1903 call PopupMenu([repeat('123456789|', 100)], 7, 16)
1904 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
1905 END
1906 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001907 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02001908 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
1909
1910 " close the menu popupwin.
1911 call term_sendkeys(buf, " ")
1912 call term_sendkeys(buf, " ")
1913 call term_sendkeys(buf, " ")
1914 call term_sendkeys(buf, " ")
1915 call term_sendkeys(buf, " ")
1916
1917 " clean up
1918 call StopVimInTerminal(buf)
1919 call delete('XtestPopupMenuMaxWidth')
1920endfunc
1921
Bram Moolenaara901a372019-07-13 16:38:50 +02001922func Test_popup_menu_with_scrollbar()
1923 if !CanRunVimInTerminal()
1924 throw 'Skipped: cannot make screendumps'
1925 endif
1926
1927 let lines =<< trim END
1928 call setline(1, range(1, 20))
1929 hi ScrollThumb ctermbg=blue
1930 hi ScrollBar ctermbg=red
1931 call popup_menu(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001932 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001933 \ minwidth: 8,
1934 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02001935 \ })
1936 END
1937 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001938 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02001939
1940 call term_sendkeys(buf, "j")
1941 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
1942
1943 call term_sendkeys(buf, "jjj")
1944 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
1945
1946 " if the cursor is the bottom line, it stays at the bottom line.
1947 call term_sendkeys(buf, repeat("j", 20))
1948 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
1949
1950 call term_sendkeys(buf, "kk")
1951 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
1952
1953 call term_sendkeys(buf, "k")
1954 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
1955
1956 " if the cursor is in the top line, it stays in the top line.
1957 call term_sendkeys(buf, repeat("k", 20))
1958 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
1959
1960 " close the menu popupwin.
1961 call term_sendkeys(buf, " ")
1962
1963 " clean up
1964 call StopVimInTerminal(buf)
1965 call delete('XtestPopupMenuScroll')
1966endfunc
1967
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001968func Test_popup_menu_filter()
1969 if !CanRunVimInTerminal()
1970 throw 'Skipped: cannot make screendumps'
1971 endif
1972
1973 let lines =<< trim END
1974 function! MyFilter(winid, key) abort
1975 if a:key == "0"
1976 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
1977 return 1
1978 endif
1979 if a:key == "G"
1980 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
1981 return 1
1982 endif
1983 if a:key == "j"
1984 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
1985 return 1
1986 endif
1987 if a:key == "k"
1988 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
1989 return 1
1990 endif
1991 if a:key == 'x'
1992 call popup_close(a:winid)
1993 return 1
1994 endif
1995 return 0
1996 endfunction
1997 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
1998 \ maxheight : 3,
1999 \ filter : 'MyFilter'
2000 \ })
2001 END
2002 call writefile(lines, 'XtestPopupMenuFilter')
2003 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2004
2005 call term_sendkeys(buf, "j")
2006 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2007
2008 call term_sendkeys(buf, "k")
2009 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2010
2011 call term_sendkeys(buf, "G")
2012 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2013
2014 call term_sendkeys(buf, "0")
2015 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2016
2017 call term_sendkeys(buf, "x")
2018
2019 " clean up
2020 call StopVimInTerminal(buf)
2021 call delete('XtestPopupMenuFilter')
2022endfunc
2023
2024func Test_popup_cursorline()
2025 if !CanRunVimInTerminal()
2026 throw 'Skipped: cannot make screendumps'
2027 endif
2028
2029 let winid = popup_create('some text', {})
2030 call assert_equal(0, popup_getoptions(winid).cursorline)
2031 call popup_close(winid)
2032
2033 let winid = popup_create('some text', #{ cursorline: 1, })
2034 call assert_equal(1, popup_getoptions(winid).cursorline)
2035 call popup_close(winid)
2036
2037 let winid = popup_create('some text', #{ cursorline: 0, })
2038 call assert_equal(0, popup_getoptions(winid).cursorline)
2039 call popup_close(winid)
2040
2041 let winid = popup_menu('some text', {})
2042 call assert_equal(1, popup_getoptions(winid).cursorline)
2043 call popup_close(winid)
2044
2045 let winid = popup_menu('some text', #{ cursorline: 1, })
2046 call assert_equal(1, popup_getoptions(winid).cursorline)
2047 call popup_close(winid)
2048
2049 let winid = popup_menu('some text', #{ cursorline: 0, })
2050 call assert_equal(0, popup_getoptions(winid).cursorline)
2051 call popup_close(winid)
2052
2053 " ---------
2054 " Pattern 1
2055 " ---------
2056 let lines =<< trim END
2057 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2058 END
2059 call writefile(lines, 'XtestPopupCursorLine')
2060 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2061 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2062 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2063 call StopVimInTerminal(buf)
2064
2065 " ---------
2066 " Pattern 2
2067 " ---------
2068 let lines =<< trim END
2069 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2070 END
2071 call writefile(lines, 'XtestPopupCursorLine')
2072 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2073 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2074 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2075 call StopVimInTerminal(buf)
2076
2077 " ---------
2078 " Pattern 3
2079 " ---------
2080 let lines =<< trim END
2081 function! MyFilter(winid, key) abort
2082 if a:key == "j"
2083 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2084 return 1
2085 endif
2086 if a:key == 'x'
2087 call popup_close(a:winid)
2088 return 1
2089 endif
2090 return 0
2091 endfunction
2092 call popup_menu(['111', '222', '333'], #{
2093 \ cursorline : 0,
2094 \ maxheight : 2,
2095 \ filter : 'MyFilter',
2096 \ })
2097 END
2098 call writefile(lines, 'XtestPopupCursorLine')
2099 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2100 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2101 call term_sendkeys(buf, "j")
2102 call term_sendkeys(buf, "j")
2103 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2104 call term_sendkeys(buf, "x")
2105 call StopVimInTerminal(buf)
2106
2107 " ---------
2108 " Pattern 4
2109 " ---------
2110 let lines =<< trim END
2111 function! MyFilter(winid, key) abort
2112 if a:key == "j"
2113 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2114 return 1
2115 endif
2116 if a:key == 'x'
2117 call popup_close(a:winid)
2118 return 1
2119 endif
2120 return 0
2121 endfunction
2122 call popup_menu(['111', '222', '333'], #{
2123 \ cursorline : 1,
2124 \ maxheight : 2,
2125 \ filter : 'MyFilter',
2126 \ })
2127 END
2128 call writefile(lines, 'XtestPopupCursorLine')
2129 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2130 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2131 call term_sendkeys(buf, "j")
2132 call term_sendkeys(buf, "j")
2133 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2134 call term_sendkeys(buf, "x")
2135 call StopVimInTerminal(buf)
2136
2137 call delete('XtestPopupCursorLine')
2138endfunc
2139
Bram Moolenaarf914a332019-07-20 15:09:56 +02002140func Test_previewpopup()
2141 if !CanRunVimInTerminal()
2142 throw 'Skipped: cannot make screendumps'
2143 endif
2144 call writefile([
2145 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2146 \ "another\tXtagfile\t/^this is another",
2147 \ "theword\tXtagfile\t/^theword"],
2148 \ 'Xtags')
2149 call writefile(range(1,20)
2150 \ + ['theword is here']
2151 \ + range(22, 27)
2152 \ + ['this is another place']
2153 \ + range(29, 40),
2154 \ "Xtagfile")
2155 let lines =<< trim END
2156 set tags=Xtags
2157 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002158 \ 'one',
2159 \ 'two',
2160 \ 'three',
2161 \ 'four',
2162 \ 'five',
2163 \ 'six',
2164 \ 'seven',
2165 \ 'find theword somewhere',
2166 \ 'nine',
2167 \ 'this is another word',
2168 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002169 set previewpopup=height:4,width:40
2170 END
2171 call writefile(lines, 'XtestPreviewPopup')
2172 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2173
2174 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2175 call term_sendkeys(buf, ":\<CR>")
2176 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2177
2178 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2179 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2180
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002181 call term_sendkeys(buf, ":call popup_move(popup_getpreview(), #{col: 15})\<CR>")
2182 call term_sendkeys(buf, ":\<CR>")
2183 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2184
2185 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2186 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2187
Bram Moolenaarf914a332019-07-20 15:09:56 +02002188 call StopVimInTerminal(buf)
2189 call delete('Xtags')
2190 call delete('Xtagfile')
2191 call delete('XtestPreviewPopup')
2192endfunc
2193
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002194" vim: shiftwidth=2 sts=2