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