blob: 6c1ac524a2f9e52c0b7298543328aadec140b1d5 [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()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
15 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020016 call prop_type_add('comment', #{highlight: 'Comment'})
17 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
18 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020019 call setwinvar(winid2, '&wincolor', 'PopupColor2')
20 END
21 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020022 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020023 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
24
Bram Moolenaarec583842019-05-26 14:11:23 +020025 " Add a tabpage
26 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020027 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020028 \ .. "#{text: 'other tab'},"
29 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020030 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020031 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020032 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020033 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
34
35 " switch back to first tabpage
36 call term_sendkeys(buf, "gt")
37 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
38
39 " close that tabpage
40 call term_sendkeys(buf, ":quit!\<CR>")
41 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
42
Bram Moolenaar202d9822019-06-11 21:56:30 +020043 " set 'columns' to a small value, size must be recomputed
44 call term_sendkeys(buf, ":let cols = &columns\<CR>")
45 call term_sendkeys(buf, ":set columns=12\<CR>")
46 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
47 call term_sendkeys(buf, ":let &columns = cols\<CR>")
48
Bram Moolenaar17146962019-05-30 00:12:11 +020049 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020050 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020051 call term_sendkeys(buf, ":redraw\<CR>")
52 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
53
Bram Moolenaar17146962019-05-30 00:12:11 +020054 " show not fitting line at bottom
55 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
56 call term_sendkeys(buf, ":redraw\<CR>")
57 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
58
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020059 " move popup over ruler
60 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020061 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020062 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020063 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
64
65 " clear all popups after moving the cursor a bit, so that ruler is updated
66 call term_sendkeys(buf, "axxx\<Esc>")
67 call term_wait(buf)
68 call term_sendkeys(buf, "0")
69 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020070 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020071 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
72
Bram Moolenaar4d784b22019-05-25 19:51:39 +020073 " clean up
74 call StopVimInTerminal(buf)
75 call delete('XtestPopup')
76endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020077
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020078func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020079 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020080
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020081 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020082 let lines =<< trim END
83 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020084 call popup_create('hello border', #{line: 2, col: 3, border: []})
85 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020086 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020087 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
88 call popup_create('paddings', #{line: 6, col: 23, padding: [1, 3, 2, 4]})
89 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
90 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 +020091 END
92 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
93 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020094 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020095 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
96
97 call StopVimInTerminal(buf)
98 call delete('XtestPopupBorder')
99 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200100
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200101 let lines =<< trim END
102 call setline(1, range(1, 100))
103 hi BlueColor ctermbg=lightblue
104 hi TopColor ctermbg=253
105 hi RightColor ctermbg=245
106 hi BottomColor ctermbg=240
107 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200108 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
109 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
110 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
111 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
112 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
113 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200114 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200115 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200116 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200117 END
118 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200119 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200120 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
121
Bram Moolenaarad24a712019-06-17 20:05:45 +0200122 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200123 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 +0200124 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
125
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200126 " check multi-byte border only with 'ambiwidth' single
127 if &ambiwidth == 'single'
128 call term_sendkeys(buf, ":call MultiByte()\<CR>")
129 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
130 endif
131
Bram Moolenaar790498b2019-06-01 22:15:29 +0200132 call StopVimInTerminal(buf)
133 call delete('XtestPopupBorder')
134
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200135 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200136 \ line: 2,
137 \ core_line: 3,
138 \ col: 3,
139 \ core_col: 4,
140 \ width: 14,
141 \ core_width: 12,
142 \ height: 3,
143 \ core_height: 1,
144 \ firstline: 1,
145 \ scrollbar: 0,
146 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200147 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200148 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200149 let options = popup_getoptions(winid)
150 call assert_equal([], options.border)
151 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200152
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200153 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200154 let with_border_or_padding.width = 15
155 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200156 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200157 let options = popup_getoptions(winid)
158 call assert_false(has_key(options, "border"))
159 call assert_equal([], options.padding)
160
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200161 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200162 \ padding: [1, 2, 3, 4],
163 \ border: [4, 0, 7, 8],
164 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
165 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200166 \ })
167 let options = popup_getoptions(winid)
168 call assert_equal([1, 0, 1, 1], options.border)
169 call assert_equal([1, 2, 3, 4], options.padding)
170 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
171 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200172
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200173 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
174 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200175 \ line: 3,
176 \ core_line: 5,
177 \ col: 8,
178 \ core_col: 10,
179 \ width: 14,
180 \ core_width: 10,
181 \ height: 5,
182 \ scrollbar: 0,
183 \ core_height: 1,
184 \ firstline: 1,
185 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200186
187 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200188endfunc
189
Bram Moolenaarb4230122019-05-30 18:40:53 +0200190func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200191 CheckScreendump
192
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200193 let lines =<< trim END
194 call setline(1, range(1, 100))
195 hi PopupColor ctermbg=lightblue
196 let winid = popup_create([
197 \ '#include <stdio.h>',
198 \ 'int main(void)',
199 \ '{',
200 \ ' printf(123);',
201 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200202 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200203 call win_execute(winid, 'set syntax=cpp')
204 END
205 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200206 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200207 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
208
209 " clean up
210 call StopVimInTerminal(buf)
211 call delete('XtestPopup')
212endfunc
213
214func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200215 CheckScreendump
216
Bram Moolenaar402502d2019-05-30 22:07:36 +0200217 let lines =<< trim END
218 call setline(1, range(1, 100))
219 hi PopupColor ctermbg=lightgrey
220 let winid = popup_create([
221 \ '#include <stdio.h>',
222 \ 'int main(void)',
223 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200224 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200225 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200226 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200227 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
228 END
229 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200230 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200231 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
232
233 " clean up
234 call StopVimInTerminal(buf)
235 call delete('XtestPopup')
236endfunc
237
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200238func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200239 CheckScreendump
240
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200241 let lines =<< trim END
242 call setline(1, ['111 222 333', '444 555 666'])
243 let winid = popup_create([
244 \ '111 222 333',
245 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200246 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200247 set hlsearch
248 /666
249 call matchadd('ErrorMsg', '111')
250 call matchadd('ErrorMsg', '444')
251 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
252 call win_execute(winid, "call matchadd('ErrorMsg', '555')")
253 END
254 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200255 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200256 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
257
258 " clean up
259 call StopVimInTerminal(buf)
260 call delete('XtestPopupMatches')
261endfunc
262
Bram Moolenaar399d8982019-06-02 15:34:29 +0200263func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200264 CheckScreendump
265
Bram Moolenaar399d8982019-06-02 15:34:29 +0200266 let lines =<< trim END
267 call setline(1, repeat([repeat('-', 60)], 15))
268 set so=0
269 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200270 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200271 \ line: 'cursor+1',
272 \ col: 'cursor',
273 \ pos: 'topleft',
274 \ border: [],
275 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200276 \ })
277 normal 25|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200278 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200279 \ line: 'cursor+1',
280 \ col: 'cursor',
281 \ pos: 'topright',
282 \ border: [],
283 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200284 \ })
285 normal 9G29|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200286 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200287 \ line: 'cursor-1',
288 \ col: 'cursor',
289 \ pos: 'botleft',
290 \ border: [],
291 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200292 \ })
293 normal 51|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200294 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200295 \ line: 'cursor-1',
296 \ col: 'cursor',
297 \ pos: 'botright',
298 \ border: [],
299 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200300 \ })
301 END
302 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200303 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200304 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
305
306 " clean up
307 call StopVimInTerminal(buf)
308 call delete('XtestPopupCorners')
309endfunc
310
Bram Moolenaar8d241042019-06-12 23:40:01 +0200311func Test_popup_firstline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200312 CheckScreendump
313
Bram Moolenaar8d241042019-06-12 23:40:01 +0200314 let lines =<< trim END
315 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200316 call popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200317 \ maxheight: 4,
318 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200319 \ })
320 END
321 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200322 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200323 call VerifyScreenDump(buf, 'Test_popupwin_firstline', {})
324
325 " clean up
326 call StopVimInTerminal(buf)
327 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200328
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200329 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200330 \ maxheight: 2,
331 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200332 \ })
333 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200334 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200335 call assert_equal(1, popup_getoptions(winid).firstline)
336
337 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200338endfunc
339
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200340func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200341 CheckScreendump
342
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200343 " create a popup that covers the command line
344 let lines =<< trim END
345 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200346 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200347 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200348 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200349 \ border: [],
350 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200351 \ })
352 func Dragit()
353 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
354 endfunc
355 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
356 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200357 func Resize()
358 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
359 endfunc
360 map <silent> <F5> :call test_setmouse(6, 41)<CR>
361 map <silent> <F6> :call test_setmouse(7, 45)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200362 END
363 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200364 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200365 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
366
367 call term_sendkeys(buf, ":call Dragit()\<CR>")
368 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
369
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200370 call term_sendkeys(buf, ":call Resize()\<CR>")
371 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
372
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200373 " clean up
374 call StopVimInTerminal(buf)
375 call delete('XtestPopupDrag')
376endfunc
377
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200378func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200379 CheckScreendump
380
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200381 let lines =<< trim END
382 call setline(1, range(1, 20))
383 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200384 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200385 \ close: 'button',
386 \ border: [],
387 \ line: 1,
388 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200389 \ })
390 func CloseMsg(id, result)
391 echomsg 'Popup closed with ' .. a:result
392 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200393 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200394 \ close: 'click',
395 \ line: 3,
396 \ col: 15,
397 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200398 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200399 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200400 \ close: 'button',
401 \ line: 5,
402 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200403 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200404 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200405 \ close: 'button',
406 \ padding: [],
407 \ line: 5,
408 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200409 \ })
410 func CloseWithX()
411 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
412 endfunc
413 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
414 func CloseWithClick()
415 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
416 endfunc
417 map <silent> <F4> :call test_setmouse(3, 17)<CR>
418 END
419 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200420 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200421 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
422
423 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
424 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
425
426 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
427 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
428
429 " clean up
430 call StopVimInTerminal(buf)
431 call delete('XtestPopupClose')
432endfunction
433
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200434func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200435 CheckScreendump
436
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200437 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200438 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200439 hi PopupColor ctermbg=lightgrey
440 let winid = popup_create([
441 \ 'some text',
442 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200443 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200444 \ line: 1,
445 \ col: 10,
446 \ wrap: 0,
447 \ fixed: 1,
448 \ zindex: 90,
449 \ padding: [],
450 \ highlight: 'PopupColor',
451 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200452 call popup_create([
453 \ 'xxxxxxxxx',
454 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200455 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200456 \ line: 3,
457 \ col: 18,
458 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200459 let winidb = popup_create([
460 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200461 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200462 \ line: 7,
463 \ col: 10,
464 \ wrap: 0,
465 \ fixed: 1,
466 \ close: 'button',
467 \ zindex: 90,
468 \ padding: [],
469 \ border: [],
470 \ 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 +0200471 END
472 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200473 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200474 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
475
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200476 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
477 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200478 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200479 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
480
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200481 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
482 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200483 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200484 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
485
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200486 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
487 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200488 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200489 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
490
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200491 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
492 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200493 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200494 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
495
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200496 " clean up
497 call StopVimInTerminal(buf)
498 call delete('XtestPopupMask')
499endfunc
500
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200501func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200502 CheckScreendump
503 CheckFeature clipboard_working
504
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200505 " create a popup with some text to be selected
506 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200507 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200508 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200509 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200510 \ drag: 1,
511 \ border: [],
512 \ line: 3,
513 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200514 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200515 \ })
516 func Select1()
517 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
518 endfunc
519 map <silent> <F3> :call test_setmouse(4, 15)<CR>
520 map <silent> <F4> :call test_setmouse(6, 23)<CR>
521 END
522 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200523 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200524 call term_sendkeys(buf, ":call Select1()\<CR>")
525 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
526
527 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
528 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200529 " clean the command line, sometimes it still shows a command
530 call term_sendkeys(buf, ":\<esc>")
531
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200532 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
533
534 " clean up
535 call StopVimInTerminal(buf)
536 call delete('XtestPopupSelect')
537endfunc
538
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200539func Test_popup_in_tab()
540 " default popup is local to tab, not visible when in other tab
541 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200542 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200543 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200544 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200545 tabnew
546 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200547 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200548 quit
549 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200550
551 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200552 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200553 " buffer is gone now
554 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200555
556 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200557 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200558 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200559 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200560 tabnew
561 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200562 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200563 quit
564 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200565 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200566
567 " create popup in other tab
568 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200569 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200570 call assert_equal(0, popup_getpos(winid).visible)
571 call assert_equal(1, popup_getoptions(winid).tabpage)
572 quit
573 call assert_equal(1, popup_getpos(winid).visible)
574 call assert_equal(0, popup_getoptions(winid).tabpage)
575 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200576endfunc
577
578func Test_popup_valid_arguments()
579 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200580 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200581 let pos = popup_getpos(winid)
582 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200583 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200584
585 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200586 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200587 let pos = popup_getpos(winid)
588 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200589 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200590
591 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200592 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200593 let pos = popup_getpos(winid)
594 let around = (&columns - pos.width) / 2
595 call assert_inrange(around - 1, around + 1, pos.col)
596 let around = (&lines - pos.height) / 2
597 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200598 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200599endfunc
600
601func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200602 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200603 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200604 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200605 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200606
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200607 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200608 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200609 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200610 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200611 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200612 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200613 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200614 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200615
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200616 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200617 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200618 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200619 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200620 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200621 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200622 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200623 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200624
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200625 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200626 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200627 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200628 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200629 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
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", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200632 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200633 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
634 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200635 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
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: "text"}, 666], {})', 'E715:')
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: "text", props: "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: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200643 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200644 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
645 call popup_clear()
646 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200647 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200648 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200649endfunc
650
Bram Moolenaareea16992019-05-31 17:34:48 +0200651func Test_win_execute_closing_curwin()
652 split
653 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200654 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200655 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200656endfunc
657
658func Test_win_execute_not_allowed()
659 let winid = popup_create('some text', {})
660 call assert_fails('call win_execute(winid, "split")', 'E994:')
661 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
662 call assert_fails('call win_execute(winid, "close")', 'E994:')
663 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200664 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200665 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
666 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
667 call assert_fails('call win_execute(winid, "next")', 'E994:')
668 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
669 call assert_fails('call win_execute(winid, "buf")', 'E994:')
670 call assert_fails('call win_execute(winid, "edit")', 'E994:')
671 call assert_fails('call win_execute(winid, "enew")', 'E994:')
672 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
673 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
674 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
675 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200676 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200677endfunc
678
Bram Moolenaar402502d2019-05-30 22:07:36 +0200679func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200680 CheckScreendump
681
Bram Moolenaar402502d2019-05-30 22:07:36 +0200682 let lines =<< trim END
683 call setline(1, range(1, 100))
684 let winid = popup_create(
685 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200686 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200687 END
688 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200689 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200690 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
691
692 " clean up
693 call StopVimInTerminal(buf)
694 call delete('XtestPopup')
695endfunc
696
697func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200698 CheckScreendump
699
Bram Moolenaar402502d2019-05-30 22:07:36 +0200700 let lines =<< trim END
701 call setline(1, range(1, 100))
702 let winid = popup_create(
703 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200704 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200705 END
706 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200707 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200708 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
709
710 " clean up
711 call StopVimInTerminal(buf)
712 call delete('XtestPopup')
713endfunc
714
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200715func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200716 CheckScreendump
717
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200718 let lines =<< trim END
719 set showbreak=>>\
720 call setline(1, range(1, 20))
721 let winid = popup_dialog(
722 \ 'a long line here',
723 \ #{filter: 'popup_filter_yesno'})
724 END
725 call writefile(lines, 'XtestPopupShowbreak')
726 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
727 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
728
729 " clean up
730 call term_sendkeys(buf, "y")
731 call StopVimInTerminal(buf)
732 call delete('XtestPopupShowbreak')
733endfunc
734
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200735func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200736 CheckFeature timers
737
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200738 topleft vnew
739 call setline(1, 'hello')
740
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200741 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200742 \ line: 1,
743 \ col: 1,
744 \ minwidth: 20,
745 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200746 \})
747 redraw
748 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
749 call assert_equal('world', line)
750
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200751 call assert_equal(winid, popup_locate(1, 1))
752 call assert_equal(winid, popup_locate(1, 20))
753 call assert_equal(0, popup_locate(1, 21))
754 call assert_equal(0, popup_locate(2, 1))
755
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200756 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200757 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200758 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
759 call assert_equal('hello', line)
760
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200761 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200762 \ line: &lines,
763 \ col: 10,
764 \ minwidth: 20,
765 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200766 \})
767 redraw
768 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
769 call assert_match('.*on the command line.*', line)
770
771 sleep 700m
772 redraw
773 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
774 call assert_notmatch('.*on the command line.*', line)
775
776 bwipe!
777endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200778
779func Test_popup_hide()
780 topleft vnew
781 call setline(1, 'hello')
782
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200783 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200784 \ line: 1,
785 \ col: 1,
786 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200787 \})
788 redraw
789 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
790 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200791 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200792 " buffer is still listed and active
793 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200794
795 call popup_hide(winid)
796 redraw
797 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
798 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200799 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200800 " buffer is still listed but hidden
801 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200802
803 call popup_show(winid)
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 Moolenaar2cd0dce2019-05-26 22:17:52 +0200808
809
810 call popup_close(winid)
811 redraw
812 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
813 call assert_equal('hello', line)
814
815 " error is given for existing non-popup window
816 call assert_fails('call popup_hide(win_getid())', 'E993:')
817
818 " no error non-existing window
819 call popup_hide(1234234)
820 call popup_show(41234234)
821
822 bwipe!
823endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200824
825func Test_popup_move()
826 topleft vnew
827 call setline(1, 'hello')
828
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200829 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200830 \ line: 1,
831 \ col: 1,
832 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200833 \})
834 redraw
835 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
836 call assert_equal('world ', line)
837
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200838 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200839 redraw
840 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
841 call assert_equal('hello ', line)
842 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
843 call assert_equal('~world', line)
844
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200845 call popup_move(winid, #{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200846 redraw
847 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
848 call assert_equal('hworld', line)
849
850 call popup_close(winid)
851
852 bwipe!
853endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200854
Bram Moolenaar402502d2019-05-30 22:07:36 +0200855func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200856 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200857 \ line: 2,
858 \ col: 3,
859 \ minwidth: 10,
860 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200861 \})
862 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200863 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200864 call assert_equal(2, res.line)
865 call assert_equal(3, res.col)
866 call assert_equal(10, res.width)
867 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200868 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200869
870 call popup_close(winid)
871endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200872
873func Test_popup_width_longest()
874 let tests = [
875 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
876 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
877 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
878 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
879 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
880 \ ]
881
882 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200883 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200884 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200885 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200886 call assert_equal(test[1], position.width)
887 call popup_close(winid)
888 endfor
889endfunc
890
891func Test_popup_wraps()
892 let tests = [
893 \ ['nowrap', 6, 1],
894 \ ['a line that wraps once', 12, 2],
895 \ ['a line that wraps two times', 12, 3],
896 \ ]
897 for test in tests
898 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200899 \ #{line: 2, col: 3, maxwidth: 12})
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 assert_equal(test[2], position.height)
904
905 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200906 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200907 endfor
908endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200909
910func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200911 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200912 \ line: 2,
913 \ col: 3,
914 \ minwidth: 10,
915 \ minheight: 11,
916 \ maxwidth: 20,
917 \ maxheight: 21,
918 \ zindex: 100,
919 \ time: 5000,
920 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200921 \})
922 redraw
923 let res = popup_getoptions(winid)
924 call assert_equal(2, res.line)
925 call assert_equal(3, res.col)
926 call assert_equal(10, res.minwidth)
927 call assert_equal(11, res.minheight)
928 call assert_equal(20, res.maxwidth)
929 call assert_equal(21, res.maxheight)
930 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200931 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +0200932 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200933 if has('timers')
934 call assert_equal(5000, res.time)
935 endif
936 call popup_close(winid)
937
938 let winid = popup_create('hello', {})
939 redraw
940 let res = popup_getoptions(winid)
941 call assert_equal(0, res.line)
942 call assert_equal(0, res.col)
943 call assert_equal(0, res.minwidth)
944 call assert_equal(0, res.minheight)
945 call assert_equal(0, res.maxwidth)
946 call assert_equal(0, res.maxheight)
947 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200948 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200949 if has('timers')
950 call assert_equal(0, res.time)
951 endif
952 call popup_close(winid)
953 call assert_equal({}, popup_getoptions(winid))
954endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200955
956func Test_popup_option_values()
957 new
958 " window-local
959 setlocal number
960 setlocal nowrap
961 " buffer-local
962 setlocal omnifunc=Something
963 " global/buffer-local
964 setlocal path=/there
965 " global/window-local
966 setlocal scrolloff=9
967
968 let winid = popup_create('hello', {})
969 call assert_equal(0, getwinvar(winid, '&number'))
970 call assert_equal(1, getwinvar(winid, '&wrap'))
971 call assert_equal('', getwinvar(winid, '&omnifunc'))
972 call assert_equal(&g:path, getwinvar(winid, '&path'))
973 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
974
975 call popup_close(winid)
976 bwipe
977endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200978
979func Test_popup_atcursor()
980 topleft vnew
981 call setline(1, [
982 \ 'xxxxxxxxxxxxxxxxx',
983 \ 'xxxxxxxxxxxxxxxxx',
984 \ 'xxxxxxxxxxxxxxxxx',
985 \])
986
987 call cursor(2, 2)
988 redraw
989 let winid = popup_atcursor('vim', {})
990 redraw
991 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
992 call assert_equal('xvimxxxxxxxxxxxxx', line)
993 call popup_close(winid)
994
995 call cursor(3, 4)
996 redraw
997 let winid = popup_atcursor('vim', {})
998 redraw
999 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1000 call assert_equal('xxxvimxxxxxxxxxxx', line)
1001 call popup_close(winid)
1002
1003 call cursor(1, 1)
1004 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001005 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001006 \ line: 'cursor+2',
1007 \ col: 'cursor+1',
1008 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001009 redraw
1010 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1011 call assert_equal('xvimxxxxxxxxxxxxx', line)
1012 call popup_close(winid)
1013
1014 call cursor(3, 3)
1015 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001016 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001017 \ line: 'cursor-2',
1018 \ col: 'cursor-1',
1019 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001020 redraw
1021 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1022 call assert_equal('xvimxxxxxxxxxxxxx', line)
1023 call popup_close(winid)
1024
Bram Moolenaar402502d2019-05-30 22:07:36 +02001025 " just enough room above
1026 call cursor(3, 3)
1027 redraw
1028 let winid = popup_atcursor(['vim', 'is great'], {})
1029 redraw
1030 let pos = popup_getpos(winid)
1031 call assert_equal(1, pos.line)
1032 call popup_close(winid)
1033
1034 " not enough room above, popup goes below the cursor
1035 call cursor(3, 3)
1036 redraw
1037 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1038 redraw
1039 let pos = popup_getpos(winid)
1040 call assert_equal(4, pos.line)
1041 call popup_close(winid)
1042
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001043 " cursor in first line, popup in line 2
1044 call cursor(1, 1)
1045 redraw
1046 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1047 redraw
1048 let pos = popup_getpos(winid)
1049 call assert_equal(2, pos.line)
1050 call popup_close(winid)
1051
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001052 bwipe!
1053endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001054
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001055func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001056 CheckScreendump
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001057
1058 let lines =<< trim END
1059 call setline(1, range(1, 20))
1060 call setline(5, 'here is some text to hover over')
1061 set balloonevalterm
1062 set balloonexpr=BalloonExpr()
1063 set balloondelay=100
1064 func BalloonExpr()
1065 let s:winid = popup_beval([v:beval_text], {})
1066 return ''
1067 endfunc
1068 func Hover()
1069 call test_setmouse(5, 15)
1070 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1071 sleep 100m
1072 endfunc
1073 func MoveOntoPopup()
1074 call test_setmouse(4, 17)
1075 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1076 endfunc
1077 func MoveAway()
1078 call test_setmouse(5, 13)
1079 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1080 endfunc
1081 END
1082 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001083 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001084 call term_wait(buf, 100)
1085 call term_sendkeys(buf, 'j')
1086 call term_sendkeys(buf, ":call Hover()\<CR>")
1087 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1088
1089 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1090 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1091
1092 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1093 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1094
1095 " clean up
1096 call StopVimInTerminal(buf)
1097 call delete('XtestPopupBeval')
1098endfunc
1099
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001100func Test_popup_filter()
1101 new
1102 call setline(1, 'some text')
1103
1104 func MyPopupFilter(winid, c)
1105 if a:c == 'e'
1106 let g:eaten = 'e'
1107 return 1
1108 endif
1109 if a:c == '0'
1110 let g:ignored = '0'
1111 return 0
1112 endif
1113 if a:c == 'x'
1114 call popup_close(a:winid)
1115 return 1
1116 endif
1117 return 0
1118 endfunc
1119
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001120 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001121 redraw
1122
1123 " e is consumed by the filter
1124 call feedkeys('e', 'xt')
1125 call assert_equal('e', g:eaten)
1126
1127 " 0 is ignored by the filter
1128 normal $
1129 call assert_equal(9, getcurpos()[2])
1130 call feedkeys('0', 'xt')
1131 call assert_equal('0', g:ignored)
1132 call assert_equal(1, getcurpos()[2])
1133
1134 " x closes the popup
1135 call feedkeys('x', 'xt')
1136 call assert_equal('e', g:eaten)
1137 call assert_equal(-1, winbufnr(winid))
1138
1139 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001140 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001141endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001142
Bram Moolenaara42d9452019-06-15 21:46:30 +02001143func ShowDialog(key, result)
1144 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001145 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001146 \ filter: 'popup_filter_yesno',
1147 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001148 \ })
1149 redraw
1150 call feedkeys(a:key, "xt")
1151 call assert_equal(winid, s:cb_winid)
1152 call assert_equal(a:result, s:cb_res)
1153endfunc
1154
1155func Test_popup_dialog()
1156 func QuitCallback(id, res)
1157 let s:cb_winid = a:id
1158 let s:cb_res = a:res
1159 endfunc
1160
1161 let winid = ShowDialog("y", 1)
1162 let winid = ShowDialog("Y", 1)
1163 let winid = ShowDialog("n", 0)
1164 let winid = ShowDialog("N", 0)
1165 let winid = ShowDialog("x", 0)
1166 let winid = ShowDialog("X", 0)
1167 let winid = ShowDialog("\<Esc>", 0)
1168 let winid = ShowDialog("\<C-C>", -1)
1169
1170 delfunc QuitCallback
1171endfunc
1172
Bram Moolenaara730e552019-06-16 19:05:31 +02001173func ShowMenu(key, result)
1174 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001175 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001176 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001177 \ })
1178 redraw
1179 call feedkeys(a:key, "xt")
1180 call assert_equal(winid, s:cb_winid)
1181 call assert_equal(a:result, s:cb_res)
1182endfunc
1183
1184func Test_popup_menu()
1185 func QuitCallback(id, res)
1186 let s:cb_winid = a:id
1187 let s:cb_res = a:res
1188 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001189 " mapping won't be used in popup
1190 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001191
1192 let winid = ShowMenu(" ", 1)
1193 let winid = ShowMenu("j \<CR>", 2)
1194 let winid = ShowMenu("JjK \<CR>", 2)
1195 let winid = ShowMenu("jjjjjj ", 3)
1196 let winid = ShowMenu("kkk ", 1)
1197 let winid = ShowMenu("x", -1)
1198 let winid = ShowMenu("X", -1)
1199 let winid = ShowMenu("\<Esc>", -1)
1200 let winid = ShowMenu("\<C-C>", -1)
1201
1202 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001203 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001204endfunc
1205
1206func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001207 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001208
1209 let lines =<< trim END
1210 call setline(1, range(1, 20))
1211 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001212 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001213 func MenuDone(id, res)
1214 echomsg "selected " .. a:res
1215 endfunc
1216 END
1217 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001218 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001219 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1220
1221 call term_sendkeys(buf, "jj")
1222 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1223
1224 call term_sendkeys(buf, " ")
1225 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1226
1227 " clean up
1228 call StopVimInTerminal(buf)
1229 call delete('XtestPopupMenu')
1230endfunc
1231
Bram Moolenaarf914a332019-07-20 15:09:56 +02001232func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001233 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001234
1235 let lines =<< trim END
1236 call setline(1, range(1, 20))
1237 hi PopupSelected ctermbg=green
1238 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1239 func MenuDone(id, res)
1240 echomsg "selected " .. a:res
1241 endfunc
1242 END
1243 call writefile(lines, 'XtestPopupNarrowMenu')
1244 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1245 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1246
1247 " clean up
1248 call term_sendkeys(buf, "x")
1249 call StopVimInTerminal(buf)
1250 call delete('XtestPopupNarrowMenu')
1251endfunc
1252
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001253func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001254 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001255
1256 " Create a popup without title or border, a line of padding will be added to
1257 " put the title on.
1258 let lines =<< trim END
1259 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001260 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001261 END
1262 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001263 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001264 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1265
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001266 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1267 call term_sendkeys(buf, ":\<CR>")
1268 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1269
1270 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1271 call term_sendkeys(buf, ":\<CR>")
1272 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1273
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001274 " clean up
1275 call StopVimInTerminal(buf)
1276 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001277
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001278 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001279 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001280 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001281 call assert_equal('Another Title', popup_getoptions(winid).title)
1282
1283 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001284endfunc
1285
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001286func Test_popup_close_callback()
1287 func PopupDone(id, result)
1288 let g:result = a:result
1289 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001290 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001291 redraw
1292 call popup_close(winid, 'done')
1293 call assert_equal('done', g:result)
1294endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001295
1296func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001297 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001298 redraw
1299 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001300 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001301 call assert_equal(5, pos.height)
1302
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001303 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001304 redraw
1305 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001306 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001307 call assert_equal(3, pos.height)
1308endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001309
1310func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001311 CheckScreendump
1312
Bram Moolenaar988c4332019-06-02 14:12:11 +02001313 " +-----------------------------+
1314 " | | |
1315 " | | |
1316 " | | |
1317 " | line1 |
1318 " |------------line2------------|
1319 " | line3 |
1320 " | line4 |
1321 " | |
1322 " | |
1323 " +-----------------------------+
1324 let lines =<< trim END
1325 only
1326 split
1327 vsplit
1328 let info_window1 = getwininfo()[0]
1329 let line = info_window1['height']
1330 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001331 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001332 \ line : line,
1333 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001334 \ })
1335 END
1336 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001337 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001338 call term_sendkeys(buf, "\<C-W>w")
1339 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1340
1341 " clean up
1342 call StopVimInTerminal(buf)
1343 call delete('XtestPopupBehind')
1344endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001345
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001346func s:VerifyPosition(p, msg, line, col, width, height)
1347 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1348 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1349 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1350 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001351endfunc
1352
1353func Test_popup_position_adjust()
1354 " Anything placed past 2 cells from of the right of the screen is moved to the
1355 " left.
1356 "
1357 " When wrapping is disabled, we also shift to the left to display on the
1358 " screen, unless fixed is set.
1359
1360 " Entries for cases which don't vary based on wrapping.
1361 " Format is per tests described below
1362 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001363 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1364 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1365 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1366 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1367 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1368 \
1369 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1370 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1371 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1372 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1373 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1374 \
1375 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1376 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1377 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1378 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1379 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1380 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001381
1382 " these test groups are dicts with:
1383 " - comment: something to identify the group of tests by
1384 " - options: dict of options to merge with the row/col in tests
1385 " - tests: list of cases. Each one is a list with elements:
1386 " - text
1387 " - row
1388 " - col
1389 " - expected row
1390 " - expected col
1391 " - expected width
1392 " - expected height
1393 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001394 \ #{
1395 \ comment: 'left-aligned with wrapping',
1396 \ options: #{
1397 \ wrap: 1,
1398 \ pos: 'botleft',
1399 \ },
1400 \ tests: both_wrap_tests + [
1401 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1402 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1403 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1404 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1405 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1406 \ ],
1407 \ },
1408 \ #{
1409 \ comment: 'left aligned without wrapping',
1410 \ options: #{
1411 \ wrap: 0,
1412 \ pos: 'botleft',
1413 \ },
1414 \ tests: both_wrap_tests + [
1415 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1416 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1417 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1418 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1419 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1420 \ ],
1421 \ },
1422 \ #{
1423 \ comment: 'left aligned with fixed position',
1424 \ options: #{
1425 \ wrap: 0,
1426 \ fixed: 1,
1427 \ pos: 'botleft',
1428 \ },
1429 \ tests: both_wrap_tests + [
1430 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1431 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1432 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1433 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1434 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1435 \ ],
1436 \ },
1437 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001438
1439 for test_group in tests
1440 for test in test_group.tests
1441 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001442 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001443 \ line: line,
1444 \ col: col,
1445 \ }
1446 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001447
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001448 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001449
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001450 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001451 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1452 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001453 endfor
1454 endfor
1455
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001456 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001457 %bwipe!
1458endfunc
1459
Bram Moolenaar3397f742019-06-02 18:40:06 +02001460func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001461 " width of screen
1462 let X = join(map(range(&columns), {->'X'}), '')
1463
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001464 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1465 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001466
1467 redraw
1468 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1469 call assert_equal(X, line)
1470
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001471 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001472 redraw
1473
1474 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001475 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1476 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001477
1478 redraw
1479 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1480 call assert_equal(X, line)
1481
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001482 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001483 redraw
1484
1485 " Extend so > window width
1486 let X .= 'x'
1487
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001488 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1489 call s:VerifyPosition(p, 'full width + 1 topleft', 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[ : -2 ], 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 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001499 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1500 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001501
1502 redraw
1503 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1504 call assert_equal(X[ : -2 ], line)
1505
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001506 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001507 redraw
1508
1509 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001510 let p = popup_create(X,
1511 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1512 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001513
1514 redraw
1515 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1516 let e_line = ' ' . X[ 1 : -2 ]
1517 call assert_equal(e_line, line)
1518
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001519 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001520 redraw
1521
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001522 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001523 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001524endfunc
1525
1526func Test_popup_moved()
1527 new
1528 call test_override('char_avail', 1)
1529 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1530
1531 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001532 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001533 redraw
1534 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001535 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001536 " trigger the check for last_cursormoved by going into insert mode
1537 call feedkeys("li\<Esc>", 'xt')
1538 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001539 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001540
1541 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001542 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001543 redraw
1544 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001545 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001546 call feedkeys("hi\<Esc>", 'xt')
1547 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001548 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001549
1550 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001551 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001552 redraw
1553 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001554 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001555 call feedkeys("li\<Esc>", 'xt')
1556 call assert_equal(1, popup_getpos(winid).visible)
1557 call feedkeys("ei\<Esc>", 'xt')
1558 call assert_equal(1, popup_getpos(winid).visible)
1559 call feedkeys("eli\<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
Bram Moolenaar17627312019-06-02 19:53:44 +02001563 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001564 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001565 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001566 redraw
1567 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001568 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001569 call feedkeys("eli\<Esc>", 'xt')
1570 call assert_equal(1, popup_getpos(winid).visible)
1571 call feedkeys("wi\<Esc>", 'xt')
1572 call assert_equal(1, popup_getpos(winid).visible)
1573 call feedkeys("Eli\<Esc>", 'xt')
1574 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001575 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001576
1577 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001578 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001579 redraw
1580 call assert_equal(1, popup_getpos(winid).visible)
1581 call feedkeys("eli\<Esc>", 'xt')
1582 call feedkeys("ei\<Esc>", 'xt')
1583 call assert_equal(1, popup_getpos(winid).visible)
1584 call feedkeys("eli\<Esc>", 'xt')
1585 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001586 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001587
1588 bwipe!
1589 call test_override('ALL', 0)
1590endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001591
1592func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001593 CheckFeature timers
1594 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001595
1596 call writefile([
1597 \ "call setline(1, range(1, 20))",
1598 \ "hi Notification ctermbg=lightblue",
1599 \ "call popup_notification('first notification', {})",
1600 \], 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001601 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001602 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1603
1604 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001605 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1606 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001607 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1608
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001609 " clean up
1610 call StopVimInTerminal(buf)
1611 call delete('XtestNotifications')
1612endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001613
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001614func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001615 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001616
1617 let lines =<< trim END
1618 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001619 hi ScrollThumb ctermbg=blue
1620 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001621 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001622 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001623 \ minwidth: 8,
1624 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001625 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001626 func ScrollUp()
1627 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1628 endfunc
1629 func ScrollDown()
1630 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1631 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001632 func ClickTop()
1633 call feedkeys("\<F4>\<LeftMouse>", "xt")
1634 endfunc
1635 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001636 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001637 call feedkeys("\<F5>\<LeftMouse>", "xt")
1638 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001639 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001640 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1641 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001642 END
1643 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001644 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001645 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1646
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001647 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001648 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001649 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1650
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001651 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001652 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001653 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1654
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001655 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001656 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001657 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1658
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001659 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb'})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001660 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1661 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1662
1663 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1664 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1665
1666 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001667 " wait a bit, otherwise it fails sometimes (double click recognized?)
1668 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001669 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1670 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1671
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001672 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1673 sleep 100m
1674 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1675 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1676
1677 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1678 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1679
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001680 " clean up
1681 call StopVimInTerminal(buf)
1682 call delete('XtestPopupScroll')
1683endfunc
1684
Bram Moolenaar437a7462019-07-05 20:17:22 +02001685func Test_popup_fitting_scrollbar()
1686 " this was causing a crash, divide by zero
1687 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001688 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001689 \ scrollbar: 1,
1690 \ maxwidth: 10,
1691 \ maxheight: 5,
1692 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001693 redraw
1694 call popup_clear()
1695endfunc
1696
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001697func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001698 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001699
1700 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001701 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001702 let p = popup_create('test', opts)
1703 call popup_settext(p, 'this is a text')
1704 END
1705
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001706 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001707 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001708 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1709
1710 " Setting to empty string clears it
1711 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1712 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1713
1714 " Setting a list
1715 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1716 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1717
1718 " Shrinking with a list
1719 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1720 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1721
1722 " Growing with a list
1723 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1724 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1725
1726 " Empty list clears
1727 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1728 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1729
1730 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001731 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001732 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1733
1734 " clean up
1735 call StopVimInTerminal(buf)
1736 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001737endfunc
1738
1739func Test_popup_hidden()
1740 new
1741
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001742 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001743 redraw
1744 call assert_equal(0, popup_getpos(winid).visible)
1745 call popup_close(winid)
1746
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001747 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001748 redraw
1749 call assert_equal(0, popup_getpos(winid).visible)
1750 call popup_close(winid)
1751
1752 func QuitCallback(id, res)
1753 let s:cb_winid = a:id
1754 let s:cb_res = a:res
1755 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001756 let winid = popup_dialog('make a choice', #{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001757 \ filter: 'popup_filter_yesno',
1758 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001759 \ })
1760 redraw
1761 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001762 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1763 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001764 exe "normal anot used by filter\<Esc>"
1765 call assert_equal('not used by filter', getline(1))
1766
1767 call popup_show(winid)
1768 call feedkeys('y', "xt")
1769 call assert_equal(1, s:cb_res)
1770
1771 bwipe!
1772 delfunc QuitCallback
1773endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001774
1775" Test options not checked elsewhere
1776func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001777 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001778 let options = popup_getoptions(winid)
1779 call assert_equal(1, options.wrap)
1780 call assert_equal(0, options.drag)
1781 call assert_equal('Beautiful', options.highlight)
1782
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001783 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001784 let options = popup_getoptions(winid)
1785 call assert_equal(0, options.wrap)
1786 call assert_equal(1, options.drag)
1787 call assert_equal('Another', options.highlight)
1788
1789 call popup_close(winid)
1790endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001791
1792func Test_popupwin_garbage_collect()
1793 func MyPopupFilter(x, winid, c)
1794 " NOP
1795 endfunc
1796
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001797 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001798 call test_garbagecollect_now()
1799 redraw
1800 " Must not crach caused by invalid memory access
1801 call feedkeys('j', 'xt')
1802 call assert_true(v:true)
1803
1804 call popup_close(winid)
1805 delfunc MyPopupFilter
1806endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001807
1808func Test_popupwin_with_buffer()
1809 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1810 let buf = bufadd('XsomeFile')
1811 call assert_equal(0, bufloaded(buf))
1812 let winid = popup_create(buf, {})
1813 call assert_notequal(0, winid)
1814 let pos = popup_getpos(winid)
1815 call assert_equal(2, pos.height)
1816 call assert_equal(1, bufloaded(buf))
1817 call popup_close(winid)
1818 call assert_equal({}, popup_getpos(winid))
1819 call assert_equal(1, bufloaded(buf))
1820 exe 'bwipe! ' .. buf
Bram Moolenaar7866b872019-07-01 22:21:01 +02001821
1822 edit test_popupwin.vim
1823 let winid = popup_create(bufnr(''), {})
1824 redraw
1825 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001826 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001827endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02001828
1829func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001830 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001831 \ maxwidth: 40,
1832 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02001833 \ })
1834 for top in range(1, 20)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001835 call popup_setoptions(winid, #{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02001836 redraw
1837 call assert_equal(19, popup_getpos(winid).width)
1838 endfor
1839 call popup_clear()
1840endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02001841
1842func Test_popupwin_buf_close()
1843 let buf = bufadd('Xtestbuf')
1844 call bufload(buf)
1845 call setbufline(buf, 1, ['just', 'some', 'lines'])
1846 let winid = popup_create(buf, {})
1847 redraw
1848 call assert_equal(3, popup_getpos(winid).height)
1849 let bufinfo = getbufinfo(buf)[0]
1850 call assert_equal(1, bufinfo.changed)
1851 call assert_equal(0, bufinfo.hidden)
1852 call assert_equal(0, bufinfo.listed)
1853 call assert_equal(1, bufinfo.loaded)
1854 call assert_equal([], bufinfo.windows)
1855 call assert_equal([winid], bufinfo.popups)
1856
1857 call popup_close(winid)
1858 call assert_equal({}, popup_getpos(winid))
1859 let bufinfo = getbufinfo(buf)[0]
1860 call assert_equal(1, bufinfo.changed)
1861 call assert_equal(1, bufinfo.hidden)
1862 call assert_equal(0, bufinfo.listed)
1863 call assert_equal(1, bufinfo.loaded)
1864 call assert_equal([], bufinfo.windows)
1865 call assert_equal([], bufinfo.popups)
1866 exe 'bwipe! ' .. buf
1867endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02001868
1869func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001870 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02001871
1872 let lines =<< trim END
1873 call setline(1, range(1, 10))
1874 hi ScrollThumb ctermbg=blue
1875 hi ScrollBar ctermbg=red
1876 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001877 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001878 \ maxwidth: 10,
1879 \ maxheight: 3,
1880 \ pos : 'topleft',
1881 \ col : a:col,
1882 \ line : a:line,
1883 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02001884 \ })
1885 endfunc
1886 call PopupMenu(['x'], 1, 1)
1887 call PopupMenu(['123456789|'], 1, 16)
1888 call PopupMenu(['123456789|' .. ' '], 7, 1)
1889 call PopupMenu([repeat('123456789|', 100)], 7, 16)
1890 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
1891 END
1892 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001893 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02001894 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
1895
1896 " close the menu popupwin.
1897 call term_sendkeys(buf, " ")
1898 call term_sendkeys(buf, " ")
1899 call term_sendkeys(buf, " ")
1900 call term_sendkeys(buf, " ")
1901 call term_sendkeys(buf, " ")
1902
1903 " clean up
1904 call StopVimInTerminal(buf)
1905 call delete('XtestPopupMenuMaxWidth')
1906endfunc
1907
Bram Moolenaara901a372019-07-13 16:38:50 +02001908func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001909 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02001910
1911 let lines =<< trim END
1912 call setline(1, range(1, 20))
1913 hi ScrollThumb ctermbg=blue
1914 hi ScrollBar ctermbg=red
1915 call popup_menu(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001916 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001917 \ minwidth: 8,
1918 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02001919 \ })
1920 END
1921 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001922 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02001923
1924 call term_sendkeys(buf, "j")
1925 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
1926
1927 call term_sendkeys(buf, "jjj")
1928 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
1929
1930 " if the cursor is the bottom line, it stays at the bottom line.
1931 call term_sendkeys(buf, repeat("j", 20))
1932 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
1933
1934 call term_sendkeys(buf, "kk")
1935 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
1936
1937 call term_sendkeys(buf, "k")
1938 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
1939
1940 " if the cursor is in the top line, it stays in the top line.
1941 call term_sendkeys(buf, repeat("k", 20))
1942 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
1943
1944 " close the menu popupwin.
1945 call term_sendkeys(buf, " ")
1946
1947 " clean up
1948 call StopVimInTerminal(buf)
1949 call delete('XtestPopupMenuScroll')
1950endfunc
1951
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001952func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001953 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001954
1955 let lines =<< trim END
1956 function! MyFilter(winid, key) abort
1957 if a:key == "0"
1958 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
1959 return 1
1960 endif
1961 if a:key == "G"
1962 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
1963 return 1
1964 endif
1965 if a:key == "j"
1966 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
1967 return 1
1968 endif
1969 if a:key == "k"
1970 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
1971 return 1
1972 endif
1973 if a:key == 'x'
1974 call popup_close(a:winid)
1975 return 1
1976 endif
1977 return 0
1978 endfunction
1979 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
1980 \ maxheight : 3,
1981 \ filter : 'MyFilter'
1982 \ })
1983 END
1984 call writefile(lines, 'XtestPopupMenuFilter')
1985 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
1986
1987 call term_sendkeys(buf, "j")
1988 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
1989
1990 call term_sendkeys(buf, "k")
1991 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
1992
1993 call term_sendkeys(buf, "G")
1994 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
1995
1996 call term_sendkeys(buf, "0")
1997 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
1998
1999 call term_sendkeys(buf, "x")
2000
2001 " clean up
2002 call StopVimInTerminal(buf)
2003 call delete('XtestPopupMenuFilter')
2004endfunc
2005
2006func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002007 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002008
2009 let winid = popup_create('some text', {})
2010 call assert_equal(0, popup_getoptions(winid).cursorline)
2011 call popup_close(winid)
2012
2013 let winid = popup_create('some text', #{ cursorline: 1, })
2014 call assert_equal(1, popup_getoptions(winid).cursorline)
2015 call popup_close(winid)
2016
2017 let winid = popup_create('some text', #{ cursorline: 0, })
2018 call assert_equal(0, popup_getoptions(winid).cursorline)
2019 call popup_close(winid)
2020
2021 let winid = popup_menu('some text', {})
2022 call assert_equal(1, popup_getoptions(winid).cursorline)
2023 call popup_close(winid)
2024
2025 let winid = popup_menu('some text', #{ cursorline: 1, })
2026 call assert_equal(1, popup_getoptions(winid).cursorline)
2027 call popup_close(winid)
2028
2029 let winid = popup_menu('some text', #{ cursorline: 0, })
2030 call assert_equal(0, popup_getoptions(winid).cursorline)
2031 call popup_close(winid)
2032
2033 " ---------
2034 " Pattern 1
2035 " ---------
2036 let lines =<< trim END
2037 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2038 END
2039 call writefile(lines, 'XtestPopupCursorLine')
2040 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2041 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2042 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2043 call StopVimInTerminal(buf)
2044
2045 " ---------
2046 " Pattern 2
2047 " ---------
2048 let lines =<< trim END
2049 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2050 END
2051 call writefile(lines, 'XtestPopupCursorLine')
2052 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2053 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2054 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2055 call StopVimInTerminal(buf)
2056
2057 " ---------
2058 " Pattern 3
2059 " ---------
2060 let lines =<< trim END
2061 function! MyFilter(winid, key) abort
2062 if a:key == "j"
2063 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2064 return 1
2065 endif
2066 if a:key == 'x'
2067 call popup_close(a:winid)
2068 return 1
2069 endif
2070 return 0
2071 endfunction
2072 call popup_menu(['111', '222', '333'], #{
2073 \ cursorline : 0,
2074 \ maxheight : 2,
2075 \ filter : 'MyFilter',
2076 \ })
2077 END
2078 call writefile(lines, 'XtestPopupCursorLine')
2079 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2080 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2081 call term_sendkeys(buf, "j")
2082 call term_sendkeys(buf, "j")
2083 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2084 call term_sendkeys(buf, "x")
2085 call StopVimInTerminal(buf)
2086
2087 " ---------
2088 " Pattern 4
2089 " ---------
2090 let lines =<< trim END
2091 function! MyFilter(winid, key) abort
2092 if a:key == "j"
2093 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2094 return 1
2095 endif
2096 if a:key == 'x'
2097 call popup_close(a:winid)
2098 return 1
2099 endif
2100 return 0
2101 endfunction
2102 call popup_menu(['111', '222', '333'], #{
2103 \ cursorline : 1,
2104 \ maxheight : 2,
2105 \ filter : 'MyFilter',
2106 \ })
2107 END
2108 call writefile(lines, 'XtestPopupCursorLine')
2109 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2110 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2111 call term_sendkeys(buf, "j")
2112 call term_sendkeys(buf, "j")
2113 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2114 call term_sendkeys(buf, "x")
2115 call StopVimInTerminal(buf)
2116
2117 call delete('XtestPopupCursorLine')
2118endfunc
2119
Bram Moolenaarf914a332019-07-20 15:09:56 +02002120func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002121 CheckScreendump
2122
Bram Moolenaarf914a332019-07-20 15:09:56 +02002123 call writefile([
2124 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2125 \ "another\tXtagfile\t/^this is another",
2126 \ "theword\tXtagfile\t/^theword"],
2127 \ 'Xtags')
2128 call writefile(range(1,20)
2129 \ + ['theword is here']
2130 \ + range(22, 27)
2131 \ + ['this is another place']
2132 \ + range(29, 40),
2133 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002134 call writefile(range(1,10)
2135 \ + ['searched word is here']
2136 \ + range(12, 20),
2137 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002138 let lines =<< trim END
2139 set tags=Xtags
2140 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002141 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002142 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002143 \ 'three',
2144 \ 'four',
2145 \ 'five',
2146 \ 'six',
2147 \ 'seven',
2148 \ 'find theword somewhere',
2149 \ 'nine',
2150 \ 'this is another word',
2151 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002152 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002153 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002154 END
2155 call writefile(lines, 'XtestPreviewPopup')
2156 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2157
2158 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2159 call term_sendkeys(buf, ":\<CR>")
2160 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2161
2162 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2163 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2164
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002165 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002166 call term_sendkeys(buf, ":\<CR>")
2167 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2168
2169 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2170 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2171
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002172 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2173 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002174 call term_sendkeys(buf, ":cd testdir\<CR>")
2175
2176 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002177 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002178 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2179
2180 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2181 call term_sendkeys(buf, ":\<CR>")
2182 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2183
2184 call term_sendkeys(buf, ":pclose\<CR>")
2185 call term_sendkeys(buf, ":psearch searched\<CR>")
2186 call term_sendkeys(buf, ":\<CR>")
2187 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002188
Bram Moolenaarf914a332019-07-20 15:09:56 +02002189 call StopVimInTerminal(buf)
2190 call delete('Xtags')
2191 call delete('Xtagfile')
2192 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002193 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002194endfunc
2195
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002196func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002197 let lines =<< trim END
2198 set completeopt+=preview,popup
2199 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002200 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002201
2202 func CompleteFuncDict(findstart, base)
2203 if a:findstart
2204 if col('.') > 10
2205 return col('.') - 10
2206 endif
2207 return 0
2208 endif
2209
2210 return {
2211 \ 'words': [
2212 \ {
2213 \ 'word': 'aword',
2214 \ 'abbr': 'wrd',
2215 \ 'menu': 'extra text',
2216 \ 'info': 'words are cool',
2217 \ 'kind': 'W',
2218 \ 'user_data': 'test'
2219 \ },
2220 \ {
2221 \ 'word': 'anotherword',
2222 \ 'abbr': 'anotwrd',
2223 \ 'menu': 'extra text',
2224 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2225 \ 'kind': 'W',
2226 \ 'user_data': 'notest'
2227 \ },
2228 \ {
2229 \ 'word': 'noinfo',
2230 \ 'abbr': 'noawrd',
2231 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002232 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002233 \ 'kind': 'W',
2234 \ 'user_data': 'notest'
2235 \ },
2236 \ {
2237 \ 'word': 'thatword',
2238 \ 'abbr': 'thatwrd',
2239 \ 'menu': 'extra text',
2240 \ 'info': 'that word is cool',
2241 \ 'kind': 'W',
2242 \ 'user_data': 'notest'
2243 \ },
2244 \ ]
2245 \ }
2246 endfunc
2247 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002248 func ChangeColor()
2249 let id = popup_findinfo()
2250 call popup_setoptions(id, #{highlight: 'InfoPopup'})
2251 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002252 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002253 return lines
2254endfunc
2255
2256func Test_popupmenu_info_border()
2257 CheckScreendump
2258
2259 let lines = Get_popupmenu_lines()
2260 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002261 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002262
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002263 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2264 call term_wait(buf, 50)
2265
2266 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2267 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2268
2269 call term_sendkeys(buf, "\<C-N>")
2270 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2271
2272 call term_sendkeys(buf, "\<C-N>")
2273 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2274
2275 call term_sendkeys(buf, "\<C-N>\<C-N>")
2276 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2277
2278 call StopVimInTerminal(buf)
2279 call delete('XtestInfoPopup')
2280endfunc
2281
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002282func Test_popupmenu_info_noborder()
2283 CheckScreendump
2284
2285 let lines = Get_popupmenu_lines()
2286 call add(lines, 'set completepopup=height:4,border:off')
2287 call writefile(lines, 'XtestInfoPopupNb')
2288
2289 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2290 call term_wait(buf, 50)
2291
2292 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2293 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2294
2295 call StopVimInTerminal(buf)
2296 call delete('XtestInfoPopupNb')
2297endfunc
2298
Bram Moolenaar258cef52019-08-21 17:29:29 +02002299func Test_popupmenu_info_align_menu()
2300 CheckScreendump
2301
2302 let lines = Get_popupmenu_lines()
2303 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2304 call writefile(lines, 'XtestInfoPopupNb')
2305
2306 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2307 call term_wait(buf, 50)
2308
2309 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2310 call term_sendkeys(buf, "\<C-N>")
2311 call term_sendkeys(buf, "\<C-N>")
2312 call term_sendkeys(buf, "\<C-N>")
2313 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2314
2315 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2316 call term_sendkeys(buf, "\<C-N>")
2317 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2318
2319 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002320 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002321 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2322 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2323 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2324
2325 call StopVimInTerminal(buf)
2326 call delete('XtestInfoPopupNb')
2327endfunc
2328
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002329" vim: shiftwidth=2 sts=2