blob: d7130863c5cc74ef1b763d2acff183f198e84d89 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature textprop
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
15 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020016 call prop_type_add('comment', #{highlight: 'Comment'})
17 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
18 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020019 call setwinvar(winid2, '&wincolor', 'PopupColor2')
20 END
21 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020022 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020023 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
24
Bram Moolenaarec583842019-05-26 14:11:23 +020025 " Add a tabpage
26 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020027 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020028 \ .. "#{text: 'other tab'},"
29 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020030 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020031 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020032 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020033 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
34
35 " switch back to first tabpage
36 call term_sendkeys(buf, "gt")
37 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
38
39 " close that tabpage
40 call term_sendkeys(buf, ":quit!\<CR>")
41 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
42
Bram Moolenaar202d9822019-06-11 21:56:30 +020043 " set 'columns' to a small value, size must be recomputed
44 call term_sendkeys(buf, ":let cols = &columns\<CR>")
45 call term_sendkeys(buf, ":set columns=12\<CR>")
46 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
47 call term_sendkeys(buf, ":let &columns = cols\<CR>")
48
Bram Moolenaar17146962019-05-30 00:12:11 +020049 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020050 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020051 call term_sendkeys(buf, ":redraw\<CR>")
52 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
53
Bram Moolenaar17146962019-05-30 00:12:11 +020054 " show not fitting line at bottom
55 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
56 call term_sendkeys(buf, ":redraw\<CR>")
57 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
58
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020059 " move popup over ruler
60 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020061 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020062 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020063 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
64
65 " clear all popups after moving the cursor a bit, so that ruler is updated
66 call term_sendkeys(buf, "axxx\<Esc>")
67 call term_wait(buf)
68 call term_sendkeys(buf, "0")
69 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020070 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020071 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
72
Bram Moolenaar4d784b22019-05-25 19:51:39 +020073 " clean up
74 call StopVimInTerminal(buf)
75 call delete('XtestPopup')
76endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020077
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020078func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020079 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020080
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020081 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020082 let lines =<< trim END
83 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020084 call popup_create('hello border', #{line: 2, col: 3, border: []})
85 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020086 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020087 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
88 call popup_create('paddings', #{line: 6, col: 23, padding: [1, 3, 2, 4]})
89 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
90 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020091 END
92 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
93 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020094 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020095 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
96
97 call StopVimInTerminal(buf)
98 call delete('XtestPopupBorder')
99 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200100
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200101 let lines =<< trim END
102 call setline(1, range(1, 100))
103 hi BlueColor ctermbg=lightblue
104 hi TopColor ctermbg=253
105 hi RightColor ctermbg=245
106 hi BottomColor ctermbg=240
107 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200108 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
109 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
110 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
111 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
112 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
113 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200114 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200115 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200116 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200117 END
118 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200119 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200120 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
121
Bram Moolenaarad24a712019-06-17 20:05:45 +0200122 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200123 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200124 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
125
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200126 " check multi-byte border only with 'ambiwidth' single
127 if &ambiwidth == 'single'
128 call term_sendkeys(buf, ":call MultiByte()\<CR>")
129 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
130 endif
131
Bram Moolenaar790498b2019-06-01 22:15:29 +0200132 call StopVimInTerminal(buf)
133 call delete('XtestPopupBorder')
134
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200135 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200136 \ line: 2,
137 \ core_line: 3,
138 \ col: 3,
139 \ core_col: 4,
140 \ width: 14,
141 \ core_width: 12,
142 \ height: 3,
143 \ core_height: 1,
144 \ firstline: 1,
145 \ scrollbar: 0,
146 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200147 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200148 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200149 let options = popup_getoptions(winid)
150 call assert_equal([], options.border)
151 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200152
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200153 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200154 let with_border_or_padding.width = 15
155 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200156 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200157 let options = popup_getoptions(winid)
158 call assert_false(has_key(options, "border"))
159 call assert_equal([], options.padding)
160
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200161 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200162 \ padding: [1, 2, 3, 4],
163 \ border: [4, 0, 7, 8],
164 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
165 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200166 \ })
167 let options = popup_getoptions(winid)
168 call assert_equal([1, 0, 1, 1], options.border)
169 call assert_equal([1, 2, 3, 4], options.padding)
170 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
171 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200172
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200173 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
174 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200175 \ line: 3,
176 \ core_line: 5,
177 \ col: 8,
178 \ core_col: 10,
179 \ width: 14,
180 \ core_width: 10,
181 \ height: 5,
182 \ scrollbar: 0,
183 \ core_height: 1,
184 \ firstline: 1,
185 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200186
187 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200188endfunc
189
Bram Moolenaarb4230122019-05-30 18:40:53 +0200190func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200191 CheckScreendump
192
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200193 let lines =<< trim END
194 call setline(1, range(1, 100))
195 hi PopupColor ctermbg=lightblue
196 let winid = popup_create([
197 \ '#include <stdio.h>',
198 \ 'int main(void)',
199 \ '{',
200 \ ' printf(123);',
201 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200202 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200203 call win_execute(winid, 'set syntax=cpp')
204 END
205 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200206 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200207 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
208
209 " clean up
210 call StopVimInTerminal(buf)
211 call delete('XtestPopup')
212endfunc
213
214func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200215 CheckScreendump
216
Bram Moolenaar402502d2019-05-30 22:07:36 +0200217 let lines =<< trim END
218 call setline(1, range(1, 100))
219 hi PopupColor ctermbg=lightgrey
220 let winid = popup_create([
221 \ '#include <stdio.h>',
222 \ 'int main(void)',
223 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200224 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200225 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200226 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200227 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
228 END
229 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200230 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200231 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
232
233 " clean up
234 call StopVimInTerminal(buf)
235 call delete('XtestPopup')
236endfunc
237
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200238func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200239 CheckScreendump
240
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200241 let lines =<< trim END
242 call setline(1, ['111 222 333', '444 555 666'])
243 let winid = popup_create([
244 \ '111 222 333',
245 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200246 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200247 set hlsearch
248 /666
249 call matchadd('ErrorMsg', '111')
250 call matchadd('ErrorMsg', '444')
251 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
252 call win_execute(winid, "call matchadd('ErrorMsg', '555')")
253 END
254 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200255 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200256 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
257
258 " clean up
259 call StopVimInTerminal(buf)
260 call delete('XtestPopupMatches')
261endfunc
262
Bram Moolenaar399d8982019-06-02 15:34:29 +0200263func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200264 CheckScreendump
265
Bram Moolenaar399d8982019-06-02 15:34:29 +0200266 let lines =<< trim END
267 call setline(1, repeat([repeat('-', 60)], 15))
268 set so=0
269 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200270 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200271 \ line: 'cursor+1',
272 \ col: 'cursor',
273 \ pos: 'topleft',
274 \ border: [],
275 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200276 \ })
277 normal 25|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200278 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200279 \ line: 'cursor+1',
280 \ col: 'cursor',
281 \ pos: 'topright',
282 \ border: [],
283 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200284 \ })
285 normal 9G29|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200286 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200287 \ line: 'cursor-1',
288 \ col: 'cursor',
289 \ pos: 'botleft',
290 \ border: [],
291 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200292 \ })
293 normal 51|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200294 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200295 \ line: 'cursor-1',
296 \ col: 'cursor',
297 \ pos: 'botright',
298 \ border: [],
299 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200300 \ })
301 END
302 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200303 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200304 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
305
306 " clean up
307 call StopVimInTerminal(buf)
308 call delete('XtestPopupCorners')
309endfunc
310
Bram Moolenaar8d241042019-06-12 23:40:01 +0200311func Test_popup_firstline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200312 CheckScreendump
313
Bram Moolenaar8d241042019-06-12 23:40:01 +0200314 let lines =<< trim END
315 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200316 call popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200317 \ maxheight: 4,
318 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200319 \ })
320 END
321 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200322 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200323 call VerifyScreenDump(buf, 'Test_popupwin_firstline', {})
324
325 " clean up
326 call StopVimInTerminal(buf)
327 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200328
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200329 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200330 \ maxheight: 2,
331 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200332 \ })
333 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200334 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200335 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200336 call popup_close(winid)
337
338 let winid = popup_create(['xxx']->repeat(50), #{
339 \ maxheight: 3,
340 \ firstline: 11,
341 \ })
342 redraw
343 call assert_equal(11, popup_getoptions(winid).firstline)
344 call assert_equal(11, popup_getpos(winid).firstline)
345
346 " Normal command changes what is displayed but not "firstline"
347 call win_execute(winid, "normal! \<c-y>")
348 call assert_equal(11, popup_getoptions(winid).firstline)
349 call assert_equal(10, popup_getpos(winid).firstline)
350
351 " Making some property change applies "firstline" again
352 call popup_setoptions(winid, #{line: 4})
353 call assert_equal(11, popup_getoptions(winid).firstline)
354 call assert_equal(11, popup_getpos(winid).firstline)
355
356 " Remove "firstline" property and scroll
357 call popup_setoptions(winid, #{firstline: 0})
358 call win_execute(winid, "normal! \<c-y>")
359 call assert_equal(0, popup_getoptions(winid).firstline)
360 call assert_equal(10, popup_getpos(winid).firstline)
361
362 " Making some property change has no side effect
363 call popup_setoptions(winid, #{line: 3})
364 call assert_equal(0, popup_getoptions(winid).firstline)
365 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaarae943152019-06-16 22:54:14 +0200366
367 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200368endfunc
369
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200370func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200371 CheckScreendump
372
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200373 " create a popup that covers the command line
374 let lines =<< trim END
375 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200376 split
377 vsplit
378 $wincmd w
379 vsplit
380 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200381 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200382 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200383 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200384 \ border: [],
385 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200386 \ })
387 func Dragit()
388 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
389 endfunc
390 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200391 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200392 func Resize()
393 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
394 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200395 map <silent> <F5> :call test_setmouse(6, 21)<CR>
396 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200397 END
398 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200399 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200400 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
401
402 call term_sendkeys(buf, ":call Dragit()\<CR>")
403 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
404
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200405 call term_sendkeys(buf, ":call Resize()\<CR>")
406 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
407
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200408 " clean up
409 call StopVimInTerminal(buf)
410 call delete('XtestPopupDrag')
411endfunc
412
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200413func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200414 CheckScreendump
415
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200416 let lines =<< trim END
417 call setline(1, range(1, 20))
418 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200419 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200420 \ close: 'button',
421 \ border: [],
422 \ line: 1,
423 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200424 \ })
425 func CloseMsg(id, result)
426 echomsg 'Popup closed with ' .. a:result
427 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200428 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200429 \ close: 'click',
430 \ line: 3,
431 \ col: 15,
432 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200433 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200434 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200435 \ close: 'button',
436 \ line: 5,
437 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200438 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200439 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200440 \ close: 'button',
441 \ padding: [],
442 \ line: 5,
443 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200444 \ })
445 func CloseWithX()
446 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
447 endfunc
448 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
449 func CloseWithClick()
450 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
451 endfunc
452 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200453 func CreateWithMenuFilter()
454 let winid = popup_create('barfoo', #{
455 \ close: 'button',
456 \ filter: 'popup_filter_menu',
457 \ border: [],
458 \ line: 1,
459 \ col: 40,
460 \ })
461 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200462 END
463 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200464 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200465 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
466
467 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
468 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
469
470 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
471 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
472
Bram Moolenaarf6396232019-08-24 19:36:00 +0200473 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
474 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
475
476 " We have to send the actual mouse code, feedkeys() would be caught the
477 " filter.
478 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
479 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
480
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200481 " clean up
482 call StopVimInTerminal(buf)
483 call delete('XtestPopupClose')
484endfunction
485
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200486func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200487 CheckScreendump
488
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200489 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200490 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200491 hi PopupColor ctermbg=lightgrey
492 let winid = popup_create([
493 \ 'some text',
494 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200495 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200496 \ line: 1,
497 \ col: 10,
498 \ wrap: 0,
499 \ fixed: 1,
500 \ zindex: 90,
501 \ padding: [],
502 \ highlight: 'PopupColor',
503 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200504 call popup_create([
505 \ 'xxxxxxxxx',
506 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200507 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200508 \ line: 3,
509 \ col: 18,
510 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200511 let winidb = popup_create([
512 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200513 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200514 \ line: 7,
515 \ col: 10,
516 \ wrap: 0,
517 \ fixed: 1,
518 \ close: 'button',
519 \ zindex: 90,
520 \ padding: [],
521 \ border: [],
522 \ 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 +0200523 END
524 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200525 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200526 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
527
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200528 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
529 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200530 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200531 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
532
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200533 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
534 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200535 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200536 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
537
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200538 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
539 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200540 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200541 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
542
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200543 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
544 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200545 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200546 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
547
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200548 " clean up
549 call StopVimInTerminal(buf)
550 call delete('XtestPopupMask')
551endfunc
552
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200553func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200554 CheckScreendump
555 CheckFeature clipboard_working
556
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200557 " create a popup with some text to be selected
558 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200559 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200560 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200561 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200562 \ drag: 1,
563 \ border: [],
564 \ line: 3,
565 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200566 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200567 \ })
568 func Select1()
569 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
570 endfunc
571 map <silent> <F3> :call test_setmouse(4, 15)<CR>
572 map <silent> <F4> :call test_setmouse(6, 23)<CR>
573 END
574 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200575 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200576 call term_sendkeys(buf, ":call Select1()\<CR>")
577 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
578
579 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
580 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200581 " clean the command line, sometimes it still shows a command
582 call term_sendkeys(buf, ":\<esc>")
583
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200584 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
585
586 " clean up
587 call StopVimInTerminal(buf)
588 call delete('XtestPopupSelect')
589endfunc
590
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200591func Test_popup_in_tab()
592 " default popup is local to tab, not visible when in other tab
593 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200594 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200595 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200596 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200597 tabnew
598 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200599 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200600 quit
601 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200602
603 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200604 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200605 " buffer is gone now
606 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200607
608 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200609 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200610 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200611 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200612 tabnew
613 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200614 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200615 quit
616 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200617 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200618
619 " create popup in other tab
620 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200621 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200622 call assert_equal(0, popup_getpos(winid).visible)
623 call assert_equal(1, popup_getoptions(winid).tabpage)
624 quit
625 call assert_equal(1, popup_getpos(winid).visible)
626 call assert_equal(0, popup_getoptions(winid).tabpage)
627 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200628endfunc
629
630func Test_popup_valid_arguments()
631 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200632 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200633 let pos = popup_getpos(winid)
634 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200635 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200636
637 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200638 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200639 let pos = popup_getpos(winid)
640 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200641 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200642
643 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200644 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200645 let pos = popup_getpos(winid)
646 let around = (&columns - pos.width) / 2
647 call assert_inrange(around - 1, around + 1, pos.col)
648 let around = (&lines - pos.height) / 2
649 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200650 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200651endfunc
652
653func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200654 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200655 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200656 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200657 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200658
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200659 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200660 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200661 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200662 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200663 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200664 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200665 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200666 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200667
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200668 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200669 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200670 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200671 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200672 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200673 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200674 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200675 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200676
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200677 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200678 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200679 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200680 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200681 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
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", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200684 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200685 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
686 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200687 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
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: "text"}, 666], {})', 'E715:')
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: "text", props: "none"}], {})', 'E714:')
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: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200695 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200696 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
697 call popup_clear()
698 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200699 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200700 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200701endfunc
702
Bram Moolenaareea16992019-05-31 17:34:48 +0200703func Test_win_execute_closing_curwin()
704 split
705 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200706 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200707 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200708endfunc
709
710func Test_win_execute_not_allowed()
711 let winid = popup_create('some text', {})
712 call assert_fails('call win_execute(winid, "split")', 'E994:')
713 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
714 call assert_fails('call win_execute(winid, "close")', 'E994:')
715 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200716 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200717 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
718 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
719 call assert_fails('call win_execute(winid, "next")', 'E994:')
720 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
721 call assert_fails('call win_execute(winid, "buf")', 'E994:')
722 call assert_fails('call win_execute(winid, "edit")', 'E994:')
723 call assert_fails('call win_execute(winid, "enew")', 'E994:')
724 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
725 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
726 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
727 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200728 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200729endfunc
730
Bram Moolenaar402502d2019-05-30 22:07:36 +0200731func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200732 CheckScreendump
733
Bram Moolenaar402502d2019-05-30 22:07:36 +0200734 let lines =<< trim END
735 call setline(1, range(1, 100))
736 let winid = popup_create(
737 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200738 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200739 END
740 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200741 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200742 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
743
744 " clean up
745 call StopVimInTerminal(buf)
746 call delete('XtestPopup')
747endfunc
748
749func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200750 CheckScreendump
751
Bram Moolenaar402502d2019-05-30 22:07:36 +0200752 let lines =<< trim END
753 call setline(1, range(1, 100))
754 let winid = popup_create(
755 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200756 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200757 END
758 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200759 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200760 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
761
762 " clean up
763 call StopVimInTerminal(buf)
764 call delete('XtestPopup')
765endfunc
766
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200767func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200768 CheckScreendump
769
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200770 let lines =<< trim END
771 set showbreak=>>\
772 call setline(1, range(1, 20))
773 let winid = popup_dialog(
774 \ 'a long line here',
775 \ #{filter: 'popup_filter_yesno'})
776 END
777 call writefile(lines, 'XtestPopupShowbreak')
778 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
779 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
780
781 " clean up
782 call term_sendkeys(buf, "y")
783 call StopVimInTerminal(buf)
784 call delete('XtestPopupShowbreak')
785endfunc
786
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200787func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200788 CheckFeature timers
789
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200790 topleft vnew
791 call setline(1, 'hello')
792
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200793 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200794 \ line: 1,
795 \ col: 1,
796 \ minwidth: 20,
797 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200798 \})
799 redraw
800 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
801 call assert_equal('world', line)
802
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200803 call assert_equal(winid, popup_locate(1, 1))
804 call assert_equal(winid, popup_locate(1, 20))
805 call assert_equal(0, popup_locate(1, 21))
806 call assert_equal(0, popup_locate(2, 1))
807
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200808 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200809 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200810 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
811 call assert_equal('hello', line)
812
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200813 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200814 \ line: &lines,
815 \ col: 10,
816 \ minwidth: 20,
817 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200818 \})
819 redraw
820 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
821 call assert_match('.*on the command line.*', line)
822
823 sleep 700m
824 redraw
825 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
826 call assert_notmatch('.*on the command line.*', line)
827
828 bwipe!
829endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200830
831func Test_popup_hide()
832 topleft vnew
833 call setline(1, 'hello')
834
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200835 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200836 \ line: 1,
837 \ col: 1,
838 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200839 \})
840 redraw
841 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
842 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200843 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200844 " buffer is still listed and active
845 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200846
847 call popup_hide(winid)
848 redraw
849 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
850 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200851 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200852 " buffer is still listed but hidden
853 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200854
855 call popup_show(winid)
856 redraw
857 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
858 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200859 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200860
861
862 call popup_close(winid)
863 redraw
864 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
865 call assert_equal('hello', line)
866
867 " error is given for existing non-popup window
868 call assert_fails('call popup_hide(win_getid())', 'E993:')
869
870 " no error non-existing window
871 call popup_hide(1234234)
872 call popup_show(41234234)
873
874 bwipe!
875endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200876
877func Test_popup_move()
878 topleft vnew
879 call setline(1, 'hello')
880
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200881 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200882 \ line: 1,
883 \ col: 1,
884 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200885 \})
886 redraw
887 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
888 call assert_equal('world ', line)
889
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200890 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200891 redraw
892 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
893 call assert_equal('hello ', line)
894 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
895 call assert_equal('~world', line)
896
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200897 call popup_move(winid, #{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200898 redraw
899 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
900 call assert_equal('hworld', line)
901
902 call popup_close(winid)
903
904 bwipe!
905endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200906
Bram Moolenaar402502d2019-05-30 22:07:36 +0200907func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200908 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200909 \ line: 2,
910 \ col: 3,
911 \ minwidth: 10,
912 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200913 \})
914 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200915 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200916 call assert_equal(2, res.line)
917 call assert_equal(3, res.col)
918 call assert_equal(10, res.width)
919 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200920 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200921
922 call popup_close(winid)
923endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200924
925func Test_popup_width_longest()
926 let tests = [
927 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
928 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
929 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
930 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
931 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
932 \ ]
933
934 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200935 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200936 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200937 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200938 call assert_equal(test[1], position.width)
939 call popup_close(winid)
940 endfor
941endfunc
942
943func Test_popup_wraps()
944 let tests = [
945 \ ['nowrap', 6, 1],
946 \ ['a line that wraps once', 12, 2],
947 \ ['a line that wraps two times', 12, 3],
948 \ ]
949 for test in tests
950 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200951 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200952 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200953 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200954 call assert_equal(test[1], position.width)
955 call assert_equal(test[2], position.height)
956
957 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200958 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200959 endfor
960endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200961
962func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200963 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200964 \ line: 2,
965 \ col: 3,
966 \ minwidth: 10,
967 \ minheight: 11,
968 \ maxwidth: 20,
969 \ maxheight: 21,
970 \ zindex: 100,
971 \ time: 5000,
972 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200973 \})
974 redraw
975 let res = popup_getoptions(winid)
976 call assert_equal(2, res.line)
977 call assert_equal(3, res.col)
978 call assert_equal(10, res.minwidth)
979 call assert_equal(11, res.minheight)
980 call assert_equal(20, res.maxwidth)
981 call assert_equal(21, res.maxheight)
982 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200983 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +0200984 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200985 if has('timers')
986 call assert_equal(5000, res.time)
987 endif
988 call popup_close(winid)
989
990 let winid = popup_create('hello', {})
991 redraw
992 let res = popup_getoptions(winid)
993 call assert_equal(0, res.line)
994 call assert_equal(0, res.col)
995 call assert_equal(0, res.minwidth)
996 call assert_equal(0, res.minheight)
997 call assert_equal(0, res.maxwidth)
998 call assert_equal(0, res.maxheight)
999 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001000 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001001 if has('timers')
1002 call assert_equal(0, res.time)
1003 endif
1004 call popup_close(winid)
1005 call assert_equal({}, popup_getoptions(winid))
1006endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001007
1008func Test_popup_option_values()
1009 new
1010 " window-local
1011 setlocal number
1012 setlocal nowrap
1013 " buffer-local
1014 setlocal omnifunc=Something
1015 " global/buffer-local
1016 setlocal path=/there
1017 " global/window-local
1018 setlocal scrolloff=9
1019
1020 let winid = popup_create('hello', {})
1021 call assert_equal(0, getwinvar(winid, '&number'))
1022 call assert_equal(1, getwinvar(winid, '&wrap'))
1023 call assert_equal('', getwinvar(winid, '&omnifunc'))
1024 call assert_equal(&g:path, getwinvar(winid, '&path'))
1025 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
1026
1027 call popup_close(winid)
1028 bwipe
1029endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001030
1031func Test_popup_atcursor()
1032 topleft vnew
1033 call setline(1, [
1034 \ 'xxxxxxxxxxxxxxxxx',
1035 \ 'xxxxxxxxxxxxxxxxx',
1036 \ 'xxxxxxxxxxxxxxxxx',
1037 \])
1038
1039 call cursor(2, 2)
1040 redraw
1041 let winid = popup_atcursor('vim', {})
1042 redraw
1043 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1044 call assert_equal('xvimxxxxxxxxxxxxx', line)
1045 call popup_close(winid)
1046
1047 call cursor(3, 4)
1048 redraw
1049 let winid = popup_atcursor('vim', {})
1050 redraw
1051 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1052 call assert_equal('xxxvimxxxxxxxxxxx', line)
1053 call popup_close(winid)
1054
1055 call cursor(1, 1)
1056 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001057 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001058 \ line: 'cursor+2',
1059 \ col: 'cursor+1',
1060 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001061 redraw
1062 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1063 call assert_equal('xvimxxxxxxxxxxxxx', line)
1064 call popup_close(winid)
1065
1066 call cursor(3, 3)
1067 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001068 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001069 \ line: 'cursor-2',
1070 \ col: 'cursor-1',
1071 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001072 redraw
1073 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1074 call assert_equal('xvimxxxxxxxxxxxxx', line)
1075 call popup_close(winid)
1076
Bram Moolenaar402502d2019-05-30 22:07:36 +02001077 " just enough room above
1078 call cursor(3, 3)
1079 redraw
1080 let winid = popup_atcursor(['vim', 'is great'], {})
1081 redraw
1082 let pos = popup_getpos(winid)
1083 call assert_equal(1, pos.line)
1084 call popup_close(winid)
1085
1086 " not enough room above, popup goes below the cursor
1087 call cursor(3, 3)
1088 redraw
1089 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1090 redraw
1091 let pos = popup_getpos(winid)
1092 call assert_equal(4, pos.line)
1093 call popup_close(winid)
1094
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001095 " cursor in first line, popup in line 2
1096 call cursor(1, 1)
1097 redraw
1098 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1099 redraw
1100 let pos = popup_getpos(winid)
1101 call assert_equal(2, pos.line)
1102 call popup_close(winid)
1103
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001104 bwipe!
1105endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001106
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001107func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001108 CheckScreendump
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001109
1110 let lines =<< trim END
1111 call setline(1, range(1, 20))
1112 call setline(5, 'here is some text to hover over')
1113 set balloonevalterm
1114 set balloonexpr=BalloonExpr()
1115 set balloondelay=100
1116 func BalloonExpr()
1117 let s:winid = popup_beval([v:beval_text], {})
1118 return ''
1119 endfunc
1120 func Hover()
1121 call test_setmouse(5, 15)
1122 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1123 sleep 100m
1124 endfunc
1125 func MoveOntoPopup()
1126 call test_setmouse(4, 17)
1127 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1128 endfunc
1129 func MoveAway()
1130 call test_setmouse(5, 13)
1131 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1132 endfunc
1133 END
1134 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001135 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001136 call term_wait(buf, 100)
1137 call term_sendkeys(buf, 'j')
1138 call term_sendkeys(buf, ":call Hover()\<CR>")
1139 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1140
1141 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1142 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1143
1144 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1145 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1146
1147 " clean up
1148 call StopVimInTerminal(buf)
1149 call delete('XtestPopupBeval')
1150endfunc
1151
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001152func Test_popup_filter()
1153 new
1154 call setline(1, 'some text')
1155
1156 func MyPopupFilter(winid, c)
1157 if a:c == 'e'
1158 let g:eaten = 'e'
1159 return 1
1160 endif
1161 if a:c == '0'
1162 let g:ignored = '0'
1163 return 0
1164 endif
1165 if a:c == 'x'
1166 call popup_close(a:winid)
1167 return 1
1168 endif
1169 return 0
1170 endfunc
1171
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001172 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001173 redraw
1174
1175 " e is consumed by the filter
1176 call feedkeys('e', 'xt')
1177 call assert_equal('e', g:eaten)
1178
1179 " 0 is ignored by the filter
1180 normal $
1181 call assert_equal(9, getcurpos()[2])
1182 call feedkeys('0', 'xt')
1183 call assert_equal('0', g:ignored)
1184 call assert_equal(1, getcurpos()[2])
1185
1186 " x closes the popup
1187 call feedkeys('x', 'xt')
1188 call assert_equal('e', g:eaten)
1189 call assert_equal(-1, winbufnr(winid))
1190
1191 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001192 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001193endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001194
Bram Moolenaara42d9452019-06-15 21:46:30 +02001195func ShowDialog(key, result)
1196 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001197 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001198 \ filter: 'popup_filter_yesno',
1199 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001200 \ })
1201 redraw
1202 call feedkeys(a:key, "xt")
1203 call assert_equal(winid, s:cb_winid)
1204 call assert_equal(a:result, s:cb_res)
1205endfunc
1206
1207func Test_popup_dialog()
1208 func QuitCallback(id, res)
1209 let s:cb_winid = a:id
1210 let s:cb_res = a:res
1211 endfunc
1212
1213 let winid = ShowDialog("y", 1)
1214 let winid = ShowDialog("Y", 1)
1215 let winid = ShowDialog("n", 0)
1216 let winid = ShowDialog("N", 0)
1217 let winid = ShowDialog("x", 0)
1218 let winid = ShowDialog("X", 0)
1219 let winid = ShowDialog("\<Esc>", 0)
1220 let winid = ShowDialog("\<C-C>", -1)
1221
1222 delfunc QuitCallback
1223endfunc
1224
Bram Moolenaara730e552019-06-16 19:05:31 +02001225func ShowMenu(key, result)
1226 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001227 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001228 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001229 \ })
1230 redraw
1231 call feedkeys(a:key, "xt")
1232 call assert_equal(winid, s:cb_winid)
1233 call assert_equal(a:result, s:cb_res)
1234endfunc
1235
1236func Test_popup_menu()
1237 func QuitCallback(id, res)
1238 let s:cb_winid = a:id
1239 let s:cb_res = a:res
1240 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001241 " mapping won't be used in popup
1242 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001243
1244 let winid = ShowMenu(" ", 1)
1245 let winid = ShowMenu("j \<CR>", 2)
1246 let winid = ShowMenu("JjK \<CR>", 2)
1247 let winid = ShowMenu("jjjjjj ", 3)
1248 let winid = ShowMenu("kkk ", 1)
1249 let winid = ShowMenu("x", -1)
1250 let winid = ShowMenu("X", -1)
1251 let winid = ShowMenu("\<Esc>", -1)
1252 let winid = ShowMenu("\<C-C>", -1)
1253
1254 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001255 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001256endfunc
1257
1258func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001259 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001260
1261 let lines =<< trim END
1262 call setline(1, range(1, 20))
1263 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001264 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001265 func MenuDone(id, res)
1266 echomsg "selected " .. a:res
1267 endfunc
1268 END
1269 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001270 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001271 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1272
1273 call term_sendkeys(buf, "jj")
1274 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1275
1276 call term_sendkeys(buf, " ")
1277 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1278
1279 " clean up
1280 call StopVimInTerminal(buf)
1281 call delete('XtestPopupMenu')
1282endfunc
1283
Bram Moolenaarf914a332019-07-20 15:09:56 +02001284func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001285 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001286
1287 let lines =<< trim END
1288 call setline(1, range(1, 20))
1289 hi PopupSelected ctermbg=green
1290 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1291 func MenuDone(id, res)
1292 echomsg "selected " .. a:res
1293 endfunc
1294 END
1295 call writefile(lines, 'XtestPopupNarrowMenu')
1296 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1297 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1298
1299 " clean up
1300 call term_sendkeys(buf, "x")
1301 call StopVimInTerminal(buf)
1302 call delete('XtestPopupNarrowMenu')
1303endfunc
1304
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001305func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001306 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001307
1308 " Create a popup without title or border, a line of padding will be added to
1309 " put the title on.
1310 let lines =<< trim END
1311 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001312 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001313 END
1314 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001315 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001316 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1317
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001318 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1319 call term_sendkeys(buf, ":\<CR>")
1320 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1321
1322 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1323 call term_sendkeys(buf, ":\<CR>")
1324 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1325
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001326 " clean up
1327 call StopVimInTerminal(buf)
1328 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001329
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001330 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001331 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001332 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001333 call assert_equal('Another Title', popup_getoptions(winid).title)
1334
1335 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001336endfunc
1337
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001338func Test_popup_close_callback()
1339 func PopupDone(id, result)
1340 let g:result = a:result
1341 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001342 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001343 redraw
1344 call popup_close(winid, 'done')
1345 call assert_equal('done', g:result)
1346endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001347
1348func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001349 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001350 redraw
1351 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001352 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001353 call assert_equal(5, pos.height)
1354
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001355 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001356 redraw
1357 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001358 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001359 call assert_equal(3, pos.height)
1360endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001361
1362func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001363 CheckScreendump
1364
Bram Moolenaar988c4332019-06-02 14:12:11 +02001365 " +-----------------------------+
1366 " | | |
1367 " | | |
1368 " | | |
1369 " | line1 |
1370 " |------------line2------------|
1371 " | line3 |
1372 " | line4 |
1373 " | |
1374 " | |
1375 " +-----------------------------+
1376 let lines =<< trim END
1377 only
1378 split
1379 vsplit
1380 let info_window1 = getwininfo()[0]
1381 let line = info_window1['height']
1382 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001383 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001384 \ line : line,
1385 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001386 \ })
1387 END
1388 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001389 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001390 call term_sendkeys(buf, "\<C-W>w")
1391 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1392
1393 " clean up
1394 call StopVimInTerminal(buf)
1395 call delete('XtestPopupBehind')
1396endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001397
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001398func s:VerifyPosition(p, msg, line, col, width, height)
1399 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1400 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1401 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1402 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001403endfunc
1404
1405func Test_popup_position_adjust()
1406 " Anything placed past 2 cells from of the right of the screen is moved to the
1407 " left.
1408 "
1409 " When wrapping is disabled, we also shift to the left to display on the
1410 " screen, unless fixed is set.
1411
1412 " Entries for cases which don't vary based on wrapping.
1413 " Format is per tests described below
1414 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001415 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1416 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1417 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1418 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1419 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1420 \
1421 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1422 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1423 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1424 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1425 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1426 \
1427 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1428 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1429 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1430 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1431 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1432 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001433
1434 " these test groups are dicts with:
1435 " - comment: something to identify the group of tests by
1436 " - options: dict of options to merge with the row/col in tests
1437 " - tests: list of cases. Each one is a list with elements:
1438 " - text
1439 " - row
1440 " - col
1441 " - expected row
1442 " - expected col
1443 " - expected width
1444 " - expected height
1445 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001446 \ #{
1447 \ comment: 'left-aligned with wrapping',
1448 \ options: #{
1449 \ wrap: 1,
1450 \ pos: 'botleft',
1451 \ },
1452 \ tests: both_wrap_tests + [
1453 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1454 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1455 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1456 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1457 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1458 \ ],
1459 \ },
1460 \ #{
1461 \ comment: 'left aligned without wrapping',
1462 \ options: #{
1463 \ wrap: 0,
1464 \ pos: 'botleft',
1465 \ },
1466 \ tests: both_wrap_tests + [
1467 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1468 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1469 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1470 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1471 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1472 \ ],
1473 \ },
1474 \ #{
1475 \ comment: 'left aligned with fixed position',
1476 \ options: #{
1477 \ wrap: 0,
1478 \ fixed: 1,
1479 \ pos: 'botleft',
1480 \ },
1481 \ tests: both_wrap_tests + [
1482 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1483 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1484 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1485 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1486 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1487 \ ],
1488 \ },
1489 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001490
1491 for test_group in tests
1492 for test in test_group.tests
1493 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001494 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001495 \ line: line,
1496 \ col: col,
1497 \ }
1498 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001499
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001500 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001501
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001502 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001503 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1504 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001505 endfor
1506 endfor
1507
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001508 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001509 %bwipe!
1510endfunc
1511
Bram Moolenaar3397f742019-06-02 18:40:06 +02001512func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001513 " width of screen
1514 let X = join(map(range(&columns), {->'X'}), '')
1515
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001516 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1517 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001518
1519 redraw
1520 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1521 call assert_equal(X, line)
1522
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001523 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001524 redraw
1525
1526 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001527 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1528 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001529
1530 redraw
1531 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1532 call assert_equal(X, line)
1533
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001534 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001535 redraw
1536
1537 " Extend so > window width
1538 let X .= 'x'
1539
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001540 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1541 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001542
1543 redraw
1544 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1545 call assert_equal(X[ : -2 ], line)
1546
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001547 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001548 redraw
1549
1550 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001551 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1552 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001553
1554 redraw
1555 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1556 call assert_equal(X[ : -2 ], line)
1557
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001558 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001559 redraw
1560
1561 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001562 let p = popup_create(X,
1563 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1564 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001565
1566 redraw
1567 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1568 let e_line = ' ' . X[ 1 : -2 ]
1569 call assert_equal(e_line, line)
1570
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001571 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001572 redraw
1573
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001574 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001575 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001576endfunc
1577
1578func Test_popup_moved()
1579 new
1580 call test_override('char_avail', 1)
1581 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1582
1583 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001584 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001585 redraw
1586 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001587 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001588 " trigger the check for last_cursormoved by going into insert mode
1589 call feedkeys("li\<Esc>", 'xt')
1590 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001591 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001592
1593 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001594 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001595 redraw
1596 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001597 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001598 call feedkeys("hi\<Esc>", 'xt')
1599 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001600 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001601
1602 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001603 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001604 redraw
1605 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001606 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001607 call feedkeys("li\<Esc>", 'xt')
1608 call assert_equal(1, popup_getpos(winid).visible)
1609 call feedkeys("ei\<Esc>", 'xt')
1610 call assert_equal(1, popup_getpos(winid).visible)
1611 call feedkeys("eli\<Esc>", 'xt')
1612 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001613 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001614
Bram Moolenaar17627312019-06-02 19:53:44 +02001615 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001616 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001617 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001618 redraw
1619 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001620 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001621 call feedkeys("eli\<Esc>", 'xt')
1622 call assert_equal(1, popup_getpos(winid).visible)
1623 call feedkeys("wi\<Esc>", 'xt')
1624 call assert_equal(1, popup_getpos(winid).visible)
1625 call feedkeys("Eli\<Esc>", 'xt')
1626 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001627 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001628
1629 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001630 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001631 redraw
1632 call assert_equal(1, popup_getpos(winid).visible)
1633 call feedkeys("eli\<Esc>", 'xt')
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
1640 bwipe!
1641 call test_override('ALL', 0)
1642endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001643
1644func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001645 CheckFeature timers
1646 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001647
1648 call writefile([
1649 \ "call setline(1, range(1, 20))",
1650 \ "hi Notification ctermbg=lightblue",
1651 \ "call popup_notification('first notification', {})",
1652 \], 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001653 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001654 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1655
1656 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001657 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1658 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001659 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1660
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001661 " clean up
1662 call StopVimInTerminal(buf)
1663 call delete('XtestNotifications')
1664endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001665
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001666func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001667 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001668
1669 let lines =<< trim END
1670 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001671 hi ScrollThumb ctermbg=blue
1672 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001673 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001674 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001675 \ minwidth: 8,
1676 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001677 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001678 func ScrollUp()
1679 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1680 endfunc
1681 func ScrollDown()
1682 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1683 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001684 func ClickTop()
1685 call feedkeys("\<F4>\<LeftMouse>", "xt")
1686 endfunc
1687 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001688 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001689 call feedkeys("\<F5>\<LeftMouse>", "xt")
1690 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001691 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001692 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1693 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001694 END
1695 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001696 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001697 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1698
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001699 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001700 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001701 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1702
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001703 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001704 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001705 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1706
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001707 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001708 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001709 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1710
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02001711 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001712 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1713 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1714
1715 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1716 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1717
1718 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001719 " wait a bit, otherwise it fails sometimes (double click recognized?)
1720 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001721 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1722 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1723
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001724 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1725 sleep 100m
1726 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1727 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1728
1729 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1730 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1731
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001732 " clean up
1733 call StopVimInTerminal(buf)
1734 call delete('XtestPopupScroll')
1735endfunc
1736
Bram Moolenaar437a7462019-07-05 20:17:22 +02001737func Test_popup_fitting_scrollbar()
1738 " this was causing a crash, divide by zero
1739 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001740 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001741 \ scrollbar: 1,
1742 \ maxwidth: 10,
1743 \ maxheight: 5,
1744 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001745 redraw
1746 call popup_clear()
1747endfunc
1748
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001749func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001750 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001751
1752 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001753 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001754 let p = popup_create('test', opts)
1755 call popup_settext(p, 'this is a text')
1756 END
1757
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001758 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001759 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001760 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1761
1762 " Setting to empty string clears it
1763 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1764 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1765
1766 " Setting a list
1767 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1768 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1769
1770 " Shrinking with a list
1771 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1772 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1773
1774 " Growing with a list
1775 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1776 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1777
1778 " Empty list clears
1779 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1780 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1781
1782 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001783 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001784 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1785
1786 " clean up
1787 call StopVimInTerminal(buf)
1788 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001789endfunc
1790
1791func Test_popup_hidden()
1792 new
1793
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001794 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001795 redraw
1796 call assert_equal(0, popup_getpos(winid).visible)
1797 call popup_close(winid)
1798
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001799 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001800 redraw
1801 call assert_equal(0, popup_getpos(winid).visible)
1802 call popup_close(winid)
1803
1804 func QuitCallback(id, res)
1805 let s:cb_winid = a:id
1806 let s:cb_res = a:res
1807 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001808 let winid = popup_dialog('make a choice', #{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001809 \ filter: 'popup_filter_yesno',
1810 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001811 \ })
1812 redraw
1813 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001814 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1815 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001816 exe "normal anot used by filter\<Esc>"
1817 call assert_equal('not used by filter', getline(1))
1818
1819 call popup_show(winid)
1820 call feedkeys('y', "xt")
1821 call assert_equal(1, s:cb_res)
1822
1823 bwipe!
1824 delfunc QuitCallback
1825endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001826
1827" Test options not checked elsewhere
1828func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001829 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001830 let options = popup_getoptions(winid)
1831 call assert_equal(1, options.wrap)
1832 call assert_equal(0, options.drag)
1833 call assert_equal('Beautiful', options.highlight)
1834
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001835 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001836 let options = popup_getoptions(winid)
1837 call assert_equal(0, options.wrap)
1838 call assert_equal(1, options.drag)
1839 call assert_equal('Another', options.highlight)
1840
1841 call popup_close(winid)
1842endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001843
1844func Test_popupwin_garbage_collect()
1845 func MyPopupFilter(x, winid, c)
1846 " NOP
1847 endfunc
1848
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001849 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001850 call test_garbagecollect_now()
1851 redraw
1852 " Must not crach caused by invalid memory access
1853 call feedkeys('j', 'xt')
1854 call assert_true(v:true)
1855
1856 call popup_close(winid)
1857 delfunc MyPopupFilter
1858endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001859
1860func Test_popupwin_with_buffer()
1861 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1862 let buf = bufadd('XsomeFile')
1863 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02001864
1865 setlocal number
1866 call setbufvar(buf, "&wrapmargin", 13)
1867
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001868 let winid = popup_create(buf, {})
1869 call assert_notequal(0, winid)
1870 let pos = popup_getpos(winid)
1871 call assert_equal(2, pos.height)
1872 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02001873
1874 " window-local option is set to default, buffer-local is not
1875 call assert_equal(0, getwinvar(winid, '&number'))
1876 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
1877
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001878 call popup_close(winid)
1879 call assert_equal({}, popup_getpos(winid))
1880 call assert_equal(1, bufloaded(buf))
1881 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02001882 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02001883
1884 edit test_popupwin.vim
1885 let winid = popup_create(bufnr(''), {})
1886 redraw
1887 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001888 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001889endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02001890
1891func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001892 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001893 \ maxwidth: 40,
1894 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02001895 \ })
1896 for top in range(1, 20)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001897 call popup_setoptions(winid, #{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02001898 redraw
1899 call assert_equal(19, popup_getpos(winid).width)
1900 endfor
1901 call popup_clear()
1902endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02001903
1904func Test_popupwin_buf_close()
1905 let buf = bufadd('Xtestbuf')
1906 call bufload(buf)
1907 call setbufline(buf, 1, ['just', 'some', 'lines'])
1908 let winid = popup_create(buf, {})
1909 redraw
1910 call assert_equal(3, popup_getpos(winid).height)
1911 let bufinfo = getbufinfo(buf)[0]
1912 call assert_equal(1, bufinfo.changed)
1913 call assert_equal(0, bufinfo.hidden)
1914 call assert_equal(0, bufinfo.listed)
1915 call assert_equal(1, bufinfo.loaded)
1916 call assert_equal([], bufinfo.windows)
1917 call assert_equal([winid], bufinfo.popups)
1918
1919 call popup_close(winid)
1920 call assert_equal({}, popup_getpos(winid))
1921 let bufinfo = getbufinfo(buf)[0]
1922 call assert_equal(1, bufinfo.changed)
1923 call assert_equal(1, bufinfo.hidden)
1924 call assert_equal(0, bufinfo.listed)
1925 call assert_equal(1, bufinfo.loaded)
1926 call assert_equal([], bufinfo.windows)
1927 call assert_equal([], bufinfo.popups)
1928 exe 'bwipe! ' .. buf
1929endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02001930
1931func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001932 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02001933
1934 let lines =<< trim END
1935 call setline(1, range(1, 10))
1936 hi ScrollThumb ctermbg=blue
1937 hi ScrollBar ctermbg=red
1938 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001939 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001940 \ maxwidth: 10,
1941 \ maxheight: 3,
1942 \ pos : 'topleft',
1943 \ col : a:col,
1944 \ line : a:line,
1945 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02001946 \ })
1947 endfunc
1948 call PopupMenu(['x'], 1, 1)
1949 call PopupMenu(['123456789|'], 1, 16)
1950 call PopupMenu(['123456789|' .. ' '], 7, 1)
1951 call PopupMenu([repeat('123456789|', 100)], 7, 16)
1952 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
1953 END
1954 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001955 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02001956 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
1957
1958 " close the menu popupwin.
1959 call term_sendkeys(buf, " ")
1960 call term_sendkeys(buf, " ")
1961 call term_sendkeys(buf, " ")
1962 call term_sendkeys(buf, " ")
1963 call term_sendkeys(buf, " ")
1964
1965 " clean up
1966 call StopVimInTerminal(buf)
1967 call delete('XtestPopupMenuMaxWidth')
1968endfunc
1969
Bram Moolenaara901a372019-07-13 16:38:50 +02001970func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001971 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02001972
1973 let lines =<< trim END
1974 call setline(1, range(1, 20))
1975 hi ScrollThumb ctermbg=blue
1976 hi ScrollBar ctermbg=red
1977 call popup_menu(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001978 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001979 \ minwidth: 8,
1980 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02001981 \ })
1982 END
1983 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001984 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02001985
1986 call term_sendkeys(buf, "j")
1987 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
1988
1989 call term_sendkeys(buf, "jjj")
1990 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
1991
1992 " if the cursor is the bottom line, it stays at the bottom line.
1993 call term_sendkeys(buf, repeat("j", 20))
1994 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
1995
1996 call term_sendkeys(buf, "kk")
1997 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
1998
1999 call term_sendkeys(buf, "k")
2000 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2001
2002 " if the cursor is in the top line, it stays in the top line.
2003 call term_sendkeys(buf, repeat("k", 20))
2004 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2005
2006 " close the menu popupwin.
2007 call term_sendkeys(buf, " ")
2008
2009 " clean up
2010 call StopVimInTerminal(buf)
2011 call delete('XtestPopupMenuScroll')
2012endfunc
2013
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002014func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002015 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002016
2017 let lines =<< trim END
2018 function! MyFilter(winid, key) abort
2019 if a:key == "0"
2020 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2021 return 1
2022 endif
2023 if a:key == "G"
2024 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2025 return 1
2026 endif
2027 if a:key == "j"
2028 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2029 return 1
2030 endif
2031 if a:key == "k"
2032 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2033 return 1
2034 endif
2035 if a:key == 'x'
2036 call popup_close(a:winid)
2037 return 1
2038 endif
2039 return 0
2040 endfunction
2041 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2042 \ maxheight : 3,
2043 \ filter : 'MyFilter'
2044 \ })
2045 END
2046 call writefile(lines, 'XtestPopupMenuFilter')
2047 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2048
2049 call term_sendkeys(buf, "j")
2050 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2051
2052 call term_sendkeys(buf, "k")
2053 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2054
2055 call term_sendkeys(buf, "G")
2056 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2057
2058 call term_sendkeys(buf, "0")
2059 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2060
2061 call term_sendkeys(buf, "x")
2062
2063 " clean up
2064 call StopVimInTerminal(buf)
2065 call delete('XtestPopupMenuFilter')
2066endfunc
2067
2068func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002069 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002070
2071 let winid = popup_create('some text', {})
2072 call assert_equal(0, popup_getoptions(winid).cursorline)
2073 call popup_close(winid)
2074
2075 let winid = popup_create('some text', #{ cursorline: 1, })
2076 call assert_equal(1, popup_getoptions(winid).cursorline)
2077 call popup_close(winid)
2078
2079 let winid = popup_create('some text', #{ cursorline: 0, })
2080 call assert_equal(0, popup_getoptions(winid).cursorline)
2081 call popup_close(winid)
2082
2083 let winid = popup_menu('some text', {})
2084 call assert_equal(1, popup_getoptions(winid).cursorline)
2085 call popup_close(winid)
2086
2087 let winid = popup_menu('some text', #{ cursorline: 1, })
2088 call assert_equal(1, popup_getoptions(winid).cursorline)
2089 call popup_close(winid)
2090
2091 let winid = popup_menu('some text', #{ cursorline: 0, })
2092 call assert_equal(0, popup_getoptions(winid).cursorline)
2093 call popup_close(winid)
2094
2095 " ---------
2096 " Pattern 1
2097 " ---------
2098 let lines =<< trim END
2099 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2100 END
2101 call writefile(lines, 'XtestPopupCursorLine')
2102 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2103 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2104 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2105 call StopVimInTerminal(buf)
2106
2107 " ---------
2108 " Pattern 2
2109 " ---------
2110 let lines =<< trim END
2111 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2112 END
2113 call writefile(lines, 'XtestPopupCursorLine')
2114 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2115 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2116 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2117 call StopVimInTerminal(buf)
2118
2119 " ---------
2120 " Pattern 3
2121 " ---------
2122 let lines =<< trim END
2123 function! MyFilter(winid, key) abort
2124 if a:key == "j"
2125 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2126 return 1
2127 endif
2128 if a:key == 'x'
2129 call popup_close(a:winid)
2130 return 1
2131 endif
2132 return 0
2133 endfunction
2134 call popup_menu(['111', '222', '333'], #{
2135 \ cursorline : 0,
2136 \ maxheight : 2,
2137 \ filter : 'MyFilter',
2138 \ })
2139 END
2140 call writefile(lines, 'XtestPopupCursorLine')
2141 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2142 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2143 call term_sendkeys(buf, "j")
2144 call term_sendkeys(buf, "j")
2145 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2146 call term_sendkeys(buf, "x")
2147 call StopVimInTerminal(buf)
2148
2149 " ---------
2150 " Pattern 4
2151 " ---------
2152 let lines =<< trim END
2153 function! MyFilter(winid, key) abort
2154 if a:key == "j"
2155 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2156 return 1
2157 endif
2158 if a:key == 'x'
2159 call popup_close(a:winid)
2160 return 1
2161 endif
2162 return 0
2163 endfunction
2164 call popup_menu(['111', '222', '333'], #{
2165 \ cursorline : 1,
2166 \ maxheight : 2,
2167 \ filter : 'MyFilter',
2168 \ })
2169 END
2170 call writefile(lines, 'XtestPopupCursorLine')
2171 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2172 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2173 call term_sendkeys(buf, "j")
2174 call term_sendkeys(buf, "j")
2175 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2176 call term_sendkeys(buf, "x")
2177 call StopVimInTerminal(buf)
2178
2179 call delete('XtestPopupCursorLine')
2180endfunc
2181
Bram Moolenaarf914a332019-07-20 15:09:56 +02002182func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002183 CheckScreendump
2184
Bram Moolenaarf914a332019-07-20 15:09:56 +02002185 call writefile([
2186 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2187 \ "another\tXtagfile\t/^this is another",
2188 \ "theword\tXtagfile\t/^theword"],
2189 \ 'Xtags')
2190 call writefile(range(1,20)
2191 \ + ['theword is here']
2192 \ + range(22, 27)
2193 \ + ['this is another place']
2194 \ + range(29, 40),
2195 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002196 call writefile(range(1,10)
2197 \ + ['searched word is here']
2198 \ + range(12, 20),
2199 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002200 let lines =<< trim END
2201 set tags=Xtags
2202 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002203 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002204 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002205 \ 'three',
2206 \ 'four',
2207 \ 'five',
2208 \ 'six',
2209 \ 'seven',
2210 \ 'find theword somewhere',
2211 \ 'nine',
2212 \ 'this is another word',
2213 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002214 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002215 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002216 END
2217 call writefile(lines, 'XtestPreviewPopup')
2218 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2219
2220 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2221 call term_sendkeys(buf, ":\<CR>")
2222 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2223
2224 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2225 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2226
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002227 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002228 call term_sendkeys(buf, ":\<CR>")
2229 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2230
2231 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2232 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2233
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002234 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2235 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002236 call term_sendkeys(buf, ":cd testdir\<CR>")
2237
2238 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002239 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002240 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2241
2242 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2243 call term_sendkeys(buf, ":\<CR>")
2244 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2245
2246 call term_sendkeys(buf, ":pclose\<CR>")
2247 call term_sendkeys(buf, ":psearch searched\<CR>")
2248 call term_sendkeys(buf, ":\<CR>")
2249 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002250
Bram Moolenaarf914a332019-07-20 15:09:56 +02002251 call StopVimInTerminal(buf)
2252 call delete('Xtags')
2253 call delete('Xtagfile')
2254 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002255 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002256endfunc
2257
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002258func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002259 let lines =<< trim END
2260 set completeopt+=preview,popup
2261 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002262 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002263
2264 func CompleteFuncDict(findstart, base)
2265 if a:findstart
2266 if col('.') > 10
2267 return col('.') - 10
2268 endif
2269 return 0
2270 endif
2271
2272 return {
2273 \ 'words': [
2274 \ {
2275 \ 'word': 'aword',
2276 \ 'abbr': 'wrd',
2277 \ 'menu': 'extra text',
2278 \ 'info': 'words are cool',
2279 \ 'kind': 'W',
2280 \ 'user_data': 'test'
2281 \ },
2282 \ {
2283 \ 'word': 'anotherword',
2284 \ 'abbr': 'anotwrd',
2285 \ 'menu': 'extra text',
2286 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2287 \ 'kind': 'W',
2288 \ 'user_data': 'notest'
2289 \ },
2290 \ {
2291 \ 'word': 'noinfo',
2292 \ 'abbr': 'noawrd',
2293 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002294 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002295 \ 'kind': 'W',
2296 \ 'user_data': 'notest'
2297 \ },
2298 \ {
2299 \ 'word': 'thatword',
2300 \ 'abbr': 'thatwrd',
2301 \ 'menu': 'extra text',
2302 \ 'info': 'that word is cool',
2303 \ 'kind': 'W',
2304 \ 'user_data': 'notest'
2305 \ },
2306 \ ]
2307 \ }
2308 endfunc
2309 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002310 func ChangeColor()
2311 let id = popup_findinfo()
2312 call popup_setoptions(id, #{highlight: 'InfoPopup'})
2313 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002314 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002315 return lines
2316endfunc
2317
2318func Test_popupmenu_info_border()
2319 CheckScreendump
2320
2321 let lines = Get_popupmenu_lines()
2322 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002323 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002324
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002325 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2326 call term_wait(buf, 50)
2327
2328 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2329 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2330
2331 call term_sendkeys(buf, "\<C-N>")
2332 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2333
2334 call term_sendkeys(buf, "\<C-N>")
2335 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2336
2337 call term_sendkeys(buf, "\<C-N>\<C-N>")
2338 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2339
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002340 " info on the left with scrollbar
2341 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2342 call term_sendkeys(buf, "\<C-N>\<C-N>")
2343 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2344
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002345 call StopVimInTerminal(buf)
2346 call delete('XtestInfoPopup')
2347endfunc
2348
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002349func Test_popupmenu_info_noborder()
2350 CheckScreendump
2351
2352 let lines = Get_popupmenu_lines()
2353 call add(lines, 'set completepopup=height:4,border:off')
2354 call writefile(lines, 'XtestInfoPopupNb')
2355
2356 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{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_nb_1', {})
2361
2362 call StopVimInTerminal(buf)
2363 call delete('XtestInfoPopupNb')
2364endfunc
2365
Bram Moolenaar258cef52019-08-21 17:29:29 +02002366func Test_popupmenu_info_align_menu()
2367 CheckScreendump
2368
2369 let lines = Get_popupmenu_lines()
2370 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2371 call writefile(lines, 'XtestInfoPopupNb')
2372
2373 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2374 call term_wait(buf, 50)
2375
2376 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2377 call term_sendkeys(buf, "\<C-N>")
2378 call term_sendkeys(buf, "\<C-N>")
2379 call term_sendkeys(buf, "\<C-N>")
2380 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2381
2382 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2383 call term_sendkeys(buf, "\<C-N>")
2384 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2385
2386 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002387 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002388 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2389 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2390 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2391
2392 call StopVimInTerminal(buf)
2393 call delete('XtestInfoPopupNb')
2394endfunc
2395
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002396func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02002397 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002398 call popup_clear()
2399 let winid = popup_notification('nothing wrong', {})
2400 call assert_equal(bufnr, winbufnr(winid))
2401 call popup_clear()
2402endfunc
2403
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002404" vim: shiftwidth=2 sts=2