blob: 1c5bcb28fe5ceab8adf85b80bd867afb6122d133 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature textprop
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
15 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020016 call prop_type_add('comment', #{highlight: 'Comment'})
17 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
18 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020019 call setwinvar(winid2, '&wincolor', 'PopupColor2')
20 END
21 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020022 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020023 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
24
Bram Moolenaarec583842019-05-26 14:11:23 +020025 " Add a tabpage
26 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020027 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020028 \ .. "#{text: 'other tab'},"
29 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020030 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020031 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020032 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020033 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
34
35 " switch back to first tabpage
36 call term_sendkeys(buf, "gt")
37 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
38
39 " close that tabpage
40 call term_sendkeys(buf, ":quit!\<CR>")
41 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
42
Bram Moolenaar202d9822019-06-11 21:56:30 +020043 " set 'columns' to a small value, size must be recomputed
44 call term_sendkeys(buf, ":let cols = &columns\<CR>")
45 call term_sendkeys(buf, ":set columns=12\<CR>")
46 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
47 call term_sendkeys(buf, ":let &columns = cols\<CR>")
48
Bram Moolenaar17146962019-05-30 00:12:11 +020049 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020050 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020051 call term_sendkeys(buf, ":redraw\<CR>")
52 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
53
Bram Moolenaar17146962019-05-30 00:12:11 +020054 " show not fitting line at bottom
55 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
56 call term_sendkeys(buf, ":redraw\<CR>")
57 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
58
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020059 " move popup over ruler
60 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020061 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020062 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020063 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
64
65 " clear all popups after moving the cursor a bit, so that ruler is updated
66 call term_sendkeys(buf, "axxx\<Esc>")
67 call term_wait(buf)
68 call term_sendkeys(buf, "0")
69 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020070 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020071 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
72
Bram Moolenaar4d784b22019-05-25 19:51:39 +020073 " clean up
74 call StopVimInTerminal(buf)
75 call delete('XtestPopup')
76endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020077
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020078func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020079 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020080
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020081 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020082 let lines =<< trim END
83 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020084 call popup_create('hello border', #{line: 2, col: 3, border: []})
85 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020086 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020087 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
88 call popup_create('paddings', #{line: 6, col: 23, padding: [1, 3, 2, 4]})
89 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
90 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020091 END
92 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
93 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020094 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020095 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
96
97 call StopVimInTerminal(buf)
98 call delete('XtestPopupBorder')
99 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200100
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200101 let lines =<< trim END
102 call setline(1, range(1, 100))
103 hi BlueColor ctermbg=lightblue
104 hi TopColor ctermbg=253
105 hi RightColor ctermbg=245
106 hi BottomColor ctermbg=240
107 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200108 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
109 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
110 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
111 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
112 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
113 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200114 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200115 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200116 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200117 END
118 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200119 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200120 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
121
Bram Moolenaarad24a712019-06-17 20:05:45 +0200122 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200123 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200124 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
125
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200126 " check multi-byte border only with 'ambiwidth' single
127 if &ambiwidth == 'single'
128 call term_sendkeys(buf, ":call MultiByte()\<CR>")
129 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
130 endif
131
Bram Moolenaar790498b2019-06-01 22:15:29 +0200132 call StopVimInTerminal(buf)
133 call delete('XtestPopupBorder')
134
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200135 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200136 \ line: 2,
137 \ core_line: 3,
138 \ col: 3,
139 \ core_col: 4,
140 \ width: 14,
141 \ core_width: 12,
142 \ height: 3,
143 \ core_height: 1,
144 \ firstline: 1,
145 \ scrollbar: 0,
146 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200147 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200148 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200149 let options = popup_getoptions(winid)
150 call assert_equal([], options.border)
151 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200152
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200153 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200154 let with_border_or_padding.width = 15
155 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200156 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200157 let options = popup_getoptions(winid)
158 call assert_false(has_key(options, "border"))
159 call assert_equal([], options.padding)
160
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200161 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200162 \ padding: [1, 2, 3, 4],
163 \ border: [4, 0, 7, 8],
164 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
165 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200166 \ })
167 let options = popup_getoptions(winid)
168 call assert_equal([1, 0, 1, 1], options.border)
169 call assert_equal([1, 2, 3, 4], options.padding)
170 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
171 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200172
Bram Moolenaara1396152019-10-20 18:46:05 +0200173 " Check that popup_setoptions() takes the output of popup_getoptions()
174 call popup_setoptions(winid, options)
175 call assert_equal(options, popup_getoptions(winid))
176
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200177 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
178 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200179 \ line: 3,
180 \ core_line: 5,
181 \ col: 8,
182 \ core_col: 10,
183 \ width: 14,
184 \ core_width: 10,
185 \ height: 5,
186 \ scrollbar: 0,
187 \ core_height: 1,
188 \ firstline: 1,
189 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200190
191 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200192endfunc
193
Bram Moolenaarb4230122019-05-30 18:40:53 +0200194func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200195 CheckScreendump
196
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200197 let lines =<< trim END
198 call setline(1, range(1, 100))
199 hi PopupColor ctermbg=lightblue
200 let winid = popup_create([
201 \ '#include <stdio.h>',
202 \ 'int main(void)',
203 \ '{',
204 \ ' printf(123);',
205 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200206 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200207 call win_execute(winid, 'set syntax=cpp')
208 END
209 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200210 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200211 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
212
213 " clean up
214 call StopVimInTerminal(buf)
215 call delete('XtestPopup')
216endfunc
217
218func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200219 CheckScreendump
220
Bram Moolenaar402502d2019-05-30 22:07:36 +0200221 let lines =<< trim END
222 call setline(1, range(1, 100))
223 hi PopupColor ctermbg=lightgrey
224 let winid = popup_create([
225 \ '#include <stdio.h>',
226 \ 'int main(void)',
227 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200228 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200229 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200230 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200231 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
232 END
233 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200234 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200235 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
236
237 " clean up
238 call StopVimInTerminal(buf)
239 call delete('XtestPopup')
240endfunc
241
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200242func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200243 CheckScreendump
244
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200245 let lines =<< trim END
246 call setline(1, ['111 222 333', '444 555 666'])
247 let winid = popup_create([
248 \ '111 222 333',
249 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200250 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200251 set hlsearch
252 /666
253 call matchadd('ErrorMsg', '111')
254 call matchadd('ErrorMsg', '444')
255 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
256 call win_execute(winid, "call matchadd('ErrorMsg', '555')")
257 END
258 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200259 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200260 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
261
262 " clean up
263 call StopVimInTerminal(buf)
264 call delete('XtestPopupMatches')
265endfunc
266
Bram Moolenaar399d8982019-06-02 15:34:29 +0200267func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200268 CheckScreendump
269
Bram Moolenaar399d8982019-06-02 15:34:29 +0200270 let lines =<< trim END
271 call setline(1, repeat([repeat('-', 60)], 15))
272 set so=0
273 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200274 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200275 \ line: 'cursor+1',
276 \ col: 'cursor',
277 \ pos: 'topleft',
278 \ border: [],
279 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200280 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200281 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200282 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200283 \ line: 'cursor+1',
284 \ col: 'cursor',
285 \ pos: 'topright',
286 \ border: [],
287 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200288 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200289 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200290 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200291 \ line: 'cursor-1',
292 \ col: 'cursor',
293 \ pos: 'botleft',
294 \ border: [],
295 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200296 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200297 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200298 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200299 \ line: 'cursor-1',
300 \ col: 'cursor',
301 \ pos: 'botright',
302 \ border: [],
303 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200304 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200305 normal 1G51|r*
306 let winid1 = popup_create(['one', 'two'], #{
307 \ line: 'cursor-1',
308 \ col: 'cursor',
309 \ pos: 'botleft',
310 \ border: [],
311 \ padding: [],
312 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200313 END
314 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200315 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200316 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
317
318 " clean up
319 call StopVimInTerminal(buf)
320 call delete('XtestPopupCorners')
321endfunc
322
Bram Moolenaar8d241042019-06-12 23:40:01 +0200323func Test_popup_firstline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200324 CheckScreendump
325
Bram Moolenaar8d241042019-06-12 23:40:01 +0200326 let lines =<< trim END
327 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200328 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200329 \ maxheight: 4,
330 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200331 \ })
332 END
333 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200334 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200335 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
336
337 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
338 call term_sendkeys(buf, ":\<CR>")
339 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200340
341 " clean up
342 call StopVimInTerminal(buf)
343 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200344
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200345 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200346 \ maxheight: 2,
347 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200348 \ })
349 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200350 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200351 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200352 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200353
354 let winid = popup_create(['xxx']->repeat(50), #{
355 \ maxheight: 3,
356 \ firstline: 11,
357 \ })
358 redraw
359 call assert_equal(11, popup_getoptions(winid).firstline)
360 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200361 " check line() works with popup window
362 call assert_equal(11, line('.', winid))
363 call assert_equal(50, line('$', winid))
364 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200365
366 " Normal command changes what is displayed but not "firstline"
367 call win_execute(winid, "normal! \<c-y>")
368 call assert_equal(11, popup_getoptions(winid).firstline)
369 call assert_equal(10, popup_getpos(winid).firstline)
370
371 " Making some property change applies "firstline" again
372 call popup_setoptions(winid, #{line: 4})
373 call assert_equal(11, popup_getoptions(winid).firstline)
374 call assert_equal(11, popup_getpos(winid).firstline)
375
376 " Remove "firstline" property and scroll
377 call popup_setoptions(winid, #{firstline: 0})
378 call win_execute(winid, "normal! \<c-y>")
379 call assert_equal(0, popup_getoptions(winid).firstline)
380 call assert_equal(10, popup_getpos(winid).firstline)
381
382 " Making some property change has no side effect
383 call popup_setoptions(winid, #{line: 3})
384 call assert_equal(0, popup_getoptions(winid).firstline)
385 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaarae943152019-06-16 22:54:14 +0200386
387 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200388endfunc
389
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200390func Test_popup_noscrolloff()
391 set scrolloff=5
392 let winid = popup_create(['xxx']->repeat(50), #{
393 \ maxheight: 5,
394 \ firstline: 11,
395 \ })
396 redraw
397 call assert_equal(11, popup_getoptions(winid).firstline)
398 call assert_equal(11, popup_getpos(winid).firstline)
399
400 call popup_setoptions(winid, #{firstline: 0})
401 call win_execute(winid, "normal! \<c-y>")
402 call assert_equal(0, popup_getoptions(winid).firstline)
403 call assert_equal(10, popup_getpos(winid).firstline)
404
405 call popup_close(winid)
406endfunc
407
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200408func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200409 CheckScreendump
410
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200411 " create a popup that covers the command line
412 let lines =<< trim END
413 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200414 split
415 vsplit
416 $wincmd w
417 vsplit
418 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200419 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200420 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200421 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200422 \ border: [],
423 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200424 \ })
425 func Dragit()
426 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
427 endfunc
428 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200429 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200430 func Resize()
431 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
432 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200433 map <silent> <F5> :call test_setmouse(6, 21)<CR>
434 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200435 END
436 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200437 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200438 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
439
440 call term_sendkeys(buf, ":call Dragit()\<CR>")
441 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
442
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200443 call term_sendkeys(buf, ":call Resize()\<CR>")
444 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
445
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200446 " clean up
447 call StopVimInTerminal(buf)
448 call delete('XtestPopupDrag')
449endfunc
450
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200451func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200452 CheckScreendump
453
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200454 let lines =<< trim END
455 call setline(1, range(1, 20))
456 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200457 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200458 \ close: 'button',
459 \ border: [],
460 \ line: 1,
461 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200462 \ })
463 func CloseMsg(id, result)
464 echomsg 'Popup closed with ' .. a:result
465 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200466 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200467 \ close: 'click',
468 \ line: 3,
469 \ col: 15,
470 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200471 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200472 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200473 \ close: 'button',
474 \ line: 5,
475 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200476 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200477 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200478 \ close: 'button',
479 \ padding: [],
480 \ line: 5,
481 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200482 \ })
483 func CloseWithX()
484 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
485 endfunc
486 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
487 func CloseWithClick()
488 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
489 endfunc
490 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200491 func CreateWithMenuFilter()
492 let winid = popup_create('barfoo', #{
493 \ close: 'button',
494 \ filter: 'popup_filter_menu',
495 \ border: [],
496 \ line: 1,
497 \ col: 40,
498 \ })
499 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200500 END
501 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200502 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200503 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
504
505 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
506 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
507
508 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
509 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
510
Bram Moolenaarf6396232019-08-24 19:36:00 +0200511 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
512 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
513
514 " We have to send the actual mouse code, feedkeys() would be caught the
515 " filter.
516 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
517 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
518
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200519 " clean up
520 call StopVimInTerminal(buf)
521 call delete('XtestPopupClose')
522endfunction
523
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200524func Test_popup_menu_wrap()
525 CheckScreendump
526
527 let lines =<< trim END
528 call setline(1, range(1, 20))
529 call popup_create([
530 \ 'one',
531 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
532 \ 'three',
533 \ 'four',
534 \ ], #{
535 \ pos: "botleft",
536 \ border: [],
537 \ padding: [0,1,0,1],
538 \ maxheight: 3,
539 \ cursorline: 1,
540 \ filter: 'popup_filter_menu',
541 \ })
542 END
543 call writefile(lines, 'XtestPopupWrap')
544 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
545 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
546
547 call term_sendkeys(buf, "jj")
548 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
549
550 " clean up
551 call term_sendkeys(buf, "\<Esc>")
552 call StopVimInTerminal(buf)
553 call delete('XtestPopupWrap')
554endfunction
555
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200556func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200557 CheckScreendump
558
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200559 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200560 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200561 hi PopupColor ctermbg=lightgrey
562 let winid = popup_create([
563 \ 'some text',
564 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200565 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200566 \ line: 1,
567 \ col: 10,
568 \ wrap: 0,
569 \ fixed: 1,
570 \ zindex: 90,
571 \ padding: [],
572 \ highlight: 'PopupColor',
573 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200574 call popup_create([
575 \ 'xxxxxxxxx',
576 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200577 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200578 \ line: 3,
579 \ col: 18,
580 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200581 let winidb = popup_create([
582 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200583 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200584 \ line: 7,
585 \ col: 10,
586 \ wrap: 0,
587 \ fixed: 1,
588 \ close: 'button',
589 \ zindex: 90,
590 \ padding: [],
591 \ border: [],
592 \ 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 +0200593 END
594 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200595 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200596 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
597
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200598 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
599 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200600 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200601 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
602
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200603 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
604 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200605 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200606 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
607
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200608 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
609 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200610 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200611 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
612
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200613 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
614 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200615 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200616 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
617
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200618 " clean up
619 call StopVimInTerminal(buf)
620 call delete('XtestPopupMask')
621endfunc
622
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200623func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200624 CheckScreendump
625 CheckFeature clipboard_working
626
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200627 " create a popup with some text to be selected
628 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200629 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200630 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200631 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200632 \ drag: 1,
633 \ border: [],
634 \ line: 3,
635 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200636 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200637 \ })
638 func Select1()
639 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
640 endfunc
641 map <silent> <F3> :call test_setmouse(4, 15)<CR>
642 map <silent> <F4> :call test_setmouse(6, 23)<CR>
643 END
644 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200645 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200646 call term_sendkeys(buf, ":call Select1()\<CR>")
647 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
648
649 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
650 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200651 " clean the command line, sometimes it still shows a command
652 call term_sendkeys(buf, ":\<esc>")
653
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200654 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
655
656 " clean up
657 call StopVimInTerminal(buf)
658 call delete('XtestPopupSelect')
659endfunc
660
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200661func Test_popup_in_tab()
662 " default popup is local to tab, not visible when in other tab
663 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200664 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200665 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200666 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200667 tabnew
668 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200669 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200670 quit
671 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200672
673 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200674 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200675 " buffer is gone now
676 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200677
678 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200679 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200680 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200681 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200682 tabnew
683 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200684 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200685 quit
686 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200687 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200688
689 " create popup in other tab
690 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200691 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200692 call assert_equal(0, popup_getpos(winid).visible)
693 call assert_equal(1, popup_getoptions(winid).tabpage)
694 quit
695 call assert_equal(1, popup_getpos(winid).visible)
696 call assert_equal(0, popup_getoptions(winid).tabpage)
697 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200698endfunc
699
700func Test_popup_valid_arguments()
701 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200702 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200703 let pos = popup_getpos(winid)
704 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200705 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200706
707 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200708 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200709 let pos = popup_getpos(winid)
710 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200711 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200712
713 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200714 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200715 let pos = popup_getpos(winid)
716 let around = (&columns - pos.width) / 2
717 call assert_inrange(around - 1, around + 1, pos.col)
718 let around = (&lines - pos.height) / 2
719 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200720 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200721endfunc
722
723func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200724 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200725 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200726 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200727 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200728
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200729 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200730 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200731 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200732 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200733 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200734 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200735 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200736 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200737
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200738 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200739 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200740 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200741 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200742 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200743 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200744 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200745 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200746
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200747 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200748 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200749 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200750 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200751 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200752 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200753 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200754 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200755 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
756 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200757 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200758 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200759
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200760 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200761 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200762 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200763 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200764 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200765 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200766 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
767 call popup_clear()
768 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200769 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200770 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200771endfunc
772
Bram Moolenaareea16992019-05-31 17:34:48 +0200773func Test_win_execute_closing_curwin()
774 split
775 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200776 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200777 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200778endfunc
779
780func Test_win_execute_not_allowed()
781 let winid = popup_create('some text', {})
782 call assert_fails('call win_execute(winid, "split")', 'E994:')
783 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
784 call assert_fails('call win_execute(winid, "close")', 'E994:')
785 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200786 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200787 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
788 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
789 call assert_fails('call win_execute(winid, "next")', 'E994:')
790 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
791 call assert_fails('call win_execute(winid, "buf")', 'E994:')
792 call assert_fails('call win_execute(winid, "edit")', 'E994:')
793 call assert_fails('call win_execute(winid, "enew")', 'E994:')
794 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
795 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
796 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
797 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200798 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200799endfunc
800
Bram Moolenaar402502d2019-05-30 22:07:36 +0200801func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200802 CheckScreendump
803
Bram Moolenaar402502d2019-05-30 22:07:36 +0200804 let lines =<< trim END
805 call setline(1, range(1, 100))
806 let winid = popup_create(
807 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200808 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200809 END
810 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200811 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200812 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
813
814 " clean up
815 call StopVimInTerminal(buf)
816 call delete('XtestPopup')
817endfunc
818
819func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200820 CheckScreendump
821
Bram Moolenaar402502d2019-05-30 22:07:36 +0200822 let lines =<< trim END
823 call setline(1, range(1, 100))
824 let winid = popup_create(
825 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200826 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200827 END
828 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200829 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200830 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
831
832 " clean up
833 call StopVimInTerminal(buf)
834 call delete('XtestPopup')
835endfunc
836
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200837func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200838 CheckScreendump
839
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200840 let lines =<< trim END
841 set showbreak=>>\
842 call setline(1, range(1, 20))
843 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +0200844 \ 'a long line here that wraps',
845 \ #{filter: 'popup_filter_yesno',
846 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200847 END
848 call writefile(lines, 'XtestPopupShowbreak')
849 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
850 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
851
852 " clean up
853 call term_sendkeys(buf, "y")
854 call StopVimInTerminal(buf)
855 call delete('XtestPopupShowbreak')
856endfunc
857
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200858func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200859 CheckFeature timers
860
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200861 topleft vnew
862 call setline(1, 'hello')
863
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200864 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200865 \ line: 1,
866 \ col: 1,
867 \ minwidth: 20,
868 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200869 \})
870 redraw
871 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
872 call assert_equal('world', line)
873
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200874 call assert_equal(winid, popup_locate(1, 1))
875 call assert_equal(winid, popup_locate(1, 20))
876 call assert_equal(0, popup_locate(1, 21))
877 call assert_equal(0, popup_locate(2, 1))
878
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200879 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200880 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +0200881 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200882 call assert_equal('hello', line)
883
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200884 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200885 \ line: &lines,
886 \ col: 10,
887 \ minwidth: 20,
888 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200889 \})
890 redraw
891 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
892 call assert_match('.*on the command line.*', line)
893
894 sleep 700m
895 redraw
896 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
897 call assert_notmatch('.*on the command line.*', line)
898
899 bwipe!
900endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200901
902func Test_popup_hide()
903 topleft vnew
904 call setline(1, 'hello')
905
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200906 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200907 \ line: 1,
908 \ col: 1,
909 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200910 \})
911 redraw
912 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
913 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200914 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200915 " buffer is still listed and active
916 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200917
918 call popup_hide(winid)
919 redraw
920 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
921 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200922 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200923 " buffer is still listed but hidden
924 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200925
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200926 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200927 redraw
928 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
929 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200930 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200931
932
933 call popup_close(winid)
934 redraw
935 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
936 call assert_equal('hello', line)
937
938 " error is given for existing non-popup window
939 call assert_fails('call popup_hide(win_getid())', 'E993:')
940
941 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200942 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200943 call popup_show(41234234)
944
945 bwipe!
946endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200947
948func Test_popup_move()
949 topleft vnew
950 call setline(1, 'hello')
951
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200952 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200953 \ line: 1,
954 \ col: 1,
955 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200956 \})
957 redraw
958 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
959 call assert_equal('world ', line)
960
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200961 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200962 redraw
963 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
964 call assert_equal('hello ', line)
965 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
966 call assert_equal('~world', line)
967
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200968 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200969 redraw
970 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
971 call assert_equal('hworld', line)
972
973 call popup_close(winid)
974
975 bwipe!
976endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200977
Bram Moolenaar402502d2019-05-30 22:07:36 +0200978func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200979 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200980 \ line: 2,
981 \ col: 3,
982 \ minwidth: 10,
983 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200984 \})
985 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200986 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200987 call assert_equal(2, res.line)
988 call assert_equal(3, res.col)
989 call assert_equal(10, res.width)
990 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200991 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200992
993 call popup_close(winid)
994endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200995
996func Test_popup_width_longest()
997 let tests = [
998 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
999 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1000 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1001 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1002 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1003 \ ]
1004
1005 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001006 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001007 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001008 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001009 call assert_equal(test[1], position.width)
1010 call popup_close(winid)
1011 endfor
1012endfunc
1013
1014func Test_popup_wraps()
1015 let tests = [
1016 \ ['nowrap', 6, 1],
1017 \ ['a line that wraps once', 12, 2],
1018 \ ['a line that wraps two times', 12, 3],
1019 \ ]
1020 for test in tests
1021 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001022 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001023 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001024 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001025 call assert_equal(test[1], position.width)
1026 call assert_equal(test[2], position.height)
1027
1028 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001029 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001030 endfor
1031endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001032
1033func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001034 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001035 \ line: 2,
1036 \ col: 3,
1037 \ minwidth: 10,
1038 \ minheight: 11,
1039 \ maxwidth: 20,
1040 \ maxheight: 21,
1041 \ zindex: 100,
1042 \ time: 5000,
1043 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001044 \})
1045 redraw
1046 let res = popup_getoptions(winid)
1047 call assert_equal(2, res.line)
1048 call assert_equal(3, res.col)
1049 call assert_equal(10, res.minwidth)
1050 call assert_equal(11, res.minheight)
1051 call assert_equal(20, res.maxwidth)
1052 call assert_equal(21, res.maxheight)
1053 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001054 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001055 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001056 if has('timers')
1057 call assert_equal(5000, res.time)
1058 endif
1059 call popup_close(winid)
1060
1061 let winid = popup_create('hello', {})
1062 redraw
1063 let res = popup_getoptions(winid)
1064 call assert_equal(0, res.line)
1065 call assert_equal(0, res.col)
1066 call assert_equal(0, res.minwidth)
1067 call assert_equal(0, res.minheight)
1068 call assert_equal(0, res.maxwidth)
1069 call assert_equal(0, res.maxheight)
1070 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001071 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001072 if has('timers')
1073 call assert_equal(0, res.time)
1074 endif
1075 call popup_close(winid)
1076 call assert_equal({}, popup_getoptions(winid))
1077endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001078
1079func Test_popup_option_values()
1080 new
1081 " window-local
1082 setlocal number
1083 setlocal nowrap
1084 " buffer-local
1085 setlocal omnifunc=Something
1086 " global/buffer-local
1087 setlocal path=/there
1088 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001089 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001090
1091 let winid = popup_create('hello', {})
1092 call assert_equal(0, getwinvar(winid, '&number'))
1093 call assert_equal(1, getwinvar(winid, '&wrap'))
1094 call assert_equal('', getwinvar(winid, '&omnifunc'))
1095 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001096 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1097
1098 " 'scrolloff' is reset to zero
1099 call assert_equal(5, &scrolloff)
1100 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001101
1102 call popup_close(winid)
1103 bwipe
1104endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001105
1106func Test_popup_atcursor()
1107 topleft vnew
1108 call setline(1, [
1109 \ 'xxxxxxxxxxxxxxxxx',
1110 \ 'xxxxxxxxxxxxxxxxx',
1111 \ 'xxxxxxxxxxxxxxxxx',
1112 \])
1113
1114 call cursor(2, 2)
1115 redraw
1116 let winid = popup_atcursor('vim', {})
1117 redraw
1118 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1119 call assert_equal('xvimxxxxxxxxxxxxx', line)
1120 call popup_close(winid)
1121
1122 call cursor(3, 4)
1123 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001124 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001125 redraw
1126 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1127 call assert_equal('xxxvimxxxxxxxxxxx', line)
1128 call popup_close(winid)
1129
1130 call cursor(1, 1)
1131 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001132 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001133 \ line: 'cursor+2',
1134 \ col: 'cursor+1',
1135 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001136 redraw
1137 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1138 call assert_equal('xvimxxxxxxxxxxxxx', line)
1139 call popup_close(winid)
1140
1141 call cursor(3, 3)
1142 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001143 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001144 \ line: 'cursor-2',
1145 \ col: 'cursor-1',
1146 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001147 redraw
1148 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1149 call assert_equal('xvimxxxxxxxxxxxxx', line)
1150 call popup_close(winid)
1151
Bram Moolenaar402502d2019-05-30 22:07:36 +02001152 " just enough room above
1153 call cursor(3, 3)
1154 redraw
1155 let winid = popup_atcursor(['vim', 'is great'], {})
1156 redraw
1157 let pos = popup_getpos(winid)
1158 call assert_equal(1, pos.line)
1159 call popup_close(winid)
1160
1161 " not enough room above, popup goes below the cursor
1162 call cursor(3, 3)
1163 redraw
1164 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1165 redraw
1166 let pos = popup_getpos(winid)
1167 call assert_equal(4, pos.line)
1168 call popup_close(winid)
1169
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001170 " cursor in first line, popup in line 2
1171 call cursor(1, 1)
1172 redraw
1173 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1174 redraw
1175 let pos = popup_getpos(winid)
1176 call assert_equal(2, pos.line)
1177 call popup_close(winid)
1178
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001179 bwipe!
1180endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001181
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001182func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001183 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001184 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001185
1186 let lines =<< trim END
1187 call setline(1, range(1, 20))
1188 call setline(5, 'here is some text to hover over')
1189 set balloonevalterm
1190 set balloonexpr=BalloonExpr()
1191 set balloondelay=100
1192 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001193 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001194 return ''
1195 endfunc
1196 func Hover()
1197 call test_setmouse(5, 15)
1198 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1199 sleep 100m
1200 endfunc
1201 func MoveOntoPopup()
1202 call test_setmouse(4, 17)
1203 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1204 endfunc
1205 func MoveAway()
1206 call test_setmouse(5, 13)
1207 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1208 endfunc
1209 END
1210 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001211 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001212 call term_wait(buf, 100)
1213 call term_sendkeys(buf, 'j')
1214 call term_sendkeys(buf, ":call Hover()\<CR>")
1215 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1216
1217 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1218 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1219
1220 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1221 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1222
1223 " clean up
1224 call StopVimInTerminal(buf)
1225 call delete('XtestPopupBeval')
1226endfunc
1227
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001228func Test_popup_filter()
1229 new
1230 call setline(1, 'some text')
1231
1232 func MyPopupFilter(winid, c)
1233 if a:c == 'e'
1234 let g:eaten = 'e'
1235 return 1
1236 endif
1237 if a:c == '0'
1238 let g:ignored = '0'
1239 return 0
1240 endif
1241 if a:c == 'x'
1242 call popup_close(a:winid)
1243 return 1
1244 endif
1245 return 0
1246 endfunc
1247
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001248 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001249 redraw
1250
1251 " e is consumed by the filter
1252 call feedkeys('e', 'xt')
1253 call assert_equal('e', g:eaten)
1254
1255 " 0 is ignored by the filter
1256 normal $
1257 call assert_equal(9, getcurpos()[2])
1258 call feedkeys('0', 'xt')
1259 call assert_equal('0', g:ignored)
1260 call assert_equal(1, getcurpos()[2])
1261
1262 " x closes the popup
1263 call feedkeys('x', 'xt')
1264 call assert_equal('e', g:eaten)
1265 call assert_equal(-1, winbufnr(winid))
1266
1267 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001268 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001269endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001270
Bram Moolenaara42d9452019-06-15 21:46:30 +02001271func ShowDialog(key, result)
1272 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001273 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001274 \ filter: 'popup_filter_yesno',
1275 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001276 \ })
1277 redraw
1278 call feedkeys(a:key, "xt")
1279 call assert_equal(winid, s:cb_winid)
1280 call assert_equal(a:result, s:cb_res)
1281endfunc
1282
1283func Test_popup_dialog()
1284 func QuitCallback(id, res)
1285 let s:cb_winid = a:id
1286 let s:cb_res = a:res
1287 endfunc
1288
1289 let winid = ShowDialog("y", 1)
1290 let winid = ShowDialog("Y", 1)
1291 let winid = ShowDialog("n", 0)
1292 let winid = ShowDialog("N", 0)
1293 let winid = ShowDialog("x", 0)
1294 let winid = ShowDialog("X", 0)
1295 let winid = ShowDialog("\<Esc>", 0)
1296 let winid = ShowDialog("\<C-C>", -1)
1297
1298 delfunc QuitCallback
1299endfunc
1300
Bram Moolenaara730e552019-06-16 19:05:31 +02001301func ShowMenu(key, result)
1302 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001303 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001304 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001305 \ })
1306 redraw
1307 call feedkeys(a:key, "xt")
1308 call assert_equal(winid, s:cb_winid)
1309 call assert_equal(a:result, s:cb_res)
1310endfunc
1311
1312func Test_popup_menu()
1313 func QuitCallback(id, res)
1314 let s:cb_winid = a:id
1315 let s:cb_res = a:res
1316 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001317 " mapping won't be used in popup
1318 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001319
1320 let winid = ShowMenu(" ", 1)
1321 let winid = ShowMenu("j \<CR>", 2)
1322 let winid = ShowMenu("JjK \<CR>", 2)
1323 let winid = ShowMenu("jjjjjj ", 3)
1324 let winid = ShowMenu("kkk ", 1)
1325 let winid = ShowMenu("x", -1)
1326 let winid = ShowMenu("X", -1)
1327 let winid = ShowMenu("\<Esc>", -1)
1328 let winid = ShowMenu("\<C-C>", -1)
1329
1330 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001331 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001332endfunc
1333
1334func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001335 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001336
1337 let lines =<< trim END
1338 call setline(1, range(1, 20))
1339 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001340 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001341 func MenuDone(id, res)
1342 echomsg "selected " .. a:res
1343 endfunc
1344 END
1345 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001346 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001347 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1348
1349 call term_sendkeys(buf, "jj")
1350 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1351
1352 call term_sendkeys(buf, " ")
1353 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1354
1355 " clean up
1356 call StopVimInTerminal(buf)
1357 call delete('XtestPopupMenu')
1358endfunc
1359
Bram Moolenaarf914a332019-07-20 15:09:56 +02001360func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001361 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001362
1363 let lines =<< trim END
1364 call setline(1, range(1, 20))
1365 hi PopupSelected ctermbg=green
1366 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1367 func MenuDone(id, res)
1368 echomsg "selected " .. a:res
1369 endfunc
1370 END
1371 call writefile(lines, 'XtestPopupNarrowMenu')
1372 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1373 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1374
1375 " clean up
1376 call term_sendkeys(buf, "x")
1377 call StopVimInTerminal(buf)
1378 call delete('XtestPopupNarrowMenu')
1379endfunc
1380
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001381func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001382 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001383
1384 " Create a popup without title or border, a line of padding will be added to
1385 " put the title on.
1386 let lines =<< trim END
1387 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001388 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001389 END
1390 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001391 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001392 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1393
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001394 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1395 call term_sendkeys(buf, ":\<CR>")
1396 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1397
1398 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1399 call term_sendkeys(buf, ":\<CR>")
1400 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1401
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001402 " clean up
1403 call StopVimInTerminal(buf)
1404 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001405
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001406 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001407 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001408 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001409 call assert_equal('Another Title', popup_getoptions(winid).title)
1410
1411 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001412endfunc
1413
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001414func Test_popup_close_callback()
1415 func PopupDone(id, result)
1416 let g:result = a:result
1417 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001418 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001419 redraw
1420 call popup_close(winid, 'done')
1421 call assert_equal('done', g:result)
1422endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001423
1424func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001425 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001426 redraw
1427 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001428 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001429 call assert_equal(5, pos.height)
1430
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001431 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001432 redraw
1433 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001434 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001435 call assert_equal(3, pos.height)
1436endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001437
1438func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001439 CheckScreendump
1440
Bram Moolenaar988c4332019-06-02 14:12:11 +02001441 " +-----------------------------+
1442 " | | |
1443 " | | |
1444 " | | |
1445 " | line1 |
1446 " |------------line2------------|
1447 " | line3 |
1448 " | line4 |
1449 " | |
1450 " | |
1451 " +-----------------------------+
1452 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001453 split
1454 vsplit
1455 let info_window1 = getwininfo()[0]
1456 let line = info_window1['height']
1457 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001458 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001459 \ line : line,
1460 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001461 \ })
1462 END
1463 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001464 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001465 call term_sendkeys(buf, "\<C-W>w")
1466 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1467
1468 " clean up
1469 call StopVimInTerminal(buf)
1470 call delete('XtestPopupBehind')
1471endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001472
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001473func s:VerifyPosition(p, msg, line, col, width, height)
1474 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1475 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1476 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1477 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001478endfunc
1479
1480func Test_popup_position_adjust()
1481 " Anything placed past 2 cells from of the right of the screen is moved to the
1482 " left.
1483 "
1484 " When wrapping is disabled, we also shift to the left to display on the
1485 " screen, unless fixed is set.
1486
1487 " Entries for cases which don't vary based on wrapping.
1488 " Format is per tests described below
1489 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001490 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1491 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1492 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1493 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1494 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1495 \
1496 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1497 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1498 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1499 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1500 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1501 \
1502 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1503 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1504 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1505 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1506 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1507 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001508
1509 " these test groups are dicts with:
1510 " - comment: something to identify the group of tests by
1511 " - options: dict of options to merge with the row/col in tests
1512 " - tests: list of cases. Each one is a list with elements:
1513 " - text
1514 " - row
1515 " - col
1516 " - expected row
1517 " - expected col
1518 " - expected width
1519 " - expected height
1520 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001521 \ #{
1522 \ comment: 'left-aligned with wrapping',
1523 \ options: #{
1524 \ wrap: 1,
1525 \ pos: 'botleft',
1526 \ },
1527 \ tests: both_wrap_tests + [
1528 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1529 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1530 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1531 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1532 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1533 \ ],
1534 \ },
1535 \ #{
1536 \ comment: 'left aligned without wrapping',
1537 \ options: #{
1538 \ wrap: 0,
1539 \ pos: 'botleft',
1540 \ },
1541 \ tests: both_wrap_tests + [
1542 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1543 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1544 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1545 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1546 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1547 \ ],
1548 \ },
1549 \ #{
1550 \ comment: 'left aligned with fixed position',
1551 \ options: #{
1552 \ wrap: 0,
1553 \ fixed: 1,
1554 \ pos: 'botleft',
1555 \ },
1556 \ tests: both_wrap_tests + [
1557 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1558 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1559 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1560 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1561 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1562 \ ],
1563 \ },
1564 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001565
1566 for test_group in tests
1567 for test in test_group.tests
1568 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001569 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001570 \ line: line,
1571 \ col: col,
1572 \ }
1573 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001574
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001575 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001576
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001577 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001578 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1579 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001580 endfor
1581 endfor
1582
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001583 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001584 %bwipe!
1585endfunc
1586
Bram Moolenaar3397f742019-06-02 18:40:06 +02001587func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001588 " width of screen
1589 let X = join(map(range(&columns), {->'X'}), '')
1590
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001591 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1592 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001593
1594 redraw
1595 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1596 call assert_equal(X, line)
1597
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001598 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001599 redraw
1600
1601 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001602 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1603 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001604
1605 redraw
1606 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1607 call assert_equal(X, line)
1608
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001609 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001610 redraw
1611
1612 " Extend so > window width
1613 let X .= 'x'
1614
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001615 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1616 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001617
1618 redraw
1619 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1620 call assert_equal(X[ : -2 ], line)
1621
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001622 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001623 redraw
1624
1625 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001626 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1627 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001628
1629 redraw
1630 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1631 call assert_equal(X[ : -2 ], line)
1632
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001633 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001634 redraw
1635
1636 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001637 let p = popup_create(X,
1638 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1639 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001640
1641 redraw
1642 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1643 let e_line = ' ' . X[ 1 : -2 ]
1644 call assert_equal(e_line, line)
1645
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001646 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001647 redraw
1648
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001649 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001650 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001651endfunc
1652
1653func Test_popup_moved()
1654 new
1655 call test_override('char_avail', 1)
1656 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1657
1658 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001659 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001660 redraw
1661 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001662 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001663 " trigger the check for last_cursormoved by going into insert mode
1664 call feedkeys("li\<Esc>", 'xt')
1665 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001666 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001667
1668 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001669 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001670 redraw
1671 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001672 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001673 call feedkeys("hi\<Esc>", 'xt')
1674 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001675 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001676
1677 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001678 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001679 redraw
1680 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001681 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001682 call feedkeys("li\<Esc>", 'xt')
1683 call assert_equal(1, popup_getpos(winid).visible)
1684 call feedkeys("ei\<Esc>", 'xt')
1685 call assert_equal(1, popup_getpos(winid).visible)
1686 call feedkeys("eli\<Esc>", 'xt')
1687 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001688 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001689
Bram Moolenaar17627312019-06-02 19:53:44 +02001690 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001691 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001692 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001693 redraw
1694 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001695 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001696 call feedkeys("eli\<Esc>", 'xt')
1697 call assert_equal(1, popup_getpos(winid).visible)
1698 call feedkeys("wi\<Esc>", 'xt')
1699 call assert_equal(1, popup_getpos(winid).visible)
1700 call feedkeys("Eli\<Esc>", 'xt')
1701 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001702 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001703
1704 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001705 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001706 redraw
1707 call assert_equal(1, popup_getpos(winid).visible)
1708 call feedkeys("eli\<Esc>", 'xt')
1709 call feedkeys("ei\<Esc>", 'xt')
1710 call assert_equal(1, popup_getpos(winid).visible)
1711 call feedkeys("eli\<Esc>", 'xt')
1712 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001713 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001714
1715 bwipe!
1716 call test_override('ALL', 0)
1717endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001718
1719func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001720 CheckFeature timers
1721 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001722
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001723 let lines =<< trim END
1724 call setline(1, range(1, 20))
1725 hi Notification ctermbg=lightblue
1726 call popup_notification('first notification', {})
1727 END
1728 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001729 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001730 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1731
1732 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001733 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1734 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001735 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1736
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001737 " clean up
1738 call StopVimInTerminal(buf)
1739 call delete('XtestNotifications')
1740endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001741
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001742func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001743 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001744
1745 let lines =<< trim END
1746 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001747 hi ScrollThumb ctermbg=blue
1748 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001749 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001750 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001751 \ minwidth: 8,
1752 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001753 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001754 func ScrollUp()
1755 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1756 endfunc
1757 func ScrollDown()
1758 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1759 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001760 func ClickTop()
1761 call feedkeys("\<F4>\<LeftMouse>", "xt")
1762 endfunc
1763 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001764 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001765 call feedkeys("\<F5>\<LeftMouse>", "xt")
1766 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001767 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001768 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1769 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001770 END
1771 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001772 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001773 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1774
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001775 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001776 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001777 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1778
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001779 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001780 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001781 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1782
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001783 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001784 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001785 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1786
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02001787 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001788 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001789 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1790 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1791
1792 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1793 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1794
1795 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001796 " wait a bit, otherwise it fails sometimes (double click recognized?)
1797 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001798 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1799 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1800
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001801 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1802 sleep 100m
1803 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1804 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1805
1806 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1807 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1808
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001809 " remove the minwidth and maxheight
1810 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02001811 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001812 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
1813
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001814 " clean up
1815 call StopVimInTerminal(buf)
1816 call delete('XtestPopupScroll')
1817endfunc
1818
Bram Moolenaar437a7462019-07-05 20:17:22 +02001819func Test_popup_fitting_scrollbar()
1820 " this was causing a crash, divide by zero
1821 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001822 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001823 \ scrollbar: 1,
1824 \ maxwidth: 10,
1825 \ maxheight: 5,
1826 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001827 redraw
1828 call popup_clear()
1829endfunc
1830
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001831func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001832 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001833
1834 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001835 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001836 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001837 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001838 END
1839
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001840 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001841 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001842 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1843
1844 " Setting to empty string clears it
1845 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1846 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1847
1848 " Setting a list
1849 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1850 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1851
1852 " Shrinking with a list
1853 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1854 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1855
1856 " Growing with a list
1857 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1858 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1859
1860 " Empty list clears
1861 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1862 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1863
1864 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001865 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001866 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1867
1868 " clean up
1869 call StopVimInTerminal(buf)
1870 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001871endfunc
1872
1873func Test_popup_hidden()
1874 new
1875
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001876 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001877 redraw
1878 call assert_equal(0, popup_getpos(winid).visible)
1879 call popup_close(winid)
1880
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001881 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001882 redraw
1883 call assert_equal(0, popup_getpos(winid).visible)
1884 call popup_close(winid)
1885
1886 func QuitCallback(id, res)
1887 let s:cb_winid = a:id
1888 let s:cb_res = a:res
1889 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001890 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001891 \ filter: 'popup_filter_yesno',
1892 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001893 \ })
1894 redraw
1895 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001896 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1897 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001898 exe "normal anot used by filter\<Esc>"
1899 call assert_equal('not used by filter', getline(1))
1900
1901 call popup_show(winid)
1902 call feedkeys('y', "xt")
1903 call assert_equal(1, s:cb_res)
1904
1905 bwipe!
1906 delfunc QuitCallback
1907endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001908
1909" Test options not checked elsewhere
1910func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001911 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001912 let options = popup_getoptions(winid)
1913 call assert_equal(1, options.wrap)
1914 call assert_equal(0, options.drag)
1915 call assert_equal('Beautiful', options.highlight)
1916
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001917 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001918 let options = popup_getoptions(winid)
1919 call assert_equal(0, options.wrap)
1920 call assert_equal(1, options.drag)
1921 call assert_equal('Another', options.highlight)
1922
1923 call popup_close(winid)
1924endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001925
1926func Test_popupwin_garbage_collect()
1927 func MyPopupFilter(x, winid, c)
1928 " NOP
1929 endfunc
1930
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001931 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001932 call test_garbagecollect_now()
1933 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001934 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001935 call feedkeys('j', 'xt')
1936 call assert_true(v:true)
1937
1938 call popup_close(winid)
1939 delfunc MyPopupFilter
1940endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001941
Bram Moolenaar581ba392019-09-03 22:08:33 +02001942func Test_popupwin_filter_mode()
1943 func MyPopupFilter(winid, c)
1944 let s:typed = a:c
1945 if a:c == ':' || a:c == "\r" || a:c == 'v'
1946 " can start cmdline mode, get out, and start/stop Visual mode
1947 return 0
1948 endif
1949 return 1
1950 endfunc
1951
1952 " Normal, Visual and Insert mode
1953 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
1954 redraw
1955 call feedkeys('x', 'xt')
1956 call assert_equal('x', s:typed)
1957
1958 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
1959 call assert_equal(':', s:typed)
1960 call assert_equal('foo', g:foo)
1961
1962 let @x = 'something'
1963 call feedkeys('v$"xy', 'xt')
1964 call assert_equal('y', s:typed)
1965 call assert_equal('something', @x) " yank command is filtered out
1966 call feedkeys('v', 'xt') " end Visual mode
1967
1968 call popup_close(winid)
1969
1970 " only Normal mode
1971 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
1972 redraw
1973 call feedkeys('x', 'xt')
1974 call assert_equal('x', s:typed)
1975
1976 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
1977 call assert_equal(':', s:typed)
1978 call assert_equal('foo', g:foo)
1979
1980 let @x = 'something'
1981 call feedkeys('v$"xy', 'xt')
1982 call assert_equal('v', s:typed)
1983 call assert_notequal('something', @x)
1984
1985 call popup_close(winid)
1986
1987 " default: all modes
1988 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
1989 redraw
1990 call feedkeys('x', 'xt')
1991 call assert_equal('x', s:typed)
1992
1993 let g:foo = 'bar'
1994 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
1995 call assert_equal("\r", s:typed)
1996 call assert_equal('bar', g:foo)
1997
1998 let @x = 'something'
1999 call feedkeys('v$"xy', 'xt')
2000 call assert_equal('y', s:typed)
2001 call assert_equal('something', @x) " yank command is filtered out
2002 call feedkeys('v', 'xt') " end Visual mode
2003
2004 call popup_close(winid)
2005 delfunc MyPopupFilter
2006endfunc
2007
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002008func Test_popupwin_with_buffer()
2009 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2010 let buf = bufadd('XsomeFile')
2011 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002012
2013 setlocal number
2014 call setbufvar(buf, "&wrapmargin", 13)
2015
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002016 let winid = popup_create(buf, {})
2017 call assert_notequal(0, winid)
2018 let pos = popup_getpos(winid)
2019 call assert_equal(2, pos.height)
2020 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002021
2022 " window-local option is set to default, buffer-local is not
2023 call assert_equal(0, getwinvar(winid, '&number'))
2024 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2025
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002026 call popup_close(winid)
2027 call assert_equal({}, popup_getpos(winid))
2028 call assert_equal(1, bufloaded(buf))
2029 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002030 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002031
2032 edit test_popupwin.vim
2033 let winid = popup_create(bufnr(''), {})
2034 redraw
2035 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002036 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002037endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002038
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002039func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002040 CheckFeature terminal
2041
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002042 let ptybuf = term_start(&shell, #{hidden: 1})
2043 call assert_fails('let winnr = popup_create(ptybuf, #{})', 'E278:')
2044 exe 'bwipe! ' .. ptybuf
2045endfunc
2046
Bram Moolenaar934470e2019-09-01 23:27:05 +02002047func Test_popupwin_with_buffer_and_filter()
2048 new Xwithfilter
2049 call setline(1, range(100))
2050 let bufnr = bufnr()
2051 hide
2052
2053 func BufferFilter(win, key)
2054 if a:key == 'G'
2055 " recursive use of "G" does not cause problems.
2056 call win_execute(a:win, 'normal! G')
2057 return 1
2058 endif
2059 return 0
2060 endfunc
2061
2062 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2063 call assert_equal(1, popup_getpos(winid).firstline)
2064 redraw
2065 call feedkeys("G", 'xt')
2066 call assert_equal(99, popup_getpos(winid).firstline)
2067
2068 call popup_close(winid)
2069 exe 'bwipe! ' .. bufnr
2070endfunc
2071
Bram Moolenaare296e312019-07-03 23:20:18 +02002072func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002073 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002074 \ maxwidth: 40,
2075 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002076 \ })
2077 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002078 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002079 redraw
2080 call assert_equal(19, popup_getpos(winid).width)
2081 endfor
2082 call popup_clear()
2083endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002084
2085func Test_popupwin_buf_close()
2086 let buf = bufadd('Xtestbuf')
2087 call bufload(buf)
2088 call setbufline(buf, 1, ['just', 'some', 'lines'])
2089 let winid = popup_create(buf, {})
2090 redraw
2091 call assert_equal(3, popup_getpos(winid).height)
2092 let bufinfo = getbufinfo(buf)[0]
2093 call assert_equal(1, bufinfo.changed)
2094 call assert_equal(0, bufinfo.hidden)
2095 call assert_equal(0, bufinfo.listed)
2096 call assert_equal(1, bufinfo.loaded)
2097 call assert_equal([], bufinfo.windows)
2098 call assert_equal([winid], bufinfo.popups)
2099
2100 call popup_close(winid)
2101 call assert_equal({}, popup_getpos(winid))
2102 let bufinfo = getbufinfo(buf)[0]
2103 call assert_equal(1, bufinfo.changed)
2104 call assert_equal(1, bufinfo.hidden)
2105 call assert_equal(0, bufinfo.listed)
2106 call assert_equal(1, bufinfo.loaded)
2107 call assert_equal([], bufinfo.windows)
2108 call assert_equal([], bufinfo.popups)
2109 exe 'bwipe! ' .. buf
2110endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002111
2112func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002113 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002114
2115 let lines =<< trim END
2116 call setline(1, range(1, 10))
2117 hi ScrollThumb ctermbg=blue
2118 hi ScrollBar ctermbg=red
2119 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002120 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002121 \ maxwidth: 10,
2122 \ maxheight: 3,
2123 \ pos : 'topleft',
2124 \ col : a:col,
2125 \ line : a:line,
2126 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002127 \ })
2128 endfunc
2129 call PopupMenu(['x'], 1, 1)
2130 call PopupMenu(['123456789|'], 1, 16)
2131 call PopupMenu(['123456789|' .. ' '], 7, 1)
2132 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2133 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2134 END
2135 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002136 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002137 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2138
2139 " close the menu popupwin.
2140 call term_sendkeys(buf, " ")
2141 call term_sendkeys(buf, " ")
2142 call term_sendkeys(buf, " ")
2143 call term_sendkeys(buf, " ")
2144 call term_sendkeys(buf, " ")
2145
2146 " clean up
2147 call StopVimInTerminal(buf)
2148 call delete('XtestPopupMenuMaxWidth')
2149endfunc
2150
Bram Moolenaara901a372019-07-13 16:38:50 +02002151func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002152 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002153
2154 let lines =<< trim END
2155 call setline(1, range(1, 20))
2156 hi ScrollThumb ctermbg=blue
2157 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002158 eval ['one', 'two', 'three', 'four', 'five',
2159 \ 'six', 'seven', 'eight', 'nine']
2160 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002161 \ minwidth: 8,
2162 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002163 \ })
2164 END
2165 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002166 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002167
2168 call term_sendkeys(buf, "j")
2169 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2170
2171 call term_sendkeys(buf, "jjj")
2172 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2173
2174 " if the cursor is the bottom line, it stays at the bottom line.
2175 call term_sendkeys(buf, repeat("j", 20))
2176 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2177
2178 call term_sendkeys(buf, "kk")
2179 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2180
2181 call term_sendkeys(buf, "k")
2182 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2183
2184 " if the cursor is in the top line, it stays in the top line.
2185 call term_sendkeys(buf, repeat("k", 20))
2186 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2187
2188 " close the menu popupwin.
2189 call term_sendkeys(buf, " ")
2190
2191 " clean up
2192 call StopVimInTerminal(buf)
2193 call delete('XtestPopupMenuScroll')
2194endfunc
2195
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002196func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002197 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002198
2199 let lines =<< trim END
2200 function! MyFilter(winid, key) abort
2201 if a:key == "0"
2202 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2203 return 1
2204 endif
2205 if a:key == "G"
2206 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2207 return 1
2208 endif
2209 if a:key == "j"
2210 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2211 return 1
2212 endif
2213 if a:key == "k"
2214 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2215 return 1
2216 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002217 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002218 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002219 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002220 endif
2221 return 0
2222 endfunction
2223 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2224 \ maxheight : 3,
2225 \ filter : 'MyFilter'
2226 \ })
2227 END
2228 call writefile(lines, 'XtestPopupMenuFilter')
2229 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2230
2231 call term_sendkeys(buf, "j")
2232 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2233
2234 call term_sendkeys(buf, "k")
2235 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2236
2237 call term_sendkeys(buf, "G")
2238 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2239
2240 call term_sendkeys(buf, "0")
2241 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2242
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002243 " check that when the popup is closed in the filter the screen is redrawn
2244 call term_sendkeys(buf, ":")
2245 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2246 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002247
2248 " clean up
2249 call StopVimInTerminal(buf)
2250 call delete('XtestPopupMenuFilter')
2251endfunc
2252
2253func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002254 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002255
2256 let winid = popup_create('some text', {})
2257 call assert_equal(0, popup_getoptions(winid).cursorline)
2258 call popup_close(winid)
2259
2260 let winid = popup_create('some text', #{ cursorline: 1, })
2261 call assert_equal(1, popup_getoptions(winid).cursorline)
2262 call popup_close(winid)
2263
2264 let winid = popup_create('some text', #{ cursorline: 0, })
2265 call assert_equal(0, popup_getoptions(winid).cursorline)
2266 call popup_close(winid)
2267
2268 let winid = popup_menu('some text', {})
2269 call assert_equal(1, popup_getoptions(winid).cursorline)
2270 call popup_close(winid)
2271
2272 let winid = popup_menu('some text', #{ cursorline: 1, })
2273 call assert_equal(1, popup_getoptions(winid).cursorline)
2274 call popup_close(winid)
2275
2276 let winid = popup_menu('some text', #{ cursorline: 0, })
2277 call assert_equal(0, popup_getoptions(winid).cursorline)
2278 call popup_close(winid)
2279
2280 " ---------
2281 " Pattern 1
2282 " ---------
2283 let lines =<< trim END
2284 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2285 END
2286 call writefile(lines, 'XtestPopupCursorLine')
2287 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2288 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2289 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2290 call StopVimInTerminal(buf)
2291
2292 " ---------
2293 " Pattern 2
2294 " ---------
2295 let lines =<< trim END
2296 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2297 END
2298 call writefile(lines, 'XtestPopupCursorLine')
2299 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2300 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2301 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2302 call StopVimInTerminal(buf)
2303
2304 " ---------
2305 " Pattern 3
2306 " ---------
2307 let lines =<< trim END
2308 function! MyFilter(winid, key) abort
2309 if a:key == "j"
2310 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2311 return 1
2312 endif
2313 if a:key == 'x'
2314 call popup_close(a:winid)
2315 return 1
2316 endif
2317 return 0
2318 endfunction
2319 call popup_menu(['111', '222', '333'], #{
2320 \ cursorline : 0,
2321 \ maxheight : 2,
2322 \ filter : 'MyFilter',
2323 \ })
2324 END
2325 call writefile(lines, 'XtestPopupCursorLine')
2326 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2327 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2328 call term_sendkeys(buf, "j")
2329 call term_sendkeys(buf, "j")
2330 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2331 call term_sendkeys(buf, "x")
2332 call StopVimInTerminal(buf)
2333
2334 " ---------
2335 " Pattern 4
2336 " ---------
2337 let lines =<< trim END
2338 function! MyFilter(winid, key) abort
2339 if a:key == "j"
2340 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2341 return 1
2342 endif
2343 if a:key == 'x'
2344 call popup_close(a:winid)
2345 return 1
2346 endif
2347 return 0
2348 endfunction
2349 call popup_menu(['111', '222', '333'], #{
2350 \ cursorline : 1,
2351 \ maxheight : 2,
2352 \ filter : 'MyFilter',
2353 \ })
2354 END
2355 call writefile(lines, 'XtestPopupCursorLine')
2356 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2357 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2358 call term_sendkeys(buf, "j")
2359 call term_sendkeys(buf, "j")
2360 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2361 call term_sendkeys(buf, "x")
2362 call StopVimInTerminal(buf)
2363
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002364 " ---------
2365 " Cursor in second line when creating the popup
2366 " ---------
2367 let lines =<< trim END
2368 let winid = popup_create(['111', '222', '333'], #{
2369 \ cursorline : 1,
2370 \ })
2371 call win_execute(winid, "2")
2372 END
2373 call writefile(lines, 'XtestPopupCursorLine')
2374 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2375 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2376 call StopVimInTerminal(buf)
2377
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002378 call delete('XtestPopupCursorLine')
2379endfunc
2380
Bram Moolenaarf914a332019-07-20 15:09:56 +02002381func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002382 CheckScreendump
2383
Bram Moolenaarf914a332019-07-20 15:09:56 +02002384 call writefile([
2385 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2386 \ "another\tXtagfile\t/^this is another",
2387 \ "theword\tXtagfile\t/^theword"],
2388 \ 'Xtags')
2389 call writefile(range(1,20)
2390 \ + ['theword is here']
2391 \ + range(22, 27)
2392 \ + ['this is another place']
2393 \ + range(29, 40),
2394 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002395 call writefile(range(1,10)
2396 \ + ['searched word is here']
2397 \ + range(12, 20),
2398 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002399 let lines =<< trim END
2400 set tags=Xtags
2401 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002402 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002403 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002404 \ 'three',
2405 \ 'four',
2406 \ 'five',
2407 \ 'six',
2408 \ 'seven',
2409 \ 'find theword somewhere',
2410 \ 'nine',
2411 \ 'this is another word',
2412 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002413 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002414 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002415 END
2416 call writefile(lines, 'XtestPreviewPopup')
2417 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2418
2419 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2420 call term_sendkeys(buf, ":\<CR>")
2421 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2422
2423 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2424 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2425
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002426 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002427 call term_sendkeys(buf, ":\<CR>")
2428 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2429
2430 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2431 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2432
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002433 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2434 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002435 call term_sendkeys(buf, ":cd testdir\<CR>")
2436
2437 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002438 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002439 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2440
2441 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2442 call term_sendkeys(buf, ":\<CR>")
2443 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2444
2445 call term_sendkeys(buf, ":pclose\<CR>")
2446 call term_sendkeys(buf, ":psearch searched\<CR>")
2447 call term_sendkeys(buf, ":\<CR>")
2448 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002449
Bram Moolenaarf914a332019-07-20 15:09:56 +02002450 call StopVimInTerminal(buf)
2451 call delete('Xtags')
2452 call delete('Xtagfile')
2453 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002454 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002455endfunc
2456
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002457func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002458 let lines =<< trim END
2459 set completeopt+=preview,popup
2460 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002461 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002462
2463 func CompleteFuncDict(findstart, base)
2464 if a:findstart
2465 if col('.') > 10
2466 return col('.') - 10
2467 endif
2468 return 0
2469 endif
2470
2471 return {
2472 \ 'words': [
2473 \ {
2474 \ 'word': 'aword',
2475 \ 'abbr': 'wrd',
2476 \ 'menu': 'extra text',
2477 \ 'info': 'words are cool',
2478 \ 'kind': 'W',
2479 \ 'user_data': 'test'
2480 \ },
2481 \ {
2482 \ 'word': 'anotherword',
2483 \ 'abbr': 'anotwrd',
2484 \ 'menu': 'extra text',
2485 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2486 \ 'kind': 'W',
2487 \ 'user_data': 'notest'
2488 \ },
2489 \ {
2490 \ 'word': 'noinfo',
2491 \ 'abbr': 'noawrd',
2492 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002493 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002494 \ 'kind': 'W',
2495 \ 'user_data': 'notest'
2496 \ },
2497 \ {
2498 \ 'word': 'thatword',
2499 \ 'abbr': 'thatwrd',
2500 \ 'menu': 'extra text',
2501 \ 'info': 'that word is cool',
2502 \ 'kind': 'W',
2503 \ 'user_data': 'notest'
2504 \ },
2505 \ ]
2506 \ }
2507 endfunc
2508 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002509 func ChangeColor()
2510 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002511 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002512 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002513
2514 func InfoHidden()
2515 set completepopup=height:4,border:off,align:menu
2516 set completeopt-=popup completeopt+=popuphidden
2517 au CompleteChanged * call HandleChange()
2518 endfunc
2519
2520 let s:counter = 0
2521 func HandleChange()
2522 let s:counter += 1
2523 let selected = complete_info(['selected']).selected
2524 if selected <= 0
2525 " First time: do nothing, info remains hidden
2526 return
2527 endif
2528 if selected == 1
2529 " Second time: show info right away
2530 let id = popup_findinfo()
2531 if id
2532 call popup_settext(id, 'immediate info ' .. s:counter)
2533 call popup_show(id)
2534 endif
2535 else
2536 " Third time: show info after a short delay
2537 call timer_start(100, 'ShowInfo')
2538 endif
2539 endfunc
2540
2541 func ShowInfo(...)
2542 let id = popup_findinfo()
2543 if id
2544 call popup_settext(id, 'async info ' .. s:counter)
2545 call popup_show(id)
2546 endif
2547 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002548 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002549 return lines
2550endfunc
2551
2552func Test_popupmenu_info_border()
2553 CheckScreendump
2554
2555 let lines = Get_popupmenu_lines()
2556 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002557 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002558
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002559 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2560 call term_wait(buf, 50)
2561
2562 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2563 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2564
2565 call term_sendkeys(buf, "\<C-N>")
2566 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2567
2568 call term_sendkeys(buf, "\<C-N>")
2569 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2570
2571 call term_sendkeys(buf, "\<C-N>\<C-N>")
2572 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2573
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002574 " info on the left with scrollbar
2575 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2576 call term_sendkeys(buf, "\<C-N>\<C-N>")
2577 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2578
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002579 call StopVimInTerminal(buf)
2580 call delete('XtestInfoPopup')
2581endfunc
2582
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002583func Test_popupmenu_info_noborder()
2584 CheckScreendump
2585
2586 let lines = Get_popupmenu_lines()
2587 call add(lines, 'set completepopup=height:4,border:off')
2588 call writefile(lines, 'XtestInfoPopupNb')
2589
2590 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2591 call term_wait(buf, 50)
2592
2593 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2594 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2595
2596 call StopVimInTerminal(buf)
2597 call delete('XtestInfoPopupNb')
2598endfunc
2599
Bram Moolenaar258cef52019-08-21 17:29:29 +02002600func Test_popupmenu_info_align_menu()
2601 CheckScreendump
2602
2603 let lines = Get_popupmenu_lines()
2604 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2605 call writefile(lines, 'XtestInfoPopupNb')
2606
2607 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2608 call term_wait(buf, 50)
2609
2610 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2611 call term_sendkeys(buf, "\<C-N>")
2612 call term_sendkeys(buf, "\<C-N>")
2613 call term_sendkeys(buf, "\<C-N>")
2614 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2615
2616 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2617 call term_sendkeys(buf, "\<C-N>")
2618 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2619
2620 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002621 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002622 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2623 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2624 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2625
2626 call StopVimInTerminal(buf)
2627 call delete('XtestInfoPopupNb')
2628endfunc
2629
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002630func Test_popupmenu_info_hidden()
2631 CheckScreendump
2632
2633 let lines = Get_popupmenu_lines()
2634 call add(lines, 'call InfoHidden()')
2635 call writefile(lines, 'XtestInfoPopupHidden')
2636
2637 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
2638 call term_wait(buf, 50)
2639
2640 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2641 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
2642
2643 call term_sendkeys(buf, "\<C-N>")
2644 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
2645
2646 call term_sendkeys(buf, "\<C-N>")
2647 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
2648
2649 call term_sendkeys(buf, "\<Esc>")
2650 call StopVimInTerminal(buf)
2651 call delete('XtestInfoPopupHidden')
2652endfunc
2653
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002654func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02002655 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002656 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002657 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002658 call assert_equal(bufnr, winbufnr(winid))
2659 call popup_clear()
2660endfunc
2661
Bram Moolenaar1824f452019-10-02 23:06:46 +02002662func Test_popupwin_getoptions_tablocal()
2663 topleft split
2664 let win1 = popup_create('nothing', #{maxheight: 8})
2665 let win2 = popup_create('something', #{maxheight: 10})
2666 let win3 = popup_create('something', #{maxheight: 15})
2667 call assert_equal(8, popup_getoptions(win1).maxheight)
2668 call assert_equal(10, popup_getoptions(win2).maxheight)
2669 call assert_equal(15, popup_getoptions(win3).maxheight)
2670 call popup_clear()
2671 quit
2672endfunc
2673
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002674func Test_popupwin_cancel()
2675 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
2676 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
2677 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
2678 call assert_equal(5, popup_getpos(win1).line)
2679 call assert_equal(10, popup_getpos(win2).line)
2680 call assert_equal(15, popup_getpos(win3).line)
2681 " TODO: this also works without patch 8.1.2110
2682 call feedkeys("\<C-C>", 'xt')
2683 call assert_equal(5, popup_getpos(win1).line)
2684 call assert_equal(10, popup_getpos(win2).line)
2685 call assert_equal({}, popup_getpos(win3))
2686 call feedkeys("\<C-C>", 'xt')
2687 call assert_equal(5, popup_getpos(win1).line)
2688 call assert_equal({}, popup_getpos(win2))
2689 call assert_equal({}, popup_getpos(win3))
2690 call feedkeys("\<C-C>", 'xt')
2691 call assert_equal({}, popup_getpos(win1))
2692 call assert_equal({}, popup_getpos(win2))
2693 call assert_equal({}, popup_getpos(win3))
2694endfunc
2695
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002696" vim: shiftwidth=2 sts=2