blob: f0959e76706d1ed57c64dc2cafaf7565ccc3cef6 [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 Moolenaar8c6173c2019-08-30 22:08:34 +0200316 let winid = 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 Moolenaar8c6173c2019-08-30 22:08:34 +0200323 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
324
325 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
326 call term_sendkeys(buf, ":\<CR>")
327 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200328
329 " clean up
330 call StopVimInTerminal(buf)
331 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200332
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200333 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200334 \ maxheight: 2,
335 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200336 \ })
337 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200338 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200339 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200340 call popup_close(winid)
341
342 let winid = popup_create(['xxx']->repeat(50), #{
343 \ maxheight: 3,
344 \ firstline: 11,
345 \ })
346 redraw
347 call assert_equal(11, popup_getoptions(winid).firstline)
348 call assert_equal(11, popup_getpos(winid).firstline)
349
350 " Normal command changes what is displayed but not "firstline"
351 call win_execute(winid, "normal! \<c-y>")
352 call assert_equal(11, popup_getoptions(winid).firstline)
353 call assert_equal(10, popup_getpos(winid).firstline)
354
355 " Making some property change applies "firstline" again
356 call popup_setoptions(winid, #{line: 4})
357 call assert_equal(11, popup_getoptions(winid).firstline)
358 call assert_equal(11, popup_getpos(winid).firstline)
359
360 " Remove "firstline" property and scroll
361 call popup_setoptions(winid, #{firstline: 0})
362 call win_execute(winid, "normal! \<c-y>")
363 call assert_equal(0, popup_getoptions(winid).firstline)
364 call assert_equal(10, popup_getpos(winid).firstline)
365
366 " Making some property change has no side effect
367 call popup_setoptions(winid, #{line: 3})
368 call assert_equal(0, popup_getoptions(winid).firstline)
369 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaarae943152019-06-16 22:54:14 +0200370
371 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200372endfunc
373
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200374func Test_popup_noscrolloff()
375 set scrolloff=5
376 let winid = popup_create(['xxx']->repeat(50), #{
377 \ maxheight: 5,
378 \ firstline: 11,
379 \ })
380 redraw
381 call assert_equal(11, popup_getoptions(winid).firstline)
382 call assert_equal(11, popup_getpos(winid).firstline)
383
384 call popup_setoptions(winid, #{firstline: 0})
385 call win_execute(winid, "normal! \<c-y>")
386 call assert_equal(0, popup_getoptions(winid).firstline)
387 call assert_equal(10, popup_getpos(winid).firstline)
388
389 call popup_close(winid)
390endfunc
391
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200392func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200393 CheckScreendump
394
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200395 " create a popup that covers the command line
396 let lines =<< trim END
397 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200398 split
399 vsplit
400 $wincmd w
401 vsplit
402 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200403 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200404 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200405 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200406 \ border: [],
407 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200408 \ })
409 func Dragit()
410 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
411 endfunc
412 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200413 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200414 func Resize()
415 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
416 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200417 map <silent> <F5> :call test_setmouse(6, 21)<CR>
418 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200419 END
420 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200421 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200422 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
423
424 call term_sendkeys(buf, ":call Dragit()\<CR>")
425 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
426
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200427 call term_sendkeys(buf, ":call Resize()\<CR>")
428 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
429
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200430 " clean up
431 call StopVimInTerminal(buf)
432 call delete('XtestPopupDrag')
433endfunc
434
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200435func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200436 CheckScreendump
437
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200438 let lines =<< trim END
439 call setline(1, range(1, 20))
440 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200441 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200442 \ close: 'button',
443 \ border: [],
444 \ line: 1,
445 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200446 \ })
447 func CloseMsg(id, result)
448 echomsg 'Popup closed with ' .. a:result
449 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200450 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200451 \ close: 'click',
452 \ line: 3,
453 \ col: 15,
454 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200455 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200456 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200457 \ close: 'button',
458 \ line: 5,
459 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200460 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200461 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200462 \ close: 'button',
463 \ padding: [],
464 \ line: 5,
465 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200466 \ })
467 func CloseWithX()
468 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
469 endfunc
470 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
471 func CloseWithClick()
472 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
473 endfunc
474 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200475 func CreateWithMenuFilter()
476 let winid = popup_create('barfoo', #{
477 \ close: 'button',
478 \ filter: 'popup_filter_menu',
479 \ border: [],
480 \ line: 1,
481 \ col: 40,
482 \ })
483 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200484 END
485 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200486 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200487 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
488
489 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
490 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
491
492 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
493 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
494
Bram Moolenaarf6396232019-08-24 19:36:00 +0200495 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
496 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
497
498 " We have to send the actual mouse code, feedkeys() would be caught the
499 " filter.
500 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
501 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
502
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200503 " clean up
504 call StopVimInTerminal(buf)
505 call delete('XtestPopupClose')
506endfunction
507
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200508func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200509 CheckScreendump
510
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200511 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200512 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200513 hi PopupColor ctermbg=lightgrey
514 let winid = popup_create([
515 \ 'some text',
516 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200517 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200518 \ line: 1,
519 \ col: 10,
520 \ wrap: 0,
521 \ fixed: 1,
522 \ zindex: 90,
523 \ padding: [],
524 \ highlight: 'PopupColor',
525 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200526 call popup_create([
527 \ 'xxxxxxxxx',
528 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200529 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200530 \ line: 3,
531 \ col: 18,
532 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200533 let winidb = popup_create([
534 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200535 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200536 \ line: 7,
537 \ col: 10,
538 \ wrap: 0,
539 \ fixed: 1,
540 \ close: 'button',
541 \ zindex: 90,
542 \ padding: [],
543 \ border: [],
544 \ 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 +0200545 END
546 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200547 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200548 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
549
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200550 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
551 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200552 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200553 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
554
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200555 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
556 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200557 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200558 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
559
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200560 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
561 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200562 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200563 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
564
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200565 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
566 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200567 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200568 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
569
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200570 " clean up
571 call StopVimInTerminal(buf)
572 call delete('XtestPopupMask')
573endfunc
574
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200575func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200576 CheckScreendump
577 CheckFeature clipboard_working
578
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200579 " create a popup with some text to be selected
580 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200581 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200582 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200583 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200584 \ drag: 1,
585 \ border: [],
586 \ line: 3,
587 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200588 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200589 \ })
590 func Select1()
591 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
592 endfunc
593 map <silent> <F3> :call test_setmouse(4, 15)<CR>
594 map <silent> <F4> :call test_setmouse(6, 23)<CR>
595 END
596 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200597 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200598 call term_sendkeys(buf, ":call Select1()\<CR>")
599 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
600
601 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
602 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200603 " clean the command line, sometimes it still shows a command
604 call term_sendkeys(buf, ":\<esc>")
605
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200606 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
607
608 " clean up
609 call StopVimInTerminal(buf)
610 call delete('XtestPopupSelect')
611endfunc
612
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200613func Test_popup_in_tab()
614 " default popup is local to tab, not visible when in other tab
615 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200616 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200617 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200618 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200619 tabnew
620 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200621 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200622 quit
623 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200624
625 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200626 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200627 " buffer is gone now
628 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200629
630 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200631 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200632 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200633 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200634 tabnew
635 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200636 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200637 quit
638 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200639 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200640
641 " create popup in other tab
642 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200643 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200644 call assert_equal(0, popup_getpos(winid).visible)
645 call assert_equal(1, popup_getoptions(winid).tabpage)
646 quit
647 call assert_equal(1, popup_getpos(winid).visible)
648 call assert_equal(0, popup_getoptions(winid).tabpage)
649 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200650endfunc
651
652func Test_popup_valid_arguments()
653 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200654 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200655 let pos = popup_getpos(winid)
656 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200657 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200658
659 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200660 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200661 let pos = popup_getpos(winid)
662 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200663 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200664
665 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200666 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200667 let pos = popup_getpos(winid)
668 let around = (&columns - pos.width) / 2
669 call assert_inrange(around - 1, around + 1, pos.col)
670 let around = (&lines - pos.height) / 2
671 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200672 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200673endfunc
674
675func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200676 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200677 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200678 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200679 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200680
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200681 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200682 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200683 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200684 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200685 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200686 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200687 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200688 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200689
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200690 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200691 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200692 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200693 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200694 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200695 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200696 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200697 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200698
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200699 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200700 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200701 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200702 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200703 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200704 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200705 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200706 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200707 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
708 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200709 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200710 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200711
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200712 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200713 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200714 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200715 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200716 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200717 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200718 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
719 call popup_clear()
720 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200721 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200722 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200723endfunc
724
Bram Moolenaareea16992019-05-31 17:34:48 +0200725func Test_win_execute_closing_curwin()
726 split
727 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200728 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200729 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200730endfunc
731
732func Test_win_execute_not_allowed()
733 let winid = popup_create('some text', {})
734 call assert_fails('call win_execute(winid, "split")', 'E994:')
735 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
736 call assert_fails('call win_execute(winid, "close")', 'E994:')
737 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200738 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200739 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
740 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
741 call assert_fails('call win_execute(winid, "next")', 'E994:')
742 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
743 call assert_fails('call win_execute(winid, "buf")', 'E994:')
744 call assert_fails('call win_execute(winid, "edit")', 'E994:')
745 call assert_fails('call win_execute(winid, "enew")', 'E994:')
746 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
747 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
748 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
749 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200750 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200751endfunc
752
Bram Moolenaar402502d2019-05-30 22:07:36 +0200753func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200754 CheckScreendump
755
Bram Moolenaar402502d2019-05-30 22:07:36 +0200756 let lines =<< trim END
757 call setline(1, range(1, 100))
758 let winid = popup_create(
759 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200760 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200761 END
762 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200763 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200764 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
765
766 " clean up
767 call StopVimInTerminal(buf)
768 call delete('XtestPopup')
769endfunc
770
771func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200772 CheckScreendump
773
Bram Moolenaar402502d2019-05-30 22:07:36 +0200774 let lines =<< trim END
775 call setline(1, range(1, 100))
776 let winid = popup_create(
777 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200778 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200779 END
780 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200781 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200782 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
783
784 " clean up
785 call StopVimInTerminal(buf)
786 call delete('XtestPopup')
787endfunc
788
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200789func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200790 CheckScreendump
791
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200792 let lines =<< trim END
793 set showbreak=>>\
794 call setline(1, range(1, 20))
795 let winid = popup_dialog(
796 \ 'a long line here',
797 \ #{filter: 'popup_filter_yesno'})
798 END
799 call writefile(lines, 'XtestPopupShowbreak')
800 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
801 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
802
803 " clean up
804 call term_sendkeys(buf, "y")
805 call StopVimInTerminal(buf)
806 call delete('XtestPopupShowbreak')
807endfunc
808
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200809func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200810 CheckFeature timers
811
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200812 topleft vnew
813 call setline(1, 'hello')
814
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200815 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200816 \ line: 1,
817 \ col: 1,
818 \ minwidth: 20,
819 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200820 \})
821 redraw
822 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
823 call assert_equal('world', line)
824
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200825 call assert_equal(winid, popup_locate(1, 1))
826 call assert_equal(winid, popup_locate(1, 20))
827 call assert_equal(0, popup_locate(1, 21))
828 call assert_equal(0, popup_locate(2, 1))
829
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200830 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200831 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200832 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
833 call assert_equal('hello', line)
834
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200835 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200836 \ line: &lines,
837 \ col: 10,
838 \ minwidth: 20,
839 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200840 \})
841 redraw
842 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
843 call assert_match('.*on the command line.*', line)
844
845 sleep 700m
846 redraw
847 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
848 call assert_notmatch('.*on the command line.*', line)
849
850 bwipe!
851endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200852
853func Test_popup_hide()
854 topleft vnew
855 call setline(1, 'hello')
856
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200857 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200858 \ line: 1,
859 \ col: 1,
860 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200861 \})
862 redraw
863 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
864 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200865 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200866 " buffer is still listed and active
867 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200868
869 call popup_hide(winid)
870 redraw
871 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
872 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200873 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200874 " buffer is still listed but hidden
875 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200876
877 call popup_show(winid)
878 redraw
879 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
880 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200881 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200882
883
884 call popup_close(winid)
885 redraw
886 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
887 call assert_equal('hello', line)
888
889 " error is given for existing non-popup window
890 call assert_fails('call popup_hide(win_getid())', 'E993:')
891
892 " no error non-existing window
893 call popup_hide(1234234)
894 call popup_show(41234234)
895
896 bwipe!
897endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200898
899func Test_popup_move()
900 topleft vnew
901 call setline(1, 'hello')
902
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200903 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200904 \ line: 1,
905 \ col: 1,
906 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200907 \})
908 redraw
909 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
910 call assert_equal('world ', line)
911
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200912 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200913 redraw
914 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
915 call assert_equal('hello ', line)
916 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
917 call assert_equal('~world', line)
918
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200919 call popup_move(winid, #{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200920 redraw
921 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
922 call assert_equal('hworld', line)
923
924 call popup_close(winid)
925
926 bwipe!
927endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200928
Bram Moolenaar402502d2019-05-30 22:07:36 +0200929func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200930 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200931 \ line: 2,
932 \ col: 3,
933 \ minwidth: 10,
934 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200935 \})
936 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200937 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200938 call assert_equal(2, res.line)
939 call assert_equal(3, res.col)
940 call assert_equal(10, res.width)
941 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200942 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200943
944 call popup_close(winid)
945endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200946
947func Test_popup_width_longest()
948 let tests = [
949 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
950 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
951 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
952 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
953 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
954 \ ]
955
956 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200957 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200958 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200959 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200960 call assert_equal(test[1], position.width)
961 call popup_close(winid)
962 endfor
963endfunc
964
965func Test_popup_wraps()
966 let tests = [
967 \ ['nowrap', 6, 1],
968 \ ['a line that wraps once', 12, 2],
969 \ ['a line that wraps two times', 12, 3],
970 \ ]
971 for test in tests
972 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200973 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200974 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200975 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200976 call assert_equal(test[1], position.width)
977 call assert_equal(test[2], position.height)
978
979 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200980 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200981 endfor
982endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200983
984func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200985 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200986 \ line: 2,
987 \ col: 3,
988 \ minwidth: 10,
989 \ minheight: 11,
990 \ maxwidth: 20,
991 \ maxheight: 21,
992 \ zindex: 100,
993 \ time: 5000,
994 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200995 \})
996 redraw
997 let res = popup_getoptions(winid)
998 call assert_equal(2, res.line)
999 call assert_equal(3, res.col)
1000 call assert_equal(10, res.minwidth)
1001 call assert_equal(11, res.minheight)
1002 call assert_equal(20, res.maxwidth)
1003 call assert_equal(21, res.maxheight)
1004 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001005 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001006 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001007 if has('timers')
1008 call assert_equal(5000, res.time)
1009 endif
1010 call popup_close(winid)
1011
1012 let winid = popup_create('hello', {})
1013 redraw
1014 let res = popup_getoptions(winid)
1015 call assert_equal(0, res.line)
1016 call assert_equal(0, res.col)
1017 call assert_equal(0, res.minwidth)
1018 call assert_equal(0, res.minheight)
1019 call assert_equal(0, res.maxwidth)
1020 call assert_equal(0, res.maxheight)
1021 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001022 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001023 if has('timers')
1024 call assert_equal(0, res.time)
1025 endif
1026 call popup_close(winid)
1027 call assert_equal({}, popup_getoptions(winid))
1028endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001029
1030func Test_popup_option_values()
1031 new
1032 " window-local
1033 setlocal number
1034 setlocal nowrap
1035 " buffer-local
1036 setlocal omnifunc=Something
1037 " global/buffer-local
1038 setlocal path=/there
1039 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001040 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001041
1042 let winid = popup_create('hello', {})
1043 call assert_equal(0, getwinvar(winid, '&number'))
1044 call assert_equal(1, getwinvar(winid, '&wrap'))
1045 call assert_equal('', getwinvar(winid, '&omnifunc'))
1046 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001047 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1048
1049 " 'scrolloff' is reset to zero
1050 call assert_equal(5, &scrolloff)
1051 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001052
1053 call popup_close(winid)
1054 bwipe
1055endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001056
1057func Test_popup_atcursor()
1058 topleft vnew
1059 call setline(1, [
1060 \ 'xxxxxxxxxxxxxxxxx',
1061 \ 'xxxxxxxxxxxxxxxxx',
1062 \ 'xxxxxxxxxxxxxxxxx',
1063 \])
1064
1065 call cursor(2, 2)
1066 redraw
1067 let winid = popup_atcursor('vim', {})
1068 redraw
1069 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1070 call assert_equal('xvimxxxxxxxxxxxxx', line)
1071 call popup_close(winid)
1072
1073 call cursor(3, 4)
1074 redraw
1075 let winid = popup_atcursor('vim', {})
1076 redraw
1077 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1078 call assert_equal('xxxvimxxxxxxxxxxx', line)
1079 call popup_close(winid)
1080
1081 call cursor(1, 1)
1082 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001083 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001084 \ line: 'cursor+2',
1085 \ col: 'cursor+1',
1086 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001087 redraw
1088 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1089 call assert_equal('xvimxxxxxxxxxxxxx', line)
1090 call popup_close(winid)
1091
1092 call cursor(3, 3)
1093 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001094 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001095 \ line: 'cursor-2',
1096 \ col: 'cursor-1',
1097 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001098 redraw
1099 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1100 call assert_equal('xvimxxxxxxxxxxxxx', line)
1101 call popup_close(winid)
1102
Bram Moolenaar402502d2019-05-30 22:07:36 +02001103 " just enough room above
1104 call cursor(3, 3)
1105 redraw
1106 let winid = popup_atcursor(['vim', 'is great'], {})
1107 redraw
1108 let pos = popup_getpos(winid)
1109 call assert_equal(1, pos.line)
1110 call popup_close(winid)
1111
1112 " not enough room above, popup goes below the cursor
1113 call cursor(3, 3)
1114 redraw
1115 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1116 redraw
1117 let pos = popup_getpos(winid)
1118 call assert_equal(4, pos.line)
1119 call popup_close(winid)
1120
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001121 " cursor in first line, popup in line 2
1122 call cursor(1, 1)
1123 redraw
1124 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1125 redraw
1126 let pos = popup_getpos(winid)
1127 call assert_equal(2, pos.line)
1128 call popup_close(winid)
1129
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001130 bwipe!
1131endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001132
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001133func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001134 CheckScreendump
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001135
1136 let lines =<< trim END
1137 call setline(1, range(1, 20))
1138 call setline(5, 'here is some text to hover over')
1139 set balloonevalterm
1140 set balloonexpr=BalloonExpr()
1141 set balloondelay=100
1142 func BalloonExpr()
1143 let s:winid = popup_beval([v:beval_text], {})
1144 return ''
1145 endfunc
1146 func Hover()
1147 call test_setmouse(5, 15)
1148 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1149 sleep 100m
1150 endfunc
1151 func MoveOntoPopup()
1152 call test_setmouse(4, 17)
1153 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1154 endfunc
1155 func MoveAway()
1156 call test_setmouse(5, 13)
1157 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1158 endfunc
1159 END
1160 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001161 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001162 call term_wait(buf, 100)
1163 call term_sendkeys(buf, 'j')
1164 call term_sendkeys(buf, ":call Hover()\<CR>")
1165 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1166
1167 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1168 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1169
1170 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1171 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1172
1173 " clean up
1174 call StopVimInTerminal(buf)
1175 call delete('XtestPopupBeval')
1176endfunc
1177
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001178func Test_popup_filter()
1179 new
1180 call setline(1, 'some text')
1181
1182 func MyPopupFilter(winid, c)
1183 if a:c == 'e'
1184 let g:eaten = 'e'
1185 return 1
1186 endif
1187 if a:c == '0'
1188 let g:ignored = '0'
1189 return 0
1190 endif
1191 if a:c == 'x'
1192 call popup_close(a:winid)
1193 return 1
1194 endif
1195 return 0
1196 endfunc
1197
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001198 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001199 redraw
1200
1201 " e is consumed by the filter
1202 call feedkeys('e', 'xt')
1203 call assert_equal('e', g:eaten)
1204
1205 " 0 is ignored by the filter
1206 normal $
1207 call assert_equal(9, getcurpos()[2])
1208 call feedkeys('0', 'xt')
1209 call assert_equal('0', g:ignored)
1210 call assert_equal(1, getcurpos()[2])
1211
1212 " x closes the popup
1213 call feedkeys('x', 'xt')
1214 call assert_equal('e', g:eaten)
1215 call assert_equal(-1, winbufnr(winid))
1216
1217 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001218 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001219endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001220
Bram Moolenaara42d9452019-06-15 21:46:30 +02001221func ShowDialog(key, result)
1222 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001223 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001224 \ filter: 'popup_filter_yesno',
1225 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001226 \ })
1227 redraw
1228 call feedkeys(a:key, "xt")
1229 call assert_equal(winid, s:cb_winid)
1230 call assert_equal(a:result, s:cb_res)
1231endfunc
1232
1233func Test_popup_dialog()
1234 func QuitCallback(id, res)
1235 let s:cb_winid = a:id
1236 let s:cb_res = a:res
1237 endfunc
1238
1239 let winid = ShowDialog("y", 1)
1240 let winid = ShowDialog("Y", 1)
1241 let winid = ShowDialog("n", 0)
1242 let winid = ShowDialog("N", 0)
1243 let winid = ShowDialog("x", 0)
1244 let winid = ShowDialog("X", 0)
1245 let winid = ShowDialog("\<Esc>", 0)
1246 let winid = ShowDialog("\<C-C>", -1)
1247
1248 delfunc QuitCallback
1249endfunc
1250
Bram Moolenaara730e552019-06-16 19:05:31 +02001251func ShowMenu(key, result)
1252 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001253 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001254 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001255 \ })
1256 redraw
1257 call feedkeys(a:key, "xt")
1258 call assert_equal(winid, s:cb_winid)
1259 call assert_equal(a:result, s:cb_res)
1260endfunc
1261
1262func Test_popup_menu()
1263 func QuitCallback(id, res)
1264 let s:cb_winid = a:id
1265 let s:cb_res = a:res
1266 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001267 " mapping won't be used in popup
1268 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001269
1270 let winid = ShowMenu(" ", 1)
1271 let winid = ShowMenu("j \<CR>", 2)
1272 let winid = ShowMenu("JjK \<CR>", 2)
1273 let winid = ShowMenu("jjjjjj ", 3)
1274 let winid = ShowMenu("kkk ", 1)
1275 let winid = ShowMenu("x", -1)
1276 let winid = ShowMenu("X", -1)
1277 let winid = ShowMenu("\<Esc>", -1)
1278 let winid = ShowMenu("\<C-C>", -1)
1279
1280 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001281 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001282endfunc
1283
1284func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001285 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001286
1287 let lines =<< trim END
1288 call setline(1, range(1, 20))
1289 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001290 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001291 func MenuDone(id, res)
1292 echomsg "selected " .. a:res
1293 endfunc
1294 END
1295 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001296 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001297 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1298
1299 call term_sendkeys(buf, "jj")
1300 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1301
1302 call term_sendkeys(buf, " ")
1303 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1304
1305 " clean up
1306 call StopVimInTerminal(buf)
1307 call delete('XtestPopupMenu')
1308endfunc
1309
Bram Moolenaarf914a332019-07-20 15:09:56 +02001310func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001311 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001312
1313 let lines =<< trim END
1314 call setline(1, range(1, 20))
1315 hi PopupSelected ctermbg=green
1316 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1317 func MenuDone(id, res)
1318 echomsg "selected " .. a:res
1319 endfunc
1320 END
1321 call writefile(lines, 'XtestPopupNarrowMenu')
1322 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1323 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1324
1325 " clean up
1326 call term_sendkeys(buf, "x")
1327 call StopVimInTerminal(buf)
1328 call delete('XtestPopupNarrowMenu')
1329endfunc
1330
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001331func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001332 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001333
1334 " Create a popup without title or border, a line of padding will be added to
1335 " put the title on.
1336 let lines =<< trim END
1337 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001338 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001339 END
1340 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001341 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001342 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1343
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001344 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1345 call term_sendkeys(buf, ":\<CR>")
1346 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1347
1348 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1349 call term_sendkeys(buf, ":\<CR>")
1350 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1351
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001352 " clean up
1353 call StopVimInTerminal(buf)
1354 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001355
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001356 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001357 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001358 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001359 call assert_equal('Another Title', popup_getoptions(winid).title)
1360
1361 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001362endfunc
1363
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001364func Test_popup_close_callback()
1365 func PopupDone(id, result)
1366 let g:result = a:result
1367 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001368 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001369 redraw
1370 call popup_close(winid, 'done')
1371 call assert_equal('done', g:result)
1372endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001373
1374func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001375 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001376 redraw
1377 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001378 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001379 call assert_equal(5, pos.height)
1380
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001381 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001382 redraw
1383 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001384 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001385 call assert_equal(3, pos.height)
1386endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001387
1388func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001389 CheckScreendump
1390
Bram Moolenaar988c4332019-06-02 14:12:11 +02001391 " +-----------------------------+
1392 " | | |
1393 " | | |
1394 " | | |
1395 " | line1 |
1396 " |------------line2------------|
1397 " | line3 |
1398 " | line4 |
1399 " | |
1400 " | |
1401 " +-----------------------------+
1402 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001403 split
1404 vsplit
1405 let info_window1 = getwininfo()[0]
1406 let line = info_window1['height']
1407 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001408 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001409 \ line : line,
1410 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001411 \ })
1412 END
1413 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001414 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001415 call term_sendkeys(buf, "\<C-W>w")
1416 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1417
1418 " clean up
1419 call StopVimInTerminal(buf)
1420 call delete('XtestPopupBehind')
1421endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001422
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001423func s:VerifyPosition(p, msg, line, col, width, height)
1424 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1425 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1426 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1427 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001428endfunc
1429
1430func Test_popup_position_adjust()
1431 " Anything placed past 2 cells from of the right of the screen is moved to the
1432 " left.
1433 "
1434 " When wrapping is disabled, we also shift to the left to display on the
1435 " screen, unless fixed is set.
1436
1437 " Entries for cases which don't vary based on wrapping.
1438 " Format is per tests described below
1439 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001440 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1441 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1442 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1443 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1444 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1445 \
1446 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1447 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1448 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1449 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1450 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1451 \
1452 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1453 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1454 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1455 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1456 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1457 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001458
1459 " these test groups are dicts with:
1460 " - comment: something to identify the group of tests by
1461 " - options: dict of options to merge with the row/col in tests
1462 " - tests: list of cases. Each one is a list with elements:
1463 " - text
1464 " - row
1465 " - col
1466 " - expected row
1467 " - expected col
1468 " - expected width
1469 " - expected height
1470 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001471 \ #{
1472 \ comment: 'left-aligned with wrapping',
1473 \ options: #{
1474 \ wrap: 1,
1475 \ pos: 'botleft',
1476 \ },
1477 \ tests: both_wrap_tests + [
1478 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1479 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1480 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1481 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1482 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1483 \ ],
1484 \ },
1485 \ #{
1486 \ comment: 'left aligned without wrapping',
1487 \ options: #{
1488 \ wrap: 0,
1489 \ pos: 'botleft',
1490 \ },
1491 \ tests: both_wrap_tests + [
1492 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1493 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1494 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1495 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1496 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1497 \ ],
1498 \ },
1499 \ #{
1500 \ comment: 'left aligned with fixed position',
1501 \ options: #{
1502 \ wrap: 0,
1503 \ fixed: 1,
1504 \ pos: 'botleft',
1505 \ },
1506 \ tests: both_wrap_tests + [
1507 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1508 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1509 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1510 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1511 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1512 \ ],
1513 \ },
1514 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001515
1516 for test_group in tests
1517 for test in test_group.tests
1518 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001519 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001520 \ line: line,
1521 \ col: col,
1522 \ }
1523 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001524
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001525 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001526
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001527 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001528 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1529 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001530 endfor
1531 endfor
1532
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001533 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001534 %bwipe!
1535endfunc
1536
Bram Moolenaar3397f742019-06-02 18:40:06 +02001537func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001538 " width of screen
1539 let X = join(map(range(&columns), {->'X'}), '')
1540
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001541 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1542 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001543
1544 redraw
1545 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1546 call assert_equal(X, line)
1547
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001548 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001549 redraw
1550
1551 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001552 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1553 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001554
1555 redraw
1556 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1557 call assert_equal(X, line)
1558
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001559 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001560 redraw
1561
1562 " Extend so > window width
1563 let X .= 'x'
1564
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001565 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1566 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001567
1568 redraw
1569 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1570 call assert_equal(X[ : -2 ], line)
1571
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001572 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001573 redraw
1574
1575 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001576 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1577 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001578
1579 redraw
1580 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1581 call assert_equal(X[ : -2 ], line)
1582
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001583 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001584 redraw
1585
1586 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001587 let p = popup_create(X,
1588 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1589 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001590
1591 redraw
1592 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1593 let e_line = ' ' . X[ 1 : -2 ]
1594 call assert_equal(e_line, line)
1595
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001596 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001597 redraw
1598
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001599 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001600 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001601endfunc
1602
1603func Test_popup_moved()
1604 new
1605 call test_override('char_avail', 1)
1606 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1607
1608 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001609 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001610 redraw
1611 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001612 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001613 " trigger the check for last_cursormoved by going into insert mode
1614 call feedkeys("li\<Esc>", 'xt')
1615 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001616 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001617
1618 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001619 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001620 redraw
1621 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001622 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001623 call feedkeys("hi\<Esc>", 'xt')
1624 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001625 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001626
1627 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001628 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001629 redraw
1630 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001631 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001632 call feedkeys("li\<Esc>", 'xt')
1633 call assert_equal(1, popup_getpos(winid).visible)
1634 call feedkeys("ei\<Esc>", 'xt')
1635 call assert_equal(1, popup_getpos(winid).visible)
1636 call feedkeys("eli\<Esc>", 'xt')
1637 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001638 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001639
Bram Moolenaar17627312019-06-02 19:53:44 +02001640 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001641 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001642 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001643 redraw
1644 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001645 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001646 call feedkeys("eli\<Esc>", 'xt')
1647 call assert_equal(1, popup_getpos(winid).visible)
1648 call feedkeys("wi\<Esc>", 'xt')
1649 call assert_equal(1, popup_getpos(winid).visible)
1650 call feedkeys("Eli\<Esc>", 'xt')
1651 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001652 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001653
1654 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001655 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001656 redraw
1657 call assert_equal(1, popup_getpos(winid).visible)
1658 call feedkeys("eli\<Esc>", 'xt')
1659 call feedkeys("ei\<Esc>", 'xt')
1660 call assert_equal(1, popup_getpos(winid).visible)
1661 call feedkeys("eli\<Esc>", 'xt')
1662 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001663 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001664
1665 bwipe!
1666 call test_override('ALL', 0)
1667endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001668
1669func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001670 CheckFeature timers
1671 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001672
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001673 let lines =<< trim END
1674 call setline(1, range(1, 20))
1675 hi Notification ctermbg=lightblue
1676 call popup_notification('first notification', {})
1677 END
1678 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001679 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001680 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1681
1682 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001683 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1684 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001685 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1686
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001687 " clean up
1688 call StopVimInTerminal(buf)
1689 call delete('XtestNotifications')
1690endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001691
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001692func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001693 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001694
1695 let lines =<< trim END
1696 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001697 hi ScrollThumb ctermbg=blue
1698 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001699 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001700 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001701 \ minwidth: 8,
1702 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001703 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001704 func ScrollUp()
1705 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1706 endfunc
1707 func ScrollDown()
1708 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1709 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001710 func ClickTop()
1711 call feedkeys("\<F4>\<LeftMouse>", "xt")
1712 endfunc
1713 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001714 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001715 call feedkeys("\<F5>\<LeftMouse>", "xt")
1716 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001717 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001718 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1719 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001720 END
1721 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001722 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001723 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1724
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001725 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001726 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001727 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1728
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001729 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001730 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001731 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1732
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001733 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001734 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001735 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1736
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02001737 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001738 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001739 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1740 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1741
1742 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1743 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1744
1745 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001746 " wait a bit, otherwise it fails sometimes (double click recognized?)
1747 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001748 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1749 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1750
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001751 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1752 sleep 100m
1753 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1754 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1755
1756 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1757 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1758
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001759 " remove the minwidth and maxheight
1760 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
1761 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
1762
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001763 " clean up
1764 call StopVimInTerminal(buf)
1765 call delete('XtestPopupScroll')
1766endfunc
1767
Bram Moolenaar437a7462019-07-05 20:17:22 +02001768func Test_popup_fitting_scrollbar()
1769 " this was causing a crash, divide by zero
1770 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001771 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001772 \ scrollbar: 1,
1773 \ maxwidth: 10,
1774 \ maxheight: 5,
1775 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001776 redraw
1777 call popup_clear()
1778endfunc
1779
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001780func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001781 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001782
1783 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001784 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001785 let p = popup_create('test', opts)
1786 call popup_settext(p, 'this is a text')
1787 END
1788
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001789 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001790 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001791 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1792
1793 " Setting to empty string clears it
1794 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1795 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1796
1797 " Setting a list
1798 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1799 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1800
1801 " Shrinking with a list
1802 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1803 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1804
1805 " Growing with a list
1806 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1807 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1808
1809 " Empty list clears
1810 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1811 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1812
1813 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001814 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001815 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1816
1817 " clean up
1818 call StopVimInTerminal(buf)
1819 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001820endfunc
1821
1822func Test_popup_hidden()
1823 new
1824
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001825 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001826 redraw
1827 call assert_equal(0, popup_getpos(winid).visible)
1828 call popup_close(winid)
1829
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001830 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001831 redraw
1832 call assert_equal(0, popup_getpos(winid).visible)
1833 call popup_close(winid)
1834
1835 func QuitCallback(id, res)
1836 let s:cb_winid = a:id
1837 let s:cb_res = a:res
1838 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001839 let winid = popup_dialog('make a choice', #{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001840 \ filter: 'popup_filter_yesno',
1841 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001842 \ })
1843 redraw
1844 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001845 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1846 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001847 exe "normal anot used by filter\<Esc>"
1848 call assert_equal('not used by filter', getline(1))
1849
1850 call popup_show(winid)
1851 call feedkeys('y', "xt")
1852 call assert_equal(1, s:cb_res)
1853
1854 bwipe!
1855 delfunc QuitCallback
1856endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001857
1858" Test options not checked elsewhere
1859func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001860 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001861 let options = popup_getoptions(winid)
1862 call assert_equal(1, options.wrap)
1863 call assert_equal(0, options.drag)
1864 call assert_equal('Beautiful', options.highlight)
1865
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001866 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001867 let options = popup_getoptions(winid)
1868 call assert_equal(0, options.wrap)
1869 call assert_equal(1, options.drag)
1870 call assert_equal('Another', options.highlight)
1871
1872 call popup_close(winid)
1873endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001874
1875func Test_popupwin_garbage_collect()
1876 func MyPopupFilter(x, winid, c)
1877 " NOP
1878 endfunc
1879
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001880 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001881 call test_garbagecollect_now()
1882 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001883 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001884 call feedkeys('j', 'xt')
1885 call assert_true(v:true)
1886
1887 call popup_close(winid)
1888 delfunc MyPopupFilter
1889endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001890
1891func Test_popupwin_with_buffer()
1892 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1893 let buf = bufadd('XsomeFile')
1894 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02001895
1896 setlocal number
1897 call setbufvar(buf, "&wrapmargin", 13)
1898
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001899 let winid = popup_create(buf, {})
1900 call assert_notequal(0, winid)
1901 let pos = popup_getpos(winid)
1902 call assert_equal(2, pos.height)
1903 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02001904
1905 " window-local option is set to default, buffer-local is not
1906 call assert_equal(0, getwinvar(winid, '&number'))
1907 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
1908
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001909 call popup_close(winid)
1910 call assert_equal({}, popup_getpos(winid))
1911 call assert_equal(1, bufloaded(buf))
1912 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02001913 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02001914
1915 edit test_popupwin.vim
1916 let winid = popup_create(bufnr(''), {})
1917 redraw
1918 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001919 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001920endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02001921
1922func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001923 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001924 \ maxwidth: 40,
1925 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02001926 \ })
1927 for top in range(1, 20)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001928 call popup_setoptions(winid, #{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02001929 redraw
1930 call assert_equal(19, popup_getpos(winid).width)
1931 endfor
1932 call popup_clear()
1933endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02001934
1935func Test_popupwin_buf_close()
1936 let buf = bufadd('Xtestbuf')
1937 call bufload(buf)
1938 call setbufline(buf, 1, ['just', 'some', 'lines'])
1939 let winid = popup_create(buf, {})
1940 redraw
1941 call assert_equal(3, popup_getpos(winid).height)
1942 let bufinfo = getbufinfo(buf)[0]
1943 call assert_equal(1, bufinfo.changed)
1944 call assert_equal(0, bufinfo.hidden)
1945 call assert_equal(0, bufinfo.listed)
1946 call assert_equal(1, bufinfo.loaded)
1947 call assert_equal([], bufinfo.windows)
1948 call assert_equal([winid], bufinfo.popups)
1949
1950 call popup_close(winid)
1951 call assert_equal({}, popup_getpos(winid))
1952 let bufinfo = getbufinfo(buf)[0]
1953 call assert_equal(1, bufinfo.changed)
1954 call assert_equal(1, bufinfo.hidden)
1955 call assert_equal(0, bufinfo.listed)
1956 call assert_equal(1, bufinfo.loaded)
1957 call assert_equal([], bufinfo.windows)
1958 call assert_equal([], bufinfo.popups)
1959 exe 'bwipe! ' .. buf
1960endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02001961
1962func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001963 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02001964
1965 let lines =<< trim END
1966 call setline(1, range(1, 10))
1967 hi ScrollThumb ctermbg=blue
1968 hi ScrollBar ctermbg=red
1969 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001970 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001971 \ maxwidth: 10,
1972 \ maxheight: 3,
1973 \ pos : 'topleft',
1974 \ col : a:col,
1975 \ line : a:line,
1976 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02001977 \ })
1978 endfunc
1979 call PopupMenu(['x'], 1, 1)
1980 call PopupMenu(['123456789|'], 1, 16)
1981 call PopupMenu(['123456789|' .. ' '], 7, 1)
1982 call PopupMenu([repeat('123456789|', 100)], 7, 16)
1983 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
1984 END
1985 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001986 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02001987 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
1988
1989 " close the menu popupwin.
1990 call term_sendkeys(buf, " ")
1991 call term_sendkeys(buf, " ")
1992 call term_sendkeys(buf, " ")
1993 call term_sendkeys(buf, " ")
1994 call term_sendkeys(buf, " ")
1995
1996 " clean up
1997 call StopVimInTerminal(buf)
1998 call delete('XtestPopupMenuMaxWidth')
1999endfunc
2000
Bram Moolenaara901a372019-07-13 16:38:50 +02002001func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002002 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002003
2004 let lines =<< trim END
2005 call setline(1, range(1, 20))
2006 hi ScrollThumb ctermbg=blue
2007 hi ScrollBar ctermbg=red
2008 call popup_menu(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002009 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002010 \ minwidth: 8,
2011 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002012 \ })
2013 END
2014 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002015 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002016
2017 call term_sendkeys(buf, "j")
2018 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2019
2020 call term_sendkeys(buf, "jjj")
2021 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2022
2023 " if the cursor is the bottom line, it stays at the bottom line.
2024 call term_sendkeys(buf, repeat("j", 20))
2025 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2026
2027 call term_sendkeys(buf, "kk")
2028 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2029
2030 call term_sendkeys(buf, "k")
2031 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2032
2033 " if the cursor is in the top line, it stays in the top line.
2034 call term_sendkeys(buf, repeat("k", 20))
2035 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2036
2037 " close the menu popupwin.
2038 call term_sendkeys(buf, " ")
2039
2040 " clean up
2041 call StopVimInTerminal(buf)
2042 call delete('XtestPopupMenuScroll')
2043endfunc
2044
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002045func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002046 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002047
2048 let lines =<< trim END
2049 function! MyFilter(winid, key) abort
2050 if a:key == "0"
2051 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2052 return 1
2053 endif
2054 if a:key == "G"
2055 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2056 return 1
2057 endif
2058 if a:key == "j"
2059 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2060 return 1
2061 endif
2062 if a:key == "k"
2063 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2064 return 1
2065 endif
2066 if a:key == 'x'
2067 call popup_close(a:winid)
2068 return 1
2069 endif
2070 return 0
2071 endfunction
2072 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2073 \ maxheight : 3,
2074 \ filter : 'MyFilter'
2075 \ })
2076 END
2077 call writefile(lines, 'XtestPopupMenuFilter')
2078 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2079
2080 call term_sendkeys(buf, "j")
2081 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2082
2083 call term_sendkeys(buf, "k")
2084 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2085
2086 call term_sendkeys(buf, "G")
2087 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2088
2089 call term_sendkeys(buf, "0")
2090 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2091
2092 call term_sendkeys(buf, "x")
2093
2094 " clean up
2095 call StopVimInTerminal(buf)
2096 call delete('XtestPopupMenuFilter')
2097endfunc
2098
2099func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002100 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002101
2102 let winid = popup_create('some text', {})
2103 call assert_equal(0, popup_getoptions(winid).cursorline)
2104 call popup_close(winid)
2105
2106 let winid = popup_create('some text', #{ cursorline: 1, })
2107 call assert_equal(1, popup_getoptions(winid).cursorline)
2108 call popup_close(winid)
2109
2110 let winid = popup_create('some text', #{ cursorline: 0, })
2111 call assert_equal(0, popup_getoptions(winid).cursorline)
2112 call popup_close(winid)
2113
2114 let winid = popup_menu('some text', {})
2115 call assert_equal(1, popup_getoptions(winid).cursorline)
2116 call popup_close(winid)
2117
2118 let winid = popup_menu('some text', #{ cursorline: 1, })
2119 call assert_equal(1, popup_getoptions(winid).cursorline)
2120 call popup_close(winid)
2121
2122 let winid = popup_menu('some text', #{ cursorline: 0, })
2123 call assert_equal(0, popup_getoptions(winid).cursorline)
2124 call popup_close(winid)
2125
2126 " ---------
2127 " Pattern 1
2128 " ---------
2129 let lines =<< trim END
2130 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2131 END
2132 call writefile(lines, 'XtestPopupCursorLine')
2133 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2134 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2135 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2136 call StopVimInTerminal(buf)
2137
2138 " ---------
2139 " Pattern 2
2140 " ---------
2141 let lines =<< trim END
2142 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2143 END
2144 call writefile(lines, 'XtestPopupCursorLine')
2145 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2146 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2147 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2148 call StopVimInTerminal(buf)
2149
2150 " ---------
2151 " Pattern 3
2152 " ---------
2153 let lines =<< trim END
2154 function! MyFilter(winid, key) abort
2155 if a:key == "j"
2156 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2157 return 1
2158 endif
2159 if a:key == 'x'
2160 call popup_close(a:winid)
2161 return 1
2162 endif
2163 return 0
2164 endfunction
2165 call popup_menu(['111', '222', '333'], #{
2166 \ cursorline : 0,
2167 \ maxheight : 2,
2168 \ filter : 'MyFilter',
2169 \ })
2170 END
2171 call writefile(lines, 'XtestPopupCursorLine')
2172 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2173 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2174 call term_sendkeys(buf, "j")
2175 call term_sendkeys(buf, "j")
2176 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2177 call term_sendkeys(buf, "x")
2178 call StopVimInTerminal(buf)
2179
2180 " ---------
2181 " Pattern 4
2182 " ---------
2183 let lines =<< trim END
2184 function! MyFilter(winid, key) abort
2185 if a:key == "j"
2186 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2187 return 1
2188 endif
2189 if a:key == 'x'
2190 call popup_close(a:winid)
2191 return 1
2192 endif
2193 return 0
2194 endfunction
2195 call popup_menu(['111', '222', '333'], #{
2196 \ cursorline : 1,
2197 \ maxheight : 2,
2198 \ filter : 'MyFilter',
2199 \ })
2200 END
2201 call writefile(lines, 'XtestPopupCursorLine')
2202 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2203 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2204 call term_sendkeys(buf, "j")
2205 call term_sendkeys(buf, "j")
2206 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2207 call term_sendkeys(buf, "x")
2208 call StopVimInTerminal(buf)
2209
2210 call delete('XtestPopupCursorLine')
2211endfunc
2212
Bram Moolenaarf914a332019-07-20 15:09:56 +02002213func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002214 CheckScreendump
2215
Bram Moolenaarf914a332019-07-20 15:09:56 +02002216 call writefile([
2217 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2218 \ "another\tXtagfile\t/^this is another",
2219 \ "theword\tXtagfile\t/^theword"],
2220 \ 'Xtags')
2221 call writefile(range(1,20)
2222 \ + ['theword is here']
2223 \ + range(22, 27)
2224 \ + ['this is another place']
2225 \ + range(29, 40),
2226 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002227 call writefile(range(1,10)
2228 \ + ['searched word is here']
2229 \ + range(12, 20),
2230 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002231 let lines =<< trim END
2232 set tags=Xtags
2233 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002234 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002235 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002236 \ 'three',
2237 \ 'four',
2238 \ 'five',
2239 \ 'six',
2240 \ 'seven',
2241 \ 'find theword somewhere',
2242 \ 'nine',
2243 \ 'this is another word',
2244 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002245 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002246 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002247 END
2248 call writefile(lines, 'XtestPreviewPopup')
2249 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2250
2251 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2252 call term_sendkeys(buf, ":\<CR>")
2253 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2254
2255 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2256 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2257
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002258 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002259 call term_sendkeys(buf, ":\<CR>")
2260 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2261
2262 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2263 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2264
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002265 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2266 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002267 call term_sendkeys(buf, ":cd testdir\<CR>")
2268
2269 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002270 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002271 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2272
2273 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2274 call term_sendkeys(buf, ":\<CR>")
2275 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2276
2277 call term_sendkeys(buf, ":pclose\<CR>")
2278 call term_sendkeys(buf, ":psearch searched\<CR>")
2279 call term_sendkeys(buf, ":\<CR>")
2280 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002281
Bram Moolenaarf914a332019-07-20 15:09:56 +02002282 call StopVimInTerminal(buf)
2283 call delete('Xtags')
2284 call delete('Xtagfile')
2285 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002286 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002287endfunc
2288
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002289func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002290 let lines =<< trim END
2291 set completeopt+=preview,popup
2292 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002293 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002294
2295 func CompleteFuncDict(findstart, base)
2296 if a:findstart
2297 if col('.') > 10
2298 return col('.') - 10
2299 endif
2300 return 0
2301 endif
2302
2303 return {
2304 \ 'words': [
2305 \ {
2306 \ 'word': 'aword',
2307 \ 'abbr': 'wrd',
2308 \ 'menu': 'extra text',
2309 \ 'info': 'words are cool',
2310 \ 'kind': 'W',
2311 \ 'user_data': 'test'
2312 \ },
2313 \ {
2314 \ 'word': 'anotherword',
2315 \ 'abbr': 'anotwrd',
2316 \ 'menu': 'extra text',
2317 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2318 \ 'kind': 'W',
2319 \ 'user_data': 'notest'
2320 \ },
2321 \ {
2322 \ 'word': 'noinfo',
2323 \ 'abbr': 'noawrd',
2324 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002325 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002326 \ 'kind': 'W',
2327 \ 'user_data': 'notest'
2328 \ },
2329 \ {
2330 \ 'word': 'thatword',
2331 \ 'abbr': 'thatwrd',
2332 \ 'menu': 'extra text',
2333 \ 'info': 'that word is cool',
2334 \ 'kind': 'W',
2335 \ 'user_data': 'notest'
2336 \ },
2337 \ ]
2338 \ }
2339 endfunc
2340 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002341 func ChangeColor()
2342 let id = popup_findinfo()
2343 call popup_setoptions(id, #{highlight: 'InfoPopup'})
2344 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002345 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002346 return lines
2347endfunc
2348
2349func Test_popupmenu_info_border()
2350 CheckScreendump
2351
2352 let lines = Get_popupmenu_lines()
2353 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002354 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002355
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002356 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2357 call term_wait(buf, 50)
2358
2359 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2360 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2361
2362 call term_sendkeys(buf, "\<C-N>")
2363 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2364
2365 call term_sendkeys(buf, "\<C-N>")
2366 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2367
2368 call term_sendkeys(buf, "\<C-N>\<C-N>")
2369 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2370
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002371 " info on the left with scrollbar
2372 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2373 call term_sendkeys(buf, "\<C-N>\<C-N>")
2374 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2375
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002376 call StopVimInTerminal(buf)
2377 call delete('XtestInfoPopup')
2378endfunc
2379
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002380func Test_popupmenu_info_noborder()
2381 CheckScreendump
2382
2383 let lines = Get_popupmenu_lines()
2384 call add(lines, 'set completepopup=height:4,border:off')
2385 call writefile(lines, 'XtestInfoPopupNb')
2386
2387 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2388 call term_wait(buf, 50)
2389
2390 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2391 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2392
2393 call StopVimInTerminal(buf)
2394 call delete('XtestInfoPopupNb')
2395endfunc
2396
Bram Moolenaar258cef52019-08-21 17:29:29 +02002397func Test_popupmenu_info_align_menu()
2398 CheckScreendump
2399
2400 let lines = Get_popupmenu_lines()
2401 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2402 call writefile(lines, 'XtestInfoPopupNb')
2403
2404 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2405 call term_wait(buf, 50)
2406
2407 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2408 call term_sendkeys(buf, "\<C-N>")
2409 call term_sendkeys(buf, "\<C-N>")
2410 call term_sendkeys(buf, "\<C-N>")
2411 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2412
2413 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2414 call term_sendkeys(buf, "\<C-N>")
2415 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2416
2417 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002418 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002419 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2420 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2421 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2422
2423 call StopVimInTerminal(buf)
2424 call delete('XtestInfoPopupNb')
2425endfunc
2426
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002427func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02002428 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002429 call popup_clear()
2430 let winid = popup_notification('nothing wrong', {})
2431 call assert_equal(bufnr, winbufnr(winid))
2432 call popup_clear()
2433endfunc
2434
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002435" vim: shiftwidth=2 sts=2