blob: e84527bf7ff6def6ff5c38aea5f74d8c700a696d [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature textprop
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
15 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020016 call prop_type_add('comment', #{highlight: 'Comment'})
17 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
18 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020019 call setwinvar(winid2, '&wincolor', 'PopupColor2')
20 END
21 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020022 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020023 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
24
Bram Moolenaarec583842019-05-26 14:11:23 +020025 " Add a tabpage
26 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020027 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020028 \ .. "#{text: 'other tab'},"
29 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020030 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020031 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020032 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020033 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
34
35 " switch back to first tabpage
36 call term_sendkeys(buf, "gt")
37 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
38
39 " close that tabpage
40 call term_sendkeys(buf, ":quit!\<CR>")
41 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
42
Bram Moolenaar202d9822019-06-11 21:56:30 +020043 " set 'columns' to a small value, size must be recomputed
44 call term_sendkeys(buf, ":let cols = &columns\<CR>")
45 call term_sendkeys(buf, ":set columns=12\<CR>")
46 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
47 call term_sendkeys(buf, ":let &columns = cols\<CR>")
48
Bram Moolenaar17146962019-05-30 00:12:11 +020049 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020050 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020051 call term_sendkeys(buf, ":redraw\<CR>")
52 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
53
Bram Moolenaar17146962019-05-30 00:12:11 +020054 " show not fitting line at bottom
55 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
56 call term_sendkeys(buf, ":redraw\<CR>")
57 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
58
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020059 " move popup over ruler
60 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020061 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020062 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020063 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
64
65 " clear all popups after moving the cursor a bit, so that ruler is updated
66 call term_sendkeys(buf, "axxx\<Esc>")
67 call term_wait(buf)
68 call term_sendkeys(buf, "0")
69 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020070 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020071 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
72
Bram Moolenaar4d784b22019-05-25 19:51:39 +020073 " clean up
74 call StopVimInTerminal(buf)
75 call delete('XtestPopup')
76endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020077
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020078func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020079 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020080
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020081 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020082 let lines =<< trim END
83 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020084 call popup_create('hello border', #{line: 2, col: 3, border: []})
85 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020086 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020087 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
88 call popup_create('paddings', #{line: 6, col: 23, padding: [1, 3, 2, 4]})
89 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
90 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020091 END
92 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
93 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020094 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020095 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
96
97 call StopVimInTerminal(buf)
98 call delete('XtestPopupBorder')
99 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200100
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200101 let lines =<< trim END
102 call setline(1, range(1, 100))
103 hi BlueColor ctermbg=lightblue
104 hi TopColor ctermbg=253
105 hi RightColor ctermbg=245
106 hi BottomColor ctermbg=240
107 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200108 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
109 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
110 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
111 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
112 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
113 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200114 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200115 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200116 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200117 END
118 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200119 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200120 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
121
Bram Moolenaarad24a712019-06-17 20:05:45 +0200122 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200123 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200124 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
125
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200126 " check multi-byte border only with 'ambiwidth' single
127 if &ambiwidth == 'single'
128 call term_sendkeys(buf, ":call MultiByte()\<CR>")
129 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
130 endif
131
Bram Moolenaar790498b2019-06-01 22:15:29 +0200132 call StopVimInTerminal(buf)
133 call delete('XtestPopupBorder')
134
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200135 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200136 \ line: 2,
137 \ core_line: 3,
138 \ col: 3,
139 \ core_col: 4,
140 \ width: 14,
141 \ core_width: 12,
142 \ height: 3,
143 \ core_height: 1,
144 \ firstline: 1,
145 \ scrollbar: 0,
146 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200147 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200148 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200149 let options = popup_getoptions(winid)
150 call assert_equal([], options.border)
151 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200152
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200153 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200154 let with_border_or_padding.width = 15
155 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200156 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200157 let options = popup_getoptions(winid)
158 call assert_false(has_key(options, "border"))
159 call assert_equal([], options.padding)
160
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200161 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200162 \ padding: [1, 2, 3, 4],
163 \ border: [4, 0, 7, 8],
164 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
165 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200166 \ })
167 let options = popup_getoptions(winid)
168 call assert_equal([1, 0, 1, 1], options.border)
169 call assert_equal([1, 2, 3, 4], options.padding)
170 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
171 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200172
Bram 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 Moolenaar8c6173c2019-08-30 22:08:34 +0200316 let winid = 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 Moolenaar8c6173c2019-08-30 22:08:34 +0200323 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
324
325 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
326 call term_sendkeys(buf, ":\<CR>")
327 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200328
329 " clean up
330 call StopVimInTerminal(buf)
331 call delete('XtestPopupFirstline')
Bram Moolenaarae943152019-06-16 22:54:14 +0200332
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200333 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200334 \ maxheight: 2,
335 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200336 \ })
337 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200338 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200339 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200340 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200341
342 let winid = popup_create(['xxx']->repeat(50), #{
343 \ maxheight: 3,
344 \ firstline: 11,
345 \ })
346 redraw
347 call assert_equal(11, popup_getoptions(winid).firstline)
348 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200349 " check line() works with popup window
350 call assert_equal(11, line('.', winid))
351 call assert_equal(50, line('$', winid))
352 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200353
354 " Normal command changes what is displayed but not "firstline"
355 call win_execute(winid, "normal! \<c-y>")
356 call assert_equal(11, popup_getoptions(winid).firstline)
357 call assert_equal(10, popup_getpos(winid).firstline)
358
359 " Making some property change applies "firstline" again
360 call popup_setoptions(winid, #{line: 4})
361 call assert_equal(11, popup_getoptions(winid).firstline)
362 call assert_equal(11, popup_getpos(winid).firstline)
363
364 " Remove "firstline" property and scroll
365 call popup_setoptions(winid, #{firstline: 0})
366 call win_execute(winid, "normal! \<c-y>")
367 call assert_equal(0, popup_getoptions(winid).firstline)
368 call assert_equal(10, popup_getpos(winid).firstline)
369
370 " Making some property change has no side effect
371 call popup_setoptions(winid, #{line: 3})
372 call assert_equal(0, popup_getoptions(winid).firstline)
373 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaarae943152019-06-16 22:54:14 +0200374
375 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200376endfunc
377
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200378func Test_popup_noscrolloff()
379 set scrolloff=5
380 let winid = popup_create(['xxx']->repeat(50), #{
381 \ maxheight: 5,
382 \ firstline: 11,
383 \ })
384 redraw
385 call assert_equal(11, popup_getoptions(winid).firstline)
386 call assert_equal(11, popup_getpos(winid).firstline)
387
388 call popup_setoptions(winid, #{firstline: 0})
389 call win_execute(winid, "normal! \<c-y>")
390 call assert_equal(0, popup_getoptions(winid).firstline)
391 call assert_equal(10, popup_getpos(winid).firstline)
392
393 call popup_close(winid)
394endfunc
395
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200396func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200397 CheckScreendump
398
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200399 " create a popup that covers the command line
400 let lines =<< trim END
401 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200402 split
403 vsplit
404 $wincmd w
405 vsplit
406 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200407 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200408 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200409 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200410 \ border: [],
411 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200412 \ })
413 func Dragit()
414 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
415 endfunc
416 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200417 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200418 func Resize()
419 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
420 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200421 map <silent> <F5> :call test_setmouse(6, 21)<CR>
422 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200423 END
424 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200425 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200426 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
427
428 call term_sendkeys(buf, ":call Dragit()\<CR>")
429 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
430
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200431 call term_sendkeys(buf, ":call Resize()\<CR>")
432 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
433
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200434 " clean up
435 call StopVimInTerminal(buf)
436 call delete('XtestPopupDrag')
437endfunc
438
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200439func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200440 CheckScreendump
441
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200442 let lines =<< trim END
443 call setline(1, range(1, 20))
444 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200445 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200446 \ close: 'button',
447 \ border: [],
448 \ line: 1,
449 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200450 \ })
451 func CloseMsg(id, result)
452 echomsg 'Popup closed with ' .. a:result
453 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200454 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200455 \ close: 'click',
456 \ line: 3,
457 \ col: 15,
458 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200459 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200460 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200461 \ close: 'button',
462 \ line: 5,
463 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200464 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200465 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200466 \ close: 'button',
467 \ padding: [],
468 \ line: 5,
469 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200470 \ })
471 func CloseWithX()
472 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
473 endfunc
474 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
475 func CloseWithClick()
476 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
477 endfunc
478 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200479 func CreateWithMenuFilter()
480 let winid = popup_create('barfoo', #{
481 \ close: 'button',
482 \ filter: 'popup_filter_menu',
483 \ border: [],
484 \ line: 1,
485 \ col: 40,
486 \ })
487 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200488 END
489 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200490 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200491 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
492
493 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
494 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
495
496 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
497 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
498
Bram Moolenaarf6396232019-08-24 19:36:00 +0200499 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
500 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
501
502 " We have to send the actual mouse code, feedkeys() would be caught the
503 " filter.
504 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
505 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
506
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200507 " clean up
508 call StopVimInTerminal(buf)
509 call delete('XtestPopupClose')
510endfunction
511
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200512func Test_popup_menu_wrap()
513 CheckScreendump
514
515 let lines =<< trim END
516 call setline(1, range(1, 20))
517 call popup_create([
518 \ 'one',
519 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
520 \ 'three',
521 \ 'four',
522 \ ], #{
523 \ pos: "botleft",
524 \ border: [],
525 \ padding: [0,1,0,1],
526 \ maxheight: 3,
527 \ cursorline: 1,
528 \ filter: 'popup_filter_menu',
529 \ })
530 END
531 call writefile(lines, 'XtestPopupWrap')
532 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
533 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
534
535 call term_sendkeys(buf, "jj")
536 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
537
538 " clean up
539 call term_sendkeys(buf, "\<Esc>")
540 call StopVimInTerminal(buf)
541 call delete('XtestPopupWrap')
542endfunction
543
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200544func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200545 CheckScreendump
546
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200547 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200548 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200549 hi PopupColor ctermbg=lightgrey
550 let winid = popup_create([
551 \ 'some text',
552 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200553 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200554 \ line: 1,
555 \ col: 10,
556 \ wrap: 0,
557 \ fixed: 1,
558 \ zindex: 90,
559 \ padding: [],
560 \ highlight: 'PopupColor',
561 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200562 call popup_create([
563 \ 'xxxxxxxxx',
564 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200565 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200566 \ line: 3,
567 \ col: 18,
568 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200569 let winidb = popup_create([
570 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200571 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200572 \ line: 7,
573 \ col: 10,
574 \ wrap: 0,
575 \ fixed: 1,
576 \ close: 'button',
577 \ zindex: 90,
578 \ padding: [],
579 \ border: [],
580 \ 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 +0200581 END
582 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200583 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200584 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
585
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200586 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
587 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200588 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200589 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
590
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200591 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
592 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200593 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200594 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
595
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200596 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
597 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200598 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200599 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
600
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200601 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
602 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200603 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200604 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
605
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200606 " clean up
607 call StopVimInTerminal(buf)
608 call delete('XtestPopupMask')
609endfunc
610
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200611func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200612 CheckScreendump
613 CheckFeature clipboard_working
614
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200615 " create a popup with some text to be selected
616 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200617 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200618 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200619 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200620 \ drag: 1,
621 \ border: [],
622 \ line: 3,
623 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200624 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200625 \ })
626 func Select1()
627 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
628 endfunc
629 map <silent> <F3> :call test_setmouse(4, 15)<CR>
630 map <silent> <F4> :call test_setmouse(6, 23)<CR>
631 END
632 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200633 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200634 call term_sendkeys(buf, ":call Select1()\<CR>")
635 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
636
637 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
638 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200639 " clean the command line, sometimes it still shows a command
640 call term_sendkeys(buf, ":\<esc>")
641
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200642 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
643
644 " clean up
645 call StopVimInTerminal(buf)
646 call delete('XtestPopupSelect')
647endfunc
648
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200649func Test_popup_in_tab()
650 " default popup is local to tab, not visible when in other tab
651 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200652 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200653 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200654 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200655 tabnew
656 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200657 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200658 quit
659 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200660
661 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200662 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200663 " buffer is gone now
664 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200665
666 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200667 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200668 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200669 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200670 tabnew
671 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200672 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200673 quit
674 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200675 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200676
677 " create popup in other tab
678 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200679 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200680 call assert_equal(0, popup_getpos(winid).visible)
681 call assert_equal(1, popup_getoptions(winid).tabpage)
682 quit
683 call assert_equal(1, popup_getpos(winid).visible)
684 call assert_equal(0, popup_getoptions(winid).tabpage)
685 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200686endfunc
687
688func Test_popup_valid_arguments()
689 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200690 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200691 let pos = popup_getpos(winid)
692 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200693 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200694
695 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200696 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200697 let pos = popup_getpos(winid)
698 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200699 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200700
701 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200702 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200703 let pos = popup_getpos(winid)
704 let around = (&columns - pos.width) / 2
705 call assert_inrange(around - 1, around + 1, pos.col)
706 let around = (&lines - pos.height) / 2
707 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200708 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200709endfunc
710
711func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200712 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200713 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200714 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200715 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200716
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200717 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200718 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200719 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200720 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200721 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200722 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200723 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200724 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200725
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200726 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200727 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200728 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200729 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200730 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200731 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200732 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200733 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200734
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200735 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200736 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200737 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200738 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200739 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200740 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200741 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200742 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200743 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
744 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200745 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200746 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200747
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200748 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200749 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200750 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200751 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200752 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200753 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200754 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
755 call popup_clear()
756 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200757 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200758 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200759endfunc
760
Bram Moolenaareea16992019-05-31 17:34:48 +0200761func Test_win_execute_closing_curwin()
762 split
763 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200764 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200765 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200766endfunc
767
768func Test_win_execute_not_allowed()
769 let winid = popup_create('some text', {})
770 call assert_fails('call win_execute(winid, "split")', 'E994:')
771 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
772 call assert_fails('call win_execute(winid, "close")', 'E994:')
773 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200774 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200775 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
776 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
777 call assert_fails('call win_execute(winid, "next")', 'E994:')
778 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
779 call assert_fails('call win_execute(winid, "buf")', 'E994:')
780 call assert_fails('call win_execute(winid, "edit")', 'E994:')
781 call assert_fails('call win_execute(winid, "enew")', 'E994:')
782 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
783 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
784 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
785 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200786 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200787endfunc
788
Bram Moolenaar402502d2019-05-30 22:07:36 +0200789func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200790 CheckScreendump
791
Bram Moolenaar402502d2019-05-30 22:07:36 +0200792 let lines =<< trim END
793 call setline(1, range(1, 100))
794 let winid = popup_create(
795 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200796 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200797 END
798 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200799 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200800 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
801
802 " clean up
803 call StopVimInTerminal(buf)
804 call delete('XtestPopup')
805endfunc
806
807func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200808 CheckScreendump
809
Bram Moolenaar402502d2019-05-30 22:07:36 +0200810 let lines =<< trim END
811 call setline(1, range(1, 100))
812 let winid = popup_create(
813 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200814 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200815 END
816 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200817 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200818 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
819
820 " clean up
821 call StopVimInTerminal(buf)
822 call delete('XtestPopup')
823endfunc
824
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200825func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200826 CheckScreendump
827
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200828 let lines =<< trim END
829 set showbreak=>>\
830 call setline(1, range(1, 20))
831 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +0200832 \ 'a long line here that wraps',
833 \ #{filter: 'popup_filter_yesno',
834 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200835 END
836 call writefile(lines, 'XtestPopupShowbreak')
837 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
838 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
839
840 " clean up
841 call term_sendkeys(buf, "y")
842 call StopVimInTerminal(buf)
843 call delete('XtestPopupShowbreak')
844endfunc
845
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200846func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200847 CheckFeature timers
848
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200849 topleft vnew
850 call setline(1, 'hello')
851
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200852 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200853 \ line: 1,
854 \ col: 1,
855 \ minwidth: 20,
856 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200857 \})
858 redraw
859 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
860 call assert_equal('world', line)
861
Bram Moolenaarb4f06282019-07-12 21:07:54 +0200862 call assert_equal(winid, popup_locate(1, 1))
863 call assert_equal(winid, popup_locate(1, 20))
864 call assert_equal(0, popup_locate(1, 21))
865 call assert_equal(0, popup_locate(2, 1))
866
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200867 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200868 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +0200869 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200870 call assert_equal('hello', line)
871
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200872 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200873 \ line: &lines,
874 \ col: 10,
875 \ minwidth: 20,
876 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200877 \})
878 redraw
879 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
880 call assert_match('.*on the command line.*', line)
881
882 sleep 700m
883 redraw
884 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
885 call assert_notmatch('.*on the command line.*', line)
886
887 bwipe!
888endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200889
890func Test_popup_hide()
891 topleft vnew
892 call setline(1, 'hello')
893
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200894 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200895 \ line: 1,
896 \ col: 1,
897 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200898 \})
899 redraw
900 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
901 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200902 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200903 " buffer is still listed and active
904 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200905
906 call popup_hide(winid)
907 redraw
908 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
909 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200910 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200911 " buffer is still listed but hidden
912 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200913
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200914 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200915 redraw
916 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
917 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200918 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200919
920
921 call popup_close(winid)
922 redraw
923 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
924 call assert_equal('hello', line)
925
926 " error is given for existing non-popup window
927 call assert_fails('call popup_hide(win_getid())', 'E993:')
928
929 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200930 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200931 call popup_show(41234234)
932
933 bwipe!
934endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200935
936func Test_popup_move()
937 topleft vnew
938 call setline(1, 'hello')
939
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200940 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200941 \ line: 1,
942 \ col: 1,
943 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200944 \})
945 redraw
946 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
947 call assert_equal('world ', line)
948
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200949 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200950 redraw
951 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
952 call assert_equal('hello ', line)
953 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
954 call assert_equal('~world', line)
955
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200956 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200957 redraw
958 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
959 call assert_equal('hworld', line)
960
961 call popup_close(winid)
962
963 bwipe!
964endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200965
Bram Moolenaar402502d2019-05-30 22:07:36 +0200966func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200967 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200968 \ line: 2,
969 \ col: 3,
970 \ minwidth: 10,
971 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +0200972 \})
973 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200974 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200975 call assert_equal(2, res.line)
976 call assert_equal(3, res.col)
977 call assert_equal(10, res.width)
978 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200979 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200980
981 call popup_close(winid)
982endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200983
984func Test_popup_width_longest()
985 let tests = [
986 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
987 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
988 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
989 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
990 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
991 \ ]
992
993 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200994 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200995 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200996 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200997 call assert_equal(test[1], position.width)
998 call popup_close(winid)
999 endfor
1000endfunc
1001
1002func Test_popup_wraps()
1003 let tests = [
1004 \ ['nowrap', 6, 1],
1005 \ ['a line that wraps once', 12, 2],
1006 \ ['a line that wraps two times', 12, 3],
1007 \ ]
1008 for test in tests
1009 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001010 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001011 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001012 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001013 call assert_equal(test[1], position.width)
1014 call assert_equal(test[2], position.height)
1015
1016 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001017 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001018 endfor
1019endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001020
1021func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001022 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001023 \ line: 2,
1024 \ col: 3,
1025 \ minwidth: 10,
1026 \ minheight: 11,
1027 \ maxwidth: 20,
1028 \ maxheight: 21,
1029 \ zindex: 100,
1030 \ time: 5000,
1031 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001032 \})
1033 redraw
1034 let res = popup_getoptions(winid)
1035 call assert_equal(2, res.line)
1036 call assert_equal(3, res.col)
1037 call assert_equal(10, res.minwidth)
1038 call assert_equal(11, res.minheight)
1039 call assert_equal(20, res.maxwidth)
1040 call assert_equal(21, res.maxheight)
1041 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001042 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001043 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001044 if has('timers')
1045 call assert_equal(5000, res.time)
1046 endif
1047 call popup_close(winid)
1048
1049 let winid = popup_create('hello', {})
1050 redraw
1051 let res = popup_getoptions(winid)
1052 call assert_equal(0, res.line)
1053 call assert_equal(0, res.col)
1054 call assert_equal(0, res.minwidth)
1055 call assert_equal(0, res.minheight)
1056 call assert_equal(0, res.maxwidth)
1057 call assert_equal(0, res.maxheight)
1058 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001059 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001060 if has('timers')
1061 call assert_equal(0, res.time)
1062 endif
1063 call popup_close(winid)
1064 call assert_equal({}, popup_getoptions(winid))
1065endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001066
1067func Test_popup_option_values()
1068 new
1069 " window-local
1070 setlocal number
1071 setlocal nowrap
1072 " buffer-local
1073 setlocal omnifunc=Something
1074 " global/buffer-local
1075 setlocal path=/there
1076 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001077 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001078
1079 let winid = popup_create('hello', {})
1080 call assert_equal(0, getwinvar(winid, '&number'))
1081 call assert_equal(1, getwinvar(winid, '&wrap'))
1082 call assert_equal('', getwinvar(winid, '&omnifunc'))
1083 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001084 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1085
1086 " 'scrolloff' is reset to zero
1087 call assert_equal(5, &scrolloff)
1088 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001089
1090 call popup_close(winid)
1091 bwipe
1092endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001093
1094func Test_popup_atcursor()
1095 topleft vnew
1096 call setline(1, [
1097 \ 'xxxxxxxxxxxxxxxxx',
1098 \ 'xxxxxxxxxxxxxxxxx',
1099 \ 'xxxxxxxxxxxxxxxxx',
1100 \])
1101
1102 call cursor(2, 2)
1103 redraw
1104 let winid = popup_atcursor('vim', {})
1105 redraw
1106 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1107 call assert_equal('xvimxxxxxxxxxxxxx', line)
1108 call popup_close(winid)
1109
1110 call cursor(3, 4)
1111 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001112 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001113 redraw
1114 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1115 call assert_equal('xxxvimxxxxxxxxxxx', line)
1116 call popup_close(winid)
1117
1118 call cursor(1, 1)
1119 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001120 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001121 \ line: 'cursor+2',
1122 \ col: 'cursor+1',
1123 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001124 redraw
1125 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1126 call assert_equal('xvimxxxxxxxxxxxxx', line)
1127 call popup_close(winid)
1128
1129 call cursor(3, 3)
1130 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001131 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001132 \ line: 'cursor-2',
1133 \ col: 'cursor-1',
1134 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001135 redraw
1136 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1137 call assert_equal('xvimxxxxxxxxxxxxx', line)
1138 call popup_close(winid)
1139
Bram Moolenaar402502d2019-05-30 22:07:36 +02001140 " just enough room above
1141 call cursor(3, 3)
1142 redraw
1143 let winid = popup_atcursor(['vim', 'is great'], {})
1144 redraw
1145 let pos = popup_getpos(winid)
1146 call assert_equal(1, pos.line)
1147 call popup_close(winid)
1148
1149 " not enough room above, popup goes below the cursor
1150 call cursor(3, 3)
1151 redraw
1152 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1153 redraw
1154 let pos = popup_getpos(winid)
1155 call assert_equal(4, pos.line)
1156 call popup_close(winid)
1157
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001158 " cursor in first line, popup in line 2
1159 call cursor(1, 1)
1160 redraw
1161 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1162 redraw
1163 let pos = popup_getpos(winid)
1164 call assert_equal(2, pos.line)
1165 call popup_close(winid)
1166
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001167 bwipe!
1168endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001169
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001170func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001171 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001172 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001173
1174 let lines =<< trim END
1175 call setline(1, range(1, 20))
1176 call setline(5, 'here is some text to hover over')
1177 set balloonevalterm
1178 set balloonexpr=BalloonExpr()
1179 set balloondelay=100
1180 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001181 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001182 return ''
1183 endfunc
1184 func Hover()
1185 call test_setmouse(5, 15)
1186 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1187 sleep 100m
1188 endfunc
1189 func MoveOntoPopup()
1190 call test_setmouse(4, 17)
1191 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1192 endfunc
1193 func MoveAway()
1194 call test_setmouse(5, 13)
1195 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1196 endfunc
1197 END
1198 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001199 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001200 call term_wait(buf, 100)
1201 call term_sendkeys(buf, 'j')
1202 call term_sendkeys(buf, ":call Hover()\<CR>")
1203 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1204
1205 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1206 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1207
1208 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1209 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1210
1211 " clean up
1212 call StopVimInTerminal(buf)
1213 call delete('XtestPopupBeval')
1214endfunc
1215
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001216func Test_popup_filter()
1217 new
1218 call setline(1, 'some text')
1219
1220 func MyPopupFilter(winid, c)
1221 if a:c == 'e'
1222 let g:eaten = 'e'
1223 return 1
1224 endif
1225 if a:c == '0'
1226 let g:ignored = '0'
1227 return 0
1228 endif
1229 if a:c == 'x'
1230 call popup_close(a:winid)
1231 return 1
1232 endif
1233 return 0
1234 endfunc
1235
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001236 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001237 redraw
1238
1239 " e is consumed by the filter
1240 call feedkeys('e', 'xt')
1241 call assert_equal('e', g:eaten)
1242
1243 " 0 is ignored by the filter
1244 normal $
1245 call assert_equal(9, getcurpos()[2])
1246 call feedkeys('0', 'xt')
1247 call assert_equal('0', g:ignored)
1248 call assert_equal(1, getcurpos()[2])
1249
1250 " x closes the popup
1251 call feedkeys('x', 'xt')
1252 call assert_equal('e', g:eaten)
1253 call assert_equal(-1, winbufnr(winid))
1254
1255 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001256 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001257endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001258
Bram Moolenaara42d9452019-06-15 21:46:30 +02001259func ShowDialog(key, result)
1260 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001261 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001262 \ filter: 'popup_filter_yesno',
1263 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001264 \ })
1265 redraw
1266 call feedkeys(a:key, "xt")
1267 call assert_equal(winid, s:cb_winid)
1268 call assert_equal(a:result, s:cb_res)
1269endfunc
1270
1271func Test_popup_dialog()
1272 func QuitCallback(id, res)
1273 let s:cb_winid = a:id
1274 let s:cb_res = a:res
1275 endfunc
1276
1277 let winid = ShowDialog("y", 1)
1278 let winid = ShowDialog("Y", 1)
1279 let winid = ShowDialog("n", 0)
1280 let winid = ShowDialog("N", 0)
1281 let winid = ShowDialog("x", 0)
1282 let winid = ShowDialog("X", 0)
1283 let winid = ShowDialog("\<Esc>", 0)
1284 let winid = ShowDialog("\<C-C>", -1)
1285
1286 delfunc QuitCallback
1287endfunc
1288
Bram Moolenaara730e552019-06-16 19:05:31 +02001289func ShowMenu(key, result)
1290 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001291 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001292 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001293 \ })
1294 redraw
1295 call feedkeys(a:key, "xt")
1296 call assert_equal(winid, s:cb_winid)
1297 call assert_equal(a:result, s:cb_res)
1298endfunc
1299
1300func Test_popup_menu()
1301 func QuitCallback(id, res)
1302 let s:cb_winid = a:id
1303 let s:cb_res = a:res
1304 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001305 " mapping won't be used in popup
1306 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001307
1308 let winid = ShowMenu(" ", 1)
1309 let winid = ShowMenu("j \<CR>", 2)
1310 let winid = ShowMenu("JjK \<CR>", 2)
1311 let winid = ShowMenu("jjjjjj ", 3)
1312 let winid = ShowMenu("kkk ", 1)
1313 let winid = ShowMenu("x", -1)
1314 let winid = ShowMenu("X", -1)
1315 let winid = ShowMenu("\<Esc>", -1)
1316 let winid = ShowMenu("\<C-C>", -1)
1317
1318 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001319 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001320endfunc
1321
1322func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001323 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001324
1325 let lines =<< trim END
1326 call setline(1, range(1, 20))
1327 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001328 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001329 func MenuDone(id, res)
1330 echomsg "selected " .. a:res
1331 endfunc
1332 END
1333 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001334 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001335 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1336
1337 call term_sendkeys(buf, "jj")
1338 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1339
1340 call term_sendkeys(buf, " ")
1341 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1342
1343 " clean up
1344 call StopVimInTerminal(buf)
1345 call delete('XtestPopupMenu')
1346endfunc
1347
Bram Moolenaarf914a332019-07-20 15:09:56 +02001348func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001349 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001350
1351 let lines =<< trim END
1352 call setline(1, range(1, 20))
1353 hi PopupSelected ctermbg=green
1354 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1355 func MenuDone(id, res)
1356 echomsg "selected " .. a:res
1357 endfunc
1358 END
1359 call writefile(lines, 'XtestPopupNarrowMenu')
1360 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1361 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1362
1363 " clean up
1364 call term_sendkeys(buf, "x")
1365 call StopVimInTerminal(buf)
1366 call delete('XtestPopupNarrowMenu')
1367endfunc
1368
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001369func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001370 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001371
1372 " Create a popup without title or border, a line of padding will be added to
1373 " put the title on.
1374 let lines =<< trim END
1375 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001376 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001377 END
1378 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001379 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001380 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1381
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001382 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1383 call term_sendkeys(buf, ":\<CR>")
1384 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1385
1386 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1387 call term_sendkeys(buf, ":\<CR>")
1388 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1389
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001390 " clean up
1391 call StopVimInTerminal(buf)
1392 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001393
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001394 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001395 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001396 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001397 call assert_equal('Another Title', popup_getoptions(winid).title)
1398
1399 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001400endfunc
1401
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001402func Test_popup_close_callback()
1403 func PopupDone(id, result)
1404 let g:result = a:result
1405 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001406 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001407 redraw
1408 call popup_close(winid, 'done')
1409 call assert_equal('done', g:result)
1410endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001411
1412func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001413 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001414 redraw
1415 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001416 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001417 call assert_equal(5, pos.height)
1418
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001419 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001420 redraw
1421 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001422 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001423 call assert_equal(3, pos.height)
1424endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001425
1426func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001427 CheckScreendump
1428
Bram Moolenaar988c4332019-06-02 14:12:11 +02001429 " +-----------------------------+
1430 " | | |
1431 " | | |
1432 " | | |
1433 " | line1 |
1434 " |------------line2------------|
1435 " | line3 |
1436 " | line4 |
1437 " | |
1438 " | |
1439 " +-----------------------------+
1440 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001441 split
1442 vsplit
1443 let info_window1 = getwininfo()[0]
1444 let line = info_window1['height']
1445 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001446 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001447 \ line : line,
1448 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001449 \ })
1450 END
1451 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001452 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001453 call term_sendkeys(buf, "\<C-W>w")
1454 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1455
1456 " clean up
1457 call StopVimInTerminal(buf)
1458 call delete('XtestPopupBehind')
1459endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001460
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001461func s:VerifyPosition(p, msg, line, col, width, height)
1462 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1463 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1464 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1465 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001466endfunc
1467
1468func Test_popup_position_adjust()
1469 " Anything placed past 2 cells from of the right of the screen is moved to the
1470 " left.
1471 "
1472 " When wrapping is disabled, we also shift to the left to display on the
1473 " screen, unless fixed is set.
1474
1475 " Entries for cases which don't vary based on wrapping.
1476 " Format is per tests described below
1477 let both_wrap_tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001478 \ ['a', 5, &columns, 5, &columns - 2, 1, 1],
1479 \ ['b', 5, &columns + 1, 5, &columns - 2, 1, 1],
1480 \ ['c', 5, &columns - 1, 5, &columns - 2, 1, 1],
1481 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
1482 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1],
1483 \
1484 \ ['aa', 5, &columns, 5, &columns - 2, 2, 1],
1485 \ ['bb', 5, &columns + 1, 5, &columns - 2, 2, 1],
1486 \ ['cc', 5, &columns - 1, 5, &columns - 2, 2, 1],
1487 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1488 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1489 \
1490 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1491 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1492 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1493 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1494 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1495 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001496
1497 " these test groups are dicts with:
1498 " - comment: something to identify the group of tests by
1499 " - options: dict of options to merge with the row/col in tests
1500 " - tests: list of cases. Each one is a list with elements:
1501 " - text
1502 " - row
1503 " - col
1504 " - expected row
1505 " - expected col
1506 " - expected width
1507 " - expected height
1508 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001509 \ #{
1510 \ comment: 'left-aligned with wrapping',
1511 \ options: #{
1512 \ wrap: 1,
1513 \ pos: 'botleft',
1514 \ },
1515 \ tests: both_wrap_tests + [
1516 \ ['aaaa', 5, &columns, 4, &columns - 2, 3, 2],
1517 \ ['bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2],
1518 \ ['cccc', 5, &columns - 1, 4, &columns - 2, 3, 2],
1519 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1520 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1521 \ ],
1522 \ },
1523 \ #{
1524 \ comment: 'left aligned without wrapping',
1525 \ options: #{
1526 \ wrap: 0,
1527 \ pos: 'botleft',
1528 \ },
1529 \ tests: both_wrap_tests + [
1530 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1531 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1532 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1533 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1534 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1535 \ ],
1536 \ },
1537 \ #{
1538 \ comment: 'left aligned with fixed position',
1539 \ options: #{
1540 \ wrap: 0,
1541 \ fixed: 1,
1542 \ pos: 'botleft',
1543 \ },
1544 \ tests: both_wrap_tests + [
1545 \ ['aaaa', 5, &columns, 5, &columns - 2, 3, 1],
1546 \ ['bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1547 \ ['cccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1548 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1549 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1550 \ ],
1551 \ },
1552 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001553
1554 for test_group in tests
1555 for test in test_group.tests
1556 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001557 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001558 \ line: line,
1559 \ col: col,
1560 \ }
1561 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001562
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001563 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001564
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001565 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001566 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1567 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001568 endfor
1569 endfor
1570
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001571 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001572 %bwipe!
1573endfunc
1574
Bram Moolenaar3397f742019-06-02 18:40:06 +02001575func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001576 " width of screen
1577 let X = join(map(range(&columns), {->'X'}), '')
1578
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001579 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1580 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001581
1582 redraw
1583 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1584 call assert_equal(X, line)
1585
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001586 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001587 redraw
1588
1589 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001590 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1591 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001592
1593 redraw
1594 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1595 call assert_equal(X, line)
1596
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001597 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001598 redraw
1599
1600 " Extend so > window width
1601 let X .= 'x'
1602
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001603 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1604 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001605
1606 redraw
1607 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1608 call assert_equal(X[ : -2 ], line)
1609
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001610 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001611 redraw
1612
1613 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001614 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1615 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001616
1617 redraw
1618 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1619 call assert_equal(X[ : -2 ], line)
1620
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001621 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001622 redraw
1623
1624 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001625 let p = popup_create(X,
1626 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1627 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001628
1629 redraw
1630 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1631 let e_line = ' ' . X[ 1 : -2 ]
1632 call assert_equal(e_line, line)
1633
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001634 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001635 redraw
1636
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001637 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001638 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001639endfunc
1640
1641func Test_popup_moved()
1642 new
1643 call test_override('char_avail', 1)
1644 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1645
1646 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001647 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001648 redraw
1649 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001650 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001651 " trigger the check for last_cursormoved by going into insert mode
1652 call feedkeys("li\<Esc>", 'xt')
1653 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001654 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001655
1656 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001657 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001658 redraw
1659 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001660 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001661 call feedkeys("hi\<Esc>", 'xt')
1662 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001663 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001664
1665 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001666 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001667 redraw
1668 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001669 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001670 call feedkeys("li\<Esc>", 'xt')
1671 call assert_equal(1, popup_getpos(winid).visible)
1672 call feedkeys("ei\<Esc>", 'xt')
1673 call assert_equal(1, popup_getpos(winid).visible)
1674 call feedkeys("eli\<Esc>", 'xt')
1675 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001676 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001677
Bram Moolenaar17627312019-06-02 19:53:44 +02001678 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001679 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001680 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001681 redraw
1682 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001683 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001684 call feedkeys("eli\<Esc>", 'xt')
1685 call assert_equal(1, popup_getpos(winid).visible)
1686 call feedkeys("wi\<Esc>", 'xt')
1687 call assert_equal(1, popup_getpos(winid).visible)
1688 call feedkeys("Eli\<Esc>", 'xt')
1689 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001690 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001691
1692 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001693 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001694 redraw
1695 call assert_equal(1, popup_getpos(winid).visible)
1696 call feedkeys("eli\<Esc>", 'xt')
1697 call feedkeys("ei\<Esc>", 'xt')
1698 call assert_equal(1, popup_getpos(winid).visible)
1699 call feedkeys("eli\<Esc>", 'xt')
1700 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001701 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001702
1703 bwipe!
1704 call test_override('ALL', 0)
1705endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001706
1707func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001708 CheckFeature timers
1709 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001710
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001711 let lines =<< trim END
1712 call setline(1, range(1, 20))
1713 hi Notification ctermbg=lightblue
1714 call popup_notification('first notification', {})
1715 END
1716 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001717 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001718 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1719
1720 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001721 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1722 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001723 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1724
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001725 " clean up
1726 call StopVimInTerminal(buf)
1727 call delete('XtestNotifications')
1728endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001729
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001730func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001731 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001732
1733 let lines =<< trim END
1734 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001735 hi ScrollThumb ctermbg=blue
1736 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001737 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001738 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001739 \ minwidth: 8,
1740 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001741 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001742 func ScrollUp()
1743 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1744 endfunc
1745 func ScrollDown()
1746 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1747 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001748 func ClickTop()
1749 call feedkeys("\<F4>\<LeftMouse>", "xt")
1750 endfunc
1751 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001752 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001753 call feedkeys("\<F5>\<LeftMouse>", "xt")
1754 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001755 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001756 map <silent> <F4> :call test_setmouse(4, 42)<CR>
1757 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001758 END
1759 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001760 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001761 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
1762
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001763 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001764 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001765 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
1766
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001767 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001768 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001769 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
1770
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001771 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02001772 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001773 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
1774
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02001775 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001776 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001777 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
1778 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
1779
1780 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1781 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
1782
1783 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02001784 " wait a bit, otherwise it fails sometimes (double click recognized?)
1785 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001786 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
1787 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
1788
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001789 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1790 sleep 100m
1791 call term_sendkeys(buf, ":call ClickTop()\<CR>")
1792 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
1793
1794 call term_sendkeys(buf, ":call ClickBot()\<CR>")
1795 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
1796
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001797 " remove the minwidth and maxheight
1798 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02001799 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02001800 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
1801
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001802 " clean up
1803 call StopVimInTerminal(buf)
1804 call delete('XtestPopupScroll')
1805endfunc
1806
Bram Moolenaar437a7462019-07-05 20:17:22 +02001807func Test_popup_fitting_scrollbar()
1808 " this was causing a crash, divide by zero
1809 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001810 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001811 \ scrollbar: 1,
1812 \ maxwidth: 10,
1813 \ maxheight: 5,
1814 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02001815 redraw
1816 call popup_clear()
1817endfunc
1818
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001819func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001820 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001821
1822 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001823 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001824 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001825 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001826 END
1827
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001828 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001829 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001830 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
1831
1832 " Setting to empty string clears it
1833 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
1834 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
1835
1836 " Setting a list
1837 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1838 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1839
1840 " Shrinking with a list
1841 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
1842 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
1843
1844 " Growing with a list
1845 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
1846 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
1847
1848 " Empty list clears
1849 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
1850 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
1851
1852 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001853 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001854 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
1855
1856 " clean up
1857 call StopVimInTerminal(buf)
1858 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001859endfunc
1860
1861func Test_popup_hidden()
1862 new
1863
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001864 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001865 redraw
1866 call assert_equal(0, popup_getpos(winid).visible)
1867 call popup_close(winid)
1868
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001869 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001870 redraw
1871 call assert_equal(0, popup_getpos(winid).visible)
1872 call popup_close(winid)
1873
1874 func QuitCallback(id, res)
1875 let s:cb_winid = a:id
1876 let s:cb_res = a:res
1877 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001878 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001879 \ filter: 'popup_filter_yesno',
1880 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001881 \ })
1882 redraw
1883 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02001884 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
1885 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02001886 exe "normal anot used by filter\<Esc>"
1887 call assert_equal('not used by filter', getline(1))
1888
1889 call popup_show(winid)
1890 call feedkeys('y', "xt")
1891 call assert_equal(1, s:cb_res)
1892
1893 bwipe!
1894 delfunc QuitCallback
1895endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02001896
1897" Test options not checked elsewhere
1898func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001899 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001900 let options = popup_getoptions(winid)
1901 call assert_equal(1, options.wrap)
1902 call assert_equal(0, options.drag)
1903 call assert_equal('Beautiful', options.highlight)
1904
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001905 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001906 let options = popup_getoptions(winid)
1907 call assert_equal(0, options.wrap)
1908 call assert_equal(1, options.drag)
1909 call assert_equal('Another', options.highlight)
1910
1911 call popup_close(winid)
1912endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001913
1914func Test_popupwin_garbage_collect()
1915 func MyPopupFilter(x, winid, c)
1916 " NOP
1917 endfunc
1918
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001919 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001920 call test_garbagecollect_now()
1921 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001922 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02001923 call feedkeys('j', 'xt')
1924 call assert_true(v:true)
1925
1926 call popup_close(winid)
1927 delfunc MyPopupFilter
1928endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001929
Bram Moolenaar581ba392019-09-03 22:08:33 +02001930func Test_popupwin_filter_mode()
1931 func MyPopupFilter(winid, c)
1932 let s:typed = a:c
1933 if a:c == ':' || a:c == "\r" || a:c == 'v'
1934 " can start cmdline mode, get out, and start/stop Visual mode
1935 return 0
1936 endif
1937 return 1
1938 endfunc
1939
1940 " Normal, Visual and Insert mode
1941 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
1942 redraw
1943 call feedkeys('x', 'xt')
1944 call assert_equal('x', s:typed)
1945
1946 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
1947 call assert_equal(':', s:typed)
1948 call assert_equal('foo', g:foo)
1949
1950 let @x = 'something'
1951 call feedkeys('v$"xy', 'xt')
1952 call assert_equal('y', s:typed)
1953 call assert_equal('something', @x) " yank command is filtered out
1954 call feedkeys('v', 'xt') " end Visual mode
1955
1956 call popup_close(winid)
1957
1958 " only Normal mode
1959 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
1960 redraw
1961 call feedkeys('x', 'xt')
1962 call assert_equal('x', s:typed)
1963
1964 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
1965 call assert_equal(':', s:typed)
1966 call assert_equal('foo', g:foo)
1967
1968 let @x = 'something'
1969 call feedkeys('v$"xy', 'xt')
1970 call assert_equal('v', s:typed)
1971 call assert_notequal('something', @x)
1972
1973 call popup_close(winid)
1974
1975 " default: all modes
1976 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
1977 redraw
1978 call feedkeys('x', 'xt')
1979 call assert_equal('x', s:typed)
1980
1981 let g:foo = 'bar'
1982 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
1983 call assert_equal("\r", s:typed)
1984 call assert_equal('bar', g:foo)
1985
1986 let @x = 'something'
1987 call feedkeys('v$"xy', 'xt')
1988 call assert_equal('y', s:typed)
1989 call assert_equal('something', @x) " yank command is filtered out
1990 call feedkeys('v', 'xt') " end Visual mode
1991
1992 call popup_close(winid)
1993 delfunc MyPopupFilter
1994endfunc
1995
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001996func Test_popupwin_with_buffer()
1997 call writefile(['some text', 'in a buffer'], 'XsomeFile')
1998 let buf = bufadd('XsomeFile')
1999 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002000
2001 setlocal number
2002 call setbufvar(buf, "&wrapmargin", 13)
2003
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002004 let winid = popup_create(buf, {})
2005 call assert_notequal(0, winid)
2006 let pos = popup_getpos(winid)
2007 call assert_equal(2, pos.height)
2008 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002009
2010 " window-local option is set to default, buffer-local is not
2011 call assert_equal(0, getwinvar(winid, '&number'))
2012 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2013
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002014 call popup_close(winid)
2015 call assert_equal({}, popup_getpos(winid))
2016 call assert_equal(1, bufloaded(buf))
2017 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002018 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002019
2020 edit test_popupwin.vim
2021 let winid = popup_create(bufnr(''), {})
2022 redraw
2023 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002024 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002025endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002026
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002027func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002028 CheckFeature terminal
2029
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002030 let ptybuf = term_start(&shell, #{hidden: 1})
2031 call assert_fails('let winnr = popup_create(ptybuf, #{})', 'E278:')
2032 exe 'bwipe! ' .. ptybuf
2033endfunc
2034
Bram Moolenaar934470e2019-09-01 23:27:05 +02002035func Test_popupwin_with_buffer_and_filter()
2036 new Xwithfilter
2037 call setline(1, range(100))
2038 let bufnr = bufnr()
2039 hide
2040
2041 func BufferFilter(win, key)
2042 if a:key == 'G'
2043 " recursive use of "G" does not cause problems.
2044 call win_execute(a:win, 'normal! G')
2045 return 1
2046 endif
2047 return 0
2048 endfunc
2049
2050 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2051 call assert_equal(1, popup_getpos(winid).firstline)
2052 redraw
2053 call feedkeys("G", 'xt')
2054 call assert_equal(99, popup_getpos(winid).firstline)
2055
2056 call popup_close(winid)
2057 exe 'bwipe! ' .. bufnr
2058endfunc
2059
Bram Moolenaare296e312019-07-03 23:20:18 +02002060func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002061 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002062 \ maxwidth: 40,
2063 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002064 \ })
2065 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002066 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002067 redraw
2068 call assert_equal(19, popup_getpos(winid).width)
2069 endfor
2070 call popup_clear()
2071endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002072
2073func Test_popupwin_buf_close()
2074 let buf = bufadd('Xtestbuf')
2075 call bufload(buf)
2076 call setbufline(buf, 1, ['just', 'some', 'lines'])
2077 let winid = popup_create(buf, {})
2078 redraw
2079 call assert_equal(3, popup_getpos(winid).height)
2080 let bufinfo = getbufinfo(buf)[0]
2081 call assert_equal(1, bufinfo.changed)
2082 call assert_equal(0, bufinfo.hidden)
2083 call assert_equal(0, bufinfo.listed)
2084 call assert_equal(1, bufinfo.loaded)
2085 call assert_equal([], bufinfo.windows)
2086 call assert_equal([winid], bufinfo.popups)
2087
2088 call popup_close(winid)
2089 call assert_equal({}, popup_getpos(winid))
2090 let bufinfo = getbufinfo(buf)[0]
2091 call assert_equal(1, bufinfo.changed)
2092 call assert_equal(1, bufinfo.hidden)
2093 call assert_equal(0, bufinfo.listed)
2094 call assert_equal(1, bufinfo.loaded)
2095 call assert_equal([], bufinfo.windows)
2096 call assert_equal([], bufinfo.popups)
2097 exe 'bwipe! ' .. buf
2098endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002099
2100func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002101 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002102
2103 let lines =<< trim END
2104 call setline(1, range(1, 10))
2105 hi ScrollThumb ctermbg=blue
2106 hi ScrollBar ctermbg=red
2107 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002108 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002109 \ maxwidth: 10,
2110 \ maxheight: 3,
2111 \ pos : 'topleft',
2112 \ col : a:col,
2113 \ line : a:line,
2114 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002115 \ })
2116 endfunc
2117 call PopupMenu(['x'], 1, 1)
2118 call PopupMenu(['123456789|'], 1, 16)
2119 call PopupMenu(['123456789|' .. ' '], 7, 1)
2120 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2121 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2122 END
2123 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002124 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002125 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2126
2127 " close the menu popupwin.
2128 call term_sendkeys(buf, " ")
2129 call term_sendkeys(buf, " ")
2130 call term_sendkeys(buf, " ")
2131 call term_sendkeys(buf, " ")
2132 call term_sendkeys(buf, " ")
2133
2134 " clean up
2135 call StopVimInTerminal(buf)
2136 call delete('XtestPopupMenuMaxWidth')
2137endfunc
2138
Bram Moolenaara901a372019-07-13 16:38:50 +02002139func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002140 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002141
2142 let lines =<< trim END
2143 call setline(1, range(1, 20))
2144 hi ScrollThumb ctermbg=blue
2145 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002146 eval ['one', 'two', 'three', 'four', 'five',
2147 \ 'six', 'seven', 'eight', 'nine']
2148 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002149 \ minwidth: 8,
2150 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002151 \ })
2152 END
2153 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002154 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002155
2156 call term_sendkeys(buf, "j")
2157 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2158
2159 call term_sendkeys(buf, "jjj")
2160 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2161
2162 " if the cursor is the bottom line, it stays at the bottom line.
2163 call term_sendkeys(buf, repeat("j", 20))
2164 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2165
2166 call term_sendkeys(buf, "kk")
2167 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2168
2169 call term_sendkeys(buf, "k")
2170 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2171
2172 " if the cursor is in the top line, it stays in the top line.
2173 call term_sendkeys(buf, repeat("k", 20))
2174 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2175
2176 " close the menu popupwin.
2177 call term_sendkeys(buf, " ")
2178
2179 " clean up
2180 call StopVimInTerminal(buf)
2181 call delete('XtestPopupMenuScroll')
2182endfunc
2183
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002184func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002185 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002186
2187 let lines =<< trim END
2188 function! MyFilter(winid, key) abort
2189 if a:key == "0"
2190 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2191 return 1
2192 endif
2193 if a:key == "G"
2194 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2195 return 1
2196 endif
2197 if a:key == "j"
2198 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2199 return 1
2200 endif
2201 if a:key == "k"
2202 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2203 return 1
2204 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002205 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002206 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002207 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002208 endif
2209 return 0
2210 endfunction
2211 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2212 \ maxheight : 3,
2213 \ filter : 'MyFilter'
2214 \ })
2215 END
2216 call writefile(lines, 'XtestPopupMenuFilter')
2217 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2218
2219 call term_sendkeys(buf, "j")
2220 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2221
2222 call term_sendkeys(buf, "k")
2223 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2224
2225 call term_sendkeys(buf, "G")
2226 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2227
2228 call term_sendkeys(buf, "0")
2229 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2230
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002231 " check that when the popup is closed in the filter the screen is redrawn
2232 call term_sendkeys(buf, ":")
2233 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2234 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002235
2236 " clean up
2237 call StopVimInTerminal(buf)
2238 call delete('XtestPopupMenuFilter')
2239endfunc
2240
2241func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002242 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002243
2244 let winid = popup_create('some text', {})
2245 call assert_equal(0, popup_getoptions(winid).cursorline)
2246 call popup_close(winid)
2247
2248 let winid = popup_create('some text', #{ cursorline: 1, })
2249 call assert_equal(1, popup_getoptions(winid).cursorline)
2250 call popup_close(winid)
2251
2252 let winid = popup_create('some text', #{ cursorline: 0, })
2253 call assert_equal(0, popup_getoptions(winid).cursorline)
2254 call popup_close(winid)
2255
2256 let winid = popup_menu('some text', {})
2257 call assert_equal(1, popup_getoptions(winid).cursorline)
2258 call popup_close(winid)
2259
2260 let winid = popup_menu('some text', #{ cursorline: 1, })
2261 call assert_equal(1, popup_getoptions(winid).cursorline)
2262 call popup_close(winid)
2263
2264 let winid = popup_menu('some text', #{ cursorline: 0, })
2265 call assert_equal(0, popup_getoptions(winid).cursorline)
2266 call popup_close(winid)
2267
2268 " ---------
2269 " Pattern 1
2270 " ---------
2271 let lines =<< trim END
2272 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2273 END
2274 call writefile(lines, 'XtestPopupCursorLine')
2275 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2276 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2277 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2278 call StopVimInTerminal(buf)
2279
2280 " ---------
2281 " Pattern 2
2282 " ---------
2283 let lines =<< trim END
2284 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2285 END
2286 call writefile(lines, 'XtestPopupCursorLine')
2287 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2288 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2289 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2290 call StopVimInTerminal(buf)
2291
2292 " ---------
2293 " Pattern 3
2294 " ---------
2295 let lines =<< trim END
2296 function! MyFilter(winid, key) abort
2297 if a:key == "j"
2298 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2299 return 1
2300 endif
2301 if a:key == 'x'
2302 call popup_close(a:winid)
2303 return 1
2304 endif
2305 return 0
2306 endfunction
2307 call popup_menu(['111', '222', '333'], #{
2308 \ cursorline : 0,
2309 \ maxheight : 2,
2310 \ filter : 'MyFilter',
2311 \ })
2312 END
2313 call writefile(lines, 'XtestPopupCursorLine')
2314 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2315 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2316 call term_sendkeys(buf, "j")
2317 call term_sendkeys(buf, "j")
2318 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2319 call term_sendkeys(buf, "x")
2320 call StopVimInTerminal(buf)
2321
2322 " ---------
2323 " Pattern 4
2324 " ---------
2325 let lines =<< trim END
2326 function! MyFilter(winid, key) abort
2327 if a:key == "j"
2328 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2329 return 1
2330 endif
2331 if a:key == 'x'
2332 call popup_close(a:winid)
2333 return 1
2334 endif
2335 return 0
2336 endfunction
2337 call popup_menu(['111', '222', '333'], #{
2338 \ cursorline : 1,
2339 \ maxheight : 2,
2340 \ filter : 'MyFilter',
2341 \ })
2342 END
2343 call writefile(lines, 'XtestPopupCursorLine')
2344 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2345 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2346 call term_sendkeys(buf, "j")
2347 call term_sendkeys(buf, "j")
2348 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2349 call term_sendkeys(buf, "x")
2350 call StopVimInTerminal(buf)
2351
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002352 " ---------
2353 " Cursor in second line when creating the popup
2354 " ---------
2355 let lines =<< trim END
2356 let winid = popup_create(['111', '222', '333'], #{
2357 \ cursorline : 1,
2358 \ })
2359 call win_execute(winid, "2")
2360 END
2361 call writefile(lines, 'XtestPopupCursorLine')
2362 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2363 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2364 call StopVimInTerminal(buf)
2365
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002366 call delete('XtestPopupCursorLine')
2367endfunc
2368
Bram Moolenaarf914a332019-07-20 15:09:56 +02002369func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002370 CheckScreendump
2371
Bram Moolenaarf914a332019-07-20 15:09:56 +02002372 call writefile([
2373 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2374 \ "another\tXtagfile\t/^this is another",
2375 \ "theword\tXtagfile\t/^theword"],
2376 \ 'Xtags')
2377 call writefile(range(1,20)
2378 \ + ['theword is here']
2379 \ + range(22, 27)
2380 \ + ['this is another place']
2381 \ + range(29, 40),
2382 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002383 call writefile(range(1,10)
2384 \ + ['searched word is here']
2385 \ + range(12, 20),
2386 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002387 let lines =<< trim END
2388 set tags=Xtags
2389 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002390 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002391 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002392 \ 'three',
2393 \ 'four',
2394 \ 'five',
2395 \ 'six',
2396 \ 'seven',
2397 \ 'find theword somewhere',
2398 \ 'nine',
2399 \ 'this is another word',
2400 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002401 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002402 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002403 END
2404 call writefile(lines, 'XtestPreviewPopup')
2405 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2406
2407 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2408 call term_sendkeys(buf, ":\<CR>")
2409 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2410
2411 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2412 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2413
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002414 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002415 call term_sendkeys(buf, ":\<CR>")
2416 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2417
2418 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2419 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2420
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002421 call term_sendkeys(buf, ":cd ..\<CR>:\<CR>")
2422 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002423 call term_sendkeys(buf, ":cd testdir\<CR>")
2424
2425 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002426 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002427 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2428
2429 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2430 call term_sendkeys(buf, ":\<CR>")
2431 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2432
2433 call term_sendkeys(buf, ":pclose\<CR>")
2434 call term_sendkeys(buf, ":psearch searched\<CR>")
2435 call term_sendkeys(buf, ":\<CR>")
2436 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002437
Bram Moolenaarf914a332019-07-20 15:09:56 +02002438 call StopVimInTerminal(buf)
2439 call delete('Xtags')
2440 call delete('Xtagfile')
2441 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002442 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002443endfunc
2444
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002445func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002446 let lines =<< trim END
2447 set completeopt+=preview,popup
2448 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002449 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002450
2451 func CompleteFuncDict(findstart, base)
2452 if a:findstart
2453 if col('.') > 10
2454 return col('.') - 10
2455 endif
2456 return 0
2457 endif
2458
2459 return {
2460 \ 'words': [
2461 \ {
2462 \ 'word': 'aword',
2463 \ 'abbr': 'wrd',
2464 \ 'menu': 'extra text',
2465 \ 'info': 'words are cool',
2466 \ 'kind': 'W',
2467 \ 'user_data': 'test'
2468 \ },
2469 \ {
2470 \ 'word': 'anotherword',
2471 \ 'abbr': 'anotwrd',
2472 \ 'menu': 'extra text',
2473 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2474 \ 'kind': 'W',
2475 \ 'user_data': 'notest'
2476 \ },
2477 \ {
2478 \ 'word': 'noinfo',
2479 \ 'abbr': 'noawrd',
2480 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002481 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002482 \ 'kind': 'W',
2483 \ 'user_data': 'notest'
2484 \ },
2485 \ {
2486 \ 'word': 'thatword',
2487 \ 'abbr': 'thatwrd',
2488 \ 'menu': 'extra text',
2489 \ 'info': 'that word is cool',
2490 \ 'kind': 'W',
2491 \ 'user_data': 'notest'
2492 \ },
2493 \ ]
2494 \ }
2495 endfunc
2496 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002497 func ChangeColor()
2498 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002499 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002500 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002501 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002502 return lines
2503endfunc
2504
2505func Test_popupmenu_info_border()
2506 CheckScreendump
2507
2508 let lines = Get_popupmenu_lines()
2509 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002510 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002511
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002512 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
2513 call term_wait(buf, 50)
2514
2515 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2516 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
2517
2518 call term_sendkeys(buf, "\<C-N>")
2519 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
2520
2521 call term_sendkeys(buf, "\<C-N>")
2522 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
2523
2524 call term_sendkeys(buf, "\<C-N>\<C-N>")
2525 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
2526
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02002527 " info on the left with scrollbar
2528 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
2529 call term_sendkeys(buf, "\<C-N>\<C-N>")
2530 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
2531
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002532 call StopVimInTerminal(buf)
2533 call delete('XtestInfoPopup')
2534endfunc
2535
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002536func Test_popupmenu_info_noborder()
2537 CheckScreendump
2538
2539 let lines = Get_popupmenu_lines()
2540 call add(lines, 'set completepopup=height:4,border:off')
2541 call writefile(lines, 'XtestInfoPopupNb')
2542
2543 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2544 call term_wait(buf, 50)
2545
2546 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2547 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
2548
2549 call StopVimInTerminal(buf)
2550 call delete('XtestInfoPopupNb')
2551endfunc
2552
Bram Moolenaar258cef52019-08-21 17:29:29 +02002553func Test_popupmenu_info_align_menu()
2554 CheckScreendump
2555
2556 let lines = Get_popupmenu_lines()
2557 call add(lines, 'set completepopup=height:4,border:off,align:menu')
2558 call writefile(lines, 'XtestInfoPopupNb')
2559
2560 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
2561 call term_wait(buf, 50)
2562
2563 call term_sendkeys(buf, "A\<C-X>\<C-U>")
2564 call term_sendkeys(buf, "\<C-N>")
2565 call term_sendkeys(buf, "\<C-N>")
2566 call term_sendkeys(buf, "\<C-N>")
2567 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
2568
2569 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
2570 call term_sendkeys(buf, "\<C-N>")
2571 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
2572
2573 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002574 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02002575 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
2576 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
2577 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
2578
2579 call StopVimInTerminal(buf)
2580 call delete('XtestInfoPopupNb')
2581endfunc
2582
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002583func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02002584 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002585 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002586 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002587 call assert_equal(bufnr, winbufnr(winid))
2588 call popup_clear()
2589endfunc
2590
Bram Moolenaar1824f452019-10-02 23:06:46 +02002591func Test_popupwin_getoptions_tablocal()
2592 topleft split
2593 let win1 = popup_create('nothing', #{maxheight: 8})
2594 let win2 = popup_create('something', #{maxheight: 10})
2595 let win3 = popup_create('something', #{maxheight: 15})
2596 call assert_equal(8, popup_getoptions(win1).maxheight)
2597 call assert_equal(10, popup_getoptions(win2).maxheight)
2598 call assert_equal(15, popup_getoptions(win3).maxheight)
2599 call popup_clear()
2600 quit
2601endfunc
2602
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02002603func Test_popupwin_cancel()
2604 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
2605 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
2606 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
2607 call assert_equal(5, popup_getpos(win1).line)
2608 call assert_equal(10, popup_getpos(win2).line)
2609 call assert_equal(15, popup_getpos(win3).line)
2610 " TODO: this also works without patch 8.1.2110
2611 call feedkeys("\<C-C>", 'xt')
2612 call assert_equal(5, popup_getpos(win1).line)
2613 call assert_equal(10, popup_getpos(win2).line)
2614 call assert_equal({}, popup_getpos(win3))
2615 call feedkeys("\<C-C>", 'xt')
2616 call assert_equal(5, popup_getpos(win1).line)
2617 call assert_equal({}, popup_getpos(win2))
2618 call assert_equal({}, popup_getpos(win3))
2619 call feedkeys("\<C-C>", 'xt')
2620 call assert_equal({}, popup_getpos(win1))
2621 call assert_equal({}, popup_getpos(win2))
2622 call assert_equal({}, popup_getpos(win3))
2623endfunc
2624
Bram Moolenaar331bafd2019-07-20 17:46:05 +02002625" vim: shiftwidth=2 sts=2