cursorhold_spec.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear = n.clear
  4. local eq = t.eq
  5. local feed = n.feed
  6. local retry = t.retry
  7. local exec = n.source
  8. local sleep = vim.uv.sleep
  9. local api = n.api
  10. before_each(clear)
  11. describe('CursorHold', function()
  12. before_each(function()
  13. exec([[
  14. let g:cursorhold = 0
  15. augroup test
  16. au CursorHold * let g:cursorhold += 1
  17. augroup END
  18. ]])
  19. end)
  20. it('is triggered correctly #12587', function()
  21. local function test_cursorhold(fn, early)
  22. local ut = 2
  23. -- if testing with small 'updatetime' fails, double its value and test again
  24. retry(10, nil, function()
  25. ut = ut * 2
  26. api.nvim_set_option_value('updatetime', ut, {})
  27. feed('0') -- reset did_cursorhold
  28. api.nvim_set_var('cursorhold', 0)
  29. sleep(ut / 4)
  30. fn()
  31. eq(0, api.nvim_get_var('cursorhold'))
  32. sleep(ut / 2)
  33. fn()
  34. eq(0, api.nvim_get_var('cursorhold'))
  35. sleep(ut / 2)
  36. eq(early, api.nvim_get_var('cursorhold'))
  37. sleep(ut / 4 * 3)
  38. eq(1, api.nvim_get_var('cursorhold'))
  39. end)
  40. end
  41. local ignore_key = api.nvim_replace_termcodes('<Ignore>', true, true, true)
  42. test_cursorhold(function() end, 1)
  43. test_cursorhold(function()
  44. feed('')
  45. end, 1)
  46. test_cursorhold(function()
  47. api.nvim_feedkeys('', 'n', true)
  48. end, 1)
  49. test_cursorhold(function()
  50. feed('<Ignore>')
  51. end, 0)
  52. test_cursorhold(function()
  53. api.nvim_feedkeys(ignore_key, 'n', true)
  54. end, 0)
  55. end)
  56. it("reducing 'updatetime' while waiting for CursorHold #20241", function()
  57. api.nvim_set_option_value('updatetime', 10000, {})
  58. feed('0') -- reset did_cursorhold
  59. api.nvim_set_var('cursorhold', 0)
  60. sleep(50)
  61. eq(0, api.nvim_get_var('cursorhold'))
  62. api.nvim_set_option_value('updatetime', 20, {})
  63. sleep(10)
  64. eq(1, api.nvim_get_var('cursorhold'))
  65. end)
  66. end)
  67. describe('CursorHoldI', function()
  68. -- NOTE: since this test uses RPC it is not necessary to trigger the initial
  69. -- issue (#3757) via timer's or RPC callbacks in the first place.
  70. it('is triggered after input', function()
  71. exec([[
  72. set updatetime=1
  73. let g:cursorhold = 0
  74. augroup test
  75. au CursorHoldI * let g:cursorhold += 1
  76. augroup END
  77. ]])
  78. feed('ifoo')
  79. retry(5, nil, function()
  80. sleep(1)
  81. eq(1, api.nvim_get_var('cursorhold'))
  82. end)
  83. end)
  84. end)