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 | b423012 | 2019-05-30 18:40:53 +0200 | [diff] [blame] | 59 | func Test_popup_with_syntax_win_execute() |
| 60 | if !CanRunVimInTerminal() |
| 61 | return |
| 62 | endif |
| 63 | call writefile([ |
| 64 | \ "call setline(1, range(1, 100))", |
| 65 | \ "hi PopupColor ctermbg=lightblue", |
| 66 | \ "let winid = popup_create([", |
| 67 | \ "\\ '#include <stdio.h>',", |
| 68 | \ "\\ 'int main(void)',", |
| 69 | \ "\\ '{',", |
| 70 | \ "\\ ' printf(123);',", |
| 71 | \ "\\ '}',", |
| 72 | \ "\\], {'line': 3, 'col': 25, 'highlight': 'PopupColor'})", |
| 73 | \ "call win_execute(winid, 'set syntax=cpp')", |
| 74 | \], 'XtestPopup') |
| 75 | let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10}) |
| 76 | call VerifyScreenDump(buf, 'Test_popupwin_10', {}) |
| 77 | |
| 78 | " clean up |
| 79 | call StopVimInTerminal(buf) |
| 80 | call delete('XtestPopup') |
| 81 | endfunc |
| 82 | |
| 83 | func Test_popup_with_syntax_setbufvar() |
| 84 | if !CanRunVimInTerminal() |
| 85 | return |
| 86 | endif |
| 87 | call writefile([ |
| 88 | \ "call setline(1, range(1, 100))", |
| 89 | \ "hi PopupColor ctermbg=lightgrey", |
| 90 | \ "let winid = popup_create([", |
| 91 | \ "\\ '#include <stdio.h>',", |
| 92 | \ "\\ 'int main(void)',", |
| 93 | \ "\\ '{',", |
| 94 | \ "\\ ' printf(567);',", |
| 95 | \ "\\ '}',", |
| 96 | \ "\\], {'line': 3, 'col': 21, 'highlight': 'PopupColor'})", |
| 97 | \ "call setbufvar(winbufnr(winid), '&syntax', 'cpp')", |
| 98 | \], 'XtestPopup') |
| 99 | let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10}) |
| 100 | call VerifyScreenDump(buf, 'Test_popupwin_11', {}) |
| 101 | |
| 102 | " clean up |
| 103 | call StopVimInTerminal(buf) |
| 104 | call delete('XtestPopup') |
| 105 | endfunc |
| 106 | |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 107 | func Test_popup_time() |
Bram Moolenaar | 35d5af6 | 2019-05-26 20:44:10 +0200 | [diff] [blame] | 108 | if !has('timers') |
| 109 | return |
| 110 | endif |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 111 | topleft vnew |
| 112 | call setline(1, 'hello') |
| 113 | |
| 114 | call popup_create('world', { |
| 115 | \ 'line': 1, |
| 116 | \ 'col': 1, |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 117 | \ 'minwidth': 20, |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 118 | \ 'time': 500, |
| 119 | \}) |
| 120 | redraw |
| 121 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 122 | call assert_equal('world', line) |
| 123 | |
| 124 | sleep 700m |
Bram Moolenaar | 35d5af6 | 2019-05-26 20:44:10 +0200 | [diff] [blame] | 125 | redraw |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 126 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 127 | call assert_equal('hello', line) |
| 128 | |
| 129 | call popup_create('on the command line', { |
| 130 | \ 'line': &lines, |
| 131 | \ 'col': 10, |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 132 | \ 'minwidth': 20, |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 133 | \ 'time': 500, |
| 134 | \}) |
| 135 | redraw |
| 136 | let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '') |
| 137 | call assert_match('.*on the command line.*', line) |
| 138 | |
| 139 | sleep 700m |
| 140 | redraw |
| 141 | let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '') |
| 142 | call assert_notmatch('.*on the command line.*', line) |
| 143 | |
| 144 | bwipe! |
| 145 | endfunc |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 146 | |
| 147 | func Test_popup_hide() |
| 148 | topleft vnew |
| 149 | call setline(1, 'hello') |
| 150 | |
| 151 | let winid = popup_create('world', { |
| 152 | \ 'line': 1, |
| 153 | \ 'col': 1, |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 154 | \ 'minwidth': 20, |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 155 | \}) |
| 156 | redraw |
| 157 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 158 | call assert_equal('world', line) |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 159 | call assert_equal(1, popup_getposition(winid).visible) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 160 | |
| 161 | call popup_hide(winid) |
| 162 | redraw |
| 163 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 164 | call assert_equal('hello', line) |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 165 | call assert_equal(0, popup_getposition(winid).visible) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 166 | |
| 167 | call popup_show(winid) |
| 168 | redraw |
| 169 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 170 | call assert_equal('world', line) |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 171 | call assert_equal(1, popup_getposition(winid).visible) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 172 | |
| 173 | |
| 174 | call popup_close(winid) |
| 175 | redraw |
| 176 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 177 | call assert_equal('hello', line) |
| 178 | |
| 179 | " error is given for existing non-popup window |
| 180 | call assert_fails('call popup_hide(win_getid())', 'E993:') |
| 181 | |
| 182 | " no error non-existing window |
| 183 | call popup_hide(1234234) |
| 184 | call popup_show(41234234) |
| 185 | |
| 186 | bwipe! |
| 187 | endfunc |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 188 | |
| 189 | func Test_popup_move() |
| 190 | topleft vnew |
| 191 | call setline(1, 'hello') |
| 192 | |
| 193 | let winid = popup_create('world', { |
| 194 | \ 'line': 1, |
| 195 | \ 'col': 1, |
| 196 | \ 'minwidth': 20, |
| 197 | \}) |
| 198 | redraw |
| 199 | let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '') |
| 200 | call assert_equal('world ', line) |
| 201 | |
| 202 | call popup_move(winid, {'line': 2, 'col': 2}) |
| 203 | redraw |
| 204 | let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '') |
| 205 | call assert_equal('hello ', line) |
| 206 | let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '') |
| 207 | call assert_equal('~world', line) |
| 208 | |
| 209 | call popup_move(winid, {'line': 1}) |
| 210 | redraw |
| 211 | let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '') |
| 212 | call assert_equal('hworld', line) |
| 213 | |
| 214 | call popup_close(winid) |
| 215 | |
| 216 | bwipe! |
| 217 | endfunc |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 218 | |
| 219 | func Test_popup_getposition() |
| 220 | let winid = popup_create('hello', { |
| 221 | \ 'line': 2, |
| 222 | \ 'col': 3, |
| 223 | \ 'minwidth': 10, |
| 224 | \ 'minheight': 11, |
| 225 | \}) |
| 226 | redraw |
| 227 | let res = popup_getposition(winid) |
| 228 | call assert_equal(2, res.line) |
| 229 | call assert_equal(3, res.col) |
| 230 | call assert_equal(10, res.width) |
| 231 | call assert_equal(11, res.height) |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 232 | call assert_equal(1, res.visible) |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 233 | |
| 234 | call popup_close(winid) |
| 235 | endfunc |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 236 | |
| 237 | func Test_popup_width_longest() |
| 238 | let tests = [ |
| 239 | \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15], |
| 240 | \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15], |
| 241 | \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15], |
| 242 | \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15], |
| 243 | \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15], |
| 244 | \ ] |
| 245 | |
| 246 | for test in tests |
| 247 | let winid = popup_create(test[0], {'line': 2, 'col': 3}) |
| 248 | redraw |
| 249 | let position = popup_getposition(winid) |
| 250 | call assert_equal(test[1], position.width) |
| 251 | call popup_close(winid) |
| 252 | endfor |
| 253 | endfunc |
| 254 | |
| 255 | func Test_popup_wraps() |
| 256 | let tests = [ |
| 257 | \ ['nowrap', 6, 1], |
| 258 | \ ['a line that wraps once', 12, 2], |
| 259 | \ ['a line that wraps two times', 12, 3], |
| 260 | \ ] |
| 261 | for test in tests |
| 262 | let winid = popup_create(test[0], |
| 263 | \ {'line': 2, 'col': 3, 'maxwidth': 12}) |
| 264 | redraw |
| 265 | let position = popup_getposition(winid) |
| 266 | call assert_equal(test[1], position.width) |
| 267 | call assert_equal(test[2], position.height) |
| 268 | |
| 269 | call popup_close(winid) |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 270 | call assert_equal({}, popup_getposition(winid)) |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 271 | endfor |
| 272 | endfunc |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame] | 273 | |
| 274 | func Test_popup_getoptions() |
| 275 | let winid = popup_create('hello', { |
| 276 | \ 'line': 2, |
| 277 | \ 'col': 3, |
| 278 | \ 'minwidth': 10, |
| 279 | \ 'minheight': 11, |
| 280 | \ 'maxwidth': 20, |
| 281 | \ 'maxheight': 21, |
| 282 | \ 'zindex': 100, |
| 283 | \ 'time': 5000, |
| 284 | \}) |
| 285 | redraw |
| 286 | let res = popup_getoptions(winid) |
| 287 | call assert_equal(2, res.line) |
| 288 | call assert_equal(3, res.col) |
| 289 | call assert_equal(10, res.minwidth) |
| 290 | call assert_equal(11, res.minheight) |
| 291 | call assert_equal(20, res.maxwidth) |
| 292 | call assert_equal(21, res.maxheight) |
| 293 | call assert_equal(100, res.zindex) |
| 294 | if has('timers') |
| 295 | call assert_equal(5000, res.time) |
| 296 | endif |
| 297 | call popup_close(winid) |
| 298 | |
| 299 | let winid = popup_create('hello', {}) |
| 300 | redraw |
| 301 | let res = popup_getoptions(winid) |
| 302 | call assert_equal(0, res.line) |
| 303 | call assert_equal(0, res.col) |
| 304 | call assert_equal(0, res.minwidth) |
| 305 | call assert_equal(0, res.minheight) |
| 306 | call assert_equal(0, res.maxwidth) |
| 307 | call assert_equal(0, res.maxheight) |
| 308 | call assert_equal(50, res.zindex) |
| 309 | if has('timers') |
| 310 | call assert_equal(0, res.time) |
| 311 | endif |
| 312 | call popup_close(winid) |
| 313 | call assert_equal({}, popup_getoptions(winid)) |
| 314 | endfunc |
Bram Moolenaar | cacc6a5 | 2019-05-30 15:22:43 +0200 | [diff] [blame] | 315 | |
| 316 | func Test_popup_option_values() |
| 317 | new |
| 318 | " window-local |
| 319 | setlocal number |
| 320 | setlocal nowrap |
| 321 | " buffer-local |
| 322 | setlocal omnifunc=Something |
| 323 | " global/buffer-local |
| 324 | setlocal path=/there |
| 325 | " global/window-local |
| 326 | setlocal scrolloff=9 |
| 327 | |
| 328 | let winid = popup_create('hello', {}) |
| 329 | call assert_equal(0, getwinvar(winid, '&number')) |
| 330 | call assert_equal(1, getwinvar(winid, '&wrap')) |
| 331 | call assert_equal('', getwinvar(winid, '&omnifunc')) |
| 332 | call assert_equal(&g:path, getwinvar(winid, '&path')) |
| 333 | call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff')) |
| 334 | |
| 335 | call popup_close(winid) |
| 336 | bwipe |
| 337 | endfunc |
Bram Moolenaar | cc31ad9 | 2019-05-30 19:25:06 +0200 | [diff] [blame] | 338 | |
| 339 | func Test_popup_atcursor() |
| 340 | topleft vnew |
| 341 | call setline(1, [ |
| 342 | \ 'xxxxxxxxxxxxxxxxx', |
| 343 | \ 'xxxxxxxxxxxxxxxxx', |
| 344 | \ 'xxxxxxxxxxxxxxxxx', |
| 345 | \]) |
| 346 | |
| 347 | call cursor(2, 2) |
| 348 | redraw |
| 349 | let winid = popup_atcursor('vim', {}) |
| 350 | redraw |
| 351 | let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '') |
| 352 | call assert_equal('xvimxxxxxxxxxxxxx', line) |
| 353 | call popup_close(winid) |
| 354 | |
| 355 | call cursor(3, 4) |
| 356 | redraw |
| 357 | let winid = popup_atcursor('vim', {}) |
| 358 | redraw |
| 359 | let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '') |
| 360 | call assert_equal('xxxvimxxxxxxxxxxx', line) |
| 361 | call popup_close(winid) |
| 362 | |
| 363 | call cursor(1, 1) |
| 364 | redraw |
| 365 | let winid = popup_create('vim', { |
| 366 | \ 'line': 'cursor+2', |
| 367 | \ 'col': 'cursor+1', |
| 368 | \}) |
| 369 | redraw |
| 370 | let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '') |
| 371 | call assert_equal('xvimxxxxxxxxxxxxx', line) |
| 372 | call popup_close(winid) |
| 373 | |
| 374 | call cursor(3, 3) |
| 375 | redraw |
| 376 | let winid = popup_create('vim', { |
| 377 | \ 'line': 'cursor-2', |
| 378 | \ 'col': 'cursor-1', |
| 379 | \}) |
| 380 | redraw |
| 381 | let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '') |
| 382 | call assert_equal('xvimxxxxxxxxxxxxx', line) |
| 383 | call popup_close(winid) |
| 384 | |
| 385 | bwipe! |
| 386 | endfunc |