blob: a6d4d622bc2c85df10177ba20905d5feacb92a94 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature textprop
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
15 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020016 call prop_type_add('comment', #{highlight: 'Comment'})
17 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
18 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020019 call setwinvar(winid2, '&wincolor', 'PopupColor2')
20 END
21 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020022 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020023 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
24
Bram Moolenaarec583842019-05-26 14:11:23 +020025 " Add a tabpage
26 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020027 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020028 \ .. "#{text: 'other tab'},"
29 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020030 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020031 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020032 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020033 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
34
35 " switch back to first tabpage
36 call term_sendkeys(buf, "gt")
37 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
38
39 " close that tabpage
40 call term_sendkeys(buf, ":quit!\<CR>")
41 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
42
Bram Moolenaar202d9822019-06-11 21:56:30 +020043 " set 'columns' to a small value, size must be recomputed
44 call term_sendkeys(buf, ":let cols = &columns\<CR>")
45 call term_sendkeys(buf, ":set columns=12\<CR>")
46 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
47 call term_sendkeys(buf, ":let &columns = cols\<CR>")
48
Bram Moolenaar17146962019-05-30 00:12:11 +020049 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020050 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020051 call term_sendkeys(buf, ":redraw\<CR>")
52 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
53
Bram Moolenaar17146962019-05-30 00:12:11 +020054 " show not fitting line at bottom
55 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
56 call term_sendkeys(buf, ":redraw\<CR>")
57 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
58
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020059 " move popup over ruler
60 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020061 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020062 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020063 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
64
65 " clear all popups after moving the cursor a bit, so that ruler is updated
66 call term_sendkeys(buf, "axxx\<Esc>")
67 call term_wait(buf)
68 call term_sendkeys(buf, "0")
69 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020070 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020071 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
72
Bram Moolenaar4d784b22019-05-25 19:51:39 +020073 " clean up
74 call StopVimInTerminal(buf)
75 call delete('XtestPopup')
76endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020077
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020078func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020079 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020080
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020081 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020082 let lines =<< trim END
83 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020084 call popup_create('hello border', #{line: 2, col: 3, border: []})
85 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020086 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020087 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
88 call popup_create('paddings', #{line: 6, col: 23, padding: [1, 3, 2, 4]})
89 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
90 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020091 END
92 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
93 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020094 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020095 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
96
97 call StopVimInTerminal(buf)
98 call delete('XtestPopupBorder')
99 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200100
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200101 let lines =<< trim END
102 call setline(1, range(1, 100))
103 hi BlueColor ctermbg=lightblue
104 hi TopColor ctermbg=253
105 hi RightColor ctermbg=245
106 hi BottomColor ctermbg=240
107 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200108 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
109 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
110 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
111 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
112 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
113 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200114 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200115 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200116 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200117 END
118 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200119 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200120 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
121
Bram Moolenaarad24a712019-06-17 20:05:45 +0200122 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200123 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200124 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
125
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200126 " check multi-byte border only with 'ambiwidth' single
127 if &ambiwidth == 'single'
128 call term_sendkeys(buf, ":call MultiByte()\<CR>")
129 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
130 endif
131
Bram Moolenaar790498b2019-06-01 22:15:29 +0200132 call StopVimInTerminal(buf)
133 call delete('XtestPopupBorder')
134
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200135 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200136 \ line: 2,
137 \ core_line: 3,
138 \ col: 3,
139 \ core_col: 4,
140 \ width: 14,
141 \ core_width: 12,
142 \ height: 3,
143 \ core_height: 1,
144 \ firstline: 1,
145 \ scrollbar: 0,
146 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200147 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200148 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200149 let options = popup_getoptions(winid)
150 call assert_equal([], options.border)
151 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200152
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200153 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200154 let with_border_or_padding.width = 15
155 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200156 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200157 let options = popup_getoptions(winid)
158 call assert_false(has_key(options, "border"))
159 call assert_equal([], options.padding)
160
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200161 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200162 \ padding: [1, 2, 3, 4],
163 \ border: [4, 0, 7, 8],
164 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
165 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200166 \ })
167 let options = popup_getoptions(winid)
168 call assert_equal([1, 0, 1, 1], options.border)
169 call assert_equal([1, 2, 3, 4], options.padding)
170 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
171 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200172
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200173 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
174 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200175 \ line: 3,
176 \ core_line: 5,
177 \ col: 8,
178 \ core_col: 10,
179 \ width: 14,
180 \ core_width: 10,
181 \ height: 5,
182 \ scrollbar: 0,
183 \ core_height: 1,
184 \ firstline: 1,
185 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200186
187 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200188endfunc
189
Bram Moolenaarb4230122019-05-30 18:40:53 +0200190func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200191 CheckScreendump
192
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200193 let lines =<< trim END
194 call setline(1, range(1, 100))
195 hi PopupColor ctermbg=lightblue
196 let winid = popup_create([
197 \ '#include <stdio.h>',
198 \ 'int main(void)',
199 \ '{',
200 \ ' printf(123);',
201 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200202 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200203 call win_execute(winid, 'set syntax=cpp')
204 END
205 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200206 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200207 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
208
209 " clean up
210 call StopVimInTerminal(buf)
211 call delete('XtestPopup')
212endfunc
213
214func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200215 CheckScreendump
216
Bram Moolenaar402502d2019-05-30 22:07:36 +0200217 let lines =<< trim END
218 call setline(1, range(1, 100))
219 hi PopupColor ctermbg=lightgrey
220 let winid = popup_create([
221 \ '#include <stdio.h>',
222 \ 'int main(void)',
223 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200224 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200225 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200226 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200227 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
228 END
229 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200230 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200231 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
232
233 " clean up
234 call StopVimInTerminal(buf)
235 call delete('XtestPopup')
236endfunc
237
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200238func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200239 CheckScreendump
240
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200241 let lines =<< trim END
242 call setline(1, ['111 222 333', '444 555 666'])
243 let winid = popup_create([
244 \ '111 222 333',
245 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200246 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200247 set hlsearch
248 /666
249 call matchadd('ErrorMsg', '111')
250 call matchadd('ErrorMsg', '444')
251 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
252 call win_execute(winid, "call matchadd('ErrorMsg', '555')")
253 END
254 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200255 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200256 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
257
258 " clean up
259 call StopVimInTerminal(buf)
260 call delete('XtestPopupMatches')
261endfunc
262
Bram Moolenaar399d8982019-06-02 15:34:29 +0200263func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200264 CheckScreendump
265
Bram Moolenaar399d8982019-06-02 15:34:29 +0200266 let lines =<< trim END
267 call setline(1, repeat([repeat('-', 60)], 15))
268 set so=0
269 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200270 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200271 \ line: 'cursor+1',
272 \ col: 'cursor',
273 \ pos: 'topleft',
274 \ border: [],
275 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200276 \ })
277 normal 25|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200278 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200279 \ line: 'cursor+1',
280 \ col: 'cursor',
281 \ pos: 'topright',
282 \ border: [],
283 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200284 \ })
285 normal 9G29|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200286 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200287 \ line: 'cursor-1',
288 \ col: 'cursor',
289 \ pos: 'botleft',
290 \ border: [],
291 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200292 \ })
293 normal 51|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200294 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200295 \ line: 'cursor-1',
296 \ col: 'cursor',
297 \ pos: 'botright',
298 \ border: [],
299 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200300 \ })
301 END
302 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200303 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200304 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
305
306 " clean up
307 call StopVimInTerminal(buf)
308 call delete('XtestPopupCorners')
309endfunc
310
Bram Moolenaar8d241042019-06-12 23:40:01 +0200311func Test_popup_firstline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200312 CheckScreendump
313
Bram Moolenaar8d241042019-06-12 23:40:01 +0200314 let lines =<< trim END
315 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200316 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200317 \ maxheight: 4,
318 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200319 \ })
320 END
321 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200322 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200323 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
324
325 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
326 call term_sendkeys(buf, ":\<CR>")
327 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200328
329 " clean up
330 call StopVimInTerminal(buf)
331 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200332
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200333 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200334 \ maxheight: 2,
335 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200336 \ })
337 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200338 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200339 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200340 call popup_close(winid)
341
342 let winid = popup_create(['xxx']->repeat(50), #{
343 \ maxheight: 3,
344 \ firstline: 11,
345 \ })
346 redraw
347 call assert_equal(11, popup_getoptions(winid).firstline)
348 call assert_equal(11, popup_getpos(winid).firstline)
349
350 " Normal command changes what is displayed but not "firstline"
351 call win_execute(winid, "normal! \<c-y>")
352 call assert_equal(11, popup_getoptions(winid).firstline)
353 call assert_equal(10, popup_getpos(winid).firstline)
354
355 " Making some property change applies "firstline" again
356 call popup_setoptions(winid, #{line: 4})
357 call assert_equal(11, popup_getoptions(winid).firstline)
358 call assert_equal(11, popup_getpos(winid).firstline)
359
360 " Remove "firstline" property and scroll
361 call popup_setoptions(winid, #{firstline: 0})
362 call win_execute(winid, "normal! \<c-y>")
363 call assert_equal(0, popup_getoptions(winid).firstline)
364 call assert_equal(10, popup_getpos(winid).firstline)
365
366 " Making some property change has no side effect
367 call popup_setoptions(winid, #{line: 3})
368 call assert_equal(0, popup_getoptions(winid).firstline)
369 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaarae943152019-06-16 22:54:14 +0200370
371 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200372endfunc
373
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200374func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200375 CheckScreendump
376
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200377 " create a popup that covers the command line
378 let lines =<< trim END
379 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200380 split
381 vsplit
382 $wincmd w
383 vsplit
384 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200385 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200386 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200387 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200388 \ border: [],
389 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200390 \ })
391 func Dragit()
392 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
393 endfunc
394 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200395 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200396 func Resize()
397 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
398 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200399 map <silent> <F5> :call test_setmouse(6, 21)<CR>
400 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200401 END
402 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200403 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200404 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
405
406 call term_sendkeys(buf, ":call Dragit()\<CR>")
407 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
408
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200409 call term_sendkeys(buf, ":call Resize()\<CR>")
410 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
411
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200412 " clean up
413 call StopVimInTerminal(buf)
414 call delete('XtestPopupDrag')
415endfunc
416
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200417func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200418 CheckScreendump
419
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200420 let lines =<< trim END
421 call setline(1, range(1, 20))
422 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200423 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200424 \ close: 'button',
425 \ border: [],
426 \ line: 1,
427 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200428 \ })
429 func CloseMsg(id, result)
430 echomsg 'Popup closed with ' .. a:result
431 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200432 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200433 \ close: 'click',
434 \ line: 3,
435 \ col: 15,
436 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200437 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200438 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200439 \ close: 'button',
440 \ line: 5,
441 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200442 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200443 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200444 \ close: 'button',
445 \ padding: [],
446 \ line: 5,
447 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200448 \ })
449 func CloseWithX()
450 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
451 endfunc
452 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
453 func CloseWithClick()
454 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
455 endfunc
456 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200457 func CreateWithMenuFilter()
458 let winid = popup_create('barfoo', #{
459 \ close: 'button',
460 \ filter: 'popup_filter_menu',
461 \ border: [],
462 \ line: 1,
463 \ col: 40,
464 \ })
465 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200466 END
467 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200468 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200469 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
470
471 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
472 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
473
474 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
475 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
476
Bram Moolenaarf6396232019-08-24 19:36:00 +0200477 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
478 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
479
480 " We have to send the actual mouse code, feedkeys() would be caught the
481 " filter.
482 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
483 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
484
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200485 " clean up
486 call StopVimInTerminal(buf)
487 call delete('XtestPopupClose')
488endfunction
489
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200490func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200491 CheckScreendump
492
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200493 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200494 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200495 hi PopupColor ctermbg=lightgrey
496 let winid = popup_create([
497 \ 'some text',
498 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200499 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200500 \ line: 1,
501 \ col: 10,
502 \ wrap: 0,
503 \ fixed: 1,
504 \ zindex: 90,
505 \ padding: [],
506 \ highlight: 'PopupColor',
507 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200508 call popup_create([
509 \ 'xxxxxxxxx',
510 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200511 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200512 \ line: 3,
513 \ col: 18,
514 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200515 let winidb = popup_create([
516 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200517 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200518 \ line: 7,
519 \ col: 10,
520 \ wrap: 0,
521 \ fixed: 1,
522 \ close: 'button',
523 \ zindex: 90,
524 \ padding: [],
525 \ border: [],
526 \ 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 +0200527 END
528 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200529 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200530 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
531
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200532 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
533 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200534 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200535 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
536
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200537 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
538 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200539 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200540 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
541
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200542 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
543 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200544 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200545 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
546
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200547 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
548 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200549 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200550 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
551
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200552 " clean up
553 call StopVimInTerminal(buf)
554 call delete('XtestPopupMask')
555endfunc
556
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200557func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200558 CheckScreendump
559 CheckFeature clipboard_working
560
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200561 " create a popup with some text to be selected
562 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200563 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200564 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200565 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200566 \ drag: 1,
567 \ border: [],
568 \ line: 3,
569 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200570 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200571 \ })
572 func Select1()
573 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
574 endfunc
575 map <silent> <F3> :call test_setmouse(4, 15)<CR>
576 map <silent> <F4> :call test_setmouse(6, 23)<CR>
577 END
578 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200579 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200580 call term_sendkeys(buf, ":call Select1()\<CR>")
581 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
582
583 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
584 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200585 " clean the command line, sometimes it still shows a command
586 call term_sendkeys(buf, ":\<esc>")
587
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200588 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
589
590 " clean up
591 call StopVimInTerminal(buf)
592 call delete('XtestPopupSelect')
593endfunc
594
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200595func Test_popup_in_tab()
596 " default popup is local to tab, not visible when in other tab
597 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200598 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200599 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200600 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200601 tabnew
602 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200603 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200604 quit
605 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200606
607 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200608 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200609 " buffer is gone now
610 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200611
612 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200613 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200614 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200615 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200616 tabnew
617 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200618 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200619 quit
620 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200621 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200622
623 " create popup in other tab
624 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200625 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200626 call assert_equal(0, popup_getpos(winid).visible)
627 call assert_equal(1, popup_getoptions(winid).tabpage)
628 quit
629 call assert_equal(1, popup_getpos(winid).visible)
630 call assert_equal(0, popup_getoptions(winid).tabpage)
631 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200632endfunc
633
634func Test_popup_valid_arguments()
635 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200636 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200637 let pos = popup_getpos(winid)
638 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200639 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200640
641 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200642 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200643 let pos = popup_getpos(winid)
644 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200645 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200646
647 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200648 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200649 let pos = popup_getpos(winid)
650 let around = (&columns - pos.width) / 2
651 call assert_inrange(around - 1, around + 1, pos.col)
652 let around = (&lines - pos.height) / 2
653 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200654 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200655endfunc
656
657func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200658 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200659 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200660 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200661 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200662
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200663 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
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: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200666 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200667 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200668 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200669 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200670 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200671
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200672 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
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: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200675 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200676 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200677 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200678 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200679 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200680
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200681 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200682 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200683 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200684 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200685 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200686 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200687 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200688 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200689 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
690 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200691 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200692 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200693
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200694 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200695 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200696 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200697 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200698 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200699 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200700 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
701 call popup_clear()
702 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200703 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200704 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200705endfunc
706
Bram Moolenaareea16992019-05-31 17:34:48 +0200707func Test_win_execute_closing_curwin()
708 split
709 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200710 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200711 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200712endfunc
713
714func Test_win_execute_not_allowed()
715 let winid = popup_create('some text', {})
716 call assert_fails('call win_execute(winid, "split")', 'E994:')
717 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
718 call assert_fails('call win_execute(winid, "close")', 'E994:')
719 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200720 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200721 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
722 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
723 call assert_fails('call win_execute(winid, "next")', 'E994:')
724 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
725 call assert_fails('call win_execute(winid, "buf")', 'E994:')
726 call assert_fails('call win_execute(winid, "edit")', 'E994:')
727 call assert_fails('call win_execute(winid, "enew")', 'E994:')
728 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
729 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
730 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
731 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200732 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200733endfunc
734
Bram Moolenaar402502d2019-05-30 22:07:36 +0200735func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200736 CheckScreendump
737
Bram Moolenaar402502d2019-05-30 22:07:36 +0200738 let lines =<< trim END
739 call setline(1, range(1, 100))
740 let winid = popup_create(
741 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200742 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200743 END
744 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200745 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200746 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
747
748 " clean up
749 call StopVimInTerminal(buf)
750 call delete('XtestPopup')
751endfunc
752
753func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200754 CheckScreendump
755
Bram Moolenaar402502d2019-05-30 22:07:36 +0200756 let lines =<< trim END
757 call setline(1, range(1, 100))
758 let winid = popup_create(
759 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200760 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200761 END
762 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200763 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200764 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
765
766 " clean up
767 call StopVimInTerminal(buf)
768 call delete('XtestPopup')
769endfunc
770
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200771func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200772 CheckScreendump
773
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200774 let lines =<< trim END
775 set showbreak=>>\
776 call setline(1, range(1, 20))
777 let winid = popup_dialog(
778 \ 'a long line here',
779 \ #{filter: 'popup_filter_yesno'})
780 END
781 call writefile(lines, 'XtestPopupShowbreak')
782 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
783 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
784
785 " clean up
786 call term_sendkeys(buf, "y")
787 call StopVimInTerminal(buf)
788 call delete('XtestPopupShowbreak')
789endfunc
790
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200791func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200792 CheckFeature timers
793
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200794 topleft vnew
795 call setline(1, 'hello')
796
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200797 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200798 \ line: 1,
799 \ col: 1,
800 \ minwidth: 20,
801 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200802 \})
803 redraw
804 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
805 call assert_equal('world', line)
806
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200807 call assert_equal(winid, popup_locate(1, 1))
808 call assert_equal(winid, popup_locate(1, 20))
809 call assert_equal(0, popup_locate(1, 21))
810 call assert_equal(0, popup_locate(2, 1))
811
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200812 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200813 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200814 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
815 call assert_equal('hello', line)
816
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200817 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200818 \ line: &lines,
819 \ col: 10,
820 \ minwidth: 20,
821 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200822 \})
823 redraw
824 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
825 call assert_match('.*on the command line.*', line)
826
827 sleep 700m
828 redraw
829 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
830 call assert_notmatch('.*on the command line.*', line)
831
832 bwipe!
833endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200834
835func Test_popup_hide()
836 topleft vnew
837 call setline(1, 'hello')
838
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200839 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200840 \ line: 1,
841 \ col: 1,
842 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200843 \})
844 redraw
845 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
846 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200847 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200848 " buffer is still listed and active
849 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200850
851 call popup_hide(winid)
852 redraw
853 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
854 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200855 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200856 " buffer is still listed but hidden
857 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200858
859 call popup_show(winid)
860 redraw
861 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
862 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200863 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200864
865
866 call popup_close(winid)
867 redraw
868 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
869 call assert_equal('hello', line)
870
871 " error is given for existing non-popup window
872 call assert_fails('call popup_hide(win_getid())', 'E993:')
873
874 " no error non-existing window
875 call popup_hide(1234234)
876 call popup_show(41234234)
877
878 bwipe!
879endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200880
881func Test_popup_move()
882 topleft vnew
883 call setline(1, 'hello')
884
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200885 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200886 \ line: 1,
887 \ col: 1,
888 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200889 \})
890 redraw
891 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
892 call assert_equal('world ', line)
893
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200894 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200895 redraw
896 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
897 call assert_equal('hello ', line)
898 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
899 call assert_equal('~world', line)
900
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200901 call popup_move(winid, #{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200902 redraw
903 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
904 call assert_equal('hworld', line)
905
906 call popup_close(winid)
907
908 bwipe!
909endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200910
Bram Moolenaar402502d2019-05-30 22:07:36 +0200911func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200912 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200913 \ line: 2,
914 \ col: 3,
915 \ minwidth: 10,
916 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200917 \})
918 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200919 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200920 call assert_equal(2, res.line)
921 call assert_equal(3, res.col)
922 call assert_equal(10, res.width)
923 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200924 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200925
926 call popup_close(winid)
927endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200928
929func Test_popup_width_longest()
930 let tests = [
931 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
932 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
933 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
934 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
935 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
936 \ ]
937
938 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200939 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200940 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200941 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200942 call assert_equal(test[1], position.width)
943 call popup_close(winid)
944 endfor
945endfunc
946
947func Test_popup_wraps()
948 let tests = [
949 \ ['nowrap', 6, 1],
950 \ ['a line that wraps once', 12, 2],
951 \ ['a line that wraps two times', 12, 3],
952 \ ]
953 for test in tests
954 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200955 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200956 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200957 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200958 call assert_equal(test[1], position.width)
959 call assert_equal(test[2], position.height)
960
961 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200962 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200963 endfor
964endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200965
966func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200967 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200968 \ line: 2,
969 \ col: 3,
970 \ minwidth: 10,
971 \ minheight: 11,
972 \ maxwidth: 20,
973 \ maxheight: 21,
974 \ zindex: 100,
975 \ time: 5000,
976 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200977 \})
978 redraw
979 let res = popup_getoptions(winid)
980 call assert_equal(2, res.line)
981 call assert_equal(3, res.col)
982 call assert_equal(10, res.minwidth)
983 call assert_equal(11, res.minheight)
984 call assert_equal(20, res.maxwidth)
985 call assert_equal(21, res.maxheight)
986 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200987 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +0200988 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200989 if has('timers')
990 call assert_equal(5000, res.time)
991 endif
992 call popup_close(winid)
993
994 let winid = popup_create('hello', {})
995 redraw
996 let res = popup_getoptions(winid)
997 call assert_equal(0, res.line)
998 call assert_equal(0, res.col)
999 call assert_equal(0, res.minwidth)
1000 call assert_equal(0, res.minheight)
1001 call assert_equal(0, res.maxwidth)
1002 call assert_equal(0, res.maxheight)
1003 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001004 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001005 if has('timers')
1006 call assert_equal(0, res.time)
1007 endif
1008 call popup_close(winid)
1009 call assert_equal({}, popup_getoptions(winid))
1010endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001011
1012func Test_popup_option_values()
1013 new
1014 " window-local
1015 setlocal number
1016 setlocal nowrap
1017 " buffer-local
1018 setlocal omnifunc=Something
1019 " global/buffer-local
1020 setlocal path=/there
1021 " global/window-local
1022 setlocal scrolloff=9
1023
1024 let winid = popup_create('hello', {})
1025 call assert_equal(0, getwinvar(winid, '&number'))
1026 call assert_equal(1, getwinvar(winid, '&wrap'))
1027 call assert_equal('', getwinvar(winid, '&omnifunc'))
1028 call assert_equal(&g:path, getwinvar(winid, '&path'))
1029 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
1030
1031 call popup_close(winid)
1032 bwipe
1033endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001034
1035func Test_popup_atcursor()
1036 topleft vnew
1037 call setline(1, [
1038 \ 'xxxxxxxxxxxxxxxxx',
1039 \ 'xxxxxxxxxxxxxxxxx',
1040 \ 'xxxxxxxxxxxxxxxxx',
1041 \])
1042
1043 call cursor(2, 2)
1044 redraw
1045 let winid = popup_atcursor('vim', {})
1046 redraw
1047 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1048 call assert_equal('xvimxxxxxxxxxxxxx', line)
1049 call popup_close(winid)
1050
1051 call cursor(3, 4)
1052 redraw
1053 let winid = popup_atcursor('vim', {})
1054 redraw
1055 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1056 call assert_equal('xxxvimxxxxxxxxxxx', line)
1057 call popup_close(winid)
1058
1059 call cursor(1, 1)
1060 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001061 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001062 \ line: 'cursor+2',
1063 \ col: 'cursor+1',
1064 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001065 redraw
1066 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1067 call assert_equal('xvimxxxxxxxxxxxxx', line)
1068 call popup_close(winid)
1069
1070 call cursor(3, 3)
1071 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001072 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001073 \ line: 'cursor-2',
1074 \ col: 'cursor-1',
1075 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001076 redraw
1077 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1078 call assert_equal('xvimxxxxxxxxxxxxx', line)
1079 call popup_close(winid)
1080
Bram Moolenaar402502d2019-05-30 22:07:36 +02001081 " just enough room above
1082 call cursor(3, 3)
1083 redraw
1084 let winid = popup_atcursor(['vim', 'is great'], {})
1085 redraw
1086 let pos = popup_getpos(winid)
1087 call assert_equal(1, pos.line)
1088 call popup_close(winid)
1089
1090 " not enough room above, popup goes below the cursor
1091 call cursor(3, 3)
1092 redraw
1093 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1094 redraw
1095 let pos = popup_getpos(winid)
1096 call assert_equal(4, pos.line)
1097 call popup_close(winid)
1098
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001099 " cursor in first line, popup in line 2
1100 call cursor(1, 1)
1101 redraw
1102 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1103 redraw
1104 let pos = popup_getpos(winid)
1105 call assert_equal(2, pos.line)
1106 call popup_close(winid)
1107
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001108 bwipe!
1109endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001110
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001111func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001112 CheckScreendump
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001113
1114 let lines =<< trim END
1115 call setline(1, range(1, 20))
1116 call setline(5, 'here is some text to hover over')
1117 set balloonevalterm
1118 set balloonexpr=BalloonExpr()
1119 set balloondelay=100
1120 func BalloonExpr()
1121 let s:winid = popup_beval([v:beval_text], {})
1122 return ''
1123 endfunc
1124 func Hover()
1125 call test_setmouse(5, 15)
1126 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1127 sleep 100m
1128 endfunc
1129 func MoveOntoPopup()
1130 call test_setmouse(4, 17)
1131 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1132 endfunc
1133 func MoveAway()
1134 call test_setmouse(5, 13)
1135 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1136 endfunc
1137 END
1138 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001139 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001140 call term_wait(buf, 100)
1141 call term_sendkeys(buf, 'j')
1142 call term_sendkeys(buf, ":call Hover()\<CR>")
1143 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1144
1145 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1146 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1147
1148 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1149 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1150
1151 " clean up
1152 call StopVimInTerminal(buf)
1153 call delete('XtestPopupBeval')
1154endfunc
1155
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001156func Test_popup_filter()
1157 new
1158 call setline(1, 'some text')
1159
1160 func MyPopupFilter(winid, c)
1161 if a:c == 'e'
1162 let g:eaten = 'e'
1163 return 1
1164 endif
1165 if a:c == '0'
1166 let g:ignored = '0'
1167 return 0
1168 endif
1169 if a:c == 'x'
1170 call popup_close(a:winid)
1171 return 1
1172 endif
1173 return 0
1174 endfunc
1175
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001176 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001177 redraw
1178
1179 " e is consumed by the filter
1180 call feedkeys('e', 'xt')
1181 call assert_equal('e', g:eaten)
1182
1183 " 0 is ignored by the filter
1184 normal $
1185 call assert_equal(9, getcurpos()[2])
1186 call feedkeys('0', 'xt')
1187 call assert_equal('0', g:ignored)
1188 call assert_equal(1, getcurpos()[2])
1189
1190 " x closes the popup
1191 call feedkeys('x', 'xt')
1192 call assert_equal('e', g:eaten)
1193 call assert_equal(-1, winbufnr(winid))
1194
1195 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001196 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001197endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001198
Bram Moolenaara42d9452019-06-15 21:46:30 +02001199func ShowDialog(key, result)
1200 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001201 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001202 \ filter: 'popup_filter_yesno',
1203 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001204 \ })
1205 redraw
1206 call feedkeys(a:key, "xt")
1207 call assert_equal(winid, s:cb_winid)
1208 call assert_equal(a:result, s:cb_res)
1209endfunc
1210
1211func Test_popup_dialog()
1212 func QuitCallback(id, res)
1213 let s:cb_winid = a:id
1214 let s:cb_res = a:res
1215 endfunc
1216
1217 let winid = ShowDialog("y", 1)
1218 let winid = ShowDialog("Y", 1)
1219 let winid = ShowDialog("n", 0)
1220 let winid = ShowDialog("N", 0)
1221 let winid = ShowDialog("x", 0)
1222 let winid = ShowDialog("X", 0)
1223 let winid = ShowDialog("\<Esc>", 0)
1224 let winid = ShowDialog("\<C-C>", -1)
1225
1226 delfunc QuitCallback
1227endfunc
1228
Bram Moolenaara730e552019-06-16 19:05:31 +02001229func ShowMenu(key, result)
1230 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001231 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001232 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001233 \ })
1234 redraw
1235 call feedkeys(a:key, "xt")
1236 call assert_equal(winid, s:cb_winid)
1237 call assert_equal(a:result, s:cb_res)
1238endfunc
1239
1240func Test_popup_menu()
1241 func QuitCallback(id, res)
1242 let s:cb_winid = a:id
1243 let s:cb_res = a:res
1244 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001245 " mapping won't be used in popup
1246 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001247
1248 let winid = ShowMenu(" ", 1)
1249 let winid = ShowMenu("j \<CR>", 2)
1250 let winid = ShowMenu("JjK \<CR>", 2)
1251 let winid = ShowMenu("jjjjjj ", 3)
1252 let winid = ShowMenu("kkk ", 1)
1253 let winid = ShowMenu("x", -1)
1254 let winid = ShowMenu("X", -1)
1255 let winid = ShowMenu("\<Esc>", -1)
1256 let winid = ShowMenu("\<C-C>", -1)
1257
1258 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001259 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001260endfunc
1261
1262func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001263 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001264
1265 let lines =<< trim END
1266 call setline(1, range(1, 20))
1267 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001268 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001269 func MenuDone(id, res)
1270 echomsg "selected " .. a:res
1271 endfunc
1272 END
1273 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001274 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001275 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1276
1277 call term_sendkeys(buf, "jj")
1278 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1279
1280 call term_sendkeys(buf, " ")
1281 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1282
1283 " clean up
1284 call StopVimInTerminal(buf)
1285 call delete('XtestPopupMenu')
1286endfunc
1287
Bram Moolenaarf914a332019-07-20 15:09:56 +02001288func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001289 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001290
1291 let lines =<< trim END
1292 call setline(1, range(1, 20))
1293 hi PopupSelected ctermbg=green
1294 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1295 func MenuDone(id, res)
1296 echomsg "selected " .. a:res
1297 endfunc
1298 END
1299 call writefile(lines, 'XtestPopupNarrowMenu')
1300 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1301 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1302
1303 " clean up
1304 call term_sendkeys(buf, "x")
1305 call StopVimInTerminal(buf)
1306 call delete('XtestPopupNarrowMenu')
1307endfunc
1308
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001309func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001310 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001311
1312 " Create a popup without title or border, a line of padding will be added to
1313 " put the title on.
1314 let lines =<< trim END
1315 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001316 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001317 END
1318 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001319 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001320 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1321
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001322 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1323 call term_sendkeys(buf, ":\<CR>")
1324 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1325
1326 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1327 call term_sendkeys(buf, ":\<CR>")
1328 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1329
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001330 " clean up
1331 call StopVimInTerminal(buf)
1332 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001333
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001334 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001335 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001336 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001337 call assert_equal('Another Title', popup_getoptions(winid).title)
1338
1339 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001340endfunc
1341
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001342func Test_popup_close_callback()
1343 func PopupDone(id, result)
1344 let g:result = a:result
1345 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001346 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001347 redraw
1348 call popup_close(winid, 'done')
1349 call assert_equal('done', g:result)
1350endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001351
1352func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001353 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001354 redraw
1355 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001356 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001357 call assert_equal(5, pos.height)
1358
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001359 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001360 redraw
1361 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001362 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001363 call assert_equal(3, pos.height)
1364endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001365
1366func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001367 CheckScreendump
1368
Bram Moolenaar988c4332019-06-02 14:12:11 +02001369 " +-----------------------------+
1370 " | | |
1371 " | | |
1372 " | | |
1373 " | line1 |
1374 " |------------line2------------|
1375 " | line3 |
1376 " | line4 |
1377 " | |
1378 " | |
1379 " +-----------------------------+
1380 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001381 split
1382 vsplit
1383 let info_window1 = getwininfo()[0]
1384 let line = info_window1['height']
1385 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001386 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001387 \ line : line,
1388 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001389 \ })
1390 END
1391 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001392 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001393 call term_sendkeys(buf, "\<C-W>w")
1394 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1395
1396 " clean up
1397 call StopVimInTerminal(buf)
1398 call delete('XtestPopupBehind')
1399endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001400
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001401func s:VerifyPosition(p, msg, line, col, width, height)
1402 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1403 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1404 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1405 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001406endfunc
1407
1408func Test_popup_position_adjust()
1409 " Anything placed past 2 cells from of the right of the screen is moved to the
1410 " left.
1411 "
1412 " When wrapping is disabled, we also shift to the left to display on the
1413 " screen, unless fixed is set.
1414
1415 " Entries for cases which don't vary based on wrapping.
1416 " Format is per tests described below
1417 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001418 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1419 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1420 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1421 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1422 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1423 \
1424 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1425 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1426 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1427 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1428 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1429 \
1430 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1431 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1432 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1433 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1434 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1435 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001436
1437 " these test groups are dicts with:
1438 " - comment: something to identify the group of tests by
1439 " - options: dict of options to merge with the row/col in tests
1440 " - tests: list of cases. Each one is a list with elements:
1441 " - text
1442 " - row
1443 " - col
1444 " - expected row
1445 " - expected col
1446 " - expected width
1447 " - expected height
1448 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001449 \ #{
1450 \ comment: 'left-aligned with wrapping',
1451 \ options: #{
1452 \ wrap: 1,
1453 \ pos: 'botleft',
1454 \ },
1455 \ tests: both_wrap_tests + [
1456 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1457 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1458 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1459 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1460 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1461 \ ],
1462 \ },
1463 \ #{
1464 \ comment: 'left aligned without wrapping',
1465 \ options: #{
1466 \ wrap: 0,
1467 \ pos: 'botleft',
1468 \ },
1469 \ tests: both_wrap_tests + [
1470 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1471 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1472 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1473 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1474 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1475 \ ],
1476 \ },
1477 \ #{
1478 \ comment: 'left aligned with fixed position',
1479 \ options: #{
1480 \ wrap: 0,
1481 \ fixed: 1,
1482 \ pos: 'botleft',
1483 \ },
1484 \ tests: both_wrap_tests + [
1485 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1486 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1487 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1488 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1489 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1490 \ ],
1491 \ },
1492 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001493
1494 for test_group in tests
1495 for test in test_group.tests
1496 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001497 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001498 \ line: line,
1499 \ col: col,
1500 \ }
1501 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001502
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001503 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001504
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001505 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001506 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1507 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001508 endfor
1509 endfor
1510
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001511 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001512 %bwipe!
1513endfunc
1514
Bram Moolenaar3397f742019-06-02 18:40:06 +02001515func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001516 " width of screen
1517 let X = join(map(range(&columns), {->'X'}), '')
1518
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001519 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1520 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001521
1522 redraw
1523 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1524 call assert_equal(X, line)
1525
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001526 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001527 redraw
1528
1529 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001530 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1531 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001532
1533 redraw
1534 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1535 call assert_equal(X, line)
1536
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001537 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001538 redraw
1539
1540 " Extend so > window width
1541 let X .= 'x'
1542
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001543 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1544 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001545
1546 redraw
1547 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1548 call assert_equal(X[ : -2 ], line)
1549
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001550 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001551 redraw
1552
1553 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001554 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1555 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001556
1557 redraw
1558 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1559 call assert_equal(X[ : -2 ], line)
1560
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001561 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001562 redraw
1563
1564 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001565 let p = popup_create(X,
1566 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1567 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001568
1569 redraw
1570 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1571 let e_line = ' ' . X[ 1 : -2 ]
1572 call assert_equal(e_line, line)
1573
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001574 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001575 redraw
1576
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001577 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001578 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001579endfunc
1580
1581func Test_popup_moved()
1582 new
1583 call test_override('char_avail', 1)
1584 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1585
1586 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001587 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001588 redraw
1589 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001590 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001591 " trigger the check for last_cursormoved by going into insert mode
1592 call feedkeys("li\<Esc>", 'xt')
1593 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001594 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001595
1596 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001597 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001598 redraw
1599 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001600 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001601 call feedkeys("hi\<Esc>", 'xt')
1602 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001603 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001604
1605 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001606 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001607 redraw
1608 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001609 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001610 call feedkeys("li\<Esc>", 'xt')
1611 call assert_equal(1, popup_getpos(winid).visible)
1612 call feedkeys("ei\<Esc>", 'xt')
1613 call assert_equal(1, popup_getpos(winid).visible)
1614 call feedkeys("eli\<Esc>", 'xt')
1615 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001616 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001617
Bram Moolenaar17627312019-06-02 19:53:44 +02001618 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001619 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001620 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001621 redraw
1622 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001623 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001624 call feedkeys("eli\<Esc>", 'xt')
1625 call assert_equal(1, popup_getpos(winid).visible)
1626 call feedkeys("wi\<Esc>", 'xt')
1627 call assert_equal(1, popup_getpos(winid).visible)
1628 call feedkeys("Eli\<Esc>", 'xt')
1629 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001630 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001631
1632 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001633 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001634 redraw
1635 call assert_equal(1, popup_getpos(winid).visible)
1636 call feedkeys("eli\<Esc>", 'xt')
1637 call feedkeys("ei\<Esc>", 'xt')
1638 call assert_equal(1, popup_getpos(winid).visible)
1639 call feedkeys("eli\<Esc>", 'xt')
1640 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001641 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001642
1643 bwipe!
1644 call test_override('ALL', 0)
1645endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001646
1647func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001648 CheckFeature timers
1649 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001650
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001651 let lines =<< trim END
1652 call setline(1, range(1, 20))
1653 hi Notification ctermbg=lightblue
1654 call popup_notification('first notification', {})
1655 END
1656 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001657 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001658 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1659
1660 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001661 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1662 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001663 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1664
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001665 " clean up
1666 call StopVimInTerminal(buf)
1667 call delete('XtestNotifications')
1668endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001669
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001670func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001671 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001672
1673 let lines =<< trim END
1674 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001675 hi ScrollThumb ctermbg=blue
1676 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001677 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001678 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001679 \ minwidth: 8,
1680 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001681 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001682 func ScrollUp()
1683 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1684 endfunc
1685 func ScrollDown()
1686 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1687 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001688 func ClickTop()
1689 call feedkeys("\<F4>\<LeftMouse>", "xt")
1690 endfunc
1691 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001692 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001693 call feedkeys("\<F5>\<LeftMouse>", "xt")
1694 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001695 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001696 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1697 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001698 END
1699 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001700 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001701 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1702
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001703 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<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_2', {})
1706
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001707 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<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_3', {})
1710
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001711 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001712 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001713 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1714
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02001715 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001716 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1717 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1718
1719 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1720 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1721
1722 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001723 " wait a bit, otherwise it fails sometimes (double click recognized?)
1724 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001725 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1726 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1727
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001728 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1729 sleep 100m
1730 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1731 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1732
1733 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1734 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1735
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001736 " remove the minwidth and maxheight
1737 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
1738 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
1739
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001740 " clean up
1741 call StopVimInTerminal(buf)
1742 call delete('XtestPopupScroll')
1743endfunc
1744
Bram Moolenaar437a7462019-07-05 20:17:22 +02001745func Test_popup_fitting_scrollbar()
1746 " this was causing a crash, divide by zero
1747 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001748 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001749 \ scrollbar: 1,
1750 \ maxwidth: 10,
1751 \ maxheight: 5,
1752 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001753 redraw
1754 call popup_clear()
1755endfunc
1756
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001757func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001758 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001759
1760 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001761 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001762 let p = popup_create('test', opts)
1763 call popup_settext(p, 'this is a text')
1764 END
1765
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001766 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001767 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001768 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1769
1770 " Setting to empty string clears it
1771 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1772 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1773
1774 " Setting 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 " Shrinking with a list
1779 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1780 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1781
1782 " Growing with a list
1783 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1784 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1785
1786 " Empty list clears
1787 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1788 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1789
1790 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001791 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001792 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1793
1794 " clean up
1795 call StopVimInTerminal(buf)
1796 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001797endfunc
1798
1799func Test_popup_hidden()
1800 new
1801
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001802 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001803 redraw
1804 call assert_equal(0, popup_getpos(winid).visible)
1805 call popup_close(winid)
1806
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001807 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001808 redraw
1809 call assert_equal(0, popup_getpos(winid).visible)
1810 call popup_close(winid)
1811
1812 func QuitCallback(id, res)
1813 let s:cb_winid = a:id
1814 let s:cb_res = a:res
1815 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001816 let winid = popup_dialog('make a choice', #{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001817 \ filter: 'popup_filter_yesno',
1818 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001819 \ })
1820 redraw
1821 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001822 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1823 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001824 exe "normal anot used by filter\<Esc>"
1825 call assert_equal('not used by filter', getline(1))
1826
1827 call popup_show(winid)
1828 call feedkeys('y', "xt")
1829 call assert_equal(1, s:cb_res)
1830
1831 bwipe!
1832 delfunc QuitCallback
1833endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001834
1835" Test options not checked elsewhere
1836func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001837 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001838 let options = popup_getoptions(winid)
1839 call assert_equal(1, options.wrap)
1840 call assert_equal(0, options.drag)
1841 call assert_equal('Beautiful', options.highlight)
1842
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001843 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001844 let options = popup_getoptions(winid)
1845 call assert_equal(0, options.wrap)
1846 call assert_equal(1, options.drag)
1847 call assert_equal('Another', options.highlight)
1848
1849 call popup_close(winid)
1850endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001851
1852func Test_popupwin_garbage_collect()
1853 func MyPopupFilter(x, winid, c)
1854 " NOP
1855 endfunc
1856
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001857 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001858 call test_garbagecollect_now()
1859 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001860 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001861 call feedkeys('j', 'xt')
1862 call assert_true(v:true)
1863
1864 call popup_close(winid)
1865 delfunc MyPopupFilter
1866endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001867
1868func Test_popupwin_with_buffer()
1869 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1870 let buf = bufadd('XsomeFile')
1871 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02001872
1873 setlocal number
1874 call setbufvar(buf, "&wrapmargin", 13)
1875
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001876 let winid = popup_create(buf, {})
1877 call assert_notequal(0, winid)
1878 let pos = popup_getpos(winid)
1879 call assert_equal(2, pos.height)
1880 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02001881
1882 " window-local option is set to default, buffer-local is not
1883 call assert_equal(0, getwinvar(winid, '&number'))
1884 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
1885
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001886 call popup_close(winid)
1887 call assert_equal({}, popup_getpos(winid))
1888 call assert_equal(1, bufloaded(buf))
1889 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02001890 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02001891
1892 edit test_popupwin.vim
1893 let winid = popup_create(bufnr(''), {})
1894 redraw
1895 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001896 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001897endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02001898
1899func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001900 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001901 \ maxwidth: 40,
1902 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02001903 \ })
1904 for top in range(1, 20)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001905 call popup_setoptions(winid, #{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02001906 redraw
1907 call assert_equal(19, popup_getpos(winid).width)
1908 endfor
1909 call popup_clear()
1910endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02001911
1912func Test_popupwin_buf_close()
1913 let buf = bufadd('Xtestbuf')
1914 call bufload(buf)
1915 call setbufline(buf, 1, ['just', 'some', 'lines'])
1916 let winid = popup_create(buf, {})
1917 redraw
1918 call assert_equal(3, popup_getpos(winid).height)
1919 let bufinfo = getbufinfo(buf)[0]
1920 call assert_equal(1, bufinfo.changed)
1921 call assert_equal(0, bufinfo.hidden)
1922 call assert_equal(0, bufinfo.listed)
1923 call assert_equal(1, bufinfo.loaded)
1924 call assert_equal([], bufinfo.windows)
1925 call assert_equal([winid], bufinfo.popups)
1926
1927 call popup_close(winid)
1928 call assert_equal({}, popup_getpos(winid))
1929 let bufinfo = getbufinfo(buf)[0]
1930 call assert_equal(1, bufinfo.changed)
1931 call assert_equal(1, bufinfo.hidden)
1932 call assert_equal(0, bufinfo.listed)
1933 call assert_equal(1, bufinfo.loaded)
1934 call assert_equal([], bufinfo.windows)
1935 call assert_equal([], bufinfo.popups)
1936 exe 'bwipe! ' .. buf
1937endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02001938
1939func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001940 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02001941
1942 let lines =<< trim END
1943 call setline(1, range(1, 10))
1944 hi ScrollThumb ctermbg=blue
1945 hi ScrollBar ctermbg=red
1946 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001947 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001948 \ maxwidth: 10,
1949 \ maxheight: 3,
1950 \ pos : 'topleft',
1951 \ col : a:col,
1952 \ line : a:line,
1953 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02001954 \ })
1955 endfunc
1956 call PopupMenu(['x'], 1, 1)
1957 call PopupMenu(['123456789|'], 1, 16)
1958 call PopupMenu(['123456789|' .. ' '], 7, 1)
1959 call PopupMenu([repeat('123456789|', 100)], 7, 16)
1960 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
1961 END
1962 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001963 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02001964 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
1965
1966 " close the menu popupwin.
1967 call term_sendkeys(buf, " ")
1968 call term_sendkeys(buf, " ")
1969 call term_sendkeys(buf, " ")
1970 call term_sendkeys(buf, " ")
1971 call term_sendkeys(buf, " ")
1972
1973 " clean up
1974 call StopVimInTerminal(buf)
1975 call delete('XtestPopupMenuMaxWidth')
1976endfunc
1977
Bram Moolenaara901a372019-07-13 16:38:50 +02001978func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001979 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02001980
1981 let lines =<< trim END
1982 call setline(1, range(1, 20))
1983 hi ScrollThumb ctermbg=blue
1984 hi ScrollBar ctermbg=red
1985 call popup_menu(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001986 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001987 \ minwidth: 8,
1988 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02001989 \ })
1990 END
1991 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001992 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02001993
1994 call term_sendkeys(buf, "j")
1995 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
1996
1997 call term_sendkeys(buf, "jjj")
1998 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
1999
2000 " if the cursor is the bottom line, it stays at the bottom line.
2001 call term_sendkeys(buf, repeat("j", 20))
2002 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2003
2004 call term_sendkeys(buf, "kk")
2005 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2006
2007 call term_sendkeys(buf, "k")
2008 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2009
2010 " if the cursor is in the top line, it stays in the top line.
2011 call term_sendkeys(buf, repeat("k", 20))
2012 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2013
2014 " close the menu popupwin.
2015 call term_sendkeys(buf, " ")
2016
2017 " clean up
2018 call StopVimInTerminal(buf)
2019 call delete('XtestPopupMenuScroll')
2020endfunc
2021
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002022func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002023 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002024
2025 let lines =<< trim END
2026 function! MyFilter(winid, key) abort
2027 if a:key == "0"
2028 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2029 return 1
2030 endif
2031 if a:key == "G"
2032 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2033 return 1
2034 endif
2035 if a:key == "j"
2036 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2037 return 1
2038 endif
2039 if a:key == "k"
2040 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2041 return 1
2042 endif
2043 if a:key == 'x'
2044 call popup_close(a:winid)
2045 return 1
2046 endif
2047 return 0
2048 endfunction
2049 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2050 \ maxheight : 3,
2051 \ filter : 'MyFilter'
2052 \ })
2053 END
2054 call writefile(lines, 'XtestPopupMenuFilter')
2055 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2056
2057 call term_sendkeys(buf, "j")
2058 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2059
2060 call term_sendkeys(buf, "k")
2061 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2062
2063 call term_sendkeys(buf, "G")
2064 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2065
2066 call term_sendkeys(buf, "0")
2067 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2068
2069 call term_sendkeys(buf, "x")
2070
2071 " clean up
2072 call StopVimInTerminal(buf)
2073 call delete('XtestPopupMenuFilter')
2074endfunc
2075
2076func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002077 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002078
2079 let winid = popup_create('some text', {})
2080 call assert_equal(0, popup_getoptions(winid).cursorline)
2081 call popup_close(winid)
2082
2083 let winid = popup_create('some text', #{ cursorline: 1, })
2084 call assert_equal(1, popup_getoptions(winid).cursorline)
2085 call popup_close(winid)
2086
2087 let winid = popup_create('some text', #{ cursorline: 0, })
2088 call assert_equal(0, popup_getoptions(winid).cursorline)
2089 call popup_close(winid)
2090
2091 let winid = popup_menu('some text', {})
2092 call assert_equal(1, popup_getoptions(winid).cursorline)
2093 call popup_close(winid)
2094
2095 let winid = popup_menu('some text', #{ cursorline: 1, })
2096 call assert_equal(1, popup_getoptions(winid).cursorline)
2097 call popup_close(winid)
2098
2099 let winid = popup_menu('some text', #{ cursorline: 0, })
2100 call assert_equal(0, popup_getoptions(winid).cursorline)
2101 call popup_close(winid)
2102
2103 " ---------
2104 " Pattern 1
2105 " ---------
2106 let lines =<< trim END
2107 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2108 END
2109 call writefile(lines, 'XtestPopupCursorLine')
2110 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2111 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2112 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2113 call StopVimInTerminal(buf)
2114
2115 " ---------
2116 " Pattern 2
2117 " ---------
2118 let lines =<< trim END
2119 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2120 END
2121 call writefile(lines, 'XtestPopupCursorLine')
2122 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2123 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2124 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2125 call StopVimInTerminal(buf)
2126
2127 " ---------
2128 " Pattern 3
2129 " ---------
2130 let lines =<< trim END
2131 function! MyFilter(winid, key) abort
2132 if a:key == "j"
2133 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2134 return 1
2135 endif
2136 if a:key == 'x'
2137 call popup_close(a:winid)
2138 return 1
2139 endif
2140 return 0
2141 endfunction
2142 call popup_menu(['111', '222', '333'], #{
2143 \ cursorline : 0,
2144 \ maxheight : 2,
2145 \ filter : 'MyFilter',
2146 \ })
2147 END
2148 call writefile(lines, 'XtestPopupCursorLine')
2149 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2150 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2151 call term_sendkeys(buf, "j")
2152 call term_sendkeys(buf, "j")
2153 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2154 call term_sendkeys(buf, "x")
2155 call StopVimInTerminal(buf)
2156
2157 " ---------
2158 " Pattern 4
2159 " ---------
2160 let lines =<< trim END
2161 function! MyFilter(winid, key) abort
2162 if a:key == "j"
2163 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2164 return 1
2165 endif
2166 if a:key == 'x'
2167 call popup_close(a:winid)
2168 return 1
2169 endif
2170 return 0
2171 endfunction
2172 call popup_menu(['111', '222', '333'], #{
2173 \ cursorline : 1,
2174 \ maxheight : 2,
2175 \ filter : 'MyFilter',
2176 \ })
2177 END
2178 call writefile(lines, 'XtestPopupCursorLine')
2179 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2180 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2181 call term_sendkeys(buf, "j")
2182 call term_sendkeys(buf, "j")
2183 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2184 call term_sendkeys(buf, "x")
2185 call StopVimInTerminal(buf)
2186
2187 call delete('XtestPopupCursorLine')
2188endfunc
2189
Bram Moolenaarf914a332019-07-20 15:09:56 +02002190func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002191 CheckScreendump
2192
Bram Moolenaarf914a332019-07-20 15:09:56 +02002193 call writefile([
2194 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2195 \ "another\tXtagfile\t/^this is another",
2196 \ "theword\tXtagfile\t/^theword"],
2197 \ 'Xtags')
2198 call writefile(range(1,20)
2199 \ + ['theword is here']
2200 \ + range(22, 27)
2201 \ + ['this is another place']
2202 \ + range(29, 40),
2203 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002204 call writefile(range(1,10)
2205 \ + ['searched word is here']
2206 \ + range(12, 20),
2207 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002208 let lines =<< trim END
2209 set tags=Xtags
2210 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002211 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002212 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002213 \ 'three',
2214 \ 'four',
2215 \ 'five',
2216 \ 'six',
2217 \ 'seven',
2218 \ 'find theword somewhere',
2219 \ 'nine',
2220 \ 'this is another word',
2221 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002222 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002223 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002224 END
2225 call writefile(lines, 'XtestPreviewPopup')
2226 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2227
2228 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2229 call term_sendkeys(buf, ":\<CR>")
2230 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2231
2232 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2233 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2234
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002235 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002236 call term_sendkeys(buf, ":\<CR>")
2237 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2238
2239 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2240 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2241
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002242 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2243 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002244 call term_sendkeys(buf, ":cd testdir\<CR>")
2245
2246 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002247 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002248 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2249
2250 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2251 call term_sendkeys(buf, ":\<CR>")
2252 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2253
2254 call term_sendkeys(buf, ":pclose\<CR>")
2255 call term_sendkeys(buf, ":psearch searched\<CR>")
2256 call term_sendkeys(buf, ":\<CR>")
2257 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002258
Bram Moolenaarf914a332019-07-20 15:09:56 +02002259 call StopVimInTerminal(buf)
2260 call delete('Xtags')
2261 call delete('Xtagfile')
2262 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002263 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002264endfunc
2265
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002266func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002267 let lines =<< trim END
2268 set completeopt+=preview,popup
2269 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002270 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002271
2272 func CompleteFuncDict(findstart, base)
2273 if a:findstart
2274 if col('.') > 10
2275 return col('.') - 10
2276 endif
2277 return 0
2278 endif
2279
2280 return {
2281 \ 'words': [
2282 \ {
2283 \ 'word': 'aword',
2284 \ 'abbr': 'wrd',
2285 \ 'menu': 'extra text',
2286 \ 'info': 'words are cool',
2287 \ 'kind': 'W',
2288 \ 'user_data': 'test'
2289 \ },
2290 \ {
2291 \ 'word': 'anotherword',
2292 \ 'abbr': 'anotwrd',
2293 \ 'menu': 'extra text',
2294 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2295 \ 'kind': 'W',
2296 \ 'user_data': 'notest'
2297 \ },
2298 \ {
2299 \ 'word': 'noinfo',
2300 \ 'abbr': 'noawrd',
2301 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002302 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002303 \ 'kind': 'W',
2304 \ 'user_data': 'notest'
2305 \ },
2306 \ {
2307 \ 'word': 'thatword',
2308 \ 'abbr': 'thatwrd',
2309 \ 'menu': 'extra text',
2310 \ 'info': 'that word is cool',
2311 \ 'kind': 'W',
2312 \ 'user_data': 'notest'
2313 \ },
2314 \ ]
2315 \ }
2316 endfunc
2317 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002318 func ChangeColor()
2319 let id = popup_findinfo()
2320 call popup_setoptions(id, #{highlight: 'InfoPopup'})
2321 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002322 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002323 return lines
2324endfunc
2325
2326func Test_popupmenu_info_border()
2327 CheckScreendump
2328
2329 let lines = Get_popupmenu_lines()
2330 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002331 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002332
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002333 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2334 call term_wait(buf, 50)
2335
2336 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2337 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2338
2339 call term_sendkeys(buf, "\<C-N>")
2340 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2341
2342 call term_sendkeys(buf, "\<C-N>")
2343 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2344
2345 call term_sendkeys(buf, "\<C-N>\<C-N>")
2346 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2347
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002348 " info on the left with scrollbar
2349 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2350 call term_sendkeys(buf, "\<C-N>\<C-N>")
2351 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2352
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002353 call StopVimInTerminal(buf)
2354 call delete('XtestInfoPopup')
2355endfunc
2356
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002357func Test_popupmenu_info_noborder()
2358 CheckScreendump
2359
2360 let lines = Get_popupmenu_lines()
2361 call add(lines, 'set completepopup=height:4,border:off')
2362 call writefile(lines, 'XtestInfoPopupNb')
2363
2364 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2365 call term_wait(buf, 50)
2366
2367 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2368 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2369
2370 call StopVimInTerminal(buf)
2371 call delete('XtestInfoPopupNb')
2372endfunc
2373
Bram Moolenaar258cef52019-08-21 17:29:29 +02002374func Test_popupmenu_info_align_menu()
2375 CheckScreendump
2376
2377 let lines = Get_popupmenu_lines()
2378 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2379 call writefile(lines, 'XtestInfoPopupNb')
2380
2381 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2382 call term_wait(buf, 50)
2383
2384 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2385 call term_sendkeys(buf, "\<C-N>")
2386 call term_sendkeys(buf, "\<C-N>")
2387 call term_sendkeys(buf, "\<C-N>")
2388 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2389
2390 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2391 call term_sendkeys(buf, "\<C-N>")
2392 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2393
2394 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002395 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002396 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2397 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2398 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2399
2400 call StopVimInTerminal(buf)
2401 call delete('XtestInfoPopupNb')
2402endfunc
2403
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002404func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02002405 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002406 call popup_clear()
2407 let winid = popup_notification('nothing wrong', {})
2408 call assert_equal(bufnr, winbufnr(winid))
2409 call popup_clear()
2410endfunc
2411
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002412" vim: shiftwidth=2 sts=2