blob: 52d5add328d0cd5d6b9f5c3b0f5a322a77947896 [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 Moolenaar4dd8fe02019-11-09 15:33:31 +01001297func Test_popup_atcursor_pos()
1298 CheckScreendump
1299
1300 let lines =<< trim END
1301 call setline(1, repeat([repeat('-', 60)], 15))
1302 set so=0
1303
1304 normal 9G3|r#
1305 let winid1 = popup_atcursor(['first', 'second'], #{
1306 \ moved: [0, 0, 0],
1307 \ })
1308 normal 9G21|r&
1309 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1310 \ pos: 'botright',
1311 \ moved: [0, 0, 0],
1312 \ })
1313 normal 3G27|r%
1314 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1315 \ pos: 'topleft',
1316 \ moved: [0, 0, 0],
1317 \ })
1318 normal 3G45|r@
1319 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1320 \ pos: 'topright',
1321 \ moved: [0, 0, 0],
1322 \ })
1323 END
1324 call writefile(lines, 'XtestPopupAtcursorPos')
1325 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1326 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1327
1328 " clean up
1329 call StopVimInTerminal(buf)
1330 call delete('XtestPopupAtcursorPos')
1331endfunc
1332
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001333func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001334 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001335 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001336
1337 let lines =<< trim END
1338 call setline(1, range(1, 20))
1339 call setline(5, 'here is some text to hover over')
1340 set balloonevalterm
1341 set balloonexpr=BalloonExpr()
1342 set balloondelay=100
1343 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001344 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001345 return ''
1346 endfunc
1347 func Hover()
1348 call test_setmouse(5, 15)
1349 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1350 sleep 100m
1351 endfunc
1352 func MoveOntoPopup()
1353 call test_setmouse(4, 17)
1354 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1355 endfunc
1356 func MoveAway()
1357 call test_setmouse(5, 13)
1358 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1359 endfunc
1360 END
1361 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001362 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001363 call term_wait(buf, 100)
1364 call term_sendkeys(buf, 'j')
1365 call term_sendkeys(buf, ":call Hover()\<CR>")
1366 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1367
1368 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1369 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1370
1371 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1372 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1373
1374 " clean up
1375 call StopVimInTerminal(buf)
1376 call delete('XtestPopupBeval')
1377endfunc
1378
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001379func Test_popup_filter()
1380 new
1381 call setline(1, 'some text')
1382
1383 func MyPopupFilter(winid, c)
1384 if a:c == 'e'
1385 let g:eaten = 'e'
1386 return 1
1387 endif
1388 if a:c == '0'
1389 let g:ignored = '0'
1390 return 0
1391 endif
1392 if a:c == 'x'
1393 call popup_close(a:winid)
1394 return 1
1395 endif
1396 return 0
1397 endfunc
1398
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001399 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001400 redraw
1401
1402 " e is consumed by the filter
1403 call feedkeys('e', 'xt')
1404 call assert_equal('e', g:eaten)
1405
1406 " 0 is ignored by the filter
1407 normal $
1408 call assert_equal(9, getcurpos()[2])
1409 call feedkeys('0', 'xt')
1410 call assert_equal('0', g:ignored)
1411 call assert_equal(1, getcurpos()[2])
1412
1413 " x closes the popup
1414 call feedkeys('x', 'xt')
1415 call assert_equal('e', g:eaten)
1416 call assert_equal(-1, winbufnr(winid))
1417
1418 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001419 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001420endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001421
Bram Moolenaara42d9452019-06-15 21:46:30 +02001422func ShowDialog(key, result)
1423 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001424 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001425 \ filter: 'popup_filter_yesno',
1426 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001427 \ })
1428 redraw
1429 call feedkeys(a:key, "xt")
1430 call assert_equal(winid, s:cb_winid)
1431 call assert_equal(a:result, s:cb_res)
1432endfunc
1433
1434func Test_popup_dialog()
1435 func QuitCallback(id, res)
1436 let s:cb_winid = a:id
1437 let s:cb_res = a:res
1438 endfunc
1439
1440 let winid = ShowDialog("y", 1)
1441 let winid = ShowDialog("Y", 1)
1442 let winid = ShowDialog("n", 0)
1443 let winid = ShowDialog("N", 0)
1444 let winid = ShowDialog("x", 0)
1445 let winid = ShowDialog("X", 0)
1446 let winid = ShowDialog("\<Esc>", 0)
1447 let winid = ShowDialog("\<C-C>", -1)
1448
1449 delfunc QuitCallback
1450endfunc
1451
Bram Moolenaara730e552019-06-16 19:05:31 +02001452func ShowMenu(key, result)
1453 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001454 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001455 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001456 \ })
1457 redraw
1458 call feedkeys(a:key, "xt")
1459 call assert_equal(winid, s:cb_winid)
1460 call assert_equal(a:result, s:cb_res)
1461endfunc
1462
1463func Test_popup_menu()
1464 func QuitCallback(id, res)
1465 let s:cb_winid = a:id
1466 let s:cb_res = a:res
1467 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001468 " mapping won't be used in popup
1469 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001470
1471 let winid = ShowMenu(" ", 1)
1472 let winid = ShowMenu("j \<CR>", 2)
1473 let winid = ShowMenu("JjK \<CR>", 2)
1474 let winid = ShowMenu("jjjjjj ", 3)
1475 let winid = ShowMenu("kkk ", 1)
1476 let winid = ShowMenu("x", -1)
1477 let winid = ShowMenu("X", -1)
1478 let winid = ShowMenu("\<Esc>", -1)
1479 let winid = ShowMenu("\<C-C>", -1)
1480
1481 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001482 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001483endfunc
1484
1485func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001486 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001487
1488 let lines =<< trim END
1489 call setline(1, range(1, 20))
1490 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001491 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001492 func MenuDone(id, res)
1493 echomsg "selected " .. a:res
1494 endfunc
1495 END
1496 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001497 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001498 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1499
1500 call term_sendkeys(buf, "jj")
1501 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1502
1503 call term_sendkeys(buf, " ")
1504 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1505
1506 " clean up
1507 call StopVimInTerminal(buf)
1508 call delete('XtestPopupMenu')
1509endfunc
1510
Bram Moolenaarf914a332019-07-20 15:09:56 +02001511func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001512 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001513
1514 let lines =<< trim END
1515 call setline(1, range(1, 20))
1516 hi PopupSelected ctermbg=green
1517 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1518 func MenuDone(id, res)
1519 echomsg "selected " .. a:res
1520 endfunc
1521 END
1522 call writefile(lines, 'XtestPopupNarrowMenu')
1523 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1524 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1525
1526 " clean up
1527 call term_sendkeys(buf, "x")
1528 call StopVimInTerminal(buf)
1529 call delete('XtestPopupNarrowMenu')
1530endfunc
1531
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001532func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001533 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001534
1535 " Create a popup without title or border, a line of padding will be added to
1536 " put the title on.
1537 let lines =<< trim END
1538 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001539 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001540 END
1541 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001542 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001543 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1544
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001545 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1546 call term_sendkeys(buf, ":\<CR>")
1547 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1548
1549 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1550 call term_sendkeys(buf, ":\<CR>")
1551 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1552
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001553 " clean up
1554 call StopVimInTerminal(buf)
1555 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001556
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001557 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001558 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001559 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001560 call assert_equal('Another Title', popup_getoptions(winid).title)
1561
1562 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001563endfunc
1564
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001565func Test_popup_close_callback()
1566 func PopupDone(id, result)
1567 let g:result = a:result
1568 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001569 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001570 redraw
1571 call popup_close(winid, 'done')
1572 call assert_equal('done', g:result)
1573endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001574
1575func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001576 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001577 redraw
1578 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001579 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001580 call assert_equal(5, pos.height)
1581
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001582 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001583 redraw
1584 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001585 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001586 call assert_equal(3, pos.height)
1587endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001588
1589func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001590 CheckScreendump
1591
Bram Moolenaar988c4332019-06-02 14:12:11 +02001592 " +-----------------------------+
1593 " | | |
1594 " | | |
1595 " | | |
1596 " | line1 |
1597 " |------------line2------------|
1598 " | line3 |
1599 " | line4 |
1600 " | |
1601 " | |
1602 " +-----------------------------+
1603 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001604 split
1605 vsplit
1606 let info_window1 = getwininfo()[0]
1607 let line = info_window1['height']
1608 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001609 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001610 \ line : line,
1611 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001612 \ })
1613 END
1614 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001615 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001616 call term_sendkeys(buf, "\<C-W>w")
1617 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1618
1619 " clean up
1620 call StopVimInTerminal(buf)
1621 call delete('XtestPopupBehind')
1622endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001623
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001624func s:VerifyPosition(p, msg, line, col, width, height)
1625 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1626 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1627 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1628 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001629endfunc
1630
1631func Test_popup_position_adjust()
1632 " Anything placed past 2 cells from of the right of the screen is moved to the
1633 " left.
1634 "
1635 " When wrapping is disabled, we also shift to the left to display on the
1636 " screen, unless fixed is set.
1637
1638 " Entries for cases which don't vary based on wrapping.
1639 " Format is per tests described below
1640 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001641 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1642 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1643 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1644 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1645 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1646 \
1647 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1648 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1649 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1650 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1651 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1652 \
1653 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1654 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1655 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1656 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1657 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1658 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001659
1660 " these test groups are dicts with:
1661 " - comment: something to identify the group of tests by
1662 " - options: dict of options to merge with the row/col in tests
1663 " - tests: list of cases. Each one is a list with elements:
1664 " - text
1665 " - row
1666 " - col
1667 " - expected row
1668 " - expected col
1669 " - expected width
1670 " - expected height
1671 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001672 \ #{
1673 \ comment: 'left-aligned with wrapping',
1674 \ options: #{
1675 \ wrap: 1,
1676 \ pos: 'botleft',
1677 \ },
1678 \ tests: both_wrap_tests + [
1679 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1680 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1681 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1682 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1683 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1684 \ ],
1685 \ },
1686 \ #{
1687 \ comment: 'left aligned without wrapping',
1688 \ options: #{
1689 \ wrap: 0,
1690 \ pos: 'botleft',
1691 \ },
1692 \ tests: both_wrap_tests + [
1693 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1694 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1695 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1696 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1697 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1698 \ ],
1699 \ },
1700 \ #{
1701 \ comment: 'left aligned with fixed position',
1702 \ options: #{
1703 \ wrap: 0,
1704 \ fixed: 1,
1705 \ pos: 'botleft',
1706 \ },
1707 \ tests: both_wrap_tests + [
1708 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1709 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1710 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1711 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1712 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1713 \ ],
1714 \ },
1715 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001716
1717 for test_group in tests
1718 for test in test_group.tests
1719 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001720 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001721 \ line: line,
1722 \ col: col,
1723 \ }
1724 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001725
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001726 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001727
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001728 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001729 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1730 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001731 endfor
1732 endfor
1733
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001734 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001735 %bwipe!
1736endfunc
1737
Bram Moolenaar3397f742019-06-02 18:40:06 +02001738func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001739 " width of screen
1740 let X = join(map(range(&columns), {->'X'}), '')
1741
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001742 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1743 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001744
1745 redraw
1746 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1747 call assert_equal(X, line)
1748
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001749 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001750 redraw
1751
1752 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001753 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1754 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 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 call assert_equal(X, line)
1759
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001760 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001761 redraw
1762
1763 " Extend so > window width
1764 let X .= 'x'
1765
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001766 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1767 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001768
1769 redraw
1770 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1771 call assert_equal(X[ : -2 ], line)
1772
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001773 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001774 redraw
1775
1776 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001777 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1778 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001779
1780 redraw
1781 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1782 call assert_equal(X[ : -2 ], line)
1783
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001784 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001785 redraw
1786
1787 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001788 let p = popup_create(X,
1789 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1790 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001791
1792 redraw
1793 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1794 let e_line = ' ' . X[ 1 : -2 ]
1795 call assert_equal(e_line, line)
1796
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001797 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001798 redraw
1799
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001800 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001801 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001802endfunc
1803
1804func Test_popup_moved()
1805 new
1806 call test_override('char_avail', 1)
1807 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1808
1809 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001810 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001811 redraw
1812 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001813 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001814 " trigger the check for last_cursormoved by going into insert mode
1815 call feedkeys("li\<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: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001821 redraw
1822 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001823 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001824 call feedkeys("hi\<Esc>", 'xt')
1825 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001826 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001827
1828 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001829 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001830 redraw
1831 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001832 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001833 call feedkeys("li\<Esc>", 'xt')
1834 call assert_equal(1, popup_getpos(winid).visible)
1835 call feedkeys("ei\<Esc>", 'xt')
1836 call assert_equal(1, popup_getpos(winid).visible)
1837 call feedkeys("eli\<Esc>", 'xt')
1838 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001839 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001840
Bram Moolenaar17627312019-06-02 19:53:44 +02001841 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001842 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001843 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001844 redraw
1845 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001846 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001847 call feedkeys("eli\<Esc>", 'xt')
1848 call assert_equal(1, popup_getpos(winid).visible)
1849 call feedkeys("wi\<Esc>", 'xt')
1850 call assert_equal(1, popup_getpos(winid).visible)
1851 call feedkeys("Eli\<Esc>", 'xt')
1852 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001853 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001854
1855 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001856 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001857 redraw
1858 call assert_equal(1, popup_getpos(winid).visible)
1859 call feedkeys("eli\<Esc>", 'xt')
1860 call feedkeys("ei\<Esc>", 'xt')
1861 call assert_equal(1, popup_getpos(winid).visible)
1862 call feedkeys("eli\<Esc>", 'xt')
1863 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001864 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001865
1866 bwipe!
1867 call test_override('ALL', 0)
1868endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001869
1870func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001871 CheckFeature timers
1872 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001873
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001874 let lines =<< trim END
1875 call setline(1, range(1, 20))
1876 hi Notification ctermbg=lightblue
1877 call popup_notification('first notification', {})
1878 END
1879 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001880 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001881 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1882
1883 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001884 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1885 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001886 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1887
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001888 " clean up
1889 call StopVimInTerminal(buf)
1890 call delete('XtestNotifications')
1891endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001892
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001893func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001894 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001895
1896 let lines =<< trim END
1897 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001898 hi ScrollThumb ctermbg=blue
1899 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001900 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001901 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001902 \ minwidth: 8,
1903 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001904 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001905 func ScrollUp()
1906 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1907 endfunc
1908 func ScrollDown()
1909 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1910 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001911 func ClickTop()
1912 call feedkeys("\<F4>\<LeftMouse>", "xt")
1913 endfunc
1914 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001915 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001916 call feedkeys("\<F5>\<LeftMouse>", "xt")
1917 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001918 func Popup_filter(winid, key)
1919 if a:key == 'j'
1920 let line = popup_getoptions(a:winid).firstline
1921 let nlines = line('$', a:winid)
1922 let newline = line < nlines ? (line + 1) : nlines
1923 call popup_setoptions(a:winid, #{firstline: newline})
1924 return v:true
1925 elseif a:key == 'x'
1926 call popup_close(a:winid)
1927 return v:true
1928 endif
1929 endfunc
1930
1931 func PopupScroll()
1932 call popup_clear()
1933 let text =<< trim END
1934 1
1935 2
1936 3
1937 4
1938 long line long line long line long line long line long line
1939 long line long line long line long line long line long line
1940 long line long line long line long line long line long line
1941 END
1942 call popup_create(text, #{
1943 \ minwidth: 30,
1944 \ maxwidth: 30,
1945 \ minheight: 4,
1946 \ maxheight: 4,
1947 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +01001948 \ lastline: 4,
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001949 \ wrap: v:true,
1950 \ scrollbar: v:true,
1951 \ mapping: v:false,
1952 \ filter: funcref('Popup_filter')
1953 \ })
1954 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001955 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001956 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1957 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001958 END
1959 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001960 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001961 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1962
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001963 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001964 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001965 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1966
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001967 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001968 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001969 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1970
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001971 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001972 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001973 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1974
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02001975 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001976 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001977 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1978 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1979
1980 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1981 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1982
1983 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001984 " wait a bit, otherwise it fails sometimes (double click recognized?)
1985 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001986 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1987 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1988
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001989 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1990 sleep 100m
1991 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1992 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1993
1994 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1995 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1996
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001997 " remove the minwidth and maxheight
1998 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02001999 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002000 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2001
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002002 " check size with non-wrapping lines
2003 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2004 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2005
2006 " check size with wrapping lines
2007 call term_sendkeys(buf, "j")
2008 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
2009 call term_sendkeys(buf, "x")
2010
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002011 " clean up
2012 call StopVimInTerminal(buf)
2013 call delete('XtestPopupScroll')
2014endfunc
2015
Bram Moolenaar437a7462019-07-05 20:17:22 +02002016func Test_popup_fitting_scrollbar()
2017 " this was causing a crash, divide by zero
2018 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002019 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002020 \ scrollbar: 1,
2021 \ maxwidth: 10,
2022 \ maxheight: 5,
2023 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002024 redraw
2025 call popup_clear()
2026endfunc
2027
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002028func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002029 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002030
2031 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002032 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002033 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002034 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002035 END
2036
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002037 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002038 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002039 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2040
2041 " Setting to empty string clears it
2042 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2043 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2044
2045 " Setting a list
2046 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2047 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2048
2049 " Shrinking with a list
2050 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2051 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2052
2053 " Growing with a list
2054 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2055 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2056
2057 " Empty list clears
2058 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2059 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2060
2061 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002062 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002063 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2064
2065 " clean up
2066 call StopVimInTerminal(buf)
2067 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002068endfunc
2069
2070func Test_popup_hidden()
2071 new
2072
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002073 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002074 redraw
2075 call assert_equal(0, popup_getpos(winid).visible)
2076 call popup_close(winid)
2077
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002078 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002079 redraw
2080 call assert_equal(0, popup_getpos(winid).visible)
2081 call popup_close(winid)
2082
2083 func QuitCallback(id, res)
2084 let s:cb_winid = a:id
2085 let s:cb_res = a:res
2086 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002087 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002088 \ filter: 'popup_filter_yesno',
2089 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002090 \ })
2091 redraw
2092 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002093 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2094 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002095 exe "normal anot used by filter\<Esc>"
2096 call assert_equal('not used by filter', getline(1))
2097
2098 call popup_show(winid)
2099 call feedkeys('y', "xt")
2100 call assert_equal(1, s:cb_res)
2101
2102 bwipe!
2103 delfunc QuitCallback
2104endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002105
2106" Test options not checked elsewhere
2107func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002108 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002109 let options = popup_getoptions(winid)
2110 call assert_equal(1, options.wrap)
2111 call assert_equal(0, options.drag)
2112 call assert_equal('Beautiful', options.highlight)
2113
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002114 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002115 let options = popup_getoptions(winid)
2116 call assert_equal(0, options.wrap)
2117 call assert_equal(1, options.drag)
2118 call assert_equal('Another', options.highlight)
2119
2120 call popup_close(winid)
2121endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002122
2123func Test_popupwin_garbage_collect()
2124 func MyPopupFilter(x, winid, c)
2125 " NOP
2126 endfunc
2127
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002128 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002129 call test_garbagecollect_now()
2130 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002131 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002132 call feedkeys('j', 'xt')
2133 call assert_true(v:true)
2134
2135 call popup_close(winid)
2136 delfunc MyPopupFilter
2137endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002138
Bram Moolenaar581ba392019-09-03 22:08:33 +02002139func Test_popupwin_filter_mode()
2140 func MyPopupFilter(winid, c)
2141 let s:typed = a:c
2142 if a:c == ':' || a:c == "\r" || a:c == 'v'
2143 " can start cmdline mode, get out, and start/stop Visual mode
2144 return 0
2145 endif
2146 return 1
2147 endfunc
2148
2149 " Normal, Visual and Insert mode
2150 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2151 redraw
2152 call feedkeys('x', 'xt')
2153 call assert_equal('x', s:typed)
2154
2155 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2156 call assert_equal(':', s:typed)
2157 call assert_equal('foo', 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
2167 " only Normal mode
2168 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2169 redraw
2170 call feedkeys('x', 'xt')
2171 call assert_equal('x', s:typed)
2172
2173 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2174 call assert_equal(':', s:typed)
2175 call assert_equal('foo', g:foo)
2176
2177 let @x = 'something'
2178 call feedkeys('v$"xy', 'xt')
2179 call assert_equal('v', s:typed)
2180 call assert_notequal('something', @x)
2181
2182 call popup_close(winid)
2183
2184 " default: all modes
2185 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2186 redraw
2187 call feedkeys('x', 'xt')
2188 call assert_equal('x', s:typed)
2189
2190 let g:foo = 'bar'
2191 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2192 call assert_equal("\r", s:typed)
2193 call assert_equal('bar', g:foo)
2194
2195 let @x = 'something'
2196 call feedkeys('v$"xy', 'xt')
2197 call assert_equal('y', s:typed)
2198 call assert_equal('something', @x) " yank command is filtered out
2199 call feedkeys('v', 'xt') " end Visual mode
2200
2201 call popup_close(winid)
2202 delfunc MyPopupFilter
2203endfunc
2204
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002205func Test_popupwin_filter_mouse()
2206 func MyPopupFilter(winid, c)
2207 let g:got_mouse_col = v:mouse_col
2208 let g:got_mouse_lnum = v:mouse_lnum
2209 return 0
2210 endfunc
2211
2212 let winid = popup_create(['short', 'long line that will wrap', 'short'], #{
2213 \ line: 4,
2214 \ col: 8,
2215 \ maxwidth: 12,
2216 \ filter: 'MyPopupFilter',
2217 \ })
2218 redraw
2219 call test_setmouse(4, 8)
2220 call feedkeys("\<LeftMouse>", 'xt')
2221 call assert_equal(1, g:got_mouse_col)
2222 call assert_equal(1, g:got_mouse_lnum)
2223
2224 call test_setmouse(5, 8)
2225 call feedkeys("\<LeftMouse>", 'xt')
2226 call assert_equal(1, g:got_mouse_col)
2227 call assert_equal(2, g:got_mouse_lnum)
2228
2229 call test_setmouse(6, 8)
2230 call feedkeys("\<LeftMouse>", 'xt')
2231 call assert_equal(13, g:got_mouse_col)
2232 call assert_equal(2, g:got_mouse_lnum)
2233
2234 call test_setmouse(7, 20)
2235 call feedkeys("\<LeftMouse>", 'xt')
2236 call assert_equal(13, g:got_mouse_col)
2237 call assert_equal(3, g:got_mouse_lnum)
2238
2239 call popup_close(winid)
2240 delfunc MyPopupFilter
2241endfunc
2242
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002243func Test_popupwin_with_buffer()
2244 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2245 let buf = bufadd('XsomeFile')
2246 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002247
2248 setlocal number
2249 call setbufvar(buf, "&wrapmargin", 13)
2250
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002251 let winid = popup_create(buf, {})
2252 call assert_notequal(0, winid)
2253 let pos = popup_getpos(winid)
2254 call assert_equal(2, pos.height)
2255 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002256
2257 " window-local option is set to default, buffer-local is not
2258 call assert_equal(0, getwinvar(winid, '&number'))
2259 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2260
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002261 call popup_close(winid)
2262 call assert_equal({}, popup_getpos(winid))
2263 call assert_equal(1, bufloaded(buf))
2264 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002265 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002266
2267 edit test_popupwin.vim
2268 let winid = popup_create(bufnr(''), {})
2269 redraw
2270 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002271 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002272endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002273
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002274func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002275 CheckFeature terminal
2276
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002277 let ptybuf = term_start(&shell, #{hidden: 1})
2278 call assert_fails('let winnr = popup_create(ptybuf, #{})', 'E278:')
2279 exe 'bwipe! ' .. ptybuf
2280endfunc
2281
Bram Moolenaar934470e2019-09-01 23:27:05 +02002282func Test_popupwin_with_buffer_and_filter()
2283 new Xwithfilter
2284 call setline(1, range(100))
2285 let bufnr = bufnr()
2286 hide
2287
2288 func BufferFilter(win, key)
2289 if a:key == 'G'
2290 " recursive use of "G" does not cause problems.
2291 call win_execute(a:win, 'normal! G')
2292 return 1
2293 endif
2294 return 0
2295 endfunc
2296
2297 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2298 call assert_equal(1, popup_getpos(winid).firstline)
2299 redraw
2300 call feedkeys("G", 'xt')
2301 call assert_equal(99, popup_getpos(winid).firstline)
2302
2303 call popup_close(winid)
2304 exe 'bwipe! ' .. bufnr
2305endfunc
2306
Bram Moolenaare296e312019-07-03 23:20:18 +02002307func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002308 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002309 \ maxwidth: 40,
2310 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002311 \ })
2312 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002313 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002314 redraw
2315 call assert_equal(19, popup_getpos(winid).width)
2316 endfor
2317 call popup_clear()
2318endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002319
2320func Test_popupwin_buf_close()
2321 let buf = bufadd('Xtestbuf')
2322 call bufload(buf)
2323 call setbufline(buf, 1, ['just', 'some', 'lines'])
2324 let winid = popup_create(buf, {})
2325 redraw
2326 call assert_equal(3, popup_getpos(winid).height)
2327 let bufinfo = getbufinfo(buf)[0]
2328 call assert_equal(1, bufinfo.changed)
2329 call assert_equal(0, bufinfo.hidden)
2330 call assert_equal(0, bufinfo.listed)
2331 call assert_equal(1, bufinfo.loaded)
2332 call assert_equal([], bufinfo.windows)
2333 call assert_equal([winid], bufinfo.popups)
2334
2335 call popup_close(winid)
2336 call assert_equal({}, popup_getpos(winid))
2337 let bufinfo = getbufinfo(buf)[0]
2338 call assert_equal(1, bufinfo.changed)
2339 call assert_equal(1, bufinfo.hidden)
2340 call assert_equal(0, bufinfo.listed)
2341 call assert_equal(1, bufinfo.loaded)
2342 call assert_equal([], bufinfo.windows)
2343 call assert_equal([], bufinfo.popups)
2344 exe 'bwipe! ' .. buf
2345endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002346
2347func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002348 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002349
2350 let lines =<< trim END
2351 call setline(1, range(1, 10))
2352 hi ScrollThumb ctermbg=blue
2353 hi ScrollBar ctermbg=red
2354 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002355 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002356 \ maxwidth: 10,
2357 \ maxheight: 3,
2358 \ pos : 'topleft',
2359 \ col : a:col,
2360 \ line : a:line,
2361 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002362 \ })
2363 endfunc
2364 call PopupMenu(['x'], 1, 1)
2365 call PopupMenu(['123456789|'], 1, 16)
2366 call PopupMenu(['123456789|' .. ' '], 7, 1)
2367 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2368 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2369 END
2370 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002371 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002372 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2373
2374 " close the menu popupwin.
2375 call term_sendkeys(buf, " ")
2376 call term_sendkeys(buf, " ")
2377 call term_sendkeys(buf, " ")
2378 call term_sendkeys(buf, " ")
2379 call term_sendkeys(buf, " ")
2380
2381 " clean up
2382 call StopVimInTerminal(buf)
2383 call delete('XtestPopupMenuMaxWidth')
2384endfunc
2385
Bram Moolenaara901a372019-07-13 16:38:50 +02002386func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002387 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002388
2389 let lines =<< trim END
2390 call setline(1, range(1, 20))
2391 hi ScrollThumb ctermbg=blue
2392 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002393 eval ['one', 'two', 'three', 'four', 'five',
2394 \ 'six', 'seven', 'eight', 'nine']
2395 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002396 \ minwidth: 8,
2397 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002398 \ })
2399 END
2400 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002401 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002402
2403 call term_sendkeys(buf, "j")
2404 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2405
2406 call term_sendkeys(buf, "jjj")
2407 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2408
2409 " if the cursor is the bottom line, it stays at the bottom line.
2410 call term_sendkeys(buf, repeat("j", 20))
2411 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2412
2413 call term_sendkeys(buf, "kk")
2414 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2415
2416 call term_sendkeys(buf, "k")
2417 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2418
2419 " if the cursor is in the top line, it stays in the top line.
2420 call term_sendkeys(buf, repeat("k", 20))
2421 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2422
2423 " close the menu popupwin.
2424 call term_sendkeys(buf, " ")
2425
2426 " clean up
2427 call StopVimInTerminal(buf)
2428 call delete('XtestPopupMenuScroll')
2429endfunc
2430
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002431func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002432 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002433
2434 let lines =<< trim END
2435 function! MyFilter(winid, key) abort
2436 if a:key == "0"
2437 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2438 return 1
2439 endif
2440 if a:key == "G"
2441 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2442 return 1
2443 endif
2444 if a:key == "j"
2445 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2446 return 1
2447 endif
2448 if a:key == "k"
2449 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2450 return 1
2451 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002452 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002453 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002454 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002455 endif
2456 return 0
2457 endfunction
2458 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2459 \ maxheight : 3,
2460 \ filter : 'MyFilter'
2461 \ })
2462 END
2463 call writefile(lines, 'XtestPopupMenuFilter')
2464 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2465
2466 call term_sendkeys(buf, "j")
2467 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2468
2469 call term_sendkeys(buf, "k")
2470 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2471
2472 call term_sendkeys(buf, "G")
2473 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2474
2475 call term_sendkeys(buf, "0")
2476 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2477
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002478 " check that when the popup is closed in the filter the screen is redrawn
2479 call term_sendkeys(buf, ":")
2480 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2481 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002482
2483 " clean up
2484 call StopVimInTerminal(buf)
2485 call delete('XtestPopupMenuFilter')
2486endfunc
2487
2488func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002489 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002490
2491 let winid = popup_create('some text', {})
2492 call assert_equal(0, popup_getoptions(winid).cursorline)
2493 call popup_close(winid)
2494
2495 let winid = popup_create('some text', #{ cursorline: 1, })
2496 call assert_equal(1, popup_getoptions(winid).cursorline)
2497 call popup_close(winid)
2498
2499 let winid = popup_create('some text', #{ cursorline: 0, })
2500 call assert_equal(0, popup_getoptions(winid).cursorline)
2501 call popup_close(winid)
2502
2503 let winid = popup_menu('some text', {})
2504 call assert_equal(1, popup_getoptions(winid).cursorline)
2505 call popup_close(winid)
2506
2507 let winid = popup_menu('some text', #{ cursorline: 1, })
2508 call assert_equal(1, popup_getoptions(winid).cursorline)
2509 call popup_close(winid)
2510
2511 let winid = popup_menu('some text', #{ cursorline: 0, })
2512 call assert_equal(0, popup_getoptions(winid).cursorline)
2513 call popup_close(winid)
2514
2515 " ---------
2516 " Pattern 1
2517 " ---------
2518 let lines =<< trim END
2519 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2520 END
2521 call writefile(lines, 'XtestPopupCursorLine')
2522 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2523 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2524 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2525 call StopVimInTerminal(buf)
2526
2527 " ---------
2528 " Pattern 2
2529 " ---------
2530 let lines =<< trim END
2531 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2532 END
2533 call writefile(lines, 'XtestPopupCursorLine')
2534 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2535 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2536 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2537 call StopVimInTerminal(buf)
2538
2539 " ---------
2540 " Pattern 3
2541 " ---------
2542 let lines =<< trim END
2543 function! MyFilter(winid, key) abort
2544 if a:key == "j"
2545 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2546 return 1
2547 endif
2548 if a:key == 'x'
2549 call popup_close(a:winid)
2550 return 1
2551 endif
2552 return 0
2553 endfunction
2554 call popup_menu(['111', '222', '333'], #{
2555 \ cursorline : 0,
2556 \ maxheight : 2,
2557 \ filter : 'MyFilter',
2558 \ })
2559 END
2560 call writefile(lines, 'XtestPopupCursorLine')
2561 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2562 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2563 call term_sendkeys(buf, "j")
2564 call term_sendkeys(buf, "j")
2565 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2566 call term_sendkeys(buf, "x")
2567 call StopVimInTerminal(buf)
2568
2569 " ---------
2570 " Pattern 4
2571 " ---------
2572 let lines =<< trim END
2573 function! MyFilter(winid, key) abort
2574 if a:key == "j"
2575 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2576 return 1
2577 endif
2578 if a:key == 'x'
2579 call popup_close(a:winid)
2580 return 1
2581 endif
2582 return 0
2583 endfunction
2584 call popup_menu(['111', '222', '333'], #{
2585 \ cursorline : 1,
2586 \ maxheight : 2,
2587 \ filter : 'MyFilter',
2588 \ })
2589 END
2590 call writefile(lines, 'XtestPopupCursorLine')
2591 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2592 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2593 call term_sendkeys(buf, "j")
2594 call term_sendkeys(buf, "j")
2595 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2596 call term_sendkeys(buf, "x")
2597 call StopVimInTerminal(buf)
2598
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002599 " ---------
2600 " Cursor in second line when creating the popup
2601 " ---------
2602 let lines =<< trim END
2603 let winid = popup_create(['111', '222', '333'], #{
2604 \ cursorline : 1,
2605 \ })
2606 call win_execute(winid, "2")
2607 END
2608 call writefile(lines, 'XtestPopupCursorLine')
2609 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2610 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2611 call StopVimInTerminal(buf)
2612
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002613 call delete('XtestPopupCursorLine')
2614endfunc
2615
Bram Moolenaarf914a332019-07-20 15:09:56 +02002616func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002617 CheckScreendump
2618
Bram Moolenaarf914a332019-07-20 15:09:56 +02002619 call writefile([
2620 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2621 \ "another\tXtagfile\t/^this is another",
2622 \ "theword\tXtagfile\t/^theword"],
2623 \ 'Xtags')
2624 call writefile(range(1,20)
2625 \ + ['theword is here']
2626 \ + range(22, 27)
2627 \ + ['this is another place']
2628 \ + range(29, 40),
2629 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002630 call writefile(range(1,10)
2631 \ + ['searched word is here']
2632 \ + range(12, 20),
2633 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002634 let lines =<< trim END
2635 set tags=Xtags
2636 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002637 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002638 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002639 \ 'three',
2640 \ 'four',
2641 \ 'five',
2642 \ 'six',
2643 \ 'seven',
2644 \ 'find theword somewhere',
2645 \ 'nine',
2646 \ 'this is another word',
2647 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002648 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002649 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002650 END
2651 call writefile(lines, 'XtestPreviewPopup')
2652 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2653
2654 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2655 call term_sendkeys(buf, ":\<CR>")
2656 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2657
2658 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2659 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2660
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002661 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002662 call term_sendkeys(buf, ":\<CR>")
2663 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2664
2665 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2666 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2667
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002668 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2669 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002670 call term_sendkeys(buf, ":cd testdir\<CR>")
2671
2672 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002673 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002674 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2675
2676 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2677 call term_sendkeys(buf, ":\<CR>")
2678 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2679
2680 call term_sendkeys(buf, ":pclose\<CR>")
2681 call term_sendkeys(buf, ":psearch searched\<CR>")
2682 call term_sendkeys(buf, ":\<CR>")
2683 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002684
Bram Moolenaarf914a332019-07-20 15:09:56 +02002685 call StopVimInTerminal(buf)
2686 call delete('Xtags')
2687 call delete('Xtagfile')
2688 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002689 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002690endfunc
2691
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002692func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002693 let lines =<< trim END
2694 set completeopt+=preview,popup
2695 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002696 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002697
2698 func CompleteFuncDict(findstart, base)
2699 if a:findstart
2700 if col('.') > 10
2701 return col('.') - 10
2702 endif
2703 return 0
2704 endif
2705
2706 return {
2707 \ 'words': [
2708 \ {
2709 \ 'word': 'aword',
2710 \ 'abbr': 'wrd',
2711 \ 'menu': 'extra text',
2712 \ 'info': 'words are cool',
2713 \ 'kind': 'W',
2714 \ 'user_data': 'test'
2715 \ },
2716 \ {
2717 \ 'word': 'anotherword',
2718 \ 'abbr': 'anotwrd',
2719 \ 'menu': 'extra text',
2720 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2721 \ 'kind': 'W',
2722 \ 'user_data': 'notest'
2723 \ },
2724 \ {
2725 \ 'word': 'noinfo',
2726 \ 'abbr': 'noawrd',
2727 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002728 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002729 \ 'kind': 'W',
2730 \ 'user_data': 'notest'
2731 \ },
2732 \ {
2733 \ 'word': 'thatword',
2734 \ 'abbr': 'thatwrd',
2735 \ 'menu': 'extra text',
2736 \ 'info': 'that word is cool',
2737 \ 'kind': 'W',
2738 \ 'user_data': 'notest'
2739 \ },
2740 \ ]
2741 \ }
2742 endfunc
2743 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002744 func ChangeColor()
2745 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002746 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002747 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002748
2749 func InfoHidden()
2750 set completepopup=height:4,border:off,align:menu
2751 set completeopt-=popup completeopt+=popuphidden
2752 au CompleteChanged * call HandleChange()
2753 endfunc
2754
2755 let s:counter = 0
2756 func HandleChange()
2757 let s:counter += 1
2758 let selected = complete_info(['selected']).selected
2759 if selected <= 0
2760 " First time: do nothing, info remains hidden
2761 return
2762 endif
2763 if selected == 1
2764 " Second time: show info right away
2765 let id = popup_findinfo()
2766 if id
2767 call popup_settext(id, 'immediate info ' .. s:counter)
2768 call popup_show(id)
2769 endif
2770 else
2771 " Third time: show info after a short delay
2772 call timer_start(100, 'ShowInfo')
2773 endif
2774 endfunc
2775
2776 func ShowInfo(...)
2777 let id = popup_findinfo()
2778 if id
2779 call popup_settext(id, 'async info ' .. s:counter)
2780 call popup_show(id)
2781 endif
2782 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002783 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002784 return lines
2785endfunc
2786
2787func Test_popupmenu_info_border()
2788 CheckScreendump
2789
2790 let lines = Get_popupmenu_lines()
2791 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002792 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002793
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002794 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2795 call term_wait(buf, 50)
2796
2797 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2798 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2799
2800 call term_sendkeys(buf, "\<C-N>")
2801 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2802
2803 call term_sendkeys(buf, "\<C-N>")
2804 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2805
2806 call term_sendkeys(buf, "\<C-N>\<C-N>")
2807 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2808
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002809 " info on the left with scrollbar
2810 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2811 call term_sendkeys(buf, "\<C-N>\<C-N>")
2812 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2813
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002814 call StopVimInTerminal(buf)
2815 call delete('XtestInfoPopup')
2816endfunc
2817
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002818func Test_popupmenu_info_noborder()
2819 CheckScreendump
2820
2821 let lines = Get_popupmenu_lines()
2822 call add(lines, 'set completepopup=height:4,border:off')
2823 call writefile(lines, 'XtestInfoPopupNb')
2824
2825 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2826 call term_wait(buf, 50)
2827
2828 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2829 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2830
2831 call StopVimInTerminal(buf)
2832 call delete('XtestInfoPopupNb')
2833endfunc
2834
Bram Moolenaar258cef52019-08-21 17:29:29 +02002835func Test_popupmenu_info_align_menu()
2836 CheckScreendump
2837
2838 let lines = Get_popupmenu_lines()
2839 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2840 call writefile(lines, 'XtestInfoPopupNb')
2841
2842 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2843 call term_wait(buf, 50)
2844
2845 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2846 call term_sendkeys(buf, "\<C-N>")
2847 call term_sendkeys(buf, "\<C-N>")
2848 call term_sendkeys(buf, "\<C-N>")
2849 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2850
2851 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2852 call term_sendkeys(buf, "\<C-N>")
2853 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2854
2855 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002856 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002857 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2858 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2859 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2860
2861 call StopVimInTerminal(buf)
2862 call delete('XtestInfoPopupNb')
2863endfunc
2864
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002865func Test_popupmenu_info_hidden()
2866 CheckScreendump
2867
2868 let lines = Get_popupmenu_lines()
2869 call add(lines, 'call InfoHidden()')
2870 call writefile(lines, 'XtestInfoPopupHidden')
2871
2872 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
2873 call term_wait(buf, 50)
2874
2875 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2876 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
2877
2878 call term_sendkeys(buf, "\<C-N>")
2879 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
2880
2881 call term_sendkeys(buf, "\<C-N>")
2882 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
2883
2884 call term_sendkeys(buf, "\<Esc>")
2885 call StopVimInTerminal(buf)
2886 call delete('XtestInfoPopupHidden')
2887endfunc
2888
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002889func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02002890 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002891 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002892 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002893 call assert_equal(bufnr, winbufnr(winid))
2894 call popup_clear()
2895endfunc
2896
Bram Moolenaar1824f452019-10-02 23:06:46 +02002897func Test_popupwin_getoptions_tablocal()
2898 topleft split
2899 let win1 = popup_create('nothing', #{maxheight: 8})
2900 let win2 = popup_create('something', #{maxheight: 10})
2901 let win3 = popup_create('something', #{maxheight: 15})
2902 call assert_equal(8, popup_getoptions(win1).maxheight)
2903 call assert_equal(10, popup_getoptions(win2).maxheight)
2904 call assert_equal(15, popup_getoptions(win3).maxheight)
2905 call popup_clear()
2906 quit
2907endfunc
2908
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002909func Test_popupwin_cancel()
2910 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
2911 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
2912 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
2913 call assert_equal(5, popup_getpos(win1).line)
2914 call assert_equal(10, popup_getpos(win2).line)
2915 call assert_equal(15, popup_getpos(win3).line)
2916 " TODO: this also works without patch 8.1.2110
2917 call feedkeys("\<C-C>", 'xt')
2918 call assert_equal(5, popup_getpos(win1).line)
2919 call assert_equal(10, popup_getpos(win2).line)
2920 call assert_equal({}, popup_getpos(win3))
2921 call feedkeys("\<C-C>", 'xt')
2922 call assert_equal(5, popup_getpos(win1).line)
2923 call assert_equal({}, popup_getpos(win2))
2924 call assert_equal({}, popup_getpos(win3))
2925 call feedkeys("\<C-C>", 'xt')
2926 call assert_equal({}, popup_getpos(win1))
2927 call assert_equal({}, popup_getpos(win2))
2928 call assert_equal({}, popup_getpos(win3))
2929endfunc
2930
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002931" vim: shiftwidth=2 sts=2