test_window.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "gtest/gtest.h"
  2. #include "lumacs/window.hpp"
  3. #include "lumacs/buffer.hpp"
  4. using namespace lumacs;
  5. class WindowTest : public ::testing::Test {
  6. protected:
  7. std::shared_ptr<Buffer> buffer;
  8. std::unique_ptr<Window> window;
  9. void SetUp() override {
  10. buffer = std::make_shared<Buffer>("test_buffer");
  11. window = std::make_unique<Window>(buffer);
  12. // Set a viewport large enough to avoid scroll margin conflicts
  13. // Margin is 3 (vertical) and 5 (horizontal).
  14. // Need height > 2*3 = 6, width > 2*5 = 10.
  15. window->set_viewport_size(20, 10);
  16. }
  17. };
  18. TEST_F(WindowTest, InitialState) {
  19. ASSERT_EQ(window->cursor().line, 0);
  20. ASSERT_EQ(window->cursor().column, 0);
  21. const auto& vp = window->viewport();
  22. ASSERT_EQ(vp.scroll_offset, 0);
  23. ASSERT_EQ(vp.horizontal_offset, 0);
  24. ASSERT_EQ(vp.width, 20);
  25. ASSERT_EQ(vp.height, 10);
  26. }
  27. TEST_F(WindowTest, VerticalScrolling) {
  28. // Fill buffer with enough lines
  29. for (int i = 0; i < 30; ++i) {
  30. size_t last_line = buffer->line_count() - 1;
  31. size_t last_col = buffer->line(last_line).size();
  32. buffer->insert_newline({last_line, last_col});
  33. }
  34. // Viewport height 10, Margin 3.
  35. // Trigger point for scroll down: scroll + height - margin
  36. // 0 + 10 - 3 = 7.
  37. // So line 6 should not scroll. Line 7 should scroll.
  38. window->set_cursor({6, 0});
  39. ASSERT_EQ(window->viewport().scroll_offset, 0);
  40. window->set_cursor({7, 0});
  41. // New scroll = cursor - height + margin + 1
  42. // 7 - 10 + 3 + 1 = 1.
  43. ASSERT_EQ(window->viewport().scroll_offset, 1);
  44. // Verify visible range: [1, 11) (10 lines)
  45. auto range = window->visible_line_range();
  46. ASSERT_EQ(range.first, 1);
  47. ASSERT_EQ(range.second, 11);
  48. }
  49. TEST_F(WindowTest, HorizontalScrolling) {
  50. // Fill line 0 with text
  51. std::string long_text = "012345678901234567890123456789"; // 30 chars
  52. buffer->insert({0,0}, long_text);
  53. // Viewport width 20, Margin 5.
  54. // Trigger point for scroll right: scroll + width - margin
  55. // 0 + 20 - 5 = 15.
  56. // Col 14 -> no scroll. Col 15 -> scroll.
  57. window->set_cursor({0, 14});
  58. ASSERT_EQ(window->viewport().horizontal_offset, 0);
  59. window->set_cursor({0, 15});
  60. // New scroll = col - width + margin + 1
  61. // 15 - 20 + 5 + 1 = 1.
  62. ASSERT_EQ(window->viewport().horizontal_offset, 1);
  63. }
  64. TEST_F(WindowTest, ScrollLinesExplicitly) {
  65. for (int i = 0; i < 30; ++i) {
  66. size_t last_line = buffer->line_count() - 1;
  67. size_t last_col = buffer->line(last_line).size();
  68. buffer->insert_newline({last_line, last_col});
  69. }
  70. // Scroll down 2 lines
  71. window->scroll_lines(2);
  72. ASSERT_EQ(window->viewport().scroll_offset, 2);
  73. // Cursor was at 0. Top of view is 2.
  74. // Cursor should be moved to 2.
  75. ASSERT_EQ(window->cursor().line, 2);
  76. // Scroll up 1 line
  77. window->scroll_lines(-1);
  78. ASSERT_EQ(window->viewport().scroll_offset, 1);
  79. }
  80. TEST_F(WindowTest, VisibleLineRange) {
  81. for (int i = 0; i < 20; ++i) {
  82. size_t last_line = buffer->line_count() - 1;
  83. size_t last_col = buffer->line(last_line).size();
  84. buffer->insert_newline({last_line, last_col});
  85. }
  86. window->scroll_lines(2);
  87. // Viewport height 10, offset 2. Range: [2, 12)
  88. auto range = window->visible_line_range();
  89. ASSERT_EQ(range.first, 2);
  90. ASSERT_EQ(range.second, 12);
  91. }