sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/

test/unit/test_line_cursor_mode.rb (6064B) - raw

      1 require "test_helper"
      2 require "dummy_buffer"
      3 
      4 require "sup"
      5 
      6 class TestLineCursorMode < Minitest::Test
      7   def setup
      8     $config = {
      9       :load_more_threads_hysteresis => 0,
     10       :load_more_threads_when_scrolling => true,
     11       :continuous_scroll => false,
     12     }
     13     @out = File.open File::NULL, "w"
     14     @inp = File.open File::NULL, "r"
     15     @screen = Ncurses.newterm "dumb", @out, @inp
     16     Redwood::BufferManager.init
     17     @modes_to_cleanup = []
     18     @lines = []
     19     @load_more = Thread::Queue.new
     20     @buffer_height = 41  # 1 for status line, 40 usable lines
     21   end
     22 
     23   def teardown
     24     @modes_to_cleanup.each { |mode| mode.cleanup }
     25     Redwood::BufferManager.deinstantiate!
     26     Ncurses.delscreen @screen
     27     @inp.close
     28     @out.close
     29     $config = nil
     30   end
     31 
     32   def make_mode
     33     mode = Redwood::LineCursorMode.new
     34     @modes_to_cleanup << mode
     35     lines = @lines
     36     mode.define_singleton_method(:lines) { lines.length }
     37     mode.define_singleton_method(:[]) { |i| lines[i] }
     38     mode.send(:to_load_more) { |n| @load_more << n }
     39     mode.buffer = Redwood::DummyBuffer.new 100, @buffer_height
     40     mode.spawned
     41     mode.draw
     42     mode
     43   end
     44 
     45   def expect_load_more n
     46     begin
     47       requested = @load_more.pop :timeout => 0.1
     48     rescue ThreadError
     49       ## Ruby < 3.2 does not obey the timeout for Queue#pop
     50       sleep 0.1
     51       requested = @load_more.pop true
     52     end
     53     refute_nil requested, "Expected load_more callbacks to fire"
     54     assert_equal n, requested
     55     (0...n).map { |i| @lines << "line #{i}" }
     56   end
     57 
     58   def test_cursor_down
     59     mode = make_mode
     60     expect_load_more 41
     61     mode.draw  # curpos gets messed up without this call, why?
     62 
     63     21.times do
     64       mode.handle_input Ncurses::CharCode.character('j')
     65     end
     66     assert_equal 21, mode.curpos
     67     assert_equal 0, mode.topline
     68 
     69     ## Two past halfway, load_more callbacks are triggered.
     70     mode.handle_input Ncurses::CharCode.character('j')
     71     assert_equal 22, mode.curpos
     72     expect_load_more 40
     73 
     74     17.times do
     75       mode.handle_input Ncurses::CharCode.character('j')
     76     end
     77     assert_equal 39, mode.curpos
     78     assert_equal 0, mode.topline
     79 
     80     ## From the bottom line, it wraps back to the top of the next page.
     81     mode.handle_input Ncurses::CharCode.character('j')
     82     assert_equal 40, mode.curpos
     83     assert_equal 40, mode.topline
     84   end
     85 
     86   def test_scroll_down
     87     mode = make_mode
     88     expect_load_more 41
     89 
     90     ## When the cursor is already at the top, it moves with the scroll.
     91     assert_equal 0, mode.curpos
     92     assert_equal 0, mode.topline
     93     mode.handle_input Ncurses::CharCode.character('J')
     94     assert_equal 1, mode.curpos
     95     assert_equal 1, mode.topline
     96 
     97     3.times do
     98       mode.handle_input Ncurses::CharCode.character('j')
     99     end
    100     assert_equal 4, mode.curpos
    101     assert_equal 1, mode.topline
    102 
    103     ## When the cursor is not at the top, it keeps its place and the
    104     ## buffer scrolls underneath it.
    105     mode.handle_input Ncurses::CharCode.character('J')
    106     assert_equal 4, mode.curpos
    107     assert_equal 2, mode.topline
    108 
    109     ## It always loads 10 more if we would scroll past the bottom.
    110     expect_load_more 10
    111   end
    112 
    113   def test_page_down
    114     mode = make_mode
    115     expect_load_more 41
    116 
    117     mode.handle_input Ncurses::CharCode.keycode(Ncurses::KEY_NPAGE)
    118     assert_equal 40, mode.curpos
    119     assert_equal 40, mode.topline
    120     expect_load_more 40
    121 
    122     mode.handle_input Ncurses::CharCode.keycode(Ncurses::KEY_NPAGE)
    123     assert_equal 80, mode.curpos
    124     assert_equal 80, mode.topline
    125     assert_equal 81, mode.lines
    126     expect_load_more 40
    127   end
    128 
    129   def test_page_down_when_fully_populated
    130     mode = make_mode
    131     expect_load_more 41
    132     (0...119).map { |i| @lines << "more line #{i}" }  # enough for 4 full pages
    133 
    134     mode.handle_input Ncurses::CharCode.keycode(Ncurses::KEY_NPAGE)
    135     assert_equal 40, mode.curpos
    136     assert_equal 40, mode.topline
    137 
    138     ## Relative cursor position is preserved when paging down.
    139     3.times do
    140       mode.handle_input Ncurses::CharCode.character('j')
    141     end
    142     assert_equal 43, mode.curpos
    143     assert_equal 40, mode.topline
    144     mode.handle_input Ncurses::CharCode.keycode(Ncurses::KEY_NPAGE)
    145     assert_equal 83, mode.curpos
    146     assert_equal 80, mode.topline
    147   end
    148 
    149   def test_half_page_down
    150     mode = make_mode
    151     expect_load_more 41
    152 
    153     mode.handle_input Ncurses::CharCode.character("\C-d")
    154     assert_equal 20, mode.curpos
    155     assert_equal 20, mode.topline
    156     expect_load_more 40
    157 
    158     mode.handle_input Ncurses::CharCode.character("\C-d")
    159     assert_equal 40, mode.curpos
    160     assert_equal 40, mode.topline
    161   end
    162 
    163   def test_half_page_down_when_fully_populated
    164     mode = make_mode
    165     expect_load_more 41
    166     (0...119).map { |i| @lines << "more line #{i}" }  # enough for 4 full pages
    167 
    168     mode.handle_input Ncurses::CharCode.character("\C-d")
    169     assert_equal 20, mode.curpos
    170     assert_equal 20, mode.topline
    171 
    172     25.times do
    173       mode.handle_input Ncurses::CharCode.character('j')
    174     end
    175     assert_equal 45, mode.curpos
    176     assert_equal 20, mode.topline
    177     mode.handle_input Ncurses::CharCode.character("\C-d")
    178     assert_equal 45, mode.curpos
    179     assert_equal 40, mode.topline
    180   end
    181 
    182   def test_half_page_up
    183     mode = make_mode
    184     expect_load_more 41
    185     (0...119).map { |i| @lines << "more line #{i}" }  # enough for 4 full pages
    186 
    187     mode.handle_input Ncurses::CharCode.keycode(Ncurses::KEY_NPAGE)
    188     assert_equal 40, mode.curpos
    189     assert_equal 40, mode.topline
    190 
    191     mode.handle_input Ncurses::CharCode.character("\C-u")
    192     assert_equal 40, mode.curpos
    193     assert_equal 20, mode.topline
    194 
    195     mode.handle_input Ncurses::CharCode.character('j')
    196     assert_equal 41, mode.curpos
    197     assert_equal 20, mode.topline
    198 
    199     mode.handle_input Ncurses::CharCode.character("\C-u")
    200     assert_equal 40, mode.curpos
    201     assert_equal 0, mode.topline
    202   end
    203 
    204   def test_jump_to_end
    205     mode = make_mode
    206     expect_load_more 41
    207 
    208     mode.handle_input Ncurses::CharCode.keycode(Ncurses::KEY_END)
    209     assert_equal 40, mode.curpos
    210     assert_equal 1, mode.topline
    211     assert_equal 41, mode.botline
    212     expect_load_more 40
    213   end
    214 end