blob: 810a2047c186321614aef3c7050fe539dc5fbea1 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature textprop
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
15 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020016 call prop_type_add('comment', #{highlight: 'Comment'})
17 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
18 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020019 call setwinvar(winid2, '&wincolor', 'PopupColor2')
20 END
21 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020022 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020023 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
24
Bram Moolenaarec583842019-05-26 14:11:23 +020025 " Add a tabpage
26 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020027 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020028 \ .. "#{text: 'other tab'},"
29 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020030 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020031 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020032 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020033 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
34
35 " switch back to first tabpage
36 call term_sendkeys(buf, "gt")
37 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
38
39 " close that tabpage
40 call term_sendkeys(buf, ":quit!\<CR>")
41 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
42
Bram Moolenaar202d9822019-06-11 21:56:30 +020043 " set 'columns' to a small value, size must be recomputed
44 call term_sendkeys(buf, ":let cols = &columns\<CR>")
45 call term_sendkeys(buf, ":set columns=12\<CR>")
46 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
47 call term_sendkeys(buf, ":let &columns = cols\<CR>")
48
Bram Moolenaar17146962019-05-30 00:12:11 +020049 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020050 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020051 call term_sendkeys(buf, ":redraw\<CR>")
52 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
53
Bram Moolenaar17146962019-05-30 00:12:11 +020054 " show not fitting line at bottom
55 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
56 call term_sendkeys(buf, ":redraw\<CR>")
57 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
58
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020059 " move popup over ruler
60 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020061 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020062 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020063 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
64
65 " clear all popups after moving the cursor a bit, so that ruler is updated
66 call term_sendkeys(buf, "axxx\<Esc>")
67 call term_wait(buf)
68 call term_sendkeys(buf, "0")
69 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020070 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020071 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
72
Bram Moolenaar4d784b22019-05-25 19:51:39 +020073 " clean up
74 call StopVimInTerminal(buf)
75 call delete('XtestPopup')
76endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020077
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020078func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020079 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020080
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020081 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020082 let lines =<< trim END
83 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020084 call popup_create('hello border', #{line: 2, col: 3, border: []})
85 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020086 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020087 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
88 call popup_create('paddings', #{line: 6, col: 23, padding: [1, 3, 2, 4]})
89 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
90 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020091 END
92 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
93 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020094 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020095 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
96
97 call StopVimInTerminal(buf)
98 call delete('XtestPopupBorder')
99 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200100
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200101 let lines =<< trim END
102 call setline(1, range(1, 100))
103 hi BlueColor ctermbg=lightblue
104 hi TopColor ctermbg=253
105 hi RightColor ctermbg=245
106 hi BottomColor ctermbg=240
107 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200108 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
109 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
110 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
111 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
112 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
113 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200114 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200115 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200116 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200117 END
118 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200119 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200120 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
121
Bram Moolenaarad24a712019-06-17 20:05:45 +0200122 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200123 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200124 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
125
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200126 " check multi-byte border only with 'ambiwidth' single
127 if &ambiwidth == 'single'
128 call term_sendkeys(buf, ":call MultiByte()\<CR>")
129 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
130 endif
131
Bram Moolenaar790498b2019-06-01 22:15:29 +0200132 call StopVimInTerminal(buf)
133 call delete('XtestPopupBorder')
134
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200135 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200136 \ line: 2,
137 \ core_line: 3,
138 \ col: 3,
139 \ core_col: 4,
140 \ width: 14,
141 \ core_width: 12,
142 \ height: 3,
143 \ core_height: 1,
144 \ firstline: 1,
145 \ scrollbar: 0,
146 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200147 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200148 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200149 let options = popup_getoptions(winid)
150 call assert_equal([], options.border)
151 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200152
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200153 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200154 let with_border_or_padding.width = 15
155 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200156 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200157 let options = popup_getoptions(winid)
158 call assert_false(has_key(options, "border"))
159 call assert_equal([], options.padding)
160
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200161 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200162 \ padding: [1, 2, 3, 4],
163 \ border: [4, 0, 7, 8],
164 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
165 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200166 \ })
167 let options = popup_getoptions(winid)
168 call assert_equal([1, 0, 1, 1], options.border)
169 call assert_equal([1, 2, 3, 4], options.padding)
170 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
171 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200172
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200173 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
174 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200175 \ line: 3,
176 \ core_line: 5,
177 \ col: 8,
178 \ core_col: 10,
179 \ width: 14,
180 \ core_width: 10,
181 \ height: 5,
182 \ scrollbar: 0,
183 \ core_height: 1,
184 \ firstline: 1,
185 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200186
187 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200188endfunc
189
Bram Moolenaarb4230122019-05-30 18:40:53 +0200190func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200191 CheckScreendump
192
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200193 let lines =<< trim END
194 call setline(1, range(1, 100))
195 hi PopupColor ctermbg=lightblue
196 let winid = popup_create([
197 \ '#include <stdio.h>',
198 \ 'int main(void)',
199 \ '{',
200 \ ' printf(123);',
201 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200202 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200203 call win_execute(winid, 'set syntax=cpp')
204 END
205 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200206 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200207 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
208
209 " clean up
210 call StopVimInTerminal(buf)
211 call delete('XtestPopup')
212endfunc
213
214func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200215 CheckScreendump
216
Bram Moolenaar402502d2019-05-30 22:07:36 +0200217 let lines =<< trim END
218 call setline(1, range(1, 100))
219 hi PopupColor ctermbg=lightgrey
220 let winid = popup_create([
221 \ '#include <stdio.h>',
222 \ 'int main(void)',
223 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200224 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200225 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200226 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200227 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
228 END
229 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200230 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200231 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
232
233 " clean up
234 call StopVimInTerminal(buf)
235 call delete('XtestPopup')
236endfunc
237
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200238func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200239 CheckScreendump
240
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200241 let lines =<< trim END
242 call setline(1, ['111 222 333', '444 555 666'])
243 let winid = popup_create([
244 \ '111 222 333',
245 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200246 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200247 set hlsearch
248 /666
249 call matchadd('ErrorMsg', '111')
250 call matchadd('ErrorMsg', '444')
251 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
252 call win_execute(winid, "call matchadd('ErrorMsg', '555')")
253 END
254 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200255 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200256 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
257
258 " clean up
259 call StopVimInTerminal(buf)
260 call delete('XtestPopupMatches')
261endfunc
262
Bram Moolenaar399d8982019-06-02 15:34:29 +0200263func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200264 CheckScreendump
265
Bram Moolenaar399d8982019-06-02 15:34:29 +0200266 let lines =<< trim END
267 call setline(1, repeat([repeat('-', 60)], 15))
268 set so=0
269 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200270 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200271 \ line: 'cursor+1',
272 \ col: 'cursor',
273 \ pos: 'topleft',
274 \ border: [],
275 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200276 \ })
277 normal 25|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200278 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200279 \ line: 'cursor+1',
280 \ col: 'cursor',
281 \ pos: 'topright',
282 \ border: [],
283 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200284 \ })
285 normal 9G29|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200286 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200287 \ line: 'cursor-1',
288 \ col: 'cursor',
289 \ pos: 'botleft',
290 \ border: [],
291 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200292 \ })
293 normal 51|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200294 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200295 \ line: 'cursor-1',
296 \ col: 'cursor',
297 \ pos: 'botright',
298 \ border: [],
299 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200300 \ })
301 END
302 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200303 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200304 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
305
306 " clean up
307 call StopVimInTerminal(buf)
308 call delete('XtestPopupCorners')
309endfunc
310
Bram Moolenaar8d241042019-06-12 23:40:01 +0200311func Test_popup_firstline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200312 CheckScreendump
313
Bram Moolenaar8d241042019-06-12 23:40:01 +0200314 let lines =<< trim END
315 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200316 call popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200317 \ maxheight: 4,
318 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200319 \ })
320 END
321 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200322 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200323 call VerifyScreenDump(buf, 'Test_popupwin_firstline', {})
324
325 " clean up
326 call StopVimInTerminal(buf)
327 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200328
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200329 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200330 \ maxheight: 2,
331 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200332 \ })
333 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200334 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200335 call assert_equal(1, popup_getoptions(winid).firstline)
336
337 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200338endfunc
339
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200340func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200341 CheckScreendump
342
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200343 " create a popup that covers the command line
344 let lines =<< trim END
345 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200346 split
347 vsplit
348 $wincmd w
349 vsplit
350 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200351 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200352 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200353 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200354 \ border: [],
355 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200356 \ })
357 func Dragit()
358 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
359 endfunc
360 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200361 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200362 func Resize()
363 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
364 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200365 map <silent> <F5> :call test_setmouse(6, 21)<CR>
366 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200367 END
368 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200369 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200370 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
371
372 call term_sendkeys(buf, ":call Dragit()\<CR>")
373 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
374
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200375 call term_sendkeys(buf, ":call Resize()\<CR>")
376 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
377
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200378 " clean up
379 call StopVimInTerminal(buf)
380 call delete('XtestPopupDrag')
381endfunc
382
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200383func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200384 CheckScreendump
385
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200386 let lines =<< trim END
387 call setline(1, range(1, 20))
388 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200389 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200390 \ close: 'button',
391 \ border: [],
392 \ line: 1,
393 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200394 \ })
395 func CloseMsg(id, result)
396 echomsg 'Popup closed with ' .. a:result
397 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200398 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200399 \ close: 'click',
400 \ line: 3,
401 \ col: 15,
402 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200403 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200404 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200405 \ close: 'button',
406 \ line: 5,
407 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200408 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200409 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200410 \ close: 'button',
411 \ padding: [],
412 \ line: 5,
413 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200414 \ })
415 func CloseWithX()
416 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
417 endfunc
418 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
419 func CloseWithClick()
420 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
421 endfunc
422 map <silent> <F4> :call test_setmouse(3, 17)<CR>
423 END
424 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200425 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200426 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
427
428 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
429 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
430
431 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
432 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
433
434 " clean up
435 call StopVimInTerminal(buf)
436 call delete('XtestPopupClose')
437endfunction
438
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200439func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200440 CheckScreendump
441
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200442 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200443 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200444 hi PopupColor ctermbg=lightgrey
445 let winid = popup_create([
446 \ 'some text',
447 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200448 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200449 \ line: 1,
450 \ col: 10,
451 \ wrap: 0,
452 \ fixed: 1,
453 \ zindex: 90,
454 \ padding: [],
455 \ highlight: 'PopupColor',
456 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200457 call popup_create([
458 \ 'xxxxxxxxx',
459 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200460 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200461 \ line: 3,
462 \ col: 18,
463 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200464 let winidb = popup_create([
465 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200466 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200467 \ line: 7,
468 \ col: 10,
469 \ wrap: 0,
470 \ fixed: 1,
471 \ close: 'button',
472 \ zindex: 90,
473 \ padding: [],
474 \ border: [],
475 \ 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 +0200476 END
477 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200478 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200479 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
480
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200481 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
482 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200483 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200484 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
485
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200486 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
487 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200488 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200489 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
490
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200491 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
492 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200493 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200494 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
495
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200496 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
497 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200498 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200499 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
500
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200501 " clean up
502 call StopVimInTerminal(buf)
503 call delete('XtestPopupMask')
504endfunc
505
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200506func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200507 CheckScreendump
508 CheckFeature clipboard_working
509
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200510 " create a popup with some text to be selected
511 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200512 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200513 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200514 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200515 \ drag: 1,
516 \ border: [],
517 \ line: 3,
518 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200519 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200520 \ })
521 func Select1()
522 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
523 endfunc
524 map <silent> <F3> :call test_setmouse(4, 15)<CR>
525 map <silent> <F4> :call test_setmouse(6, 23)<CR>
526 END
527 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200528 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200529 call term_sendkeys(buf, ":call Select1()\<CR>")
530 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
531
532 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
533 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200534 " clean the command line, sometimes it still shows a command
535 call term_sendkeys(buf, ":\<esc>")
536
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200537 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
538
539 " clean up
540 call StopVimInTerminal(buf)
541 call delete('XtestPopupSelect')
542endfunc
543
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200544func Test_popup_in_tab()
545 " default popup is local to tab, not visible when in other tab
546 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200547 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200548 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200549 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200550 tabnew
551 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200552 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200553 quit
554 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200555
556 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200557 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200558 " buffer is gone now
559 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200560
561 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200562 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200563 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200564 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200565 tabnew
566 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200567 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200568 quit
569 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200570 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200571
572 " create popup in other tab
573 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200574 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200575 call assert_equal(0, popup_getpos(winid).visible)
576 call assert_equal(1, popup_getoptions(winid).tabpage)
577 quit
578 call assert_equal(1, popup_getpos(winid).visible)
579 call assert_equal(0, popup_getoptions(winid).tabpage)
580 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200581endfunc
582
583func Test_popup_valid_arguments()
584 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200585 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200586 let pos = popup_getpos(winid)
587 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200588 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200589
590 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200591 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200592 let pos = popup_getpos(winid)
593 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200594 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200595
596 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200597 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200598 let pos = popup_getpos(winid)
599 let around = (&columns - pos.width) / 2
600 call assert_inrange(around - 1, around + 1, pos.col)
601 let around = (&lines - pos.height) / 2
602 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200603 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200604endfunc
605
606func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200607 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200608 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200609 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200610 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200611
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200612 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200613 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200614 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200615 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200616 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200617 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200618 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200619 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200620
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200621 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200622 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200623 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200624 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200625 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200626 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200627 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200628 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200629
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200630 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200631 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200632 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200633 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200634 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200635 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200636 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200637 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200638 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
639 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200640 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200641 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200642
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200643 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200644 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200645 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200646 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200647 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200648 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200649 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
650 call popup_clear()
651 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200652 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200653 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200654endfunc
655
Bram Moolenaareea16992019-05-31 17:34:48 +0200656func Test_win_execute_closing_curwin()
657 split
658 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200659 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200660 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200661endfunc
662
663func Test_win_execute_not_allowed()
664 let winid = popup_create('some text', {})
665 call assert_fails('call win_execute(winid, "split")', 'E994:')
666 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
667 call assert_fails('call win_execute(winid, "close")', 'E994:')
668 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200669 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200670 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
671 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
672 call assert_fails('call win_execute(winid, "next")', 'E994:')
673 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
674 call assert_fails('call win_execute(winid, "buf")', 'E994:')
675 call assert_fails('call win_execute(winid, "edit")', 'E994:')
676 call assert_fails('call win_execute(winid, "enew")', 'E994:')
677 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
678 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
679 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
680 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200681 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200682endfunc
683
Bram Moolenaar402502d2019-05-30 22:07:36 +0200684func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200685 CheckScreendump
686
Bram Moolenaar402502d2019-05-30 22:07:36 +0200687 let lines =<< trim END
688 call setline(1, range(1, 100))
689 let winid = popup_create(
690 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200691 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200692 END
693 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200694 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200695 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
696
697 " clean up
698 call StopVimInTerminal(buf)
699 call delete('XtestPopup')
700endfunc
701
702func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200703 CheckScreendump
704
Bram Moolenaar402502d2019-05-30 22:07:36 +0200705 let lines =<< trim END
706 call setline(1, range(1, 100))
707 let winid = popup_create(
708 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200709 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200710 END
711 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200712 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200713 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
714
715 " clean up
716 call StopVimInTerminal(buf)
717 call delete('XtestPopup')
718endfunc
719
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200720func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200721 CheckScreendump
722
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200723 let lines =<< trim END
724 set showbreak=>>\
725 call setline(1, range(1, 20))
726 let winid = popup_dialog(
727 \ 'a long line here',
728 \ #{filter: 'popup_filter_yesno'})
729 END
730 call writefile(lines, 'XtestPopupShowbreak')
731 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
732 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
733
734 " clean up
735 call term_sendkeys(buf, "y")
736 call StopVimInTerminal(buf)
737 call delete('XtestPopupShowbreak')
738endfunc
739
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200740func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200741 CheckFeature timers
742
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200743 topleft vnew
744 call setline(1, 'hello')
745
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200746 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200747 \ line: 1,
748 \ col: 1,
749 \ minwidth: 20,
750 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200751 \})
752 redraw
753 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
754 call assert_equal('world', line)
755
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200756 call assert_equal(winid, popup_locate(1, 1))
757 call assert_equal(winid, popup_locate(1, 20))
758 call assert_equal(0, popup_locate(1, 21))
759 call assert_equal(0, popup_locate(2, 1))
760
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200761 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200762 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200763 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
764 call assert_equal('hello', line)
765
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200766 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200767 \ line: &lines,
768 \ col: 10,
769 \ minwidth: 20,
770 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200771 \})
772 redraw
773 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
774 call assert_match('.*on the command line.*', line)
775
776 sleep 700m
777 redraw
778 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
779 call assert_notmatch('.*on the command line.*', line)
780
781 bwipe!
782endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200783
784func Test_popup_hide()
785 topleft vnew
786 call setline(1, 'hello')
787
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200788 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200789 \ line: 1,
790 \ col: 1,
791 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200792 \})
793 redraw
794 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
795 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200796 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200797 " buffer is still listed and active
798 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200799
800 call popup_hide(winid)
801 redraw
802 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
803 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200804 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200805 " buffer is still listed but hidden
806 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200807
808 call popup_show(winid)
809 redraw
810 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
811 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200812 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200813
814
815 call popup_close(winid)
816 redraw
817 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
818 call assert_equal('hello', line)
819
820 " error is given for existing non-popup window
821 call assert_fails('call popup_hide(win_getid())', 'E993:')
822
823 " no error non-existing window
824 call popup_hide(1234234)
825 call popup_show(41234234)
826
827 bwipe!
828endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200829
830func Test_popup_move()
831 topleft vnew
832 call setline(1, 'hello')
833
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200834 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200835 \ line: 1,
836 \ col: 1,
837 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200838 \})
839 redraw
840 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
841 call assert_equal('world ', line)
842
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200843 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200844 redraw
845 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
846 call assert_equal('hello ', line)
847 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
848 call assert_equal('~world', line)
849
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200850 call popup_move(winid, #{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200851 redraw
852 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
853 call assert_equal('hworld', line)
854
855 call popup_close(winid)
856
857 bwipe!
858endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200859
Bram Moolenaar402502d2019-05-30 22:07:36 +0200860func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200861 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200862 \ line: 2,
863 \ col: 3,
864 \ minwidth: 10,
865 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200866 \})
867 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200868 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200869 call assert_equal(2, res.line)
870 call assert_equal(3, res.col)
871 call assert_equal(10, res.width)
872 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200873 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200874
875 call popup_close(winid)
876endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200877
878func Test_popup_width_longest()
879 let tests = [
880 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
881 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
882 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
883 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
884 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
885 \ ]
886
887 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200888 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200889 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200890 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200891 call assert_equal(test[1], position.width)
892 call popup_close(winid)
893 endfor
894endfunc
895
896func Test_popup_wraps()
897 let tests = [
898 \ ['nowrap', 6, 1],
899 \ ['a line that wraps once', 12, 2],
900 \ ['a line that wraps two times', 12, 3],
901 \ ]
902 for test in tests
903 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200904 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200905 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200906 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200907 call assert_equal(test[1], position.width)
908 call assert_equal(test[2], position.height)
909
910 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200911 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200912 endfor
913endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200914
915func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200916 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200917 \ line: 2,
918 \ col: 3,
919 \ minwidth: 10,
920 \ minheight: 11,
921 \ maxwidth: 20,
922 \ maxheight: 21,
923 \ zindex: 100,
924 \ time: 5000,
925 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200926 \})
927 redraw
928 let res = popup_getoptions(winid)
929 call assert_equal(2, res.line)
930 call assert_equal(3, res.col)
931 call assert_equal(10, res.minwidth)
932 call assert_equal(11, res.minheight)
933 call assert_equal(20, res.maxwidth)
934 call assert_equal(21, res.maxheight)
935 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200936 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +0200937 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200938 if has('timers')
939 call assert_equal(5000, res.time)
940 endif
941 call popup_close(winid)
942
943 let winid = popup_create('hello', {})
944 redraw
945 let res = popup_getoptions(winid)
946 call assert_equal(0, res.line)
947 call assert_equal(0, res.col)
948 call assert_equal(0, res.minwidth)
949 call assert_equal(0, res.minheight)
950 call assert_equal(0, res.maxwidth)
951 call assert_equal(0, res.maxheight)
952 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200953 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200954 if has('timers')
955 call assert_equal(0, res.time)
956 endif
957 call popup_close(winid)
958 call assert_equal({}, popup_getoptions(winid))
959endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200960
961func Test_popup_option_values()
962 new
963 " window-local
964 setlocal number
965 setlocal nowrap
966 " buffer-local
967 setlocal omnifunc=Something
968 " global/buffer-local
969 setlocal path=/there
970 " global/window-local
971 setlocal scrolloff=9
972
973 let winid = popup_create('hello', {})
974 call assert_equal(0, getwinvar(winid, '&number'))
975 call assert_equal(1, getwinvar(winid, '&wrap'))
976 call assert_equal('', getwinvar(winid, '&omnifunc'))
977 call assert_equal(&g:path, getwinvar(winid, '&path'))
978 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
979
980 call popup_close(winid)
981 bwipe
982endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200983
984func Test_popup_atcursor()
985 topleft vnew
986 call setline(1, [
987 \ 'xxxxxxxxxxxxxxxxx',
988 \ 'xxxxxxxxxxxxxxxxx',
989 \ 'xxxxxxxxxxxxxxxxx',
990 \])
991
992 call cursor(2, 2)
993 redraw
994 let winid = popup_atcursor('vim', {})
995 redraw
996 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
997 call assert_equal('xvimxxxxxxxxxxxxx', line)
998 call popup_close(winid)
999
1000 call cursor(3, 4)
1001 redraw
1002 let winid = popup_atcursor('vim', {})
1003 redraw
1004 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1005 call assert_equal('xxxvimxxxxxxxxxxx', line)
1006 call popup_close(winid)
1007
1008 call cursor(1, 1)
1009 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001010 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001011 \ line: 'cursor+2',
1012 \ col: 'cursor+1',
1013 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001014 redraw
1015 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1016 call assert_equal('xvimxxxxxxxxxxxxx', line)
1017 call popup_close(winid)
1018
1019 call cursor(3, 3)
1020 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001021 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001022 \ line: 'cursor-2',
1023 \ col: 'cursor-1',
1024 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001025 redraw
1026 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1027 call assert_equal('xvimxxxxxxxxxxxxx', line)
1028 call popup_close(winid)
1029
Bram Moolenaar402502d2019-05-30 22:07:36 +02001030 " just enough room above
1031 call cursor(3, 3)
1032 redraw
1033 let winid = popup_atcursor(['vim', 'is great'], {})
1034 redraw
1035 let pos = popup_getpos(winid)
1036 call assert_equal(1, pos.line)
1037 call popup_close(winid)
1038
1039 " not enough room above, popup goes below the cursor
1040 call cursor(3, 3)
1041 redraw
1042 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1043 redraw
1044 let pos = popup_getpos(winid)
1045 call assert_equal(4, pos.line)
1046 call popup_close(winid)
1047
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001048 " cursor in first line, popup in line 2
1049 call cursor(1, 1)
1050 redraw
1051 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1052 redraw
1053 let pos = popup_getpos(winid)
1054 call assert_equal(2, pos.line)
1055 call popup_close(winid)
1056
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001057 bwipe!
1058endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001059
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001060func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001061 CheckScreendump
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001062
1063 let lines =<< trim END
1064 call setline(1, range(1, 20))
1065 call setline(5, 'here is some text to hover over')
1066 set balloonevalterm
1067 set balloonexpr=BalloonExpr()
1068 set balloondelay=100
1069 func BalloonExpr()
1070 let s:winid = popup_beval([v:beval_text], {})
1071 return ''
1072 endfunc
1073 func Hover()
1074 call test_setmouse(5, 15)
1075 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1076 sleep 100m
1077 endfunc
1078 func MoveOntoPopup()
1079 call test_setmouse(4, 17)
1080 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1081 endfunc
1082 func MoveAway()
1083 call test_setmouse(5, 13)
1084 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1085 endfunc
1086 END
1087 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001088 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001089 call term_wait(buf, 100)
1090 call term_sendkeys(buf, 'j')
1091 call term_sendkeys(buf, ":call Hover()\<CR>")
1092 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1093
1094 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1095 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1096
1097 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1098 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1099
1100 " clean up
1101 call StopVimInTerminal(buf)
1102 call delete('XtestPopupBeval')
1103endfunc
1104
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001105func Test_popup_filter()
1106 new
1107 call setline(1, 'some text')
1108
1109 func MyPopupFilter(winid, c)
1110 if a:c == 'e'
1111 let g:eaten = 'e'
1112 return 1
1113 endif
1114 if a:c == '0'
1115 let g:ignored = '0'
1116 return 0
1117 endif
1118 if a:c == 'x'
1119 call popup_close(a:winid)
1120 return 1
1121 endif
1122 return 0
1123 endfunc
1124
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001125 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001126 redraw
1127
1128 " e is consumed by the filter
1129 call feedkeys('e', 'xt')
1130 call assert_equal('e', g:eaten)
1131
1132 " 0 is ignored by the filter
1133 normal $
1134 call assert_equal(9, getcurpos()[2])
1135 call feedkeys('0', 'xt')
1136 call assert_equal('0', g:ignored)
1137 call assert_equal(1, getcurpos()[2])
1138
1139 " x closes the popup
1140 call feedkeys('x', 'xt')
1141 call assert_equal('e', g:eaten)
1142 call assert_equal(-1, winbufnr(winid))
1143
1144 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001145 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001146endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001147
Bram Moolenaara42d9452019-06-15 21:46:30 +02001148func ShowDialog(key, result)
1149 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001150 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001151 \ filter: 'popup_filter_yesno',
1152 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001153 \ })
1154 redraw
1155 call feedkeys(a:key, "xt")
1156 call assert_equal(winid, s:cb_winid)
1157 call assert_equal(a:result, s:cb_res)
1158endfunc
1159
1160func Test_popup_dialog()
1161 func QuitCallback(id, res)
1162 let s:cb_winid = a:id
1163 let s:cb_res = a:res
1164 endfunc
1165
1166 let winid = ShowDialog("y", 1)
1167 let winid = ShowDialog("Y", 1)
1168 let winid = ShowDialog("n", 0)
1169 let winid = ShowDialog("N", 0)
1170 let winid = ShowDialog("x", 0)
1171 let winid = ShowDialog("X", 0)
1172 let winid = ShowDialog("\<Esc>", 0)
1173 let winid = ShowDialog("\<C-C>", -1)
1174
1175 delfunc QuitCallback
1176endfunc
1177
Bram Moolenaara730e552019-06-16 19:05:31 +02001178func ShowMenu(key, result)
1179 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001180 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001181 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001182 \ })
1183 redraw
1184 call feedkeys(a:key, "xt")
1185 call assert_equal(winid, s:cb_winid)
1186 call assert_equal(a:result, s:cb_res)
1187endfunc
1188
1189func Test_popup_menu()
1190 func QuitCallback(id, res)
1191 let s:cb_winid = a:id
1192 let s:cb_res = a:res
1193 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001194 " mapping won't be used in popup
1195 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001196
1197 let winid = ShowMenu(" ", 1)
1198 let winid = ShowMenu("j \<CR>", 2)
1199 let winid = ShowMenu("JjK \<CR>", 2)
1200 let winid = ShowMenu("jjjjjj ", 3)
1201 let winid = ShowMenu("kkk ", 1)
1202 let winid = ShowMenu("x", -1)
1203 let winid = ShowMenu("X", -1)
1204 let winid = ShowMenu("\<Esc>", -1)
1205 let winid = ShowMenu("\<C-C>", -1)
1206
1207 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001208 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001209endfunc
1210
1211func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001212 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001213
1214 let lines =<< trim END
1215 call setline(1, range(1, 20))
1216 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001217 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001218 func MenuDone(id, res)
1219 echomsg "selected " .. a:res
1220 endfunc
1221 END
1222 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001223 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001224 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1225
1226 call term_sendkeys(buf, "jj")
1227 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1228
1229 call term_sendkeys(buf, " ")
1230 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1231
1232 " clean up
1233 call StopVimInTerminal(buf)
1234 call delete('XtestPopupMenu')
1235endfunc
1236
Bram Moolenaarf914a332019-07-20 15:09:56 +02001237func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001238 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001239
1240 let lines =<< trim END
1241 call setline(1, range(1, 20))
1242 hi PopupSelected ctermbg=green
1243 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1244 func MenuDone(id, res)
1245 echomsg "selected " .. a:res
1246 endfunc
1247 END
1248 call writefile(lines, 'XtestPopupNarrowMenu')
1249 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1250 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1251
1252 " clean up
1253 call term_sendkeys(buf, "x")
1254 call StopVimInTerminal(buf)
1255 call delete('XtestPopupNarrowMenu')
1256endfunc
1257
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001258func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001259 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001260
1261 " Create a popup without title or border, a line of padding will be added to
1262 " put the title on.
1263 let lines =<< trim END
1264 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001265 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001266 END
1267 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001268 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001269 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1270
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001271 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1272 call term_sendkeys(buf, ":\<CR>")
1273 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1274
1275 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1276 call term_sendkeys(buf, ":\<CR>")
1277 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1278
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001279 " clean up
1280 call StopVimInTerminal(buf)
1281 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001282
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001283 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001284 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001285 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001286 call assert_equal('Another Title', popup_getoptions(winid).title)
1287
1288 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001289endfunc
1290
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001291func Test_popup_close_callback()
1292 func PopupDone(id, result)
1293 let g:result = a:result
1294 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001295 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001296 redraw
1297 call popup_close(winid, 'done')
1298 call assert_equal('done', g:result)
1299endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001300
1301func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001302 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001303 redraw
1304 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001305 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001306 call assert_equal(5, pos.height)
1307
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001308 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001309 redraw
1310 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001311 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001312 call assert_equal(3, pos.height)
1313endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001314
1315func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001316 CheckScreendump
1317
Bram Moolenaar988c4332019-06-02 14:12:11 +02001318 " +-----------------------------+
1319 " | | |
1320 " | | |
1321 " | | |
1322 " | line1 |
1323 " |------------line2------------|
1324 " | line3 |
1325 " | line4 |
1326 " | |
1327 " | |
1328 " +-----------------------------+
1329 let lines =<< trim END
1330 only
1331 split
1332 vsplit
1333 let info_window1 = getwininfo()[0]
1334 let line = info_window1['height']
1335 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001336 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001337 \ line : line,
1338 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001339 \ })
1340 END
1341 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001342 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001343 call term_sendkeys(buf, "\<C-W>w")
1344 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1345
1346 " clean up
1347 call StopVimInTerminal(buf)
1348 call delete('XtestPopupBehind')
1349endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001350
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001351func s:VerifyPosition(p, msg, line, col, width, height)
1352 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1353 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1354 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1355 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001356endfunc
1357
1358func Test_popup_position_adjust()
1359 " Anything placed past 2 cells from of the right of the screen is moved to the
1360 " left.
1361 "
1362 " When wrapping is disabled, we also shift to the left to display on the
1363 " screen, unless fixed is set.
1364
1365 " Entries for cases which don't vary based on wrapping.
1366 " Format is per tests described below
1367 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001368 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1369 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1370 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1371 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1372 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1373 \
1374 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1375 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1376 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1377 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1378 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1379 \
1380 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1381 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1382 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1383 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1384 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1385 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001386
1387 " these test groups are dicts with:
1388 " - comment: something to identify the group of tests by
1389 " - options: dict of options to merge with the row/col in tests
1390 " - tests: list of cases. Each one is a list with elements:
1391 " - text
1392 " - row
1393 " - col
1394 " - expected row
1395 " - expected col
1396 " - expected width
1397 " - expected height
1398 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001399 \ #{
1400 \ comment: 'left-aligned with wrapping',
1401 \ options: #{
1402 \ wrap: 1,
1403 \ pos: 'botleft',
1404 \ },
1405 \ tests: both_wrap_tests + [
1406 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1407 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1408 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1409 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1410 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1411 \ ],
1412 \ },
1413 \ #{
1414 \ comment: 'left aligned without wrapping',
1415 \ options: #{
1416 \ wrap: 0,
1417 \ pos: 'botleft',
1418 \ },
1419 \ tests: both_wrap_tests + [
1420 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1421 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1422 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1423 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1424 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1425 \ ],
1426 \ },
1427 \ #{
1428 \ comment: 'left aligned with fixed position',
1429 \ options: #{
1430 \ wrap: 0,
1431 \ fixed: 1,
1432 \ pos: 'botleft',
1433 \ },
1434 \ tests: both_wrap_tests + [
1435 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1436 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1437 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1438 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1439 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1440 \ ],
1441 \ },
1442 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001443
1444 for test_group in tests
1445 for test in test_group.tests
1446 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001447 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001448 \ line: line,
1449 \ col: col,
1450 \ }
1451 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001452
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001453 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001454
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001455 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001456 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1457 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001458 endfor
1459 endfor
1460
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001461 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001462 %bwipe!
1463endfunc
1464
Bram Moolenaar3397f742019-06-02 18:40:06 +02001465func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001466 " width of screen
1467 let X = join(map(range(&columns), {->'X'}), '')
1468
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001469 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1470 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001471
1472 redraw
1473 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1474 call assert_equal(X, line)
1475
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001476 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001477 redraw
1478
1479 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001480 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1481 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001482
1483 redraw
1484 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1485 call assert_equal(X, line)
1486
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001487 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001488 redraw
1489
1490 " Extend so > window width
1491 let X .= 'x'
1492
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001493 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1494 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001495
1496 redraw
1497 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1498 call assert_equal(X[ : -2 ], line)
1499
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001500 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001501 redraw
1502
1503 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001504 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1505 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001506
1507 redraw
1508 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1509 call assert_equal(X[ : -2 ], line)
1510
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001511 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001512 redraw
1513
1514 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001515 let p = popup_create(X,
1516 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1517 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001518
1519 redraw
1520 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1521 let e_line = ' ' . X[ 1 : -2 ]
1522 call assert_equal(e_line, line)
1523
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001524 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001525 redraw
1526
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001527 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001528 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001529endfunc
1530
1531func Test_popup_moved()
1532 new
1533 call test_override('char_avail', 1)
1534 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1535
1536 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001537 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001538 redraw
1539 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001540 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001541 " trigger the check for last_cursormoved by going into insert mode
1542 call feedkeys("li\<Esc>", 'xt')
1543 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001544 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001545
1546 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001547 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001548 redraw
1549 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001550 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001551 call feedkeys("hi\<Esc>", 'xt')
1552 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001553 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001554
1555 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001556 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001557 redraw
1558 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001559 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001560 call feedkeys("li\<Esc>", 'xt')
1561 call assert_equal(1, popup_getpos(winid).visible)
1562 call feedkeys("ei\<Esc>", 'xt')
1563 call assert_equal(1, popup_getpos(winid).visible)
1564 call feedkeys("eli\<Esc>", 'xt')
1565 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001566 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001567
Bram Moolenaar17627312019-06-02 19:53:44 +02001568 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001569 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001570 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001571 redraw
1572 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001573 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001574 call feedkeys("eli\<Esc>", 'xt')
1575 call assert_equal(1, popup_getpos(winid).visible)
1576 call feedkeys("wi\<Esc>", 'xt')
1577 call assert_equal(1, popup_getpos(winid).visible)
1578 call feedkeys("Eli\<Esc>", 'xt')
1579 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001580 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001581
1582 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001583 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001584 redraw
1585 call assert_equal(1, popup_getpos(winid).visible)
1586 call feedkeys("eli\<Esc>", 'xt')
1587 call feedkeys("ei\<Esc>", 'xt')
1588 call assert_equal(1, popup_getpos(winid).visible)
1589 call feedkeys("eli\<Esc>", 'xt')
1590 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001591 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001592
1593 bwipe!
1594 call test_override('ALL', 0)
1595endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001596
1597func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001598 CheckFeature timers
1599 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001600
1601 call writefile([
1602 \ "call setline(1, range(1, 20))",
1603 \ "hi Notification ctermbg=lightblue",
1604 \ "call popup_notification('first notification', {})",
1605 \], 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001606 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001607 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1608
1609 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001610 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1611 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001612 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1613
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001614 " clean up
1615 call StopVimInTerminal(buf)
1616 call delete('XtestNotifications')
1617endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001618
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001619func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001620 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001621
1622 let lines =<< trim END
1623 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001624 hi ScrollThumb ctermbg=blue
1625 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001626 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001627 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001628 \ minwidth: 8,
1629 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001630 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001631 func ScrollUp()
1632 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1633 endfunc
1634 func ScrollDown()
1635 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1636 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001637 func ClickTop()
1638 call feedkeys("\<F4>\<LeftMouse>", "xt")
1639 endfunc
1640 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001641 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001642 call feedkeys("\<F5>\<LeftMouse>", "xt")
1643 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001644 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001645 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1646 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001647 END
1648 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001649 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001650 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1651
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001652 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001653 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001654 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1655
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001656 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001657 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001658 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1659
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001660 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001661 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001662 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1663
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001664 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb'})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001665 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1666 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1667
1668 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1669 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1670
1671 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001672 " wait a bit, otherwise it fails sometimes (double click recognized?)
1673 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001674 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1675 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1676
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001677 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1678 sleep 100m
1679 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1680 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1681
1682 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1683 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1684
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001685 " clean up
1686 call StopVimInTerminal(buf)
1687 call delete('XtestPopupScroll')
1688endfunc
1689
Bram Moolenaar437a7462019-07-05 20:17:22 +02001690func Test_popup_fitting_scrollbar()
1691 " this was causing a crash, divide by zero
1692 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001693 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001694 \ scrollbar: 1,
1695 \ maxwidth: 10,
1696 \ maxheight: 5,
1697 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001698 redraw
1699 call popup_clear()
1700endfunc
1701
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001702func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001703 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001704
1705 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001706 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001707 let p = popup_create('test', opts)
1708 call popup_settext(p, 'this is a text')
1709 END
1710
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001711 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001712 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001713 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1714
1715 " Setting to empty string clears it
1716 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1717 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1718
1719 " Setting a list
1720 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1721 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1722
1723 " Shrinking with a list
1724 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1725 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1726
1727 " Growing with a list
1728 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1729 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1730
1731 " Empty list clears
1732 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1733 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1734
1735 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001736 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001737 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1738
1739 " clean up
1740 call StopVimInTerminal(buf)
1741 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001742endfunc
1743
1744func Test_popup_hidden()
1745 new
1746
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001747 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001748 redraw
1749 call assert_equal(0, popup_getpos(winid).visible)
1750 call popup_close(winid)
1751
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001752 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001753 redraw
1754 call assert_equal(0, popup_getpos(winid).visible)
1755 call popup_close(winid)
1756
1757 func QuitCallback(id, res)
1758 let s:cb_winid = a:id
1759 let s:cb_res = a:res
1760 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001761 let winid = popup_dialog('make a choice', #{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001762 \ filter: 'popup_filter_yesno',
1763 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001764 \ })
1765 redraw
1766 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001767 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1768 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001769 exe "normal anot used by filter\<Esc>"
1770 call assert_equal('not used by filter', getline(1))
1771
1772 call popup_show(winid)
1773 call feedkeys('y', "xt")
1774 call assert_equal(1, s:cb_res)
1775
1776 bwipe!
1777 delfunc QuitCallback
1778endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001779
1780" Test options not checked elsewhere
1781func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001782 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001783 let options = popup_getoptions(winid)
1784 call assert_equal(1, options.wrap)
1785 call assert_equal(0, options.drag)
1786 call assert_equal('Beautiful', options.highlight)
1787
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001788 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001789 let options = popup_getoptions(winid)
1790 call assert_equal(0, options.wrap)
1791 call assert_equal(1, options.drag)
1792 call assert_equal('Another', options.highlight)
1793
1794 call popup_close(winid)
1795endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001796
1797func Test_popupwin_garbage_collect()
1798 func MyPopupFilter(x, winid, c)
1799 " NOP
1800 endfunc
1801
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001802 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001803 call test_garbagecollect_now()
1804 redraw
1805 " Must not crach caused by invalid memory access
1806 call feedkeys('j', 'xt')
1807 call assert_true(v:true)
1808
1809 call popup_close(winid)
1810 delfunc MyPopupFilter
1811endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001812
1813func Test_popupwin_with_buffer()
1814 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1815 let buf = bufadd('XsomeFile')
1816 call assert_equal(0, bufloaded(buf))
1817 let winid = popup_create(buf, {})
1818 call assert_notequal(0, winid)
1819 let pos = popup_getpos(winid)
1820 call assert_equal(2, pos.height)
1821 call assert_equal(1, bufloaded(buf))
1822 call popup_close(winid)
1823 call assert_equal({}, popup_getpos(winid))
1824 call assert_equal(1, bufloaded(buf))
1825 exe 'bwipe! ' .. buf
Bram Moolenaar7866b872019-07-01 22:21:01 +02001826
1827 edit test_popupwin.vim
1828 let winid = popup_create(bufnr(''), {})
1829 redraw
1830 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001831 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001832endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02001833
1834func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001835 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001836 \ maxwidth: 40,
1837 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02001838 \ })
1839 for top in range(1, 20)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001840 call popup_setoptions(winid, #{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02001841 redraw
1842 call assert_equal(19, popup_getpos(winid).width)
1843 endfor
1844 call popup_clear()
1845endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02001846
1847func Test_popupwin_buf_close()
1848 let buf = bufadd('Xtestbuf')
1849 call bufload(buf)
1850 call setbufline(buf, 1, ['just', 'some', 'lines'])
1851 let winid = popup_create(buf, {})
1852 redraw
1853 call assert_equal(3, popup_getpos(winid).height)
1854 let bufinfo = getbufinfo(buf)[0]
1855 call assert_equal(1, bufinfo.changed)
1856 call assert_equal(0, bufinfo.hidden)
1857 call assert_equal(0, bufinfo.listed)
1858 call assert_equal(1, bufinfo.loaded)
1859 call assert_equal([], bufinfo.windows)
1860 call assert_equal([winid], bufinfo.popups)
1861
1862 call popup_close(winid)
1863 call assert_equal({}, popup_getpos(winid))
1864 let bufinfo = getbufinfo(buf)[0]
1865 call assert_equal(1, bufinfo.changed)
1866 call assert_equal(1, bufinfo.hidden)
1867 call assert_equal(0, bufinfo.listed)
1868 call assert_equal(1, bufinfo.loaded)
1869 call assert_equal([], bufinfo.windows)
1870 call assert_equal([], bufinfo.popups)
1871 exe 'bwipe! ' .. buf
1872endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02001873
1874func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001875 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02001876
1877 let lines =<< trim END
1878 call setline(1, range(1, 10))
1879 hi ScrollThumb ctermbg=blue
1880 hi ScrollBar ctermbg=red
1881 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001882 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001883 \ maxwidth: 10,
1884 \ maxheight: 3,
1885 \ pos : 'topleft',
1886 \ col : a:col,
1887 \ line : a:line,
1888 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02001889 \ })
1890 endfunc
1891 call PopupMenu(['x'], 1, 1)
1892 call PopupMenu(['123456789|'], 1, 16)
1893 call PopupMenu(['123456789|' .. ' '], 7, 1)
1894 call PopupMenu([repeat('123456789|', 100)], 7, 16)
1895 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
1896 END
1897 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001898 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02001899 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
1900
1901 " close the menu popupwin.
1902 call term_sendkeys(buf, " ")
1903 call term_sendkeys(buf, " ")
1904 call term_sendkeys(buf, " ")
1905 call term_sendkeys(buf, " ")
1906 call term_sendkeys(buf, " ")
1907
1908 " clean up
1909 call StopVimInTerminal(buf)
1910 call delete('XtestPopupMenuMaxWidth')
1911endfunc
1912
Bram Moolenaara901a372019-07-13 16:38:50 +02001913func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001914 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02001915
1916 let lines =<< trim END
1917 call setline(1, range(1, 20))
1918 hi ScrollThumb ctermbg=blue
1919 hi ScrollBar ctermbg=red
1920 call popup_menu(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001921 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001922 \ minwidth: 8,
1923 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02001924 \ })
1925 END
1926 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001927 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02001928
1929 call term_sendkeys(buf, "j")
1930 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
1931
1932 call term_sendkeys(buf, "jjj")
1933 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
1934
1935 " if the cursor is the bottom line, it stays at the bottom line.
1936 call term_sendkeys(buf, repeat("j", 20))
1937 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
1938
1939 call term_sendkeys(buf, "kk")
1940 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
1941
1942 call term_sendkeys(buf, "k")
1943 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
1944
1945 " if the cursor is in the top line, it stays in the top line.
1946 call term_sendkeys(buf, repeat("k", 20))
1947 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
1948
1949 " close the menu popupwin.
1950 call term_sendkeys(buf, " ")
1951
1952 " clean up
1953 call StopVimInTerminal(buf)
1954 call delete('XtestPopupMenuScroll')
1955endfunc
1956
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001957func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001958 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001959
1960 let lines =<< trim END
1961 function! MyFilter(winid, key) abort
1962 if a:key == "0"
1963 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
1964 return 1
1965 endif
1966 if a:key == "G"
1967 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
1968 return 1
1969 endif
1970 if a:key == "j"
1971 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
1972 return 1
1973 endif
1974 if a:key == "k"
1975 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
1976 return 1
1977 endif
1978 if a:key == 'x'
1979 call popup_close(a:winid)
1980 return 1
1981 endif
1982 return 0
1983 endfunction
1984 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
1985 \ maxheight : 3,
1986 \ filter : 'MyFilter'
1987 \ })
1988 END
1989 call writefile(lines, 'XtestPopupMenuFilter')
1990 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
1991
1992 call term_sendkeys(buf, "j")
1993 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
1994
1995 call term_sendkeys(buf, "k")
1996 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
1997
1998 call term_sendkeys(buf, "G")
1999 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2000
2001 call term_sendkeys(buf, "0")
2002 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2003
2004 call term_sendkeys(buf, "x")
2005
2006 " clean up
2007 call StopVimInTerminal(buf)
2008 call delete('XtestPopupMenuFilter')
2009endfunc
2010
2011func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002012 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002013
2014 let winid = popup_create('some text', {})
2015 call assert_equal(0, popup_getoptions(winid).cursorline)
2016 call popup_close(winid)
2017
2018 let winid = popup_create('some text', #{ cursorline: 1, })
2019 call assert_equal(1, popup_getoptions(winid).cursorline)
2020 call popup_close(winid)
2021
2022 let winid = popup_create('some text', #{ cursorline: 0, })
2023 call assert_equal(0, popup_getoptions(winid).cursorline)
2024 call popup_close(winid)
2025
2026 let winid = popup_menu('some text', {})
2027 call assert_equal(1, popup_getoptions(winid).cursorline)
2028 call popup_close(winid)
2029
2030 let winid = popup_menu('some text', #{ cursorline: 1, })
2031 call assert_equal(1, popup_getoptions(winid).cursorline)
2032 call popup_close(winid)
2033
2034 let winid = popup_menu('some text', #{ cursorline: 0, })
2035 call assert_equal(0, popup_getoptions(winid).cursorline)
2036 call popup_close(winid)
2037
2038 " ---------
2039 " Pattern 1
2040 " ---------
2041 let lines =<< trim END
2042 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2043 END
2044 call writefile(lines, 'XtestPopupCursorLine')
2045 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2046 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2047 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2048 call StopVimInTerminal(buf)
2049
2050 " ---------
2051 " Pattern 2
2052 " ---------
2053 let lines =<< trim END
2054 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2055 END
2056 call writefile(lines, 'XtestPopupCursorLine')
2057 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2058 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2059 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2060 call StopVimInTerminal(buf)
2061
2062 " ---------
2063 " Pattern 3
2064 " ---------
2065 let lines =<< trim END
2066 function! MyFilter(winid, key) abort
2067 if a:key == "j"
2068 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2069 return 1
2070 endif
2071 if a:key == 'x'
2072 call popup_close(a:winid)
2073 return 1
2074 endif
2075 return 0
2076 endfunction
2077 call popup_menu(['111', '222', '333'], #{
2078 \ cursorline : 0,
2079 \ maxheight : 2,
2080 \ filter : 'MyFilter',
2081 \ })
2082 END
2083 call writefile(lines, 'XtestPopupCursorLine')
2084 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2085 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2086 call term_sendkeys(buf, "j")
2087 call term_sendkeys(buf, "j")
2088 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2089 call term_sendkeys(buf, "x")
2090 call StopVimInTerminal(buf)
2091
2092 " ---------
2093 " Pattern 4
2094 " ---------
2095 let lines =<< trim END
2096 function! MyFilter(winid, key) abort
2097 if a:key == "j"
2098 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2099 return 1
2100 endif
2101 if a:key == 'x'
2102 call popup_close(a:winid)
2103 return 1
2104 endif
2105 return 0
2106 endfunction
2107 call popup_menu(['111', '222', '333'], #{
2108 \ cursorline : 1,
2109 \ maxheight : 2,
2110 \ filter : 'MyFilter',
2111 \ })
2112 END
2113 call writefile(lines, 'XtestPopupCursorLine')
2114 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2115 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2116 call term_sendkeys(buf, "j")
2117 call term_sendkeys(buf, "j")
2118 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2119 call term_sendkeys(buf, "x")
2120 call StopVimInTerminal(buf)
2121
2122 call delete('XtestPopupCursorLine')
2123endfunc
2124
Bram Moolenaarf914a332019-07-20 15:09:56 +02002125func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002126 CheckScreendump
2127
Bram Moolenaarf914a332019-07-20 15:09:56 +02002128 call writefile([
2129 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2130 \ "another\tXtagfile\t/^this is another",
2131 \ "theword\tXtagfile\t/^theword"],
2132 \ 'Xtags')
2133 call writefile(range(1,20)
2134 \ + ['theword is here']
2135 \ + range(22, 27)
2136 \ + ['this is another place']
2137 \ + range(29, 40),
2138 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002139 call writefile(range(1,10)
2140 \ + ['searched word is here']
2141 \ + range(12, 20),
2142 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002143 let lines =<< trim END
2144 set tags=Xtags
2145 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002146 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002147 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002148 \ 'three',
2149 \ 'four',
2150 \ 'five',
2151 \ 'six',
2152 \ 'seven',
2153 \ 'find theword somewhere',
2154 \ 'nine',
2155 \ 'this is another word',
2156 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002157 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002158 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002159 END
2160 call writefile(lines, 'XtestPreviewPopup')
2161 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2162
2163 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2164 call term_sendkeys(buf, ":\<CR>")
2165 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2166
2167 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2168 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2169
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002170 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002171 call term_sendkeys(buf, ":\<CR>")
2172 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2173
2174 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2175 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2176
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002177 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2178 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002179 call term_sendkeys(buf, ":cd testdir\<CR>")
2180
2181 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002182 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002183 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2184
2185 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2186 call term_sendkeys(buf, ":\<CR>")
2187 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2188
2189 call term_sendkeys(buf, ":pclose\<CR>")
2190 call term_sendkeys(buf, ":psearch searched\<CR>")
2191 call term_sendkeys(buf, ":\<CR>")
2192 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002193
Bram Moolenaarf914a332019-07-20 15:09:56 +02002194 call StopVimInTerminal(buf)
2195 call delete('Xtags')
2196 call delete('Xtagfile')
2197 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002198 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002199endfunc
2200
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002201func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002202 let lines =<< trim END
2203 set completeopt+=preview,popup
2204 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002205 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002206
2207 func CompleteFuncDict(findstart, base)
2208 if a:findstart
2209 if col('.') > 10
2210 return col('.') - 10
2211 endif
2212 return 0
2213 endif
2214
2215 return {
2216 \ 'words': [
2217 \ {
2218 \ 'word': 'aword',
2219 \ 'abbr': 'wrd',
2220 \ 'menu': 'extra text',
2221 \ 'info': 'words are cool',
2222 \ 'kind': 'W',
2223 \ 'user_data': 'test'
2224 \ },
2225 \ {
2226 \ 'word': 'anotherword',
2227 \ 'abbr': 'anotwrd',
2228 \ 'menu': 'extra text',
2229 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2230 \ 'kind': 'W',
2231 \ 'user_data': 'notest'
2232 \ },
2233 \ {
2234 \ 'word': 'noinfo',
2235 \ 'abbr': 'noawrd',
2236 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002237 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002238 \ 'kind': 'W',
2239 \ 'user_data': 'notest'
2240 \ },
2241 \ {
2242 \ 'word': 'thatword',
2243 \ 'abbr': 'thatwrd',
2244 \ 'menu': 'extra text',
2245 \ 'info': 'that word is cool',
2246 \ 'kind': 'W',
2247 \ 'user_data': 'notest'
2248 \ },
2249 \ ]
2250 \ }
2251 endfunc
2252 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002253 func ChangeColor()
2254 let id = popup_findinfo()
2255 call popup_setoptions(id, #{highlight: 'InfoPopup'})
2256 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002257 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002258 return lines
2259endfunc
2260
2261func Test_popupmenu_info_border()
2262 CheckScreendump
2263
2264 let lines = Get_popupmenu_lines()
2265 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002266 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002267
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002268 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2269 call term_wait(buf, 50)
2270
2271 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2272 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2273
2274 call term_sendkeys(buf, "\<C-N>")
2275 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2276
2277 call term_sendkeys(buf, "\<C-N>")
2278 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2279
2280 call term_sendkeys(buf, "\<C-N>\<C-N>")
2281 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2282
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002283 " info on the left with scrollbar
2284 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2285 call term_sendkeys(buf, "\<C-N>\<C-N>")
2286 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2287
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002288 call StopVimInTerminal(buf)
2289 call delete('XtestInfoPopup')
2290endfunc
2291
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002292func Test_popupmenu_info_noborder()
2293 CheckScreendump
2294
2295 let lines = Get_popupmenu_lines()
2296 call add(lines, 'set completepopup=height:4,border:off')
2297 call writefile(lines, 'XtestInfoPopupNb')
2298
2299 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2300 call term_wait(buf, 50)
2301
2302 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2303 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2304
2305 call StopVimInTerminal(buf)
2306 call delete('XtestInfoPopupNb')
2307endfunc
2308
Bram Moolenaar258cef52019-08-21 17:29:29 +02002309func Test_popupmenu_info_align_menu()
2310 CheckScreendump
2311
2312 let lines = Get_popupmenu_lines()
2313 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2314 call writefile(lines, 'XtestInfoPopupNb')
2315
2316 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2317 call term_wait(buf, 50)
2318
2319 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2320 call term_sendkeys(buf, "\<C-N>")
2321 call term_sendkeys(buf, "\<C-N>")
2322 call term_sendkeys(buf, "\<C-N>")
2323 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2324
2325 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2326 call term_sendkeys(buf, "\<C-N>")
2327 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2328
2329 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002330 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002331 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2332 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2333 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2334
2335 call StopVimInTerminal(buf)
2336 call delete('XtestInfoPopupNb')
2337endfunc
2338
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002339func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02002340 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002341 call popup_clear()
2342 let winid = popup_notification('nothing wrong', {})
2343 call assert_equal(bufnr, winbufnr(winid))
2344 call popup_clear()
2345endfunc
2346
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002347" vim: shiftwidth=2 sts=2