blob: 26ab870a4c75304f8fc62a4237113b7339dfa880 [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 Moolenaar4c6d9042019-07-16 22:04:02 +0200346 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200347 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200348 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200349 \ border: [],
350 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200351 \ })
352 func Dragit()
353 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
354 endfunc
355 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
356 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200357 func Resize()
358 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
359 endfunc
360 map <silent> <F5> :call test_setmouse(6, 41)<CR>
361 map <silent> <F6> :call test_setmouse(7, 45)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200362 END
363 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200364 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200365 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
366
367 call term_sendkeys(buf, ":call Dragit()\<CR>")
368 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
369
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200370 call term_sendkeys(buf, ":call Resize()\<CR>")
371 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
372
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200373 " clean up
374 call StopVimInTerminal(buf)
375 call delete('XtestPopupDrag')
376endfunc
377
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200378func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200379 CheckScreendump
380
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200381 let lines =<< trim END
382 call setline(1, range(1, 20))
383 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200384 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200385 \ close: 'button',
386 \ border: [],
387 \ line: 1,
388 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200389 \ })
390 func CloseMsg(id, result)
391 echomsg 'Popup closed with ' .. a:result
392 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200393 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200394 \ close: 'click',
395 \ line: 3,
396 \ col: 15,
397 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200398 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200399 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200400 \ close: 'button',
401 \ line: 5,
402 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200403 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200404 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200405 \ close: 'button',
406 \ padding: [],
407 \ line: 5,
408 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200409 \ })
410 func CloseWithX()
411 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
412 endfunc
413 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
414 func CloseWithClick()
415 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
416 endfunc
417 map <silent> <F4> :call test_setmouse(3, 17)<CR>
418 END
419 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200420 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200421 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
422
423 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
424 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
425
426 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
427 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
428
429 " clean up
430 call StopVimInTerminal(buf)
431 call delete('XtestPopupClose')
432endfunction
433
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200434func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200435 CheckScreendump
436
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200437 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200438 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200439 hi PopupColor ctermbg=lightgrey
440 let winid = popup_create([
441 \ 'some text',
442 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200443 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200444 \ line: 1,
445 \ col: 10,
446 \ wrap: 0,
447 \ fixed: 1,
448 \ zindex: 90,
449 \ padding: [],
450 \ highlight: 'PopupColor',
451 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200452 call popup_create([
453 \ 'xxxxxxxxx',
454 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200455 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200456 \ line: 3,
457 \ col: 18,
458 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200459 let winidb = popup_create([
460 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200461 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200462 \ line: 7,
463 \ col: 10,
464 \ wrap: 0,
465 \ fixed: 1,
466 \ close: 'button',
467 \ zindex: 90,
468 \ padding: [],
469 \ border: [],
470 \ 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 +0200471 END
472 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200473 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200474 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
475
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200476 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
477 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200478 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200479 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
480
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200481 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
482 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200483 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200484 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
485
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200486 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
487 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<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_4', {})
490
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200491 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
492 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200493 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200494 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
495
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200496 " clean up
497 call StopVimInTerminal(buf)
498 call delete('XtestPopupMask')
499endfunc
500
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200501func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200502 CheckScreendump
503 CheckFeature clipboard_working
504
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200505 " create a popup with some text to be selected
506 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200507 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200508 call setline(1, range(1, 20))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200509 let winid = popup_create(['the word', 'some more', 'several words here'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200510 \ drag: 1,
511 \ border: [],
512 \ line: 3,
513 \ col: 10,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200514 \ })
515 func Select1()
516 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
517 endfunc
518 map <silent> <F3> :call test_setmouse(4, 15)<CR>
519 map <silent> <F4> :call test_setmouse(6, 23)<CR>
520 END
521 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200522 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200523 call term_sendkeys(buf, ":call Select1()\<CR>")
524 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
525
526 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
527 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200528 " clean the command line, sometimes it still shows a command
529 call term_sendkeys(buf, ":\<esc>")
530
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200531 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
532
533 " clean up
534 call StopVimInTerminal(buf)
535 call delete('XtestPopupSelect')
536endfunc
537
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200538func Test_popup_in_tab()
539 " default popup is local to tab, not visible when in other tab
540 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200541 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200542 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200543 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200544 tabnew
545 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200546 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200547 quit
548 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200549
550 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200551 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200552 " buffer is gone now
553 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200554
555 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200556 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200557 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200558 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200559 tabnew
560 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200561 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200562 quit
563 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200564 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200565
566 " create popup in other tab
567 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200568 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200569 call assert_equal(0, popup_getpos(winid).visible)
570 call assert_equal(1, popup_getoptions(winid).tabpage)
571 quit
572 call assert_equal(1, popup_getpos(winid).visible)
573 call assert_equal(0, popup_getoptions(winid).tabpage)
574 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200575endfunc
576
577func Test_popup_valid_arguments()
578 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200579 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200580 let pos = popup_getpos(winid)
581 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200582 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200583
584 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200585 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200586 let pos = popup_getpos(winid)
587 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200588 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200589
590 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200591 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200592 let pos = popup_getpos(winid)
593 let around = (&columns - pos.width) / 2
594 call assert_inrange(around - 1, around + 1, pos.col)
595 let around = (&lines - pos.height) / 2
596 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200597 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200598endfunc
599
600func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200601 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200602 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200603 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200604 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200605
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200606 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200607 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200608 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200609 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200610 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200611 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200612 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200613 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200614
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200615 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200616 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200617 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200618 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200619 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200620 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200621 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200622 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200623
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200624 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200625 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200626 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200627 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200628 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200629 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200630 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200631 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200632 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
633 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200634 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200635 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200636
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200637 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200638 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200639 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200640 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200641 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200642 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200643 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
644 call popup_clear()
645 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200646 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200647 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200648endfunc
649
Bram Moolenaareea16992019-05-31 17:34:48 +0200650func Test_win_execute_closing_curwin()
651 split
652 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200653 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200654 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200655endfunc
656
657func Test_win_execute_not_allowed()
658 let winid = popup_create('some text', {})
659 call assert_fails('call win_execute(winid, "split")', 'E994:')
660 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
661 call assert_fails('call win_execute(winid, "close")', 'E994:')
662 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200663 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200664 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
665 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
666 call assert_fails('call win_execute(winid, "next")', 'E994:')
667 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
668 call assert_fails('call win_execute(winid, "buf")', 'E994:')
669 call assert_fails('call win_execute(winid, "edit")', 'E994:')
670 call assert_fails('call win_execute(winid, "enew")', 'E994:')
671 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
672 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
673 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
674 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200675 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200676endfunc
677
Bram Moolenaar402502d2019-05-30 22:07:36 +0200678func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200679 CheckScreendump
680
Bram Moolenaar402502d2019-05-30 22:07:36 +0200681 let lines =<< trim END
682 call setline(1, range(1, 100))
683 let winid = popup_create(
684 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200685 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200686 END
687 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200688 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200689 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
690
691 " clean up
692 call StopVimInTerminal(buf)
693 call delete('XtestPopup')
694endfunc
695
696func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200697 CheckScreendump
698
Bram Moolenaar402502d2019-05-30 22:07:36 +0200699 let lines =<< trim END
700 call setline(1, range(1, 100))
701 let winid = popup_create(
702 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200703 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200704 END
705 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200706 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200707 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
708
709 " clean up
710 call StopVimInTerminal(buf)
711 call delete('XtestPopup')
712endfunc
713
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200714func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200715 CheckScreendump
716
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200717 let lines =<< trim END
718 set showbreak=>>\
719 call setline(1, range(1, 20))
720 let winid = popup_dialog(
721 \ 'a long line here',
722 \ #{filter: 'popup_filter_yesno'})
723 END
724 call writefile(lines, 'XtestPopupShowbreak')
725 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
726 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
727
728 " clean up
729 call term_sendkeys(buf, "y")
730 call StopVimInTerminal(buf)
731 call delete('XtestPopupShowbreak')
732endfunc
733
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200734func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200735 CheckFeature timers
736
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200737 topleft vnew
738 call setline(1, 'hello')
739
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200740 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200741 \ line: 1,
742 \ col: 1,
743 \ minwidth: 20,
744 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200745 \})
746 redraw
747 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
748 call assert_equal('world', line)
749
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200750 call assert_equal(winid, popup_locate(1, 1))
751 call assert_equal(winid, popup_locate(1, 20))
752 call assert_equal(0, popup_locate(1, 21))
753 call assert_equal(0, popup_locate(2, 1))
754
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200755 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200756 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200757 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
758 call assert_equal('hello', line)
759
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200760 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200761 \ line: &lines,
762 \ col: 10,
763 \ minwidth: 20,
764 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200765 \})
766 redraw
767 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
768 call assert_match('.*on the command line.*', line)
769
770 sleep 700m
771 redraw
772 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
773 call assert_notmatch('.*on the command line.*', line)
774
775 bwipe!
776endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200777
778func Test_popup_hide()
779 topleft vnew
780 call setline(1, 'hello')
781
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200782 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200783 \ line: 1,
784 \ col: 1,
785 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200786 \})
787 redraw
788 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
789 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200790 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200791 " buffer is still listed and active
792 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200793
794 call popup_hide(winid)
795 redraw
796 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
797 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200798 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200799 " buffer is still listed but hidden
800 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200801
802 call popup_show(winid)
803 redraw
804 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
805 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200806 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200807
808
809 call popup_close(winid)
810 redraw
811 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
812 call assert_equal('hello', line)
813
814 " error is given for existing non-popup window
815 call assert_fails('call popup_hide(win_getid())', 'E993:')
816
817 " no error non-existing window
818 call popup_hide(1234234)
819 call popup_show(41234234)
820
821 bwipe!
822endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200823
824func Test_popup_move()
825 topleft vnew
826 call setline(1, 'hello')
827
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200828 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200829 \ line: 1,
830 \ col: 1,
831 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200832 \})
833 redraw
834 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
835 call assert_equal('world ', line)
836
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200837 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200838 redraw
839 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
840 call assert_equal('hello ', line)
841 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
842 call assert_equal('~world', line)
843
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200844 call popup_move(winid, #{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200845 redraw
846 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
847 call assert_equal('hworld', line)
848
849 call popup_close(winid)
850
851 bwipe!
852endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200853
Bram Moolenaar402502d2019-05-30 22:07:36 +0200854func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200855 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200856 \ line: 2,
857 \ col: 3,
858 \ minwidth: 10,
859 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200860 \})
861 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200862 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200863 call assert_equal(2, res.line)
864 call assert_equal(3, res.col)
865 call assert_equal(10, res.width)
866 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200867 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200868
869 call popup_close(winid)
870endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200871
872func Test_popup_width_longest()
873 let tests = [
874 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
875 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
876 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
877 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
878 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
879 \ ]
880
881 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200882 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200883 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200884 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200885 call assert_equal(test[1], position.width)
886 call popup_close(winid)
887 endfor
888endfunc
889
890func Test_popup_wraps()
891 let tests = [
892 \ ['nowrap', 6, 1],
893 \ ['a line that wraps once', 12, 2],
894 \ ['a line that wraps two times', 12, 3],
895 \ ]
896 for test in tests
897 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200898 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200899 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200900 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200901 call assert_equal(test[1], position.width)
902 call assert_equal(test[2], position.height)
903
904 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200905 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200906 endfor
907endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200908
909func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200910 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200911 \ line: 2,
912 \ col: 3,
913 \ minwidth: 10,
914 \ minheight: 11,
915 \ maxwidth: 20,
916 \ maxheight: 21,
917 \ zindex: 100,
918 \ time: 5000,
919 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200920 \})
921 redraw
922 let res = popup_getoptions(winid)
923 call assert_equal(2, res.line)
924 call assert_equal(3, res.col)
925 call assert_equal(10, res.minwidth)
926 call assert_equal(11, res.minheight)
927 call assert_equal(20, res.maxwidth)
928 call assert_equal(21, res.maxheight)
929 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200930 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +0200931 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200932 if has('timers')
933 call assert_equal(5000, res.time)
934 endif
935 call popup_close(winid)
936
937 let winid = popup_create('hello', {})
938 redraw
939 let res = popup_getoptions(winid)
940 call assert_equal(0, res.line)
941 call assert_equal(0, res.col)
942 call assert_equal(0, res.minwidth)
943 call assert_equal(0, res.minheight)
944 call assert_equal(0, res.maxwidth)
945 call assert_equal(0, res.maxheight)
946 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200947 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200948 if has('timers')
949 call assert_equal(0, res.time)
950 endif
951 call popup_close(winid)
952 call assert_equal({}, popup_getoptions(winid))
953endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200954
955func Test_popup_option_values()
956 new
957 " window-local
958 setlocal number
959 setlocal nowrap
960 " buffer-local
961 setlocal omnifunc=Something
962 " global/buffer-local
963 setlocal path=/there
964 " global/window-local
965 setlocal scrolloff=9
966
967 let winid = popup_create('hello', {})
968 call assert_equal(0, getwinvar(winid, '&number'))
969 call assert_equal(1, getwinvar(winid, '&wrap'))
970 call assert_equal('', getwinvar(winid, '&omnifunc'))
971 call assert_equal(&g:path, getwinvar(winid, '&path'))
972 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
973
974 call popup_close(winid)
975 bwipe
976endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200977
978func Test_popup_atcursor()
979 topleft vnew
980 call setline(1, [
981 \ 'xxxxxxxxxxxxxxxxx',
982 \ 'xxxxxxxxxxxxxxxxx',
983 \ 'xxxxxxxxxxxxxxxxx',
984 \])
985
986 call cursor(2, 2)
987 redraw
988 let winid = popup_atcursor('vim', {})
989 redraw
990 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
991 call assert_equal('xvimxxxxxxxxxxxxx', line)
992 call popup_close(winid)
993
994 call cursor(3, 4)
995 redraw
996 let winid = popup_atcursor('vim', {})
997 redraw
998 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
999 call assert_equal('xxxvimxxxxxxxxxxx', line)
1000 call popup_close(winid)
1001
1002 call cursor(1, 1)
1003 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001004 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001005 \ line: 'cursor+2',
1006 \ col: 'cursor+1',
1007 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001008 redraw
1009 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1010 call assert_equal('xvimxxxxxxxxxxxxx', line)
1011 call popup_close(winid)
1012
1013 call cursor(3, 3)
1014 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001015 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001016 \ line: 'cursor-2',
1017 \ col: 'cursor-1',
1018 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001019 redraw
1020 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1021 call assert_equal('xvimxxxxxxxxxxxxx', line)
1022 call popup_close(winid)
1023
Bram Moolenaar402502d2019-05-30 22:07:36 +02001024 " just enough room above
1025 call cursor(3, 3)
1026 redraw
1027 let winid = popup_atcursor(['vim', 'is great'], {})
1028 redraw
1029 let pos = popup_getpos(winid)
1030 call assert_equal(1, pos.line)
1031 call popup_close(winid)
1032
1033 " not enough room above, popup goes below the cursor
1034 call cursor(3, 3)
1035 redraw
1036 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1037 redraw
1038 let pos = popup_getpos(winid)
1039 call assert_equal(4, pos.line)
1040 call popup_close(winid)
1041
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001042 " cursor in first line, popup in line 2
1043 call cursor(1, 1)
1044 redraw
1045 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1046 redraw
1047 let pos = popup_getpos(winid)
1048 call assert_equal(2, pos.line)
1049 call popup_close(winid)
1050
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001051 bwipe!
1052endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001053
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001054func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001055 CheckScreendump
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001056
1057 let lines =<< trim END
1058 call setline(1, range(1, 20))
1059 call setline(5, 'here is some text to hover over')
1060 set balloonevalterm
1061 set balloonexpr=BalloonExpr()
1062 set balloondelay=100
1063 func BalloonExpr()
1064 let s:winid = popup_beval([v:beval_text], {})
1065 return ''
1066 endfunc
1067 func Hover()
1068 call test_setmouse(5, 15)
1069 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1070 sleep 100m
1071 endfunc
1072 func MoveOntoPopup()
1073 call test_setmouse(4, 17)
1074 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1075 endfunc
1076 func MoveAway()
1077 call test_setmouse(5, 13)
1078 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1079 endfunc
1080 END
1081 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001082 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001083 call term_wait(buf, 100)
1084 call term_sendkeys(buf, 'j')
1085 call term_sendkeys(buf, ":call Hover()\<CR>")
1086 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1087
1088 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1089 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1090
1091 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1092 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1093
1094 " clean up
1095 call StopVimInTerminal(buf)
1096 call delete('XtestPopupBeval')
1097endfunc
1098
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001099func Test_popup_filter()
1100 new
1101 call setline(1, 'some text')
1102
1103 func MyPopupFilter(winid, c)
1104 if a:c == 'e'
1105 let g:eaten = 'e'
1106 return 1
1107 endif
1108 if a:c == '0'
1109 let g:ignored = '0'
1110 return 0
1111 endif
1112 if a:c == 'x'
1113 call popup_close(a:winid)
1114 return 1
1115 endif
1116 return 0
1117 endfunc
1118
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001119 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001120 redraw
1121
1122 " e is consumed by the filter
1123 call feedkeys('e', 'xt')
1124 call assert_equal('e', g:eaten)
1125
1126 " 0 is ignored by the filter
1127 normal $
1128 call assert_equal(9, getcurpos()[2])
1129 call feedkeys('0', 'xt')
1130 call assert_equal('0', g:ignored)
1131 call assert_equal(1, getcurpos()[2])
1132
1133 " x closes the popup
1134 call feedkeys('x', 'xt')
1135 call assert_equal('e', g:eaten)
1136 call assert_equal(-1, winbufnr(winid))
1137
1138 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001139 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001140endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001141
Bram Moolenaara42d9452019-06-15 21:46:30 +02001142func ShowDialog(key, result)
1143 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001144 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001145 \ filter: 'popup_filter_yesno',
1146 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001147 \ })
1148 redraw
1149 call feedkeys(a:key, "xt")
1150 call assert_equal(winid, s:cb_winid)
1151 call assert_equal(a:result, s:cb_res)
1152endfunc
1153
1154func Test_popup_dialog()
1155 func QuitCallback(id, res)
1156 let s:cb_winid = a:id
1157 let s:cb_res = a:res
1158 endfunc
1159
1160 let winid = ShowDialog("y", 1)
1161 let winid = ShowDialog("Y", 1)
1162 let winid = ShowDialog("n", 0)
1163 let winid = ShowDialog("N", 0)
1164 let winid = ShowDialog("x", 0)
1165 let winid = ShowDialog("X", 0)
1166 let winid = ShowDialog("\<Esc>", 0)
1167 let winid = ShowDialog("\<C-C>", -1)
1168
1169 delfunc QuitCallback
1170endfunc
1171
Bram Moolenaara730e552019-06-16 19:05:31 +02001172func ShowMenu(key, result)
1173 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001174 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001175 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001176 \ })
1177 redraw
1178 call feedkeys(a:key, "xt")
1179 call assert_equal(winid, s:cb_winid)
1180 call assert_equal(a:result, s:cb_res)
1181endfunc
1182
1183func Test_popup_menu()
1184 func QuitCallback(id, res)
1185 let s:cb_winid = a:id
1186 let s:cb_res = a:res
1187 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001188 " mapping won't be used in popup
1189 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001190
1191 let winid = ShowMenu(" ", 1)
1192 let winid = ShowMenu("j \<CR>", 2)
1193 let winid = ShowMenu("JjK \<CR>", 2)
1194 let winid = ShowMenu("jjjjjj ", 3)
1195 let winid = ShowMenu("kkk ", 1)
1196 let winid = ShowMenu("x", -1)
1197 let winid = ShowMenu("X", -1)
1198 let winid = ShowMenu("\<Esc>", -1)
1199 let winid = ShowMenu("\<C-C>", -1)
1200
1201 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001202 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001203endfunc
1204
1205func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001206 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001207
1208 let lines =<< trim END
1209 call setline(1, range(1, 20))
1210 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001211 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001212 func MenuDone(id, res)
1213 echomsg "selected " .. a:res
1214 endfunc
1215 END
1216 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001217 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001218 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1219
1220 call term_sendkeys(buf, "jj")
1221 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1222
1223 call term_sendkeys(buf, " ")
1224 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1225
1226 " clean up
1227 call StopVimInTerminal(buf)
1228 call delete('XtestPopupMenu')
1229endfunc
1230
Bram Moolenaarf914a332019-07-20 15:09:56 +02001231func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001232 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001233
1234 let lines =<< trim END
1235 call setline(1, range(1, 20))
1236 hi PopupSelected ctermbg=green
1237 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1238 func MenuDone(id, res)
1239 echomsg "selected " .. a:res
1240 endfunc
1241 END
1242 call writefile(lines, 'XtestPopupNarrowMenu')
1243 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1244 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1245
1246 " clean up
1247 call term_sendkeys(buf, "x")
1248 call StopVimInTerminal(buf)
1249 call delete('XtestPopupNarrowMenu')
1250endfunc
1251
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001252func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001253 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001254
1255 " Create a popup without title or border, a line of padding will be added to
1256 " put the title on.
1257 let lines =<< trim END
1258 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001259 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001260 END
1261 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001262 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001263 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1264
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001265 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1266 call term_sendkeys(buf, ":\<CR>")
1267 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1268
1269 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1270 call term_sendkeys(buf, ":\<CR>")
1271 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1272
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001273 " clean up
1274 call StopVimInTerminal(buf)
1275 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001276
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001277 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001278 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001279 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001280 call assert_equal('Another Title', popup_getoptions(winid).title)
1281
1282 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001283endfunc
1284
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001285func Test_popup_close_callback()
1286 func PopupDone(id, result)
1287 let g:result = a:result
1288 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001289 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001290 redraw
1291 call popup_close(winid, 'done')
1292 call assert_equal('done', g:result)
1293endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001294
1295func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001296 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001297 redraw
1298 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001299 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001300 call assert_equal(5, pos.height)
1301
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001302 let winid = popup_create([], #{border: []})
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(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001306 call assert_equal(3, pos.height)
1307endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001308
1309func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001310 CheckScreendump
1311
Bram Moolenaar988c4332019-06-02 14:12:11 +02001312 " +-----------------------------+
1313 " | | |
1314 " | | |
1315 " | | |
1316 " | line1 |
1317 " |------------line2------------|
1318 " | line3 |
1319 " | line4 |
1320 " | |
1321 " | |
1322 " +-----------------------------+
1323 let lines =<< trim END
1324 only
1325 split
1326 vsplit
1327 let info_window1 = getwininfo()[0]
1328 let line = info_window1['height']
1329 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001330 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001331 \ line : line,
1332 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001333 \ })
1334 END
1335 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001336 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001337 call term_sendkeys(buf, "\<C-W>w")
1338 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1339
1340 " clean up
1341 call StopVimInTerminal(buf)
1342 call delete('XtestPopupBehind')
1343endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001344
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001345func s:VerifyPosition(p, msg, line, col, width, height)
1346 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1347 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1348 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1349 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001350endfunc
1351
1352func Test_popup_position_adjust()
1353 " Anything placed past 2 cells from of the right of the screen is moved to the
1354 " left.
1355 "
1356 " When wrapping is disabled, we also shift to the left to display on the
1357 " screen, unless fixed is set.
1358
1359 " Entries for cases which don't vary based on wrapping.
1360 " Format is per tests described below
1361 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001362 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1363 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1364 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1365 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1366 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1367 \
1368 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1369 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1370 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1371 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1372 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1373 \
1374 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1375 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1376 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1377 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1378 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1379 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001380
1381 " these test groups are dicts with:
1382 " - comment: something to identify the group of tests by
1383 " - options: dict of options to merge with the row/col in tests
1384 " - tests: list of cases. Each one is a list with elements:
1385 " - text
1386 " - row
1387 " - col
1388 " - expected row
1389 " - expected col
1390 " - expected width
1391 " - expected height
1392 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001393 \ #{
1394 \ comment: 'left-aligned with wrapping',
1395 \ options: #{
1396 \ wrap: 1,
1397 \ pos: 'botleft',
1398 \ },
1399 \ tests: both_wrap_tests + [
1400 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1401 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1402 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1403 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1404 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1405 \ ],
1406 \ },
1407 \ #{
1408 \ comment: 'left aligned without wrapping',
1409 \ options: #{
1410 \ wrap: 0,
1411 \ pos: 'botleft',
1412 \ },
1413 \ tests: both_wrap_tests + [
1414 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1415 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1416 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1417 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1418 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1419 \ ],
1420 \ },
1421 \ #{
1422 \ comment: 'left aligned with fixed position',
1423 \ options: #{
1424 \ wrap: 0,
1425 \ fixed: 1,
1426 \ pos: 'botleft',
1427 \ },
1428 \ tests: both_wrap_tests + [
1429 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1430 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1431 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1432 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1433 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1434 \ ],
1435 \ },
1436 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001437
1438 for test_group in tests
1439 for test in test_group.tests
1440 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001441 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001442 \ line: line,
1443 \ col: col,
1444 \ }
1445 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001446
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001447 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001448
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001449 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001450 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1451 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001452 endfor
1453 endfor
1454
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001455 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001456 %bwipe!
1457endfunc
1458
Bram Moolenaar3397f742019-06-02 18:40:06 +02001459func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001460 " width of screen
1461 let X = join(map(range(&columns), {->'X'}), '')
1462
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001463 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1464 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001465
1466 redraw
1467 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1468 call assert_equal(X, line)
1469
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001470 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001471 redraw
1472
1473 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001474 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1475 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001476
1477 redraw
1478 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1479 call assert_equal(X, line)
1480
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001481 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001482 redraw
1483
1484 " Extend so > window width
1485 let X .= 'x'
1486
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001487 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1488 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001489
1490 redraw
1491 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1492 call assert_equal(X[ : -2 ], line)
1493
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001494 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001495 redraw
1496
1497 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001498 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1499 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001500
1501 redraw
1502 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1503 call assert_equal(X[ : -2 ], line)
1504
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001505 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001506 redraw
1507
1508 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001509 let p = popup_create(X,
1510 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1511 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001512
1513 redraw
1514 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1515 let e_line = ' ' . X[ 1 : -2 ]
1516 call assert_equal(e_line, line)
1517
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001518 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001519 redraw
1520
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001521 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001522 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001523endfunc
1524
1525func Test_popup_moved()
1526 new
1527 call test_override('char_avail', 1)
1528 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1529
1530 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001531 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001532 redraw
1533 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001534 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001535 " trigger the check for last_cursormoved by going into insert mode
1536 call feedkeys("li\<Esc>", 'xt')
1537 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001538 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001539
1540 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001541 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001542 redraw
1543 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001544 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001545 call feedkeys("hi\<Esc>", 'xt')
1546 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001547 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001548
1549 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001550 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001551 redraw
1552 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001553 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001554 call feedkeys("li\<Esc>", 'xt')
1555 call assert_equal(1, popup_getpos(winid).visible)
1556 call feedkeys("ei\<Esc>", 'xt')
1557 call assert_equal(1, popup_getpos(winid).visible)
1558 call feedkeys("eli\<Esc>", 'xt')
1559 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001560 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001561
Bram Moolenaar17627312019-06-02 19:53:44 +02001562 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001563 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001564 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001565 redraw
1566 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001567 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001568 call feedkeys("eli\<Esc>", 'xt')
1569 call assert_equal(1, popup_getpos(winid).visible)
1570 call feedkeys("wi\<Esc>", 'xt')
1571 call assert_equal(1, popup_getpos(winid).visible)
1572 call feedkeys("Eli\<Esc>", 'xt')
1573 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001574 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001575
1576 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001577 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001578 redraw
1579 call assert_equal(1, popup_getpos(winid).visible)
1580 call feedkeys("eli\<Esc>", 'xt')
1581 call feedkeys("ei\<Esc>", 'xt')
1582 call assert_equal(1, popup_getpos(winid).visible)
1583 call feedkeys("eli\<Esc>", 'xt')
1584 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001585 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001586
1587 bwipe!
1588 call test_override('ALL', 0)
1589endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001590
1591func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001592 CheckFeature timers
1593 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001594
1595 call writefile([
1596 \ "call setline(1, range(1, 20))",
1597 \ "hi Notification ctermbg=lightblue",
1598 \ "call popup_notification('first notification', {})",
1599 \], 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001600 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001601 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1602
1603 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001604 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1605 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001606 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1607
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001608 " clean up
1609 call StopVimInTerminal(buf)
1610 call delete('XtestNotifications')
1611endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001612
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001613func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001614 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001615
1616 let lines =<< trim END
1617 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001618 hi ScrollThumb ctermbg=blue
1619 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001620 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001621 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001622 \ minwidth: 8,
1623 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001624 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001625 func ScrollUp()
1626 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1627 endfunc
1628 func ScrollDown()
1629 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1630 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001631 func ClickTop()
1632 call feedkeys("\<F4>\<LeftMouse>", "xt")
1633 endfunc
1634 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001635 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001636 call feedkeys("\<F5>\<LeftMouse>", "xt")
1637 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001638 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001639 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1640 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001641 END
1642 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001643 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001644 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1645
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001646 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001647 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001648 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1649
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001650 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001651 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001652 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1653
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001654 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001655 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001656 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1657
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001658 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb'})\<CR>")
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001659 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1660 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1661
1662 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1663 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1664
1665 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001666 " wait a bit, otherwise it fails sometimes (double click recognized?)
1667 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001668 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1669 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1670
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001671 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1672 sleep 100m
1673 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1674 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1675
1676 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1677 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1678
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001679 " clean up
1680 call StopVimInTerminal(buf)
1681 call delete('XtestPopupScroll')
1682endfunc
1683
Bram Moolenaar437a7462019-07-05 20:17:22 +02001684func Test_popup_fitting_scrollbar()
1685 " this was causing a crash, divide by zero
1686 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001687 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001688 \ scrollbar: 1,
1689 \ maxwidth: 10,
1690 \ maxheight: 5,
1691 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001692 redraw
1693 call popup_clear()
1694endfunc
1695
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001696func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001697 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001698
1699 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001700 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001701 let p = popup_create('test', opts)
1702 call popup_settext(p, 'this is a text')
1703 END
1704
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001705 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001706 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001707 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1708
1709 " Setting to empty string clears it
1710 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1711 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1712
1713 " Setting a list
1714 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1715 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1716
1717 " Shrinking with a list
1718 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1719 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1720
1721 " Growing with a list
1722 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1723 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1724
1725 " Empty list clears
1726 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1727 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1728
1729 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001730 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001731 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1732
1733 " clean up
1734 call StopVimInTerminal(buf)
1735 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001736endfunc
1737
1738func Test_popup_hidden()
1739 new
1740
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001741 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001742 redraw
1743 call assert_equal(0, popup_getpos(winid).visible)
1744 call popup_close(winid)
1745
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001746 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001747 redraw
1748 call assert_equal(0, popup_getpos(winid).visible)
1749 call popup_close(winid)
1750
1751 func QuitCallback(id, res)
1752 let s:cb_winid = a:id
1753 let s:cb_res = a:res
1754 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001755 let winid = popup_dialog('make a choice', #{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001756 \ filter: 'popup_filter_yesno',
1757 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001758 \ })
1759 redraw
1760 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001761 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1762 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001763 exe "normal anot used by filter\<Esc>"
1764 call assert_equal('not used by filter', getline(1))
1765
1766 call popup_show(winid)
1767 call feedkeys('y', "xt")
1768 call assert_equal(1, s:cb_res)
1769
1770 bwipe!
1771 delfunc QuitCallback
1772endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001773
1774" Test options not checked elsewhere
1775func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001776 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001777 let options = popup_getoptions(winid)
1778 call assert_equal(1, options.wrap)
1779 call assert_equal(0, options.drag)
1780 call assert_equal('Beautiful', options.highlight)
1781
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001782 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001783 let options = popup_getoptions(winid)
1784 call assert_equal(0, options.wrap)
1785 call assert_equal(1, options.drag)
1786 call assert_equal('Another', options.highlight)
1787
1788 call popup_close(winid)
1789endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001790
1791func Test_popupwin_garbage_collect()
1792 func MyPopupFilter(x, winid, c)
1793 " NOP
1794 endfunc
1795
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001796 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001797 call test_garbagecollect_now()
1798 redraw
1799 " Must not crach caused by invalid memory access
1800 call feedkeys('j', 'xt')
1801 call assert_true(v:true)
1802
1803 call popup_close(winid)
1804 delfunc MyPopupFilter
1805endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001806
1807func Test_popupwin_with_buffer()
1808 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1809 let buf = bufadd('XsomeFile')
1810 call assert_equal(0, bufloaded(buf))
1811 let winid = popup_create(buf, {})
1812 call assert_notequal(0, winid)
1813 let pos = popup_getpos(winid)
1814 call assert_equal(2, pos.height)
1815 call assert_equal(1, bufloaded(buf))
1816 call popup_close(winid)
1817 call assert_equal({}, popup_getpos(winid))
1818 call assert_equal(1, bufloaded(buf))
1819 exe 'bwipe! ' .. buf
Bram Moolenaar7866b872019-07-01 22:21:01 +02001820
1821 edit test_popupwin.vim
1822 let winid = popup_create(bufnr(''), {})
1823 redraw
1824 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02001825 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001826endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02001827
1828func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001829 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001830 \ maxwidth: 40,
1831 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02001832 \ })
1833 for top in range(1, 20)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001834 call popup_setoptions(winid, #{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02001835 redraw
1836 call assert_equal(19, popup_getpos(winid).width)
1837 endfor
1838 call popup_clear()
1839endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02001840
1841func Test_popupwin_buf_close()
1842 let buf = bufadd('Xtestbuf')
1843 call bufload(buf)
1844 call setbufline(buf, 1, ['just', 'some', 'lines'])
1845 let winid = popup_create(buf, {})
1846 redraw
1847 call assert_equal(3, popup_getpos(winid).height)
1848 let bufinfo = getbufinfo(buf)[0]
1849 call assert_equal(1, bufinfo.changed)
1850 call assert_equal(0, bufinfo.hidden)
1851 call assert_equal(0, bufinfo.listed)
1852 call assert_equal(1, bufinfo.loaded)
1853 call assert_equal([], bufinfo.windows)
1854 call assert_equal([winid], bufinfo.popups)
1855
1856 call popup_close(winid)
1857 call assert_equal({}, popup_getpos(winid))
1858 let bufinfo = getbufinfo(buf)[0]
1859 call assert_equal(1, bufinfo.changed)
1860 call assert_equal(1, bufinfo.hidden)
1861 call assert_equal(0, bufinfo.listed)
1862 call assert_equal(1, bufinfo.loaded)
1863 call assert_equal([], bufinfo.windows)
1864 call assert_equal([], bufinfo.popups)
1865 exe 'bwipe! ' .. buf
1866endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02001867
1868func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001869 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02001870
1871 let lines =<< trim END
1872 call setline(1, range(1, 10))
1873 hi ScrollThumb ctermbg=blue
1874 hi ScrollBar ctermbg=red
1875 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001876 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001877 \ maxwidth: 10,
1878 \ maxheight: 3,
1879 \ pos : 'topleft',
1880 \ col : a:col,
1881 \ line : a:line,
1882 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02001883 \ })
1884 endfunc
1885 call PopupMenu(['x'], 1, 1)
1886 call PopupMenu(['123456789|'], 1, 16)
1887 call PopupMenu(['123456789|' .. ' '], 7, 1)
1888 call PopupMenu([repeat('123456789|', 100)], 7, 16)
1889 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
1890 END
1891 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001892 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02001893 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
1894
1895 " close the menu popupwin.
1896 call term_sendkeys(buf, " ")
1897 call term_sendkeys(buf, " ")
1898 call term_sendkeys(buf, " ")
1899 call term_sendkeys(buf, " ")
1900 call term_sendkeys(buf, " ")
1901
1902 " clean up
1903 call StopVimInTerminal(buf)
1904 call delete('XtestPopupMenuMaxWidth')
1905endfunc
1906
Bram Moolenaara901a372019-07-13 16:38:50 +02001907func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001908 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02001909
1910 let lines =<< trim END
1911 call setline(1, range(1, 20))
1912 hi ScrollThumb ctermbg=blue
1913 hi ScrollBar ctermbg=red
1914 call popup_menu(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001915 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001916 \ minwidth: 8,
1917 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02001918 \ })
1919 END
1920 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001921 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02001922
1923 call term_sendkeys(buf, "j")
1924 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
1925
1926 call term_sendkeys(buf, "jjj")
1927 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
1928
1929 " if the cursor is the bottom line, it stays at the bottom line.
1930 call term_sendkeys(buf, repeat("j", 20))
1931 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
1932
1933 call term_sendkeys(buf, "kk")
1934 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
1935
1936 call term_sendkeys(buf, "k")
1937 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
1938
1939 " if the cursor is in the top line, it stays in the top line.
1940 call term_sendkeys(buf, repeat("k", 20))
1941 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
1942
1943 " close the menu popupwin.
1944 call term_sendkeys(buf, " ")
1945
1946 " clean up
1947 call StopVimInTerminal(buf)
1948 call delete('XtestPopupMenuScroll')
1949endfunc
1950
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001951func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001952 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02001953
1954 let lines =<< trim END
1955 function! MyFilter(winid, key) abort
1956 if a:key == "0"
1957 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
1958 return 1
1959 endif
1960 if a:key == "G"
1961 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
1962 return 1
1963 endif
1964 if a:key == "j"
1965 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
1966 return 1
1967 endif
1968 if a:key == "k"
1969 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
1970 return 1
1971 endif
1972 if a:key == 'x'
1973 call popup_close(a:winid)
1974 return 1
1975 endif
1976 return 0
1977 endfunction
1978 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
1979 \ maxheight : 3,
1980 \ filter : 'MyFilter'
1981 \ })
1982 END
1983 call writefile(lines, 'XtestPopupMenuFilter')
1984 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
1985
1986 call term_sendkeys(buf, "j")
1987 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
1988
1989 call term_sendkeys(buf, "k")
1990 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
1991
1992 call term_sendkeys(buf, "G")
1993 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
1994
1995 call term_sendkeys(buf, "0")
1996 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
1997
1998 call term_sendkeys(buf, "x")
1999
2000 " clean up
2001 call StopVimInTerminal(buf)
2002 call delete('XtestPopupMenuFilter')
2003endfunc
2004
2005func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002006 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002007
2008 let winid = popup_create('some text', {})
2009 call assert_equal(0, popup_getoptions(winid).cursorline)
2010 call popup_close(winid)
2011
2012 let winid = popup_create('some text', #{ cursorline: 1, })
2013 call assert_equal(1, popup_getoptions(winid).cursorline)
2014 call popup_close(winid)
2015
2016 let winid = popup_create('some text', #{ cursorline: 0, })
2017 call assert_equal(0, popup_getoptions(winid).cursorline)
2018 call popup_close(winid)
2019
2020 let winid = popup_menu('some text', {})
2021 call assert_equal(1, popup_getoptions(winid).cursorline)
2022 call popup_close(winid)
2023
2024 let winid = popup_menu('some text', #{ cursorline: 1, })
2025 call assert_equal(1, popup_getoptions(winid).cursorline)
2026 call popup_close(winid)
2027
2028 let winid = popup_menu('some text', #{ cursorline: 0, })
2029 call assert_equal(0, popup_getoptions(winid).cursorline)
2030 call popup_close(winid)
2031
2032 " ---------
2033 " Pattern 1
2034 " ---------
2035 let lines =<< trim END
2036 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2037 END
2038 call writefile(lines, 'XtestPopupCursorLine')
2039 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2040 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2041 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2042 call StopVimInTerminal(buf)
2043
2044 " ---------
2045 " Pattern 2
2046 " ---------
2047 let lines =<< trim END
2048 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2049 END
2050 call writefile(lines, 'XtestPopupCursorLine')
2051 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2052 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2053 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2054 call StopVimInTerminal(buf)
2055
2056 " ---------
2057 " Pattern 3
2058 " ---------
2059 let lines =<< trim END
2060 function! MyFilter(winid, key) abort
2061 if a:key == "j"
2062 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2063 return 1
2064 endif
2065 if a:key == 'x'
2066 call popup_close(a:winid)
2067 return 1
2068 endif
2069 return 0
2070 endfunction
2071 call popup_menu(['111', '222', '333'], #{
2072 \ cursorline : 0,
2073 \ maxheight : 2,
2074 \ filter : 'MyFilter',
2075 \ })
2076 END
2077 call writefile(lines, 'XtestPopupCursorLine')
2078 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2079 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2080 call term_sendkeys(buf, "j")
2081 call term_sendkeys(buf, "j")
2082 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2083 call term_sendkeys(buf, "x")
2084 call StopVimInTerminal(buf)
2085
2086 " ---------
2087 " Pattern 4
2088 " ---------
2089 let lines =<< trim END
2090 function! MyFilter(winid, key) abort
2091 if a:key == "j"
2092 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2093 return 1
2094 endif
2095 if a:key == 'x'
2096 call popup_close(a:winid)
2097 return 1
2098 endif
2099 return 0
2100 endfunction
2101 call popup_menu(['111', '222', '333'], #{
2102 \ cursorline : 1,
2103 \ maxheight : 2,
2104 \ filter : 'MyFilter',
2105 \ })
2106 END
2107 call writefile(lines, 'XtestPopupCursorLine')
2108 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2109 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2110 call term_sendkeys(buf, "j")
2111 call term_sendkeys(buf, "j")
2112 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2113 call term_sendkeys(buf, "x")
2114 call StopVimInTerminal(buf)
2115
2116 call delete('XtestPopupCursorLine')
2117endfunc
2118
Bram Moolenaarf914a332019-07-20 15:09:56 +02002119func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002120 CheckScreendump
2121
Bram Moolenaarf914a332019-07-20 15:09:56 +02002122 call writefile([
2123 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2124 \ "another\tXtagfile\t/^this is another",
2125 \ "theword\tXtagfile\t/^theword"],
2126 \ 'Xtags')
2127 call writefile(range(1,20)
2128 \ + ['theword is here']
2129 \ + range(22, 27)
2130 \ + ['this is another place']
2131 \ + range(29, 40),
2132 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002133 call writefile(range(1,10)
2134 \ + ['searched word is here']
2135 \ + range(12, 20),
2136 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002137 let lines =<< trim END
2138 set tags=Xtags
2139 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002140 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002141 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002142 \ 'three',
2143 \ 'four',
2144 \ 'five',
2145 \ 'six',
2146 \ 'seven',
2147 \ 'find theword somewhere',
2148 \ 'nine',
2149 \ 'this is another word',
2150 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002151 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002152 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002153 END
2154 call writefile(lines, 'XtestPreviewPopup')
2155 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2156
2157 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2158 call term_sendkeys(buf, ":\<CR>")
2159 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2160
2161 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2162 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2163
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002164 call term_sendkeys(buf, ":call popup_move(popup_getpreview(), #{col: 15})\<CR>")
2165 call term_sendkeys(buf, ":\<CR>")
2166 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2167
2168 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2169 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2170
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002171 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2172 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002173 call term_sendkeys(buf, ":cd testdir\<CR>")
2174
2175 call term_sendkeys(buf, ":pclose\<CR>")
2176 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2177
2178 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2179 call term_sendkeys(buf, ":\<CR>")
2180 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2181
2182 call term_sendkeys(buf, ":pclose\<CR>")
2183 call term_sendkeys(buf, ":psearch searched\<CR>")
2184 call term_sendkeys(buf, ":\<CR>")
2185 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002186
Bram Moolenaarf914a332019-07-20 15:09:56 +02002187 call StopVimInTerminal(buf)
2188 call delete('Xtags')
2189 call delete('Xtagfile')
2190 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002191 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002192endfunc
2193
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002194" vim: shiftwidth=2 sts=2