Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 1 | " Tests for popup windows |
| 2 | |
| 3 | if !has('textprop') |
| 4 | finish |
| 5 | endif |
| 6 | |
| 7 | source screendump.vim |
| 8 | |
| 9 | func Test_simple_popup() |
| 10 | if !CanRunVimInTerminal() |
| 11 | return |
| 12 | endif |
| 13 | call writefile([ |
| 14 | \ "call setline(1, range(1, 100))", |
Bram Moolenaar | 20c023a | 2019-05-26 21:03:24 +0200 | [diff] [blame] | 15 | \ "hi PopupColor1 ctermbg=lightblue", |
| 16 | \ "hi PopupColor2 ctermbg=lightcyan", |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 17 | \ "hi Comment ctermfg=red", |
| 18 | \ "call prop_type_add('comment', {'highlight': 'Comment'})", |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 19 | \ "let winid = popup_create('hello there', {'line': 3, 'col': 11, 'minwidth': 20, 'highlight': 'PopupColor1'})", |
| 20 | \ "let winid2 = popup_create(['another one', 'another two', 'another three'], {'line': 3, 'col': 25, 'minwidth': 20})", |
Bram Moolenaar | 20c023a | 2019-05-26 21:03:24 +0200 | [diff] [blame] | 21 | \ "call setwinvar(winid2, '&wincolor', 'PopupColor2')", |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 22 | \], 'XtestPopup') |
| 23 | let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10}) |
| 24 | call VerifyScreenDump(buf, 'Test_popupwin_01', {}) |
| 25 | |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 26 | " Add a tabpage |
| 27 | call term_sendkeys(buf, ":tabnew\<CR>") |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 28 | call term_sendkeys(buf, ":let popupwin = popup_create([" |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 29 | \ .. "{'text': 'other tab'}," |
| 30 | \ .. "{'text': 'a comment line', 'props': [{" |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 31 | \ .. "'col': 3, 'length': 7, 'minwidth': 20, 'type': 'comment'" |
Bram Moolenaar | 7a8d027 | 2019-05-26 23:32:06 +0200 | [diff] [blame] | 32 | \ .. "}]}," |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 33 | \ .. "], {'line': 4, 'col': 9, 'minwidth': 20})\<CR>") |
Bram Moolenaar | ec58384 | 2019-05-26 14:11:23 +0200 | [diff] [blame] | 34 | call VerifyScreenDump(buf, 'Test_popupwin_02', {}) |
| 35 | |
| 36 | " switch back to first tabpage |
| 37 | call term_sendkeys(buf, "gt") |
| 38 | call VerifyScreenDump(buf, 'Test_popupwin_03', {}) |
| 39 | |
| 40 | " close that tabpage |
| 41 | call term_sendkeys(buf, ":quit!\<CR>") |
| 42 | call VerifyScreenDump(buf, 'Test_popupwin_04', {}) |
| 43 | |
Bram Moolenaar | 1714696 | 2019-05-30 00:12:11 +0200 | [diff] [blame] | 44 | " resize popup, show empty line at bottom |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 45 | call term_sendkeys(buf, ":call popup_move(popupwin, {'minwidth': 15, 'maxwidth': 25, 'minheight': 3, 'maxheight': 5})\<CR>") |
| 46 | call term_sendkeys(buf, ":redraw\<CR>") |
| 47 | call VerifyScreenDump(buf, 'Test_popupwin_05', {}) |
| 48 | |
Bram Moolenaar | 1714696 | 2019-05-30 00:12:11 +0200 | [diff] [blame] | 49 | " show not fitting line at bottom |
| 50 | call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>") |
| 51 | call term_sendkeys(buf, ":redraw\<CR>") |
| 52 | call VerifyScreenDump(buf, 'Test_popupwin_06', {}) |
| 53 | |
Bram Moolenaar | 4d784b2 | 2019-05-25 19:51:39 +0200 | [diff] [blame] | 54 | " clean up |
| 55 | call StopVimInTerminal(buf) |
| 56 | call delete('XtestPopup') |
| 57 | endfunc |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 58 | |
Bram Moolenaar | 2fd8e35 | 2019-06-01 20:16:48 +0200 | [diff] [blame] | 59 | func Test_popup_with_border_and_padding() |
| 60 | if !CanRunVimInTerminal() |
| 61 | return |
| 62 | endif |
Bram Moolenaar | 2fd8e35 | 2019-06-01 20:16:48 +0200 | [diff] [blame] | 63 | |
Bram Moolenaar | 3bfd04e | 2019-06-01 20:45:21 +0200 | [diff] [blame^] | 64 | for iter in range(0, 1) |
| 65 | call writefile([iter == 1 ? '' : 'set enc=latin1', |
| 66 | \ "call setline(1, range(1, 100))", |
| 67 | \ "call popup_create('hello border', {'line': 2, 'col': 3, 'border': []})", |
| 68 | \ "call popup_create('hello padding', {'line': 2, 'col': 23, 'padding': []})", |
| 69 | \ "call popup_create('hello both', {'line': 2, 'col': 43, 'border': [], 'padding': []})", |
| 70 | \ "call popup_create('border TL', {'line': 6, 'col': 3, 'border': [1, 0, 0, 4]})", |
| 71 | \ "call popup_create('paddings', {'line': 6, 'col': 23, 'padding': [1, 3, 2, 4]})", |
| 72 | \], 'XtestPopupBorder') |
| 73 | let buf = RunVimInTerminal('-S XtestPopupBorder', {'rows': 15}) |
| 74 | call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {}) |
| 75 | |
| 76 | call StopVimInTerminal(buf) |
| 77 | call delete('XtestPopupBorder') |
| 78 | endfor |
Bram Moolenaar | 2fd8e35 | 2019-06-01 20:16:48 +0200 | [diff] [blame] | 79 | |
| 80 | let with_border_or_padding = { |
| 81 | \ 'line': 2, |
| 82 | \ 'core_line': 3, |
| 83 | \ 'col': 3, |
| 84 | \ 'core_col': 4, |
| 85 | \ 'width': 14, |
| 86 | \ 'core_width': 12, |
| 87 | \ 'height': 3, |
| 88 | \ 'core_height': 1, |
| 89 | \ 'visible': 1} |
| 90 | let winid = popup_create('hello border', {'line': 2, 'col': 3, 'border': []})", |
| 91 | call assert_equal(with_border_or_padding, popup_getpos(winid)) |
| 92 | |
| 93 | let winid = popup_create('hello paddng', {'line': 2, 'col': 3, 'padding': []}) |
| 94 | call assert_equal(with_border_or_padding, popup_getpos(winid)) |
| 95 | |
| 96 | let winid = popup_create('hello both', {'line': 3, 'col': 8, 'border': [], 'padding': []}) |
| 97 | call assert_equal({ |
| 98 | \ 'line': 3, |
| 99 | \ 'core_line': 5, |
| 100 | \ 'col': 8, |
| 101 | \ 'core_col': 10, |
| 102 | \ 'width': 14, |
| 103 | \ 'core_width': 10, |
| 104 | \ 'height': 5, |
| 105 | \ 'core_height': 1, |
| 106 | \ 'visible': 1}, popup_getpos(winid)) |
| 107 | endfunc |
| 108 | |
Bram Moolenaar | b423012 | 2019-05-30 18:40:53 +0200 | [diff] [blame] | 109 | func Test_popup_with_syntax_win_execute() |
| 110 | if !CanRunVimInTerminal() |
| 111 | return |
| 112 | endif |
| 113 | call writefile([ |
| 114 | \ "call setline(1, range(1, 100))", |
| 115 | \ "hi PopupColor ctermbg=lightblue", |
| 116 | \ "let winid = popup_create([", |
| 117 | \ "\\ '#include <stdio.h>',", |
| 118 | \ "\\ 'int main(void)',", |
| 119 | \ "\\ '{',", |
| 120 | \ "\\ ' printf(123);',", |
| 121 | \ "\\ '}',", |
| 122 | \ "\\], {'line': 3, 'col': 25, 'highlight': 'PopupColor'})", |
| 123 | \ "call win_execute(winid, 'set syntax=cpp')", |
| 124 | \], 'XtestPopup') |
| 125 | let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10}) |
| 126 | call VerifyScreenDump(buf, 'Test_popupwin_10', {}) |
| 127 | |
| 128 | " clean up |
| 129 | call StopVimInTerminal(buf) |
| 130 | call delete('XtestPopup') |
| 131 | endfunc |
| 132 | |
| 133 | func Test_popup_with_syntax_setbufvar() |
| 134 | if !CanRunVimInTerminal() |
| 135 | return |
| 136 | endif |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 137 | let lines =<< trim END |
| 138 | call setline(1, range(1, 100)) |
| 139 | hi PopupColor ctermbg=lightgrey |
| 140 | let winid = popup_create([ |
| 141 | \ '#include <stdio.h>', |
| 142 | \ 'int main(void)', |
| 143 | \ '{', |
| 144 | \ ' printf(567);', |
| 145 | \ '}', |
| 146 | \], {'line': 3, 'col': 21, 'highlight': 'PopupColor'}) |
| 147 | call setbufvar(winbufnr(winid), '&syntax', 'cpp') |
| 148 | END |
| 149 | call writefile(lines, 'XtestPopup') |
Bram Moolenaar | b423012 | 2019-05-30 18:40:53 +0200 | [diff] [blame] | 150 | let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10}) |
| 151 | call VerifyScreenDump(buf, 'Test_popupwin_11', {}) |
| 152 | |
| 153 | " clean up |
| 154 | call StopVimInTerminal(buf) |
| 155 | call delete('XtestPopup') |
| 156 | endfunc |
| 157 | |
Bram Moolenaar | eea1699 | 2019-05-31 17:34:48 +0200 | [diff] [blame] | 158 | func Test_win_execute_closing_curwin() |
| 159 | split |
| 160 | let winid = popup_create('some text', {}) |
Bram Moolenaar | 815b76b | 2019-06-01 14:15:52 +0200 | [diff] [blame] | 161 | call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994') |
| 162 | popupclear |
| 163 | endfunc |
| 164 | |
| 165 | func Test_win_execute_not_allowed() |
| 166 | let winid = popup_create('some text', {}) |
| 167 | call assert_fails('call win_execute(winid, "split")', 'E994:') |
| 168 | call assert_fails('call win_execute(winid, "vsplit")', 'E994:') |
| 169 | call assert_fails('call win_execute(winid, "close")', 'E994:') |
| 170 | call assert_fails('call win_execute(winid, "bdelete")', 'E994:') |
Bram Moolenaar | 2d24784 | 2019-06-01 17:06:25 +0200 | [diff] [blame] | 171 | call assert_fails('call win_execute(winid, "bwipe!")', 'E994:') |
Bram Moolenaar | 815b76b | 2019-06-01 14:15:52 +0200 | [diff] [blame] | 172 | call assert_fails('call win_execute(winid, "tabnew")', 'E994:') |
| 173 | call assert_fails('call win_execute(winid, "tabnext")', 'E994:') |
| 174 | call assert_fails('call win_execute(winid, "next")', 'E994:') |
| 175 | call assert_fails('call win_execute(winid, "rewind")', 'E994:') |
| 176 | call assert_fails('call win_execute(winid, "buf")', 'E994:') |
| 177 | call assert_fails('call win_execute(winid, "edit")', 'E994:') |
| 178 | call assert_fails('call win_execute(winid, "enew")', 'E994:') |
| 179 | call assert_fails('call win_execute(winid, "wincmd x")', 'E994:') |
| 180 | call assert_fails('call win_execute(winid, "wincmd w")', 'E994:') |
| 181 | call assert_fails('call win_execute(winid, "wincmd t")', 'E994:') |
| 182 | call assert_fails('call win_execute(winid, "wincmd b")', 'E994:') |
Bram Moolenaar | eea1699 | 2019-05-31 17:34:48 +0200 | [diff] [blame] | 183 | popupclear |
| 184 | endfunc |
| 185 | |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 186 | func Test_popup_with_wrap() |
| 187 | if !CanRunVimInTerminal() |
| 188 | return |
| 189 | endif |
| 190 | let lines =<< trim END |
| 191 | call setline(1, range(1, 100)) |
| 192 | let winid = popup_create( |
| 193 | \ 'a long line that wont fit', |
| 194 | \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1}) |
| 195 | END |
| 196 | call writefile(lines, 'XtestPopup') |
| 197 | let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10}) |
| 198 | call VerifyScreenDump(buf, 'Test_popupwin_wrap', {}) |
| 199 | |
| 200 | " clean up |
| 201 | call StopVimInTerminal(buf) |
| 202 | call delete('XtestPopup') |
| 203 | endfunc |
| 204 | |
| 205 | func Test_popup_without_wrap() |
| 206 | if !CanRunVimInTerminal() |
| 207 | return |
| 208 | endif |
| 209 | let lines =<< trim END |
| 210 | call setline(1, range(1, 100)) |
| 211 | let winid = popup_create( |
| 212 | \ 'a long line that wont fit', |
| 213 | \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0}) |
| 214 | END |
| 215 | call writefile(lines, 'XtestPopup') |
| 216 | let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10}) |
| 217 | call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {}) |
| 218 | |
| 219 | " clean up |
| 220 | call StopVimInTerminal(buf) |
| 221 | call delete('XtestPopup') |
| 222 | endfunc |
| 223 | |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 224 | func Test_popup_time() |
Bram Moolenaar | 35d5af6 | 2019-05-26 20:44:10 +0200 | [diff] [blame] | 225 | if !has('timers') |
| 226 | return |
| 227 | endif |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 228 | topleft vnew |
| 229 | call setline(1, 'hello') |
| 230 | |
| 231 | call popup_create('world', { |
| 232 | \ 'line': 1, |
| 233 | \ 'col': 1, |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 234 | \ 'minwidth': 20, |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 235 | \ 'time': 500, |
| 236 | \}) |
| 237 | redraw |
| 238 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 239 | call assert_equal('world', line) |
| 240 | |
| 241 | sleep 700m |
Bram Moolenaar | 35d5af6 | 2019-05-26 20:44:10 +0200 | [diff] [blame] | 242 | redraw |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 243 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 244 | call assert_equal('hello', line) |
| 245 | |
| 246 | call popup_create('on the command line', { |
| 247 | \ 'line': &lines, |
| 248 | \ 'col': 10, |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 249 | \ 'minwidth': 20, |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 250 | \ 'time': 500, |
| 251 | \}) |
| 252 | redraw |
| 253 | let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '') |
| 254 | call assert_match('.*on the command line.*', line) |
| 255 | |
| 256 | sleep 700m |
| 257 | redraw |
| 258 | let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '') |
| 259 | call assert_notmatch('.*on the command line.*', line) |
| 260 | |
| 261 | bwipe! |
| 262 | endfunc |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 263 | |
| 264 | func Test_popup_hide() |
| 265 | topleft vnew |
| 266 | call setline(1, 'hello') |
| 267 | |
| 268 | let winid = popup_create('world', { |
| 269 | \ 'line': 1, |
| 270 | \ 'col': 1, |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 271 | \ 'minwidth': 20, |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 272 | \}) |
| 273 | redraw |
| 274 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 275 | call assert_equal('world', line) |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 276 | call assert_equal(1, popup_getpos(winid).visible) |
Bram Moolenaar | c6896e2 | 2019-05-30 22:32:34 +0200 | [diff] [blame] | 277 | " buffer is still listed and active |
| 278 | call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u')) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 279 | |
| 280 | call popup_hide(winid) |
| 281 | redraw |
| 282 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 283 | call assert_equal('hello', line) |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 284 | call assert_equal(0, popup_getpos(winid).visible) |
Bram Moolenaar | c6896e2 | 2019-05-30 22:32:34 +0200 | [diff] [blame] | 285 | " buffer is still listed but hidden |
| 286 | call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u')) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 287 | |
| 288 | call popup_show(winid) |
| 289 | redraw |
| 290 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 291 | call assert_equal('world', line) |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 292 | call assert_equal(1, popup_getpos(winid).visible) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 293 | |
| 294 | |
| 295 | call popup_close(winid) |
| 296 | redraw |
| 297 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 298 | call assert_equal('hello', line) |
| 299 | |
| 300 | " error is given for existing non-popup window |
| 301 | call assert_fails('call popup_hide(win_getid())', 'E993:') |
| 302 | |
| 303 | " no error non-existing window |
| 304 | call popup_hide(1234234) |
| 305 | call popup_show(41234234) |
| 306 | |
| 307 | bwipe! |
| 308 | endfunc |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 309 | |
| 310 | func Test_popup_move() |
| 311 | topleft vnew |
| 312 | call setline(1, 'hello') |
| 313 | |
| 314 | let winid = popup_create('world', { |
| 315 | \ 'line': 1, |
| 316 | \ 'col': 1, |
| 317 | \ 'minwidth': 20, |
| 318 | \}) |
| 319 | redraw |
| 320 | let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '') |
| 321 | call assert_equal('world ', line) |
| 322 | |
| 323 | call popup_move(winid, {'line': 2, 'col': 2}) |
| 324 | redraw |
| 325 | let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '') |
| 326 | call assert_equal('hello ', line) |
| 327 | let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '') |
| 328 | call assert_equal('~world', line) |
| 329 | |
| 330 | call popup_move(winid, {'line': 1}) |
| 331 | redraw |
| 332 | let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '') |
| 333 | call assert_equal('hworld', line) |
| 334 | |
| 335 | call popup_close(winid) |
| 336 | |
| 337 | bwipe! |
| 338 | endfunc |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 339 | |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 340 | func Test_popup_getpos() |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 341 | let winid = popup_create('hello', { |
| 342 | \ 'line': 2, |
| 343 | \ 'col': 3, |
| 344 | \ 'minwidth': 10, |
| 345 | \ 'minheight': 11, |
| 346 | \}) |
| 347 | redraw |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 348 | let res = popup_getpos(winid) |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 349 | call assert_equal(2, res.line) |
| 350 | call assert_equal(3, res.col) |
| 351 | call assert_equal(10, res.width) |
| 352 | call assert_equal(11, res.height) |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 353 | call assert_equal(1, res.visible) |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 354 | |
| 355 | call popup_close(winid) |
| 356 | endfunc |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 357 | |
| 358 | func Test_popup_width_longest() |
| 359 | let tests = [ |
| 360 | \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15], |
| 361 | \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15], |
| 362 | \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15], |
| 363 | \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15], |
| 364 | \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15], |
| 365 | \ ] |
| 366 | |
| 367 | for test in tests |
| 368 | let winid = popup_create(test[0], {'line': 2, 'col': 3}) |
| 369 | redraw |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 370 | let position = popup_getpos(winid) |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 371 | call assert_equal(test[1], position.width) |
| 372 | call popup_close(winid) |
| 373 | endfor |
| 374 | endfunc |
| 375 | |
| 376 | func Test_popup_wraps() |
| 377 | let tests = [ |
| 378 | \ ['nowrap', 6, 1], |
| 379 | \ ['a line that wraps once', 12, 2], |
| 380 | \ ['a line that wraps two times', 12, 3], |
| 381 | \ ] |
| 382 | for test in tests |
| 383 | let winid = popup_create(test[0], |
| 384 | \ {'line': 2, 'col': 3, 'maxwidth': 12}) |
| 385 | redraw |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 386 | let position = popup_getpos(winid) |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 387 | call assert_equal(test[1], position.width) |
| 388 | call assert_equal(test[2], position.height) |
| 389 | |
| 390 | call popup_close(winid) |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 391 | call assert_equal({}, popup_getpos(winid)) |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 392 | endfor |
| 393 | endfunc |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 394 | |
| 395 | func Test_popup_getoptions() |
| 396 | let winid = popup_create('hello', { |
| 397 | \ 'line': 2, |
| 398 | \ 'col': 3, |
| 399 | \ 'minwidth': 10, |
| 400 | \ 'minheight': 11, |
| 401 | \ 'maxwidth': 20, |
| 402 | \ 'maxheight': 21, |
| 403 | \ 'zindex': 100, |
| 404 | \ 'time': 5000, |
| 405 | \}) |
| 406 | redraw |
| 407 | let res = popup_getoptions(winid) |
| 408 | call assert_equal(2, res.line) |
| 409 | call assert_equal(3, res.col) |
| 410 | call assert_equal(10, res.minwidth) |
| 411 | call assert_equal(11, res.minheight) |
| 412 | call assert_equal(20, res.maxwidth) |
| 413 | call assert_equal(21, res.maxheight) |
| 414 | call assert_equal(100, res.zindex) |
| 415 | if has('timers') |
| 416 | call assert_equal(5000, res.time) |
| 417 | endif |
| 418 | call popup_close(winid) |
| 419 | |
| 420 | let winid = popup_create('hello', {}) |
| 421 | redraw |
| 422 | let res = popup_getoptions(winid) |
| 423 | call assert_equal(0, res.line) |
| 424 | call assert_equal(0, res.col) |
| 425 | call assert_equal(0, res.minwidth) |
| 426 | call assert_equal(0, res.minheight) |
| 427 | call assert_equal(0, res.maxwidth) |
| 428 | call assert_equal(0, res.maxheight) |
| 429 | call assert_equal(50, res.zindex) |
| 430 | if has('timers') |
| 431 | call assert_equal(0, res.time) |
| 432 | endif |
| 433 | call popup_close(winid) |
| 434 | call assert_equal({}, popup_getoptions(winid)) |
| 435 | endfunc |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 436 | |
| 437 | func Test_popup_option_values() |
| 438 | new |
| 439 | " window-local |
| 440 | setlocal number |
| 441 | setlocal nowrap |
| 442 | " buffer-local |
| 443 | setlocal omnifunc=Something |
| 444 | " global/buffer-local |
| 445 | setlocal path=/there |
| 446 | " global/window-local |
| 447 | setlocal scrolloff=9 |
| 448 | |
| 449 | let winid = popup_create('hello', {}) |
| 450 | call assert_equal(0, getwinvar(winid, '&number')) |
| 451 | call assert_equal(1, getwinvar(winid, '&wrap')) |
| 452 | call assert_equal('', getwinvar(winid, '&omnifunc')) |
| 453 | call assert_equal(&g:path, getwinvar(winid, '&path')) |
| 454 | call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff')) |
| 455 | |
| 456 | call popup_close(winid) |
| 457 | bwipe |
| 458 | endfunc |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 459 | |
| 460 | func Test_popup_atcursor() |
| 461 | topleft vnew |
| 462 | call setline(1, [ |
| 463 | \ 'xxxxxxxxxxxxxxxxx', |
| 464 | \ 'xxxxxxxxxxxxxxxxx', |
| 465 | \ 'xxxxxxxxxxxxxxxxx', |
| 466 | \]) |
| 467 | |
| 468 | call cursor(2, 2) |
| 469 | redraw |
| 470 | let winid = popup_atcursor('vim', {}) |
| 471 | redraw |
| 472 | let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '') |
| 473 | call assert_equal('xvimxxxxxxxxxxxxx', line) |
| 474 | call popup_close(winid) |
| 475 | |
| 476 | call cursor(3, 4) |
| 477 | redraw |
| 478 | let winid = popup_atcursor('vim', {}) |
| 479 | redraw |
| 480 | let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '') |
| 481 | call assert_equal('xxxvimxxxxxxxxxxx', line) |
| 482 | call popup_close(winid) |
| 483 | |
| 484 | call cursor(1, 1) |
| 485 | redraw |
| 486 | let winid = popup_create('vim', { |
| 487 | \ 'line': 'cursor+2', |
| 488 | \ 'col': 'cursor+1', |
| 489 | \}) |
| 490 | redraw |
| 491 | let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '') |
| 492 | call assert_equal('xvimxxxxxxxxxxxxx', line) |
| 493 | call popup_close(winid) |
| 494 | |
| 495 | call cursor(3, 3) |
| 496 | redraw |
| 497 | let winid = popup_create('vim', { |
| 498 | \ 'line': 'cursor-2', |
| 499 | \ 'col': 'cursor-1', |
| 500 | \}) |
| 501 | redraw |
| 502 | let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '') |
| 503 | call assert_equal('xvimxxxxxxxxxxxxx', line) |
| 504 | call popup_close(winid) |
| 505 | |
Bram Moolenaar | 402502d | 2019-05-30 22:07:36 +0200 | [diff] [blame] | 506 | " just enough room above |
| 507 | call cursor(3, 3) |
| 508 | redraw |
| 509 | let winid = popup_atcursor(['vim', 'is great'], {}) |
| 510 | redraw |
| 511 | let pos = popup_getpos(winid) |
| 512 | call assert_equal(1, pos.line) |
| 513 | call popup_close(winid) |
| 514 | |
| 515 | " not enough room above, popup goes below the cursor |
| 516 | call cursor(3, 3) |
| 517 | redraw |
| 518 | let winid = popup_atcursor(['vim', 'is', 'great'], {}) |
| 519 | redraw |
| 520 | let pos = popup_getpos(winid) |
| 521 | call assert_equal(4, pos.line) |
| 522 | call popup_close(winid) |
| 523 | |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 524 | bwipe! |
| 525 | endfunc |
Bram Moolenaar | bf0eff0 | 2019-06-01 17:13:36 +0200 | [diff] [blame] | 526 | |
| 527 | func Test_popup_filter() |
| 528 | new |
| 529 | call setline(1, 'some text') |
| 530 | |
| 531 | func MyPopupFilter(winid, c) |
| 532 | if a:c == 'e' |
| 533 | let g:eaten = 'e' |
| 534 | return 1 |
| 535 | endif |
| 536 | if a:c == '0' |
| 537 | let g:ignored = '0' |
| 538 | return 0 |
| 539 | endif |
| 540 | if a:c == 'x' |
| 541 | call popup_close(a:winid) |
| 542 | return 1 |
| 543 | endif |
| 544 | return 0 |
| 545 | endfunc |
| 546 | |
| 547 | let winid = popup_create('something', {'filter': 'MyPopupFilter'}) |
| 548 | redraw |
| 549 | |
| 550 | " e is consumed by the filter |
| 551 | call feedkeys('e', 'xt') |
| 552 | call assert_equal('e', g:eaten) |
| 553 | |
| 554 | " 0 is ignored by the filter |
| 555 | normal $ |
| 556 | call assert_equal(9, getcurpos()[2]) |
| 557 | call feedkeys('0', 'xt') |
| 558 | call assert_equal('0', g:ignored) |
| 559 | call assert_equal(1, getcurpos()[2]) |
| 560 | |
| 561 | " x closes the popup |
| 562 | call feedkeys('x', 'xt') |
| 563 | call assert_equal('e', g:eaten) |
| 564 | call assert_equal(-1, winbufnr(winid)) |
| 565 | |
| 566 | delfunc MyPopupFilter |
| 567 | popupclear |
| 568 | endfunc |