scroll_test.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. (function() {
  2. "use strict";
  3. namespace = "scroll_";
  4. testCM("bars_hidden", function(cm) {
  5. for (var i = 0;; i++) {
  6. var wrapBox = cm.getWrapperElement().getBoundingClientRect();
  7. var scrollBox = cm.getScrollerElement().getBoundingClientRect();
  8. is(wrapBox.bottom < scrollBox.bottom - 10);
  9. is(wrapBox.right < scrollBox.right - 10);
  10. if (i == 1) break;
  11. cm.getWrapperElement().style.height = "auto";
  12. cm.refresh();
  13. }
  14. });
  15. function barH(cm) { return byClassName(cm.getWrapperElement(), "CodeMirror-hscrollbar")[0]; }
  16. function barV(cm) { return byClassName(cm.getWrapperElement(), "CodeMirror-vscrollbar")[0]; }
  17. function displayBottom(cm, scrollbar) {
  18. if (scrollbar && cm.display.scroller.offsetHeight > cm.display.scroller.clientHeight)
  19. return barH(cm).getBoundingClientRect().top;
  20. else
  21. return cm.getWrapperElement().getBoundingClientRect().bottom - 1;
  22. }
  23. function displayRight(cm, scrollbar) {
  24. if (scrollbar && cm.display.scroller.offsetWidth > cm.display.scroller.clientWidth)
  25. return barV(cm).getBoundingClientRect().left;
  26. else
  27. return cm.getWrapperElement().getBoundingClientRect().right - 1;
  28. }
  29. function testMovedownFixed(cm, hScroll) {
  30. cm.setSize("100px", "100px");
  31. if (hScroll) cm.setValue(new Array(100).join("x"));
  32. var bottom = displayBottom(cm, hScroll);
  33. for (var i = 0; i < 30; i++) {
  34. cm.replaceSelection("x\n");
  35. var cursorBottom = cm.cursorCoords(null, "window").bottom;
  36. is(cursorBottom <= bottom);
  37. }
  38. is(cursorBottom >= bottom - 5);
  39. }
  40. testCM("movedown_fixed", function(cm) {testMovedownFixed(cm, false);});
  41. testCM("movedown_hscroll_fixed", function(cm) {testMovedownFixed(cm, true);});
  42. function testMovedownResize(cm, hScroll) {
  43. cm.getWrapperElement().style.height = "auto";
  44. if (hScroll) cm.setValue(new Array(100).join("x"));
  45. cm.refresh();
  46. for (var i = 0; i < 30; i++) {
  47. cm.replaceSelection("x\n");
  48. var bottom = displayBottom(cm, hScroll);
  49. var cursorBottom = cm.cursorCoords(null, "window").bottom;
  50. is(cursorBottom <= bottom);
  51. is(cursorBottom >= bottom - 5);
  52. }
  53. }
  54. testCM("movedown_resize", function(cm) {testMovedownResize(cm, false);});
  55. testCM("movedown_hscroll_resize", function(cm) {testMovedownResize(cm, true);});
  56. function testMoveright(cm, wrap, scroll) {
  57. cm.setSize("100px", "100px");
  58. if (wrap) cm.setOption("lineWrapping", true);
  59. if (scroll) {
  60. cm.setValue("\n" + new Array(100).join("x\n"));
  61. cm.setCursor(Pos(0, 0));
  62. }
  63. var right = displayRight(cm, scroll);
  64. for (var i = 0; i < 10; i++) {
  65. cm.replaceSelection("xxxxxxxxxx");
  66. var cursorRight = cm.cursorCoords(null, "window").right;
  67. is(cursorRight < right);
  68. }
  69. if (!wrap) is(cursorRight > right - 20);
  70. }
  71. testCM("moveright", function(cm) {testMoveright(cm, false, false);});
  72. testCM("moveright_wrap", function(cm) {testMoveright(cm, true, false);});
  73. testCM("moveright_scroll", function(cm) {testMoveright(cm, false, true);});
  74. testCM("moveright_scroll_wrap", function(cm) {testMoveright(cm, true, true);});
  75. testCM("suddenly_wide", function(cm) {
  76. addDoc(cm, 100, 100);
  77. cm.replaceSelection(new Array(600).join("l ") + "\n");
  78. cm.execCommand("goLineUp");
  79. cm.execCommand("goLineEnd");
  80. is(barH(cm).scrollLeft > cm.getScrollerElement().scrollLeft - 1);
  81. });
  82. testCM("wrap_changes_height", function(cm) {
  83. var line = new Array(20).join("a ") + "\n";
  84. cm.setValue(new Array(20).join(line));
  85. var box = cm.getWrapperElement().getBoundingClientRect();
  86. cm.setSize(cm.cursorCoords(Pos(0), "window").right - box.left + 2,
  87. cm.cursorCoords(Pos(19, 0), "window").bottom - box.top + 2);
  88. cm.setCursor(Pos(19, 0));
  89. cm.replaceSelection("\n");
  90. is(cm.cursorCoords(null, "window").bottom < displayBottom(cm, false));
  91. }, {lineWrapping: true});
  92. testCM("height_auto_with_gutter_expect_no_scroll_after_line_delete", function(cm) {
  93. cm.setSize(null, "auto");
  94. cm.setValue("x\n");
  95. cm.execCommand("goDocEnd");
  96. cm.execCommand("delCharBefore");
  97. eq(cm.getScrollInfo().top, 0);
  98. cm.scrollTo(null, 10);
  99. is(cm.getScrollInfo().top < 5);
  100. }, {lineNumbers: true});
  101. testCM("bidi_ensureCursorVisible", function(cm) {
  102. cm.setValue("<dd>وضع الاستخدام. عندما لا تعطى، وهذا الافتراضي إلى الطريقة الاولى\n");
  103. cm.execCommand("goLineStart");
  104. eq(cm.getScrollInfo().left, 0);
  105. cm.execCommand("goCharRight");
  106. cm.execCommand("goCharRight");
  107. cm.execCommand("goCharRight");
  108. eqCursorPos(cm.getCursor(), Pos(0, 3, "before"));
  109. eq(cm.getScrollInfo().left, 0);
  110. }, {lineWrapping: false});
  111. })();