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) |
| 111 | |
| 112 | call popup_hide(winid) |
| 113 | redraw |
| 114 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 115 | call assert_equal('hello', line) |
| 116 | |
| 117 | call popup_show(winid) |
| 118 | redraw |
| 119 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 120 | call assert_equal('world', line) |
| 121 | |
| 122 | |
| 123 | call popup_close(winid) |
| 124 | redraw |
| 125 | let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '') |
| 126 | call assert_equal('hello', line) |
| 127 | |
| 128 | " error is given for existing non-popup window |
| 129 | call assert_fails('call popup_hide(win_getid())', 'E993:') |
| 130 | |
| 131 | " no error non-existing window |
| 132 | call popup_hide(1234234) |
| 133 | call popup_show(41234234) |
| 134 | |
| 135 | bwipe! |
| 136 | endfunc |
Bram Moolenaar | 60cdb30 | 2019-05-27 21:54:10 +0200 | [diff] [blame] | 137 | |
| 138 | func Test_popup_move() |
| 139 | topleft vnew |
| 140 | call setline(1, 'hello') |
| 141 | |
| 142 | let winid = popup_create('world', { |
| 143 | \ 'line': 1, |
| 144 | \ 'col': 1, |
| 145 | \ 'minwidth': 20, |
| 146 | \}) |
| 147 | redraw |
| 148 | let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '') |
| 149 | call assert_equal('world ', line) |
| 150 | |
| 151 | call popup_move(winid, {'line': 2, 'col': 2}) |
| 152 | redraw |
| 153 | let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '') |
| 154 | call assert_equal('hello ', line) |
| 155 | let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '') |
| 156 | call assert_equal('~world', line) |
| 157 | |
| 158 | call popup_move(winid, {'line': 1}) |
| 159 | redraw |
| 160 | let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '') |
| 161 | call assert_equal('hworld', line) |
| 162 | |
| 163 | call popup_close(winid) |
| 164 | |
| 165 | bwipe! |
| 166 | endfunc |
Bram Moolenaar | bc13354 | 2019-05-29 20:26:46 +0200 | [diff] [blame] | 167 | |
| 168 | func Test_popup_getposition() |
| 169 | let winid = popup_create('hello', { |
| 170 | \ 'line': 2, |
| 171 | \ 'col': 3, |
| 172 | \ 'minwidth': 10, |
| 173 | \ 'minheight': 11, |
| 174 | \}) |
| 175 | redraw |
| 176 | let res = popup_getposition(winid) |
| 177 | call assert_equal(2, res.line) |
| 178 | call assert_equal(3, res.col) |
| 179 | call assert_equal(10, res.width) |
| 180 | call assert_equal(11, res.height) |
| 181 | |
| 182 | call popup_close(winid) |
| 183 | endfunc |
Bram Moolenaar | 88c4e1f | 2019-05-29 23:14:28 +0200 | [diff] [blame] | 184 | |
| 185 | func Test_popup_width_longest() |
| 186 | let tests = [ |
| 187 | \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15], |
| 188 | \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15], |
| 189 | \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15], |
| 190 | \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15], |
| 191 | \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15], |
| 192 | \ ] |
| 193 | |
| 194 | for test in tests |
| 195 | let winid = popup_create(test[0], {'line': 2, 'col': 3}) |
| 196 | redraw |
| 197 | let position = popup_getposition(winid) |
| 198 | call assert_equal(test[1], position.width) |
| 199 | call popup_close(winid) |
| 200 | endfor |
| 201 | endfunc |
| 202 | |
| 203 | func Test_popup_wraps() |
| 204 | let tests = [ |
| 205 | \ ['nowrap', 6, 1], |
| 206 | \ ['a line that wraps once', 12, 2], |
| 207 | \ ['a line that wraps two times', 12, 3], |
| 208 | \ ] |
| 209 | for test in tests |
| 210 | let winid = popup_create(test[0], |
| 211 | \ {'line': 2, 'col': 3, 'maxwidth': 12}) |
| 212 | redraw |
| 213 | let position = popup_getposition(winid) |
| 214 | call assert_equal(test[1], position.width) |
| 215 | call assert_equal(test[2], position.height) |
| 216 | |
| 217 | call popup_close(winid) |
| 218 | endfor |
| 219 | endfunc |