blob: ee188e39c3f92b1b89b060dd5f7f2719136a6a76 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
3if !has('textprop')
4 finish
5endif
6
7source screendump.vim
8
9func Test_simple_popup()
10 if !CanRunVimInTerminal()
11 return
12 endif
13 call writefile([
14 \ "call setline(1, range(1, 100))",
Bram Moolenaar20c023a2019-05-26 21:03:24 +020015 \ "hi PopupColor1 ctermbg=lightblue",
16 \ "hi PopupColor2 ctermbg=lightcyan",
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020017 \ "hi Comment ctermfg=red",
18 \ "call prop_type_add('comment', {'highlight': 'Comment'})",
Bram Moolenaar60cdb302019-05-27 21:54:10 +020019 \ "let winid = popup_create('hello there', {'line': 3, 'col': 11, 'minwidth': 20, 'highlight': 'PopupColor1'})",
20 \ "let winid2 = popup_create(['another one', 'another two', 'another three'], {'line': 3, 'col': 25, 'minwidth': 20})",
Bram Moolenaar20c023a2019-05-26 21:03:24 +020021 \ "call setwinvar(winid2, '&wincolor', 'PopupColor2')",
Bram Moolenaar4d784b22019-05-25 19:51:39 +020022 \], 'XtestPopup')
23 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
24 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020029 \ .. "{'text': 'other tab'},"
30 \ .. "{'text': 'a comment line', 'props': [{"
Bram Moolenaar60cdb302019-05-27 21:54:10 +020031 \ .. "'col': 3, 'length': 7, 'minwidth': 20, 'type': 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar60cdb302019-05-27 21:54:10 +020033 \ .. "], {'line': 4, 'col': 9, 'minwidth': 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
35
36 " switch back to first tabpage
37 call term_sendkeys(buf, "gt")
38 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
39
40 " close that tabpage
41 call term_sendkeys(buf, ":quit!\<CR>")
42 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
43
Bram Moolenaar17146962019-05-30 00:12:11 +020044 " resize popup, show empty line at bottom
Bram Moolenaar60cdb302019-05-27 21:54:10 +020045 call term_sendkeys(buf, ":call popup_move(popupwin, {'minwidth': 15, 'maxwidth': 25, 'minheight': 3, 'maxheight': 5})\<CR>")
46 call term_sendkeys(buf, ":redraw\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
48
Bram Moolenaar17146962019-05-30 00:12:11 +020049 " show not fitting line at bottom
50 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
51 call term_sendkeys(buf, ":redraw\<CR>")
52 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
53
Bram Moolenaar4d784b22019-05-25 19:51:39 +020054 " clean up
55 call StopVimInTerminal(buf)
56 call delete('XtestPopup')
57endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020058
Bram Moolenaarb4230122019-05-30 18:40:53 +020059func Test_popup_with_syntax_win_execute()
60 if !CanRunVimInTerminal()
61 return
62 endif
63 call writefile([
64 \ "call setline(1, range(1, 100))",
65 \ "hi PopupColor ctermbg=lightblue",
66 \ "let winid = popup_create([",
67 \ "\\ '#include <stdio.h>',",
68 \ "\\ 'int main(void)',",
69 \ "\\ '{',",
70 \ "\\ ' printf(123);',",
71 \ "\\ '}',",
72 \ "\\], {'line': 3, 'col': 25, 'highlight': 'PopupColor'})",
73 \ "call win_execute(winid, 'set syntax=cpp')",
74 \], 'XtestPopup')
75 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
76 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
77
78 " clean up
79 call StopVimInTerminal(buf)
80 call delete('XtestPopup')
81endfunc
82
83func Test_popup_with_syntax_setbufvar()
84 if !CanRunVimInTerminal()
85 return
86 endif
87 call writefile([
88 \ "call setline(1, range(1, 100))",
89 \ "hi PopupColor ctermbg=lightgrey",
90 \ "let winid = popup_create([",
91 \ "\\ '#include <stdio.h>',",
92 \ "\\ 'int main(void)',",
93 \ "\\ '{',",
94 \ "\\ ' printf(567);',",
95 \ "\\ '}',",
96 \ "\\], {'line': 3, 'col': 21, 'highlight': 'PopupColor'})",
97 \ "call setbufvar(winbufnr(winid), '&syntax', 'cpp')",
98 \], 'XtestPopup')
99 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
100 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
101
102 " clean up
103 call StopVimInTerminal(buf)
104 call delete('XtestPopup')
105endfunc
106
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200107func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200108 if !has('timers')
109 return
110 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200111 topleft vnew
112 call setline(1, 'hello')
113
114 call popup_create('world', {
115 \ 'line': 1,
116 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200117 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200118 \ 'time': 500,
119 \})
120 redraw
121 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
122 call assert_equal('world', line)
123
124 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200125 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200126 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
127 call assert_equal('hello', line)
128
129 call popup_create('on the command line', {
130 \ 'line': &lines,
131 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200132 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200133 \ 'time': 500,
134 \})
135 redraw
136 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
137 call assert_match('.*on the command line.*', line)
138
139 sleep 700m
140 redraw
141 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
142 call assert_notmatch('.*on the command line.*', line)
143
144 bwipe!
145endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200146
147func Test_popup_hide()
148 topleft vnew
149 call setline(1, 'hello')
150
151 let winid = popup_create('world', {
152 \ 'line': 1,
153 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200154 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200155 \})
156 redraw
157 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
158 call assert_equal('world', line)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200159 call assert_equal(1, popup_getposition(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200160
161 call popup_hide(winid)
162 redraw
163 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
164 call assert_equal('hello', line)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200165 call assert_equal(0, popup_getposition(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200166
167 call popup_show(winid)
168 redraw
169 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
170 call assert_equal('world', line)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200171 call assert_equal(1, popup_getposition(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200172
173
174 call popup_close(winid)
175 redraw
176 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
177 call assert_equal('hello', line)
178
179 " error is given for existing non-popup window
180 call assert_fails('call popup_hide(win_getid())', 'E993:')
181
182 " no error non-existing window
183 call popup_hide(1234234)
184 call popup_show(41234234)
185
186 bwipe!
187endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200188
189func Test_popup_move()
190 topleft vnew
191 call setline(1, 'hello')
192
193 let winid = popup_create('world', {
194 \ 'line': 1,
195 \ 'col': 1,
196 \ 'minwidth': 20,
197 \})
198 redraw
199 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
200 call assert_equal('world ', line)
201
202 call popup_move(winid, {'line': 2, 'col': 2})
203 redraw
204 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
205 call assert_equal('hello ', line)
206 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
207 call assert_equal('~world', line)
208
209 call popup_move(winid, {'line': 1})
210 redraw
211 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
212 call assert_equal('hworld', line)
213
214 call popup_close(winid)
215
216 bwipe!
217endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200218
219func Test_popup_getposition()
220 let winid = popup_create('hello', {
221 \ 'line': 2,
222 \ 'col': 3,
223 \ 'minwidth': 10,
224 \ 'minheight': 11,
225 \})
226 redraw
227 let res = popup_getposition(winid)
228 call assert_equal(2, res.line)
229 call assert_equal(3, res.col)
230 call assert_equal(10, res.width)
231 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200232 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200233
234 call popup_close(winid)
235endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200236
237func Test_popup_width_longest()
238 let tests = [
239 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
240 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
241 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
242 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
243 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
244 \ ]
245
246 for test in tests
247 let winid = popup_create(test[0], {'line': 2, 'col': 3})
248 redraw
249 let position = popup_getposition(winid)
250 call assert_equal(test[1], position.width)
251 call popup_close(winid)
252 endfor
253endfunc
254
255func Test_popup_wraps()
256 let tests = [
257 \ ['nowrap', 6, 1],
258 \ ['a line that wraps once', 12, 2],
259 \ ['a line that wraps two times', 12, 3],
260 \ ]
261 for test in tests
262 let winid = popup_create(test[0],
263 \ {'line': 2, 'col': 3, 'maxwidth': 12})
264 redraw
265 let position = popup_getposition(winid)
266 call assert_equal(test[1], position.width)
267 call assert_equal(test[2], position.height)
268
269 call popup_close(winid)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200270 call assert_equal({}, popup_getposition(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200271 endfor
272endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200273
274func Test_popup_getoptions()
275 let winid = popup_create('hello', {
276 \ 'line': 2,
277 \ 'col': 3,
278 \ 'minwidth': 10,
279 \ 'minheight': 11,
280 \ 'maxwidth': 20,
281 \ 'maxheight': 21,
282 \ 'zindex': 100,
283 \ 'time': 5000,
284 \})
285 redraw
286 let res = popup_getoptions(winid)
287 call assert_equal(2, res.line)
288 call assert_equal(3, res.col)
289 call assert_equal(10, res.minwidth)
290 call assert_equal(11, res.minheight)
291 call assert_equal(20, res.maxwidth)
292 call assert_equal(21, res.maxheight)
293 call assert_equal(100, res.zindex)
294 if has('timers')
295 call assert_equal(5000, res.time)
296 endif
297 call popup_close(winid)
298
299 let winid = popup_create('hello', {})
300 redraw
301 let res = popup_getoptions(winid)
302 call assert_equal(0, res.line)
303 call assert_equal(0, res.col)
304 call assert_equal(0, res.minwidth)
305 call assert_equal(0, res.minheight)
306 call assert_equal(0, res.maxwidth)
307 call assert_equal(0, res.maxheight)
308 call assert_equal(50, res.zindex)
309 if has('timers')
310 call assert_equal(0, res.time)
311 endif
312 call popup_close(winid)
313 call assert_equal({}, popup_getoptions(winid))
314endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200315
316func Test_popup_option_values()
317 new
318 " window-local
319 setlocal number
320 setlocal nowrap
321 " buffer-local
322 setlocal omnifunc=Something
323 " global/buffer-local
324 setlocal path=/there
325 " global/window-local
326 setlocal scrolloff=9
327
328 let winid = popup_create('hello', {})
329 call assert_equal(0, getwinvar(winid, '&number'))
330 call assert_equal(1, getwinvar(winid, '&wrap'))
331 call assert_equal('', getwinvar(winid, '&omnifunc'))
332 call assert_equal(&g:path, getwinvar(winid, '&path'))
333 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
334
335 call popup_close(winid)
336 bwipe
337endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200338
339func Test_popup_atcursor()
340 topleft vnew
341 call setline(1, [
342 \ 'xxxxxxxxxxxxxxxxx',
343 \ 'xxxxxxxxxxxxxxxxx',
344 \ 'xxxxxxxxxxxxxxxxx',
345 \])
346
347 call cursor(2, 2)
348 redraw
349 let winid = popup_atcursor('vim', {})
350 redraw
351 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
352 call assert_equal('xvimxxxxxxxxxxxxx', line)
353 call popup_close(winid)
354
355 call cursor(3, 4)
356 redraw
357 let winid = popup_atcursor('vim', {})
358 redraw
359 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
360 call assert_equal('xxxvimxxxxxxxxxxx', line)
361 call popup_close(winid)
362
363 call cursor(1, 1)
364 redraw
365 let winid = popup_create('vim', {
366 \ 'line': 'cursor+2',
367 \ 'col': 'cursor+1',
368 \})
369 redraw
370 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
371 call assert_equal('xvimxxxxxxxxxxxxx', line)
372 call popup_close(winid)
373
374 call cursor(3, 3)
375 redraw
376 let winid = popup_create('vim', {
377 \ 'line': 'cursor-2',
378 \ 'col': 'cursor-1',
379 \})
380 redraw
381 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
382 call assert_equal('xvimxxxxxxxxxxxxx', line)
383 call popup_close(winid)
384
385 bwipe!
386endfunc