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 | |
| 59 | func Test_popup_time() |
Bram Moolenaar | 35d5af6 | 2019-05-26 20:44:10 +0200 | [diff] [blame] | 60 | if !has('timers') |
| 61 | return |
| 62 | endif |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 63 | topleft vnew |
| 64 | call setline(1, 'hello') |
| 65 | |
| 66 | call popup_create('world', { |
| 67 | \ 'line': 1, |
| 68 | \ 'col': 1, |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 69 | \ 'minwidth': 20, |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 70 | \ 'time': 500, |
| 71 | \}) |
| 72 | redraw |
| 73 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 74 | call assert_equal('world', line) |
| 75 | |
| 76 | sleep 700m |
Bram Moolenaar | 35d5af6 | 2019-05-26 20:44:10 +0200 | [diff] [blame] | 77 | redraw |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 78 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 79 | call assert_equal('hello', line) |
| 80 | |
| 81 | call popup_create('on the command line', { |
| 82 | \ 'line': &lines, |
| 83 | \ 'col': 10, |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 84 | \ 'minwidth': 20, |
Bram Moolenaar | 51fe3b1 | 2019-05-26 20:10:06 +0200 | [diff] [blame] | 85 | \ 'time': 500, |
| 86 | \}) |
| 87 | redraw |
| 88 | let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '') |
| 89 | call assert_match('.*on the command line.*', line) |
| 90 | |
| 91 | sleep 700m |
| 92 | redraw |
| 93 | let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '') |
| 94 | call assert_notmatch('.*on the command line.*', line) |
| 95 | |
| 96 | bwipe! |
| 97 | endfunc |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 98 | |
| 99 | func Test_popup_hide() |
| 100 | topleft vnew |
| 101 | call setline(1, 'hello') |
| 102 | |
| 103 | let winid = popup_create('world', { |
| 104 | \ 'line': 1, |
| 105 | \ 'col': 1, |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 106 | \ 'minwidth': 20, |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 107 | \}) |
| 108 | redraw |
| 109 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 110 | call assert_equal('world', line) |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame^] | 111 | call assert_equal(1, popup_getposition(winid).visible) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 112 | |
| 113 | call popup_hide(winid) |
| 114 | redraw |
| 115 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 116 | call assert_equal('hello', line) |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame^] | 117 | call assert_equal(0, popup_getposition(winid).visible) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 118 | |
| 119 | call popup_show(winid) |
| 120 | redraw |
| 121 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 122 | call assert_equal('world', line) |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame^] | 123 | call assert_equal(1, popup_getposition(winid).visible) |
Bram Moolenaar | 2cd0dce | 2019-05-26 22:17:52 +0200 | [diff] [blame] | 124 | |
| 125 | |
| 126 | call popup_close(winid) |
| 127 | redraw |
| 128 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 129 | call assert_equal('hello', line) |
| 130 | |
| 131 | " error is given for existing non-popup window |
| 132 | call assert_fails('call popup_hide(win_getid())', 'E993:') |
| 133 | |
| 134 | " no error non-existing window |
| 135 | call popup_hide(1234234) |
| 136 | call popup_show(41234234) |
| 137 | |
| 138 | bwipe! |
| 139 | endfunc |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 140 | |
| 141 | func Test_popup_move() |
| 142 | topleft vnew |
| 143 | call setline(1, 'hello') |
| 144 | |
| 145 | let winid = popup_create('world', { |
| 146 | \ 'line': 1, |
| 147 | \ 'col': 1, |
| 148 | \ 'minwidth': 20, |
| 149 | \}) |
| 150 | redraw |
| 151 | let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '') |
| 152 | call assert_equal('world ', line) |
| 153 | |
| 154 | call popup_move(winid, {'line': 2, 'col': 2}) |
| 155 | redraw |
| 156 | let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '') |
| 157 | call assert_equal('hello ', line) |
| 158 | let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '') |
| 159 | call assert_equal('~world', line) |
| 160 | |
| 161 | call popup_move(winid, {'line': 1}) |
| 162 | redraw |
| 163 | let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '') |
| 164 | call assert_equal('hworld', line) |
| 165 | |
| 166 | call popup_close(winid) |
| 167 | |
| 168 | bwipe! |
| 169 | endfunc |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 170 | |
| 171 | func Test_popup_getposition() |
| 172 | let winid = popup_create('hello', { |
| 173 | \ 'line': 2, |
| 174 | \ 'col': 3, |
| 175 | \ 'minwidth': 10, |
| 176 | \ 'minheight': 11, |
| 177 | \}) |
| 178 | redraw |
| 179 | let res = popup_getposition(winid) |
| 180 | call assert_equal(2, res.line) |
| 181 | call assert_equal(3, res.col) |
| 182 | call assert_equal(10, res.width) |
| 183 | call assert_equal(11, res.height) |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame^] | 184 | call assert_equal(1, res.visible) |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 185 | |
| 186 | call popup_close(winid) |
| 187 | endfunc |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 188 | |
| 189 | func Test_popup_width_longest() |
| 190 | let tests = [ |
| 191 | \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15], |
| 192 | \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15], |
| 193 | \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15], |
| 194 | \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15], |
| 195 | \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15], |
| 196 | \ ] |
| 197 | |
| 198 | for test in tests |
| 199 | let winid = popup_create(test[0], {'line': 2, 'col': 3}) |
| 200 | redraw |
| 201 | let position = popup_getposition(winid) |
| 202 | call assert_equal(test[1], position.width) |
| 203 | call popup_close(winid) |
| 204 | endfor |
| 205 | endfunc |
| 206 | |
| 207 | func Test_popup_wraps() |
| 208 | let tests = [ |
| 209 | \ ['nowrap', 6, 1], |
| 210 | \ ['a line that wraps once', 12, 2], |
| 211 | \ ['a line that wraps two times', 12, 3], |
| 212 | \ ] |
| 213 | for test in tests |
| 214 | let winid = popup_create(test[0], |
| 215 | \ {'line': 2, 'col': 3, 'maxwidth': 12}) |
| 216 | redraw |
| 217 | let position = popup_getposition(winid) |
| 218 | call assert_equal(test[1], position.width) |
| 219 | call assert_equal(test[2], position.height) |
| 220 | |
| 221 | call popup_close(winid) |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame^] | 222 | call assert_equal({}, popup_getposition(winid)) |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 223 | endfor |
| 224 | endfunc |
Bram Moolenaar | 8c2a600 | 2019-05-30 14:29:45 +0200 | [diff] [blame^] | 225 | |
| 226 | func Test_popup_getoptions() |
| 227 | let winid = popup_create('hello', { |
| 228 | \ 'line': 2, |
| 229 | \ 'col': 3, |
| 230 | \ 'minwidth': 10, |
| 231 | \ 'minheight': 11, |
| 232 | \ 'maxwidth': 20, |
| 233 | \ 'maxheight': 21, |
| 234 | \ 'zindex': 100, |
| 235 | \ 'time': 5000, |
| 236 | \}) |
| 237 | redraw |
| 238 | let res = popup_getoptions(winid) |
| 239 | call assert_equal(2, res.line) |
| 240 | call assert_equal(3, res.col) |
| 241 | call assert_equal(10, res.minwidth) |
| 242 | call assert_equal(11, res.minheight) |
| 243 | call assert_equal(20, res.maxwidth) |
| 244 | call assert_equal(21, res.maxheight) |
| 245 | call assert_equal(100, res.zindex) |
| 246 | if has('timers') |
| 247 | call assert_equal(5000, res.time) |
| 248 | endif |
| 249 | call popup_close(winid) |
| 250 | |
| 251 | let winid = popup_create('hello', {}) |
| 252 | redraw |
| 253 | let res = popup_getoptions(winid) |
| 254 | call assert_equal(0, res.line) |
| 255 | call assert_equal(0, res.col) |
| 256 | call assert_equal(0, res.minwidth) |
| 257 | call assert_equal(0, res.minheight) |
| 258 | call assert_equal(0, res.maxwidth) |
| 259 | call assert_equal(0, res.maxheight) |
| 260 | call assert_equal(50, res.zindex) |
| 261 | if has('timers') |
| 262 | call assert_equal(0, res.time) |
| 263 | endif |
| 264 | call popup_close(winid) |
| 265 | call assert_equal({}, popup_getoptions(winid)) |
| 266 | endfunc |