blob: 06a33d4bad73568ff83799ff1314444feb79cc62 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
3if !has('textprop')
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02004 throw 'Skipped: textprop feature missing'
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005endif
6
7source screendump.vim
8
9func Test_simple_popup()
10 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020011 throw 'Skipped: cannot make screendumps'
Bram Moolenaar4d784b22019-05-25 19:51:39 +020012 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 Moolenaar202d9822019-06-11 21:56:30 +020044 " set 'columns' to a small value, size must be recomputed
45 call term_sendkeys(buf, ":let cols = &columns\<CR>")
46 call term_sendkeys(buf, ":set columns=12\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
48 call term_sendkeys(buf, ":let &columns = cols\<CR>")
49
Bram Moolenaar17146962019-05-30 00:12:11 +020050 " resize popup, show empty line at bottom
Bram Moolenaar60cdb302019-05-27 21:54:10 +020051 call term_sendkeys(buf, ":call popup_move(popupwin, {'minwidth': 15, 'maxwidth': 25, 'minheight': 3, 'maxheight': 5})\<CR>")
52 call term_sendkeys(buf, ":redraw\<CR>")
53 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
54
Bram Moolenaar17146962019-05-30 00:12:11 +020055 " show not fitting line at bottom
56 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
57 call term_sendkeys(buf, ":redraw\<CR>")
58 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
59
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020060 " move popup over ruler
61 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
62 call term_sendkeys(buf, ":call popup_move(popupwin, {'line': 7, 'col': 55})\<CR>")
63 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
64
65 " clear all popups after moving the cursor a bit, so that ruler is updated
66 call term_sendkeys(buf, "axxx\<Esc>")
67 call term_wait(buf)
68 call term_sendkeys(buf, "0")
69 call term_wait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020070 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020071 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
72
Bram Moolenaar4d784b22019-05-25 19:51:39 +020073 " clean up
74 call StopVimInTerminal(buf)
75 call delete('XtestPopup')
76endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020077
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020078func Test_popup_with_border_and_padding()
79 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +020080 throw 'Skipped: cannot make screendumps'
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081 endif
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020082
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020083 for iter in range(0, 1)
84 call writefile([iter == 1 ? '' : 'set enc=latin1',
85 \ "call setline(1, range(1, 100))",
86 \ "call popup_create('hello border', {'line': 2, 'col': 3, 'border': []})",
87 \ "call popup_create('hello padding', {'line': 2, 'col': 23, 'padding': []})",
88 \ "call popup_create('hello both', {'line': 2, 'col': 43, 'border': [], 'padding': []})",
89 \ "call popup_create('border TL', {'line': 6, 'col': 3, 'border': [1, 0, 0, 4]})",
90 \ "call popup_create('paddings', {'line': 6, 'col': 23, 'padding': [1, 3, 2, 4]})",
91 \], 'XtestPopupBorder')
92 let buf = RunVimInTerminal('-S XtestPopupBorder', {'rows': 15})
93 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
94
95 call StopVimInTerminal(buf)
96 call delete('XtestPopupBorder')
97 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020098
Bram Moolenaar790498b2019-06-01 22:15:29 +020099 call writefile([
100 \ "call setline(1, range(1, 100))",
101 \ "hi BlueColor ctermbg=lightblue",
102 \ "hi TopColor ctermbg=253",
103 \ "hi RightColor ctermbg=245",
104 \ "hi BottomColor ctermbg=240",
105 \ "hi LeftColor ctermbg=248",
106 \ "call popup_create('hello border', {'line': 2, 'col': 3, 'border': [], 'borderhighlight': ['BlueColor']})",
107 \ "call popup_create(['hello border', 'and more'], {'line': 2, 'col': 23, 'border': [], 'borderhighlight': ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})",
108 \ "call popup_create(['hello border', 'lines only'], {'line': 2, 'col': 43, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['x']})",
109 \ "call popup_create(['hello border', 'with corners'], {'line': 2, 'col': 60, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['x', '#']})",
110 \ "call popup_create(['hello border', 'with numbers'], {'line': 6, 'col': 3, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': ['0', '1', '2', '3', '4', '5', '6', '7']})",
111 \ "call popup_create(['hello border', 'just blanks'], {'line': 7, 'col': 23, 'border': [], 'borderhighlight': ['BlueColor'], 'borderchars': [' ']})",
112 \], 'XtestPopupBorder')
113 let buf = RunVimInTerminal('-S XtestPopupBorder', {'rows': 12})
114 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
115
116 call StopVimInTerminal(buf)
117 call delete('XtestPopupBorder')
118
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200119 let with_border_or_padding = {
120 \ 'line': 2,
121 \ 'core_line': 3,
122 \ 'col': 3,
123 \ 'core_col': 4,
124 \ 'width': 14,
125 \ 'core_width': 12,
126 \ 'height': 3,
127 \ 'core_height': 1,
128 \ 'visible': 1}
129 let winid = popup_create('hello border', {'line': 2, 'col': 3, 'border': []})",
130 call assert_equal(with_border_or_padding, popup_getpos(winid))
131
132 let winid = popup_create('hello paddng', {'line': 2, 'col': 3, 'padding': []})
133 call assert_equal(with_border_or_padding, popup_getpos(winid))
134
135 let winid = popup_create('hello both', {'line': 3, 'col': 8, 'border': [], 'padding': []})
136 call assert_equal({
137 \ 'line': 3,
138 \ 'core_line': 5,
139 \ 'col': 8,
140 \ 'core_col': 10,
141 \ 'width': 14,
142 \ 'core_width': 10,
143 \ 'height': 5,
144 \ 'core_height': 1,
145 \ 'visible': 1}, popup_getpos(winid))
146endfunc
147
Bram Moolenaarb4230122019-05-30 18:40:53 +0200148func Test_popup_with_syntax_win_execute()
149 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200150 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200151 endif
152 call writefile([
153 \ "call setline(1, range(1, 100))",
154 \ "hi PopupColor ctermbg=lightblue",
155 \ "let winid = popup_create([",
156 \ "\\ '#include <stdio.h>',",
157 \ "\\ 'int main(void)',",
158 \ "\\ '{',",
159 \ "\\ ' printf(123);',",
160 \ "\\ '}',",
161 \ "\\], {'line': 3, 'col': 25, 'highlight': 'PopupColor'})",
162 \ "call win_execute(winid, 'set syntax=cpp')",
163 \], 'XtestPopup')
164 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
165 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
166
167 " clean up
168 call StopVimInTerminal(buf)
169 call delete('XtestPopup')
170endfunc
171
172func Test_popup_with_syntax_setbufvar()
173 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200174 throw 'Skipped: cannot make screendumps'
Bram Moolenaarb4230122019-05-30 18:40:53 +0200175 endif
Bram Moolenaar402502d2019-05-30 22:07:36 +0200176 let lines =<< trim END
177 call setline(1, range(1, 100))
178 hi PopupColor ctermbg=lightgrey
179 let winid = popup_create([
180 \ '#include <stdio.h>',
181 \ 'int main(void)',
182 \ '{',
183 \ ' printf(567);',
184 \ '}',
185 \], {'line': 3, 'col': 21, 'highlight': 'PopupColor'})
186 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
187 END
188 call writefile(lines, 'XtestPopup')
Bram Moolenaarb4230122019-05-30 18:40:53 +0200189 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
190 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
191
192 " clean up
193 call StopVimInTerminal(buf)
194 call delete('XtestPopup')
195endfunc
196
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200197func Test_popup_with_matches()
198 if !CanRunVimInTerminal()
199 throw 'Skipped: cannot make screendumps'
200 endif
201 let lines =<< trim END
202 call setline(1, ['111 222 333', '444 555 666'])
203 let winid = popup_create([
204 \ '111 222 333',
205 \ '444 555 666',
206 \], {'line': 3, 'col': 10, 'border': []})
207 set hlsearch
208 /666
209 call matchadd('ErrorMsg', '111')
210 call matchadd('ErrorMsg', '444')
211 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
212 call win_execute(winid, "call matchadd('ErrorMsg', '555')")
213 END
214 call writefile(lines, 'XtestPopupMatches')
215 let buf = RunVimInTerminal('-S XtestPopupMatches', {'rows': 10})
216 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
217
218 " clean up
219 call StopVimInTerminal(buf)
220 call delete('XtestPopupMatches')
221endfunc
222
Bram Moolenaar399d8982019-06-02 15:34:29 +0200223func Test_popup_all_corners()
224 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200225 throw 'Skipped: cannot make screendumps'
Bram Moolenaar399d8982019-06-02 15:34:29 +0200226 endif
227 let lines =<< trim END
228 call setline(1, repeat([repeat('-', 60)], 15))
229 set so=0
230 normal 2G3|r#
231 let winid1 = popup_create(['first', 'second'], {
232 \ 'line': 'cursor+1',
233 \ 'col': 'cursor',
234 \ 'pos': 'topleft',
235 \ 'border': [],
236 \ 'padding': [],
237 \ })
238 normal 25|r@
239 let winid1 = popup_create(['First', 'SeconD'], {
240 \ 'line': 'cursor+1',
241 \ 'col': 'cursor',
242 \ 'pos': 'topright',
243 \ 'border': [],
244 \ 'padding': [],
245 \ })
246 normal 9G29|r%
247 let winid1 = popup_create(['fiRSt', 'seCOnd'], {
248 \ 'line': 'cursor-1',
249 \ 'col': 'cursor',
250 \ 'pos': 'botleft',
251 \ 'border': [],
252 \ 'padding': [],
253 \ })
254 normal 51|r&
255 let winid1 = popup_create(['FIrsT', 'SEcoND'], {
256 \ 'line': 'cursor-1',
257 \ 'col': 'cursor',
258 \ 'pos': 'botright',
259 \ 'border': [],
260 \ 'padding': [],
261 \ })
262 END
263 call writefile(lines, 'XtestPopupCorners')
264 let buf = RunVimInTerminal('-S XtestPopupCorners', {'rows': 12})
265 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
266
267 " clean up
268 call StopVimInTerminal(buf)
269 call delete('XtestPopupCorners')
270endfunc
271
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200272func Test_popup_in_tab()
273 " default popup is local to tab, not visible when in other tab
274 let winid = popup_create("text", {})
275 call assert_equal(1, popup_getpos(winid).visible)
276 tabnew
277 call assert_equal(0, popup_getpos(winid).visible)
278 quit
279 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200280 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200281
282 " global popup is visible in any tab
283 let winid = popup_create("text", {'tab': -1})
284 call assert_equal(1, popup_getpos(winid).visible)
285 tabnew
286 call assert_equal(1, popup_getpos(winid).visible)
287 quit
288 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200289 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200290endfunc
291
292func Test_popup_valid_arguments()
293 " Zero value is like the property wasn't there
294 let winid = popup_create("text", {"col": 0})
295 let pos = popup_getpos(winid)
296 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200297 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200298
299 " using cursor column has minimum value of 1
300 let winid = popup_create("text", {"col": 'cursor-100'})
301 let pos = popup_getpos(winid)
302 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200303 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200304
305 " center
306 let winid = popup_create("text", {"pos": 'center'})
307 let pos = popup_getpos(winid)
308 let around = (&columns - pos.width) / 2
309 call assert_inrange(around - 1, around + 1, pos.col)
310 let around = (&lines - pos.height) / 2
311 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200312 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200313endfunc
314
315func Test_popup_invalid_arguments()
316 call assert_fails('call popup_create(666, {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200317 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200318 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200319 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200320
321 call assert_fails('call popup_create("text", {"col": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200322 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200323 call assert_fails('call popup_create("text", {"col": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200324 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200325 call assert_fails('call popup_create("text", {"col": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200326 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200327 call assert_fails('call popup_create("text", {"col": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200328 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200329
330 call assert_fails('call popup_create("text", {"line": "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200331 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200332 call assert_fails('call popup_create("text", {"line": "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200333 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200334 call assert_fails('call popup_create("text", {"line": "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200335 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200336 call assert_fails('call popup_create("text", {"line": "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200337 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200338
339 call assert_fails('call popup_create("text", {"pos": "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200340 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200341 call assert_fails('call popup_create("text", {"padding": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200342 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200343 call assert_fails('call popup_create("text", {"border": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200344 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200345 call assert_fails('call popup_create("text", {"borderhighlight": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200346 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200347 call assert_fails('call popup_create("text", {"borderchars": "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200348 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200349
350 call assert_fails('call popup_create([{"text": "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200351 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200352 call assert_fails('call popup_create([{"text": "text", "props": "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200353 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200354 call assert_fails('call popup_create([{"text": "text", "props": ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200355 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200356endfunc
357
Bram Moolenaareea16992019-05-31 17:34:48 +0200358func Test_win_execute_closing_curwin()
359 split
360 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200361 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200362 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200363endfunc
364
365func Test_win_execute_not_allowed()
366 let winid = popup_create('some text', {})
367 call assert_fails('call win_execute(winid, "split")', 'E994:')
368 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
369 call assert_fails('call win_execute(winid, "close")', 'E994:')
370 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200371 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200372 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
373 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
374 call assert_fails('call win_execute(winid, "next")', 'E994:')
375 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
376 call assert_fails('call win_execute(winid, "buf")', 'E994:')
377 call assert_fails('call win_execute(winid, "edit")', 'E994:')
378 call assert_fails('call win_execute(winid, "enew")', 'E994:')
379 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
380 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
381 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
382 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200383 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200384endfunc
385
Bram Moolenaar402502d2019-05-30 22:07:36 +0200386func Test_popup_with_wrap()
387 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200388 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200389 endif
390 let lines =<< trim END
391 call setline(1, range(1, 100))
392 let winid = popup_create(
393 \ 'a long line that wont fit',
394 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 1})
395 END
396 call writefile(lines, 'XtestPopup')
397 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
398 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
399
400 " clean up
401 call StopVimInTerminal(buf)
402 call delete('XtestPopup')
403endfunc
404
405func Test_popup_without_wrap()
406 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200407 throw 'Skipped: cannot make screendumps'
Bram Moolenaar402502d2019-05-30 22:07:36 +0200408 endif
409 let lines =<< trim END
410 call setline(1, range(1, 100))
411 let winid = popup_create(
412 \ 'a long line that wont fit',
413 \ {'line': 3, 'col': 20, 'maxwidth': 10, 'wrap': 0})
414 END
415 call writefile(lines, 'XtestPopup')
416 let buf = RunVimInTerminal('-S XtestPopup', {'rows': 10})
417 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
418
419 " clean up
420 call StopVimInTerminal(buf)
421 call delete('XtestPopup')
422endfunc
423
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200424func Test_popup_time()
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200425 if !has('timers')
426 return
427 endif
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200428 topleft vnew
429 call setline(1, 'hello')
430
431 call popup_create('world', {
432 \ 'line': 1,
433 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200434 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200435 \ 'time': 500,
436 \})
437 redraw
438 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
439 call assert_equal('world', line)
440
441 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +0200442 redraw
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200443 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
444 call assert_equal('hello', line)
445
446 call popup_create('on the command line', {
447 \ 'line': &lines,
448 \ 'col': 10,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200449 \ 'minwidth': 20,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +0200450 \ 'time': 500,
451 \})
452 redraw
453 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
454 call assert_match('.*on the command line.*', line)
455
456 sleep 700m
457 redraw
458 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
459 call assert_notmatch('.*on the command line.*', line)
460
461 bwipe!
462endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200463
464func Test_popup_hide()
465 topleft vnew
466 call setline(1, 'hello')
467
468 let winid = popup_create('world', {
469 \ 'line': 1,
470 \ 'col': 1,
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200471 \ 'minwidth': 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200472 \})
473 redraw
474 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
475 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200476 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200477 " buffer is still listed and active
478 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200479
480 call popup_hide(winid)
481 redraw
482 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
483 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200484 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +0200485 " buffer is still listed but hidden
486 call assert_match(winbufnr(winid) .. 'u h.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200487
488 call popup_show(winid)
489 redraw
490 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
491 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200492 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200493
494
495 call popup_close(winid)
496 redraw
497 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
498 call assert_equal('hello', line)
499
500 " error is given for existing non-popup window
501 call assert_fails('call popup_hide(win_getid())', 'E993:')
502
503 " no error non-existing window
504 call popup_hide(1234234)
505 call popup_show(41234234)
506
507 bwipe!
508endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200509
510func Test_popup_move()
511 topleft vnew
512 call setline(1, 'hello')
513
514 let winid = popup_create('world', {
515 \ 'line': 1,
516 \ 'col': 1,
517 \ 'minwidth': 20,
518 \})
519 redraw
520 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
521 call assert_equal('world ', line)
522
523 call popup_move(winid, {'line': 2, 'col': 2})
524 redraw
525 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
526 call assert_equal('hello ', line)
527 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
528 call assert_equal('~world', line)
529
530 call popup_move(winid, {'line': 1})
531 redraw
532 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
533 call assert_equal('hworld', line)
534
535 call popup_close(winid)
536
537 bwipe!
538endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +0200539
Bram Moolenaar402502d2019-05-30 22:07:36 +0200540func Test_popup_getpos()
Bram Moolenaarbc133542019-05-29 20:26:46 +0200541 let winid = popup_create('hello', {
542 \ 'line': 2,
543 \ 'col': 3,
544 \ 'minwidth': 10,
545 \ 'minheight': 11,
546 \})
547 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200548 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200549 call assert_equal(2, res.line)
550 call assert_equal(3, res.col)
551 call assert_equal(10, res.width)
552 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200553 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +0200554
555 call popup_close(winid)
556endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200557
558func Test_popup_width_longest()
559 let tests = [
560 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
561 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
562 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
563 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
564 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
565 \ ]
566
567 for test in tests
568 let winid = popup_create(test[0], {'line': 2, 'col': 3})
569 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200570 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200571 call assert_equal(test[1], position.width)
572 call popup_close(winid)
573 endfor
574endfunc
575
576func Test_popup_wraps()
577 let tests = [
578 \ ['nowrap', 6, 1],
579 \ ['a line that wraps once', 12, 2],
580 \ ['a line that wraps two times', 12, 3],
581 \ ]
582 for test in tests
583 let winid = popup_create(test[0],
584 \ {'line': 2, 'col': 3, 'maxwidth': 12})
585 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +0200586 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200587 call assert_equal(test[1], position.width)
588 call assert_equal(test[2], position.height)
589
590 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +0200591 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +0200592 endfor
593endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200594
595func Test_popup_getoptions()
596 let winid = popup_create('hello', {
597 \ 'line': 2,
598 \ 'col': 3,
599 \ 'minwidth': 10,
600 \ 'minheight': 11,
601 \ 'maxwidth': 20,
602 \ 'maxheight': 21,
603 \ 'zindex': 100,
604 \ 'time': 5000,
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200605 \ 'fixed': 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200606 \})
607 redraw
608 let res = popup_getoptions(winid)
609 call assert_equal(2, res.line)
610 call assert_equal(3, res.col)
611 call assert_equal(10, res.minwidth)
612 call assert_equal(11, res.minheight)
613 call assert_equal(20, res.maxwidth)
614 call assert_equal(21, res.maxheight)
615 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200616 call assert_equal(1, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200617 if has('timers')
618 call assert_equal(5000, res.time)
619 endif
620 call popup_close(winid)
621
622 let winid = popup_create('hello', {})
623 redraw
624 let res = popup_getoptions(winid)
625 call assert_equal(0, res.line)
626 call assert_equal(0, res.col)
627 call assert_equal(0, res.minwidth)
628 call assert_equal(0, res.minheight)
629 call assert_equal(0, res.maxwidth)
630 call assert_equal(0, res.maxheight)
631 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200632 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200633 if has('timers')
634 call assert_equal(0, res.time)
635 endif
636 call popup_close(winid)
637 call assert_equal({}, popup_getoptions(winid))
638endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +0200639
640func Test_popup_option_values()
641 new
642 " window-local
643 setlocal number
644 setlocal nowrap
645 " buffer-local
646 setlocal omnifunc=Something
647 " global/buffer-local
648 setlocal path=/there
649 " global/window-local
650 setlocal scrolloff=9
651
652 let winid = popup_create('hello', {})
653 call assert_equal(0, getwinvar(winid, '&number'))
654 call assert_equal(1, getwinvar(winid, '&wrap'))
655 call assert_equal('', getwinvar(winid, '&omnifunc'))
656 call assert_equal(&g:path, getwinvar(winid, '&path'))
657 call assert_equal(&g:scrolloff, getwinvar(winid, '&scrolloff'))
658
659 call popup_close(winid)
660 bwipe
661endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200662
663func Test_popup_atcursor()
664 topleft vnew
665 call setline(1, [
666 \ 'xxxxxxxxxxxxxxxxx',
667 \ 'xxxxxxxxxxxxxxxxx',
668 \ 'xxxxxxxxxxxxxxxxx',
669 \])
670
671 call cursor(2, 2)
672 redraw
673 let winid = popup_atcursor('vim', {})
674 redraw
675 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
676 call assert_equal('xvimxxxxxxxxxxxxx', line)
677 call popup_close(winid)
678
679 call cursor(3, 4)
680 redraw
681 let winid = popup_atcursor('vim', {})
682 redraw
683 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
684 call assert_equal('xxxvimxxxxxxxxxxx', line)
685 call popup_close(winid)
686
687 call cursor(1, 1)
688 redraw
689 let winid = popup_create('vim', {
690 \ 'line': 'cursor+2',
691 \ 'col': 'cursor+1',
692 \})
693 redraw
694 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
695 call assert_equal('xvimxxxxxxxxxxxxx', line)
696 call popup_close(winid)
697
698 call cursor(3, 3)
699 redraw
700 let winid = popup_create('vim', {
701 \ 'line': 'cursor-2',
702 \ 'col': 'cursor-1',
703 \})
704 redraw
705 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
706 call assert_equal('xvimxxxxxxxxxxxxx', line)
707 call popup_close(winid)
708
Bram Moolenaar402502d2019-05-30 22:07:36 +0200709 " just enough room above
710 call cursor(3, 3)
711 redraw
712 let winid = popup_atcursor(['vim', 'is great'], {})
713 redraw
714 let pos = popup_getpos(winid)
715 call assert_equal(1, pos.line)
716 call popup_close(winid)
717
718 " not enough room above, popup goes below the cursor
719 call cursor(3, 3)
720 redraw
721 let winid = popup_atcursor(['vim', 'is', 'great'], {})
722 redraw
723 let pos = popup_getpos(winid)
724 call assert_equal(4, pos.line)
725 call popup_close(winid)
726
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200727 " cursor in first line, popup in line 2
728 call cursor(1, 1)
729 redraw
730 let winid = popup_atcursor(['vim', 'is', 'great'], {})
731 redraw
732 let pos = popup_getpos(winid)
733 call assert_equal(2, pos.line)
734 call popup_close(winid)
735
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200736 bwipe!
737endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200738
739func Test_popup_filter()
740 new
741 call setline(1, 'some text')
742
743 func MyPopupFilter(winid, c)
744 if a:c == 'e'
745 let g:eaten = 'e'
746 return 1
747 endif
748 if a:c == '0'
749 let g:ignored = '0'
750 return 0
751 endif
752 if a:c == 'x'
753 call popup_close(a:winid)
754 return 1
755 endif
756 return 0
757 endfunc
758
759 let winid = popup_create('something', {'filter': 'MyPopupFilter'})
760 redraw
761
762 " e is consumed by the filter
763 call feedkeys('e', 'xt')
764 call assert_equal('e', g:eaten)
765
766 " 0 is ignored by the filter
767 normal $
768 call assert_equal(9, getcurpos()[2])
769 call feedkeys('0', 'xt')
770 call assert_equal('0', g:ignored)
771 call assert_equal(1, getcurpos()[2])
772
773 " x closes the popup
774 call feedkeys('x', 'xt')
775 call assert_equal('e', g:eaten)
776 call assert_equal(-1, winbufnr(winid))
777
778 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200779 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +0200780endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200781
782func Test_popup_close_callback()
783 func PopupDone(id, result)
784 let g:result = a:result
785 endfunc
786 let winid = popup_create('something', {'callback': 'PopupDone'})
787 redraw
788 call popup_close(winid, 'done')
789 call assert_equal('done', g:result)
790endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +0200791
792func Test_popup_empty()
793 let winid = popup_create('', {'padding': [2,2,2,2]})
794 redraw
795 let pos = popup_getpos(winid)
796 call assert_equal(4, pos.width)
797 call assert_equal(5, pos.height)
798
799 let winid = popup_create([], {'border': []})
800 redraw
801 let pos = popup_getpos(winid)
802 call assert_equal(2, pos.width)
803 call assert_equal(3, pos.height)
804endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +0200805
806func Test_popup_never_behind()
807 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +0200808 throw 'Skipped: cannot make screendumps'
Bram Moolenaar988c4332019-06-02 14:12:11 +0200809 endif
810 " +-----------------------------+
811 " | | |
812 " | | |
813 " | | |
814 " | line1 |
815 " |------------line2------------|
816 " | line3 |
817 " | line4 |
818 " | |
819 " | |
820 " +-----------------------------+
821 let lines =<< trim END
822 only
823 split
824 vsplit
825 let info_window1 = getwininfo()[0]
826 let line = info_window1['height']
827 let col = info_window1['width']
828 call popup_create(['line1', 'line2', 'line3', 'line4'], {
829 \ 'line' : line,
830 \ 'col' : col,
831 \ })
832 END
833 call writefile(lines, 'XtestPopupBehind')
834 let buf = RunVimInTerminal('-S XtestPopupBehind', {'rows': 10})
835 call term_sendkeys(buf, "\<C-W>w")
836 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
837
838 " clean up
839 call StopVimInTerminal(buf)
840 call delete('XtestPopupBehind')
841endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200842
843func s:VerifyPosition( p, msg, line, col, width, height )
844 call assert_equal( a:line, popup_getpos( a:p ).line, a:msg . ' (l)' )
845 call assert_equal( a:col, popup_getpos( a:p ).col, a:msg . ' (c)' )
846 call assert_equal( a:width, popup_getpos( a:p ).width, a:msg . ' (w)' )
847 call assert_equal( a:height, popup_getpos( a:p ).height, a:msg . ' (h)' )
848endfunc
849
850func Test_popup_position_adjust()
851 " Anything placed past 2 cells from of the right of the screen is moved to the
852 " left.
853 "
854 " When wrapping is disabled, we also shift to the left to display on the
855 " screen, unless fixed is set.
856
857 " Entries for cases which don't vary based on wrapping.
858 " Format is per tests described below
859 let both_wrap_tests = [
860 \ [ 'a', 5, &columns, 5, &columns - 2, 1, 1 ],
861 \ [ 'b', 5, &columns + 1, 5, &columns - 2, 1, 1 ],
862 \ [ 'c', 5, &columns - 1, 5, &columns - 2, 1, 1 ],
863 \ [ 'd', 5, &columns - 2, 5, &columns - 2, 1, 1 ],
864 \ [ 'e', 5, &columns - 3, 5, &columns - 3, 1, 1 ],
865 \
866 \ [ 'aa', 5, &columns, 5, &columns - 2, 2, 1 ],
867 \ [ 'bb', 5, &columns + 1, 5, &columns - 2, 2, 1 ],
868 \ [ 'cc', 5, &columns - 1, 5, &columns - 2, 2, 1 ],
869 \ [ 'dd', 5, &columns - 2, 5, &columns - 2, 2, 1 ],
870 \ [ 'ee', 5, &columns - 3, 5, &columns - 3, 2, 1 ],
871 \
872 \ [ 'aaa', 5, &columns, 5, &columns - 2, 3, 1 ],
873 \ [ 'bbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
874 \ [ 'ccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
875 \ [ 'ddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
876 \ [ 'eee', 5, &columns - 3, 5, &columns - 3, 3, 1 ],
877 \ ]
878
879 " these test groups are dicts with:
880 " - comment: something to identify the group of tests by
881 " - options: dict of options to merge with the row/col in tests
882 " - tests: list of cases. Each one is a list with elements:
883 " - text
884 " - row
885 " - col
886 " - expected row
887 " - expected col
888 " - expected width
889 " - expected height
890 let tests = [
891 \ {
892 \ 'comment': 'left-aligned with wrapping',
893 \ 'options': {
894 \ 'wrap': 1,
895 \ 'pos': 'botleft',
896 \ },
897 \ 'tests': both_wrap_tests + [
898 \ [ 'aaaa', 5, &columns, 4, &columns - 2, 3, 2 ],
899 \ [ 'bbbb', 5, &columns + 1, 4, &columns - 2, 3, 2 ],
900 \ [ 'cccc', 5, &columns - 1, 4, &columns - 2, 3, 2 ],
901 \ [ 'dddd', 5, &columns - 2, 4, &columns - 2, 3, 2 ],
902 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
903 \ ],
904 \ },
905 \ {
906 \ 'comment': 'left aligned without wrapping',
907 \ 'options': {
908 \ 'wrap': 0,
909 \ 'pos': 'botleft',
910 \ },
911 \ 'tests': both_wrap_tests + [
912 \ [ 'aaaa', 5, &columns, 5, &columns - 3, 4, 1 ],
913 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1 ],
914 \ [ 'cccc', 5, &columns - 1, 5, &columns - 3, 4, 1 ],
915 \ [ 'dddd', 5, &columns - 2, 5, &columns - 3, 4, 1 ],
916 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
917 \ ],
918 \ },
919 \ {
920 \ 'comment': 'left aligned with fixed position',
921 \ 'options': {
922 \ 'wrap': 0,
923 \ 'fixed': 1,
924 \ 'pos': 'botleft',
925 \ },
926 \ 'tests': both_wrap_tests + [
927 \ [ 'aaaa', 5, &columns, 5, &columns - 2, 3, 1 ],
928 \ [ 'bbbb', 5, &columns + 1, 5, &columns - 2, 3, 1 ],
929 \ [ 'cccc', 5, &columns - 1, 5, &columns - 2, 3, 1 ],
930 \ [ 'dddd', 5, &columns - 2, 5, &columns - 2, 3, 1 ],
931 \ [ 'eeee', 5, &columns - 3, 5, &columns - 3, 4, 1 ],
932 \ ],
933 \ },
934 \ ]
935
936 for test_group in tests
937 for test in test_group.tests
938 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
939 let options = {
940 \ 'line': line,
941 \ 'col': col,
942 \ }
943 call extend( options, test_group.options )
944
945 let p = popup_create( text, options )
946
947 let msg = string( extend( options, { 'text': text } ) )
948 call s:VerifyPosition( p, msg, e_line, e_col, e_width, e_height )
949 call popup_close( p )
950 endfor
951 endfor
952
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200953 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200954 %bwipe!
955endfunc
956
Bram Moolenaar3397f742019-06-02 18:40:06 +0200957func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +0200958 " width of screen
959 let X = join(map(range(&columns), {->'X'}), '')
960
961 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
962 call s:VerifyPosition( p, 'full width topleft', 1, 1, &columns, 1 )
963
964 redraw
965 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
966 call assert_equal(X, line)
967
968 call popup_close( p )
969 redraw
970
971 " Same if placed on the right hand side
972 let p = popup_create( X, { 'line': 1, 'col': &columns, 'wrap': 0 } )
973 call s:VerifyPosition( p, 'full width topright', 1, 1, &columns, 1 )
974
975 redraw
976 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
977 call assert_equal(X, line)
978
979 call popup_close( p )
980 redraw
981
982 " Extend so > window width
983 let X .= 'x'
984
985 let p = popup_create( X, { 'line': 1, 'col': 1, 'wrap': 0 } )
986 call s:VerifyPosition( p, 'full width + 1 topleft', 1, 1, &columns, 1 )
987
988 redraw
989 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
990 call assert_equal(X[ : -2 ], line)
991
992 call popup_close( p )
993 redraw
994
995 " Shifted then truncated (the x is not visible)
996 let p = popup_create( X, { 'line': 1, 'col': &columns - 3, 'wrap': 0 } )
997 call s:VerifyPosition( p, 'full width + 1 topright', 1, 1, &columns, 1 )
998
999 redraw
1000 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1001 call assert_equal(X[ : -2 ], line)
1002
1003 call popup_close( p )
1004 redraw
1005
1006 " Not shifted, just truncated
1007 let p = popup_create( X,
1008 \ { 'line': 1, 'col': 2, 'wrap': 0, 'fixed': 1 } )
1009 call s:VerifyPosition( p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
1010
1011 redraw
1012 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1013 let e_line = ' ' . X[ 1 : -2 ]
1014 call assert_equal(e_line, line)
1015
1016 call popup_close( p )
1017 redraw
1018
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001019 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001020 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001021endfunc
1022
1023func Test_popup_moved()
1024 new
1025 call test_override('char_avail', 1)
1026 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1027
1028 exe "normal gg0/word\<CR>"
1029 let winid = popup_atcursor('text', {'moved': 'any'})
1030 redraw
1031 call assert_equal(1, popup_getpos(winid).visible)
1032 " trigger the check for last_cursormoved by going into insert mode
1033 call feedkeys("li\<Esc>", 'xt')
1034 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001035 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001036
1037 exe "normal gg0/word\<CR>"
1038 let winid = popup_atcursor('text', {'moved': 'word'})
1039 redraw
1040 call assert_equal(1, popup_getpos(winid).visible)
1041 call feedkeys("hi\<Esc>", 'xt')
1042 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001043 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001044
1045 exe "normal gg0/word\<CR>"
1046 let winid = popup_atcursor('text', {'moved': 'word'})
1047 redraw
1048 call assert_equal(1, popup_getpos(winid).visible)
1049 call feedkeys("li\<Esc>", 'xt')
1050 call assert_equal(1, popup_getpos(winid).visible)
1051 call feedkeys("ei\<Esc>", 'xt')
1052 call assert_equal(1, popup_getpos(winid).visible)
1053 call feedkeys("eli\<Esc>", 'xt')
1054 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001055 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001056
Bram Moolenaar17627312019-06-02 19:53:44 +02001057 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001058 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001059 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001060 redraw
1061 call assert_equal(1, popup_getpos(winid).visible)
1062 call feedkeys("eli\<Esc>", 'xt')
1063 call assert_equal(1, popup_getpos(winid).visible)
1064 call feedkeys("wi\<Esc>", 'xt')
1065 call assert_equal(1, popup_getpos(winid).visible)
1066 call feedkeys("Eli\<Esc>", 'xt')
1067 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001068 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001069
1070 exe "normal gg0/word\<CR>"
1071 let winid = popup_atcursor('text', {'moved': [5, 10]})
1072 redraw
1073 call assert_equal(1, popup_getpos(winid).visible)
1074 call feedkeys("eli\<Esc>", 'xt')
1075 call feedkeys("ei\<Esc>", 'xt')
1076 call assert_equal(1, popup_getpos(winid).visible)
1077 call feedkeys("eli\<Esc>", 'xt')
1078 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001079 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001080
1081 bwipe!
1082 call test_override('ALL', 0)
1083endfunc