blob: 0a45df4e1fd742ff1202495003573ef41f07ed5e [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,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100145 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200146 \ scrollbar: 0,
147 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200148 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200149 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200150 let options = popup_getoptions(winid)
151 call assert_equal([], options.border)
152 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200153
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200154 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200155 let with_border_or_padding.width = 15
156 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200157 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200158 let options = popup_getoptions(winid)
159 call assert_false(has_key(options, "border"))
160 call assert_equal([], options.padding)
161
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200162 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200163 \ padding: [1, 2, 3, 4],
164 \ border: [4, 0, 7, 8],
165 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
166 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200167 \ })
168 let options = popup_getoptions(winid)
169 call assert_equal([1, 0, 1, 1], options.border)
170 call assert_equal([1, 2, 3, 4], options.padding)
171 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
172 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200173
Bram Moolenaara1396152019-10-20 18:46:05 +0200174 " Check that popup_setoptions() takes the output of popup_getoptions()
175 call popup_setoptions(winid, options)
176 call assert_equal(options, popup_getoptions(winid))
177
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200178 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
179 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200180 \ line: 3,
181 \ core_line: 5,
182 \ col: 8,
183 \ core_col: 10,
184 \ width: 14,
185 \ core_width: 10,
186 \ height: 5,
187 \ scrollbar: 0,
188 \ core_height: 1,
189 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100190 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200191 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200192
193 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200194endfunc
195
Bram Moolenaarb4230122019-05-30 18:40:53 +0200196func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200197 CheckScreendump
198
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200199 let lines =<< trim END
200 call setline(1, range(1, 100))
201 hi PopupColor ctermbg=lightblue
202 let winid = popup_create([
203 \ '#include <stdio.h>',
204 \ 'int main(void)',
205 \ '{',
206 \ ' printf(123);',
207 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200208 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200209 call win_execute(winid, 'set syntax=cpp')
210 END
211 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200212 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200213 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
214
215 " clean up
216 call StopVimInTerminal(buf)
217 call delete('XtestPopup')
218endfunc
219
220func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200221 CheckScreendump
222
Bram Moolenaar402502d2019-05-30 22:07:36 +0200223 let lines =<< trim END
224 call setline(1, range(1, 100))
225 hi PopupColor ctermbg=lightgrey
226 let winid = popup_create([
227 \ '#include <stdio.h>',
228 \ 'int main(void)',
229 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200230 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200231 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200232 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200233 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
234 END
235 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200236 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200237 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
238
239 " clean up
240 call StopVimInTerminal(buf)
241 call delete('XtestPopup')
242endfunc
243
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200244func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200245 CheckScreendump
246
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200247 let lines =<< trim END
248 call setline(1, ['111 222 333', '444 555 666'])
249 let winid = popup_create([
250 \ '111 222 333',
251 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200252 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200253 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100254 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200255 /666
256 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100257 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200258 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100259 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200260 END
261 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200262 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200263 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
264
265 " clean up
266 call StopVimInTerminal(buf)
267 call delete('XtestPopupMatches')
268endfunc
269
Bram Moolenaar399d8982019-06-02 15:34:29 +0200270func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200271 CheckScreendump
272
Bram Moolenaar399d8982019-06-02 15:34:29 +0200273 let lines =<< trim END
274 call setline(1, repeat([repeat('-', 60)], 15))
275 set so=0
276 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200277 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200278 \ line: 'cursor+1',
279 \ col: 'cursor',
280 \ pos: 'topleft',
281 \ border: [],
282 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200283 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200284 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200285 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200286 \ line: 'cursor+1',
287 \ col: 'cursor',
288 \ pos: 'topright',
289 \ border: [],
290 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200291 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200292 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200293 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200294 \ line: 'cursor-1',
295 \ col: 'cursor',
296 \ pos: 'botleft',
297 \ border: [],
298 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200299 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200300 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200301 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200302 \ line: 'cursor-1',
303 \ col: 'cursor',
304 \ pos: 'botright',
305 \ border: [],
306 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200307 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200308 normal 1G51|r*
309 let winid1 = popup_create(['one', 'two'], #{
310 \ line: 'cursor-1',
311 \ col: 'cursor',
312 \ pos: 'botleft',
313 \ border: [],
314 \ padding: [],
315 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200316 END
317 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200318 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200319 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
320
321 " clean up
322 call StopVimInTerminal(buf)
323 call delete('XtestPopupCorners')
324endfunc
325
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100326func Test_popup_nospace()
327 CheckScreendump
328
329 let lines =<< trim END
330 call setline(1, repeat([repeat('-', 60)], 15))
331 set so=0
332
333 " cursor in a line in top half, using "botleft" with popup that
334 " does fit
335 normal 5G2|r@
336 let winid1 = popup_create(['one', 'two'], #{
337 \ line: 'cursor-1',
338 \ col: 'cursor',
339 \ pos: 'botleft',
340 \ border: [],
341 \ })
342 " cursor in a line in top half, using "botleft" with popup that
343 " doesn't fit: gets truncated
344 normal 5G9|r#
345 let winid1 = popup_create(['one', 'two', 'tee'], #{
346 \ line: 'cursor-1',
347 \ col: 'cursor',
348 \ pos: 'botleft',
349 \ posinvert: 0,
350 \ border: [],
351 \ })
352 " cursor in a line in top half, using "botleft" with popup that
353 " doesn't fit and 'posinvert' set: flips to below.
354 normal 5G16|r%
355 let winid1 = popup_create(['one', 'two', 'tee'], #{
356 \ line: 'cursor-1',
357 \ col: 'cursor',
358 \ pos: 'botleft',
359 \ border: [],
360 \ })
361 " cursor in a line in bottom half, using "botleft" with popup that
362 " doesn't fit: does not flip.
363 normal 8G23|r*
364 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
365 \ line: 'cursor-1',
366 \ col: 'cursor',
367 \ pos: 'botleft',
368 \ border: [],
369 \ })
370
371 " cursor in a line in bottom half, using "topleft" with popup that
372 " does fit
373 normal 8G30|r@
374 let winid1 = popup_create(['one', 'two'], #{
375 \ line: 'cursor+1',
376 \ col: 'cursor',
377 \ pos: 'topleft',
378 \ border: [],
379 \ })
380 " cursor in a line in top half, using "topleft" with popup that
381 " doesn't fit: truncated
382 normal 8G37|r#
383 let winid1 = popup_create(['one', 'two', 'tee'], #{
384 \ line: 'cursor+1',
385 \ col: 'cursor',
386 \ pos: 'topleft',
387 \ posinvert: 0,
388 \ border: [],
389 \ })
390 " cursor in a line in top half, using "topleft" with popup that
391 " doesn't fit and "posinvert" set: flips to below.
392 normal 8G44|r%
393 let winid1 = popup_create(['one', 'two', 'tee'], #{
394 \ line: 'cursor+1',
395 \ col: 'cursor',
396 \ pos: 'topleft',
397 \ border: [],
398 \ })
399 " cursor in a line in top half, using "topleft" with popup that
400 " doesn't fit: does not flip.
401 normal 5G51|r*
402 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
403 \ line: 'cursor+1',
404 \ col: 'cursor',
405 \ pos: 'topleft',
406 \ border: [],
407 \ })
408 END
409 call writefile(lines, 'XtestPopupNospace')
410 let buf = RunVimInTerminal('-S XtestPopupNospace', #{rows: 12})
411 call VerifyScreenDump(buf, 'Test_popupwin_nospace', {})
412
413 " clean up
414 call StopVimInTerminal(buf)
415 call delete('XtestPopupNospace')
416endfunc
417
Bram Moolenaar8d241042019-06-12 23:40:01 +0200418func Test_popup_firstline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200419 CheckScreendump
420
Bram Moolenaar8d241042019-06-12 23:40:01 +0200421 let lines =<< trim END
422 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200423 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200424 \ maxheight: 4,
425 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200426 \ })
427 END
428 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200429 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200430 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
431
432 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
433 call term_sendkeys(buf, ":\<CR>")
434 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200435
436 " clean up
437 call StopVimInTerminal(buf)
438 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200439
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200440 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200441 \ maxheight: 2,
442 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200443 \ })
444 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200445 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200446 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200447 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200448
449 let winid = popup_create(['xxx']->repeat(50), #{
450 \ maxheight: 3,
451 \ firstline: 11,
452 \ })
453 redraw
454 call assert_equal(11, popup_getoptions(winid).firstline)
455 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200456 " check line() works with popup window
457 call assert_equal(11, line('.', winid))
458 call assert_equal(50, line('$', winid))
459 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200460
461 " Normal command changes what is displayed but not "firstline"
462 call win_execute(winid, "normal! \<c-y>")
463 call assert_equal(11, popup_getoptions(winid).firstline)
464 call assert_equal(10, popup_getpos(winid).firstline)
465
466 " Making some property change applies "firstline" again
467 call popup_setoptions(winid, #{line: 4})
468 call assert_equal(11, popup_getoptions(winid).firstline)
469 call assert_equal(11, popup_getpos(winid).firstline)
470
471 " Remove "firstline" property and scroll
472 call popup_setoptions(winid, #{firstline: 0})
473 call win_execute(winid, "normal! \<c-y>")
474 call assert_equal(0, popup_getoptions(winid).firstline)
475 call assert_equal(10, popup_getpos(winid).firstline)
476
477 " Making some property change has no side effect
478 call popup_setoptions(winid, #{line: 3})
479 call assert_equal(0, popup_getoptions(winid).firstline)
480 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaarae943152019-06-16 22:54:14 +0200481
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100482 " CTRL-D scrolls down half a page
483 let winid = popup_create(['xxx']->repeat(50), #{
484 \ maxheight: 8,
485 \ })
486 redraw
487 call assert_equal(1, popup_getpos(winid).firstline)
488 call win_execute(winid, "normal! \<C-D>")
489 call assert_equal(5, popup_getpos(winid).firstline)
490 call win_execute(winid, "normal! \<C-D>")
491 call assert_equal(9, popup_getpos(winid).firstline)
492 call win_execute(winid, "normal! \<C-U>")
493 call assert_equal(5, popup_getpos(winid).firstline)
494
495 call win_execute(winid, "normal! \<C-F>")
496 call assert_equal(11, popup_getpos(winid).firstline)
497 call win_execute(winid, "normal! \<C-B>")
498 call assert_equal(5, popup_getpos(winid).firstline)
499
Bram Moolenaarae943152019-06-16 22:54:14 +0200500 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200501endfunc
502
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200503func Test_popup_noscrolloff()
504 set scrolloff=5
505 let winid = popup_create(['xxx']->repeat(50), #{
506 \ maxheight: 5,
507 \ firstline: 11,
508 \ })
509 redraw
510 call assert_equal(11, popup_getoptions(winid).firstline)
511 call assert_equal(11, popup_getpos(winid).firstline)
512
513 call popup_setoptions(winid, #{firstline: 0})
514 call win_execute(winid, "normal! \<c-y>")
515 call assert_equal(0, popup_getoptions(winid).firstline)
516 call assert_equal(10, popup_getpos(winid).firstline)
517
518 call popup_close(winid)
519endfunc
520
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200521func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200522 CheckScreendump
523
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200524 " create a popup that covers the command line
525 let lines =<< trim END
526 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200527 split
528 vsplit
529 $wincmd w
530 vsplit
531 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200532 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200533 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200534 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200535 \ border: [],
536 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200537 \ })
538 func Dragit()
539 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
540 endfunc
541 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200542 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200543 func Resize()
544 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
545 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200546 map <silent> <F5> :call test_setmouse(6, 21)<CR>
547 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200548 END
549 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200550 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200551 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
552
553 call term_sendkeys(buf, ":call Dragit()\<CR>")
554 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
555
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200556 call term_sendkeys(buf, ":call Resize()\<CR>")
557 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
558
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200559 " clean up
560 call StopVimInTerminal(buf)
561 call delete('XtestPopupDrag')
562endfunc
563
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200564func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200565 CheckScreendump
566
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200567 let lines =<< trim END
568 call setline(1, range(1, 20))
569 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200570 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200571 \ close: 'button',
572 \ border: [],
573 \ line: 1,
574 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200575 \ })
576 func CloseMsg(id, result)
577 echomsg 'Popup closed with ' .. a:result
578 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200579 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200580 \ close: 'click',
581 \ line: 3,
582 \ col: 15,
583 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200584 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200585 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200586 \ close: 'button',
587 \ line: 5,
588 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200589 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200590 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200591 \ close: 'button',
592 \ padding: [],
593 \ line: 5,
594 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200595 \ })
596 func CloseWithX()
597 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
598 endfunc
599 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
600 func CloseWithClick()
601 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
602 endfunc
603 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200604 func CreateWithMenuFilter()
605 let winid = popup_create('barfoo', #{
606 \ close: 'button',
607 \ filter: 'popup_filter_menu',
608 \ border: [],
609 \ line: 1,
610 \ col: 40,
611 \ })
612 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200613 END
614 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200615 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200616 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
617
618 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
619 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
620
621 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
622 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
623
Bram Moolenaarf6396232019-08-24 19:36:00 +0200624 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
625 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
626
627 " We have to send the actual mouse code, feedkeys() would be caught the
628 " filter.
629 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
630 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
631
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200632 " clean up
633 call StopVimInTerminal(buf)
634 call delete('XtestPopupClose')
635endfunction
636
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200637func Test_popup_menu_wrap()
638 CheckScreendump
639
640 let lines =<< trim END
641 call setline(1, range(1, 20))
642 call popup_create([
643 \ 'one',
644 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
645 \ 'three',
646 \ 'four',
647 \ ], #{
648 \ pos: "botleft",
649 \ border: [],
650 \ padding: [0,1,0,1],
651 \ maxheight: 3,
652 \ cursorline: 1,
653 \ filter: 'popup_filter_menu',
654 \ })
655 END
656 call writefile(lines, 'XtestPopupWrap')
657 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
658 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
659
660 call term_sendkeys(buf, "jj")
661 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
662
663 " clean up
664 call term_sendkeys(buf, "\<Esc>")
665 call StopVimInTerminal(buf)
666 call delete('XtestPopupWrap')
667endfunction
668
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200669func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200670 CheckScreendump
671
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200672 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200673 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200674 hi PopupColor ctermbg=lightgrey
675 let winid = popup_create([
676 \ 'some text',
677 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200678 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200679 \ line: 1,
680 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100681 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200682 \ wrap: 0,
683 \ fixed: 1,
684 \ zindex: 90,
685 \ padding: [],
686 \ highlight: 'PopupColor',
687 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200688 call popup_create([
689 \ 'xxxxxxxxx',
690 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200691 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200692 \ line: 3,
693 \ col: 18,
694 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200695 let winidb = popup_create([
696 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200697 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200698 \ line: 7,
699 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100700 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200701 \ wrap: 0,
702 \ fixed: 1,
703 \ close: 'button',
704 \ zindex: 90,
705 \ padding: [],
706 \ border: [],
707 \ 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 +0200708 END
709 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200710 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200711 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
712
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200713 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
714 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200715 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200716 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
717
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200718 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
719 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200720 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200721 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
722
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200723 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
724 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200725 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200726 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
727
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200728 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
729 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200730 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200731 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
732
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200733 " clean up
734 call StopVimInTerminal(buf)
735 call delete('XtestPopupMask')
736endfunc
737
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200738func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200739 CheckScreendump
740 CheckFeature clipboard_working
741
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200742 " create a popup with some text to be selected
743 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200744 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200745 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200746 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200747 \ drag: 1,
748 \ border: [],
749 \ line: 3,
750 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200751 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200752 \ })
753 func Select1()
754 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
755 endfunc
756 map <silent> <F3> :call test_setmouse(4, 15)<CR>
757 map <silent> <F4> :call test_setmouse(6, 23)<CR>
758 END
759 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200760 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200761 call term_sendkeys(buf, ":call Select1()\<CR>")
762 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
763
764 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
765 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200766 " clean the command line, sometimes it still shows a command
767 call term_sendkeys(buf, ":\<esc>")
768
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200769 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
770
771 " clean up
772 call StopVimInTerminal(buf)
773 call delete('XtestPopupSelect')
774endfunc
775
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200776func Test_popup_in_tab()
777 " default popup is local to tab, not visible when in other tab
778 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200779 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200780 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200781 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200782 tabnew
783 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200784 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200785 quit
786 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200787
788 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200789 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200790 " buffer is gone now
791 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200792
793 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200794 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200795 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200796 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200797 tabnew
798 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200799 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200800 quit
801 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200802 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200803
804 " create popup in other tab
805 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200806 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200807 call assert_equal(0, popup_getpos(winid).visible)
808 call assert_equal(1, popup_getoptions(winid).tabpage)
809 quit
810 call assert_equal(1, popup_getpos(winid).visible)
811 call assert_equal(0, popup_getoptions(winid).tabpage)
812 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200813endfunc
814
815func Test_popup_valid_arguments()
816 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200817 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200818 let pos = popup_getpos(winid)
819 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200820 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200821
822 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200823 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200824 let pos = popup_getpos(winid)
825 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200826 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200827
828 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200829 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200830 let pos = popup_getpos(winid)
831 let around = (&columns - pos.width) / 2
832 call assert_inrange(around - 1, around + 1, pos.col)
833 let around = (&lines - pos.height) / 2
834 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200835 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200836endfunc
837
838func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200839 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200840 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200841 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200842 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200843
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200844 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200845 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200846 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200847 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200848 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200849 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200850 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200851 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200852
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200853 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200854 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200855 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200856 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200857 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200858 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200859 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200860 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200861
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200862 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200863 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200864 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200865 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200866 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200867 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200868 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200869 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200870 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
871 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200872 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200873 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200874
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200875 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200876 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200877 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200878 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200879 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200880 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200881 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
882 call popup_clear()
883 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200884 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200885 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200886endfunc
887
Bram Moolenaareea16992019-05-31 17:34:48 +0200888func Test_win_execute_closing_curwin()
889 split
890 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200891 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200892 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200893endfunc
894
895func Test_win_execute_not_allowed()
896 let winid = popup_create('some text', {})
897 call assert_fails('call win_execute(winid, "split")', 'E994:')
898 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
899 call assert_fails('call win_execute(winid, "close")', 'E994:')
900 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200901 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200902 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
903 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
904 call assert_fails('call win_execute(winid, "next")', 'E994:')
905 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
906 call assert_fails('call win_execute(winid, "buf")', 'E994:')
907 call assert_fails('call win_execute(winid, "edit")', 'E994:')
908 call assert_fails('call win_execute(winid, "enew")', 'E994:')
909 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
910 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
911 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
912 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200913 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200914endfunc
915
Bram Moolenaar402502d2019-05-30 22:07:36 +0200916func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200917 CheckScreendump
918
Bram Moolenaar402502d2019-05-30 22:07:36 +0200919 let lines =<< trim END
920 call setline(1, range(1, 100))
921 let winid = popup_create(
922 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200923 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200924 END
925 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200926 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200927 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
928
929 " clean up
930 call StopVimInTerminal(buf)
931 call delete('XtestPopup')
932endfunc
933
934func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200935 CheckScreendump
936
Bram Moolenaar402502d2019-05-30 22:07:36 +0200937 let lines =<< trim END
938 call setline(1, range(1, 100))
939 let winid = popup_create(
940 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200941 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200942 END
943 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200944 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200945 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
946
947 " clean up
948 call StopVimInTerminal(buf)
949 call delete('XtestPopup')
950endfunc
951
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200952func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200953 CheckScreendump
954
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200955 let lines =<< trim END
956 set showbreak=>>\
957 call setline(1, range(1, 20))
958 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +0200959 \ 'a long line here that wraps',
960 \ #{filter: 'popup_filter_yesno',
961 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200962 END
963 call writefile(lines, 'XtestPopupShowbreak')
964 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
965 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
966
967 " clean up
968 call term_sendkeys(buf, "y")
969 call StopVimInTerminal(buf)
970 call delete('XtestPopupShowbreak')
971endfunc
972
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200973func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200974 CheckFeature timers
975
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200976 topleft vnew
977 call setline(1, 'hello')
978
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200979 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200980 \ line: 1,
981 \ col: 1,
982 \ minwidth: 20,
983 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200984 \})
985 redraw
986 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
987 call assert_equal('world', line)
988
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200989 call assert_equal(winid, popup_locate(1, 1))
990 call assert_equal(winid, popup_locate(1, 20))
991 call assert_equal(0, popup_locate(1, 21))
992 call assert_equal(0, popup_locate(2, 1))
993
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200994 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200995 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +0200996 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200997 call assert_equal('hello', line)
998
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200999 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001000 \ line: &lines,
1001 \ col: 10,
1002 \ minwidth: 20,
1003 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001004 \})
1005 redraw
1006 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1007 call assert_match('.*on the command line.*', line)
1008
1009 sleep 700m
1010 redraw
1011 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1012 call assert_notmatch('.*on the command line.*', line)
1013
1014 bwipe!
1015endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001016
1017func Test_popup_hide()
1018 topleft vnew
1019 call setline(1, 'hello')
1020
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001021 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001022 \ line: 1,
1023 \ col: 1,
1024 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001025 \})
1026 redraw
1027 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1028 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001029 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001030 " buffer is still listed and active
1031 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001032
1033 call popup_hide(winid)
1034 redraw
1035 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1036 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001037 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001038 " buffer is still listed but hidden
1039 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001040
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001041 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001042 redraw
1043 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1044 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001045 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001046
1047
1048 call popup_close(winid)
1049 redraw
1050 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1051 call assert_equal('hello', line)
1052
1053 " error is given for existing non-popup window
1054 call assert_fails('call popup_hide(win_getid())', 'E993:')
1055
1056 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001057 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001058 call popup_show(41234234)
1059
1060 bwipe!
1061endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001062
1063func Test_popup_move()
1064 topleft vnew
1065 call setline(1, 'hello')
1066
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001067 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001068 \ line: 1,
1069 \ col: 1,
1070 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001071 \})
1072 redraw
1073 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1074 call assert_equal('world ', line)
1075
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001076 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001077 redraw
1078 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1079 call assert_equal('hello ', line)
1080 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1081 call assert_equal('~world', line)
1082
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001083 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001084 redraw
1085 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1086 call assert_equal('hworld', line)
1087
1088 call popup_close(winid)
1089
1090 bwipe!
1091endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001092
Bram Moolenaar402502d2019-05-30 22:07:36 +02001093func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001094 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001095 \ line: 2,
1096 \ col: 3,
1097 \ minwidth: 10,
1098 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001099 \})
1100 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001101 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001102 call assert_equal(2, res.line)
1103 call assert_equal(3, res.col)
1104 call assert_equal(10, res.width)
1105 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001106 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001107
1108 call popup_close(winid)
1109endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001110
1111func Test_popup_width_longest()
1112 let tests = [
1113 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1114 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1115 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1116 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1117 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1118 \ ]
1119
1120 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001121 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001122 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001123 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001124 call assert_equal(test[1], position.width)
1125 call popup_close(winid)
1126 endfor
1127endfunc
1128
1129func Test_popup_wraps()
1130 let tests = [
1131 \ ['nowrap', 6, 1],
1132 \ ['a line that wraps once', 12, 2],
1133 \ ['a line that wraps two times', 12, 3],
1134 \ ]
1135 for test in tests
1136 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001137 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001138 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001139 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001140 call assert_equal(test[1], position.width)
1141 call assert_equal(test[2], position.height)
1142
1143 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001144 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001145 endfor
1146endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001147
1148func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001149 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001150 \ line: 2,
1151 \ col: 3,
1152 \ minwidth: 10,
1153 \ minheight: 11,
1154 \ maxwidth: 20,
1155 \ maxheight: 21,
1156 \ zindex: 100,
1157 \ time: 5000,
1158 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001159 \})
1160 redraw
1161 let res = popup_getoptions(winid)
1162 call assert_equal(2, res.line)
1163 call assert_equal(3, res.col)
1164 call assert_equal(10, res.minwidth)
1165 call assert_equal(11, res.minheight)
1166 call assert_equal(20, res.maxwidth)
1167 call assert_equal(21, res.maxheight)
1168 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001169 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001170 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001171 if has('timers')
1172 call assert_equal(5000, res.time)
1173 endif
1174 call popup_close(winid)
1175
1176 let winid = popup_create('hello', {})
1177 redraw
1178 let res = popup_getoptions(winid)
1179 call assert_equal(0, res.line)
1180 call assert_equal(0, res.col)
1181 call assert_equal(0, res.minwidth)
1182 call assert_equal(0, res.minheight)
1183 call assert_equal(0, res.maxwidth)
1184 call assert_equal(0, res.maxheight)
1185 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001186 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001187 if has('timers')
1188 call assert_equal(0, res.time)
1189 endif
1190 call popup_close(winid)
1191 call assert_equal({}, popup_getoptions(winid))
1192endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001193
1194func Test_popup_option_values()
1195 new
1196 " window-local
1197 setlocal number
1198 setlocal nowrap
1199 " buffer-local
1200 setlocal omnifunc=Something
1201 " global/buffer-local
1202 setlocal path=/there
1203 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001204 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001205
1206 let winid = popup_create('hello', {})
1207 call assert_equal(0, getwinvar(winid, '&number'))
1208 call assert_equal(1, getwinvar(winid, '&wrap'))
1209 call assert_equal('', getwinvar(winid, '&omnifunc'))
1210 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001211 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1212
1213 " 'scrolloff' is reset to zero
1214 call assert_equal(5, &scrolloff)
1215 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001216
1217 call popup_close(winid)
1218 bwipe
1219endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001220
1221func Test_popup_atcursor()
1222 topleft vnew
1223 call setline(1, [
1224 \ 'xxxxxxxxxxxxxxxxx',
1225 \ 'xxxxxxxxxxxxxxxxx',
1226 \ 'xxxxxxxxxxxxxxxxx',
1227 \])
1228
1229 call cursor(2, 2)
1230 redraw
1231 let winid = popup_atcursor('vim', {})
1232 redraw
1233 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1234 call assert_equal('xvimxxxxxxxxxxxxx', line)
1235 call popup_close(winid)
1236
1237 call cursor(3, 4)
1238 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001239 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001240 redraw
1241 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1242 call assert_equal('xxxvimxxxxxxxxxxx', line)
1243 call popup_close(winid)
1244
1245 call cursor(1, 1)
1246 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001247 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001248 \ line: 'cursor+2',
1249 \ col: 'cursor+1',
1250 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001251 redraw
1252 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1253 call assert_equal('xvimxxxxxxxxxxxxx', line)
1254 call popup_close(winid)
1255
1256 call cursor(3, 3)
1257 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001258 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001259 \ line: 'cursor-2',
1260 \ col: 'cursor-1',
1261 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001262 redraw
1263 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1264 call assert_equal('xvimxxxxxxxxxxxxx', line)
1265 call popup_close(winid)
1266
Bram Moolenaar402502d2019-05-30 22:07:36 +02001267 " just enough room above
1268 call cursor(3, 3)
1269 redraw
1270 let winid = popup_atcursor(['vim', 'is great'], {})
1271 redraw
1272 let pos = popup_getpos(winid)
1273 call assert_equal(1, pos.line)
1274 call popup_close(winid)
1275
1276 " not enough room above, popup goes below the cursor
1277 call cursor(3, 3)
1278 redraw
1279 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1280 redraw
1281 let pos = popup_getpos(winid)
1282 call assert_equal(4, pos.line)
1283 call popup_close(winid)
1284
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001285 " cursor in first line, popup in line 2
1286 call cursor(1, 1)
1287 redraw
1288 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1289 redraw
1290 let pos = popup_getpos(winid)
1291 call assert_equal(2, pos.line)
1292 call popup_close(winid)
1293
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001294 bwipe!
1295endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001296
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001297func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001298 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001299 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001300
1301 let lines =<< trim END
1302 call setline(1, range(1, 20))
1303 call setline(5, 'here is some text to hover over')
1304 set balloonevalterm
1305 set balloonexpr=BalloonExpr()
1306 set balloondelay=100
1307 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001308 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001309 return ''
1310 endfunc
1311 func Hover()
1312 call test_setmouse(5, 15)
1313 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1314 sleep 100m
1315 endfunc
1316 func MoveOntoPopup()
1317 call test_setmouse(4, 17)
1318 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1319 endfunc
1320 func MoveAway()
1321 call test_setmouse(5, 13)
1322 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1323 endfunc
1324 END
1325 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001326 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001327 call term_wait(buf, 100)
1328 call term_sendkeys(buf, 'j')
1329 call term_sendkeys(buf, ":call Hover()\<CR>")
1330 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1331
1332 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1333 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1334
1335 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1336 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1337
1338 " clean up
1339 call StopVimInTerminal(buf)
1340 call delete('XtestPopupBeval')
1341endfunc
1342
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001343func Test_popup_filter()
1344 new
1345 call setline(1, 'some text')
1346
1347 func MyPopupFilter(winid, c)
1348 if a:c == 'e'
1349 let g:eaten = 'e'
1350 return 1
1351 endif
1352 if a:c == '0'
1353 let g:ignored = '0'
1354 return 0
1355 endif
1356 if a:c == 'x'
1357 call popup_close(a:winid)
1358 return 1
1359 endif
1360 return 0
1361 endfunc
1362
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001363 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001364 redraw
1365
1366 " e is consumed by the filter
1367 call feedkeys('e', 'xt')
1368 call assert_equal('e', g:eaten)
1369
1370 " 0 is ignored by the filter
1371 normal $
1372 call assert_equal(9, getcurpos()[2])
1373 call feedkeys('0', 'xt')
1374 call assert_equal('0', g:ignored)
1375 call assert_equal(1, getcurpos()[2])
1376
1377 " x closes the popup
1378 call feedkeys('x', 'xt')
1379 call assert_equal('e', g:eaten)
1380 call assert_equal(-1, winbufnr(winid))
1381
1382 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001383 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001384endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001385
Bram Moolenaara42d9452019-06-15 21:46:30 +02001386func ShowDialog(key, result)
1387 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001388 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001389 \ filter: 'popup_filter_yesno',
1390 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001391 \ })
1392 redraw
1393 call feedkeys(a:key, "xt")
1394 call assert_equal(winid, s:cb_winid)
1395 call assert_equal(a:result, s:cb_res)
1396endfunc
1397
1398func Test_popup_dialog()
1399 func QuitCallback(id, res)
1400 let s:cb_winid = a:id
1401 let s:cb_res = a:res
1402 endfunc
1403
1404 let winid = ShowDialog("y", 1)
1405 let winid = ShowDialog("Y", 1)
1406 let winid = ShowDialog("n", 0)
1407 let winid = ShowDialog("N", 0)
1408 let winid = ShowDialog("x", 0)
1409 let winid = ShowDialog("X", 0)
1410 let winid = ShowDialog("\<Esc>", 0)
1411 let winid = ShowDialog("\<C-C>", -1)
1412
1413 delfunc QuitCallback
1414endfunc
1415
Bram Moolenaara730e552019-06-16 19:05:31 +02001416func ShowMenu(key, result)
1417 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001418 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001419 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001420 \ })
1421 redraw
1422 call feedkeys(a:key, "xt")
1423 call assert_equal(winid, s:cb_winid)
1424 call assert_equal(a:result, s:cb_res)
1425endfunc
1426
1427func Test_popup_menu()
1428 func QuitCallback(id, res)
1429 let s:cb_winid = a:id
1430 let s:cb_res = a:res
1431 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001432 " mapping won't be used in popup
1433 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001434
1435 let winid = ShowMenu(" ", 1)
1436 let winid = ShowMenu("j \<CR>", 2)
1437 let winid = ShowMenu("JjK \<CR>", 2)
1438 let winid = ShowMenu("jjjjjj ", 3)
1439 let winid = ShowMenu("kkk ", 1)
1440 let winid = ShowMenu("x", -1)
1441 let winid = ShowMenu("X", -1)
1442 let winid = ShowMenu("\<Esc>", -1)
1443 let winid = ShowMenu("\<C-C>", -1)
1444
1445 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001446 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001447endfunc
1448
1449func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001450 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001451
1452 let lines =<< trim END
1453 call setline(1, range(1, 20))
1454 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001455 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001456 func MenuDone(id, res)
1457 echomsg "selected " .. a:res
1458 endfunc
1459 END
1460 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001461 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001462 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1463
1464 call term_sendkeys(buf, "jj")
1465 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1466
1467 call term_sendkeys(buf, " ")
1468 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1469
1470 " clean up
1471 call StopVimInTerminal(buf)
1472 call delete('XtestPopupMenu')
1473endfunc
1474
Bram Moolenaarf914a332019-07-20 15:09:56 +02001475func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001476 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001477
1478 let lines =<< trim END
1479 call setline(1, range(1, 20))
1480 hi PopupSelected ctermbg=green
1481 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1482 func MenuDone(id, res)
1483 echomsg "selected " .. a:res
1484 endfunc
1485 END
1486 call writefile(lines, 'XtestPopupNarrowMenu')
1487 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1488 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1489
1490 " clean up
1491 call term_sendkeys(buf, "x")
1492 call StopVimInTerminal(buf)
1493 call delete('XtestPopupNarrowMenu')
1494endfunc
1495
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001496func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001497 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001498
1499 " Create a popup without title or border, a line of padding will be added to
1500 " put the title on.
1501 let lines =<< trim END
1502 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001503 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001504 END
1505 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001506 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001507 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1508
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001509 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1510 call term_sendkeys(buf, ":\<CR>")
1511 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1512
1513 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1514 call term_sendkeys(buf, ":\<CR>")
1515 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1516
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001517 " clean up
1518 call StopVimInTerminal(buf)
1519 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001520
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001521 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001522 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001523 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001524 call assert_equal('Another Title', popup_getoptions(winid).title)
1525
1526 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001527endfunc
1528
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001529func Test_popup_close_callback()
1530 func PopupDone(id, result)
1531 let g:result = a:result
1532 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001533 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001534 redraw
1535 call popup_close(winid, 'done')
1536 call assert_equal('done', g:result)
1537endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001538
1539func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001540 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001541 redraw
1542 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001543 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001544 call assert_equal(5, pos.height)
1545
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001546 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001547 redraw
1548 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001549 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001550 call assert_equal(3, pos.height)
1551endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001552
1553func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001554 CheckScreendump
1555
Bram Moolenaar988c4332019-06-02 14:12:11 +02001556 " +-----------------------------+
1557 " | | |
1558 " | | |
1559 " | | |
1560 " | line1 |
1561 " |------------line2------------|
1562 " | line3 |
1563 " | line4 |
1564 " | |
1565 " | |
1566 " +-----------------------------+
1567 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001568 split
1569 vsplit
1570 let info_window1 = getwininfo()[0]
1571 let line = info_window1['height']
1572 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001573 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001574 \ line : line,
1575 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001576 \ })
1577 END
1578 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001579 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001580 call term_sendkeys(buf, "\<C-W>w")
1581 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1582
1583 " clean up
1584 call StopVimInTerminal(buf)
1585 call delete('XtestPopupBehind')
1586endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001587
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001588func s:VerifyPosition(p, msg, line, col, width, height)
1589 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1590 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1591 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1592 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001593endfunc
1594
1595func Test_popup_position_adjust()
1596 " Anything placed past 2 cells from of the right of the screen is moved to the
1597 " left.
1598 "
1599 " When wrapping is disabled, we also shift to the left to display on the
1600 " screen, unless fixed is set.
1601
1602 " Entries for cases which don't vary based on wrapping.
1603 " Format is per tests described below
1604 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001605 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1606 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1607 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1608 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1609 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1610 \
1611 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1612 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1613 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1614 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1615 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1616 \
1617 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1618 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1619 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1620 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1621 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1622 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001623
1624 " these test groups are dicts with:
1625 " - comment: something to identify the group of tests by
1626 " - options: dict of options to merge with the row/col in tests
1627 " - tests: list of cases. Each one is a list with elements:
1628 " - text
1629 " - row
1630 " - col
1631 " - expected row
1632 " - expected col
1633 " - expected width
1634 " - expected height
1635 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001636 \ #{
1637 \ comment: 'left-aligned with wrapping',
1638 \ options: #{
1639 \ wrap: 1,
1640 \ pos: 'botleft',
1641 \ },
1642 \ tests: both_wrap_tests + [
1643 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1644 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1645 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1646 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1647 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1648 \ ],
1649 \ },
1650 \ #{
1651 \ comment: 'left aligned without wrapping',
1652 \ options: #{
1653 \ wrap: 0,
1654 \ pos: 'botleft',
1655 \ },
1656 \ tests: both_wrap_tests + [
1657 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1658 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1659 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1660 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1661 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1662 \ ],
1663 \ },
1664 \ #{
1665 \ comment: 'left aligned with fixed position',
1666 \ options: #{
1667 \ wrap: 0,
1668 \ fixed: 1,
1669 \ pos: 'botleft',
1670 \ },
1671 \ tests: both_wrap_tests + [
1672 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1673 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1674 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1675 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1676 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1677 \ ],
1678 \ },
1679 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001680
1681 for test_group in tests
1682 for test in test_group.tests
1683 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001684 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001685 \ line: line,
1686 \ col: col,
1687 \ }
1688 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001689
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001690 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001691
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001692 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001693 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1694 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001695 endfor
1696 endfor
1697
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001698 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001699 %bwipe!
1700endfunc
1701
Bram Moolenaar3397f742019-06-02 18:40:06 +02001702func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001703 " width of screen
1704 let X = join(map(range(&columns), {->'X'}), '')
1705
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001706 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1707 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001708
1709 redraw
1710 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1711 call assert_equal(X, line)
1712
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001713 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001714 redraw
1715
1716 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001717 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1718 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001719
1720 redraw
1721 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1722 call assert_equal(X, line)
1723
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001724 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001725 redraw
1726
1727 " Extend so > window width
1728 let X .= 'x'
1729
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001730 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1731 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001732
1733 redraw
1734 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1735 call assert_equal(X[ : -2 ], line)
1736
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001737 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001738 redraw
1739
1740 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001741 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1742 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001743
1744 redraw
1745 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1746 call assert_equal(X[ : -2 ], line)
1747
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001748 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001749 redraw
1750
1751 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001752 let p = popup_create(X,
1753 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1754 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001755
1756 redraw
1757 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1758 let e_line = ' ' . X[ 1 : -2 ]
1759 call assert_equal(e_line, line)
1760
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001761 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001762 redraw
1763
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001764 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001765 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001766endfunc
1767
1768func Test_popup_moved()
1769 new
1770 call test_override('char_avail', 1)
1771 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1772
1773 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001774 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001775 redraw
1776 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001777 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001778 " trigger the check for last_cursormoved by going into insert mode
1779 call feedkeys("li\<Esc>", 'xt')
1780 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001781 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001782
1783 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001784 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001785 redraw
1786 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001787 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001788 call feedkeys("hi\<Esc>", 'xt')
1789 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001790 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001791
1792 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001793 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001794 redraw
1795 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001796 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001797 call feedkeys("li\<Esc>", 'xt')
1798 call assert_equal(1, popup_getpos(winid).visible)
1799 call feedkeys("ei\<Esc>", 'xt')
1800 call assert_equal(1, popup_getpos(winid).visible)
1801 call feedkeys("eli\<Esc>", 'xt')
1802 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001803 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001804
Bram Moolenaar17627312019-06-02 19:53:44 +02001805 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001806 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001807 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001808 redraw
1809 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001810 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001811 call feedkeys("eli\<Esc>", 'xt')
1812 call assert_equal(1, popup_getpos(winid).visible)
1813 call feedkeys("wi\<Esc>", 'xt')
1814 call assert_equal(1, popup_getpos(winid).visible)
1815 call feedkeys("Eli\<Esc>", 'xt')
1816 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001817 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001818
1819 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001820 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001821 redraw
1822 call assert_equal(1, popup_getpos(winid).visible)
1823 call feedkeys("eli\<Esc>", 'xt')
1824 call feedkeys("ei\<Esc>", 'xt')
1825 call assert_equal(1, popup_getpos(winid).visible)
1826 call feedkeys("eli\<Esc>", 'xt')
1827 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001828 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001829
1830 bwipe!
1831 call test_override('ALL', 0)
1832endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001833
1834func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001835 CheckFeature timers
1836 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001837
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001838 let lines =<< trim END
1839 call setline(1, range(1, 20))
1840 hi Notification ctermbg=lightblue
1841 call popup_notification('first notification', {})
1842 END
1843 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001844 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001845 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1846
1847 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001848 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1849 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001850 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1851
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001852 " clean up
1853 call StopVimInTerminal(buf)
1854 call delete('XtestNotifications')
1855endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001856
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001857func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001858 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001859
1860 let lines =<< trim END
1861 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001862 hi ScrollThumb ctermbg=blue
1863 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001864 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001865 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001866 \ minwidth: 8,
1867 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001868 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001869 func ScrollUp()
1870 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1871 endfunc
1872 func ScrollDown()
1873 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1874 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001875 func ClickTop()
1876 call feedkeys("\<F4>\<LeftMouse>", "xt")
1877 endfunc
1878 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001879 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001880 call feedkeys("\<F5>\<LeftMouse>", "xt")
1881 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001882 func Popup_filter(winid, key)
1883 if a:key == 'j'
1884 let line = popup_getoptions(a:winid).firstline
1885 let nlines = line('$', a:winid)
1886 let newline = line < nlines ? (line + 1) : nlines
1887 call popup_setoptions(a:winid, #{firstline: newline})
1888 return v:true
1889 elseif a:key == 'x'
1890 call popup_close(a:winid)
1891 return v:true
1892 endif
1893 endfunc
1894
1895 func PopupScroll()
1896 call popup_clear()
1897 let text =<< trim END
1898 1
1899 2
1900 3
1901 4
1902 long line long line long line long line long line long line
1903 long line long line long line long line long line long line
1904 long line long line long line long line long line long line
1905 END
1906 call popup_create(text, #{
1907 \ minwidth: 30,
1908 \ maxwidth: 30,
1909 \ minheight: 4,
1910 \ maxheight: 4,
1911 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +01001912 \ lastline: 4,
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001913 \ wrap: v:true,
1914 \ scrollbar: v:true,
1915 \ mapping: v:false,
1916 \ filter: funcref('Popup_filter')
1917 \ })
1918 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001919 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001920 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1921 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001922 END
1923 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001924 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001925 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1926
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001927 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001928 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001929 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1930
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001931 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001932 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001933 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1934
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001935 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001936 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001937 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1938
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02001939 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001940 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001941 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1942 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1943
1944 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1945 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1946
1947 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001948 " wait a bit, otherwise it fails sometimes (double click recognized?)
1949 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001950 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1951 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1952
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001953 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1954 sleep 100m
1955 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1956 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1957
1958 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1959 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1960
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001961 " remove the minwidth and maxheight
1962 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02001963 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001964 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
1965
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001966 " check size with non-wrapping lines
1967 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
1968 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
1969
1970 " check size with wrapping lines
1971 call term_sendkeys(buf, "j")
1972 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
1973 call term_sendkeys(buf, "x")
1974
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001975 " clean up
1976 call StopVimInTerminal(buf)
1977 call delete('XtestPopupScroll')
1978endfunc
1979
Bram Moolenaar437a7462019-07-05 20:17:22 +02001980func Test_popup_fitting_scrollbar()
1981 " this was causing a crash, divide by zero
1982 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001983 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001984 \ scrollbar: 1,
1985 \ maxwidth: 10,
1986 \ maxheight: 5,
1987 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001988 redraw
1989 call popup_clear()
1990endfunc
1991
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001992func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001993 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001994
1995 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001996 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001997 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001998 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001999 END
2000
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002001 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002002 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002003 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2004
2005 " Setting to empty string clears it
2006 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2007 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2008
2009 " Setting a list
2010 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2011 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2012
2013 " Shrinking with a list
2014 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2015 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2016
2017 " Growing with a list
2018 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2019 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2020
2021 " Empty list clears
2022 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2023 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2024
2025 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002026 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002027 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2028
2029 " clean up
2030 call StopVimInTerminal(buf)
2031 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002032endfunc
2033
2034func Test_popup_hidden()
2035 new
2036
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002037 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002038 redraw
2039 call assert_equal(0, popup_getpos(winid).visible)
2040 call popup_close(winid)
2041
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002042 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002043 redraw
2044 call assert_equal(0, popup_getpos(winid).visible)
2045 call popup_close(winid)
2046
2047 func QuitCallback(id, res)
2048 let s:cb_winid = a:id
2049 let s:cb_res = a:res
2050 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002051 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002052 \ filter: 'popup_filter_yesno',
2053 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002054 \ })
2055 redraw
2056 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002057 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2058 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002059 exe "normal anot used by filter\<Esc>"
2060 call assert_equal('not used by filter', getline(1))
2061
2062 call popup_show(winid)
2063 call feedkeys('y', "xt")
2064 call assert_equal(1, s:cb_res)
2065
2066 bwipe!
2067 delfunc QuitCallback
2068endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002069
2070" Test options not checked elsewhere
2071func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002072 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002073 let options = popup_getoptions(winid)
2074 call assert_equal(1, options.wrap)
2075 call assert_equal(0, options.drag)
2076 call assert_equal('Beautiful', options.highlight)
2077
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002078 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002079 let options = popup_getoptions(winid)
2080 call assert_equal(0, options.wrap)
2081 call assert_equal(1, options.drag)
2082 call assert_equal('Another', options.highlight)
2083
2084 call popup_close(winid)
2085endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002086
2087func Test_popupwin_garbage_collect()
2088 func MyPopupFilter(x, winid, c)
2089 " NOP
2090 endfunc
2091
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002092 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002093 call test_garbagecollect_now()
2094 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002095 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002096 call feedkeys('j', 'xt')
2097 call assert_true(v:true)
2098
2099 call popup_close(winid)
2100 delfunc MyPopupFilter
2101endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002102
Bram Moolenaar581ba392019-09-03 22:08:33 +02002103func Test_popupwin_filter_mode()
2104 func MyPopupFilter(winid, c)
2105 let s:typed = a:c
2106 if a:c == ':' || a:c == "\r" || a:c == 'v'
2107 " can start cmdline mode, get out, and start/stop Visual mode
2108 return 0
2109 endif
2110 return 1
2111 endfunc
2112
2113 " Normal, Visual and Insert mode
2114 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2115 redraw
2116 call feedkeys('x', 'xt')
2117 call assert_equal('x', s:typed)
2118
2119 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2120 call assert_equal(':', s:typed)
2121 call assert_equal('foo', g:foo)
2122
2123 let @x = 'something'
2124 call feedkeys('v$"xy', 'xt')
2125 call assert_equal('y', s:typed)
2126 call assert_equal('something', @x) " yank command is filtered out
2127 call feedkeys('v', 'xt') " end Visual mode
2128
2129 call popup_close(winid)
2130
2131 " only Normal mode
2132 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2133 redraw
2134 call feedkeys('x', 'xt')
2135 call assert_equal('x', s:typed)
2136
2137 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2138 call assert_equal(':', s:typed)
2139 call assert_equal('foo', g:foo)
2140
2141 let @x = 'something'
2142 call feedkeys('v$"xy', 'xt')
2143 call assert_equal('v', s:typed)
2144 call assert_notequal('something', @x)
2145
2146 call popup_close(winid)
2147
2148 " default: all modes
2149 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2150 redraw
2151 call feedkeys('x', 'xt')
2152 call assert_equal('x', s:typed)
2153
2154 let g:foo = 'bar'
2155 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2156 call assert_equal("\r", s:typed)
2157 call assert_equal('bar', g:foo)
2158
2159 let @x = 'something'
2160 call feedkeys('v$"xy', 'xt')
2161 call assert_equal('y', s:typed)
2162 call assert_equal('something', @x) " yank command is filtered out
2163 call feedkeys('v', 'xt') " end Visual mode
2164
2165 call popup_close(winid)
2166 delfunc MyPopupFilter
2167endfunc
2168
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002169func Test_popupwin_with_buffer()
2170 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2171 let buf = bufadd('XsomeFile')
2172 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002173
2174 setlocal number
2175 call setbufvar(buf, "&wrapmargin", 13)
2176
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002177 let winid = popup_create(buf, {})
2178 call assert_notequal(0, winid)
2179 let pos = popup_getpos(winid)
2180 call assert_equal(2, pos.height)
2181 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002182
2183 " window-local option is set to default, buffer-local is not
2184 call assert_equal(0, getwinvar(winid, '&number'))
2185 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2186
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002187 call popup_close(winid)
2188 call assert_equal({}, popup_getpos(winid))
2189 call assert_equal(1, bufloaded(buf))
2190 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002191 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002192
2193 edit test_popupwin.vim
2194 let winid = popup_create(bufnr(''), {})
2195 redraw
2196 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002197 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002198endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002199
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002200func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002201 CheckFeature terminal
2202
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002203 let ptybuf = term_start(&shell, #{hidden: 1})
2204 call assert_fails('let winnr = popup_create(ptybuf, #{})', 'E278:')
2205 exe 'bwipe! ' .. ptybuf
2206endfunc
2207
Bram Moolenaar934470e2019-09-01 23:27:05 +02002208func Test_popupwin_with_buffer_and_filter()
2209 new Xwithfilter
2210 call setline(1, range(100))
2211 let bufnr = bufnr()
2212 hide
2213
2214 func BufferFilter(win, key)
2215 if a:key == 'G'
2216 " recursive use of "G" does not cause problems.
2217 call win_execute(a:win, 'normal! G')
2218 return 1
2219 endif
2220 return 0
2221 endfunc
2222
2223 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2224 call assert_equal(1, popup_getpos(winid).firstline)
2225 redraw
2226 call feedkeys("G", 'xt')
2227 call assert_equal(99, popup_getpos(winid).firstline)
2228
2229 call popup_close(winid)
2230 exe 'bwipe! ' .. bufnr
2231endfunc
2232
Bram Moolenaare296e312019-07-03 23:20:18 +02002233func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002234 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002235 \ maxwidth: 40,
2236 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002237 \ })
2238 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002239 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002240 redraw
2241 call assert_equal(19, popup_getpos(winid).width)
2242 endfor
2243 call popup_clear()
2244endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002245
2246func Test_popupwin_buf_close()
2247 let buf = bufadd('Xtestbuf')
2248 call bufload(buf)
2249 call setbufline(buf, 1, ['just', 'some', 'lines'])
2250 let winid = popup_create(buf, {})
2251 redraw
2252 call assert_equal(3, popup_getpos(winid).height)
2253 let bufinfo = getbufinfo(buf)[0]
2254 call assert_equal(1, bufinfo.changed)
2255 call assert_equal(0, bufinfo.hidden)
2256 call assert_equal(0, bufinfo.listed)
2257 call assert_equal(1, bufinfo.loaded)
2258 call assert_equal([], bufinfo.windows)
2259 call assert_equal([winid], bufinfo.popups)
2260
2261 call popup_close(winid)
2262 call assert_equal({}, popup_getpos(winid))
2263 let bufinfo = getbufinfo(buf)[0]
2264 call assert_equal(1, bufinfo.changed)
2265 call assert_equal(1, bufinfo.hidden)
2266 call assert_equal(0, bufinfo.listed)
2267 call assert_equal(1, bufinfo.loaded)
2268 call assert_equal([], bufinfo.windows)
2269 call assert_equal([], bufinfo.popups)
2270 exe 'bwipe! ' .. buf
2271endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002272
2273func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002274 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002275
2276 let lines =<< trim END
2277 call setline(1, range(1, 10))
2278 hi ScrollThumb ctermbg=blue
2279 hi ScrollBar ctermbg=red
2280 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002281 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002282 \ maxwidth: 10,
2283 \ maxheight: 3,
2284 \ pos : 'topleft',
2285 \ col : a:col,
2286 \ line : a:line,
2287 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002288 \ })
2289 endfunc
2290 call PopupMenu(['x'], 1, 1)
2291 call PopupMenu(['123456789|'], 1, 16)
2292 call PopupMenu(['123456789|' .. ' '], 7, 1)
2293 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2294 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2295 END
2296 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002297 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002298 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2299
2300 " close the menu popupwin.
2301 call term_sendkeys(buf, " ")
2302 call term_sendkeys(buf, " ")
2303 call term_sendkeys(buf, " ")
2304 call term_sendkeys(buf, " ")
2305 call term_sendkeys(buf, " ")
2306
2307 " clean up
2308 call StopVimInTerminal(buf)
2309 call delete('XtestPopupMenuMaxWidth')
2310endfunc
2311
Bram Moolenaara901a372019-07-13 16:38:50 +02002312func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002313 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002314
2315 let lines =<< trim END
2316 call setline(1, range(1, 20))
2317 hi ScrollThumb ctermbg=blue
2318 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002319 eval ['one', 'two', 'three', 'four', 'five',
2320 \ 'six', 'seven', 'eight', 'nine']
2321 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002322 \ minwidth: 8,
2323 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002324 \ })
2325 END
2326 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002327 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002328
2329 call term_sendkeys(buf, "j")
2330 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2331
2332 call term_sendkeys(buf, "jjj")
2333 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2334
2335 " if the cursor is the bottom line, it stays at the bottom line.
2336 call term_sendkeys(buf, repeat("j", 20))
2337 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2338
2339 call term_sendkeys(buf, "kk")
2340 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2341
2342 call term_sendkeys(buf, "k")
2343 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2344
2345 " if the cursor is in the top line, it stays in the top line.
2346 call term_sendkeys(buf, repeat("k", 20))
2347 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2348
2349 " close the menu popupwin.
2350 call term_sendkeys(buf, " ")
2351
2352 " clean up
2353 call StopVimInTerminal(buf)
2354 call delete('XtestPopupMenuScroll')
2355endfunc
2356
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002357func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002358 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002359
2360 let lines =<< trim END
2361 function! MyFilter(winid, key) abort
2362 if a:key == "0"
2363 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2364 return 1
2365 endif
2366 if a:key == "G"
2367 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2368 return 1
2369 endif
2370 if a:key == "j"
2371 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2372 return 1
2373 endif
2374 if a:key == "k"
2375 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2376 return 1
2377 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002378 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002379 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002380 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002381 endif
2382 return 0
2383 endfunction
2384 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2385 \ maxheight : 3,
2386 \ filter : 'MyFilter'
2387 \ })
2388 END
2389 call writefile(lines, 'XtestPopupMenuFilter')
2390 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2391
2392 call term_sendkeys(buf, "j")
2393 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2394
2395 call term_sendkeys(buf, "k")
2396 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2397
2398 call term_sendkeys(buf, "G")
2399 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2400
2401 call term_sendkeys(buf, "0")
2402 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2403
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002404 " check that when the popup is closed in the filter the screen is redrawn
2405 call term_sendkeys(buf, ":")
2406 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2407 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002408
2409 " clean up
2410 call StopVimInTerminal(buf)
2411 call delete('XtestPopupMenuFilter')
2412endfunc
2413
2414func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002415 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002416
2417 let winid = popup_create('some text', {})
2418 call assert_equal(0, popup_getoptions(winid).cursorline)
2419 call popup_close(winid)
2420
2421 let winid = popup_create('some text', #{ cursorline: 1, })
2422 call assert_equal(1, popup_getoptions(winid).cursorline)
2423 call popup_close(winid)
2424
2425 let winid = popup_create('some text', #{ cursorline: 0, })
2426 call assert_equal(0, popup_getoptions(winid).cursorline)
2427 call popup_close(winid)
2428
2429 let winid = popup_menu('some text', {})
2430 call assert_equal(1, popup_getoptions(winid).cursorline)
2431 call popup_close(winid)
2432
2433 let winid = popup_menu('some text', #{ cursorline: 1, })
2434 call assert_equal(1, popup_getoptions(winid).cursorline)
2435 call popup_close(winid)
2436
2437 let winid = popup_menu('some text', #{ cursorline: 0, })
2438 call assert_equal(0, popup_getoptions(winid).cursorline)
2439 call popup_close(winid)
2440
2441 " ---------
2442 " Pattern 1
2443 " ---------
2444 let lines =<< trim END
2445 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2446 END
2447 call writefile(lines, 'XtestPopupCursorLine')
2448 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2449 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2450 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2451 call StopVimInTerminal(buf)
2452
2453 " ---------
2454 " Pattern 2
2455 " ---------
2456 let lines =<< trim END
2457 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2458 END
2459 call writefile(lines, 'XtestPopupCursorLine')
2460 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2461 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2462 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2463 call StopVimInTerminal(buf)
2464
2465 " ---------
2466 " Pattern 3
2467 " ---------
2468 let lines =<< trim END
2469 function! MyFilter(winid, key) abort
2470 if a:key == "j"
2471 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2472 return 1
2473 endif
2474 if a:key == 'x'
2475 call popup_close(a:winid)
2476 return 1
2477 endif
2478 return 0
2479 endfunction
2480 call popup_menu(['111', '222', '333'], #{
2481 \ cursorline : 0,
2482 \ maxheight : 2,
2483 \ filter : 'MyFilter',
2484 \ })
2485 END
2486 call writefile(lines, 'XtestPopupCursorLine')
2487 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2488 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2489 call term_sendkeys(buf, "j")
2490 call term_sendkeys(buf, "j")
2491 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2492 call term_sendkeys(buf, "x")
2493 call StopVimInTerminal(buf)
2494
2495 " ---------
2496 " Pattern 4
2497 " ---------
2498 let lines =<< trim END
2499 function! MyFilter(winid, key) abort
2500 if a:key == "j"
2501 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2502 return 1
2503 endif
2504 if a:key == 'x'
2505 call popup_close(a:winid)
2506 return 1
2507 endif
2508 return 0
2509 endfunction
2510 call popup_menu(['111', '222', '333'], #{
2511 \ cursorline : 1,
2512 \ maxheight : 2,
2513 \ filter : 'MyFilter',
2514 \ })
2515 END
2516 call writefile(lines, 'XtestPopupCursorLine')
2517 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2518 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2519 call term_sendkeys(buf, "j")
2520 call term_sendkeys(buf, "j")
2521 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2522 call term_sendkeys(buf, "x")
2523 call StopVimInTerminal(buf)
2524
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002525 " ---------
2526 " Cursor in second line when creating the popup
2527 " ---------
2528 let lines =<< trim END
2529 let winid = popup_create(['111', '222', '333'], #{
2530 \ cursorline : 1,
2531 \ })
2532 call win_execute(winid, "2")
2533 END
2534 call writefile(lines, 'XtestPopupCursorLine')
2535 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2536 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2537 call StopVimInTerminal(buf)
2538
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002539 call delete('XtestPopupCursorLine')
2540endfunc
2541
Bram Moolenaarf914a332019-07-20 15:09:56 +02002542func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002543 CheckScreendump
2544
Bram Moolenaarf914a332019-07-20 15:09:56 +02002545 call writefile([
2546 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2547 \ "another\tXtagfile\t/^this is another",
2548 \ "theword\tXtagfile\t/^theword"],
2549 \ 'Xtags')
2550 call writefile(range(1,20)
2551 \ + ['theword is here']
2552 \ + range(22, 27)
2553 \ + ['this is another place']
2554 \ + range(29, 40),
2555 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002556 call writefile(range(1,10)
2557 \ + ['searched word is here']
2558 \ + range(12, 20),
2559 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002560 let lines =<< trim END
2561 set tags=Xtags
2562 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002563 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002564 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002565 \ 'three',
2566 \ 'four',
2567 \ 'five',
2568 \ 'six',
2569 \ 'seven',
2570 \ 'find theword somewhere',
2571 \ 'nine',
2572 \ 'this is another word',
2573 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002574 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002575 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002576 END
2577 call writefile(lines, 'XtestPreviewPopup')
2578 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2579
2580 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2581 call term_sendkeys(buf, ":\<CR>")
2582 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2583
2584 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2585 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2586
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002587 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002588 call term_sendkeys(buf, ":\<CR>")
2589 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2590
2591 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2592 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2593
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002594 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2595 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002596 call term_sendkeys(buf, ":cd testdir\<CR>")
2597
2598 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002599 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002600 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2601
2602 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2603 call term_sendkeys(buf, ":\<CR>")
2604 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2605
2606 call term_sendkeys(buf, ":pclose\<CR>")
2607 call term_sendkeys(buf, ":psearch searched\<CR>")
2608 call term_sendkeys(buf, ":\<CR>")
2609 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002610
Bram Moolenaarf914a332019-07-20 15:09:56 +02002611 call StopVimInTerminal(buf)
2612 call delete('Xtags')
2613 call delete('Xtagfile')
2614 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002615 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002616endfunc
2617
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002618func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002619 let lines =<< trim END
2620 set completeopt+=preview,popup
2621 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002622 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002623
2624 func CompleteFuncDict(findstart, base)
2625 if a:findstart
2626 if col('.') > 10
2627 return col('.') - 10
2628 endif
2629 return 0
2630 endif
2631
2632 return {
2633 \ 'words': [
2634 \ {
2635 \ 'word': 'aword',
2636 \ 'abbr': 'wrd',
2637 \ 'menu': 'extra text',
2638 \ 'info': 'words are cool',
2639 \ 'kind': 'W',
2640 \ 'user_data': 'test'
2641 \ },
2642 \ {
2643 \ 'word': 'anotherword',
2644 \ 'abbr': 'anotwrd',
2645 \ 'menu': 'extra text',
2646 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2647 \ 'kind': 'W',
2648 \ 'user_data': 'notest'
2649 \ },
2650 \ {
2651 \ 'word': 'noinfo',
2652 \ 'abbr': 'noawrd',
2653 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002654 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002655 \ 'kind': 'W',
2656 \ 'user_data': 'notest'
2657 \ },
2658 \ {
2659 \ 'word': 'thatword',
2660 \ 'abbr': 'thatwrd',
2661 \ 'menu': 'extra text',
2662 \ 'info': 'that word is cool',
2663 \ 'kind': 'W',
2664 \ 'user_data': 'notest'
2665 \ },
2666 \ ]
2667 \ }
2668 endfunc
2669 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002670 func ChangeColor()
2671 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002672 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002673 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002674
2675 func InfoHidden()
2676 set completepopup=height:4,border:off,align:menu
2677 set completeopt-=popup completeopt+=popuphidden
2678 au CompleteChanged * call HandleChange()
2679 endfunc
2680
2681 let s:counter = 0
2682 func HandleChange()
2683 let s:counter += 1
2684 let selected = complete_info(['selected']).selected
2685 if selected <= 0
2686 " First time: do nothing, info remains hidden
2687 return
2688 endif
2689 if selected == 1
2690 " Second time: show info right away
2691 let id = popup_findinfo()
2692 if id
2693 call popup_settext(id, 'immediate info ' .. s:counter)
2694 call popup_show(id)
2695 endif
2696 else
2697 " Third time: show info after a short delay
2698 call timer_start(100, 'ShowInfo')
2699 endif
2700 endfunc
2701
2702 func ShowInfo(...)
2703 let id = popup_findinfo()
2704 if id
2705 call popup_settext(id, 'async info ' .. s:counter)
2706 call popup_show(id)
2707 endif
2708 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002709 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002710 return lines
2711endfunc
2712
2713func Test_popupmenu_info_border()
2714 CheckScreendump
2715
2716 let lines = Get_popupmenu_lines()
2717 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002718 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002719
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002720 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2721 call term_wait(buf, 50)
2722
2723 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2724 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2725
2726 call term_sendkeys(buf, "\<C-N>")
2727 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2728
2729 call term_sendkeys(buf, "\<C-N>")
2730 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2731
2732 call term_sendkeys(buf, "\<C-N>\<C-N>")
2733 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2734
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002735 " info on the left with scrollbar
2736 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2737 call term_sendkeys(buf, "\<C-N>\<C-N>")
2738 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2739
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002740 call StopVimInTerminal(buf)
2741 call delete('XtestInfoPopup')
2742endfunc
2743
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002744func Test_popupmenu_info_noborder()
2745 CheckScreendump
2746
2747 let lines = Get_popupmenu_lines()
2748 call add(lines, 'set completepopup=height:4,border:off')
2749 call writefile(lines, 'XtestInfoPopupNb')
2750
2751 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2752 call term_wait(buf, 50)
2753
2754 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2755 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2756
2757 call StopVimInTerminal(buf)
2758 call delete('XtestInfoPopupNb')
2759endfunc
2760
Bram Moolenaar258cef52019-08-21 17:29:29 +02002761func Test_popupmenu_info_align_menu()
2762 CheckScreendump
2763
2764 let lines = Get_popupmenu_lines()
2765 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2766 call writefile(lines, 'XtestInfoPopupNb')
2767
2768 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2769 call term_wait(buf, 50)
2770
2771 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2772 call term_sendkeys(buf, "\<C-N>")
2773 call term_sendkeys(buf, "\<C-N>")
2774 call term_sendkeys(buf, "\<C-N>")
2775 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2776
2777 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2778 call term_sendkeys(buf, "\<C-N>")
2779 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2780
2781 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002782 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002783 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2784 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2785 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2786
2787 call StopVimInTerminal(buf)
2788 call delete('XtestInfoPopupNb')
2789endfunc
2790
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002791func Test_popupmenu_info_hidden()
2792 CheckScreendump
2793
2794 let lines = Get_popupmenu_lines()
2795 call add(lines, 'call InfoHidden()')
2796 call writefile(lines, 'XtestInfoPopupHidden')
2797
2798 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
2799 call term_wait(buf, 50)
2800
2801 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2802 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
2803
2804 call term_sendkeys(buf, "\<C-N>")
2805 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
2806
2807 call term_sendkeys(buf, "\<C-N>")
2808 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
2809
2810 call term_sendkeys(buf, "\<Esc>")
2811 call StopVimInTerminal(buf)
2812 call delete('XtestInfoPopupHidden')
2813endfunc
2814
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002815func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02002816 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002817 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002818 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002819 call assert_equal(bufnr, winbufnr(winid))
2820 call popup_clear()
2821endfunc
2822
Bram Moolenaar1824f452019-10-02 23:06:46 +02002823func Test_popupwin_getoptions_tablocal()
2824 topleft split
2825 let win1 = popup_create('nothing', #{maxheight: 8})
2826 let win2 = popup_create('something', #{maxheight: 10})
2827 let win3 = popup_create('something', #{maxheight: 15})
2828 call assert_equal(8, popup_getoptions(win1).maxheight)
2829 call assert_equal(10, popup_getoptions(win2).maxheight)
2830 call assert_equal(15, popup_getoptions(win3).maxheight)
2831 call popup_clear()
2832 quit
2833endfunc
2834
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002835func Test_popupwin_cancel()
2836 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
2837 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
2838 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
2839 call assert_equal(5, popup_getpos(win1).line)
2840 call assert_equal(10, popup_getpos(win2).line)
2841 call assert_equal(15, popup_getpos(win3).line)
2842 " TODO: this also works without patch 8.1.2110
2843 call feedkeys("\<C-C>", 'xt')
2844 call assert_equal(5, popup_getpos(win1).line)
2845 call assert_equal(10, popup_getpos(win2).line)
2846 call assert_equal({}, popup_getpos(win3))
2847 call feedkeys("\<C-C>", 'xt')
2848 call assert_equal(5, popup_getpos(win1).line)
2849 call assert_equal({}, popup_getpos(win2))
2850 call assert_equal({}, popup_getpos(win3))
2851 call feedkeys("\<C-C>", 'xt')
2852 call assert_equal({}, popup_getpos(win1))
2853 call assert_equal({}, popup_getpos(win2))
2854 call assert_equal({}, popup_getpos(win3))
2855endfunc
2856
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002857" vim: shiftwidth=2 sts=2