blob: e38b7211e0881717170f7f7f97fb08f580d680aa [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature textprop
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
15 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020016 call prop_type_add('comment', #{highlight: 'Comment'})
17 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
18 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020019 call setwinvar(winid2, '&wincolor', 'PopupColor2')
20 END
21 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020022 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020023 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
24
Bram Moolenaarec583842019-05-26 14:11:23 +020025 " Add a tabpage
26 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020027 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020028 \ .. "#{text: 'other tab'},"
29 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020030 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020031 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020032 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020033 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
34
35 " switch back to first tabpage
36 call term_sendkeys(buf, "gt")
37 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
38
39 " close that tabpage
40 call term_sendkeys(buf, ":quit!\<CR>")
41 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
42
Bram Moolenaar202d9822019-06-11 21:56:30 +020043 " set 'columns' to a small value, size must be recomputed
44 call term_sendkeys(buf, ":let cols = &columns\<CR>")
45 call term_sendkeys(buf, ":set columns=12\<CR>")
46 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
47 call term_sendkeys(buf, ":let &columns = cols\<CR>")
48
Bram Moolenaar17146962019-05-30 00:12:11 +020049 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020050 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020051 call term_sendkeys(buf, ":redraw\<CR>")
52 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
53
Bram Moolenaar17146962019-05-30 00:12:11 +020054 " show not fitting line at bottom
55 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
56 call term_sendkeys(buf, ":redraw\<CR>")
57 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
58
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020059 " move popup over ruler
60 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020061 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020062 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020063 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
64
65 " clear all popups after moving the cursor a bit, so that ruler is updated
66 call term_sendkeys(buf, "axxx\<Esc>")
67 call term_wait(buf)
68 call term_sendkeys(buf, "0")
69 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020070 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020071 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
72
Bram Moolenaar4d784b22019-05-25 19:51:39 +020073 " clean up
74 call StopVimInTerminal(buf)
75 call delete('XtestPopup')
76endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020077
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020078func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020079 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020080
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020081 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020082 let lines =<< trim END
83 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020084 call popup_create('hello border', #{line: 2, col: 3, border: []})
85 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020086 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020087 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
88 call popup_create('paddings', #{line: 6, col: 23, padding: [1, 3, 2, 4]})
89 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
90 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020091 END
92 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
93 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020094 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020095 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
96
97 call StopVimInTerminal(buf)
98 call delete('XtestPopupBorder')
99 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200100
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200101 let lines =<< trim END
102 call setline(1, range(1, 100))
103 hi BlueColor ctermbg=lightblue
104 hi TopColor ctermbg=253
105 hi RightColor ctermbg=245
106 hi BottomColor ctermbg=240
107 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200108 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
109 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
110 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
111 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
112 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
113 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200114 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200115 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200116 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200117 END
118 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200119 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200120 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
121
Bram Moolenaarad24a712019-06-17 20:05:45 +0200122 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200123 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200124 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
125
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200126 " check multi-byte border only with 'ambiwidth' single
127 if &ambiwidth == 'single'
128 call term_sendkeys(buf, ":call MultiByte()\<CR>")
129 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
130 endif
131
Bram Moolenaar790498b2019-06-01 22:15:29 +0200132 call StopVimInTerminal(buf)
133 call delete('XtestPopupBorder')
134
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200135 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200136 \ line: 2,
137 \ core_line: 3,
138 \ col: 3,
139 \ core_col: 4,
140 \ width: 14,
141 \ core_width: 12,
142 \ height: 3,
143 \ core_height: 1,
144 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100145 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200146 \ scrollbar: 0,
147 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200148 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200149 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200150 let options = popup_getoptions(winid)
151 call assert_equal([], options.border)
152 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200153
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200154 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200155 let with_border_or_padding.width = 15
156 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200157 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200158 let options = popup_getoptions(winid)
159 call assert_false(has_key(options, "border"))
160 call assert_equal([], options.padding)
161
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200162 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200163 \ padding: [1, 2, 3, 4],
164 \ border: [4, 0, 7, 8],
165 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
166 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200167 \ })
168 let options = popup_getoptions(winid)
169 call assert_equal([1, 0, 1, 1], options.border)
170 call assert_equal([1, 2, 3, 4], options.padding)
171 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
172 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200173
Bram Moolenaara1396152019-10-20 18:46:05 +0200174 " Check that popup_setoptions() takes the output of popup_getoptions()
175 call popup_setoptions(winid, options)
176 call assert_equal(options, popup_getoptions(winid))
177
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200178 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
179 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200180 \ line: 3,
181 \ core_line: 5,
182 \ col: 8,
183 \ core_col: 10,
184 \ width: 14,
185 \ core_width: 10,
186 \ height: 5,
187 \ scrollbar: 0,
188 \ core_height: 1,
189 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100190 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200191 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200192
193 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200194endfunc
195
Bram Moolenaarb4230122019-05-30 18:40:53 +0200196func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200197 CheckScreendump
198
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200199 let lines =<< trim END
200 call setline(1, range(1, 100))
201 hi PopupColor ctermbg=lightblue
202 let winid = popup_create([
203 \ '#include <stdio.h>',
204 \ 'int main(void)',
205 \ '{',
206 \ ' printf(123);',
207 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200208 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200209 call win_execute(winid, 'set syntax=cpp')
210 END
211 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200212 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200213 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
214
215 " clean up
216 call StopVimInTerminal(buf)
217 call delete('XtestPopup')
218endfunc
219
220func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200221 CheckScreendump
222
Bram Moolenaar402502d2019-05-30 22:07:36 +0200223 let lines =<< trim END
224 call setline(1, range(1, 100))
225 hi PopupColor ctermbg=lightgrey
226 let winid = popup_create([
227 \ '#include <stdio.h>',
228 \ 'int main(void)',
229 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200230 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200231 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200232 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200233 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
234 END
235 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200236 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200237 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
238
239 " clean up
240 call StopVimInTerminal(buf)
241 call delete('XtestPopup')
242endfunc
243
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200244func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200245 CheckScreendump
246
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200247 let lines =<< trim END
248 call setline(1, ['111 222 333', '444 555 666'])
249 let winid = popup_create([
250 \ '111 222 333',
251 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200252 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200253 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100254 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200255 /666
256 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100257 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200258 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100259 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200260 END
261 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200262 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200263 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
264
265 " clean up
266 call StopVimInTerminal(buf)
267 call delete('XtestPopupMatches')
268endfunc
269
Bram Moolenaar399d8982019-06-02 15:34:29 +0200270func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200271 CheckScreendump
272
Bram Moolenaar399d8982019-06-02 15:34:29 +0200273 let lines =<< trim END
274 call setline(1, repeat([repeat('-', 60)], 15))
275 set so=0
276 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200277 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200278 \ line: 'cursor+1',
279 \ col: 'cursor',
280 \ pos: 'topleft',
281 \ border: [],
282 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200283 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200284 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200285 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200286 \ line: 'cursor+1',
287 \ col: 'cursor',
288 \ pos: 'topright',
289 \ border: [],
290 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200291 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200292 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200293 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200294 \ line: 'cursor-1',
295 \ col: 'cursor',
296 \ pos: 'botleft',
297 \ border: [],
298 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200299 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200300 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200301 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200302 \ line: 'cursor-1',
303 \ col: 'cursor',
304 \ pos: 'botright',
305 \ border: [],
306 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200307 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200308 normal 1G51|r*
309 let winid1 = popup_create(['one', 'two'], #{
310 \ line: 'cursor-1',
311 \ col: 'cursor',
312 \ pos: 'botleft',
313 \ border: [],
314 \ padding: [],
315 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200316 END
317 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200318 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200319 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
320
321 " clean up
322 call StopVimInTerminal(buf)
323 call delete('XtestPopupCorners')
324endfunc
325
Bram Moolenaar8d241042019-06-12 23:40:01 +0200326func Test_popup_firstline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200327 CheckScreendump
328
Bram Moolenaar8d241042019-06-12 23:40:01 +0200329 let lines =<< trim END
330 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200331 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200332 \ maxheight: 4,
333 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200334 \ })
335 END
336 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200337 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200338 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
339
340 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
341 call term_sendkeys(buf, ":\<CR>")
342 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200343
344 " clean up
345 call StopVimInTerminal(buf)
346 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200347
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200348 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200349 \ maxheight: 2,
350 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200351 \ })
352 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200353 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200354 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200355 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200356
357 let winid = popup_create(['xxx']->repeat(50), #{
358 \ maxheight: 3,
359 \ firstline: 11,
360 \ })
361 redraw
362 call assert_equal(11, popup_getoptions(winid).firstline)
363 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200364 " check line() works with popup window
365 call assert_equal(11, line('.', winid))
366 call assert_equal(50, line('$', winid))
367 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200368
369 " Normal command changes what is displayed but not "firstline"
370 call win_execute(winid, "normal! \<c-y>")
371 call assert_equal(11, popup_getoptions(winid).firstline)
372 call assert_equal(10, popup_getpos(winid).firstline)
373
374 " Making some property change applies "firstline" again
375 call popup_setoptions(winid, #{line: 4})
376 call assert_equal(11, popup_getoptions(winid).firstline)
377 call assert_equal(11, popup_getpos(winid).firstline)
378
379 " Remove "firstline" property and scroll
380 call popup_setoptions(winid, #{firstline: 0})
381 call win_execute(winid, "normal! \<c-y>")
382 call assert_equal(0, popup_getoptions(winid).firstline)
383 call assert_equal(10, popup_getpos(winid).firstline)
384
385 " Making some property change has no side effect
386 call popup_setoptions(winid, #{line: 3})
387 call assert_equal(0, popup_getoptions(winid).firstline)
388 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaarae943152019-06-16 22:54:14 +0200389
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100390 " CTRL-D scrolls down half a page
391 let winid = popup_create(['xxx']->repeat(50), #{
392 \ maxheight: 8,
393 \ })
394 redraw
395 call assert_equal(1, popup_getpos(winid).firstline)
396 call win_execute(winid, "normal! \<C-D>")
397 call assert_equal(5, popup_getpos(winid).firstline)
398 call win_execute(winid, "normal! \<C-D>")
399 call assert_equal(9, popup_getpos(winid).firstline)
400 call win_execute(winid, "normal! \<C-U>")
401 call assert_equal(5, popup_getpos(winid).firstline)
402
403 call win_execute(winid, "normal! \<C-F>")
404 call assert_equal(11, popup_getpos(winid).firstline)
405 call win_execute(winid, "normal! \<C-B>")
406 call assert_equal(5, popup_getpos(winid).firstline)
407
Bram Moolenaarae943152019-06-16 22:54:14 +0200408 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200409endfunc
410
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200411func Test_popup_noscrolloff()
412 set scrolloff=5
413 let winid = popup_create(['xxx']->repeat(50), #{
414 \ maxheight: 5,
415 \ firstline: 11,
416 \ })
417 redraw
418 call assert_equal(11, popup_getoptions(winid).firstline)
419 call assert_equal(11, popup_getpos(winid).firstline)
420
421 call popup_setoptions(winid, #{firstline: 0})
422 call win_execute(winid, "normal! \<c-y>")
423 call assert_equal(0, popup_getoptions(winid).firstline)
424 call assert_equal(10, popup_getpos(winid).firstline)
425
426 call popup_close(winid)
427endfunc
428
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200429func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200430 CheckScreendump
431
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200432 " create a popup that covers the command line
433 let lines =<< trim END
434 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200435 split
436 vsplit
437 $wincmd w
438 vsplit
439 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200440 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200441 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200442 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200443 \ border: [],
444 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200445 \ })
446 func Dragit()
447 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
448 endfunc
449 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200450 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200451 func Resize()
452 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
453 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200454 map <silent> <F5> :call test_setmouse(6, 21)<CR>
455 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200456 END
457 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200458 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200459 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
460
461 call term_sendkeys(buf, ":call Dragit()\<CR>")
462 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
463
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200464 call term_sendkeys(buf, ":call Resize()\<CR>")
465 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
466
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200467 " clean up
468 call StopVimInTerminal(buf)
469 call delete('XtestPopupDrag')
470endfunc
471
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200472func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200473 CheckScreendump
474
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200475 let lines =<< trim END
476 call setline(1, range(1, 20))
477 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200478 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200479 \ close: 'button',
480 \ border: [],
481 \ line: 1,
482 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200483 \ })
484 func CloseMsg(id, result)
485 echomsg 'Popup closed with ' .. a:result
486 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200487 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200488 \ close: 'click',
489 \ line: 3,
490 \ col: 15,
491 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200492 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200493 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200494 \ close: 'button',
495 \ line: 5,
496 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200497 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200498 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200499 \ close: 'button',
500 \ padding: [],
501 \ line: 5,
502 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200503 \ })
504 func CloseWithX()
505 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
506 endfunc
507 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
508 func CloseWithClick()
509 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
510 endfunc
511 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200512 func CreateWithMenuFilter()
513 let winid = popup_create('barfoo', #{
514 \ close: 'button',
515 \ filter: 'popup_filter_menu',
516 \ border: [],
517 \ line: 1,
518 \ col: 40,
519 \ })
520 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200521 END
522 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200523 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200524 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
525
526 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
527 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
528
529 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
530 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
531
Bram Moolenaarf6396232019-08-24 19:36:00 +0200532 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
533 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
534
535 " We have to send the actual mouse code, feedkeys() would be caught the
536 " filter.
537 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
538 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
539
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200540 " clean up
541 call StopVimInTerminal(buf)
542 call delete('XtestPopupClose')
543endfunction
544
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200545func Test_popup_menu_wrap()
546 CheckScreendump
547
548 let lines =<< trim END
549 call setline(1, range(1, 20))
550 call popup_create([
551 \ 'one',
552 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
553 \ 'three',
554 \ 'four',
555 \ ], #{
556 \ pos: "botleft",
557 \ border: [],
558 \ padding: [0,1,0,1],
559 \ maxheight: 3,
560 \ cursorline: 1,
561 \ filter: 'popup_filter_menu',
562 \ })
563 END
564 call writefile(lines, 'XtestPopupWrap')
565 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
566 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
567
568 call term_sendkeys(buf, "jj")
569 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
570
571 " clean up
572 call term_sendkeys(buf, "\<Esc>")
573 call StopVimInTerminal(buf)
574 call delete('XtestPopupWrap')
575endfunction
576
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200577func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200578 CheckScreendump
579
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200580 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200581 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200582 hi PopupColor ctermbg=lightgrey
583 let winid = popup_create([
584 \ 'some text',
585 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200586 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200587 \ line: 1,
588 \ col: 10,
589 \ wrap: 0,
590 \ fixed: 1,
591 \ zindex: 90,
592 \ padding: [],
593 \ highlight: 'PopupColor',
594 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200595 call popup_create([
596 \ 'xxxxxxxxx',
597 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200598 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200599 \ line: 3,
600 \ col: 18,
601 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200602 let winidb = popup_create([
603 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200604 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200605 \ line: 7,
606 \ col: 10,
607 \ wrap: 0,
608 \ fixed: 1,
609 \ close: 'button',
610 \ zindex: 90,
611 \ padding: [],
612 \ border: [],
613 \ 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 +0200614 END
615 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200616 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200617 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
618
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200619 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
620 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200621 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200622 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
623
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200624 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
625 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200626 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200627 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
628
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200629 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
630 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200631 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200632 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
633
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200634 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
635 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200636 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200637 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
638
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200639 " clean up
640 call StopVimInTerminal(buf)
641 call delete('XtestPopupMask')
642endfunc
643
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200644func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200645 CheckScreendump
646 CheckFeature clipboard_working
647
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200648 " create a popup with some text to be selected
649 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200650 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200651 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200652 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200653 \ drag: 1,
654 \ border: [],
655 \ line: 3,
656 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200657 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200658 \ })
659 func Select1()
660 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
661 endfunc
662 map <silent> <F3> :call test_setmouse(4, 15)<CR>
663 map <silent> <F4> :call test_setmouse(6, 23)<CR>
664 END
665 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200666 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200667 call term_sendkeys(buf, ":call Select1()\<CR>")
668 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
669
670 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
671 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200672 " clean the command line, sometimes it still shows a command
673 call term_sendkeys(buf, ":\<esc>")
674
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200675 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
676
677 " clean up
678 call StopVimInTerminal(buf)
679 call delete('XtestPopupSelect')
680endfunc
681
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200682func Test_popup_in_tab()
683 " default popup is local to tab, not visible when in other tab
684 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200685 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200686 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200687 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200688 tabnew
689 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200690 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200691 quit
692 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200693
694 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200695 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200696 " buffer is gone now
697 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200698
699 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200700 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200701 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200702 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200703 tabnew
704 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200705 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200706 quit
707 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200708 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200709
710 " create popup in other tab
711 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200712 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200713 call assert_equal(0, popup_getpos(winid).visible)
714 call assert_equal(1, popup_getoptions(winid).tabpage)
715 quit
716 call assert_equal(1, popup_getpos(winid).visible)
717 call assert_equal(0, popup_getoptions(winid).tabpage)
718 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200719endfunc
720
721func Test_popup_valid_arguments()
722 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200723 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200724 let pos = popup_getpos(winid)
725 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200726 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200727
728 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200729 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200730 let pos = popup_getpos(winid)
731 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200732 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200733
734 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200735 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200736 let pos = popup_getpos(winid)
737 let around = (&columns - pos.width) / 2
738 call assert_inrange(around - 1, around + 1, pos.col)
739 let around = (&lines - pos.height) / 2
740 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200741 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200742endfunc
743
744func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200745 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200746 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200747 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200748 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200749
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200750 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200751 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200752 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
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", #{col: "cursor+x"})', 'E15:')
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", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200757 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200758
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200759 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200760 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200761 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200762 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200763 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200764 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200765 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200766 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200767
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200768 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200769 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200770 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200771 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200772 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200773 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200774 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200775 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200776 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
777 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200778 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200779 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200780
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200781 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200782 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200783 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200784 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200785 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200786 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200787 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
788 call popup_clear()
789 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200790 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200791 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200792endfunc
793
Bram Moolenaareea16992019-05-31 17:34:48 +0200794func Test_win_execute_closing_curwin()
795 split
796 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200797 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200798 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200799endfunc
800
801func Test_win_execute_not_allowed()
802 let winid = popup_create('some text', {})
803 call assert_fails('call win_execute(winid, "split")', 'E994:')
804 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
805 call assert_fails('call win_execute(winid, "close")', 'E994:')
806 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200807 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200808 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
809 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
810 call assert_fails('call win_execute(winid, "next")', 'E994:')
811 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
812 call assert_fails('call win_execute(winid, "buf")', 'E994:')
813 call assert_fails('call win_execute(winid, "edit")', 'E994:')
814 call assert_fails('call win_execute(winid, "enew")', 'E994:')
815 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
816 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
817 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
818 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200819 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200820endfunc
821
Bram Moolenaar402502d2019-05-30 22:07:36 +0200822func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200823 CheckScreendump
824
Bram Moolenaar402502d2019-05-30 22:07:36 +0200825 let lines =<< trim END
826 call setline(1, range(1, 100))
827 let winid = popup_create(
828 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200829 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200830 END
831 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200832 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200833 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
834
835 " clean up
836 call StopVimInTerminal(buf)
837 call delete('XtestPopup')
838endfunc
839
840func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200841 CheckScreendump
842
Bram Moolenaar402502d2019-05-30 22:07:36 +0200843 let lines =<< trim END
844 call setline(1, range(1, 100))
845 let winid = popup_create(
846 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200847 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200848 END
849 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200850 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200851 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
852
853 " clean up
854 call StopVimInTerminal(buf)
855 call delete('XtestPopup')
856endfunc
857
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200858func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200859 CheckScreendump
860
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200861 let lines =<< trim END
862 set showbreak=>>\
863 call setline(1, range(1, 20))
864 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +0200865 \ 'a long line here that wraps',
866 \ #{filter: 'popup_filter_yesno',
867 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200868 END
869 call writefile(lines, 'XtestPopupShowbreak')
870 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
871 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
872
873 " clean up
874 call term_sendkeys(buf, "y")
875 call StopVimInTerminal(buf)
876 call delete('XtestPopupShowbreak')
877endfunc
878
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200879func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200880 CheckFeature timers
881
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200882 topleft vnew
883 call setline(1, 'hello')
884
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200885 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200886 \ line: 1,
887 \ col: 1,
888 \ minwidth: 20,
889 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200890 \})
891 redraw
892 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
893 call assert_equal('world', line)
894
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200895 call assert_equal(winid, popup_locate(1, 1))
896 call assert_equal(winid, popup_locate(1, 20))
897 call assert_equal(0, popup_locate(1, 21))
898 call assert_equal(0, popup_locate(2, 1))
899
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200900 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200901 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +0200902 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200903 call assert_equal('hello', line)
904
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200905 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200906 \ line: &lines,
907 \ col: 10,
908 \ minwidth: 20,
909 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200910 \})
911 redraw
912 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
913 call assert_match('.*on the command line.*', line)
914
915 sleep 700m
916 redraw
917 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
918 call assert_notmatch('.*on the command line.*', line)
919
920 bwipe!
921endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200922
923func Test_popup_hide()
924 topleft vnew
925 call setline(1, 'hello')
926
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200927 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200928 \ line: 1,
929 \ col: 1,
930 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200931 \})
932 redraw
933 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
934 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200935 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200936 " buffer is still listed and active
937 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200938
939 call popup_hide(winid)
940 redraw
941 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
942 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200943 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200944 " buffer is still listed but hidden
945 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200946
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200947 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200948 redraw
949 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
950 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200951 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200952
953
954 call popup_close(winid)
955 redraw
956 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
957 call assert_equal('hello', line)
958
959 " error is given for existing non-popup window
960 call assert_fails('call popup_hide(win_getid())', 'E993:')
961
962 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200963 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200964 call popup_show(41234234)
965
966 bwipe!
967endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200968
969func Test_popup_move()
970 topleft vnew
971 call setline(1, 'hello')
972
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200973 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200974 \ line: 1,
975 \ col: 1,
976 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200977 \})
978 redraw
979 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
980 call assert_equal('world ', line)
981
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200982 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200983 redraw
984 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
985 call assert_equal('hello ', line)
986 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
987 call assert_equal('~world', line)
988
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200989 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200990 redraw
991 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
992 call assert_equal('hworld', line)
993
994 call popup_close(winid)
995
996 bwipe!
997endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200998
Bram Moolenaar402502d2019-05-30 22:07:36 +0200999func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001000 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001001 \ line: 2,
1002 \ col: 3,
1003 \ minwidth: 10,
1004 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001005 \})
1006 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001007 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001008 call assert_equal(2, res.line)
1009 call assert_equal(3, res.col)
1010 call assert_equal(10, res.width)
1011 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001012 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001013
1014 call popup_close(winid)
1015endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001016
1017func Test_popup_width_longest()
1018 let tests = [
1019 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1020 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1021 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1022 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1023 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1024 \ ]
1025
1026 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001027 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001028 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001029 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001030 call assert_equal(test[1], position.width)
1031 call popup_close(winid)
1032 endfor
1033endfunc
1034
1035func Test_popup_wraps()
1036 let tests = [
1037 \ ['nowrap', 6, 1],
1038 \ ['a line that wraps once', 12, 2],
1039 \ ['a line that wraps two times', 12, 3],
1040 \ ]
1041 for test in tests
1042 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001043 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001044 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001045 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001046 call assert_equal(test[1], position.width)
1047 call assert_equal(test[2], position.height)
1048
1049 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001050 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001051 endfor
1052endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001053
1054func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001055 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001056 \ line: 2,
1057 \ col: 3,
1058 \ minwidth: 10,
1059 \ minheight: 11,
1060 \ maxwidth: 20,
1061 \ maxheight: 21,
1062 \ zindex: 100,
1063 \ time: 5000,
1064 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001065 \})
1066 redraw
1067 let res = popup_getoptions(winid)
1068 call assert_equal(2, res.line)
1069 call assert_equal(3, res.col)
1070 call assert_equal(10, res.minwidth)
1071 call assert_equal(11, res.minheight)
1072 call assert_equal(20, res.maxwidth)
1073 call assert_equal(21, res.maxheight)
1074 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001075 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001076 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001077 if has('timers')
1078 call assert_equal(5000, res.time)
1079 endif
1080 call popup_close(winid)
1081
1082 let winid = popup_create('hello', {})
1083 redraw
1084 let res = popup_getoptions(winid)
1085 call assert_equal(0, res.line)
1086 call assert_equal(0, res.col)
1087 call assert_equal(0, res.minwidth)
1088 call assert_equal(0, res.minheight)
1089 call assert_equal(0, res.maxwidth)
1090 call assert_equal(0, res.maxheight)
1091 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001092 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001093 if has('timers')
1094 call assert_equal(0, res.time)
1095 endif
1096 call popup_close(winid)
1097 call assert_equal({}, popup_getoptions(winid))
1098endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001099
1100func Test_popup_option_values()
1101 new
1102 " window-local
1103 setlocal number
1104 setlocal nowrap
1105 " buffer-local
1106 setlocal omnifunc=Something
1107 " global/buffer-local
1108 setlocal path=/there
1109 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001110 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001111
1112 let winid = popup_create('hello', {})
1113 call assert_equal(0, getwinvar(winid, '&number'))
1114 call assert_equal(1, getwinvar(winid, '&wrap'))
1115 call assert_equal('', getwinvar(winid, '&omnifunc'))
1116 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001117 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1118
1119 " 'scrolloff' is reset to zero
1120 call assert_equal(5, &scrolloff)
1121 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001122
1123 call popup_close(winid)
1124 bwipe
1125endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001126
1127func Test_popup_atcursor()
1128 topleft vnew
1129 call setline(1, [
1130 \ 'xxxxxxxxxxxxxxxxx',
1131 \ 'xxxxxxxxxxxxxxxxx',
1132 \ 'xxxxxxxxxxxxxxxxx',
1133 \])
1134
1135 call cursor(2, 2)
1136 redraw
1137 let winid = popup_atcursor('vim', {})
1138 redraw
1139 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1140 call assert_equal('xvimxxxxxxxxxxxxx', line)
1141 call popup_close(winid)
1142
1143 call cursor(3, 4)
1144 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001145 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001146 redraw
1147 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1148 call assert_equal('xxxvimxxxxxxxxxxx', line)
1149 call popup_close(winid)
1150
1151 call cursor(1, 1)
1152 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001153 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001154 \ line: 'cursor+2',
1155 \ col: 'cursor+1',
1156 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001157 redraw
1158 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1159 call assert_equal('xvimxxxxxxxxxxxxx', line)
1160 call popup_close(winid)
1161
1162 call cursor(3, 3)
1163 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001164 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001165 \ line: 'cursor-2',
1166 \ col: 'cursor-1',
1167 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001168 redraw
1169 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1170 call assert_equal('xvimxxxxxxxxxxxxx', line)
1171 call popup_close(winid)
1172
Bram Moolenaar402502d2019-05-30 22:07:36 +02001173 " just enough room above
1174 call cursor(3, 3)
1175 redraw
1176 let winid = popup_atcursor(['vim', 'is great'], {})
1177 redraw
1178 let pos = popup_getpos(winid)
1179 call assert_equal(1, pos.line)
1180 call popup_close(winid)
1181
1182 " not enough room above, popup goes below the cursor
1183 call cursor(3, 3)
1184 redraw
1185 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1186 redraw
1187 let pos = popup_getpos(winid)
1188 call assert_equal(4, pos.line)
1189 call popup_close(winid)
1190
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001191 " cursor in first line, popup in line 2
1192 call cursor(1, 1)
1193 redraw
1194 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1195 redraw
1196 let pos = popup_getpos(winid)
1197 call assert_equal(2, pos.line)
1198 call popup_close(winid)
1199
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001200 bwipe!
1201endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001202
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001203func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001204 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001205 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001206
1207 let lines =<< trim END
1208 call setline(1, range(1, 20))
1209 call setline(5, 'here is some text to hover over')
1210 set balloonevalterm
1211 set balloonexpr=BalloonExpr()
1212 set balloondelay=100
1213 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001214 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001215 return ''
1216 endfunc
1217 func Hover()
1218 call test_setmouse(5, 15)
1219 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1220 sleep 100m
1221 endfunc
1222 func MoveOntoPopup()
1223 call test_setmouse(4, 17)
1224 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1225 endfunc
1226 func MoveAway()
1227 call test_setmouse(5, 13)
1228 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1229 endfunc
1230 END
1231 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001232 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001233 call term_wait(buf, 100)
1234 call term_sendkeys(buf, 'j')
1235 call term_sendkeys(buf, ":call Hover()\<CR>")
1236 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1237
1238 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1239 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1240
1241 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1242 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1243
1244 " clean up
1245 call StopVimInTerminal(buf)
1246 call delete('XtestPopupBeval')
1247endfunc
1248
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001249func Test_popup_filter()
1250 new
1251 call setline(1, 'some text')
1252
1253 func MyPopupFilter(winid, c)
1254 if a:c == 'e'
1255 let g:eaten = 'e'
1256 return 1
1257 endif
1258 if a:c == '0'
1259 let g:ignored = '0'
1260 return 0
1261 endif
1262 if a:c == 'x'
1263 call popup_close(a:winid)
1264 return 1
1265 endif
1266 return 0
1267 endfunc
1268
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001269 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001270 redraw
1271
1272 " e is consumed by the filter
1273 call feedkeys('e', 'xt')
1274 call assert_equal('e', g:eaten)
1275
1276 " 0 is ignored by the filter
1277 normal $
1278 call assert_equal(9, getcurpos()[2])
1279 call feedkeys('0', 'xt')
1280 call assert_equal('0', g:ignored)
1281 call assert_equal(1, getcurpos()[2])
1282
1283 " x closes the popup
1284 call feedkeys('x', 'xt')
1285 call assert_equal('e', g:eaten)
1286 call assert_equal(-1, winbufnr(winid))
1287
1288 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001289 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001290endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001291
Bram Moolenaara42d9452019-06-15 21:46:30 +02001292func ShowDialog(key, result)
1293 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001294 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001295 \ filter: 'popup_filter_yesno',
1296 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +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_dialog()
1305 func QuitCallback(id, res)
1306 let s:cb_winid = a:id
1307 let s:cb_res = a:res
1308 endfunc
1309
1310 let winid = ShowDialog("y", 1)
1311 let winid = ShowDialog("Y", 1)
1312 let winid = ShowDialog("n", 0)
1313 let winid = ShowDialog("N", 0)
1314 let winid = ShowDialog("x", 0)
1315 let winid = ShowDialog("X", 0)
1316 let winid = ShowDialog("\<Esc>", 0)
1317 let winid = ShowDialog("\<C-C>", -1)
1318
1319 delfunc QuitCallback
1320endfunc
1321
Bram Moolenaara730e552019-06-16 19:05:31 +02001322func ShowMenu(key, result)
1323 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001324 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001325 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001326 \ })
1327 redraw
1328 call feedkeys(a:key, "xt")
1329 call assert_equal(winid, s:cb_winid)
1330 call assert_equal(a:result, s:cb_res)
1331endfunc
1332
1333func Test_popup_menu()
1334 func QuitCallback(id, res)
1335 let s:cb_winid = a:id
1336 let s:cb_res = a:res
1337 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001338 " mapping won't be used in popup
1339 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001340
1341 let winid = ShowMenu(" ", 1)
1342 let winid = ShowMenu("j \<CR>", 2)
1343 let winid = ShowMenu("JjK \<CR>", 2)
1344 let winid = ShowMenu("jjjjjj ", 3)
1345 let winid = ShowMenu("kkk ", 1)
1346 let winid = ShowMenu("x", -1)
1347 let winid = ShowMenu("X", -1)
1348 let winid = ShowMenu("\<Esc>", -1)
1349 let winid = ShowMenu("\<C-C>", -1)
1350
1351 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001352 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001353endfunc
1354
1355func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001356 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001357
1358 let lines =<< trim END
1359 call setline(1, range(1, 20))
1360 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001361 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001362 func MenuDone(id, res)
1363 echomsg "selected " .. a:res
1364 endfunc
1365 END
1366 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001367 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001368 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1369
1370 call term_sendkeys(buf, "jj")
1371 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1372
1373 call term_sendkeys(buf, " ")
1374 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1375
1376 " clean up
1377 call StopVimInTerminal(buf)
1378 call delete('XtestPopupMenu')
1379endfunc
1380
Bram Moolenaarf914a332019-07-20 15:09:56 +02001381func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001382 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001383
1384 let lines =<< trim END
1385 call setline(1, range(1, 20))
1386 hi PopupSelected ctermbg=green
1387 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1388 func MenuDone(id, res)
1389 echomsg "selected " .. a:res
1390 endfunc
1391 END
1392 call writefile(lines, 'XtestPopupNarrowMenu')
1393 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1394 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1395
1396 " clean up
1397 call term_sendkeys(buf, "x")
1398 call StopVimInTerminal(buf)
1399 call delete('XtestPopupNarrowMenu')
1400endfunc
1401
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001402func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001403 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001404
1405 " Create a popup without title or border, a line of padding will be added to
1406 " put the title on.
1407 let lines =<< trim END
1408 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001409 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001410 END
1411 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001412 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001413 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1414
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001415 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1416 call term_sendkeys(buf, ":\<CR>")
1417 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1418
1419 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1420 call term_sendkeys(buf, ":\<CR>")
1421 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1422
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001423 " clean up
1424 call StopVimInTerminal(buf)
1425 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001426
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001427 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001428 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001429 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001430 call assert_equal('Another Title', popup_getoptions(winid).title)
1431
1432 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001433endfunc
1434
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001435func Test_popup_close_callback()
1436 func PopupDone(id, result)
1437 let g:result = a:result
1438 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001439 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001440 redraw
1441 call popup_close(winid, 'done')
1442 call assert_equal('done', g:result)
1443endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001444
1445func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001446 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001447 redraw
1448 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001449 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001450 call assert_equal(5, pos.height)
1451
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001452 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001453 redraw
1454 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001455 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001456 call assert_equal(3, pos.height)
1457endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001458
1459func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001460 CheckScreendump
1461
Bram Moolenaar988c4332019-06-02 14:12:11 +02001462 " +-----------------------------+
1463 " | | |
1464 " | | |
1465 " | | |
1466 " | line1 |
1467 " |------------line2------------|
1468 " | line3 |
1469 " | line4 |
1470 " | |
1471 " | |
1472 " +-----------------------------+
1473 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001474 split
1475 vsplit
1476 let info_window1 = getwininfo()[0]
1477 let line = info_window1['height']
1478 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001479 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001480 \ line : line,
1481 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001482 \ })
1483 END
1484 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001485 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001486 call term_sendkeys(buf, "\<C-W>w")
1487 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1488
1489 " clean up
1490 call StopVimInTerminal(buf)
1491 call delete('XtestPopupBehind')
1492endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001493
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001494func s:VerifyPosition(p, msg, line, col, width, height)
1495 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1496 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1497 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1498 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001499endfunc
1500
1501func Test_popup_position_adjust()
1502 " Anything placed past 2 cells from of the right of the screen is moved to the
1503 " left.
1504 "
1505 " When wrapping is disabled, we also shift to the left to display on the
1506 " screen, unless fixed is set.
1507
1508 " Entries for cases which don't vary based on wrapping.
1509 " Format is per tests described below
1510 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001511 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1512 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1513 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1514 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1515 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1516 \
1517 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1518 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1519 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1520 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1521 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1522 \
1523 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1524 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1525 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1526 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1527 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1528 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001529
1530 " these test groups are dicts with:
1531 " - comment: something to identify the group of tests by
1532 " - options: dict of options to merge with the row/col in tests
1533 " - tests: list of cases. Each one is a list with elements:
1534 " - text
1535 " - row
1536 " - col
1537 " - expected row
1538 " - expected col
1539 " - expected width
1540 " - expected height
1541 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001542 \ #{
1543 \ comment: 'left-aligned with wrapping',
1544 \ options: #{
1545 \ wrap: 1,
1546 \ pos: 'botleft',
1547 \ },
1548 \ tests: both_wrap_tests + [
1549 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1550 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1551 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1552 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1553 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1554 \ ],
1555 \ },
1556 \ #{
1557 \ comment: 'left aligned without wrapping',
1558 \ options: #{
1559 \ wrap: 0,
1560 \ pos: 'botleft',
1561 \ },
1562 \ tests: both_wrap_tests + [
1563 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1564 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1565 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1566 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1567 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1568 \ ],
1569 \ },
1570 \ #{
1571 \ comment: 'left aligned with fixed position',
1572 \ options: #{
1573 \ wrap: 0,
1574 \ fixed: 1,
1575 \ pos: 'botleft',
1576 \ },
1577 \ tests: both_wrap_tests + [
1578 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1579 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1580 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1581 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1582 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1583 \ ],
1584 \ },
1585 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001586
1587 for test_group in tests
1588 for test in test_group.tests
1589 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001590 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001591 \ line: line,
1592 \ col: col,
1593 \ }
1594 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001595
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001596 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001597
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001598 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001599 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1600 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001601 endfor
1602 endfor
1603
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001604 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001605 %bwipe!
1606endfunc
1607
Bram Moolenaar3397f742019-06-02 18:40:06 +02001608func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001609 " width of screen
1610 let X = join(map(range(&columns), {->'X'}), '')
1611
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001612 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1613 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001614
1615 redraw
1616 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1617 call assert_equal(X, line)
1618
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001619 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001620 redraw
1621
1622 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001623 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1624 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001625
1626 redraw
1627 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1628 call assert_equal(X, line)
1629
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001630 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001631 redraw
1632
1633 " Extend so > window width
1634 let X .= 'x'
1635
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001636 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1637 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001638
1639 redraw
1640 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1641 call assert_equal(X[ : -2 ], line)
1642
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001643 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001644 redraw
1645
1646 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001647 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1648 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001649
1650 redraw
1651 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1652 call assert_equal(X[ : -2 ], line)
1653
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001654 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001655 redraw
1656
1657 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001658 let p = popup_create(X,
1659 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1660 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001661
1662 redraw
1663 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1664 let e_line = ' ' . X[ 1 : -2 ]
1665 call assert_equal(e_line, line)
1666
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001667 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001668 redraw
1669
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001670 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001671 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001672endfunc
1673
1674func Test_popup_moved()
1675 new
1676 call test_override('char_avail', 1)
1677 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1678
1679 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001680 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001681 redraw
1682 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001683 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001684 " trigger the check for last_cursormoved by going into insert mode
1685 call feedkeys("li\<Esc>", 'xt')
1686 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001687 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001688
1689 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001690 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001691 redraw
1692 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001693 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001694 call feedkeys("hi\<Esc>", 'xt')
1695 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001696 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001697
1698 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001699 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001700 redraw
1701 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001702 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001703 call feedkeys("li\<Esc>", 'xt')
1704 call assert_equal(1, popup_getpos(winid).visible)
1705 call feedkeys("ei\<Esc>", 'xt')
1706 call assert_equal(1, popup_getpos(winid).visible)
1707 call feedkeys("eli\<Esc>", 'xt')
1708 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001709 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001710
Bram Moolenaar17627312019-06-02 19:53:44 +02001711 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001712 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001713 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001714 redraw
1715 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001716 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001717 call feedkeys("eli\<Esc>", 'xt')
1718 call assert_equal(1, popup_getpos(winid).visible)
1719 call feedkeys("wi\<Esc>", 'xt')
1720 call assert_equal(1, popup_getpos(winid).visible)
1721 call feedkeys("Eli\<Esc>", 'xt')
1722 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001723 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001724
1725 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001726 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001727 redraw
1728 call assert_equal(1, popup_getpos(winid).visible)
1729 call feedkeys("eli\<Esc>", 'xt')
1730 call feedkeys("ei\<Esc>", 'xt')
1731 call assert_equal(1, popup_getpos(winid).visible)
1732 call feedkeys("eli\<Esc>", 'xt')
1733 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001734 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001735
1736 bwipe!
1737 call test_override('ALL', 0)
1738endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001739
1740func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001741 CheckFeature timers
1742 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001743
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001744 let lines =<< trim END
1745 call setline(1, range(1, 20))
1746 hi Notification ctermbg=lightblue
1747 call popup_notification('first notification', {})
1748 END
1749 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001750 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001751 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1752
1753 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001754 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1755 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001756 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1757
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001758 " clean up
1759 call StopVimInTerminal(buf)
1760 call delete('XtestNotifications')
1761endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001762
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001763func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001764 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001765
1766 let lines =<< trim END
1767 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001768 hi ScrollThumb ctermbg=blue
1769 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001770 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001771 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001772 \ minwidth: 8,
1773 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001774 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001775 func ScrollUp()
1776 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1777 endfunc
1778 func ScrollDown()
1779 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1780 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001781 func ClickTop()
1782 call feedkeys("\<F4>\<LeftMouse>", "xt")
1783 endfunc
1784 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001785 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001786 call feedkeys("\<F5>\<LeftMouse>", "xt")
1787 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001788 func Popup_filter(winid, key)
1789 if a:key == 'j'
1790 let line = popup_getoptions(a:winid).firstline
1791 let nlines = line('$', a:winid)
1792 let newline = line < nlines ? (line + 1) : nlines
1793 call popup_setoptions(a:winid, #{firstline: newline})
1794 return v:true
1795 elseif a:key == 'x'
1796 call popup_close(a:winid)
1797 return v:true
1798 endif
1799 endfunc
1800
1801 func PopupScroll()
1802 call popup_clear()
1803 let text =<< trim END
1804 1
1805 2
1806 3
1807 4
1808 long line long line long line long line long line long line
1809 long line long line long line long line long line long line
1810 long line long line long line long line long line long line
1811 END
1812 call popup_create(text, #{
1813 \ minwidth: 30,
1814 \ maxwidth: 30,
1815 \ minheight: 4,
1816 \ maxheight: 4,
1817 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +01001818 \ lastline: 4,
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001819 \ wrap: v:true,
1820 \ scrollbar: v:true,
1821 \ mapping: v:false,
1822 \ filter: funcref('Popup_filter')
1823 \ })
1824 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001825 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001826 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1827 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001828 END
1829 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001830 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001831 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1832
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001833 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001834 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001835 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1836
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001837 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001838 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001839 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1840
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001841 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001842 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001843 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1844
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02001845 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001846 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001847 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1848 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1849
1850 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1851 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1852
1853 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001854 " wait a bit, otherwise it fails sometimes (double click recognized?)
1855 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001856 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1857 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1858
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001859 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1860 sleep 100m
1861 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1862 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1863
1864 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1865 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1866
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001867 " remove the minwidth and maxheight
1868 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02001869 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001870 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
1871
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001872 " check size with non-wrapping lines
1873 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
1874 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
1875
1876 " check size with wrapping lines
1877 call term_sendkeys(buf, "j")
1878 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
1879 call term_sendkeys(buf, "x")
1880
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001881 " clean up
1882 call StopVimInTerminal(buf)
1883 call delete('XtestPopupScroll')
1884endfunc
1885
Bram Moolenaar437a7462019-07-05 20:17:22 +02001886func Test_popup_fitting_scrollbar()
1887 " this was causing a crash, divide by zero
1888 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001889 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001890 \ scrollbar: 1,
1891 \ maxwidth: 10,
1892 \ maxheight: 5,
1893 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001894 redraw
1895 call popup_clear()
1896endfunc
1897
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001898func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001899 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001900
1901 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001902 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001903 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001904 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001905 END
1906
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001907 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001908 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001909 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1910
1911 " Setting to empty string clears it
1912 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1913 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1914
1915 " Setting a list
1916 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1917 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1918
1919 " Shrinking with a list
1920 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1921 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1922
1923 " Growing with a list
1924 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1925 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1926
1927 " Empty list clears
1928 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1929 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1930
1931 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001932 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001933 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1934
1935 " clean up
1936 call StopVimInTerminal(buf)
1937 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001938endfunc
1939
1940func Test_popup_hidden()
1941 new
1942
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001943 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001944 redraw
1945 call assert_equal(0, popup_getpos(winid).visible)
1946 call popup_close(winid)
1947
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001948 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001949 redraw
1950 call assert_equal(0, popup_getpos(winid).visible)
1951 call popup_close(winid)
1952
1953 func QuitCallback(id, res)
1954 let s:cb_winid = a:id
1955 let s:cb_res = a:res
1956 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001957 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001958 \ filter: 'popup_filter_yesno',
1959 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001960 \ })
1961 redraw
1962 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001963 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1964 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001965 exe "normal anot used by filter\<Esc>"
1966 call assert_equal('not used by filter', getline(1))
1967
1968 call popup_show(winid)
1969 call feedkeys('y', "xt")
1970 call assert_equal(1, s:cb_res)
1971
1972 bwipe!
1973 delfunc QuitCallback
1974endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001975
1976" Test options not checked elsewhere
1977func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001978 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001979 let options = popup_getoptions(winid)
1980 call assert_equal(1, options.wrap)
1981 call assert_equal(0, options.drag)
1982 call assert_equal('Beautiful', options.highlight)
1983
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001984 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001985 let options = popup_getoptions(winid)
1986 call assert_equal(0, options.wrap)
1987 call assert_equal(1, options.drag)
1988 call assert_equal('Another', options.highlight)
1989
1990 call popup_close(winid)
1991endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001992
1993func Test_popupwin_garbage_collect()
1994 func MyPopupFilter(x, winid, c)
1995 " NOP
1996 endfunc
1997
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001998 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001999 call test_garbagecollect_now()
2000 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002001 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002002 call feedkeys('j', 'xt')
2003 call assert_true(v:true)
2004
2005 call popup_close(winid)
2006 delfunc MyPopupFilter
2007endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002008
Bram Moolenaar581ba392019-09-03 22:08:33 +02002009func Test_popupwin_filter_mode()
2010 func MyPopupFilter(winid, c)
2011 let s:typed = a:c
2012 if a:c == ':' || a:c == "\r" || a:c == 'v'
2013 " can start cmdline mode, get out, and start/stop Visual mode
2014 return 0
2015 endif
2016 return 1
2017 endfunc
2018
2019 " Normal, Visual and Insert mode
2020 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2021 redraw
2022 call feedkeys('x', 'xt')
2023 call assert_equal('x', s:typed)
2024
2025 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2026 call assert_equal(':', s:typed)
2027 call assert_equal('foo', g:foo)
2028
2029 let @x = 'something'
2030 call feedkeys('v$"xy', 'xt')
2031 call assert_equal('y', s:typed)
2032 call assert_equal('something', @x) " yank command is filtered out
2033 call feedkeys('v', 'xt') " end Visual mode
2034
2035 call popup_close(winid)
2036
2037 " only Normal mode
2038 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2039 redraw
2040 call feedkeys('x', 'xt')
2041 call assert_equal('x', s:typed)
2042
2043 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2044 call assert_equal(':', s:typed)
2045 call assert_equal('foo', g:foo)
2046
2047 let @x = 'something'
2048 call feedkeys('v$"xy', 'xt')
2049 call assert_equal('v', s:typed)
2050 call assert_notequal('something', @x)
2051
2052 call popup_close(winid)
2053
2054 " default: all modes
2055 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2056 redraw
2057 call feedkeys('x', 'xt')
2058 call assert_equal('x', s:typed)
2059
2060 let g:foo = 'bar'
2061 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2062 call assert_equal("\r", s:typed)
2063 call assert_equal('bar', g:foo)
2064
2065 let @x = 'something'
2066 call feedkeys('v$"xy', 'xt')
2067 call assert_equal('y', s:typed)
2068 call assert_equal('something', @x) " yank command is filtered out
2069 call feedkeys('v', 'xt') " end Visual mode
2070
2071 call popup_close(winid)
2072 delfunc MyPopupFilter
2073endfunc
2074
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002075func Test_popupwin_with_buffer()
2076 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2077 let buf = bufadd('XsomeFile')
2078 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002079
2080 setlocal number
2081 call setbufvar(buf, "&wrapmargin", 13)
2082
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002083 let winid = popup_create(buf, {})
2084 call assert_notequal(0, winid)
2085 let pos = popup_getpos(winid)
2086 call assert_equal(2, pos.height)
2087 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002088
2089 " window-local option is set to default, buffer-local is not
2090 call assert_equal(0, getwinvar(winid, '&number'))
2091 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2092
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002093 call popup_close(winid)
2094 call assert_equal({}, popup_getpos(winid))
2095 call assert_equal(1, bufloaded(buf))
2096 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002097 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002098
2099 edit test_popupwin.vim
2100 let winid = popup_create(bufnr(''), {})
2101 redraw
2102 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002103 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002104endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002105
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002106func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002107 CheckFeature terminal
2108
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002109 let ptybuf = term_start(&shell, #{hidden: 1})
2110 call assert_fails('let winnr = popup_create(ptybuf, #{})', 'E278:')
2111 exe 'bwipe! ' .. ptybuf
2112endfunc
2113
Bram Moolenaar934470e2019-09-01 23:27:05 +02002114func Test_popupwin_with_buffer_and_filter()
2115 new Xwithfilter
2116 call setline(1, range(100))
2117 let bufnr = bufnr()
2118 hide
2119
2120 func BufferFilter(win, key)
2121 if a:key == 'G'
2122 " recursive use of "G" does not cause problems.
2123 call win_execute(a:win, 'normal! G')
2124 return 1
2125 endif
2126 return 0
2127 endfunc
2128
2129 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2130 call assert_equal(1, popup_getpos(winid).firstline)
2131 redraw
2132 call feedkeys("G", 'xt')
2133 call assert_equal(99, popup_getpos(winid).firstline)
2134
2135 call popup_close(winid)
2136 exe 'bwipe! ' .. bufnr
2137endfunc
2138
Bram Moolenaare296e312019-07-03 23:20:18 +02002139func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002140 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002141 \ maxwidth: 40,
2142 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002143 \ })
2144 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002145 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002146 redraw
2147 call assert_equal(19, popup_getpos(winid).width)
2148 endfor
2149 call popup_clear()
2150endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002151
2152func Test_popupwin_buf_close()
2153 let buf = bufadd('Xtestbuf')
2154 call bufload(buf)
2155 call setbufline(buf, 1, ['just', 'some', 'lines'])
2156 let winid = popup_create(buf, {})
2157 redraw
2158 call assert_equal(3, popup_getpos(winid).height)
2159 let bufinfo = getbufinfo(buf)[0]
2160 call assert_equal(1, bufinfo.changed)
2161 call assert_equal(0, bufinfo.hidden)
2162 call assert_equal(0, bufinfo.listed)
2163 call assert_equal(1, bufinfo.loaded)
2164 call assert_equal([], bufinfo.windows)
2165 call assert_equal([winid], bufinfo.popups)
2166
2167 call popup_close(winid)
2168 call assert_equal({}, popup_getpos(winid))
2169 let bufinfo = getbufinfo(buf)[0]
2170 call assert_equal(1, bufinfo.changed)
2171 call assert_equal(1, bufinfo.hidden)
2172 call assert_equal(0, bufinfo.listed)
2173 call assert_equal(1, bufinfo.loaded)
2174 call assert_equal([], bufinfo.windows)
2175 call assert_equal([], bufinfo.popups)
2176 exe 'bwipe! ' .. buf
2177endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002178
2179func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002180 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002181
2182 let lines =<< trim END
2183 call setline(1, range(1, 10))
2184 hi ScrollThumb ctermbg=blue
2185 hi ScrollBar ctermbg=red
2186 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002187 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002188 \ maxwidth: 10,
2189 \ maxheight: 3,
2190 \ pos : 'topleft',
2191 \ col : a:col,
2192 \ line : a:line,
2193 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002194 \ })
2195 endfunc
2196 call PopupMenu(['x'], 1, 1)
2197 call PopupMenu(['123456789|'], 1, 16)
2198 call PopupMenu(['123456789|' .. ' '], 7, 1)
2199 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2200 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2201 END
2202 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002203 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002204 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2205
2206 " close the menu popupwin.
2207 call term_sendkeys(buf, " ")
2208 call term_sendkeys(buf, " ")
2209 call term_sendkeys(buf, " ")
2210 call term_sendkeys(buf, " ")
2211 call term_sendkeys(buf, " ")
2212
2213 " clean up
2214 call StopVimInTerminal(buf)
2215 call delete('XtestPopupMenuMaxWidth')
2216endfunc
2217
Bram Moolenaara901a372019-07-13 16:38:50 +02002218func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002219 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002220
2221 let lines =<< trim END
2222 call setline(1, range(1, 20))
2223 hi ScrollThumb ctermbg=blue
2224 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002225 eval ['one', 'two', 'three', 'four', 'five',
2226 \ 'six', 'seven', 'eight', 'nine']
2227 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002228 \ minwidth: 8,
2229 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002230 \ })
2231 END
2232 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002233 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002234
2235 call term_sendkeys(buf, "j")
2236 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2237
2238 call term_sendkeys(buf, "jjj")
2239 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2240
2241 " if the cursor is the bottom line, it stays at the bottom line.
2242 call term_sendkeys(buf, repeat("j", 20))
2243 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2244
2245 call term_sendkeys(buf, "kk")
2246 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2247
2248 call term_sendkeys(buf, "k")
2249 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2250
2251 " if the cursor is in the top line, it stays in the top line.
2252 call term_sendkeys(buf, repeat("k", 20))
2253 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2254
2255 " close the menu popupwin.
2256 call term_sendkeys(buf, " ")
2257
2258 " clean up
2259 call StopVimInTerminal(buf)
2260 call delete('XtestPopupMenuScroll')
2261endfunc
2262
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002263func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002264 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002265
2266 let lines =<< trim END
2267 function! MyFilter(winid, key) abort
2268 if a:key == "0"
2269 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2270 return 1
2271 endif
2272 if a:key == "G"
2273 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2274 return 1
2275 endif
2276 if a:key == "j"
2277 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2278 return 1
2279 endif
2280 if a:key == "k"
2281 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2282 return 1
2283 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002284 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002285 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002286 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002287 endif
2288 return 0
2289 endfunction
2290 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2291 \ maxheight : 3,
2292 \ filter : 'MyFilter'
2293 \ })
2294 END
2295 call writefile(lines, 'XtestPopupMenuFilter')
2296 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2297
2298 call term_sendkeys(buf, "j")
2299 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2300
2301 call term_sendkeys(buf, "k")
2302 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2303
2304 call term_sendkeys(buf, "G")
2305 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2306
2307 call term_sendkeys(buf, "0")
2308 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2309
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002310 " check that when the popup is closed in the filter the screen is redrawn
2311 call term_sendkeys(buf, ":")
2312 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2313 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002314
2315 " clean up
2316 call StopVimInTerminal(buf)
2317 call delete('XtestPopupMenuFilter')
2318endfunc
2319
2320func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002321 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002322
2323 let winid = popup_create('some text', {})
2324 call assert_equal(0, popup_getoptions(winid).cursorline)
2325 call popup_close(winid)
2326
2327 let winid = popup_create('some text', #{ cursorline: 1, })
2328 call assert_equal(1, popup_getoptions(winid).cursorline)
2329 call popup_close(winid)
2330
2331 let winid = popup_create('some text', #{ cursorline: 0, })
2332 call assert_equal(0, popup_getoptions(winid).cursorline)
2333 call popup_close(winid)
2334
2335 let winid = popup_menu('some text', {})
2336 call assert_equal(1, popup_getoptions(winid).cursorline)
2337 call popup_close(winid)
2338
2339 let winid = popup_menu('some text', #{ cursorline: 1, })
2340 call assert_equal(1, popup_getoptions(winid).cursorline)
2341 call popup_close(winid)
2342
2343 let winid = popup_menu('some text', #{ cursorline: 0, })
2344 call assert_equal(0, popup_getoptions(winid).cursorline)
2345 call popup_close(winid)
2346
2347 " ---------
2348 " Pattern 1
2349 " ---------
2350 let lines =<< trim END
2351 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2352 END
2353 call writefile(lines, 'XtestPopupCursorLine')
2354 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2355 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2356 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2357 call StopVimInTerminal(buf)
2358
2359 " ---------
2360 " Pattern 2
2361 " ---------
2362 let lines =<< trim END
2363 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2364 END
2365 call writefile(lines, 'XtestPopupCursorLine')
2366 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2367 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2368 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2369 call StopVimInTerminal(buf)
2370
2371 " ---------
2372 " Pattern 3
2373 " ---------
2374 let lines =<< trim END
2375 function! MyFilter(winid, key) abort
2376 if a:key == "j"
2377 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2378 return 1
2379 endif
2380 if a:key == 'x'
2381 call popup_close(a:winid)
2382 return 1
2383 endif
2384 return 0
2385 endfunction
2386 call popup_menu(['111', '222', '333'], #{
2387 \ cursorline : 0,
2388 \ maxheight : 2,
2389 \ filter : 'MyFilter',
2390 \ })
2391 END
2392 call writefile(lines, 'XtestPopupCursorLine')
2393 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2394 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2395 call term_sendkeys(buf, "j")
2396 call term_sendkeys(buf, "j")
2397 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2398 call term_sendkeys(buf, "x")
2399 call StopVimInTerminal(buf)
2400
2401 " ---------
2402 " Pattern 4
2403 " ---------
2404 let lines =<< trim END
2405 function! MyFilter(winid, key) abort
2406 if a:key == "j"
2407 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2408 return 1
2409 endif
2410 if a:key == 'x'
2411 call popup_close(a:winid)
2412 return 1
2413 endif
2414 return 0
2415 endfunction
2416 call popup_menu(['111', '222', '333'], #{
2417 \ cursorline : 1,
2418 \ maxheight : 2,
2419 \ filter : 'MyFilter',
2420 \ })
2421 END
2422 call writefile(lines, 'XtestPopupCursorLine')
2423 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2424 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2425 call term_sendkeys(buf, "j")
2426 call term_sendkeys(buf, "j")
2427 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2428 call term_sendkeys(buf, "x")
2429 call StopVimInTerminal(buf)
2430
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002431 " ---------
2432 " Cursor in second line when creating the popup
2433 " ---------
2434 let lines =<< trim END
2435 let winid = popup_create(['111', '222', '333'], #{
2436 \ cursorline : 1,
2437 \ })
2438 call win_execute(winid, "2")
2439 END
2440 call writefile(lines, 'XtestPopupCursorLine')
2441 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2442 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2443 call StopVimInTerminal(buf)
2444
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002445 call delete('XtestPopupCursorLine')
2446endfunc
2447
Bram Moolenaarf914a332019-07-20 15:09:56 +02002448func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002449 CheckScreendump
2450
Bram Moolenaarf914a332019-07-20 15:09:56 +02002451 call writefile([
2452 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2453 \ "another\tXtagfile\t/^this is another",
2454 \ "theword\tXtagfile\t/^theword"],
2455 \ 'Xtags')
2456 call writefile(range(1,20)
2457 \ + ['theword is here']
2458 \ + range(22, 27)
2459 \ + ['this is another place']
2460 \ + range(29, 40),
2461 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002462 call writefile(range(1,10)
2463 \ + ['searched word is here']
2464 \ + range(12, 20),
2465 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002466 let lines =<< trim END
2467 set tags=Xtags
2468 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002469 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002470 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002471 \ 'three',
2472 \ 'four',
2473 \ 'five',
2474 \ 'six',
2475 \ 'seven',
2476 \ 'find theword somewhere',
2477 \ 'nine',
2478 \ 'this is another word',
2479 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002480 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002481 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002482 END
2483 call writefile(lines, 'XtestPreviewPopup')
2484 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2485
2486 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2487 call term_sendkeys(buf, ":\<CR>")
2488 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2489
2490 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2491 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2492
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002493 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002494 call term_sendkeys(buf, ":\<CR>")
2495 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2496
2497 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2498 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2499
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002500 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2501 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002502 call term_sendkeys(buf, ":cd testdir\<CR>")
2503
2504 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002505 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002506 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2507
2508 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2509 call term_sendkeys(buf, ":\<CR>")
2510 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2511
2512 call term_sendkeys(buf, ":pclose\<CR>")
2513 call term_sendkeys(buf, ":psearch searched\<CR>")
2514 call term_sendkeys(buf, ":\<CR>")
2515 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002516
Bram Moolenaarf914a332019-07-20 15:09:56 +02002517 call StopVimInTerminal(buf)
2518 call delete('Xtags')
2519 call delete('Xtagfile')
2520 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002521 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002522endfunc
2523
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002524func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002525 let lines =<< trim END
2526 set completeopt+=preview,popup
2527 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002528 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002529
2530 func CompleteFuncDict(findstart, base)
2531 if a:findstart
2532 if col('.') > 10
2533 return col('.') - 10
2534 endif
2535 return 0
2536 endif
2537
2538 return {
2539 \ 'words': [
2540 \ {
2541 \ 'word': 'aword',
2542 \ 'abbr': 'wrd',
2543 \ 'menu': 'extra text',
2544 \ 'info': 'words are cool',
2545 \ 'kind': 'W',
2546 \ 'user_data': 'test'
2547 \ },
2548 \ {
2549 \ 'word': 'anotherword',
2550 \ 'abbr': 'anotwrd',
2551 \ 'menu': 'extra text',
2552 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2553 \ 'kind': 'W',
2554 \ 'user_data': 'notest'
2555 \ },
2556 \ {
2557 \ 'word': 'noinfo',
2558 \ 'abbr': 'noawrd',
2559 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002560 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002561 \ 'kind': 'W',
2562 \ 'user_data': 'notest'
2563 \ },
2564 \ {
2565 \ 'word': 'thatword',
2566 \ 'abbr': 'thatwrd',
2567 \ 'menu': 'extra text',
2568 \ 'info': 'that word is cool',
2569 \ 'kind': 'W',
2570 \ 'user_data': 'notest'
2571 \ },
2572 \ ]
2573 \ }
2574 endfunc
2575 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002576 func ChangeColor()
2577 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002578 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002579 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002580
2581 func InfoHidden()
2582 set completepopup=height:4,border:off,align:menu
2583 set completeopt-=popup completeopt+=popuphidden
2584 au CompleteChanged * call HandleChange()
2585 endfunc
2586
2587 let s:counter = 0
2588 func HandleChange()
2589 let s:counter += 1
2590 let selected = complete_info(['selected']).selected
2591 if selected <= 0
2592 " First time: do nothing, info remains hidden
2593 return
2594 endif
2595 if selected == 1
2596 " Second time: show info right away
2597 let id = popup_findinfo()
2598 if id
2599 call popup_settext(id, 'immediate info ' .. s:counter)
2600 call popup_show(id)
2601 endif
2602 else
2603 " Third time: show info after a short delay
2604 call timer_start(100, 'ShowInfo')
2605 endif
2606 endfunc
2607
2608 func ShowInfo(...)
2609 let id = popup_findinfo()
2610 if id
2611 call popup_settext(id, 'async info ' .. s:counter)
2612 call popup_show(id)
2613 endif
2614 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002615 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002616 return lines
2617endfunc
2618
2619func Test_popupmenu_info_border()
2620 CheckScreendump
2621
2622 let lines = Get_popupmenu_lines()
2623 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002624 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002625
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002626 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2627 call term_wait(buf, 50)
2628
2629 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2630 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2631
2632 call term_sendkeys(buf, "\<C-N>")
2633 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2634
2635 call term_sendkeys(buf, "\<C-N>")
2636 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2637
2638 call term_sendkeys(buf, "\<C-N>\<C-N>")
2639 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2640
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002641 " info on the left with scrollbar
2642 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2643 call term_sendkeys(buf, "\<C-N>\<C-N>")
2644 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2645
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002646 call StopVimInTerminal(buf)
2647 call delete('XtestInfoPopup')
2648endfunc
2649
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002650func Test_popupmenu_info_noborder()
2651 CheckScreendump
2652
2653 let lines = Get_popupmenu_lines()
2654 call add(lines, 'set completepopup=height:4,border:off')
2655 call writefile(lines, 'XtestInfoPopupNb')
2656
2657 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2658 call term_wait(buf, 50)
2659
2660 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2661 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2662
2663 call StopVimInTerminal(buf)
2664 call delete('XtestInfoPopupNb')
2665endfunc
2666
Bram Moolenaar258cef52019-08-21 17:29:29 +02002667func Test_popupmenu_info_align_menu()
2668 CheckScreendump
2669
2670 let lines = Get_popupmenu_lines()
2671 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2672 call writefile(lines, 'XtestInfoPopupNb')
2673
2674 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2675 call term_wait(buf, 50)
2676
2677 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2678 call term_sendkeys(buf, "\<C-N>")
2679 call term_sendkeys(buf, "\<C-N>")
2680 call term_sendkeys(buf, "\<C-N>")
2681 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2682
2683 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2684 call term_sendkeys(buf, "\<C-N>")
2685 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2686
2687 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002688 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002689 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2690 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2691 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2692
2693 call StopVimInTerminal(buf)
2694 call delete('XtestInfoPopupNb')
2695endfunc
2696
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002697func Test_popupmenu_info_hidden()
2698 CheckScreendump
2699
2700 let lines = Get_popupmenu_lines()
2701 call add(lines, 'call InfoHidden()')
2702 call writefile(lines, 'XtestInfoPopupHidden')
2703
2704 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
2705 call term_wait(buf, 50)
2706
2707 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2708 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
2709
2710 call term_sendkeys(buf, "\<C-N>")
2711 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
2712
2713 call term_sendkeys(buf, "\<C-N>")
2714 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
2715
2716 call term_sendkeys(buf, "\<Esc>")
2717 call StopVimInTerminal(buf)
2718 call delete('XtestInfoPopupHidden')
2719endfunc
2720
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002721func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02002722 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002723 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002724 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002725 call assert_equal(bufnr, winbufnr(winid))
2726 call popup_clear()
2727endfunc
2728
Bram Moolenaar1824f452019-10-02 23:06:46 +02002729func Test_popupwin_getoptions_tablocal()
2730 topleft split
2731 let win1 = popup_create('nothing', #{maxheight: 8})
2732 let win2 = popup_create('something', #{maxheight: 10})
2733 let win3 = popup_create('something', #{maxheight: 15})
2734 call assert_equal(8, popup_getoptions(win1).maxheight)
2735 call assert_equal(10, popup_getoptions(win2).maxheight)
2736 call assert_equal(15, popup_getoptions(win3).maxheight)
2737 call popup_clear()
2738 quit
2739endfunc
2740
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002741func Test_popupwin_cancel()
2742 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
2743 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
2744 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
2745 call assert_equal(5, popup_getpos(win1).line)
2746 call assert_equal(10, popup_getpos(win2).line)
2747 call assert_equal(15, popup_getpos(win3).line)
2748 " TODO: this also works without patch 8.1.2110
2749 call feedkeys("\<C-C>", 'xt')
2750 call assert_equal(5, popup_getpos(win1).line)
2751 call assert_equal(10, popup_getpos(win2).line)
2752 call assert_equal({}, popup_getpos(win3))
2753 call feedkeys("\<C-C>", 'xt')
2754 call assert_equal(5, popup_getpos(win1).line)
2755 call assert_equal({}, popup_getpos(win2))
2756 call assert_equal({}, popup_getpos(win3))
2757 call feedkeys("\<C-C>", 'xt')
2758 call assert_equal({}, popup_getpos(win1))
2759 call assert_equal({}, popup_getpos(win2))
2760 call assert_equal({}, popup_getpos(win3))
2761endfunc
2762
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002763" vim: shiftwidth=2 sts=2