blob: 94718578746b62f9b0f6cb1bf14dd5c27d120b37 [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 Moolenaar356375f2019-08-24 14:46:29 +0200346 split
347 vsplit
348 $wincmd w
349 vsplit
350 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200351 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200352 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200353 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200354 \ border: [],
355 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200356 \ })
357 func Dragit()
358 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
359 endfunc
360 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200361 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200362 func Resize()
363 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
364 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200365 map <silent> <F5> :call test_setmouse(6, 21)<CR>
366 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200367 END
368 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200369 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200370 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
371
372 call term_sendkeys(buf, ":call Dragit()\<CR>")
373 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
374
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200375 call term_sendkeys(buf, ":call Resize()\<CR>")
376 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
377
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200378 " clean up
379 call StopVimInTerminal(buf)
380 call delete('XtestPopupDrag')
381endfunc
382
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200383func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200384 CheckScreendump
385
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200386 let lines =<< trim END
387 call setline(1, range(1, 20))
388 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200389 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200390 \ close: 'button',
391 \ border: [],
392 \ line: 1,
393 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200394 \ })
395 func CloseMsg(id, result)
396 echomsg 'Popup closed with ' .. a:result
397 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200398 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200399 \ close: 'click',
400 \ line: 3,
401 \ col: 15,
402 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200403 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200404 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200405 \ close: 'button',
406 \ line: 5,
407 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200408 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200409 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200410 \ close: 'button',
411 \ padding: [],
412 \ line: 5,
413 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200414 \ })
415 func CloseWithX()
416 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
417 endfunc
418 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
419 func CloseWithClick()
420 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
421 endfunc
422 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200423 func CreateWithMenuFilter()
424 let winid = popup_create('barfoo', #{
425 \ close: 'button',
426 \ filter: 'popup_filter_menu',
427 \ border: [],
428 \ line: 1,
429 \ col: 40,
430 \ })
431 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200432 END
433 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200434 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200435 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
436
437 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
438 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
439
440 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
441 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
442
Bram Moolenaarf6396232019-08-24 19:36:00 +0200443 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
444 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
445
446 " We have to send the actual mouse code, feedkeys() would be caught the
447 " filter.
448 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
449 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
450
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200451 " clean up
452 call StopVimInTerminal(buf)
453 call delete('XtestPopupClose')
454endfunction
455
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200456func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200457 CheckScreendump
458
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200459 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200460 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200461 hi PopupColor ctermbg=lightgrey
462 let winid = popup_create([
463 \ 'some text',
464 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200465 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200466 \ line: 1,
467 \ col: 10,
468 \ wrap: 0,
469 \ fixed: 1,
470 \ zindex: 90,
471 \ padding: [],
472 \ highlight: 'PopupColor',
473 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200474 call popup_create([
475 \ 'xxxxxxxxx',
476 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200477 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200478 \ line: 3,
479 \ col: 18,
480 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200481 let winidb = popup_create([
482 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200483 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200484 \ line: 7,
485 \ col: 10,
486 \ wrap: 0,
487 \ fixed: 1,
488 \ close: 'button',
489 \ zindex: 90,
490 \ padding: [],
491 \ border: [],
492 \ 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 +0200493 END
494 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200495 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200496 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
497
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200498 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
499 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200500 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200501 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
502
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200503 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
504 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200505 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200506 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
507
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200508 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
509 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200510 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200511 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
512
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200513 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
514 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200515 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200516 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
517
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200518 " clean up
519 call StopVimInTerminal(buf)
520 call delete('XtestPopupMask')
521endfunc
522
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200523func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200524 CheckScreendump
525 CheckFeature clipboard_working
526
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200527 " create a popup with some text to be selected
528 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200529 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200530 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200531 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200532 \ drag: 1,
533 \ border: [],
534 \ line: 3,
535 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200536 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200537 \ })
538 func Select1()
539 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
540 endfunc
541 map <silent> <F3> :call test_setmouse(4, 15)<CR>
542 map <silent> <F4> :call test_setmouse(6, 23)<CR>
543 END
544 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200545 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200546 call term_sendkeys(buf, ":call Select1()\<CR>")
547 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
548
549 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
550 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200551 " clean the command line, sometimes it still shows a command
552 call term_sendkeys(buf, ":\<esc>")
553
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200554 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
555
556 " clean up
557 call StopVimInTerminal(buf)
558 call delete('XtestPopupSelect')
559endfunc
560
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200561func Test_popup_in_tab()
562 " default popup is local to tab, not visible when in other tab
563 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200564 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200565 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200566 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200567 tabnew
568 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200569 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200570 quit
571 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200572
573 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200574 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200575 " buffer is gone now
576 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200577
578 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200579 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200580 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200581 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200582 tabnew
583 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200584 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200585 quit
586 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200587 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200588
589 " create popup in other tab
590 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200591 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200592 call assert_equal(0, popup_getpos(winid).visible)
593 call assert_equal(1, popup_getoptions(winid).tabpage)
594 quit
595 call assert_equal(1, popup_getpos(winid).visible)
596 call assert_equal(0, popup_getoptions(winid).tabpage)
597 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200598endfunc
599
600func Test_popup_valid_arguments()
601 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200602 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200603 let pos = popup_getpos(winid)
604 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200605 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200606
607 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200608 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200609 let pos = popup_getpos(winid)
610 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200611 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200612
613 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200614 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200615 let pos = popup_getpos(winid)
616 let around = (&columns - pos.width) / 2
617 call assert_inrange(around - 1, around + 1, pos.col)
618 let around = (&lines - pos.height) / 2
619 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200620 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200621endfunc
622
623func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200624 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200625 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200626 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200627 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200628
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200629 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200630 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200631 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200632 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200633 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200634 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200635 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200636 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200637
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200638 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200639 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200640 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
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", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200643 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200644 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200645 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200646
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200647 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200648 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200649 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200650 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200651 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200652 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200653 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200654 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200655 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
656 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200657 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200658 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200659
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200660 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200661 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200662 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200663 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200664 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200665 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200666 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
667 call popup_clear()
668 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200669 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200670 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200671endfunc
672
Bram Moolenaareea16992019-05-31 17:34:48 +0200673func Test_win_execute_closing_curwin()
674 split
675 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200676 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200677 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200678endfunc
679
680func Test_win_execute_not_allowed()
681 let winid = popup_create('some text', {})
682 call assert_fails('call win_execute(winid, "split")', 'E994:')
683 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
684 call assert_fails('call win_execute(winid, "close")', 'E994:')
685 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200686 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200687 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
688 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
689 call assert_fails('call win_execute(winid, "next")', 'E994:')
690 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
691 call assert_fails('call win_execute(winid, "buf")', 'E994:')
692 call assert_fails('call win_execute(winid, "edit")', 'E994:')
693 call assert_fails('call win_execute(winid, "enew")', 'E994:')
694 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
695 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
696 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
697 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200698 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200699endfunc
700
Bram Moolenaar402502d2019-05-30 22:07:36 +0200701func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200702 CheckScreendump
703
Bram Moolenaar402502d2019-05-30 22:07:36 +0200704 let lines =<< trim END
705 call setline(1, range(1, 100))
706 let winid = popup_create(
707 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200708 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200709 END
710 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200711 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200712 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
713
714 " clean up
715 call StopVimInTerminal(buf)
716 call delete('XtestPopup')
717endfunc
718
719func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200720 CheckScreendump
721
Bram Moolenaar402502d2019-05-30 22:07:36 +0200722 let lines =<< trim END
723 call setline(1, range(1, 100))
724 let winid = popup_create(
725 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200726 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200727 END
728 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200729 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200730 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
731
732 " clean up
733 call StopVimInTerminal(buf)
734 call delete('XtestPopup')
735endfunc
736
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200737func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200738 CheckScreendump
739
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200740 let lines =<< trim END
741 set showbreak=>>\
742 call setline(1, range(1, 20))
743 let winid = popup_dialog(
744 \ 'a long line here',
745 \ #{filter: 'popup_filter_yesno'})
746 END
747 call writefile(lines, 'XtestPopupShowbreak')
748 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
749 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
750
751 " clean up
752 call term_sendkeys(buf, "y")
753 call StopVimInTerminal(buf)
754 call delete('XtestPopupShowbreak')
755endfunc
756
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200757func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200758 CheckFeature timers
759
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200760 topleft vnew
761 call setline(1, 'hello')
762
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200763 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200764 \ line: 1,
765 \ col: 1,
766 \ minwidth: 20,
767 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200768 \})
769 redraw
770 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
771 call assert_equal('world', line)
772
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200773 call assert_equal(winid, popup_locate(1, 1))
774 call assert_equal(winid, popup_locate(1, 20))
775 call assert_equal(0, popup_locate(1, 21))
776 call assert_equal(0, popup_locate(2, 1))
777
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200778 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200779 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200780 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
781 call assert_equal('hello', line)
782
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200783 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200784 \ line: &lines,
785 \ col: 10,
786 \ minwidth: 20,
787 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200788 \})
789 redraw
790 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
791 call assert_match('.*on the command line.*', line)
792
793 sleep 700m
794 redraw
795 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
796 call assert_notmatch('.*on the command line.*', line)
797
798 bwipe!
799endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200800
801func Test_popup_hide()
802 topleft vnew
803 call setline(1, 'hello')
804
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200805 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200806 \ line: 1,
807 \ col: 1,
808 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200809 \})
810 redraw
811 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
812 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200813 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200814 " buffer is still listed and active
815 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200816
817 call popup_hide(winid)
818 redraw
819 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
820 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200821 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200822 " buffer is still listed but hidden
823 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200824
825 call popup_show(winid)
826 redraw
827 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
828 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200829 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200830
831
832 call popup_close(winid)
833 redraw
834 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
835 call assert_equal('hello', line)
836
837 " error is given for existing non-popup window
838 call assert_fails('call popup_hide(win_getid())', 'E993:')
839
840 " no error non-existing window
841 call popup_hide(1234234)
842 call popup_show(41234234)
843
844 bwipe!
845endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200846
847func Test_popup_move()
848 topleft vnew
849 call setline(1, 'hello')
850
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200851 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200852 \ line: 1,
853 \ col: 1,
854 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200855 \})
856 redraw
857 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
858 call assert_equal('world ', line)
859
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200860 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200861 redraw
862 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
863 call assert_equal('hello ', line)
864 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
865 call assert_equal('~world', line)
866
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200867 call popup_move(winid, #{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200868 redraw
869 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
870 call assert_equal('hworld', line)
871
872 call popup_close(winid)
873
874 bwipe!
875endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200876
Bram Moolenaar402502d2019-05-30 22:07:36 +0200877func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200878 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200879 \ line: 2,
880 \ col: 3,
881 \ minwidth: 10,
882 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200883 \})
884 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200885 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200886 call assert_equal(2, res.line)
887 call assert_equal(3, res.col)
888 call assert_equal(10, res.width)
889 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200890 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200891
892 call popup_close(winid)
893endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200894
895func Test_popup_width_longest()
896 let tests = [
897 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
898 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
899 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
900 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
901 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
902 \ ]
903
904 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200905 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200906 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200907 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200908 call assert_equal(test[1], position.width)
909 call popup_close(winid)
910 endfor
911endfunc
912
913func Test_popup_wraps()
914 let tests = [
915 \ ['nowrap', 6, 1],
916 \ ['a line that wraps once', 12, 2],
917 \ ['a line that wraps two times', 12, 3],
918 \ ]
919 for test in tests
920 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200921 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200922 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200923 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200924 call assert_equal(test[1], position.width)
925 call assert_equal(test[2], position.height)
926
927 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200928 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200929 endfor
930endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200931
932func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200933 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200934 \ line: 2,
935 \ col: 3,
936 \ minwidth: 10,
937 \ minheight: 11,
938 \ maxwidth: 20,
939 \ maxheight: 21,
940 \ zindex: 100,
941 \ time: 5000,
942 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200943 \})
944 redraw
945 let res = popup_getoptions(winid)
946 call assert_equal(2, res.line)
947 call assert_equal(3, res.col)
948 call assert_equal(10, res.minwidth)
949 call assert_equal(11, res.minheight)
950 call assert_equal(20, res.maxwidth)
951 call assert_equal(21, res.maxheight)
952 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200953 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +0200954 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200955 if has('timers')
956 call assert_equal(5000, res.time)
957 endif
958 call popup_close(winid)
959
960 let winid = popup_create('hello', {})
961 redraw
962 let res = popup_getoptions(winid)
963 call assert_equal(0, res.line)
964 call assert_equal(0, res.col)
965 call assert_equal(0, res.minwidth)
966 call assert_equal(0, res.minheight)
967 call assert_equal(0, res.maxwidth)
968 call assert_equal(0, res.maxheight)
969 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200970 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200971 if has('timers')
972 call assert_equal(0, res.time)
973 endif
974 call popup_close(winid)
975 call assert_equal({}, popup_getoptions(winid))
976endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200977
978func Test_popup_option_values()
979 new
980 " window-local
981 setlocal number
982 setlocal nowrap
983 " buffer-local
984 setlocal omnifunc=Something
985 " global/buffer-local
986 setlocal path=/there
987 " global/window-local
988 setlocal scrolloff=9
989
990 let winid = popup_create('hello', {})
991 call assert_equal(0, getwinvar(winid, '&number'))
992 call assert_equal(1, getwinvar(winid, '&wrap'))
993 call assert_equal('', getwinvar(winid, '&omnifunc'))
994 call assert_equal(&g:path, getwinvar(winid, '&path'))
995 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
996
997 call popup_close(winid)
998 bwipe
999endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001000
1001func Test_popup_atcursor()
1002 topleft vnew
1003 call setline(1, [
1004 \ 'xxxxxxxxxxxxxxxxx',
1005 \ 'xxxxxxxxxxxxxxxxx',
1006 \ 'xxxxxxxxxxxxxxxxx',
1007 \])
1008
1009 call cursor(2, 2)
1010 redraw
1011 let winid = popup_atcursor('vim', {})
1012 redraw
1013 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1014 call assert_equal('xvimxxxxxxxxxxxxx', line)
1015 call popup_close(winid)
1016
1017 call cursor(3, 4)
1018 redraw
1019 let winid = popup_atcursor('vim', {})
1020 redraw
1021 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1022 call assert_equal('xxxvimxxxxxxxxxxx', line)
1023 call popup_close(winid)
1024
1025 call cursor(1, 1)
1026 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001027 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001028 \ line: 'cursor+2',
1029 \ col: 'cursor+1',
1030 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001031 redraw
1032 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1033 call assert_equal('xvimxxxxxxxxxxxxx', line)
1034 call popup_close(winid)
1035
1036 call cursor(3, 3)
1037 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001038 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001039 \ line: 'cursor-2',
1040 \ col: 'cursor-1',
1041 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001042 redraw
1043 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1044 call assert_equal('xvimxxxxxxxxxxxxx', line)
1045 call popup_close(winid)
1046
Bram Moolenaar402502d2019-05-30 22:07:36 +02001047 " just enough room above
1048 call cursor(3, 3)
1049 redraw
1050 let winid = popup_atcursor(['vim', 'is great'], {})
1051 redraw
1052 let pos = popup_getpos(winid)
1053 call assert_equal(1, pos.line)
1054 call popup_close(winid)
1055
1056 " not enough room above, popup goes below the cursor
1057 call cursor(3, 3)
1058 redraw
1059 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1060 redraw
1061 let pos = popup_getpos(winid)
1062 call assert_equal(4, pos.line)
1063 call popup_close(winid)
1064
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001065 " cursor in first line, popup in line 2
1066 call cursor(1, 1)
1067 redraw
1068 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1069 redraw
1070 let pos = popup_getpos(winid)
1071 call assert_equal(2, pos.line)
1072 call popup_close(winid)
1073
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001074 bwipe!
1075endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001076
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001077func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001078 CheckScreendump
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001079
1080 let lines =<< trim END
1081 call setline(1, range(1, 20))
1082 call setline(5, 'here is some text to hover over')
1083 set balloonevalterm
1084 set balloonexpr=BalloonExpr()
1085 set balloondelay=100
1086 func BalloonExpr()
1087 let s:winid = popup_beval([v:beval_text], {})
1088 return ''
1089 endfunc
1090 func Hover()
1091 call test_setmouse(5, 15)
1092 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1093 sleep 100m
1094 endfunc
1095 func MoveOntoPopup()
1096 call test_setmouse(4, 17)
1097 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1098 endfunc
1099 func MoveAway()
1100 call test_setmouse(5, 13)
1101 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1102 endfunc
1103 END
1104 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001105 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001106 call term_wait(buf, 100)
1107 call term_sendkeys(buf, 'j')
1108 call term_sendkeys(buf, ":call Hover()\<CR>")
1109 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1110
1111 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1112 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1113
1114 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1115 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1116
1117 " clean up
1118 call StopVimInTerminal(buf)
1119 call delete('XtestPopupBeval')
1120endfunc
1121
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001122func Test_popup_filter()
1123 new
1124 call setline(1, 'some text')
1125
1126 func MyPopupFilter(winid, c)
1127 if a:c == 'e'
1128 let g:eaten = 'e'
1129 return 1
1130 endif
1131 if a:c == '0'
1132 let g:ignored = '0'
1133 return 0
1134 endif
1135 if a:c == 'x'
1136 call popup_close(a:winid)
1137 return 1
1138 endif
1139 return 0
1140 endfunc
1141
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001142 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001143 redraw
1144
1145 " e is consumed by the filter
1146 call feedkeys('e', 'xt')
1147 call assert_equal('e', g:eaten)
1148
1149 " 0 is ignored by the filter
1150 normal $
1151 call assert_equal(9, getcurpos()[2])
1152 call feedkeys('0', 'xt')
1153 call assert_equal('0', g:ignored)
1154 call assert_equal(1, getcurpos()[2])
1155
1156 " x closes the popup
1157 call feedkeys('x', 'xt')
1158 call assert_equal('e', g:eaten)
1159 call assert_equal(-1, winbufnr(winid))
1160
1161 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001162 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001163endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001164
Bram Moolenaara42d9452019-06-15 21:46:30 +02001165func ShowDialog(key, result)
1166 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001167 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001168 \ filter: 'popup_filter_yesno',
1169 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001170 \ })
1171 redraw
1172 call feedkeys(a:key, "xt")
1173 call assert_equal(winid, s:cb_winid)
1174 call assert_equal(a:result, s:cb_res)
1175endfunc
1176
1177func Test_popup_dialog()
1178 func QuitCallback(id, res)
1179 let s:cb_winid = a:id
1180 let s:cb_res = a:res
1181 endfunc
1182
1183 let winid = ShowDialog("y", 1)
1184 let winid = ShowDialog("Y", 1)
1185 let winid = ShowDialog("n", 0)
1186 let winid = ShowDialog("N", 0)
1187 let winid = ShowDialog("x", 0)
1188 let winid = ShowDialog("X", 0)
1189 let winid = ShowDialog("\<Esc>", 0)
1190 let winid = ShowDialog("\<C-C>", -1)
1191
1192 delfunc QuitCallback
1193endfunc
1194
Bram Moolenaara730e552019-06-16 19:05:31 +02001195func ShowMenu(key, result)
1196 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001197 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001198 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001199 \ })
1200 redraw
1201 call feedkeys(a:key, "xt")
1202 call assert_equal(winid, s:cb_winid)
1203 call assert_equal(a:result, s:cb_res)
1204endfunc
1205
1206func Test_popup_menu()
1207 func QuitCallback(id, res)
1208 let s:cb_winid = a:id
1209 let s:cb_res = a:res
1210 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001211 " mapping won't be used in popup
1212 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001213
1214 let winid = ShowMenu(" ", 1)
1215 let winid = ShowMenu("j \<CR>", 2)
1216 let winid = ShowMenu("JjK \<CR>", 2)
1217 let winid = ShowMenu("jjjjjj ", 3)
1218 let winid = ShowMenu("kkk ", 1)
1219 let winid = ShowMenu("x", -1)
1220 let winid = ShowMenu("X", -1)
1221 let winid = ShowMenu("\<Esc>", -1)
1222 let winid = ShowMenu("\<C-C>", -1)
1223
1224 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001225 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001226endfunc
1227
1228func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001229 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001230
1231 let lines =<< trim END
1232 call setline(1, range(1, 20))
1233 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001234 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001235 func MenuDone(id, res)
1236 echomsg "selected " .. a:res
1237 endfunc
1238 END
1239 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001240 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001241 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1242
1243 call term_sendkeys(buf, "jj")
1244 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1245
1246 call term_sendkeys(buf, " ")
1247 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1248
1249 " clean up
1250 call StopVimInTerminal(buf)
1251 call delete('XtestPopupMenu')
1252endfunc
1253
Bram Moolenaarf914a332019-07-20 15:09:56 +02001254func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001255 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001256
1257 let lines =<< trim END
1258 call setline(1, range(1, 20))
1259 hi PopupSelected ctermbg=green
1260 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1261 func MenuDone(id, res)
1262 echomsg "selected " .. a:res
1263 endfunc
1264 END
1265 call writefile(lines, 'XtestPopupNarrowMenu')
1266 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1267 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1268
1269 " clean up
1270 call term_sendkeys(buf, "x")
1271 call StopVimInTerminal(buf)
1272 call delete('XtestPopupNarrowMenu')
1273endfunc
1274
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001275func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001276 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001277
1278 " Create a popup without title or border, a line of padding will be added to
1279 " put the title on.
1280 let lines =<< trim END
1281 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001282 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001283 END
1284 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001285 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001286 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1287
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001288 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1289 call term_sendkeys(buf, ":\<CR>")
1290 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1291
1292 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1293 call term_sendkeys(buf, ":\<CR>")
1294 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1295
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001296 " clean up
1297 call StopVimInTerminal(buf)
1298 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001299
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001300 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001301 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001302 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001303 call assert_equal('Another Title', popup_getoptions(winid).title)
1304
1305 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001306endfunc
1307
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001308func Test_popup_close_callback()
1309 func PopupDone(id, result)
1310 let g:result = a:result
1311 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001312 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001313 redraw
1314 call popup_close(winid, 'done')
1315 call assert_equal('done', g:result)
1316endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001317
1318func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001319 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001320 redraw
1321 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001322 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001323 call assert_equal(5, pos.height)
1324
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001325 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001326 redraw
1327 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001328 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001329 call assert_equal(3, pos.height)
1330endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001331
1332func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001333 CheckScreendump
1334
Bram Moolenaar988c4332019-06-02 14:12:11 +02001335 " +-----------------------------+
1336 " | | |
1337 " | | |
1338 " | | |
1339 " | line1 |
1340 " |------------line2------------|
1341 " | line3 |
1342 " | line4 |
1343 " | |
1344 " | |
1345 " +-----------------------------+
1346 let lines =<< trim END
1347 only
1348 split
1349 vsplit
1350 let info_window1 = getwininfo()[0]
1351 let line = info_window1['height']
1352 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001353 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001354 \ line : line,
1355 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001356 \ })
1357 END
1358 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001359 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001360 call term_sendkeys(buf, "\<C-W>w")
1361 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1362
1363 " clean up
1364 call StopVimInTerminal(buf)
1365 call delete('XtestPopupBehind')
1366endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001367
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001368func s:VerifyPosition(p, msg, line, col, width, height)
1369 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1370 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1371 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1372 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001373endfunc
1374
1375func Test_popup_position_adjust()
1376 " Anything placed past 2 cells from of the right of the screen is moved to the
1377 " left.
1378 "
1379 " When wrapping is disabled, we also shift to the left to display on the
1380 " screen, unless fixed is set.
1381
1382 " Entries for cases which don't vary based on wrapping.
1383 " Format is per tests described below
1384 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001385 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1386 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1387 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1388 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1389 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1390 \
1391 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1392 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1393 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1394 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1395 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1396 \
1397 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1398 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1399 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1400 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1401 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1402 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001403
1404 " these test groups are dicts with:
1405 " - comment: something to identify the group of tests by
1406 " - options: dict of options to merge with the row/col in tests
1407 " - tests: list of cases. Each one is a list with elements:
1408 " - text
1409 " - row
1410 " - col
1411 " - expected row
1412 " - expected col
1413 " - expected width
1414 " - expected height
1415 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001416 \ #{
1417 \ comment: 'left-aligned with wrapping',
1418 \ options: #{
1419 \ wrap: 1,
1420 \ pos: 'botleft',
1421 \ },
1422 \ tests: both_wrap_tests + [
1423 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1424 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1425 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1426 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1427 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1428 \ ],
1429 \ },
1430 \ #{
1431 \ comment: 'left aligned without wrapping',
1432 \ options: #{
1433 \ wrap: 0,
1434 \ pos: 'botleft',
1435 \ },
1436 \ tests: both_wrap_tests + [
1437 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1438 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1439 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1440 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1441 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1442 \ ],
1443 \ },
1444 \ #{
1445 \ comment: 'left aligned with fixed position',
1446 \ options: #{
1447 \ wrap: 0,
1448 \ fixed: 1,
1449 \ pos: 'botleft',
1450 \ },
1451 \ tests: both_wrap_tests + [
1452 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1453 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1454 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1455 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1456 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1457 \ ],
1458 \ },
1459 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001460
1461 for test_group in tests
1462 for test in test_group.tests
1463 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001464 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001465 \ line: line,
1466 \ col: col,
1467 \ }
1468 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001469
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001470 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001471
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001472 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001473 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1474 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001475 endfor
1476 endfor
1477
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001478 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001479 %bwipe!
1480endfunc
1481
Bram Moolenaar3397f742019-06-02 18:40:06 +02001482func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001483 " width of screen
1484 let X = join(map(range(&columns), {->'X'}), '')
1485
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001486 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1487 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001488
1489 redraw
1490 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1491 call assert_equal(X, line)
1492
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001493 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001494 redraw
1495
1496 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001497 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1498 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001499
1500 redraw
1501 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1502 call assert_equal(X, line)
1503
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001504 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001505 redraw
1506
1507 " Extend so > window width
1508 let X .= 'x'
1509
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001510 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1511 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001512
1513 redraw
1514 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1515 call assert_equal(X[ : -2 ], line)
1516
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001517 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001518 redraw
1519
1520 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001521 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1522 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001523
1524 redraw
1525 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1526 call assert_equal(X[ : -2 ], line)
1527
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001528 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001529 redraw
1530
1531 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001532 let p = popup_create(X,
1533 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1534 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001535
1536 redraw
1537 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1538 let e_line = ' ' . X[ 1 : -2 ]
1539 call assert_equal(e_line, line)
1540
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001541 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001542 redraw
1543
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001544 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001545 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001546endfunc
1547
1548func Test_popup_moved()
1549 new
1550 call test_override('char_avail', 1)
1551 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1552
1553 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001554 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001555 redraw
1556 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001557 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001558 " trigger the check for last_cursormoved by going into insert mode
1559 call feedkeys("li\<Esc>", 'xt')
1560 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001561 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001562
1563 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001564 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001565 redraw
1566 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001567 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001568 call feedkeys("hi\<Esc>", 'xt')
1569 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001570 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001571
1572 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001573 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001574 redraw
1575 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001576 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001577 call feedkeys("li\<Esc>", 'xt')
1578 call assert_equal(1, popup_getpos(winid).visible)
1579 call feedkeys("ei\<Esc>", 'xt')
1580 call assert_equal(1, popup_getpos(winid).visible)
1581 call feedkeys("eli\<Esc>", 'xt')
1582 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001583 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001584
Bram Moolenaar17627312019-06-02 19:53:44 +02001585 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001586 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001587 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001588 redraw
1589 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001590 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001591 call feedkeys("eli\<Esc>", 'xt')
1592 call assert_equal(1, popup_getpos(winid).visible)
1593 call feedkeys("wi\<Esc>", 'xt')
1594 call assert_equal(1, popup_getpos(winid).visible)
1595 call feedkeys("Eli\<Esc>", 'xt')
1596 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001597 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001598
1599 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001600 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001601 redraw
1602 call assert_equal(1, popup_getpos(winid).visible)
1603 call feedkeys("eli\<Esc>", 'xt')
1604 call feedkeys("ei\<Esc>", 'xt')
1605 call assert_equal(1, popup_getpos(winid).visible)
1606 call feedkeys("eli\<Esc>", 'xt')
1607 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001608 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001609
1610 bwipe!
1611 call test_override('ALL', 0)
1612endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001613
1614func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001615 CheckFeature timers
1616 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001617
1618 call writefile([
1619 \ "call setline(1, range(1, 20))",
1620 \ "hi Notification ctermbg=lightblue",
1621 \ "call popup_notification('first notification', {})",
1622 \], 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001623 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001624 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1625
1626 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001627 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1628 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001629 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1630
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001631 " clean up
1632 call StopVimInTerminal(buf)
1633 call delete('XtestNotifications')
1634endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001635
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001636func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001637 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001638
1639 let lines =<< trim END
1640 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001641 hi ScrollThumb ctermbg=blue
1642 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001643 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001644 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001645 \ minwidth: 8,
1646 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001647 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001648 func ScrollUp()
1649 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1650 endfunc
1651 func ScrollDown()
1652 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1653 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001654 func ClickTop()
1655 call feedkeys("\<F4>\<LeftMouse>", "xt")
1656 endfunc
1657 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001658 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001659 call feedkeys("\<F5>\<LeftMouse>", "xt")
1660 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001661 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001662 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1663 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001664 END
1665 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001666 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001667 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1668
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001669 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001670 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001671 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1672
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001673 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001674 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001675 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1676
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001677 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001678 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001679 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1680
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001681 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb'})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001682 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1683 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1684
1685 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1686 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1687
1688 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001689 " wait a bit, otherwise it fails sometimes (double click recognized?)
1690 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001691 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1692 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1693
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001694 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1695 sleep 100m
1696 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1697 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1698
1699 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1700 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1701
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001702 " clean up
1703 call StopVimInTerminal(buf)
1704 call delete('XtestPopupScroll')
1705endfunc
1706
Bram Moolenaar437a7462019-07-05 20:17:22 +02001707func Test_popup_fitting_scrollbar()
1708 " this was causing a crash, divide by zero
1709 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001710 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001711 \ scrollbar: 1,
1712 \ maxwidth: 10,
1713 \ maxheight: 5,
1714 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001715 redraw
1716 call popup_clear()
1717endfunc
1718
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001719func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001720 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001721
1722 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001723 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001724 let p = popup_create('test', opts)
1725 call popup_settext(p, 'this is a text')
1726 END
1727
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001728 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001729 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001730 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1731
1732 " Setting to empty string clears it
1733 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1734 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1735
1736 " Setting a list
1737 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1738 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1739
1740 " Shrinking with a list
1741 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1742 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1743
1744 " Growing with a list
1745 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1746 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1747
1748 " Empty list clears
1749 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1750 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1751
1752 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001753 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001754 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1755
1756 " clean up
1757 call StopVimInTerminal(buf)
1758 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001759endfunc
1760
1761func Test_popup_hidden()
1762 new
1763
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001764 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001765 redraw
1766 call assert_equal(0, popup_getpos(winid).visible)
1767 call popup_close(winid)
1768
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001769 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001770 redraw
1771 call assert_equal(0, popup_getpos(winid).visible)
1772 call popup_close(winid)
1773
1774 func QuitCallback(id, res)
1775 let s:cb_winid = a:id
1776 let s:cb_res = a:res
1777 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001778 let winid = popup_dialog('make a choice', #{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001779 \ filter: 'popup_filter_yesno',
1780 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001781 \ })
1782 redraw
1783 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001784 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1785 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001786 exe "normal anot used by filter\<Esc>"
1787 call assert_equal('not used by filter', getline(1))
1788
1789 call popup_show(winid)
1790 call feedkeys('y', "xt")
1791 call assert_equal(1, s:cb_res)
1792
1793 bwipe!
1794 delfunc QuitCallback
1795endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001796
1797" Test options not checked elsewhere
1798func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001799 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001800 let options = popup_getoptions(winid)
1801 call assert_equal(1, options.wrap)
1802 call assert_equal(0, options.drag)
1803 call assert_equal('Beautiful', options.highlight)
1804
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001805 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001806 let options = popup_getoptions(winid)
1807 call assert_equal(0, options.wrap)
1808 call assert_equal(1, options.drag)
1809 call assert_equal('Another', options.highlight)
1810
1811 call popup_close(winid)
1812endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001813
1814func Test_popupwin_garbage_collect()
1815 func MyPopupFilter(x, winid, c)
1816 " NOP
1817 endfunc
1818
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001819 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001820 call test_garbagecollect_now()
1821 redraw
1822 " Must not crach caused by invalid memory access
1823 call feedkeys('j', 'xt')
1824 call assert_true(v:true)
1825
1826 call popup_close(winid)
1827 delfunc MyPopupFilter
1828endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001829
1830func Test_popupwin_with_buffer()
1831 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1832 let buf = bufadd('XsomeFile')
1833 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02001834
1835 setlocal number
1836 call setbufvar(buf, "&wrapmargin", 13)
1837
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001838 let winid = popup_create(buf, {})
1839 call assert_notequal(0, winid)
1840 let pos = popup_getpos(winid)
1841 call assert_equal(2, pos.height)
1842 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02001843
1844 " window-local option is set to default, buffer-local is not
1845 call assert_equal(0, getwinvar(winid, '&number'))
1846 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
1847
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001848 call popup_close(winid)
1849 call assert_equal({}, popup_getpos(winid))
1850 call assert_equal(1, bufloaded(buf))
1851 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02001852 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02001853
1854 edit test_popupwin.vim
1855 let winid = popup_create(bufnr(''), {})
1856 redraw
1857 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001858 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001859endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02001860
1861func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001862 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001863 \ maxwidth: 40,
1864 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02001865 \ })
1866 for top in range(1, 20)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001867 call popup_setoptions(winid, #{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02001868 redraw
1869 call assert_equal(19, popup_getpos(winid).width)
1870 endfor
1871 call popup_clear()
1872endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02001873
1874func Test_popupwin_buf_close()
1875 let buf = bufadd('Xtestbuf')
1876 call bufload(buf)
1877 call setbufline(buf, 1, ['just', 'some', 'lines'])
1878 let winid = popup_create(buf, {})
1879 redraw
1880 call assert_equal(3, popup_getpos(winid).height)
1881 let bufinfo = getbufinfo(buf)[0]
1882 call assert_equal(1, bufinfo.changed)
1883 call assert_equal(0, bufinfo.hidden)
1884 call assert_equal(0, bufinfo.listed)
1885 call assert_equal(1, bufinfo.loaded)
1886 call assert_equal([], bufinfo.windows)
1887 call assert_equal([winid], bufinfo.popups)
1888
1889 call popup_close(winid)
1890 call assert_equal({}, popup_getpos(winid))
1891 let bufinfo = getbufinfo(buf)[0]
1892 call assert_equal(1, bufinfo.changed)
1893 call assert_equal(1, bufinfo.hidden)
1894 call assert_equal(0, bufinfo.listed)
1895 call assert_equal(1, bufinfo.loaded)
1896 call assert_equal([], bufinfo.windows)
1897 call assert_equal([], bufinfo.popups)
1898 exe 'bwipe! ' .. buf
1899endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02001900
1901func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001902 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02001903
1904 let lines =<< trim END
1905 call setline(1, range(1, 10))
1906 hi ScrollThumb ctermbg=blue
1907 hi ScrollBar ctermbg=red
1908 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001909 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001910 \ maxwidth: 10,
1911 \ maxheight: 3,
1912 \ pos : 'topleft',
1913 \ col : a:col,
1914 \ line : a:line,
1915 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02001916 \ })
1917 endfunc
1918 call PopupMenu(['x'], 1, 1)
1919 call PopupMenu(['123456789|'], 1, 16)
1920 call PopupMenu(['123456789|' .. ' '], 7, 1)
1921 call PopupMenu([repeat('123456789|', 100)], 7, 16)
1922 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
1923 END
1924 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001925 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02001926 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
1927
1928 " close the menu popupwin.
1929 call term_sendkeys(buf, " ")
1930 call term_sendkeys(buf, " ")
1931 call term_sendkeys(buf, " ")
1932 call term_sendkeys(buf, " ")
1933 call term_sendkeys(buf, " ")
1934
1935 " clean up
1936 call StopVimInTerminal(buf)
1937 call delete('XtestPopupMenuMaxWidth')
1938endfunc
1939
Bram Moolenaara901a372019-07-13 16:38:50 +02001940func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001941 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02001942
1943 let lines =<< trim END
1944 call setline(1, range(1, 20))
1945 hi ScrollThumb ctermbg=blue
1946 hi ScrollBar ctermbg=red
1947 call popup_menu(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001948 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001949 \ minwidth: 8,
1950 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02001951 \ })
1952 END
1953 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001954 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02001955
1956 call term_sendkeys(buf, "j")
1957 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
1958
1959 call term_sendkeys(buf, "jjj")
1960 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
1961
1962 " if the cursor is the bottom line, it stays at the bottom line.
1963 call term_sendkeys(buf, repeat("j", 20))
1964 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
1965
1966 call term_sendkeys(buf, "kk")
1967 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
1968
1969 call term_sendkeys(buf, "k")
1970 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
1971
1972 " if the cursor is in the top line, it stays in the top line.
1973 call term_sendkeys(buf, repeat("k", 20))
1974 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
1975
1976 " close the menu popupwin.
1977 call term_sendkeys(buf, " ")
1978
1979 " clean up
1980 call StopVimInTerminal(buf)
1981 call delete('XtestPopupMenuScroll')
1982endfunc
1983
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001984func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001985 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001986
1987 let lines =<< trim END
1988 function! MyFilter(winid, key) abort
1989 if a:key == "0"
1990 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
1991 return 1
1992 endif
1993 if a:key == "G"
1994 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
1995 return 1
1996 endif
1997 if a:key == "j"
1998 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
1999 return 1
2000 endif
2001 if a:key == "k"
2002 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2003 return 1
2004 endif
2005 if a:key == 'x'
2006 call popup_close(a:winid)
2007 return 1
2008 endif
2009 return 0
2010 endfunction
2011 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2012 \ maxheight : 3,
2013 \ filter : 'MyFilter'
2014 \ })
2015 END
2016 call writefile(lines, 'XtestPopupMenuFilter')
2017 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2018
2019 call term_sendkeys(buf, "j")
2020 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2021
2022 call term_sendkeys(buf, "k")
2023 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2024
2025 call term_sendkeys(buf, "G")
2026 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2027
2028 call term_sendkeys(buf, "0")
2029 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2030
2031 call term_sendkeys(buf, "x")
2032
2033 " clean up
2034 call StopVimInTerminal(buf)
2035 call delete('XtestPopupMenuFilter')
2036endfunc
2037
2038func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002039 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002040
2041 let winid = popup_create('some text', {})
2042 call assert_equal(0, popup_getoptions(winid).cursorline)
2043 call popup_close(winid)
2044
2045 let winid = popup_create('some text', #{ cursorline: 1, })
2046 call assert_equal(1, popup_getoptions(winid).cursorline)
2047 call popup_close(winid)
2048
2049 let winid = popup_create('some text', #{ cursorline: 0, })
2050 call assert_equal(0, popup_getoptions(winid).cursorline)
2051 call popup_close(winid)
2052
2053 let winid = popup_menu('some text', {})
2054 call assert_equal(1, popup_getoptions(winid).cursorline)
2055 call popup_close(winid)
2056
2057 let winid = popup_menu('some text', #{ cursorline: 1, })
2058 call assert_equal(1, popup_getoptions(winid).cursorline)
2059 call popup_close(winid)
2060
2061 let winid = popup_menu('some text', #{ cursorline: 0, })
2062 call assert_equal(0, popup_getoptions(winid).cursorline)
2063 call popup_close(winid)
2064
2065 " ---------
2066 " Pattern 1
2067 " ---------
2068 let lines =<< trim END
2069 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2070 END
2071 call writefile(lines, 'XtestPopupCursorLine')
2072 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2073 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2074 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2075 call StopVimInTerminal(buf)
2076
2077 " ---------
2078 " Pattern 2
2079 " ---------
2080 let lines =<< trim END
2081 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2082 END
2083 call writefile(lines, 'XtestPopupCursorLine')
2084 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2085 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2086 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2087 call StopVimInTerminal(buf)
2088
2089 " ---------
2090 " Pattern 3
2091 " ---------
2092 let lines =<< trim END
2093 function! MyFilter(winid, key) abort
2094 if a:key == "j"
2095 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2096 return 1
2097 endif
2098 if a:key == 'x'
2099 call popup_close(a:winid)
2100 return 1
2101 endif
2102 return 0
2103 endfunction
2104 call popup_menu(['111', '222', '333'], #{
2105 \ cursorline : 0,
2106 \ maxheight : 2,
2107 \ filter : 'MyFilter',
2108 \ })
2109 END
2110 call writefile(lines, 'XtestPopupCursorLine')
2111 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2112 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2113 call term_sendkeys(buf, "j")
2114 call term_sendkeys(buf, "j")
2115 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2116 call term_sendkeys(buf, "x")
2117 call StopVimInTerminal(buf)
2118
2119 " ---------
2120 " Pattern 4
2121 " ---------
2122 let lines =<< trim END
2123 function! MyFilter(winid, key) abort
2124 if a:key == "j"
2125 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2126 return 1
2127 endif
2128 if a:key == 'x'
2129 call popup_close(a:winid)
2130 return 1
2131 endif
2132 return 0
2133 endfunction
2134 call popup_menu(['111', '222', '333'], #{
2135 \ cursorline : 1,
2136 \ maxheight : 2,
2137 \ filter : 'MyFilter',
2138 \ })
2139 END
2140 call writefile(lines, 'XtestPopupCursorLine')
2141 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2142 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2143 call term_sendkeys(buf, "j")
2144 call term_sendkeys(buf, "j")
2145 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2146 call term_sendkeys(buf, "x")
2147 call StopVimInTerminal(buf)
2148
2149 call delete('XtestPopupCursorLine')
2150endfunc
2151
Bram Moolenaarf914a332019-07-20 15:09:56 +02002152func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002153 CheckScreendump
2154
Bram Moolenaarf914a332019-07-20 15:09:56 +02002155 call writefile([
2156 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2157 \ "another\tXtagfile\t/^this is another",
2158 \ "theword\tXtagfile\t/^theword"],
2159 \ 'Xtags')
2160 call writefile(range(1,20)
2161 \ + ['theword is here']
2162 \ + range(22, 27)
2163 \ + ['this is another place']
2164 \ + range(29, 40),
2165 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002166 call writefile(range(1,10)
2167 \ + ['searched word is here']
2168 \ + range(12, 20),
2169 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002170 let lines =<< trim END
2171 set tags=Xtags
2172 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002173 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002174 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002175 \ 'three',
2176 \ 'four',
2177 \ 'five',
2178 \ 'six',
2179 \ 'seven',
2180 \ 'find theword somewhere',
2181 \ 'nine',
2182 \ 'this is another word',
2183 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002184 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002185 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002186 END
2187 call writefile(lines, 'XtestPreviewPopup')
2188 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2189
2190 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2191 call term_sendkeys(buf, ":\<CR>")
2192 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2193
2194 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2195 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2196
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002197 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002198 call term_sendkeys(buf, ":\<CR>")
2199 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2200
2201 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2202 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2203
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002204 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2205 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002206 call term_sendkeys(buf, ":cd testdir\<CR>")
2207
2208 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002209 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002210 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2211
2212 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2213 call term_sendkeys(buf, ":\<CR>")
2214 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2215
2216 call term_sendkeys(buf, ":pclose\<CR>")
2217 call term_sendkeys(buf, ":psearch searched\<CR>")
2218 call term_sendkeys(buf, ":\<CR>")
2219 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002220
Bram Moolenaarf914a332019-07-20 15:09:56 +02002221 call StopVimInTerminal(buf)
2222 call delete('Xtags')
2223 call delete('Xtagfile')
2224 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002225 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002226endfunc
2227
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002228func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002229 let lines =<< trim END
2230 set completeopt+=preview,popup
2231 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002232 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002233
2234 func CompleteFuncDict(findstart, base)
2235 if a:findstart
2236 if col('.') > 10
2237 return col('.') - 10
2238 endif
2239 return 0
2240 endif
2241
2242 return {
2243 \ 'words': [
2244 \ {
2245 \ 'word': 'aword',
2246 \ 'abbr': 'wrd',
2247 \ 'menu': 'extra text',
2248 \ 'info': 'words are cool',
2249 \ 'kind': 'W',
2250 \ 'user_data': 'test'
2251 \ },
2252 \ {
2253 \ 'word': 'anotherword',
2254 \ 'abbr': 'anotwrd',
2255 \ 'menu': 'extra text',
2256 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2257 \ 'kind': 'W',
2258 \ 'user_data': 'notest'
2259 \ },
2260 \ {
2261 \ 'word': 'noinfo',
2262 \ 'abbr': 'noawrd',
2263 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002264 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002265 \ 'kind': 'W',
2266 \ 'user_data': 'notest'
2267 \ },
2268 \ {
2269 \ 'word': 'thatword',
2270 \ 'abbr': 'thatwrd',
2271 \ 'menu': 'extra text',
2272 \ 'info': 'that word is cool',
2273 \ 'kind': 'W',
2274 \ 'user_data': 'notest'
2275 \ },
2276 \ ]
2277 \ }
2278 endfunc
2279 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002280 func ChangeColor()
2281 let id = popup_findinfo()
2282 call popup_setoptions(id, #{highlight: 'InfoPopup'})
2283 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002284 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002285 return lines
2286endfunc
2287
2288func Test_popupmenu_info_border()
2289 CheckScreendump
2290
2291 let lines = Get_popupmenu_lines()
2292 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002293 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002294
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002295 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2296 call term_wait(buf, 50)
2297
2298 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2299 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2300
2301 call term_sendkeys(buf, "\<C-N>")
2302 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2303
2304 call term_sendkeys(buf, "\<C-N>")
2305 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2306
2307 call term_sendkeys(buf, "\<C-N>\<C-N>")
2308 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2309
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002310 " info on the left with scrollbar
2311 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2312 call term_sendkeys(buf, "\<C-N>\<C-N>")
2313 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2314
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002315 call StopVimInTerminal(buf)
2316 call delete('XtestInfoPopup')
2317endfunc
2318
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002319func Test_popupmenu_info_noborder()
2320 CheckScreendump
2321
2322 let lines = Get_popupmenu_lines()
2323 call add(lines, 'set completepopup=height:4,border:off')
2324 call writefile(lines, 'XtestInfoPopupNb')
2325
2326 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2327 call term_wait(buf, 50)
2328
2329 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2330 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2331
2332 call StopVimInTerminal(buf)
2333 call delete('XtestInfoPopupNb')
2334endfunc
2335
Bram Moolenaar258cef52019-08-21 17:29:29 +02002336func Test_popupmenu_info_align_menu()
2337 CheckScreendump
2338
2339 let lines = Get_popupmenu_lines()
2340 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2341 call writefile(lines, 'XtestInfoPopupNb')
2342
2343 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2344 call term_wait(buf, 50)
2345
2346 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2347 call term_sendkeys(buf, "\<C-N>")
2348 call term_sendkeys(buf, "\<C-N>")
2349 call term_sendkeys(buf, "\<C-N>")
2350 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2351
2352 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2353 call term_sendkeys(buf, "\<C-N>")
2354 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2355
2356 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002357 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002358 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2359 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2360 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2361
2362 call StopVimInTerminal(buf)
2363 call delete('XtestInfoPopupNb')
2364endfunc
2365
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002366func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02002367 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002368 call popup_clear()
2369 let winid = popup_notification('nothing wrong', {})
2370 call assert_equal(bufnr, winbufnr(winid))
2371 call popup_clear()
2372endfunc
2373
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002374" vim: shiftwidth=2 sts=2