blob: a11e5a03f34457d115f1016acbc0ef0d8a398a50 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
3if !has('textprop')
4 finish
5endif
6
7source screendump.vim
8
9func Test_simple_popup()
10 if !CanRunVimInTerminal()
11 return
12 endif
13 call writefile([
14 \ "call setline(1, range(1, 100))",
Bram Moolenaar20c023a2019-05-26 21:03:24 +020015 \ "hi PopupColor1 ctermbg=lightblue",
16 \ "hi PopupColor2 ctermbg=lightcyan",
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020017 \ "hi Comment ctermfg=red",
18 \ "call prop_type_add('comment', {'highlight': 'Comment'})",
Bram Moolenaar60cdb302019-05-27 21:54:10 +020019 \ "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 Moolenaar20c023a2019-05-26 21:03:24 +020021 \ "call setwinvar(winid2, '&wincolor', 'PopupColor2')",
Bram Moolenaar4d784b22019-05-25 19:51:39 +020022 \], 'XtestPopup')
23 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
24 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020029 \ .. "{'text': 'other tab'},"
30 \ .. "{'text': 'a comment line', 'props': [{"
Bram Moolenaar60cdb302019-05-27 21:54:10 +020031 \ .. "'col': 3, 'length': 7, 'minwidth': 20, 'type': 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar60cdb302019-05-27 21:54:10 +020033 \ .. "], {'line': 4, 'col': 9, 'minwidth': 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 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 Moolenaar17146962019-05-30 00:12:11 +020044 " resize popup, show empty line at bottom
Bram Moolenaar60cdb302019-05-27 21:54:10 +020045 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 Moolenaar17146962019-05-30 00:12:11 +020049 " 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 Moolenaar4d784b22019-05-25 19:51:39 +020054 " clean up
55 call StopVimInTerminal(buf)
56 call delete('XtestPopup')
57endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020058
59func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +020060 if !has('timers')
61 return
62 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020063 topleft vnew
64 call setline(1, 'hello')
65
66 call popup_create('world', {
67 \ 'line': 1,
68 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +020069 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020070 \ '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 Moolenaar35d5af62019-05-26 20:44:10 +020077 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020078 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 Moolenaar60cdb302019-05-27 21:54:10 +020084 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020085 \ '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!
97endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +020098
99func 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 Moolenaar60cdb302019-05-27 21:54:10 +0200106 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200107 \})
108 redraw
109 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
110 call assert_equal('world', line)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200111 call assert_equal(1, popup_getposition(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200112
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 Moolenaar8c2a6002019-05-30 14:29:45 +0200117 call assert_equal(0, popup_getposition(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200118
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 Moolenaar8c2a6002019-05-30 14:29:45 +0200123 call assert_equal(1, popup_getposition(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200124
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!
139endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200140
141func 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!
169endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200170
171func 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 Moolenaar8c2a6002019-05-30 14:29:45 +0200184 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200185
186 call popup_close(winid)
187endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200188
189func 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
205endfunc
206
207func 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 Moolenaar8c2a6002019-05-30 14:29:45 +0200222 call assert_equal({}, popup_getposition(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200223 endfor
224endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200225
226func 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))
266endfunc