blob: dc382d83b51cee8708ccf7361576a020f06402de [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 \ })
281 normal 25|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 \ })
289 normal 9G29|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 \ })
297 normal 51|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 \ })
305 END
306 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200307 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200308 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
309
310 " clean up
311 call StopVimInTerminal(buf)
312 call delete('XtestPopupCorners')
313endfunc
314
Bram Moolenaar8d241042019-06-12 23:40:01 +0200315func Test_popup_firstline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200316 CheckScreendump
317
Bram Moolenaar8d241042019-06-12 23:40:01 +0200318 let lines =<< trim END
319 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200320 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200321 \ maxheight: 4,
322 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200323 \ })
324 END
325 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200326 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200327 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
328
329 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
330 call term_sendkeys(buf, ":\<CR>")
331 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200332
333 " clean up
334 call StopVimInTerminal(buf)
335 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200336
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200337 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200338 \ maxheight: 2,
339 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200340 \ })
341 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200342 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200343 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200344 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200345
346 let winid = popup_create(['xxx']->repeat(50), #{
347 \ maxheight: 3,
348 \ firstline: 11,
349 \ })
350 redraw
351 call assert_equal(11, popup_getoptions(winid).firstline)
352 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200353 " check line() works with popup window
354 call assert_equal(11, line('.', winid))
355 call assert_equal(50, line('$', winid))
356 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200357
358 " Normal command changes what is displayed but not "firstline"
359 call win_execute(winid, "normal! \<c-y>")
360 call assert_equal(11, popup_getoptions(winid).firstline)
361 call assert_equal(10, popup_getpos(winid).firstline)
362
363 " Making some property change applies "firstline" again
364 call popup_setoptions(winid, #{line: 4})
365 call assert_equal(11, popup_getoptions(winid).firstline)
366 call assert_equal(11, popup_getpos(winid).firstline)
367
368 " Remove "firstline" property and scroll
369 call popup_setoptions(winid, #{firstline: 0})
370 call win_execute(winid, "normal! \<c-y>")
371 call assert_equal(0, popup_getoptions(winid).firstline)
372 call assert_equal(10, popup_getpos(winid).firstline)
373
374 " Making some property change has no side effect
375 call popup_setoptions(winid, #{line: 3})
376 call assert_equal(0, popup_getoptions(winid).firstline)
377 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaarae943152019-06-16 22:54:14 +0200378
379 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200380endfunc
381
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200382func Test_popup_noscrolloff()
383 set scrolloff=5
384 let winid = popup_create(['xxx']->repeat(50), #{
385 \ maxheight: 5,
386 \ firstline: 11,
387 \ })
388 redraw
389 call assert_equal(11, popup_getoptions(winid).firstline)
390 call assert_equal(11, popup_getpos(winid).firstline)
391
392 call popup_setoptions(winid, #{firstline: 0})
393 call win_execute(winid, "normal! \<c-y>")
394 call assert_equal(0, popup_getoptions(winid).firstline)
395 call assert_equal(10, popup_getpos(winid).firstline)
396
397 call popup_close(winid)
398endfunc
399
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200400func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200401 CheckScreendump
402
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200403 " create a popup that covers the command line
404 let lines =<< trim END
405 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200406 split
407 vsplit
408 $wincmd w
409 vsplit
410 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200411 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200412 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200413 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200414 \ border: [],
415 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200416 \ })
417 func Dragit()
418 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
419 endfunc
420 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200421 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200422 func Resize()
423 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
424 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200425 map <silent> <F5> :call test_setmouse(6, 21)<CR>
426 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200427 END
428 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200429 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200430 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
431
432 call term_sendkeys(buf, ":call Dragit()\<CR>")
433 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
434
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200435 call term_sendkeys(buf, ":call Resize()\<CR>")
436 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
437
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200438 " clean up
439 call StopVimInTerminal(buf)
440 call delete('XtestPopupDrag')
441endfunc
442
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200443func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200444 CheckScreendump
445
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200446 let lines =<< trim END
447 call setline(1, range(1, 20))
448 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200449 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200450 \ close: 'button',
451 \ border: [],
452 \ line: 1,
453 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200454 \ })
455 func CloseMsg(id, result)
456 echomsg 'Popup closed with ' .. a:result
457 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200458 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200459 \ close: 'click',
460 \ line: 3,
461 \ col: 15,
462 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200463 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200464 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200465 \ close: 'button',
466 \ line: 5,
467 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200468 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200469 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200470 \ close: 'button',
471 \ padding: [],
472 \ line: 5,
473 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200474 \ })
475 func CloseWithX()
476 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
477 endfunc
478 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
479 func CloseWithClick()
480 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
481 endfunc
482 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200483 func CreateWithMenuFilter()
484 let winid = popup_create('barfoo', #{
485 \ close: 'button',
486 \ filter: 'popup_filter_menu',
487 \ border: [],
488 \ line: 1,
489 \ col: 40,
490 \ })
491 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200492 END
493 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200494 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200495 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
496
497 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
498 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
499
500 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
501 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
502
Bram Moolenaarf6396232019-08-24 19:36:00 +0200503 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
504 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
505
506 " We have to send the actual mouse code, feedkeys() would be caught the
507 " filter.
508 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
509 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
510
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200511 " clean up
512 call StopVimInTerminal(buf)
513 call delete('XtestPopupClose')
514endfunction
515
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200516func Test_popup_menu_wrap()
517 CheckScreendump
518
519 let lines =<< trim END
520 call setline(1, range(1, 20))
521 call popup_create([
522 \ 'one',
523 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
524 \ 'three',
525 \ 'four',
526 \ ], #{
527 \ pos: "botleft",
528 \ border: [],
529 \ padding: [0,1,0,1],
530 \ maxheight: 3,
531 \ cursorline: 1,
532 \ filter: 'popup_filter_menu',
533 \ })
534 END
535 call writefile(lines, 'XtestPopupWrap')
536 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
537 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
538
539 call term_sendkeys(buf, "jj")
540 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
541
542 " clean up
543 call term_sendkeys(buf, "\<Esc>")
544 call StopVimInTerminal(buf)
545 call delete('XtestPopupWrap')
546endfunction
547
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200548func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200549 CheckScreendump
550
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200551 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200552 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200553 hi PopupColor ctermbg=lightgrey
554 let winid = popup_create([
555 \ 'some text',
556 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200557 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200558 \ line: 1,
559 \ col: 10,
560 \ wrap: 0,
561 \ fixed: 1,
562 \ zindex: 90,
563 \ padding: [],
564 \ highlight: 'PopupColor',
565 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200566 call popup_create([
567 \ 'xxxxxxxxx',
568 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200569 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200570 \ line: 3,
571 \ col: 18,
572 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200573 let winidb = popup_create([
574 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200575 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200576 \ line: 7,
577 \ col: 10,
578 \ wrap: 0,
579 \ fixed: 1,
580 \ close: 'button',
581 \ zindex: 90,
582 \ padding: [],
583 \ border: [],
584 \ 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 +0200585 END
586 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200587 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200588 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
589
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200590 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
591 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200592 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200593 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
594
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200595 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
596 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200597 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200598 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
599
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200600 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
601 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200602 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200603 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
604
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200605 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
606 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200607 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200608 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
609
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200610 " clean up
611 call StopVimInTerminal(buf)
612 call delete('XtestPopupMask')
613endfunc
614
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200615func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200616 CheckScreendump
617 CheckFeature clipboard_working
618
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200619 " create a popup with some text to be selected
620 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200621 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200622 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200623 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200624 \ drag: 1,
625 \ border: [],
626 \ line: 3,
627 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200628 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200629 \ })
630 func Select1()
631 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
632 endfunc
633 map <silent> <F3> :call test_setmouse(4, 15)<CR>
634 map <silent> <F4> :call test_setmouse(6, 23)<CR>
635 END
636 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200637 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200638 call term_sendkeys(buf, ":call Select1()\<CR>")
639 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
640
641 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
642 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200643 " clean the command line, sometimes it still shows a command
644 call term_sendkeys(buf, ":\<esc>")
645
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200646 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
647
648 " clean up
649 call StopVimInTerminal(buf)
650 call delete('XtestPopupSelect')
651endfunc
652
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200653func Test_popup_in_tab()
654 " default popup is local to tab, not visible when in other tab
655 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200656 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200657 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200658 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200659 tabnew
660 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200661 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200662 quit
663 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200664
665 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200666 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200667 " buffer is gone now
668 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200669
670 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200671 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200672 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200673 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200674 tabnew
675 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200676 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200677 quit
678 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200679 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200680
681 " create popup in other tab
682 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200683 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200684 call assert_equal(0, popup_getpos(winid).visible)
685 call assert_equal(1, popup_getoptions(winid).tabpage)
686 quit
687 call assert_equal(1, popup_getpos(winid).visible)
688 call assert_equal(0, popup_getoptions(winid).tabpage)
689 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200690endfunc
691
692func Test_popup_valid_arguments()
693 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200694 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200695 let pos = popup_getpos(winid)
696 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200697 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200698
699 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200700 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200701 let pos = popup_getpos(winid)
702 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200703 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200704
705 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200706 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200707 let pos = popup_getpos(winid)
708 let around = (&columns - pos.width) / 2
709 call assert_inrange(around - 1, around + 1, pos.col)
710 let around = (&lines - pos.height) / 2
711 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200712 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200713endfunc
714
715func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200716 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200717 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200718 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200719 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200720
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200721 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200722 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200723 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200724 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200725 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200726 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200727 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200728 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200729
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200730 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200731 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200732 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200733 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200734 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200735 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200736 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200737 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200738
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200739 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200740 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200741 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200742 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200743 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200744 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200745 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200746 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200747 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
748 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200749 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200750 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200751
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200752 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200753 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200754 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200755 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200756 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200757 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200758 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
759 call popup_clear()
760 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200761 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200762 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200763endfunc
764
Bram Moolenaareea16992019-05-31 17:34:48 +0200765func Test_win_execute_closing_curwin()
766 split
767 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200768 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200769 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200770endfunc
771
772func Test_win_execute_not_allowed()
773 let winid = popup_create('some text', {})
774 call assert_fails('call win_execute(winid, "split")', 'E994:')
775 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
776 call assert_fails('call win_execute(winid, "close")', 'E994:')
777 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200778 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200779 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
780 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
781 call assert_fails('call win_execute(winid, "next")', 'E994:')
782 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
783 call assert_fails('call win_execute(winid, "buf")', 'E994:')
784 call assert_fails('call win_execute(winid, "edit")', 'E994:')
785 call assert_fails('call win_execute(winid, "enew")', 'E994:')
786 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
787 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
788 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
789 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200790 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200791endfunc
792
Bram Moolenaar402502d2019-05-30 22:07:36 +0200793func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200794 CheckScreendump
795
Bram Moolenaar402502d2019-05-30 22:07:36 +0200796 let lines =<< trim END
797 call setline(1, range(1, 100))
798 let winid = popup_create(
799 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200800 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200801 END
802 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200803 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200804 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
805
806 " clean up
807 call StopVimInTerminal(buf)
808 call delete('XtestPopup')
809endfunc
810
811func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200812 CheckScreendump
813
Bram Moolenaar402502d2019-05-30 22:07:36 +0200814 let lines =<< trim END
815 call setline(1, range(1, 100))
816 let winid = popup_create(
817 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200818 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200819 END
820 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200821 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200822 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
823
824 " clean up
825 call StopVimInTerminal(buf)
826 call delete('XtestPopup')
827endfunc
828
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200829func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200830 CheckScreendump
831
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200832 let lines =<< trim END
833 set showbreak=>>\
834 call setline(1, range(1, 20))
835 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +0200836 \ 'a long line here that wraps',
837 \ #{filter: 'popup_filter_yesno',
838 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200839 END
840 call writefile(lines, 'XtestPopupShowbreak')
841 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
842 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
843
844 " clean up
845 call term_sendkeys(buf, "y")
846 call StopVimInTerminal(buf)
847 call delete('XtestPopupShowbreak')
848endfunc
849
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200850func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200851 CheckFeature timers
852
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200853 topleft vnew
854 call setline(1, 'hello')
855
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200856 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200857 \ line: 1,
858 \ col: 1,
859 \ minwidth: 20,
860 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200861 \})
862 redraw
863 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
864 call assert_equal('world', line)
865
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200866 call assert_equal(winid, popup_locate(1, 1))
867 call assert_equal(winid, popup_locate(1, 20))
868 call assert_equal(0, popup_locate(1, 21))
869 call assert_equal(0, popup_locate(2, 1))
870
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200871 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200872 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +0200873 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200874 call assert_equal('hello', line)
875
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200876 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200877 \ line: &lines,
878 \ col: 10,
879 \ minwidth: 20,
880 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200881 \})
882 redraw
883 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
884 call assert_match('.*on the command line.*', line)
885
886 sleep 700m
887 redraw
888 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
889 call assert_notmatch('.*on the command line.*', line)
890
891 bwipe!
892endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200893
894func Test_popup_hide()
895 topleft vnew
896 call setline(1, 'hello')
897
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200898 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200899 \ line: 1,
900 \ col: 1,
901 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200902 \})
903 redraw
904 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
905 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200906 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200907 " buffer is still listed and active
908 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200909
910 call popup_hide(winid)
911 redraw
912 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
913 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200914 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200915 " buffer is still listed but hidden
916 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200917
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200918 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200919 redraw
920 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
921 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200922 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200923
924
925 call popup_close(winid)
926 redraw
927 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
928 call assert_equal('hello', line)
929
930 " error is given for existing non-popup window
931 call assert_fails('call popup_hide(win_getid())', 'E993:')
932
933 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200934 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200935 call popup_show(41234234)
936
937 bwipe!
938endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200939
940func Test_popup_move()
941 topleft vnew
942 call setline(1, 'hello')
943
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200944 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200945 \ line: 1,
946 \ col: 1,
947 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200948 \})
949 redraw
950 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
951 call assert_equal('world ', line)
952
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200953 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200954 redraw
955 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
956 call assert_equal('hello ', line)
957 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
958 call assert_equal('~world', line)
959
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200960 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200961 redraw
962 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
963 call assert_equal('hworld', line)
964
965 call popup_close(winid)
966
967 bwipe!
968endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200969
Bram Moolenaar402502d2019-05-30 22:07:36 +0200970func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200971 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200972 \ line: 2,
973 \ col: 3,
974 \ minwidth: 10,
975 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200976 \})
977 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200978 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200979 call assert_equal(2, res.line)
980 call assert_equal(3, res.col)
981 call assert_equal(10, res.width)
982 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200983 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200984
985 call popup_close(winid)
986endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200987
988func Test_popup_width_longest()
989 let tests = [
990 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
991 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
992 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
993 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
994 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
995 \ ]
996
997 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200998 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200999 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001000 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001001 call assert_equal(test[1], position.width)
1002 call popup_close(winid)
1003 endfor
1004endfunc
1005
1006func Test_popup_wraps()
1007 let tests = [
1008 \ ['nowrap', 6, 1],
1009 \ ['a line that wraps once', 12, 2],
1010 \ ['a line that wraps two times', 12, 3],
1011 \ ]
1012 for test in tests
1013 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001014 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001015 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001016 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001017 call assert_equal(test[1], position.width)
1018 call assert_equal(test[2], position.height)
1019
1020 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001021 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001022 endfor
1023endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001024
1025func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001026 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001027 \ line: 2,
1028 \ col: 3,
1029 \ minwidth: 10,
1030 \ minheight: 11,
1031 \ maxwidth: 20,
1032 \ maxheight: 21,
1033 \ zindex: 100,
1034 \ time: 5000,
1035 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001036 \})
1037 redraw
1038 let res = popup_getoptions(winid)
1039 call assert_equal(2, res.line)
1040 call assert_equal(3, res.col)
1041 call assert_equal(10, res.minwidth)
1042 call assert_equal(11, res.minheight)
1043 call assert_equal(20, res.maxwidth)
1044 call assert_equal(21, res.maxheight)
1045 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001046 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001047 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001048 if has('timers')
1049 call assert_equal(5000, res.time)
1050 endif
1051 call popup_close(winid)
1052
1053 let winid = popup_create('hello', {})
1054 redraw
1055 let res = popup_getoptions(winid)
1056 call assert_equal(0, res.line)
1057 call assert_equal(0, res.col)
1058 call assert_equal(0, res.minwidth)
1059 call assert_equal(0, res.minheight)
1060 call assert_equal(0, res.maxwidth)
1061 call assert_equal(0, res.maxheight)
1062 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001063 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001064 if has('timers')
1065 call assert_equal(0, res.time)
1066 endif
1067 call popup_close(winid)
1068 call assert_equal({}, popup_getoptions(winid))
1069endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001070
1071func Test_popup_option_values()
1072 new
1073 " window-local
1074 setlocal number
1075 setlocal nowrap
1076 " buffer-local
1077 setlocal omnifunc=Something
1078 " global/buffer-local
1079 setlocal path=/there
1080 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001081 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001082
1083 let winid = popup_create('hello', {})
1084 call assert_equal(0, getwinvar(winid, '&number'))
1085 call assert_equal(1, getwinvar(winid, '&wrap'))
1086 call assert_equal('', getwinvar(winid, '&omnifunc'))
1087 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001088 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1089
1090 " 'scrolloff' is reset to zero
1091 call assert_equal(5, &scrolloff)
1092 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001093
1094 call popup_close(winid)
1095 bwipe
1096endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001097
1098func Test_popup_atcursor()
1099 topleft vnew
1100 call setline(1, [
1101 \ 'xxxxxxxxxxxxxxxxx',
1102 \ 'xxxxxxxxxxxxxxxxx',
1103 \ 'xxxxxxxxxxxxxxxxx',
1104 \])
1105
1106 call cursor(2, 2)
1107 redraw
1108 let winid = popup_atcursor('vim', {})
1109 redraw
1110 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1111 call assert_equal('xvimxxxxxxxxxxxxx', line)
1112 call popup_close(winid)
1113
1114 call cursor(3, 4)
1115 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001116 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001117 redraw
1118 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1119 call assert_equal('xxxvimxxxxxxxxxxx', line)
1120 call popup_close(winid)
1121
1122 call cursor(1, 1)
1123 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001124 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001125 \ line: 'cursor+2',
1126 \ col: 'cursor+1',
1127 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001128 redraw
1129 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1130 call assert_equal('xvimxxxxxxxxxxxxx', line)
1131 call popup_close(winid)
1132
1133 call cursor(3, 3)
1134 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001135 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001136 \ line: 'cursor-2',
1137 \ col: 'cursor-1',
1138 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001139 redraw
1140 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1141 call assert_equal('xvimxxxxxxxxxxxxx', line)
1142 call popup_close(winid)
1143
Bram Moolenaar402502d2019-05-30 22:07:36 +02001144 " just enough room above
1145 call cursor(3, 3)
1146 redraw
1147 let winid = popup_atcursor(['vim', 'is great'], {})
1148 redraw
1149 let pos = popup_getpos(winid)
1150 call assert_equal(1, pos.line)
1151 call popup_close(winid)
1152
1153 " not enough room above, popup goes below the cursor
1154 call cursor(3, 3)
1155 redraw
1156 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1157 redraw
1158 let pos = popup_getpos(winid)
1159 call assert_equal(4, pos.line)
1160 call popup_close(winid)
1161
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001162 " cursor in first line, popup in line 2
1163 call cursor(1, 1)
1164 redraw
1165 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1166 redraw
1167 let pos = popup_getpos(winid)
1168 call assert_equal(2, pos.line)
1169 call popup_close(winid)
1170
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001171 bwipe!
1172endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001173
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001174func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001175 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001176 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001177
1178 let lines =<< trim END
1179 call setline(1, range(1, 20))
1180 call setline(5, 'here is some text to hover over')
1181 set balloonevalterm
1182 set balloonexpr=BalloonExpr()
1183 set balloondelay=100
1184 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001185 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001186 return ''
1187 endfunc
1188 func Hover()
1189 call test_setmouse(5, 15)
1190 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1191 sleep 100m
1192 endfunc
1193 func MoveOntoPopup()
1194 call test_setmouse(4, 17)
1195 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1196 endfunc
1197 func MoveAway()
1198 call test_setmouse(5, 13)
1199 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1200 endfunc
1201 END
1202 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001203 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001204 call term_wait(buf, 100)
1205 call term_sendkeys(buf, 'j')
1206 call term_sendkeys(buf, ":call Hover()\<CR>")
1207 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1208
1209 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1210 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1211
1212 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1213 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1214
1215 " clean up
1216 call StopVimInTerminal(buf)
1217 call delete('XtestPopupBeval')
1218endfunc
1219
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001220func Test_popup_filter()
1221 new
1222 call setline(1, 'some text')
1223
1224 func MyPopupFilter(winid, c)
1225 if a:c == 'e'
1226 let g:eaten = 'e'
1227 return 1
1228 endif
1229 if a:c == '0'
1230 let g:ignored = '0'
1231 return 0
1232 endif
1233 if a:c == 'x'
1234 call popup_close(a:winid)
1235 return 1
1236 endif
1237 return 0
1238 endfunc
1239
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001240 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001241 redraw
1242
1243 " e is consumed by the filter
1244 call feedkeys('e', 'xt')
1245 call assert_equal('e', g:eaten)
1246
1247 " 0 is ignored by the filter
1248 normal $
1249 call assert_equal(9, getcurpos()[2])
1250 call feedkeys('0', 'xt')
1251 call assert_equal('0', g:ignored)
1252 call assert_equal(1, getcurpos()[2])
1253
1254 " x closes the popup
1255 call feedkeys('x', 'xt')
1256 call assert_equal('e', g:eaten)
1257 call assert_equal(-1, winbufnr(winid))
1258
1259 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001260 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001261endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001262
Bram Moolenaara42d9452019-06-15 21:46:30 +02001263func ShowDialog(key, result)
1264 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001265 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001266 \ filter: 'popup_filter_yesno',
1267 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001268 \ })
1269 redraw
1270 call feedkeys(a:key, "xt")
1271 call assert_equal(winid, s:cb_winid)
1272 call assert_equal(a:result, s:cb_res)
1273endfunc
1274
1275func Test_popup_dialog()
1276 func QuitCallback(id, res)
1277 let s:cb_winid = a:id
1278 let s:cb_res = a:res
1279 endfunc
1280
1281 let winid = ShowDialog("y", 1)
1282 let winid = ShowDialog("Y", 1)
1283 let winid = ShowDialog("n", 0)
1284 let winid = ShowDialog("N", 0)
1285 let winid = ShowDialog("x", 0)
1286 let winid = ShowDialog("X", 0)
1287 let winid = ShowDialog("\<Esc>", 0)
1288 let winid = ShowDialog("\<C-C>", -1)
1289
1290 delfunc QuitCallback
1291endfunc
1292
Bram Moolenaara730e552019-06-16 19:05:31 +02001293func ShowMenu(key, result)
1294 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001295 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001296 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001297 \ })
1298 redraw
1299 call feedkeys(a:key, "xt")
1300 call assert_equal(winid, s:cb_winid)
1301 call assert_equal(a:result, s:cb_res)
1302endfunc
1303
1304func Test_popup_menu()
1305 func QuitCallback(id, res)
1306 let s:cb_winid = a:id
1307 let s:cb_res = a:res
1308 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001309 " mapping won't be used in popup
1310 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001311
1312 let winid = ShowMenu(" ", 1)
1313 let winid = ShowMenu("j \<CR>", 2)
1314 let winid = ShowMenu("JjK \<CR>", 2)
1315 let winid = ShowMenu("jjjjjj ", 3)
1316 let winid = ShowMenu("kkk ", 1)
1317 let winid = ShowMenu("x", -1)
1318 let winid = ShowMenu("X", -1)
1319 let winid = ShowMenu("\<Esc>", -1)
1320 let winid = ShowMenu("\<C-C>", -1)
1321
1322 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001323 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001324endfunc
1325
1326func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001327 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001328
1329 let lines =<< trim END
1330 call setline(1, range(1, 20))
1331 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001332 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001333 func MenuDone(id, res)
1334 echomsg "selected " .. a:res
1335 endfunc
1336 END
1337 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001338 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001339 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1340
1341 call term_sendkeys(buf, "jj")
1342 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1343
1344 call term_sendkeys(buf, " ")
1345 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1346
1347 " clean up
1348 call StopVimInTerminal(buf)
1349 call delete('XtestPopupMenu')
1350endfunc
1351
Bram Moolenaarf914a332019-07-20 15:09:56 +02001352func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001353 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001354
1355 let lines =<< trim END
1356 call setline(1, range(1, 20))
1357 hi PopupSelected ctermbg=green
1358 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1359 func MenuDone(id, res)
1360 echomsg "selected " .. a:res
1361 endfunc
1362 END
1363 call writefile(lines, 'XtestPopupNarrowMenu')
1364 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1365 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1366
1367 " clean up
1368 call term_sendkeys(buf, "x")
1369 call StopVimInTerminal(buf)
1370 call delete('XtestPopupNarrowMenu')
1371endfunc
1372
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001373func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001374 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001375
1376 " Create a popup without title or border, a line of padding will be added to
1377 " put the title on.
1378 let lines =<< trim END
1379 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001380 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001381 END
1382 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001383 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001384 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1385
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001386 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1387 call term_sendkeys(buf, ":\<CR>")
1388 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1389
1390 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1391 call term_sendkeys(buf, ":\<CR>")
1392 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1393
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001394 " clean up
1395 call StopVimInTerminal(buf)
1396 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001397
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001398 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001399 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001400 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001401 call assert_equal('Another Title', popup_getoptions(winid).title)
1402
1403 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001404endfunc
1405
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001406func Test_popup_close_callback()
1407 func PopupDone(id, result)
1408 let g:result = a:result
1409 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001410 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001411 redraw
1412 call popup_close(winid, 'done')
1413 call assert_equal('done', g:result)
1414endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001415
1416func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001417 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001418 redraw
1419 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001420 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001421 call assert_equal(5, pos.height)
1422
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001423 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001424 redraw
1425 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001426 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001427 call assert_equal(3, pos.height)
1428endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001429
1430func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001431 CheckScreendump
1432
Bram Moolenaar988c4332019-06-02 14:12:11 +02001433 " +-----------------------------+
1434 " | | |
1435 " | | |
1436 " | | |
1437 " | line1 |
1438 " |------------line2------------|
1439 " | line3 |
1440 " | line4 |
1441 " | |
1442 " | |
1443 " +-----------------------------+
1444 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001445 split
1446 vsplit
1447 let info_window1 = getwininfo()[0]
1448 let line = info_window1['height']
1449 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001450 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001451 \ line : line,
1452 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001453 \ })
1454 END
1455 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001456 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001457 call term_sendkeys(buf, "\<C-W>w")
1458 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1459
1460 " clean up
1461 call StopVimInTerminal(buf)
1462 call delete('XtestPopupBehind')
1463endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001464
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001465func s:VerifyPosition(p, msg, line, col, width, height)
1466 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1467 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1468 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1469 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001470endfunc
1471
1472func Test_popup_position_adjust()
1473 " Anything placed past 2 cells from of the right of the screen is moved to the
1474 " left.
1475 "
1476 " When wrapping is disabled, we also shift to the left to display on the
1477 " screen, unless fixed is set.
1478
1479 " Entries for cases which don't vary based on wrapping.
1480 " Format is per tests described below
1481 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001482 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1483 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1484 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1485 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1486 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1487 \
1488 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1489 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1490 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1491 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1492 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1493 \
1494 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1495 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1496 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1497 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1498 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1499 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001500
1501 " these test groups are dicts with:
1502 " - comment: something to identify the group of tests by
1503 " - options: dict of options to merge with the row/col in tests
1504 " - tests: list of cases. Each one is a list with elements:
1505 " - text
1506 " - row
1507 " - col
1508 " - expected row
1509 " - expected col
1510 " - expected width
1511 " - expected height
1512 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001513 \ #{
1514 \ comment: 'left-aligned with wrapping',
1515 \ options: #{
1516 \ wrap: 1,
1517 \ pos: 'botleft',
1518 \ },
1519 \ tests: both_wrap_tests + [
1520 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1521 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1522 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1523 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1524 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1525 \ ],
1526 \ },
1527 \ #{
1528 \ comment: 'left aligned without wrapping',
1529 \ options: #{
1530 \ wrap: 0,
1531 \ pos: 'botleft',
1532 \ },
1533 \ tests: both_wrap_tests + [
1534 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1535 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1536 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1537 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1538 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1539 \ ],
1540 \ },
1541 \ #{
1542 \ comment: 'left aligned with fixed position',
1543 \ options: #{
1544 \ wrap: 0,
1545 \ fixed: 1,
1546 \ pos: 'botleft',
1547 \ },
1548 \ tests: both_wrap_tests + [
1549 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1550 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1551 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1552 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1553 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1554 \ ],
1555 \ },
1556 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001557
1558 for test_group in tests
1559 for test in test_group.tests
1560 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001561 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001562 \ line: line,
1563 \ col: col,
1564 \ }
1565 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001566
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001567 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001568
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001569 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001570 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1571 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001572 endfor
1573 endfor
1574
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001575 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001576 %bwipe!
1577endfunc
1578
Bram Moolenaar3397f742019-06-02 18:40:06 +02001579func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001580 " width of screen
1581 let X = join(map(range(&columns), {->'X'}), '')
1582
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001583 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1584 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001585
1586 redraw
1587 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1588 call assert_equal(X, line)
1589
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001590 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001591 redraw
1592
1593 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001594 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1595 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001596
1597 redraw
1598 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1599 call assert_equal(X, line)
1600
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001601 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001602 redraw
1603
1604 " Extend so > window width
1605 let X .= 'x'
1606
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001607 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1608 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001609
1610 redraw
1611 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1612 call assert_equal(X[ : -2 ], line)
1613
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001614 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001615 redraw
1616
1617 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001618 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1619 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001620
1621 redraw
1622 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1623 call assert_equal(X[ : -2 ], line)
1624
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001625 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001626 redraw
1627
1628 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001629 let p = popup_create(X,
1630 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1631 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001632
1633 redraw
1634 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1635 let e_line = ' ' . X[ 1 : -2 ]
1636 call assert_equal(e_line, line)
1637
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001638 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001639 redraw
1640
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001641 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001642 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001643endfunc
1644
1645func Test_popup_moved()
1646 new
1647 call test_override('char_avail', 1)
1648 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1649
1650 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001651 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001652 redraw
1653 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001654 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001655 " trigger the check for last_cursormoved by going into insert mode
1656 call feedkeys("li\<Esc>", 'xt')
1657 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001658 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001659
1660 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001661 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001662 redraw
1663 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001664 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001665 call feedkeys("hi\<Esc>", 'xt')
1666 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001667 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001668
1669 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001670 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001671 redraw
1672 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001673 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001674 call feedkeys("li\<Esc>", 'xt')
1675 call assert_equal(1, popup_getpos(winid).visible)
1676 call feedkeys("ei\<Esc>", 'xt')
1677 call assert_equal(1, popup_getpos(winid).visible)
1678 call feedkeys("eli\<Esc>", 'xt')
1679 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001680 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001681
Bram Moolenaar17627312019-06-02 19:53:44 +02001682 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001683 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001684 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001685 redraw
1686 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001687 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001688 call feedkeys("eli\<Esc>", 'xt')
1689 call assert_equal(1, popup_getpos(winid).visible)
1690 call feedkeys("wi\<Esc>", 'xt')
1691 call assert_equal(1, popup_getpos(winid).visible)
1692 call feedkeys("Eli\<Esc>", 'xt')
1693 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001694 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001695
1696 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001697 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001698 redraw
1699 call assert_equal(1, popup_getpos(winid).visible)
1700 call feedkeys("eli\<Esc>", 'xt')
1701 call feedkeys("ei\<Esc>", 'xt')
1702 call assert_equal(1, popup_getpos(winid).visible)
1703 call feedkeys("eli\<Esc>", 'xt')
1704 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001705 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001706
1707 bwipe!
1708 call test_override('ALL', 0)
1709endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001710
1711func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001712 CheckFeature timers
1713 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001714
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001715 let lines =<< trim END
1716 call setline(1, range(1, 20))
1717 hi Notification ctermbg=lightblue
1718 call popup_notification('first notification', {})
1719 END
1720 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001721 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001722 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1723
1724 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001725 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1726 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001727 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1728
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001729 " clean up
1730 call StopVimInTerminal(buf)
1731 call delete('XtestNotifications')
1732endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001733
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001734func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001735 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001736
1737 let lines =<< trim END
1738 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001739 hi ScrollThumb ctermbg=blue
1740 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001741 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001742 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001743 \ minwidth: 8,
1744 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001745 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001746 func ScrollUp()
1747 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1748 endfunc
1749 func ScrollDown()
1750 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1751 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001752 func ClickTop()
1753 call feedkeys("\<F4>\<LeftMouse>", "xt")
1754 endfunc
1755 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001756 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001757 call feedkeys("\<F5>\<LeftMouse>", "xt")
1758 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001759 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001760 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1761 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001762 END
1763 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001764 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001765 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1766
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001767 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001768 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001769 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1770
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001771 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001772 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001773 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1774
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001775 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001776 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001777 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1778
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02001779 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001780 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001781 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1782 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1783
1784 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1785 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1786
1787 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001788 " wait a bit, otherwise it fails sometimes (double click recognized?)
1789 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001790 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1791 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1792
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001793 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1794 sleep 100m
1795 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1796 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1797
1798 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1799 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1800
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001801 " remove the minwidth and maxheight
1802 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02001803 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001804 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
1805
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001806 " clean up
1807 call StopVimInTerminal(buf)
1808 call delete('XtestPopupScroll')
1809endfunc
1810
Bram Moolenaar437a7462019-07-05 20:17:22 +02001811func Test_popup_fitting_scrollbar()
1812 " this was causing a crash, divide by zero
1813 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001814 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001815 \ scrollbar: 1,
1816 \ maxwidth: 10,
1817 \ maxheight: 5,
1818 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001819 redraw
1820 call popup_clear()
1821endfunc
1822
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001823func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001824 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001825
1826 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001827 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001828 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001829 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001830 END
1831
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001832 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001833 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001834 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1835
1836 " Setting to empty string clears it
1837 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1838 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1839
1840 " Setting a list
1841 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1842 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1843
1844 " Shrinking with a list
1845 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1846 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1847
1848 " Growing with a list
1849 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1850 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1851
1852 " Empty list clears
1853 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1854 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1855
1856 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001857 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001858 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1859
1860 " clean up
1861 call StopVimInTerminal(buf)
1862 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001863endfunc
1864
1865func Test_popup_hidden()
1866 new
1867
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001868 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001869 redraw
1870 call assert_equal(0, popup_getpos(winid).visible)
1871 call popup_close(winid)
1872
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001873 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001874 redraw
1875 call assert_equal(0, popup_getpos(winid).visible)
1876 call popup_close(winid)
1877
1878 func QuitCallback(id, res)
1879 let s:cb_winid = a:id
1880 let s:cb_res = a:res
1881 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001882 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001883 \ filter: 'popup_filter_yesno',
1884 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001885 \ })
1886 redraw
1887 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001888 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1889 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001890 exe "normal anot used by filter\<Esc>"
1891 call assert_equal('not used by filter', getline(1))
1892
1893 call popup_show(winid)
1894 call feedkeys('y', "xt")
1895 call assert_equal(1, s:cb_res)
1896
1897 bwipe!
1898 delfunc QuitCallback
1899endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001900
1901" Test options not checked elsewhere
1902func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001903 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001904 let options = popup_getoptions(winid)
1905 call assert_equal(1, options.wrap)
1906 call assert_equal(0, options.drag)
1907 call assert_equal('Beautiful', options.highlight)
1908
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001909 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001910 let options = popup_getoptions(winid)
1911 call assert_equal(0, options.wrap)
1912 call assert_equal(1, options.drag)
1913 call assert_equal('Another', options.highlight)
1914
1915 call popup_close(winid)
1916endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001917
1918func Test_popupwin_garbage_collect()
1919 func MyPopupFilter(x, winid, c)
1920 " NOP
1921 endfunc
1922
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001923 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001924 call test_garbagecollect_now()
1925 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001926 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001927 call feedkeys('j', 'xt')
1928 call assert_true(v:true)
1929
1930 call popup_close(winid)
1931 delfunc MyPopupFilter
1932endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001933
Bram Moolenaar581ba392019-09-03 22:08:33 +02001934func Test_popupwin_filter_mode()
1935 func MyPopupFilter(winid, c)
1936 let s:typed = a:c
1937 if a:c == ':' || a:c == "\r" || a:c == 'v'
1938 " can start cmdline mode, get out, and start/stop Visual mode
1939 return 0
1940 endif
1941 return 1
1942 endfunc
1943
1944 " Normal, Visual and Insert mode
1945 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
1946 redraw
1947 call feedkeys('x', 'xt')
1948 call assert_equal('x', s:typed)
1949
1950 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
1951 call assert_equal(':', s:typed)
1952 call assert_equal('foo', g:foo)
1953
1954 let @x = 'something'
1955 call feedkeys('v$"xy', 'xt')
1956 call assert_equal('y', s:typed)
1957 call assert_equal('something', @x) " yank command is filtered out
1958 call feedkeys('v', 'xt') " end Visual mode
1959
1960 call popup_close(winid)
1961
1962 " only Normal mode
1963 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
1964 redraw
1965 call feedkeys('x', 'xt')
1966 call assert_equal('x', s:typed)
1967
1968 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
1969 call assert_equal(':', s:typed)
1970 call assert_equal('foo', g:foo)
1971
1972 let @x = 'something'
1973 call feedkeys('v$"xy', 'xt')
1974 call assert_equal('v', s:typed)
1975 call assert_notequal('something', @x)
1976
1977 call popup_close(winid)
1978
1979 " default: all modes
1980 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
1981 redraw
1982 call feedkeys('x', 'xt')
1983 call assert_equal('x', s:typed)
1984
1985 let g:foo = 'bar'
1986 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
1987 call assert_equal("\r", s:typed)
1988 call assert_equal('bar', g:foo)
1989
1990 let @x = 'something'
1991 call feedkeys('v$"xy', 'xt')
1992 call assert_equal('y', s:typed)
1993 call assert_equal('something', @x) " yank command is filtered out
1994 call feedkeys('v', 'xt') " end Visual mode
1995
1996 call popup_close(winid)
1997 delfunc MyPopupFilter
1998endfunc
1999
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002000func Test_popupwin_with_buffer()
2001 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2002 let buf = bufadd('XsomeFile')
2003 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002004
2005 setlocal number
2006 call setbufvar(buf, "&wrapmargin", 13)
2007
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002008 let winid = popup_create(buf, {})
2009 call assert_notequal(0, winid)
2010 let pos = popup_getpos(winid)
2011 call assert_equal(2, pos.height)
2012 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002013
2014 " window-local option is set to default, buffer-local is not
2015 call assert_equal(0, getwinvar(winid, '&number'))
2016 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2017
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002018 call popup_close(winid)
2019 call assert_equal({}, popup_getpos(winid))
2020 call assert_equal(1, bufloaded(buf))
2021 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002022 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002023
2024 edit test_popupwin.vim
2025 let winid = popup_create(bufnr(''), {})
2026 redraw
2027 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002028 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002029endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002030
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002031func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002032 CheckFeature terminal
2033
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002034 let ptybuf = term_start(&shell, #{hidden: 1})
2035 call assert_fails('let winnr = popup_create(ptybuf, #{})', 'E278:')
2036 exe 'bwipe! ' .. ptybuf
2037endfunc
2038
Bram Moolenaar934470e2019-09-01 23:27:05 +02002039func Test_popupwin_with_buffer_and_filter()
2040 new Xwithfilter
2041 call setline(1, range(100))
2042 let bufnr = bufnr()
2043 hide
2044
2045 func BufferFilter(win, key)
2046 if a:key == 'G'
2047 " recursive use of "G" does not cause problems.
2048 call win_execute(a:win, 'normal! G')
2049 return 1
2050 endif
2051 return 0
2052 endfunc
2053
2054 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2055 call assert_equal(1, popup_getpos(winid).firstline)
2056 redraw
2057 call feedkeys("G", 'xt')
2058 call assert_equal(99, popup_getpos(winid).firstline)
2059
2060 call popup_close(winid)
2061 exe 'bwipe! ' .. bufnr
2062endfunc
2063
Bram Moolenaare296e312019-07-03 23:20:18 +02002064func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002065 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002066 \ maxwidth: 40,
2067 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002068 \ })
2069 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002070 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002071 redraw
2072 call assert_equal(19, popup_getpos(winid).width)
2073 endfor
2074 call popup_clear()
2075endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002076
2077func Test_popupwin_buf_close()
2078 let buf = bufadd('Xtestbuf')
2079 call bufload(buf)
2080 call setbufline(buf, 1, ['just', 'some', 'lines'])
2081 let winid = popup_create(buf, {})
2082 redraw
2083 call assert_equal(3, popup_getpos(winid).height)
2084 let bufinfo = getbufinfo(buf)[0]
2085 call assert_equal(1, bufinfo.changed)
2086 call assert_equal(0, bufinfo.hidden)
2087 call assert_equal(0, bufinfo.listed)
2088 call assert_equal(1, bufinfo.loaded)
2089 call assert_equal([], bufinfo.windows)
2090 call assert_equal([winid], bufinfo.popups)
2091
2092 call popup_close(winid)
2093 call assert_equal({}, popup_getpos(winid))
2094 let bufinfo = getbufinfo(buf)[0]
2095 call assert_equal(1, bufinfo.changed)
2096 call assert_equal(1, bufinfo.hidden)
2097 call assert_equal(0, bufinfo.listed)
2098 call assert_equal(1, bufinfo.loaded)
2099 call assert_equal([], bufinfo.windows)
2100 call assert_equal([], bufinfo.popups)
2101 exe 'bwipe! ' .. buf
2102endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002103
2104func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002105 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002106
2107 let lines =<< trim END
2108 call setline(1, range(1, 10))
2109 hi ScrollThumb ctermbg=blue
2110 hi ScrollBar ctermbg=red
2111 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002112 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002113 \ maxwidth: 10,
2114 \ maxheight: 3,
2115 \ pos : 'topleft',
2116 \ col : a:col,
2117 \ line : a:line,
2118 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002119 \ })
2120 endfunc
2121 call PopupMenu(['x'], 1, 1)
2122 call PopupMenu(['123456789|'], 1, 16)
2123 call PopupMenu(['123456789|' .. ' '], 7, 1)
2124 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2125 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2126 END
2127 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002128 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002129 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2130
2131 " close the menu popupwin.
2132 call term_sendkeys(buf, " ")
2133 call term_sendkeys(buf, " ")
2134 call term_sendkeys(buf, " ")
2135 call term_sendkeys(buf, " ")
2136 call term_sendkeys(buf, " ")
2137
2138 " clean up
2139 call StopVimInTerminal(buf)
2140 call delete('XtestPopupMenuMaxWidth')
2141endfunc
2142
Bram Moolenaara901a372019-07-13 16:38:50 +02002143func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002144 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002145
2146 let lines =<< trim END
2147 call setline(1, range(1, 20))
2148 hi ScrollThumb ctermbg=blue
2149 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002150 eval ['one', 'two', 'three', 'four', 'five',
2151 \ 'six', 'seven', 'eight', 'nine']
2152 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002153 \ minwidth: 8,
2154 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002155 \ })
2156 END
2157 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002158 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002159
2160 call term_sendkeys(buf, "j")
2161 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2162
2163 call term_sendkeys(buf, "jjj")
2164 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2165
2166 " if the cursor is the bottom line, it stays at the bottom line.
2167 call term_sendkeys(buf, repeat("j", 20))
2168 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2169
2170 call term_sendkeys(buf, "kk")
2171 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2172
2173 call term_sendkeys(buf, "k")
2174 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2175
2176 " if the cursor is in the top line, it stays in the top line.
2177 call term_sendkeys(buf, repeat("k", 20))
2178 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2179
2180 " close the menu popupwin.
2181 call term_sendkeys(buf, " ")
2182
2183 " clean up
2184 call StopVimInTerminal(buf)
2185 call delete('XtestPopupMenuScroll')
2186endfunc
2187
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002188func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002189 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002190
2191 let lines =<< trim END
2192 function! MyFilter(winid, key) abort
2193 if a:key == "0"
2194 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2195 return 1
2196 endif
2197 if a:key == "G"
2198 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2199 return 1
2200 endif
2201 if a:key == "j"
2202 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2203 return 1
2204 endif
2205 if a:key == "k"
2206 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2207 return 1
2208 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002209 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002210 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002211 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002212 endif
2213 return 0
2214 endfunction
2215 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2216 \ maxheight : 3,
2217 \ filter : 'MyFilter'
2218 \ })
2219 END
2220 call writefile(lines, 'XtestPopupMenuFilter')
2221 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2222
2223 call term_sendkeys(buf, "j")
2224 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2225
2226 call term_sendkeys(buf, "k")
2227 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2228
2229 call term_sendkeys(buf, "G")
2230 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2231
2232 call term_sendkeys(buf, "0")
2233 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2234
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002235 " check that when the popup is closed in the filter the screen is redrawn
2236 call term_sendkeys(buf, ":")
2237 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2238 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002239
2240 " clean up
2241 call StopVimInTerminal(buf)
2242 call delete('XtestPopupMenuFilter')
2243endfunc
2244
2245func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002246 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002247
2248 let winid = popup_create('some text', {})
2249 call assert_equal(0, popup_getoptions(winid).cursorline)
2250 call popup_close(winid)
2251
2252 let winid = popup_create('some text', #{ cursorline: 1, })
2253 call assert_equal(1, popup_getoptions(winid).cursorline)
2254 call popup_close(winid)
2255
2256 let winid = popup_create('some text', #{ cursorline: 0, })
2257 call assert_equal(0, popup_getoptions(winid).cursorline)
2258 call popup_close(winid)
2259
2260 let winid = popup_menu('some text', {})
2261 call assert_equal(1, popup_getoptions(winid).cursorline)
2262 call popup_close(winid)
2263
2264 let winid = popup_menu('some text', #{ cursorline: 1, })
2265 call assert_equal(1, popup_getoptions(winid).cursorline)
2266 call popup_close(winid)
2267
2268 let winid = popup_menu('some text', #{ cursorline: 0, })
2269 call assert_equal(0, popup_getoptions(winid).cursorline)
2270 call popup_close(winid)
2271
2272 " ---------
2273 " Pattern 1
2274 " ---------
2275 let lines =<< trim END
2276 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2277 END
2278 call writefile(lines, 'XtestPopupCursorLine')
2279 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2280 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2281 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2282 call StopVimInTerminal(buf)
2283
2284 " ---------
2285 " Pattern 2
2286 " ---------
2287 let lines =<< trim END
2288 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2289 END
2290 call writefile(lines, 'XtestPopupCursorLine')
2291 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2292 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2293 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2294 call StopVimInTerminal(buf)
2295
2296 " ---------
2297 " Pattern 3
2298 " ---------
2299 let lines =<< trim END
2300 function! MyFilter(winid, key) abort
2301 if a:key == "j"
2302 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2303 return 1
2304 endif
2305 if a:key == 'x'
2306 call popup_close(a:winid)
2307 return 1
2308 endif
2309 return 0
2310 endfunction
2311 call popup_menu(['111', '222', '333'], #{
2312 \ cursorline : 0,
2313 \ maxheight : 2,
2314 \ filter : 'MyFilter',
2315 \ })
2316 END
2317 call writefile(lines, 'XtestPopupCursorLine')
2318 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2319 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2320 call term_sendkeys(buf, "j")
2321 call term_sendkeys(buf, "j")
2322 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2323 call term_sendkeys(buf, "x")
2324 call StopVimInTerminal(buf)
2325
2326 " ---------
2327 " Pattern 4
2328 " ---------
2329 let lines =<< trim END
2330 function! MyFilter(winid, key) abort
2331 if a:key == "j"
2332 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2333 return 1
2334 endif
2335 if a:key == 'x'
2336 call popup_close(a:winid)
2337 return 1
2338 endif
2339 return 0
2340 endfunction
2341 call popup_menu(['111', '222', '333'], #{
2342 \ cursorline : 1,
2343 \ maxheight : 2,
2344 \ filter : 'MyFilter',
2345 \ })
2346 END
2347 call writefile(lines, 'XtestPopupCursorLine')
2348 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2349 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2350 call term_sendkeys(buf, "j")
2351 call term_sendkeys(buf, "j")
2352 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2353 call term_sendkeys(buf, "x")
2354 call StopVimInTerminal(buf)
2355
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002356 " ---------
2357 " Cursor in second line when creating the popup
2358 " ---------
2359 let lines =<< trim END
2360 let winid = popup_create(['111', '222', '333'], #{
2361 \ cursorline : 1,
2362 \ })
2363 call win_execute(winid, "2")
2364 END
2365 call writefile(lines, 'XtestPopupCursorLine')
2366 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2367 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2368 call StopVimInTerminal(buf)
2369
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002370 call delete('XtestPopupCursorLine')
2371endfunc
2372
Bram Moolenaarf914a332019-07-20 15:09:56 +02002373func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002374 CheckScreendump
2375
Bram Moolenaarf914a332019-07-20 15:09:56 +02002376 call writefile([
2377 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2378 \ "another\tXtagfile\t/^this is another",
2379 \ "theword\tXtagfile\t/^theword"],
2380 \ 'Xtags')
2381 call writefile(range(1,20)
2382 \ + ['theword is here']
2383 \ + range(22, 27)
2384 \ + ['this is another place']
2385 \ + range(29, 40),
2386 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002387 call writefile(range(1,10)
2388 \ + ['searched word is here']
2389 \ + range(12, 20),
2390 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002391 let lines =<< trim END
2392 set tags=Xtags
2393 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002394 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002395 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002396 \ 'three',
2397 \ 'four',
2398 \ 'five',
2399 \ 'six',
2400 \ 'seven',
2401 \ 'find theword somewhere',
2402 \ 'nine',
2403 \ 'this is another word',
2404 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002405 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002406 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002407 END
2408 call writefile(lines, 'XtestPreviewPopup')
2409 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2410
2411 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2412 call term_sendkeys(buf, ":\<CR>")
2413 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2414
2415 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2416 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2417
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002418 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002419 call term_sendkeys(buf, ":\<CR>")
2420 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2421
2422 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2423 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2424
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002425 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2426 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002427 call term_sendkeys(buf, ":cd testdir\<CR>")
2428
2429 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002430 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002431 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2432
2433 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2434 call term_sendkeys(buf, ":\<CR>")
2435 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2436
2437 call term_sendkeys(buf, ":pclose\<CR>")
2438 call term_sendkeys(buf, ":psearch searched\<CR>")
2439 call term_sendkeys(buf, ":\<CR>")
2440 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002441
Bram Moolenaarf914a332019-07-20 15:09:56 +02002442 call StopVimInTerminal(buf)
2443 call delete('Xtags')
2444 call delete('Xtagfile')
2445 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002446 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002447endfunc
2448
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002449func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002450 let lines =<< trim END
2451 set completeopt+=preview,popup
2452 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002453 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002454
2455 func CompleteFuncDict(findstart, base)
2456 if a:findstart
2457 if col('.') > 10
2458 return col('.') - 10
2459 endif
2460 return 0
2461 endif
2462
2463 return {
2464 \ 'words': [
2465 \ {
2466 \ 'word': 'aword',
2467 \ 'abbr': 'wrd',
2468 \ 'menu': 'extra text',
2469 \ 'info': 'words are cool',
2470 \ 'kind': 'W',
2471 \ 'user_data': 'test'
2472 \ },
2473 \ {
2474 \ 'word': 'anotherword',
2475 \ 'abbr': 'anotwrd',
2476 \ 'menu': 'extra text',
2477 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2478 \ 'kind': 'W',
2479 \ 'user_data': 'notest'
2480 \ },
2481 \ {
2482 \ 'word': 'noinfo',
2483 \ 'abbr': 'noawrd',
2484 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002485 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002486 \ 'kind': 'W',
2487 \ 'user_data': 'notest'
2488 \ },
2489 \ {
2490 \ 'word': 'thatword',
2491 \ 'abbr': 'thatwrd',
2492 \ 'menu': 'extra text',
2493 \ 'info': 'that word is cool',
2494 \ 'kind': 'W',
2495 \ 'user_data': 'notest'
2496 \ },
2497 \ ]
2498 \ }
2499 endfunc
2500 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002501 func ChangeColor()
2502 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002503 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002504 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002505
2506 func InfoHidden()
2507 set completepopup=height:4,border:off,align:menu
2508 set completeopt-=popup completeopt+=popuphidden
2509 au CompleteChanged * call HandleChange()
2510 endfunc
2511
2512 let s:counter = 0
2513 func HandleChange()
2514 let s:counter += 1
2515 let selected = complete_info(['selected']).selected
2516 if selected <= 0
2517 " First time: do nothing, info remains hidden
2518 return
2519 endif
2520 if selected == 1
2521 " Second time: show info right away
2522 let id = popup_findinfo()
2523 if id
2524 call popup_settext(id, 'immediate info ' .. s:counter)
2525 call popup_show(id)
2526 endif
2527 else
2528 " Third time: show info after a short delay
2529 call timer_start(100, 'ShowInfo')
2530 endif
2531 endfunc
2532
2533 func ShowInfo(...)
2534 let id = popup_findinfo()
2535 if id
2536 call popup_settext(id, 'async info ' .. s:counter)
2537 call popup_show(id)
2538 endif
2539 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002540 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002541 return lines
2542endfunc
2543
2544func Test_popupmenu_info_border()
2545 CheckScreendump
2546
2547 let lines = Get_popupmenu_lines()
2548 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002549 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002550
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002551 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2552 call term_wait(buf, 50)
2553
2554 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2555 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2556
2557 call term_sendkeys(buf, "\<C-N>")
2558 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2559
2560 call term_sendkeys(buf, "\<C-N>")
2561 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2562
2563 call term_sendkeys(buf, "\<C-N>\<C-N>")
2564 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2565
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002566 " info on the left with scrollbar
2567 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2568 call term_sendkeys(buf, "\<C-N>\<C-N>")
2569 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2570
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002571 call StopVimInTerminal(buf)
2572 call delete('XtestInfoPopup')
2573endfunc
2574
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002575func Test_popupmenu_info_noborder()
2576 CheckScreendump
2577
2578 let lines = Get_popupmenu_lines()
2579 call add(lines, 'set completepopup=height:4,border:off')
2580 call writefile(lines, 'XtestInfoPopupNb')
2581
2582 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2583 call term_wait(buf, 50)
2584
2585 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2586 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2587
2588 call StopVimInTerminal(buf)
2589 call delete('XtestInfoPopupNb')
2590endfunc
2591
Bram Moolenaar258cef52019-08-21 17:29:29 +02002592func Test_popupmenu_info_align_menu()
2593 CheckScreendump
2594
2595 let lines = Get_popupmenu_lines()
2596 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2597 call writefile(lines, 'XtestInfoPopupNb')
2598
2599 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2600 call term_wait(buf, 50)
2601
2602 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2603 call term_sendkeys(buf, "\<C-N>")
2604 call term_sendkeys(buf, "\<C-N>")
2605 call term_sendkeys(buf, "\<C-N>")
2606 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2607
2608 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2609 call term_sendkeys(buf, "\<C-N>")
2610 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2611
2612 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002613 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002614 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2615 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2616 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2617
2618 call StopVimInTerminal(buf)
2619 call delete('XtestInfoPopupNb')
2620endfunc
2621
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002622func Test_popupmenu_info_hidden()
2623 CheckScreendump
2624
2625 let lines = Get_popupmenu_lines()
2626 call add(lines, 'call InfoHidden()')
2627 call writefile(lines, 'XtestInfoPopupHidden')
2628
2629 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
2630 call term_wait(buf, 50)
2631
2632 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2633 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
2634
2635 call term_sendkeys(buf, "\<C-N>")
2636 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
2637
2638 call term_sendkeys(buf, "\<C-N>")
2639 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
2640
2641 call term_sendkeys(buf, "\<Esc>")
2642 call StopVimInTerminal(buf)
2643 call delete('XtestInfoPopupHidden')
2644endfunc
2645
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002646func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02002647 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002648 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002649 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002650 call assert_equal(bufnr, winbufnr(winid))
2651 call popup_clear()
2652endfunc
2653
Bram Moolenaar1824f452019-10-02 23:06:46 +02002654func Test_popupwin_getoptions_tablocal()
2655 topleft split
2656 let win1 = popup_create('nothing', #{maxheight: 8})
2657 let win2 = popup_create('something', #{maxheight: 10})
2658 let win3 = popup_create('something', #{maxheight: 15})
2659 call assert_equal(8, popup_getoptions(win1).maxheight)
2660 call assert_equal(10, popup_getoptions(win2).maxheight)
2661 call assert_equal(15, popup_getoptions(win3).maxheight)
2662 call popup_clear()
2663 quit
2664endfunc
2665
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002666func Test_popupwin_cancel()
2667 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
2668 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
2669 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
2670 call assert_equal(5, popup_getpos(win1).line)
2671 call assert_equal(10, popup_getpos(win2).line)
2672 call assert_equal(15, popup_getpos(win3).line)
2673 " TODO: this also works without patch 8.1.2110
2674 call feedkeys("\<C-C>", 'xt')
2675 call assert_equal(5, popup_getpos(win1).line)
2676 call assert_equal(10, popup_getpos(win2).line)
2677 call assert_equal({}, popup_getpos(win3))
2678 call feedkeys("\<C-C>", 'xt')
2679 call assert_equal(5, popup_getpos(win1).line)
2680 call assert_equal({}, popup_getpos(win2))
2681 call assert_equal({}, popup_getpos(win3))
2682 call feedkeys("\<C-C>", 'xt')
2683 call assert_equal({}, popup_getpos(win1))
2684 call assert_equal({}, popup_getpos(win2))
2685 call assert_equal({}, popup_getpos(win3))
2686endfunc
2687
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002688" vim: shiftwidth=2 sts=2