blob: d4b98aee7be43d95550fdfbde80dabf3be868091 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature textprop
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
9 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020010 throw 'Skipped: cannot make screendumps'
Bram Moolenaar4d784b22019-05-25 19:51:39 +020011 endif
Bram Moolenaare7eb9272019-06-24 00:58:07 +020012 let lines =<< trim END
13 call setline(1, range(1, 100))
14 hi PopupColor1 ctermbg=lightblue
15 hi PopupColor2 ctermbg=lightcyan
16 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020017 call prop_type_add('comment', #{highlight: 'Comment'})
18 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
19 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020020 call setwinvar(winid2, '&wincolor', 'PopupColor2')
21 END
22 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020023 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020024 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020029 \ .. "#{text: 'other tab'},"
30 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020031 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020033 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
35
36 " switch back to first tabpage
37 call term_sendkeys(buf, "gt")
38 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
39
40 " close that tabpage
41 call term_sendkeys(buf, ":quit!\<CR>")
42 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
43
Bram Moolenaar202d9822019-06-11 21:56:30 +020044 " set 'columns' to a small value, size must be recomputed
45 call term_sendkeys(buf, ":let cols = &columns\<CR>")
46 call term_sendkeys(buf, ":set columns=12\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
48 call term_sendkeys(buf, ":let &columns = cols\<CR>")
49
Bram Moolenaar17146962019-05-30 00:12:11 +020050 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020051 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020052 call term_sendkeys(buf, ":redraw\<CR>")
53 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
54
Bram Moolenaar17146962019-05-30 00:12:11 +020055 " show not fitting line at bottom
56 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
57 call term_sendkeys(buf, ":redraw\<CR>")
58 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
59
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020060 " move popup over ruler
61 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020062 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020063 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020064 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
65
66 " clear all popups after moving the cursor a bit, so that ruler is updated
67 call term_sendkeys(buf, "axxx\<Esc>")
68 call term_wait(buf)
69 call term_sendkeys(buf, "0")
70 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020071 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020072 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
73
Bram Moolenaar4d784b22019-05-25 19:51:39 +020074 " clean up
75 call StopVimInTerminal(buf)
76 call delete('XtestPopup')
77endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020078
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020079func Test_popup_with_border_and_padding()
80 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020081 throw 'Skipped: cannot make screendumps'
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082 endif
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020083
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020084 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020085 let lines =<< trim END
86 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020087 call popup_create('hello border', #{line: 2, col: 3, border: []})
88 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
89 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: []})
90 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
91 call popup_create('paddings', #{line: 6, col: 23, padding: [1, 3, 2, 4]})
92 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
93 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020094 END
95 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
96 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020097 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020098 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
99
100 call StopVimInTerminal(buf)
101 call delete('XtestPopupBorder')
102 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200103
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200104 let lines =<< trim END
105 call setline(1, range(1, 100))
106 hi BlueColor ctermbg=lightblue
107 hi TopColor ctermbg=253
108 hi RightColor ctermbg=245
109 hi BottomColor ctermbg=240
110 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200111 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
112 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
113 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
114 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
115 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
116 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200117 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200118 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200119 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200120 END
121 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200122 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200123 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
124
Bram Moolenaarad24a712019-06-17 20:05:45 +0200125 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200126 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200127 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
128
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200129 " check multi-byte border only with 'ambiwidth' single
130 if &ambiwidth == 'single'
131 call term_sendkeys(buf, ":call MultiByte()\<CR>")
132 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
133 endif
134
Bram Moolenaar790498b2019-06-01 22:15:29 +0200135 call StopVimInTerminal(buf)
136 call delete('XtestPopupBorder')
137
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200138 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200139 \ line: 2,
140 \ core_line: 3,
141 \ col: 3,
142 \ core_col: 4,
143 \ width: 14,
144 \ core_width: 12,
145 \ height: 3,
146 \ core_height: 1,
147 \ firstline: 1,
148 \ scrollbar: 0,
149 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200150 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200151 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200152 let options = popup_getoptions(winid)
153 call assert_equal([], options.border)
154 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200155
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200156 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200157 let with_border_or_padding.width = 15
158 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200159 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200160 let options = popup_getoptions(winid)
161 call assert_false(has_key(options, "border"))
162 call assert_equal([], options.padding)
163
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200164 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200165 \ padding: [1, 2, 3, 4],
166 \ border: [4, 0, 7, 8],
167 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
168 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200169 \ })
170 let options = popup_getoptions(winid)
171 call assert_equal([1, 0, 1, 1], options.border)
172 call assert_equal([1, 2, 3, 4], options.padding)
173 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
174 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200175
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200176 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
177 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200178 \ line: 3,
179 \ core_line: 5,
180 \ col: 8,
181 \ core_col: 10,
182 \ width: 14,
183 \ core_width: 10,
184 \ height: 5,
185 \ scrollbar: 0,
186 \ core_height: 1,
187 \ firstline: 1,
188 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200189
190 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200191endfunc
192
Bram Moolenaarb4230122019-05-30 18:40:53 +0200193func Test_popup_with_syntax_win_execute()
194 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200195 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200196 endif
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200197 let lines =<< trim END
198 call setline(1, range(1, 100))
199 hi PopupColor ctermbg=lightblue
200 let winid = popup_create([
201 \ '#include <stdio.h>',
202 \ 'int main(void)',
203 \ '{',
204 \ ' printf(123);',
205 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200206 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200207 call win_execute(winid, 'set syntax=cpp')
208 END
209 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200210 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200211 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
212
213 " clean up
214 call StopVimInTerminal(buf)
215 call delete('XtestPopup')
216endfunc
217
218func Test_popup_with_syntax_setbufvar()
219 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200220 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200221 endif
Bram Moolenaar402502d2019-05-30 22:07:36 +0200222 let lines =<< trim END
223 call setline(1, range(1, 100))
224 hi PopupColor ctermbg=lightgrey
225 let winid = popup_create([
226 \ '#include <stdio.h>',
227 \ 'int main(void)',
228 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200229 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200230 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200231 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200232 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
233 END
234 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200235 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200236 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
237
238 " clean up
239 call StopVimInTerminal(buf)
240 call delete('XtestPopup')
241endfunc
242
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200243func Test_popup_with_matches()
244 if !CanRunVimInTerminal()
245 throw 'Skipped: cannot make screendumps'
246 endif
247 let lines =<< trim END
248 call setline(1, ['111 222 333', '444 555 666'])
249 let winid = popup_create([
250 \ '111 222 333',
251 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200252 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200253 set hlsearch
254 /666
255 call matchadd('ErrorMsg', '111')
256 call matchadd('ErrorMsg', '444')
257 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
258 call win_execute(winid, "call matchadd('ErrorMsg', '555')")
259 END
260 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200261 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200262 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
263
264 " clean up
265 call StopVimInTerminal(buf)
266 call delete('XtestPopupMatches')
267endfunc
268
Bram Moolenaar399d8982019-06-02 15:34:29 +0200269func Test_popup_all_corners()
270 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200271 throw 'Skipped: cannot make screendumps'
Bram Moolenaar399d8982019-06-02 15:34:29 +0200272 endif
273 let lines =<< trim END
274 call setline(1, repeat([repeat('-', 60)], 15))
275 set so=0
276 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200277 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200278 \ line: 'cursor+1',
279 \ col: 'cursor',
280 \ pos: 'topleft',
281 \ border: [],
282 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200283 \ })
284 normal 25|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200285 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200286 \ line: 'cursor+1',
287 \ col: 'cursor',
288 \ pos: 'topright',
289 \ border: [],
290 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200291 \ })
292 normal 9G29|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200293 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200294 \ line: 'cursor-1',
295 \ col: 'cursor',
296 \ pos: 'botleft',
297 \ border: [],
298 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200299 \ })
300 normal 51|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200301 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200302 \ line: 'cursor-1',
303 \ col: 'cursor',
304 \ pos: 'botright',
305 \ border: [],
306 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200307 \ })
308 END
309 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200310 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200311 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
312
313 " clean up
314 call StopVimInTerminal(buf)
315 call delete('XtestPopupCorners')
316endfunc
317
Bram Moolenaar8d241042019-06-12 23:40:01 +0200318func Test_popup_firstline()
319 if !CanRunVimInTerminal()
320 throw 'Skipped: cannot make screendumps'
321 endif
322 let lines =<< trim END
323 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200324 call popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200325 \ maxheight: 4,
326 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200327 \ })
328 END
329 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200330 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200331 call VerifyScreenDump(buf, 'Test_popupwin_firstline', {})
332
333 " clean up
334 call StopVimInTerminal(buf)
335 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200336
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200337 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200338 \ maxheight: 2,
339 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200340 \ })
341 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200342 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200343 call assert_equal(1, popup_getoptions(winid).firstline)
344
345 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200346endfunc
347
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200348func Test_popup_drag()
349 if !CanRunVimInTerminal()
350 throw 'Skipped: cannot make screendumps'
351 endif
352 " create a popup that covers the command line
353 let lines =<< trim END
354 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200355 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200356 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200357 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200358 \ border: [],
359 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200360 \ })
361 func Dragit()
362 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
363 endfunc
364 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
365 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200366 func Resize()
367 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
368 endfunc
369 map <silent> <F5> :call test_setmouse(6, 41)<CR>
370 map <silent> <F6> :call test_setmouse(7, 45)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200371 END
372 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200373 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200374 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
375
376 call term_sendkeys(buf, ":call Dragit()\<CR>")
377 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
378
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200379 call term_sendkeys(buf, ":call Resize()\<CR>")
380 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
381
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200382 " clean up
383 call StopVimInTerminal(buf)
384 call delete('XtestPopupDrag')
385endfunc
386
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200387func Test_popup_close_with_mouse()
388 if !CanRunVimInTerminal()
389 throw 'Skipped: cannot make screendumps'
390 endif
391 let lines =<< trim END
392 call setline(1, range(1, 20))
393 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200394 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200395 \ close: 'button',
396 \ border: [],
397 \ line: 1,
398 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200399 \ })
400 func CloseMsg(id, result)
401 echomsg 'Popup closed with ' .. a:result
402 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200403 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200404 \ close: 'click',
405 \ line: 3,
406 \ col: 15,
407 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200408 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200409 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200410 \ close: 'button',
411 \ line: 5,
412 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200413 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200414 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200415 \ close: 'button',
416 \ padding: [],
417 \ line: 5,
418 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200419 \ })
420 func CloseWithX()
421 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
422 endfunc
423 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
424 func CloseWithClick()
425 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
426 endfunc
427 map <silent> <F4> :call test_setmouse(3, 17)<CR>
428 END
429 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200430 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200431 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
432
433 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
434 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
435
436 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
437 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
438
439 " clean up
440 call StopVimInTerminal(buf)
441 call delete('XtestPopupClose')
442endfunction
443
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200444func Test_popup_with_mask()
445 if !CanRunVimInTerminal()
446 throw 'Skipped: cannot make screendumps'
447 endif
448 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200449 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200450 hi PopupColor ctermbg=lightgrey
451 let winid = popup_create([
452 \ 'some text',
453 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200454 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200455 \ line: 1,
456 \ col: 10,
457 \ wrap: 0,
458 \ fixed: 1,
459 \ zindex: 90,
460 \ padding: [],
461 \ highlight: 'PopupColor',
462 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200463 call popup_create([
464 \ 'xxxxxxxxx',
465 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200466 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200467 \ line: 3,
468 \ col: 18,
469 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200470 let winidb = popup_create([
471 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200472 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200473 \ line: 7,
474 \ col: 10,
475 \ wrap: 0,
476 \ fixed: 1,
477 \ close: 'button',
478 \ zindex: 90,
479 \ padding: [],
480 \ border: [],
481 \ mask: [[1,2,1,1], [-5,-1,4,4], [7,9,2,3], [3,5,5,5],[-7,-4,5,5]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200482 END
483 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200484 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200485 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
486
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200487 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
488 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200489 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200490 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
491
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200492 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
493 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200494 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200495 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
496
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200497 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
498 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200499 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200500 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
501
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200502 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
503 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200504 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200505 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
506
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200507 " clean up
508 call StopVimInTerminal(buf)
509 call delete('XtestPopupMask')
510endfunc
511
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200512func Test_popup_select()
513 if !CanRunVimInTerminal()
514 throw 'Skipped: cannot make screendumps'
515 endif
Bram Moolenaar650a6372019-06-15 00:29:33 +0200516 if !has('clipboard')
517 throw 'Skipped: clipboard feature missing'
518 endif
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200519 " create a popup with some text to be selected
520 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200521 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200522 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200523 let winid = popup_create(['the word', 'some more', 'several words here'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200524 \ drag: 1,
525 \ border: [],
526 \ line: 3,
527 \ col: 10,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200528 \ })
529 func Select1()
530 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
531 endfunc
532 map <silent> <F3> :call test_setmouse(4, 15)<CR>
533 map <silent> <F4> :call test_setmouse(6, 23)<CR>
534 END
535 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200536 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200537 call term_sendkeys(buf, ":call Select1()\<CR>")
538 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
539
540 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
541 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200542 " clean the command line, sometimes it still shows a command
543 call term_sendkeys(buf, ":\<esc>")
544
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200545 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
546
547 " clean up
548 call StopVimInTerminal(buf)
549 call delete('XtestPopupSelect')
550endfunc
551
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200552func Test_popup_in_tab()
553 " default popup is local to tab, not visible when in other tab
554 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200555 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200556 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200557 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200558 tabnew
559 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200560 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200561 quit
562 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200563
564 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200565 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200566 " buffer is gone now
567 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200568
569 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200570 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200571 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200572 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200573 tabnew
574 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200575 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200576 quit
577 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200578 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200579
580 " create popup in other tab
581 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200582 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200583 call assert_equal(0, popup_getpos(winid).visible)
584 call assert_equal(1, popup_getoptions(winid).tabpage)
585 quit
586 call assert_equal(1, popup_getpos(winid).visible)
587 call assert_equal(0, popup_getoptions(winid).tabpage)
588 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200589endfunc
590
591func Test_popup_valid_arguments()
592 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200593 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200594 let pos = popup_getpos(winid)
595 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200596 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200597
598 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200599 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200600 let pos = popup_getpos(winid)
601 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200602 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200603
604 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200605 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200606 let pos = popup_getpos(winid)
607 let around = (&columns - pos.width) / 2
608 call assert_inrange(around - 1, around + 1, pos.col)
609 let around = (&lines - pos.height) / 2
610 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200611 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200612endfunc
613
614func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200615 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200616 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200617 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200618 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200619
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200620 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200621 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200622 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200623 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200624 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200625 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200626 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200627 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200628
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200629 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200630 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200631 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200632 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200633 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200634 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200635 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200636 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200637
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200638 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200639 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200640 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200641 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200642 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200643 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200644 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200645 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200646 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
647 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200648 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200649 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200650
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200651 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200652 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200653 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200654 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200655 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200656 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200657 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
658 call popup_clear()
659 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
660 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200661endfunc
662
Bram Moolenaareea16992019-05-31 17:34:48 +0200663func Test_win_execute_closing_curwin()
664 split
665 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200666 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200667 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200668endfunc
669
670func Test_win_execute_not_allowed()
671 let winid = popup_create('some text', {})
672 call assert_fails('call win_execute(winid, "split")', 'E994:')
673 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
674 call assert_fails('call win_execute(winid, "close")', 'E994:')
675 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200676 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200677 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
678 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
679 call assert_fails('call win_execute(winid, "next")', 'E994:')
680 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
681 call assert_fails('call win_execute(winid, "buf")', 'E994:')
682 call assert_fails('call win_execute(winid, "edit")', 'E994:')
683 call assert_fails('call win_execute(winid, "enew")', 'E994:')
684 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
685 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
686 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
687 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200688 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200689endfunc
690
Bram Moolenaar402502d2019-05-30 22:07:36 +0200691func Test_popup_with_wrap()
692 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200693 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200694 endif
695 let lines =<< trim END
696 call setline(1, range(1, 100))
697 let winid = popup_create(
698 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200699 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200700 END
701 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200702 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200703 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
704
705 " clean up
706 call StopVimInTerminal(buf)
707 call delete('XtestPopup')
708endfunc
709
710func Test_popup_without_wrap()
711 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200712 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200713 endif
714 let lines =<< trim END
715 call setline(1, range(1, 100))
716 let winid = popup_create(
717 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200718 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200719 END
720 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200721 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200722 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
723
724 " clean up
725 call StopVimInTerminal(buf)
726 call delete('XtestPopup')
727endfunc
728
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200729func Test_popup_with_showbreak()
730 if !CanRunVimInTerminal()
731 throw 'Skipped: cannot make screendumps'
732 endif
733 let lines =<< trim END
734 set showbreak=>>\
735 call setline(1, range(1, 20))
736 let winid = popup_dialog(
737 \ 'a long line here',
738 \ #{filter: 'popup_filter_yesno'})
739 END
740 call writefile(lines, 'XtestPopupShowbreak')
741 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
742 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
743
744 " clean up
745 call term_sendkeys(buf, "y")
746 call StopVimInTerminal(buf)
747 call delete('XtestPopupShowbreak')
748endfunc
749
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200750func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200751 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +0200752 throw 'Skipped: timer feature not supported'
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200753 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200754 topleft vnew
755 call setline(1, 'hello')
756
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200757 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200758 \ line: 1,
759 \ col: 1,
760 \ minwidth: 20,
761 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200762 \})
763 redraw
764 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
765 call assert_equal('world', line)
766
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200767 call assert_equal(winid, popup_locate(1, 1))
768 call assert_equal(winid, popup_locate(1, 20))
769 call assert_equal(0, popup_locate(1, 21))
770 call assert_equal(0, popup_locate(2, 1))
771
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200772 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200773 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200774 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
775 call assert_equal('hello', line)
776
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200777 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200778 \ line: &lines,
779 \ col: 10,
780 \ minwidth: 20,
781 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200782 \})
783 redraw
784 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
785 call assert_match('.*on the command line.*', line)
786
787 sleep 700m
788 redraw
789 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
790 call assert_notmatch('.*on the command line.*', line)
791
792 bwipe!
793endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200794
795func Test_popup_hide()
796 topleft vnew
797 call setline(1, 'hello')
798
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200799 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200800 \ line: 1,
801 \ col: 1,
802 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200803 \})
804 redraw
805 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
806 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200807 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200808 " buffer is still listed and active
809 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200810
811 call popup_hide(winid)
812 redraw
813 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
814 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200815 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200816 " buffer is still listed but hidden
817 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200818
819 call popup_show(winid)
820 redraw
821 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
822 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200823 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200824
825
826 call popup_close(winid)
827 redraw
828 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
829 call assert_equal('hello', line)
830
831 " error is given for existing non-popup window
832 call assert_fails('call popup_hide(win_getid())', 'E993:')
833
834 " no error non-existing window
835 call popup_hide(1234234)
836 call popup_show(41234234)
837
838 bwipe!
839endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200840
841func Test_popup_move()
842 topleft vnew
843 call setline(1, 'hello')
844
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200845 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200846 \ line: 1,
847 \ col: 1,
848 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200849 \})
850 redraw
851 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
852 call assert_equal('world ', line)
853
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200854 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200855 redraw
856 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
857 call assert_equal('hello ', line)
858 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
859 call assert_equal('~world', line)
860
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200861 call popup_move(winid, #{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200862 redraw
863 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
864 call assert_equal('hworld', line)
865
866 call popup_close(winid)
867
868 bwipe!
869endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200870
Bram Moolenaar402502d2019-05-30 22:07:36 +0200871func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200872 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200873 \ line: 2,
874 \ col: 3,
875 \ minwidth: 10,
876 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200877 \})
878 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200879 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200880 call assert_equal(2, res.line)
881 call assert_equal(3, res.col)
882 call assert_equal(10, res.width)
883 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200884 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200885
886 call popup_close(winid)
887endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200888
889func Test_popup_width_longest()
890 let tests = [
891 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
892 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
893 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
894 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
895 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
896 \ ]
897
898 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200899 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200900 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200901 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200902 call assert_equal(test[1], position.width)
903 call popup_close(winid)
904 endfor
905endfunc
906
907func Test_popup_wraps()
908 let tests = [
909 \ ['nowrap', 6, 1],
910 \ ['a line that wraps once', 12, 2],
911 \ ['a line that wraps two times', 12, 3],
912 \ ]
913 for test in tests
914 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200915 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200916 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200917 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200918 call assert_equal(test[1], position.width)
919 call assert_equal(test[2], position.height)
920
921 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200922 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200923 endfor
924endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200925
926func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200927 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200928 \ line: 2,
929 \ col: 3,
930 \ minwidth: 10,
931 \ minheight: 11,
932 \ maxwidth: 20,
933 \ maxheight: 21,
934 \ zindex: 100,
935 \ time: 5000,
936 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200937 \})
938 redraw
939 let res = popup_getoptions(winid)
940 call assert_equal(2, res.line)
941 call assert_equal(3, res.col)
942 call assert_equal(10, res.minwidth)
943 call assert_equal(11, res.minheight)
944 call assert_equal(20, res.maxwidth)
945 call assert_equal(21, res.maxheight)
946 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200947 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200948 if has('timers')
949 call assert_equal(5000, res.time)
950 endif
951 call popup_close(winid)
952
953 let winid = popup_create('hello', {})
954 redraw
955 let res = popup_getoptions(winid)
956 call assert_equal(0, res.line)
957 call assert_equal(0, res.col)
958 call assert_equal(0, res.minwidth)
959 call assert_equal(0, res.minheight)
960 call assert_equal(0, res.maxwidth)
961 call assert_equal(0, res.maxheight)
962 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200963 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200964 if has('timers')
965 call assert_equal(0, res.time)
966 endif
967 call popup_close(winid)
968 call assert_equal({}, popup_getoptions(winid))
969endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200970
971func Test_popup_option_values()
972 new
973 " window-local
974 setlocal number
975 setlocal nowrap
976 " buffer-local
977 setlocal omnifunc=Something
978 " global/buffer-local
979 setlocal path=/there
980 " global/window-local
981 setlocal scrolloff=9
982
983 let winid = popup_create('hello', {})
984 call assert_equal(0, getwinvar(winid, '&number'))
985 call assert_equal(1, getwinvar(winid, '&wrap'))
986 call assert_equal('', getwinvar(winid, '&omnifunc'))
987 call assert_equal(&g:path, getwinvar(winid, '&path'))
988 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
989
990 call popup_close(winid)
991 bwipe
992endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200993
994func Test_popup_atcursor()
995 topleft vnew
996 call setline(1, [
997 \ 'xxxxxxxxxxxxxxxxx',
998 \ 'xxxxxxxxxxxxxxxxx',
999 \ 'xxxxxxxxxxxxxxxxx',
1000 \])
1001
1002 call cursor(2, 2)
1003 redraw
1004 let winid = popup_atcursor('vim', {})
1005 redraw
1006 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1007 call assert_equal('xvimxxxxxxxxxxxxx', line)
1008 call popup_close(winid)
1009
1010 call cursor(3, 4)
1011 redraw
1012 let winid = popup_atcursor('vim', {})
1013 redraw
1014 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1015 call assert_equal('xxxvimxxxxxxxxxxx', line)
1016 call popup_close(winid)
1017
1018 call cursor(1, 1)
1019 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001020 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001021 \ line: 'cursor+2',
1022 \ col: 'cursor+1',
1023 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001024 redraw
1025 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1026 call assert_equal('xvimxxxxxxxxxxxxx', line)
1027 call popup_close(winid)
1028
1029 call cursor(3, 3)
1030 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001031 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001032 \ line: 'cursor-2',
1033 \ col: 'cursor-1',
1034 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001035 redraw
1036 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1037 call assert_equal('xvimxxxxxxxxxxxxx', line)
1038 call popup_close(winid)
1039
Bram Moolenaar402502d2019-05-30 22:07:36 +02001040 " just enough room above
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(1, pos.line)
1047 call popup_close(winid)
1048
1049 " not enough room above, popup goes below the cursor
1050 call cursor(3, 3)
1051 redraw
1052 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1053 redraw
1054 let pos = popup_getpos(winid)
1055 call assert_equal(4, pos.line)
1056 call popup_close(winid)
1057
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001058 " cursor in first line, popup in line 2
1059 call cursor(1, 1)
1060 redraw
1061 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1062 redraw
1063 let pos = popup_getpos(winid)
1064 call assert_equal(2, pos.line)
1065 call popup_close(winid)
1066
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001067 bwipe!
1068endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001069
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001070func Test_popup_beval()
1071 if !CanRunVimInTerminal()
1072 throw 'Skipped: cannot make screendumps'
1073 endif
1074
1075 let lines =<< trim END
1076 call setline(1, range(1, 20))
1077 call setline(5, 'here is some text to hover over')
1078 set balloonevalterm
1079 set balloonexpr=BalloonExpr()
1080 set balloondelay=100
1081 func BalloonExpr()
1082 let s:winid = popup_beval([v:beval_text], {})
1083 return ''
1084 endfunc
1085 func Hover()
1086 call test_setmouse(5, 15)
1087 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1088 sleep 100m
1089 endfunc
1090 func MoveOntoPopup()
1091 call test_setmouse(4, 17)
1092 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1093 endfunc
1094 func MoveAway()
1095 call test_setmouse(5, 13)
1096 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1097 endfunc
1098 END
1099 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001100 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001101 call term_wait(buf, 100)
1102 call term_sendkeys(buf, 'j')
1103 call term_sendkeys(buf, ":call Hover()\<CR>")
1104 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1105
1106 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1107 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1108
1109 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1110 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1111
1112 " clean up
1113 call StopVimInTerminal(buf)
1114 call delete('XtestPopupBeval')
1115endfunc
1116
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001117func Test_popup_filter()
1118 new
1119 call setline(1, 'some text')
1120
1121 func MyPopupFilter(winid, c)
1122 if a:c == 'e'
1123 let g:eaten = 'e'
1124 return 1
1125 endif
1126 if a:c == '0'
1127 let g:ignored = '0'
1128 return 0
1129 endif
1130 if a:c == 'x'
1131 call popup_close(a:winid)
1132 return 1
1133 endif
1134 return 0
1135 endfunc
1136
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001137 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001138 redraw
1139
1140 " e is consumed by the filter
1141 call feedkeys('e', 'xt')
1142 call assert_equal('e', g:eaten)
1143
1144 " 0 is ignored by the filter
1145 normal $
1146 call assert_equal(9, getcurpos()[2])
1147 call feedkeys('0', 'xt')
1148 call assert_equal('0', g:ignored)
1149 call assert_equal(1, getcurpos()[2])
1150
1151 " x closes the popup
1152 call feedkeys('x', 'xt')
1153 call assert_equal('e', g:eaten)
1154 call assert_equal(-1, winbufnr(winid))
1155
1156 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001157 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001158endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001159
Bram Moolenaara42d9452019-06-15 21:46:30 +02001160func ShowDialog(key, result)
1161 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001162 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001163 \ filter: 'popup_filter_yesno',
1164 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001165 \ })
1166 redraw
1167 call feedkeys(a:key, "xt")
1168 call assert_equal(winid, s:cb_winid)
1169 call assert_equal(a:result, s:cb_res)
1170endfunc
1171
1172func Test_popup_dialog()
1173 func QuitCallback(id, res)
1174 let s:cb_winid = a:id
1175 let s:cb_res = a:res
1176 endfunc
1177
1178 let winid = ShowDialog("y", 1)
1179 let winid = ShowDialog("Y", 1)
1180 let winid = ShowDialog("n", 0)
1181 let winid = ShowDialog("N", 0)
1182 let winid = ShowDialog("x", 0)
1183 let winid = ShowDialog("X", 0)
1184 let winid = ShowDialog("\<Esc>", 0)
1185 let winid = ShowDialog("\<C-C>", -1)
1186
1187 delfunc QuitCallback
1188endfunc
1189
Bram Moolenaara730e552019-06-16 19:05:31 +02001190func ShowMenu(key, result)
1191 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001192 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001193 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001194 \ })
1195 redraw
1196 call feedkeys(a:key, "xt")
1197 call assert_equal(winid, s:cb_winid)
1198 call assert_equal(a:result, s:cb_res)
1199endfunc
1200
1201func Test_popup_menu()
1202 func QuitCallback(id, res)
1203 let s:cb_winid = a:id
1204 let s:cb_res = a:res
1205 endfunc
1206
1207 let winid = ShowMenu(" ", 1)
1208 let winid = ShowMenu("j \<CR>", 2)
1209 let winid = ShowMenu("JjK \<CR>", 2)
1210 let winid = ShowMenu("jjjjjj ", 3)
1211 let winid = ShowMenu("kkk ", 1)
1212 let winid = ShowMenu("x", -1)
1213 let winid = ShowMenu("X", -1)
1214 let winid = ShowMenu("\<Esc>", -1)
1215 let winid = ShowMenu("\<C-C>", -1)
1216
1217 delfunc QuitCallback
1218endfunc
1219
1220func Test_popup_menu_screenshot()
1221 if !CanRunVimInTerminal()
1222 throw 'Skipped: cannot make screendumps'
1223 endif
1224
1225 let lines =<< trim END
1226 call setline(1, range(1, 20))
1227 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001228 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001229 func MenuDone(id, res)
1230 echomsg "selected " .. a:res
1231 endfunc
1232 END
1233 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001234 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001235 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1236
1237 call term_sendkeys(buf, "jj")
1238 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1239
1240 call term_sendkeys(buf, " ")
1241 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1242
1243 " clean up
1244 call StopVimInTerminal(buf)
1245 call delete('XtestPopupMenu')
1246endfunc
1247
Bram Moolenaarf914a332019-07-20 15:09:56 +02001248func Test_popup_menu_narrow()
1249 if !CanRunVimInTerminal()
1250 throw 'Skipped: cannot make screendumps'
1251 endif
1252
1253 let lines =<< trim END
1254 call setline(1, range(1, 20))
1255 hi PopupSelected ctermbg=green
1256 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1257 func MenuDone(id, res)
1258 echomsg "selected " .. a:res
1259 endfunc
1260 END
1261 call writefile(lines, 'XtestPopupNarrowMenu')
1262 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1263 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1264
1265 " clean up
1266 call term_sendkeys(buf, "x")
1267 call StopVimInTerminal(buf)
1268 call delete('XtestPopupNarrowMenu')
1269endfunc
1270
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001271func Test_popup_title()
1272 if !CanRunVimInTerminal()
1273 throw 'Skipped: cannot make screendumps'
1274 endif
1275
1276 " Create a popup without title or border, a line of padding will be added to
1277 " put the title on.
1278 let lines =<< trim END
1279 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001280 call popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001281 END
1282 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001283 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001284 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1285
1286 " clean up
1287 call StopVimInTerminal(buf)
1288 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001289
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001290 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001291 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001292 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001293 call assert_equal('Another Title', popup_getoptions(winid).title)
1294
1295 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001296endfunc
1297
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001298func Test_popup_close_callback()
1299 func PopupDone(id, result)
1300 let g:result = a:result
1301 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001302 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001303 redraw
1304 call popup_close(winid, 'done')
1305 call assert_equal('done', g:result)
1306endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001307
1308func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001309 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001310 redraw
1311 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001312 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001313 call assert_equal(5, pos.height)
1314
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001315 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001316 redraw
1317 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001318 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001319 call assert_equal(3, pos.height)
1320endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001321
1322func Test_popup_never_behind()
1323 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001324 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +02001325 endif
1326 " +-----------------------------+
1327 " | | |
1328 " | | |
1329 " | | |
1330 " | line1 |
1331 " |------------line2------------|
1332 " | line3 |
1333 " | line4 |
1334 " | |
1335 " | |
1336 " +-----------------------------+
1337 let lines =<< trim END
1338 only
1339 split
1340 vsplit
1341 let info_window1 = getwininfo()[0]
1342 let line = info_window1['height']
1343 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001344 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001345 \ line : line,
1346 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001347 \ })
1348 END
1349 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001350 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001351 call term_sendkeys(buf, "\<C-W>w")
1352 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1353
1354 " clean up
1355 call StopVimInTerminal(buf)
1356 call delete('XtestPopupBehind')
1357endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001358
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001359func s:VerifyPosition(p, msg, line, col, width, height)
1360 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1361 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1362 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1363 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001364endfunc
1365
1366func Test_popup_position_adjust()
1367 " Anything placed past 2 cells from of the right of the screen is moved to the
1368 " left.
1369 "
1370 " When wrapping is disabled, we also shift to the left to display on the
1371 " screen, unless fixed is set.
1372
1373 " Entries for cases which don't vary based on wrapping.
1374 " Format is per tests described below
1375 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001376 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1377 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1378 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1379 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1380 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1381 \
1382 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1383 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1384 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1385 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1386 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1387 \
1388 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1389 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1390 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1391 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1392 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1393 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001394
1395 " these test groups are dicts with:
1396 " - comment: something to identify the group of tests by
1397 " - options: dict of options to merge with the row/col in tests
1398 " - tests: list of cases. Each one is a list with elements:
1399 " - text
1400 " - row
1401 " - col
1402 " - expected row
1403 " - expected col
1404 " - expected width
1405 " - expected height
1406 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001407 \ #{
1408 \ comment: 'left-aligned with wrapping',
1409 \ options: #{
1410 \ wrap: 1,
1411 \ pos: 'botleft',
1412 \ },
1413 \ tests: both_wrap_tests + [
1414 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1415 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1416 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1417 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1418 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1419 \ ],
1420 \ },
1421 \ #{
1422 \ comment: 'left aligned without wrapping',
1423 \ options: #{
1424 \ wrap: 0,
1425 \ pos: 'botleft',
1426 \ },
1427 \ tests: both_wrap_tests + [
1428 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1429 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1430 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1431 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1432 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1433 \ ],
1434 \ },
1435 \ #{
1436 \ comment: 'left aligned with fixed position',
1437 \ options: #{
1438 \ wrap: 0,
1439 \ fixed: 1,
1440 \ pos: 'botleft',
1441 \ },
1442 \ tests: both_wrap_tests + [
1443 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1444 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1445 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1446 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1447 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1448 \ ],
1449 \ },
1450 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001451
1452 for test_group in tests
1453 for test in test_group.tests
1454 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001455 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001456 \ line: line,
1457 \ col: col,
1458 \ }
1459 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001460
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001461 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001462
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001463 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001464 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1465 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001466 endfor
1467 endfor
1468
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001469 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001470 %bwipe!
1471endfunc
1472
Bram Moolenaar3397f742019-06-02 18:40:06 +02001473func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001474 " width of screen
1475 let X = join(map(range(&columns), {->'X'}), '')
1476
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001477 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1478 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001479
1480 redraw
1481 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1482 call assert_equal(X, line)
1483
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001484 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001485 redraw
1486
1487 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001488 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1489 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001490
1491 redraw
1492 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1493 call assert_equal(X, line)
1494
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001495 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001496 redraw
1497
1498 " Extend so > window width
1499 let X .= 'x'
1500
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001501 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1502 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001503
1504 redraw
1505 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1506 call assert_equal(X[ : -2 ], line)
1507
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001508 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001509 redraw
1510
1511 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001512 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1513 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001514
1515 redraw
1516 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1517 call assert_equal(X[ : -2 ], line)
1518
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001519 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001520 redraw
1521
1522 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001523 let p = popup_create(X,
1524 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1525 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001526
1527 redraw
1528 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1529 let e_line = ' ' . X[ 1 : -2 ]
1530 call assert_equal(e_line, line)
1531
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001532 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001533 redraw
1534
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001535 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001536 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001537endfunc
1538
1539func Test_popup_moved()
1540 new
1541 call test_override('char_avail', 1)
1542 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1543
1544 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001545 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001546 redraw
1547 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001548 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001549 " trigger the check for last_cursormoved by going into insert mode
1550 call feedkeys("li\<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("hi\<Esc>", 'xt')
1560 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001561 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001562
1563 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001564 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001565 redraw
1566 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001567 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001568 call feedkeys("li\<Esc>", 'xt')
1569 call assert_equal(1, popup_getpos(winid).visible)
1570 call feedkeys("ei\<Esc>", 'xt')
1571 call assert_equal(1, popup_getpos(winid).visible)
1572 call feedkeys("eli\<Esc>", 'xt')
1573 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001574 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001575
Bram Moolenaar17627312019-06-02 19:53:44 +02001576 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001577 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001578 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001579 redraw
1580 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001581 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001582 call feedkeys("eli\<Esc>", 'xt')
1583 call assert_equal(1, popup_getpos(winid).visible)
1584 call feedkeys("wi\<Esc>", 'xt')
1585 call assert_equal(1, popup_getpos(winid).visible)
1586 call feedkeys("Eli\<Esc>", 'xt')
1587 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001588 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001589
1590 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001591 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001592 redraw
1593 call assert_equal(1, popup_getpos(winid).visible)
1594 call feedkeys("eli\<Esc>", 'xt')
1595 call feedkeys("ei\<Esc>", 'xt')
1596 call assert_equal(1, popup_getpos(winid).visible)
1597 call feedkeys("eli\<Esc>", 'xt')
1598 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001599 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001600
1601 bwipe!
1602 call test_override('ALL', 0)
1603endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001604
1605func Test_notifications()
1606 if !has('timers')
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02001607 throw 'Skipped: timer feature not supported'
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001608 endif
1609 if !CanRunVimInTerminal()
1610 throw 'Skipped: cannot make screendumps'
1611 endif
1612
1613 call writefile([
1614 \ "call setline(1, range(1, 20))",
1615 \ "hi Notification ctermbg=lightblue",
1616 \ "call popup_notification('first notification', {})",
1617 \], 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001618 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001619 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1620
1621 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001622 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1623 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001624 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1625
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001626 " clean up
1627 call StopVimInTerminal(buf)
1628 call delete('XtestNotifications')
1629endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001630
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001631func Test_popup_scrollbar()
1632 if !CanRunVimInTerminal()
1633 throw 'Skipped: cannot make screendumps'
1634 endif
1635
1636 let lines =<< trim END
1637 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001638 hi ScrollThumb ctermbg=blue
1639 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001640 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001641 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001642 \ minwidth: 8,
1643 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001644 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001645 func ScrollUp()
1646 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1647 endfunc
1648 func ScrollDown()
1649 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1650 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001651 func ClickTop()
1652 call feedkeys("\<F4>\<LeftMouse>", "xt")
1653 endfunc
1654 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001655 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001656 call feedkeys("\<F5>\<LeftMouse>", "xt")
1657 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001658 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001659 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1660 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001661 END
1662 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001663 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001664 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1665
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001666 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001667 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001668 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1669
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001670 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001671 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001672 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1673
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001674 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001675 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001676 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1677
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001678 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb'})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001679 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1680 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1681
1682 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1683 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1684
1685 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001686 " wait a bit, otherwise it fails sometimes (double click recognized?)
1687 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001688 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1689 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1690
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001691 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1692 sleep 100m
1693 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1694 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1695
1696 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1697 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1698
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001699 " clean up
1700 call StopVimInTerminal(buf)
1701 call delete('XtestPopupScroll')
1702endfunc
1703
Bram Moolenaar437a7462019-07-05 20:17:22 +02001704func Test_popup_fitting_scrollbar()
1705 " this was causing a crash, divide by zero
1706 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001707 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001708 \ scrollbar: 1,
1709 \ maxwidth: 10,
1710 \ maxheight: 5,
1711 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001712 redraw
1713 call popup_clear()
1714endfunc
1715
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001716func Test_popup_settext()
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001717 if !CanRunVimInTerminal()
1718 throw 'Skipped: cannot make screendumps'
1719 endif
1720
1721 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001722 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001723 let p = popup_create('test', opts)
1724 call popup_settext(p, 'this is a text')
1725 END
1726
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001727 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001728 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001729 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1730
1731 " Setting to empty string clears it
1732 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1733 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1734
1735 " Setting a list
1736 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1737 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1738
1739 " Shrinking with a list
1740 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1741 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1742
1743 " Growing with a list
1744 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1745 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1746
1747 " Empty list clears
1748 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1749 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1750
1751 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001752 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001753 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1754
1755 " clean up
1756 call StopVimInTerminal(buf)
1757 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001758endfunc
1759
1760func Test_popup_hidden()
1761 new
1762
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001763 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001764 redraw
1765 call assert_equal(0, popup_getpos(winid).visible)
1766 call popup_close(winid)
1767
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001768 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001769 redraw
1770 call assert_equal(0, popup_getpos(winid).visible)
1771 call popup_close(winid)
1772
1773 func QuitCallback(id, res)
1774 let s:cb_winid = a:id
1775 let s:cb_res = a:res
1776 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001777 let winid = popup_dialog('make a choice', #{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001778 \ filter: 'popup_filter_yesno',
1779 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001780 \ })
1781 redraw
1782 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001783 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1784 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001785 exe "normal anot used by filter\<Esc>"
1786 call assert_equal('not used by filter', getline(1))
1787
1788 call popup_show(winid)
1789 call feedkeys('y', "xt")
1790 call assert_equal(1, s:cb_res)
1791
1792 bwipe!
1793 delfunc QuitCallback
1794endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001795
1796" Test options not checked elsewhere
1797func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001798 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001799 let options = popup_getoptions(winid)
1800 call assert_equal(1, options.wrap)
1801 call assert_equal(0, options.drag)
1802 call assert_equal('Beautiful', options.highlight)
1803
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001804 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001805 let options = popup_getoptions(winid)
1806 call assert_equal(0, options.wrap)
1807 call assert_equal(1, options.drag)
1808 call assert_equal('Another', options.highlight)
1809
1810 call popup_close(winid)
1811endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001812
1813func Test_popupwin_garbage_collect()
1814 func MyPopupFilter(x, winid, c)
1815 " NOP
1816 endfunc
1817
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001818 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001819 call test_garbagecollect_now()
1820 redraw
1821 " Must not crach caused by invalid memory access
1822 call feedkeys('j', 'xt')
1823 call assert_true(v:true)
1824
1825 call popup_close(winid)
1826 delfunc MyPopupFilter
1827endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001828
1829func Test_popupwin_with_buffer()
1830 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1831 let buf = bufadd('XsomeFile')
1832 call assert_equal(0, bufloaded(buf))
1833 let winid = popup_create(buf, {})
1834 call assert_notequal(0, winid)
1835 let pos = popup_getpos(winid)
1836 call assert_equal(2, pos.height)
1837 call assert_equal(1, bufloaded(buf))
1838 call popup_close(winid)
1839 call assert_equal({}, popup_getpos(winid))
1840 call assert_equal(1, bufloaded(buf))
1841 exe 'bwipe! ' .. buf
Bram Moolenaar7866b872019-07-01 22:21:01 +02001842
1843 edit test_popupwin.vim
1844 let winid = popup_create(bufnr(''), {})
1845 redraw
1846 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001847 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001848endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02001849
1850func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001851 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001852 \ maxwidth: 40,
1853 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02001854 \ })
1855 for top in range(1, 20)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001856 call popup_setoptions(winid, #{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02001857 redraw
1858 call assert_equal(19, popup_getpos(winid).width)
1859 endfor
1860 call popup_clear()
1861endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02001862
1863func Test_popupwin_buf_close()
1864 let buf = bufadd('Xtestbuf')
1865 call bufload(buf)
1866 call setbufline(buf, 1, ['just', 'some', 'lines'])
1867 let winid = popup_create(buf, {})
1868 redraw
1869 call assert_equal(3, popup_getpos(winid).height)
1870 let bufinfo = getbufinfo(buf)[0]
1871 call assert_equal(1, bufinfo.changed)
1872 call assert_equal(0, bufinfo.hidden)
1873 call assert_equal(0, bufinfo.listed)
1874 call assert_equal(1, bufinfo.loaded)
1875 call assert_equal([], bufinfo.windows)
1876 call assert_equal([winid], bufinfo.popups)
1877
1878 call popup_close(winid)
1879 call assert_equal({}, popup_getpos(winid))
1880 let bufinfo = getbufinfo(buf)[0]
1881 call assert_equal(1, bufinfo.changed)
1882 call assert_equal(1, bufinfo.hidden)
1883 call assert_equal(0, bufinfo.listed)
1884 call assert_equal(1, bufinfo.loaded)
1885 call assert_equal([], bufinfo.windows)
1886 call assert_equal([], bufinfo.popups)
1887 exe 'bwipe! ' .. buf
1888endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02001889
1890func Test_popup_menu_with_maxwidth()
1891 if !CanRunVimInTerminal()
1892 throw 'Skipped: cannot make screendumps'
1893 endif
1894
1895 let lines =<< trim END
1896 call setline(1, range(1, 10))
1897 hi ScrollThumb ctermbg=blue
1898 hi ScrollBar ctermbg=red
1899 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001900 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001901 \ maxwidth: 10,
1902 \ maxheight: 3,
1903 \ pos : 'topleft',
1904 \ col : a:col,
1905 \ line : a:line,
1906 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02001907 \ })
1908 endfunc
1909 call PopupMenu(['x'], 1, 1)
1910 call PopupMenu(['123456789|'], 1, 16)
1911 call PopupMenu(['123456789|' .. ' '], 7, 1)
1912 call PopupMenu([repeat('123456789|', 100)], 7, 16)
1913 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
1914 END
1915 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001916 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02001917 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
1918
1919 " close the menu popupwin.
1920 call term_sendkeys(buf, " ")
1921 call term_sendkeys(buf, " ")
1922 call term_sendkeys(buf, " ")
1923 call term_sendkeys(buf, " ")
1924 call term_sendkeys(buf, " ")
1925
1926 " clean up
1927 call StopVimInTerminal(buf)
1928 call delete('XtestPopupMenuMaxWidth')
1929endfunc
1930
Bram Moolenaara901a372019-07-13 16:38:50 +02001931func Test_popup_menu_with_scrollbar()
1932 if !CanRunVimInTerminal()
1933 throw 'Skipped: cannot make screendumps'
1934 endif
1935
1936 let lines =<< trim END
1937 call setline(1, range(1, 20))
1938 hi ScrollThumb ctermbg=blue
1939 hi ScrollBar ctermbg=red
1940 call popup_menu(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001941 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001942 \ minwidth: 8,
1943 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02001944 \ })
1945 END
1946 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001947 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02001948
1949 call term_sendkeys(buf, "j")
1950 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
1951
1952 call term_sendkeys(buf, "jjj")
1953 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
1954
1955 " if the cursor is the bottom line, it stays at the bottom line.
1956 call term_sendkeys(buf, repeat("j", 20))
1957 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
1958
1959 call term_sendkeys(buf, "kk")
1960 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
1961
1962 call term_sendkeys(buf, "k")
1963 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
1964
1965 " if the cursor is in the top line, it stays in the top line.
1966 call term_sendkeys(buf, repeat("k", 20))
1967 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
1968
1969 " close the menu popupwin.
1970 call term_sendkeys(buf, " ")
1971
1972 " clean up
1973 call StopVimInTerminal(buf)
1974 call delete('XtestPopupMenuScroll')
1975endfunc
1976
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001977func Test_popup_menu_filter()
1978 if !CanRunVimInTerminal()
1979 throw 'Skipped: cannot make screendumps'
1980 endif
1981
1982 let lines =<< trim END
1983 function! MyFilter(winid, key) abort
1984 if a:key == "0"
1985 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
1986 return 1
1987 endif
1988 if a:key == "G"
1989 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
1990 return 1
1991 endif
1992 if a:key == "j"
1993 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
1994 return 1
1995 endif
1996 if a:key == "k"
1997 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
1998 return 1
1999 endif
2000 if a:key == 'x'
2001 call popup_close(a:winid)
2002 return 1
2003 endif
2004 return 0
2005 endfunction
2006 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2007 \ maxheight : 3,
2008 \ filter : 'MyFilter'
2009 \ })
2010 END
2011 call writefile(lines, 'XtestPopupMenuFilter')
2012 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2013
2014 call term_sendkeys(buf, "j")
2015 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2016
2017 call term_sendkeys(buf, "k")
2018 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2019
2020 call term_sendkeys(buf, "G")
2021 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2022
2023 call term_sendkeys(buf, "0")
2024 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2025
2026 call term_sendkeys(buf, "x")
2027
2028 " clean up
2029 call StopVimInTerminal(buf)
2030 call delete('XtestPopupMenuFilter')
2031endfunc
2032
2033func Test_popup_cursorline()
2034 if !CanRunVimInTerminal()
2035 throw 'Skipped: cannot make screendumps'
2036 endif
2037
2038 let winid = popup_create('some text', {})
2039 call assert_equal(0, popup_getoptions(winid).cursorline)
2040 call popup_close(winid)
2041
2042 let winid = popup_create('some text', #{ cursorline: 1, })
2043 call assert_equal(1, popup_getoptions(winid).cursorline)
2044 call popup_close(winid)
2045
2046 let winid = popup_create('some text', #{ cursorline: 0, })
2047 call assert_equal(0, popup_getoptions(winid).cursorline)
2048 call popup_close(winid)
2049
2050 let winid = popup_menu('some text', {})
2051 call assert_equal(1, popup_getoptions(winid).cursorline)
2052 call popup_close(winid)
2053
2054 let winid = popup_menu('some text', #{ cursorline: 1, })
2055 call assert_equal(1, popup_getoptions(winid).cursorline)
2056 call popup_close(winid)
2057
2058 let winid = popup_menu('some text', #{ cursorline: 0, })
2059 call assert_equal(0, popup_getoptions(winid).cursorline)
2060 call popup_close(winid)
2061
2062 " ---------
2063 " Pattern 1
2064 " ---------
2065 let lines =<< trim END
2066 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2067 END
2068 call writefile(lines, 'XtestPopupCursorLine')
2069 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2070 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2071 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2072 call StopVimInTerminal(buf)
2073
2074 " ---------
2075 " Pattern 2
2076 " ---------
2077 let lines =<< trim END
2078 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2079 END
2080 call writefile(lines, 'XtestPopupCursorLine')
2081 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2082 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2083 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2084 call StopVimInTerminal(buf)
2085
2086 " ---------
2087 " Pattern 3
2088 " ---------
2089 let lines =<< trim END
2090 function! MyFilter(winid, key) abort
2091 if a:key == "j"
2092 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2093 return 1
2094 endif
2095 if a:key == 'x'
2096 call popup_close(a:winid)
2097 return 1
2098 endif
2099 return 0
2100 endfunction
2101 call popup_menu(['111', '222', '333'], #{
2102 \ cursorline : 0,
2103 \ maxheight : 2,
2104 \ filter : 'MyFilter',
2105 \ })
2106 END
2107 call writefile(lines, 'XtestPopupCursorLine')
2108 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2109 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2110 call term_sendkeys(buf, "j")
2111 call term_sendkeys(buf, "j")
2112 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2113 call term_sendkeys(buf, "x")
2114 call StopVimInTerminal(buf)
2115
2116 " ---------
2117 " Pattern 4
2118 " ---------
2119 let lines =<< trim END
2120 function! MyFilter(winid, key) abort
2121 if a:key == "j"
2122 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2123 return 1
2124 endif
2125 if a:key == 'x'
2126 call popup_close(a:winid)
2127 return 1
2128 endif
2129 return 0
2130 endfunction
2131 call popup_menu(['111', '222', '333'], #{
2132 \ cursorline : 1,
2133 \ maxheight : 2,
2134 \ filter : 'MyFilter',
2135 \ })
2136 END
2137 call writefile(lines, 'XtestPopupCursorLine')
2138 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2139 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2140 call term_sendkeys(buf, "j")
2141 call term_sendkeys(buf, "j")
2142 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2143 call term_sendkeys(buf, "x")
2144 call StopVimInTerminal(buf)
2145
2146 call delete('XtestPopupCursorLine')
2147endfunc
2148
Bram Moolenaarf914a332019-07-20 15:09:56 +02002149func Test_previewpopup()
2150 if !CanRunVimInTerminal()
2151 throw 'Skipped: cannot make screendumps'
2152 endif
2153 call writefile([
2154 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2155 \ "another\tXtagfile\t/^this is another",
2156 \ "theword\tXtagfile\t/^theword"],
2157 \ 'Xtags')
2158 call writefile(range(1,20)
2159 \ + ['theword is here']
2160 \ + range(22, 27)
2161 \ + ['this is another place']
2162 \ + range(29, 40),
2163 \ "Xtagfile")
2164 let lines =<< trim END
2165 set tags=Xtags
2166 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002167 \ 'one',
2168 \ 'two',
2169 \ 'three',
2170 \ 'four',
2171 \ 'five',
2172 \ 'six',
2173 \ 'seven',
2174 \ 'find theword somewhere',
2175 \ 'nine',
2176 \ 'this is another word',
2177 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002178 set previewpopup=height:4,width:40
2179 END
2180 call writefile(lines, 'XtestPreviewPopup')
2181 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2182
2183 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2184 call term_sendkeys(buf, ":\<CR>")
2185 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2186
2187 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2188 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2189
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002190 call term_sendkeys(buf, ":call popup_move(popup_getpreview(), #{col: 15})\<CR>")
2191 call term_sendkeys(buf, ":\<CR>")
2192 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2193
2194 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2195 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2196
Bram Moolenaarf914a332019-07-20 15:09:56 +02002197 call StopVimInTerminal(buf)
2198 call delete('Xtags')
2199 call delete('Xtagfile')
2200 call delete('XtestPreviewPopup')
2201endfunc
2202
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002203" vim: shiftwidth=2 sts=2