blob: 2dcd14d376308104a9d8ad30a2cc1f8c0d8c2094 [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 Moolenaarf2885d32019-11-02 20:21:25 +01001767 func Popup_filter(winid, key)
1768 if a:key == 'j'
1769 let line = popup_getoptions(a:winid).firstline
1770 let nlines = line('$', a:winid)
1771 let newline = line < nlines ? (line + 1) : nlines
1772 call popup_setoptions(a:winid, #{firstline: newline})
1773 return v:true
1774 elseif a:key == 'x'
1775 call popup_close(a:winid)
1776 return v:true
1777 endif
1778 endfunc
1779
1780 func PopupScroll()
1781 call popup_clear()
1782 let text =<< trim END
1783 1
1784 2
1785 3
1786 4
1787 long line long line long line long line long line long line
1788 long line long line long line long line long line long line
1789 long line long line long line long line long line long line
1790 END
1791 call popup_create(text, #{
1792 \ minwidth: 30,
1793 \ maxwidth: 30,
1794 \ minheight: 4,
1795 \ maxheight: 4,
1796 \ firstline: 1,
1797 \ wrap: v:true,
1798 \ scrollbar: v:true,
1799 \ mapping: v:false,
1800 \ filter: funcref('Popup_filter')
1801 \ })
1802 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001803 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001804 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1805 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001806 END
1807 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001808 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001809 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1810
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001811 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001812 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001813 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1814
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001815 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001816 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001817 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1818
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001819 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001820 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001821 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1822
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02001823 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001824 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001825 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1826 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1827
1828 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1829 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1830
1831 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001832 " wait a bit, otherwise it fails sometimes (double click recognized?)
1833 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001834 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1835 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1836
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001837 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1838 sleep 100m
1839 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1840 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1841
1842 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1843 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1844
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001845 " remove the minwidth and maxheight
1846 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02001847 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001848 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
1849
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001850 " check size with non-wrapping lines
1851 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
1852 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
1853
1854 " check size with wrapping lines
1855 call term_sendkeys(buf, "j")
1856 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
1857 call term_sendkeys(buf, "x")
1858
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001859 " clean up
1860 call StopVimInTerminal(buf)
1861 call delete('XtestPopupScroll')
1862endfunc
1863
Bram Moolenaar437a7462019-07-05 20:17:22 +02001864func Test_popup_fitting_scrollbar()
1865 " this was causing a crash, divide by zero
1866 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001867 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001868 \ scrollbar: 1,
1869 \ maxwidth: 10,
1870 \ maxheight: 5,
1871 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001872 redraw
1873 call popup_clear()
1874endfunc
1875
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001876func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001877 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001878
1879 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001880 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001881 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001882 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001883 END
1884
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001885 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001886 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001887 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1888
1889 " Setting to empty string clears it
1890 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1891 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1892
1893 " Setting a list
1894 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1895 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1896
1897 " Shrinking with a list
1898 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1899 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1900
1901 " Growing with a list
1902 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1903 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1904
1905 " Empty list clears
1906 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1907 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1908
1909 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001910 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001911 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1912
1913 " clean up
1914 call StopVimInTerminal(buf)
1915 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001916endfunc
1917
1918func Test_popup_hidden()
1919 new
1920
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001921 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001922 redraw
1923 call assert_equal(0, popup_getpos(winid).visible)
1924 call popup_close(winid)
1925
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001926 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001927 redraw
1928 call assert_equal(0, popup_getpos(winid).visible)
1929 call popup_close(winid)
1930
1931 func QuitCallback(id, res)
1932 let s:cb_winid = a:id
1933 let s:cb_res = a:res
1934 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001935 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001936 \ filter: 'popup_filter_yesno',
1937 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001938 \ })
1939 redraw
1940 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001941 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1942 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001943 exe "normal anot used by filter\<Esc>"
1944 call assert_equal('not used by filter', getline(1))
1945
1946 call popup_show(winid)
1947 call feedkeys('y', "xt")
1948 call assert_equal(1, s:cb_res)
1949
1950 bwipe!
1951 delfunc QuitCallback
1952endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001953
1954" Test options not checked elsewhere
1955func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001956 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001957 let options = popup_getoptions(winid)
1958 call assert_equal(1, options.wrap)
1959 call assert_equal(0, options.drag)
1960 call assert_equal('Beautiful', options.highlight)
1961
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001962 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001963 let options = popup_getoptions(winid)
1964 call assert_equal(0, options.wrap)
1965 call assert_equal(1, options.drag)
1966 call assert_equal('Another', options.highlight)
1967
1968 call popup_close(winid)
1969endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001970
1971func Test_popupwin_garbage_collect()
1972 func MyPopupFilter(x, winid, c)
1973 " NOP
1974 endfunc
1975
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001976 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001977 call test_garbagecollect_now()
1978 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001979 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001980 call feedkeys('j', 'xt')
1981 call assert_true(v:true)
1982
1983 call popup_close(winid)
1984 delfunc MyPopupFilter
1985endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001986
Bram Moolenaar581ba392019-09-03 22:08:33 +02001987func Test_popupwin_filter_mode()
1988 func MyPopupFilter(winid, c)
1989 let s:typed = a:c
1990 if a:c == ':' || a:c == "\r" || a:c == 'v'
1991 " can start cmdline mode, get out, and start/stop Visual mode
1992 return 0
1993 endif
1994 return 1
1995 endfunc
1996
1997 " Normal, Visual and Insert mode
1998 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
1999 redraw
2000 call feedkeys('x', 'xt')
2001 call assert_equal('x', s:typed)
2002
2003 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2004 call assert_equal(':', s:typed)
2005 call assert_equal('foo', g:foo)
2006
2007 let @x = 'something'
2008 call feedkeys('v$"xy', 'xt')
2009 call assert_equal('y', s:typed)
2010 call assert_equal('something', @x) " yank command is filtered out
2011 call feedkeys('v', 'xt') " end Visual mode
2012
2013 call popup_close(winid)
2014
2015 " only Normal mode
2016 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2017 redraw
2018 call feedkeys('x', 'xt')
2019 call assert_equal('x', s:typed)
2020
2021 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2022 call assert_equal(':', s:typed)
2023 call assert_equal('foo', g:foo)
2024
2025 let @x = 'something'
2026 call feedkeys('v$"xy', 'xt')
2027 call assert_equal('v', s:typed)
2028 call assert_notequal('something', @x)
2029
2030 call popup_close(winid)
2031
2032 " default: all modes
2033 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2034 redraw
2035 call feedkeys('x', 'xt')
2036 call assert_equal('x', s:typed)
2037
2038 let g:foo = 'bar'
2039 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2040 call assert_equal("\r", s:typed)
2041 call assert_equal('bar', g:foo)
2042
2043 let @x = 'something'
2044 call feedkeys('v$"xy', 'xt')
2045 call assert_equal('y', s:typed)
2046 call assert_equal('something', @x) " yank command is filtered out
2047 call feedkeys('v', 'xt') " end Visual mode
2048
2049 call popup_close(winid)
2050 delfunc MyPopupFilter
2051endfunc
2052
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002053func Test_popupwin_with_buffer()
2054 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2055 let buf = bufadd('XsomeFile')
2056 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002057
2058 setlocal number
2059 call setbufvar(buf, "&wrapmargin", 13)
2060
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002061 let winid = popup_create(buf, {})
2062 call assert_notequal(0, winid)
2063 let pos = popup_getpos(winid)
2064 call assert_equal(2, pos.height)
2065 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002066
2067 " window-local option is set to default, buffer-local is not
2068 call assert_equal(0, getwinvar(winid, '&number'))
2069 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2070
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002071 call popup_close(winid)
2072 call assert_equal({}, popup_getpos(winid))
2073 call assert_equal(1, bufloaded(buf))
2074 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002075 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002076
2077 edit test_popupwin.vim
2078 let winid = popup_create(bufnr(''), {})
2079 redraw
2080 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002081 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002082endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002083
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002084func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002085 CheckFeature terminal
2086
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002087 let ptybuf = term_start(&shell, #{hidden: 1})
2088 call assert_fails('let winnr = popup_create(ptybuf, #{})', 'E278:')
2089 exe 'bwipe! ' .. ptybuf
2090endfunc
2091
Bram Moolenaar934470e2019-09-01 23:27:05 +02002092func Test_popupwin_with_buffer_and_filter()
2093 new Xwithfilter
2094 call setline(1, range(100))
2095 let bufnr = bufnr()
2096 hide
2097
2098 func BufferFilter(win, key)
2099 if a:key == 'G'
2100 " recursive use of "G" does not cause problems.
2101 call win_execute(a:win, 'normal! G')
2102 return 1
2103 endif
2104 return 0
2105 endfunc
2106
2107 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2108 call assert_equal(1, popup_getpos(winid).firstline)
2109 redraw
2110 call feedkeys("G", 'xt')
2111 call assert_equal(99, popup_getpos(winid).firstline)
2112
2113 call popup_close(winid)
2114 exe 'bwipe! ' .. bufnr
2115endfunc
2116
Bram Moolenaare296e312019-07-03 23:20:18 +02002117func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002118 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002119 \ maxwidth: 40,
2120 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002121 \ })
2122 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002123 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002124 redraw
2125 call assert_equal(19, popup_getpos(winid).width)
2126 endfor
2127 call popup_clear()
2128endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002129
2130func Test_popupwin_buf_close()
2131 let buf = bufadd('Xtestbuf')
2132 call bufload(buf)
2133 call setbufline(buf, 1, ['just', 'some', 'lines'])
2134 let winid = popup_create(buf, {})
2135 redraw
2136 call assert_equal(3, popup_getpos(winid).height)
2137 let bufinfo = getbufinfo(buf)[0]
2138 call assert_equal(1, bufinfo.changed)
2139 call assert_equal(0, bufinfo.hidden)
2140 call assert_equal(0, bufinfo.listed)
2141 call assert_equal(1, bufinfo.loaded)
2142 call assert_equal([], bufinfo.windows)
2143 call assert_equal([winid], bufinfo.popups)
2144
2145 call popup_close(winid)
2146 call assert_equal({}, popup_getpos(winid))
2147 let bufinfo = getbufinfo(buf)[0]
2148 call assert_equal(1, bufinfo.changed)
2149 call assert_equal(1, bufinfo.hidden)
2150 call assert_equal(0, bufinfo.listed)
2151 call assert_equal(1, bufinfo.loaded)
2152 call assert_equal([], bufinfo.windows)
2153 call assert_equal([], bufinfo.popups)
2154 exe 'bwipe! ' .. buf
2155endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002156
2157func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002158 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002159
2160 let lines =<< trim END
2161 call setline(1, range(1, 10))
2162 hi ScrollThumb ctermbg=blue
2163 hi ScrollBar ctermbg=red
2164 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002165 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002166 \ maxwidth: 10,
2167 \ maxheight: 3,
2168 \ pos : 'topleft',
2169 \ col : a:col,
2170 \ line : a:line,
2171 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002172 \ })
2173 endfunc
2174 call PopupMenu(['x'], 1, 1)
2175 call PopupMenu(['123456789|'], 1, 16)
2176 call PopupMenu(['123456789|' .. ' '], 7, 1)
2177 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2178 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2179 END
2180 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002181 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002182 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2183
2184 " close the menu popupwin.
2185 call term_sendkeys(buf, " ")
2186 call term_sendkeys(buf, " ")
2187 call term_sendkeys(buf, " ")
2188 call term_sendkeys(buf, " ")
2189 call term_sendkeys(buf, " ")
2190
2191 " clean up
2192 call StopVimInTerminal(buf)
2193 call delete('XtestPopupMenuMaxWidth')
2194endfunc
2195
Bram Moolenaara901a372019-07-13 16:38:50 +02002196func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002197 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002198
2199 let lines =<< trim END
2200 call setline(1, range(1, 20))
2201 hi ScrollThumb ctermbg=blue
2202 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002203 eval ['one', 'two', 'three', 'four', 'five',
2204 \ 'six', 'seven', 'eight', 'nine']
2205 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002206 \ minwidth: 8,
2207 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002208 \ })
2209 END
2210 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002211 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002212
2213 call term_sendkeys(buf, "j")
2214 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2215
2216 call term_sendkeys(buf, "jjj")
2217 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2218
2219 " if the cursor is the bottom line, it stays at the bottom line.
2220 call term_sendkeys(buf, repeat("j", 20))
2221 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2222
2223 call term_sendkeys(buf, "kk")
2224 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2225
2226 call term_sendkeys(buf, "k")
2227 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2228
2229 " if the cursor is in the top line, it stays in the top line.
2230 call term_sendkeys(buf, repeat("k", 20))
2231 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2232
2233 " close the menu popupwin.
2234 call term_sendkeys(buf, " ")
2235
2236 " clean up
2237 call StopVimInTerminal(buf)
2238 call delete('XtestPopupMenuScroll')
2239endfunc
2240
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002241func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002242 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002243
2244 let lines =<< trim END
2245 function! MyFilter(winid, key) abort
2246 if a:key == "0"
2247 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2248 return 1
2249 endif
2250 if a:key == "G"
2251 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2252 return 1
2253 endif
2254 if a:key == "j"
2255 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2256 return 1
2257 endif
2258 if a:key == "k"
2259 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2260 return 1
2261 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002262 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002263 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002264 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002265 endif
2266 return 0
2267 endfunction
2268 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2269 \ maxheight : 3,
2270 \ filter : 'MyFilter'
2271 \ })
2272 END
2273 call writefile(lines, 'XtestPopupMenuFilter')
2274 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2275
2276 call term_sendkeys(buf, "j")
2277 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2278
2279 call term_sendkeys(buf, "k")
2280 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2281
2282 call term_sendkeys(buf, "G")
2283 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2284
2285 call term_sendkeys(buf, "0")
2286 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2287
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002288 " check that when the popup is closed in the filter the screen is redrawn
2289 call term_sendkeys(buf, ":")
2290 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2291 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002292
2293 " clean up
2294 call StopVimInTerminal(buf)
2295 call delete('XtestPopupMenuFilter')
2296endfunc
2297
2298func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002299 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002300
2301 let winid = popup_create('some text', {})
2302 call assert_equal(0, popup_getoptions(winid).cursorline)
2303 call popup_close(winid)
2304
2305 let winid = popup_create('some text', #{ cursorline: 1, })
2306 call assert_equal(1, popup_getoptions(winid).cursorline)
2307 call popup_close(winid)
2308
2309 let winid = popup_create('some text', #{ cursorline: 0, })
2310 call assert_equal(0, popup_getoptions(winid).cursorline)
2311 call popup_close(winid)
2312
2313 let winid = popup_menu('some text', {})
2314 call assert_equal(1, popup_getoptions(winid).cursorline)
2315 call popup_close(winid)
2316
2317 let winid = popup_menu('some text', #{ cursorline: 1, })
2318 call assert_equal(1, popup_getoptions(winid).cursorline)
2319 call popup_close(winid)
2320
2321 let winid = popup_menu('some text', #{ cursorline: 0, })
2322 call assert_equal(0, popup_getoptions(winid).cursorline)
2323 call popup_close(winid)
2324
2325 " ---------
2326 " Pattern 1
2327 " ---------
2328 let lines =<< trim END
2329 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2330 END
2331 call writefile(lines, 'XtestPopupCursorLine')
2332 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2333 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2334 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2335 call StopVimInTerminal(buf)
2336
2337 " ---------
2338 " Pattern 2
2339 " ---------
2340 let lines =<< trim END
2341 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2342 END
2343 call writefile(lines, 'XtestPopupCursorLine')
2344 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2345 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2346 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2347 call StopVimInTerminal(buf)
2348
2349 " ---------
2350 " Pattern 3
2351 " ---------
2352 let lines =<< trim END
2353 function! MyFilter(winid, key) abort
2354 if a:key == "j"
2355 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2356 return 1
2357 endif
2358 if a:key == 'x'
2359 call popup_close(a:winid)
2360 return 1
2361 endif
2362 return 0
2363 endfunction
2364 call popup_menu(['111', '222', '333'], #{
2365 \ cursorline : 0,
2366 \ maxheight : 2,
2367 \ filter : 'MyFilter',
2368 \ })
2369 END
2370 call writefile(lines, 'XtestPopupCursorLine')
2371 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2372 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2373 call term_sendkeys(buf, "j")
2374 call term_sendkeys(buf, "j")
2375 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2376 call term_sendkeys(buf, "x")
2377 call StopVimInTerminal(buf)
2378
2379 " ---------
2380 " Pattern 4
2381 " ---------
2382 let lines =<< trim END
2383 function! MyFilter(winid, key) abort
2384 if a:key == "j"
2385 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2386 return 1
2387 endif
2388 if a:key == 'x'
2389 call popup_close(a:winid)
2390 return 1
2391 endif
2392 return 0
2393 endfunction
2394 call popup_menu(['111', '222', '333'], #{
2395 \ cursorline : 1,
2396 \ maxheight : 2,
2397 \ filter : 'MyFilter',
2398 \ })
2399 END
2400 call writefile(lines, 'XtestPopupCursorLine')
2401 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2402 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2403 call term_sendkeys(buf, "j")
2404 call term_sendkeys(buf, "j")
2405 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2406 call term_sendkeys(buf, "x")
2407 call StopVimInTerminal(buf)
2408
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002409 " ---------
2410 " Cursor in second line when creating the popup
2411 " ---------
2412 let lines =<< trim END
2413 let winid = popup_create(['111', '222', '333'], #{
2414 \ cursorline : 1,
2415 \ })
2416 call win_execute(winid, "2")
2417 END
2418 call writefile(lines, 'XtestPopupCursorLine')
2419 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2420 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2421 call StopVimInTerminal(buf)
2422
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002423 call delete('XtestPopupCursorLine')
2424endfunc
2425
Bram Moolenaarf914a332019-07-20 15:09:56 +02002426func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002427 CheckScreendump
2428
Bram Moolenaarf914a332019-07-20 15:09:56 +02002429 call writefile([
2430 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2431 \ "another\tXtagfile\t/^this is another",
2432 \ "theword\tXtagfile\t/^theword"],
2433 \ 'Xtags')
2434 call writefile(range(1,20)
2435 \ + ['theword is here']
2436 \ + range(22, 27)
2437 \ + ['this is another place']
2438 \ + range(29, 40),
2439 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002440 call writefile(range(1,10)
2441 \ + ['searched word is here']
2442 \ + range(12, 20),
2443 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002444 let lines =<< trim END
2445 set tags=Xtags
2446 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002447 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002448 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002449 \ 'three',
2450 \ 'four',
2451 \ 'five',
2452 \ 'six',
2453 \ 'seven',
2454 \ 'find theword somewhere',
2455 \ 'nine',
2456 \ 'this is another word',
2457 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002458 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002459 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002460 END
2461 call writefile(lines, 'XtestPreviewPopup')
2462 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2463
2464 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2465 call term_sendkeys(buf, ":\<CR>")
2466 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2467
2468 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2469 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2470
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002471 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002472 call term_sendkeys(buf, ":\<CR>")
2473 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2474
2475 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2476 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2477
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002478 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2479 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002480 call term_sendkeys(buf, ":cd testdir\<CR>")
2481
2482 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002483 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002484 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2485
2486 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2487 call term_sendkeys(buf, ":\<CR>")
2488 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2489
2490 call term_sendkeys(buf, ":pclose\<CR>")
2491 call term_sendkeys(buf, ":psearch searched\<CR>")
2492 call term_sendkeys(buf, ":\<CR>")
2493 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002494
Bram Moolenaarf914a332019-07-20 15:09:56 +02002495 call StopVimInTerminal(buf)
2496 call delete('Xtags')
2497 call delete('Xtagfile')
2498 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002499 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002500endfunc
2501
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002502func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002503 let lines =<< trim END
2504 set completeopt+=preview,popup
2505 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002506 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002507
2508 func CompleteFuncDict(findstart, base)
2509 if a:findstart
2510 if col('.') > 10
2511 return col('.') - 10
2512 endif
2513 return 0
2514 endif
2515
2516 return {
2517 \ 'words': [
2518 \ {
2519 \ 'word': 'aword',
2520 \ 'abbr': 'wrd',
2521 \ 'menu': 'extra text',
2522 \ 'info': 'words are cool',
2523 \ 'kind': 'W',
2524 \ 'user_data': 'test'
2525 \ },
2526 \ {
2527 \ 'word': 'anotherword',
2528 \ 'abbr': 'anotwrd',
2529 \ 'menu': 'extra text',
2530 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2531 \ 'kind': 'W',
2532 \ 'user_data': 'notest'
2533 \ },
2534 \ {
2535 \ 'word': 'noinfo',
2536 \ 'abbr': 'noawrd',
2537 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002538 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002539 \ 'kind': 'W',
2540 \ 'user_data': 'notest'
2541 \ },
2542 \ {
2543 \ 'word': 'thatword',
2544 \ 'abbr': 'thatwrd',
2545 \ 'menu': 'extra text',
2546 \ 'info': 'that word is cool',
2547 \ 'kind': 'W',
2548 \ 'user_data': 'notest'
2549 \ },
2550 \ ]
2551 \ }
2552 endfunc
2553 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002554 func ChangeColor()
2555 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002556 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002557 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002558
2559 func InfoHidden()
2560 set completepopup=height:4,border:off,align:menu
2561 set completeopt-=popup completeopt+=popuphidden
2562 au CompleteChanged * call HandleChange()
2563 endfunc
2564
2565 let s:counter = 0
2566 func HandleChange()
2567 let s:counter += 1
2568 let selected = complete_info(['selected']).selected
2569 if selected <= 0
2570 " First time: do nothing, info remains hidden
2571 return
2572 endif
2573 if selected == 1
2574 " Second time: show info right away
2575 let id = popup_findinfo()
2576 if id
2577 call popup_settext(id, 'immediate info ' .. s:counter)
2578 call popup_show(id)
2579 endif
2580 else
2581 " Third time: show info after a short delay
2582 call timer_start(100, 'ShowInfo')
2583 endif
2584 endfunc
2585
2586 func ShowInfo(...)
2587 let id = popup_findinfo()
2588 if id
2589 call popup_settext(id, 'async info ' .. s:counter)
2590 call popup_show(id)
2591 endif
2592 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002593 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002594 return lines
2595endfunc
2596
2597func Test_popupmenu_info_border()
2598 CheckScreendump
2599
2600 let lines = Get_popupmenu_lines()
2601 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002602 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002603
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002604 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2605 call term_wait(buf, 50)
2606
2607 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2608 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2609
2610 call term_sendkeys(buf, "\<C-N>")
2611 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2612
2613 call term_sendkeys(buf, "\<C-N>")
2614 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2615
2616 call term_sendkeys(buf, "\<C-N>\<C-N>")
2617 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2618
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002619 " info on the left with scrollbar
2620 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2621 call term_sendkeys(buf, "\<C-N>\<C-N>")
2622 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2623
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002624 call StopVimInTerminal(buf)
2625 call delete('XtestInfoPopup')
2626endfunc
2627
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002628func Test_popupmenu_info_noborder()
2629 CheckScreendump
2630
2631 let lines = Get_popupmenu_lines()
2632 call add(lines, 'set completepopup=height:4,border:off')
2633 call writefile(lines, 'XtestInfoPopupNb')
2634
2635 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2636 call term_wait(buf, 50)
2637
2638 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2639 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2640
2641 call StopVimInTerminal(buf)
2642 call delete('XtestInfoPopupNb')
2643endfunc
2644
Bram Moolenaar258cef52019-08-21 17:29:29 +02002645func Test_popupmenu_info_align_menu()
2646 CheckScreendump
2647
2648 let lines = Get_popupmenu_lines()
2649 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2650 call writefile(lines, 'XtestInfoPopupNb')
2651
2652 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2653 call term_wait(buf, 50)
2654
2655 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2656 call term_sendkeys(buf, "\<C-N>")
2657 call term_sendkeys(buf, "\<C-N>")
2658 call term_sendkeys(buf, "\<C-N>")
2659 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2660
2661 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2662 call term_sendkeys(buf, "\<C-N>")
2663 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2664
2665 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002666 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002667 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2668 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2669 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2670
2671 call StopVimInTerminal(buf)
2672 call delete('XtestInfoPopupNb')
2673endfunc
2674
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002675func Test_popupmenu_info_hidden()
2676 CheckScreendump
2677
2678 let lines = Get_popupmenu_lines()
2679 call add(lines, 'call InfoHidden()')
2680 call writefile(lines, 'XtestInfoPopupHidden')
2681
2682 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
2683 call term_wait(buf, 50)
2684
2685 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2686 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
2687
2688 call term_sendkeys(buf, "\<C-N>")
2689 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
2690
2691 call term_sendkeys(buf, "\<C-N>")
2692 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
2693
2694 call term_sendkeys(buf, "\<Esc>")
2695 call StopVimInTerminal(buf)
2696 call delete('XtestInfoPopupHidden')
2697endfunc
2698
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002699func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02002700 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002701 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002702 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002703 call assert_equal(bufnr, winbufnr(winid))
2704 call popup_clear()
2705endfunc
2706
Bram Moolenaar1824f452019-10-02 23:06:46 +02002707func Test_popupwin_getoptions_tablocal()
2708 topleft split
2709 let win1 = popup_create('nothing', #{maxheight: 8})
2710 let win2 = popup_create('something', #{maxheight: 10})
2711 let win3 = popup_create('something', #{maxheight: 15})
2712 call assert_equal(8, popup_getoptions(win1).maxheight)
2713 call assert_equal(10, popup_getoptions(win2).maxheight)
2714 call assert_equal(15, popup_getoptions(win3).maxheight)
2715 call popup_clear()
2716 quit
2717endfunc
2718
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002719func Test_popupwin_cancel()
2720 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
2721 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
2722 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
2723 call assert_equal(5, popup_getpos(win1).line)
2724 call assert_equal(10, popup_getpos(win2).line)
2725 call assert_equal(15, popup_getpos(win3).line)
2726 " TODO: this also works without patch 8.1.2110
2727 call feedkeys("\<C-C>", 'xt')
2728 call assert_equal(5, popup_getpos(win1).line)
2729 call assert_equal(10, popup_getpos(win2).line)
2730 call assert_equal({}, popup_getpos(win3))
2731 call feedkeys("\<C-C>", 'xt')
2732 call assert_equal(5, popup_getpos(win1).line)
2733 call assert_equal({}, popup_getpos(win2))
2734 call assert_equal({}, popup_getpos(win3))
2735 call feedkeys("\<C-C>", 'xt')
2736 call assert_equal({}, popup_getpos(win1))
2737 call assert_equal({}, popup_getpos(win2))
2738 call assert_equal({}, popup_getpos(win3))
2739endfunc
2740
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002741" vim: shiftwidth=2 sts=2