blob: 4511cd1ac0351b5785fe123f99000437010284c0 [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)
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!
136endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200137
138func 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!
166endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200167
168func 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)
183endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200184
185func 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
201endfunc
202
203func 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
219endfunc