tui_spec.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. local helpers = require("test.unit.helpers")(after_each)
  2. local cimport = helpers.cimport
  3. local eq = helpers.eq
  4. local ffi = helpers.ffi
  5. local itp = helpers.gen_itp(it)
  6. local to_cstr = helpers.to_cstr
  7. local cinput = cimport("./src/nvim/tui/input.h")
  8. local rbuffer = cimport("./test/unit/fixtures/rbuffer.h")
  9. local globals = cimport("./src/nvim/globals.h")
  10. local multiqueue = cimport("./test/unit/fixtures/multiqueue.h")
  11. itp('handle_background_color', function()
  12. local handle_background_color = cinput.ut_handle_background_color
  13. local term_input = ffi.new('TermInput', {})
  14. local events = globals.main_loop.thread_events
  15. local kIncomplete = cinput.kIncomplete
  16. local kNotApplicable = cinput.kNotApplicable
  17. local kComplete = cinput.kComplete
  18. -- Short-circuit when not waiting for response.
  19. term_input.waiting_for_bg_response = 0
  20. eq(kNotApplicable, handle_background_color(term_input))
  21. local capacity = 100
  22. local rbuf = ffi.gc(rbuffer.rbuffer_new(capacity), rbuffer.rbuffer_free)
  23. term_input.read_stream.buffer = rbuf
  24. local function assert_bg(colorspace, color, bg)
  25. local term_response = '\027]11;'..colorspace..':'..color..'\007'
  26. rbuffer.rbuffer_write(rbuf, to_cstr(term_response), #term_response)
  27. term_input.waiting_for_bg_response = 1
  28. eq(kComplete, handle_background_color(term_input))
  29. eq(0, term_input.waiting_for_bg_response)
  30. eq(1, multiqueue.multiqueue_size(events))
  31. local event = multiqueue.multiqueue_get(events)
  32. local bg_event = ffi.cast("Event*", event.argv[1])
  33. eq(bg, ffi.string(bg_event.argv[0]))
  34. -- Buffer has been consumed.
  35. eq(0, rbuf.size)
  36. end
  37. assert_bg('rgb', '0000/0000/0000', 'dark')
  38. assert_bg('rgb', 'ffff/ffff/ffff', 'light')
  39. assert_bg('rgb', '000/000/000', 'dark')
  40. assert_bg('rgb', 'fff/fff/fff', 'light')
  41. assert_bg('rgb', '00/00/00', 'dark')
  42. assert_bg('rgb', 'ff/ff/ff', 'light')
  43. assert_bg('rgb', '0/0/0', 'dark')
  44. assert_bg('rgb', 'f/f/f', 'light')
  45. assert_bg('rgb', 'f/0/0', 'dark')
  46. assert_bg('rgb', '0/f/0', 'light')
  47. assert_bg('rgb', '0/0/f', 'dark')
  48. assert_bg('rgb', '1/1/1', 'dark')
  49. assert_bg('rgb', '2/2/2', 'dark')
  50. assert_bg('rgb', '3/3/3', 'dark')
  51. assert_bg('rgb', '4/4/4', 'dark')
  52. assert_bg('rgb', '5/5/5', 'dark')
  53. assert_bg('rgb', '6/6/6', 'dark')
  54. assert_bg('rgb', '7/7/7', 'dark')
  55. assert_bg('rgb', '8/8/8', 'light')
  56. assert_bg('rgb', '9/9/9', 'light')
  57. assert_bg('rgb', 'a/a/a', 'light')
  58. assert_bg('rgb', 'b/b/b', 'light')
  59. assert_bg('rgb', 'c/c/c', 'light')
  60. assert_bg('rgb', 'd/d/d', 'light')
  61. assert_bg('rgb', 'e/e/e', 'light')
  62. assert_bg('rgb', '0/e/0', 'light')
  63. assert_bg('rgb', '0/d/0', 'light')
  64. assert_bg('rgb', '0/c/0', 'dark')
  65. assert_bg('rgb', '0/b/0', 'dark')
  66. assert_bg('rgb', 'f/0/f', 'dark')
  67. assert_bg('rgb', 'f/1/f', 'dark')
  68. assert_bg('rgb', 'f/2/f', 'dark')
  69. assert_bg('rgb', 'f/3/f', 'light')
  70. assert_bg('rgb', 'f/4/f', 'light')
  71. assert_bg('rgba', '0000/0000/0000/0000', 'dark')
  72. assert_bg('rgba', '0000/0000/0000/ffff', 'dark')
  73. assert_bg('rgba', 'ffff/ffff/ffff/0000', 'light')
  74. assert_bg('rgba', 'ffff/ffff/ffff/ffff', 'light')
  75. assert_bg('rgba', '000/000/000/000', 'dark')
  76. assert_bg('rgba', '000/000/000/fff', 'dark')
  77. assert_bg('rgba', 'fff/fff/fff/000', 'light')
  78. assert_bg('rgba', 'fff/fff/fff/fff', 'light')
  79. assert_bg('rgba', '00/00/00/00', 'dark')
  80. assert_bg('rgba', '00/00/00/ff', 'dark')
  81. assert_bg('rgba', 'ff/ff/ff/00', 'light')
  82. assert_bg('rgba', 'ff/ff/ff/ff', 'light')
  83. assert_bg('rgba', '0/0/0/0', 'dark')
  84. assert_bg('rgba', '0/0/0/f', 'dark')
  85. assert_bg('rgba', 'f/f/f/0', 'light')
  86. assert_bg('rgba', 'f/f/f/f', 'light')
  87. -- Incomplete sequence: necessarily correct behavior.
  88. local term_response = '\027]11;rgba:f/f/f/f' -- missing '\007
  89. rbuffer.rbuffer_write(rbuf, to_cstr(term_response), #term_response)
  90. term_input.waiting_for_bg_response = 1
  91. eq(kIncomplete, handle_background_color(term_input))
  92. eq(1, term_input.waiting_for_bg_response)
  93. eq(#term_response, rbuf.size)
  94. term_response = '\007'
  95. rbuffer.rbuffer_write(rbuf, to_cstr(term_response), #term_response)
  96. eq(kComplete, handle_background_color(term_input))
  97. eq(0, term_input.waiting_for_bg_response)
  98. local event = multiqueue.multiqueue_get(events)
  99. local bg_event = ffi.cast("Event*", event.argv[1])
  100. eq('light', ffi.string(bg_event.argv[0]))
  101. eq(0, multiqueue.multiqueue_size(events))
  102. eq(0, rbuf.size)
  103. term_response = '\027]11;rg'
  104. rbuffer.rbuffer_write(rbuf, to_cstr(term_response), #term_response)
  105. term_input.waiting_for_bg_response = 1
  106. eq(kIncomplete, handle_background_color(term_input))
  107. eq(1, term_input.waiting_for_bg_response)
  108. eq(#term_response, rbuf.size)
  109. term_response = 'ba:f/f/f/f\007'
  110. rbuffer.rbuffer_write(rbuf, to_cstr(term_response), #term_response)
  111. eq(kComplete, handle_background_color(term_input))
  112. eq(0, term_input.waiting_for_bg_response)
  113. event = multiqueue.multiqueue_get(events)
  114. bg_event = ffi.cast("Event*", event.argv[1])
  115. eq('light', ffi.string(bg_event.argv[0]))
  116. eq(0, multiqueue.multiqueue_size(events))
  117. eq(0, rbuf.size)
  118. -- Does nothing when not at start of buffer.
  119. term_response = '123\027]11;rgba:f/f/f/f\007456'
  120. rbuffer.rbuffer_write(rbuf, to_cstr(term_response), #term_response)
  121. term_input.waiting_for_bg_response = 3
  122. eq(kNotApplicable, handle_background_color(term_input))
  123. eq(2, term_input.waiting_for_bg_response)
  124. eq(0, multiqueue.multiqueue_size(events))
  125. eq(#term_response, rbuf.size)
  126. rbuffer.rbuffer_consumed(rbuf, #term_response)
  127. -- Keeps trailing buffer.
  128. term_response = '\027]11;rgba:f/f/f/f\007456'
  129. rbuffer.rbuffer_write(rbuf, to_cstr(term_response), #term_response)
  130. term_input.waiting_for_bg_response = 1
  131. eq(kComplete, handle_background_color(term_input))
  132. eq(0, term_input.waiting_for_bg_response)
  133. eq(1, multiqueue.multiqueue_size(events))
  134. eq(3, rbuf.size)
  135. rbuffer.rbuffer_consumed(rbuf, rbuf.size)
  136. end)