robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
14.1 KiB428 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each, after_each = t.describe, t.it, t.before_each, t.after_each
6local clear = n.clear
7local eq = t.eq
8local eval = n.eval
9local exec = n.exec
10local command = n.command
11local feed = n.feed
12local api = n.api
13local assert_alive = n.assert_alive
14
15before_each(clear)
16
17describe('WinResized', function()
18  -- oldtest: Test_WinResized()
19  it('works', function()
20    exec([[
21      set scrolloff=0
22      call setline(1, ['111', '222'])
23      vnew
24      call setline(1, ['aaa', 'bbb'])
25      new
26      call setline(1, ['foo', 'bar'])
27
28      let g:resized = 0
29      au WinResized * let g:resized += 1
30      au WinResized * let g:v_event = deepcopy(v:event)
31    ]])
32    eq(0, eval('g:resized'))
33
34    -- increase window height, two windows will be reported
35    feed('<C-W>+')
36    eq(1, eval('g:resized'))
37    eq({ windows = { 1002, 1001 } }, eval('g:v_event'))
38
39    -- increase window width, three windows will be reported
40    feed('<C-W>>')
41    eq(2, eval('g:resized'))
42    eq({ windows = { 1002, 1001, 1000 } }, eval('g:v_event'))
43  end)
44
45  it('is triggered in terminal mode #21197 #27207', function()
46    exec([[
47      autocmd TermOpen * startinsert
48      let g:resized = 0
49      autocmd WinResized * let g:resized += 1
50    ]])
51    eq(0, eval('g:resized'))
52
53    command('vsplit term://')
54    feed('<Ignore>') -- Add input to separate two RPC requests
55    eq({ mode = 't', blocking = false }, api.nvim_get_mode())
56    eq(1, eval('g:resized'))
57
58    command('split')
59    feed('<Ignore>') -- Add input to separate two RPC requests
60    eq({ mode = 't', blocking = false }, api.nvim_get_mode())
61    eq(2, eval('g:resized'))
62  end)
63end)
64
65describe('WinScrolled', function()
66  local win_id
67
68  before_each(function()
69    win_id = api.nvim_get_current_win()
70    command(string.format('autocmd WinScrolled %d let g:matched = v:true', win_id))
71    exec([[
72      let g:scrolled = 0
73      au WinScrolled * let g:scrolled += 1
74      au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
75      au WinScrolled * let g:afile = str2nr(expand('<afile>'))
76      au WinScrolled * let g:v_event = deepcopy(v:event)
77    ]])
78  end)
79
80  after_each(function()
81    eq(true, eval('g:matched'))
82    eq(win_id, eval('g:amatch'))
83    eq(win_id, eval('g:afile'))
84  end)
85
86  it('is triggered by scrolling vertically', function()
87    local lines = { '123', '123' }
88    api.nvim_buf_set_lines(0, 0, -1, true, lines)
89    eq(0, eval('g:scrolled'))
90
91    feed('<C-E>')
92    eq(1, eval('g:scrolled'))
93    eq({
94      all = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
95      ['1000'] = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
96    }, eval('g:v_event'))
97
98    feed('<C-Y>')
99    eq(2, eval('g:scrolled'))
100    eq({
101      all = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
102      ['1000'] = { leftcol = 0, topline = -1, topfill = 0, width = 0, height = 0, skipcol = 0 },
103    }, eval('g:v_event'))
104  end)
105
106  it('is triggered by scrolling horizontally', function()
107    command('set nowrap')
108    local width = api.nvim_win_get_width(0)
109    local line = '123' .. ('*'):rep(width * 2)
110    local lines = { line, line }
111    api.nvim_buf_set_lines(0, 0, -1, true, lines)
112    eq(0, eval('g:scrolled'))
113
114    feed('zl')
115    eq(1, eval('g:scrolled'))
116    eq({
117      all = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
118      ['1000'] = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
119    }, eval('g:v_event'))
120
121    feed('zh')
122    eq(2, eval('g:scrolled'))
123    eq({
124      all = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
125      ['1000'] = { leftcol = -1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
126    }, eval('g:v_event'))
127  end)
128
129  it('is triggered by horizontal scrolling from cursor move', function()
130    command('set nowrap')
131    local lines = { '', '', 'Foo' }
132    api.nvim_buf_set_lines(0, 0, -1, true, lines)
133    api.nvim_win_set_cursor(0, { 3, 0 })
134    eq(0, eval('g:scrolled'))
135
136    feed('zl')
137    eq(1, eval('g:scrolled'))
138    eq({
139      all = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
140      ['1000'] = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
141    }, eval('g:v_event'))
142
143    feed('zl')
144    eq(2, eval('g:scrolled'))
145    eq({
146      all = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
147      ['1000'] = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
148    }, eval('g:v_event'))
149
150    feed('h')
151    eq(3, eval('g:scrolled'))
152    eq({
153      all = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
154      ['1000'] = { leftcol = -1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
155    }, eval('g:v_event'))
156
157    feed('zh')
158    eq(4, eval('g:scrolled'))
159    eq({
160      all = { leftcol = 1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
161      ['1000'] = { leftcol = -1, topline = 0, topfill = 0, width = 0, height = 0, skipcol = 0 },
162    }, eval('g:v_event'))
163  end)
164
165  -- oldtest: Test_WinScrolled_long_wrapped()
166  it('is triggered by scrolling on a long wrapped line #19968', function()
167    local height = api.nvim_win_get_height(0)
168    local width = api.nvim_win_get_width(0)
169    api.nvim_buf_set_lines(0, 0, -1, true, { ('foo'):rep(height * width) })
170    api.nvim_win_set_cursor(0, { 1, height * width - 1 })
171    eq(0, eval('g:scrolled'))
172
173    feed('gj')
174    eq(1, eval('g:scrolled'))
175    eq({
176      all = { leftcol = 0, topline = 0, topfill = 0, width = 0, height = 0, skipcol = width },
177      ['1000'] = { leftcol = 0, topline = 0, topfill = 0, width = 0, height = 0, skipcol = width },
178    }, eval('g:v_event'))
179
180    feed('0')
181    eq(2, eval('g:scrolled'))
182    eq({
183      all = { leftcol = 0, topline = 0, topfill = 0, width = 0, height = 0, skipcol = width },
184      ['1000'] = { leftcol = 0, topline = 0, topfill = 0, width = 0, height = 0, skipcol = -width },
185    }, eval('g:v_event'))
186
187    feed('$')
188    eq(3, eval('g:scrolled'))
189  end)
190
191  it('is triggered when the window scrolls in Insert mode', function()
192    local height = api.nvim_win_get_height(0)
193    local lines = {}
194    for i = 1, height * 2 do
195      lines[i] = tostring(i)
196    end
197    api.nvim_buf_set_lines(0, 0, -1, true, lines)
198
199    feed('M')
200    eq(0, eval('g:scrolled'))
201
202    feed('i<C-X><C-E><Esc>')
203    eq(1, eval('g:scrolled'))
204    eq({
205      all = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
206      ['1000'] = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
207    }, eval('g:v_event'))
208
209    feed('i<C-X><C-Y><Esc>')
210    eq(2, eval('g:scrolled'))
211    eq({
212      all = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
213      ['1000'] = { leftcol = 0, topline = -1, topfill = 0, width = 0, height = 0, skipcol = 0 },
214    }, eval('g:v_event'))
215
216    feed('L')
217    eq(2, eval('g:scrolled'))
218
219    feed('A<CR><Esc>')
220    eq(3, eval('g:scrolled'))
221    eq({
222      all = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
223      ['1000'] = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
224    }, eval('g:v_event'))
225  end)
226end)
227
228describe('WinScrolled', function()
229  -- oldtest: Test_WinScrolled_mouse()
230  it('is triggered by mouse scrolling in another window', function()
231    local _ = Screen.new(75, 10)
232    exec([[
233      set nowrap scrolloff=0
234      set mouse=a
235      call setline(1, ['foo']->repeat(32))
236      split
237      let g:scrolled = 0
238      au WinScrolled * let g:scrolled += 1
239    ]])
240    eq(0, eval('g:scrolled'))
241
242    -- With the upper split focused, send a scroll-down event to the unfocused one.
243    api.nvim_input_mouse('wheel', 'down', '', 0, 6, 0)
244    eq(1, eval('g:scrolled'))
245
246    -- Again, but this time while we're in insert mode.
247    feed('i')
248    api.nvim_input_mouse('wheel', 'down', '', 0, 6, 0)
249    feed('<Esc>')
250    eq(2, eval('g:scrolled'))
251  end)
252
253  it('is triggered by incsearch scrolling #21207', function()
254    local lines = {}
255    for i = 1, 100 do
256      lines[i] = 'line ' .. i
257    end
258    lines[60] = 'special target line'
259    api.nvim_buf_set_lines(0, 0, -1, true, lines)
260    exec([[
261      set incsearch scrolloff=0
262      let g:scrolled = 0
263      au WinScrolled * let g:scrolled += 1
264    ]])
265    eq(0, eval('g:scrolled'))
266
267    -- Typing the pattern scrolls to the match.
268    feed('/target')
269    eq(1, eval('g:scrolled'))
270
271    -- <C-c> restores the original view, which is another scroll.
272    feed('<C-c>')
273    eq(2, eval('g:scrolled'))
274  end)
275
276  -- oldtest: Test_WinScrolled_close_curwin()
277  it('closing window does not cause use-after-free #13265', function()
278    exec([[
279      set nowrap scrolloff=0
280      call setline(1, ['aaa', 'bbb'])
281      vsplit
282      au WinScrolled * close
283    ]])
284
285    -- This was using freed memory
286    feed('<C-E>')
287    assert_alive()
288  end)
289
290  -- oldtest: Test_WinScrolled_diff()
291  it('is triggered for both windows when scrolling in diff mode', function()
292    exec([[
293      set diffopt+=foldcolumn:0
294      call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
295      vnew
296      call setline(1, ['d', 'e', 'f', 'g', 'h', 'i'])
297      windo diffthis
298      au WinScrolled * let g:v_event = deepcopy(v:event)
299    ]])
300
301    feed('<C-E>')
302    eq({
303      all = { leftcol = 0, topline = 1, topfill = 1, width = 0, height = 0, skipcol = 0 },
304      ['1000'] = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
305      ['1001'] = { leftcol = 0, topline = 0, topfill = -1, width = 0, height = 0, skipcol = 0 },
306    }, eval('g:v_event'))
307
308    feed('2<C-E>')
309    eq({
310      all = { leftcol = 0, topline = 2, topfill = 2, width = 0, height = 0, skipcol = 0 },
311      ['1000'] = { leftcol = 0, topline = 2, topfill = 0, width = 0, height = 0, skipcol = 0 },
312      ['1001'] = { leftcol = 0, topline = 0, topfill = -2, width = 0, height = 0, skipcol = 0 },
313    }, eval('g:v_event'))
314
315    feed('<C-E>')
316    eq({
317      all = { leftcol = 0, topline = 2, topfill = 0, width = 0, height = 0, skipcol = 0 },
318      ['1000'] = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
319      ['1001'] = { leftcol = 0, topline = 1, topfill = 0, width = 0, height = 0, skipcol = 0 },
320    }, eval('g:v_event'))
321
322    feed('2<C-Y>')
323    eq({
324      all = { leftcol = 0, topline = 3, topfill = 1, width = 0, height = 0, skipcol = 0 },
325      ['1000'] = { leftcol = 0, topline = -2, topfill = 0, width = 0, height = 0, skipcol = 0 },
326      ['1001'] = { leftcol = 0, topline = -1, topfill = 1, width = 0, height = 0, skipcol = 0 },
327    }, eval('g:v_event'))
328  end)
329
330  it('is triggered by mouse scrolling in unfocused floating window #18222', function()
331    local screen = Screen.new(80, 24)
332
333    exec([[
334      let g:scrolled = 0
335      autocmd WinScrolled * let g:scrolled += 1
336      autocmd WinScrolled * let g:amatch = expand('<amatch>')
337      autocmd WinScrolled * let g:v_event = deepcopy(v:event)
338    ]])
339    eq(0, eval('g:scrolled'))
340
341    local buf = api.nvim_create_buf(true, true)
342    api.nvim_buf_set_lines(
343      buf,
344      0,
345      -1,
346      false,
347      { '@', 'b', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n' }
348    )
349    local win = api.nvim_open_win(buf, false, {
350      height = 5,
351      width = 10,
352      col = 0,
353      row = 1,
354      relative = 'editor',
355      style = 'minimal',
356    })
357    screen:expect({ any = '@' })
358    local winid_str = tostring(win)
359    -- WinScrolled should not be triggered when creating a new floating window
360    eq(0, eval('g:scrolled'))
361
362    api.nvim_input_mouse('wheel', 'down', '', 0, 3, 3)
363    eq(1, eval('g:scrolled'))
364    eq(winid_str, eval('g:amatch'))
365    eq({
366      all = { leftcol = 0, topline = 3, topfill = 0, width = 0, height = 0, skipcol = 0 },
367      [winid_str] = { leftcol = 0, topline = 3, topfill = 0, width = 0, height = 0, skipcol = 0 },
368    }, eval('g:v_event'))
369
370    api.nvim_input_mouse('wheel', 'up', '', 0, 3, 3)
371    eq(2, eval('g:scrolled'))
372    eq(tostring(win), eval('g:amatch'))
373    eq({
374      all = { leftcol = 0, topline = 3, topfill = 0, width = 0, height = 0, skipcol = 0 },
375      [winid_str] = { leftcol = 0, topline = -3, topfill = 0, width = 0, height = 0, skipcol = 0 },
376    }, eval('g:v_event'))
377  end)
378
379  it('does not crash when WinResized closes popup before WinScrolled #35803', function()
380    exec([[
381      set scrolloff=0
382      call setline(1, range(1, 100))
383
384      " Create first popup window (will be resized and closed)
385      let buf1 = nvim_create_buf(v:false, v:true)
386      call nvim_buf_set_lines(buf1, 0, -1, v:false, map(range(1, 50), 'string(v:val)'))
387      let popup1 = nvim_open_win(buf1, v:false, {
388        \ 'relative': 'editor',
389        \ 'width': 20,
390        \ 'height': 5,
391        \ 'col': 10,
392        \ 'row': 5
393        \ })
394
395      " Create second popup window (will be scrolled)
396      let buf2 = nvim_create_buf(v:false, v:true)
397      call nvim_buf_set_lines(buf2, 0, -1, v:false, map(range(1, 50), 'string(v:val)'))
398      let popup2 = nvim_open_win(buf2, v:false, {
399        \ 'relative': 'editor',
400        \ 'width': 20,
401        \ 'height': 5,
402        \ 'col': 35,
403        \ 'row': 5
404        \ })
405
406      let g:resized = 0
407      let g:scrolled = 0
408
409      " WinResized autocmd resizes and closes the first popup
410      autocmd WinResized * let g:resized += 1 | call nvim_win_set_height(popup1, 10) | call nvim_win_close(popup1, v:true)
411      " WinScrolled autocmd scrolls the second popup
412      autocmd WinScrolled * let g:scrolled += 1 | call nvim_win_call(popup2, {-> execute('normal! \<C-E>')})
413    ]])
414    eq(0, eval('g:resized'))
415    eq(0, eval('g:scrolled'))
416
417    -- Trigger a resize on popup1, which will close it
418    -- This should trigger WinResized (which closes popup1) and WinScrolled (which scrolls popup2)
419    -- Before the fix, WinScrolled would use the freed pointer causing a crash
420    api.nvim_win_set_height(eval('popup1'), 8)
421
422    -- The key is that it should not crash when WinResized closes a window
423    -- that WinScrolled might have referenced via a stale buf_T pointer
424    assert_alive()
425    -- Verify autocmds were actually triggered
426    eq(1, eval('g:resized > 0'))
427  end)
428end)