editor_navigation_test.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. if (typeof process !== "undefined") {
  31. require("amd-loader");
  32. require("./test/mockdom");
  33. }
  34. define(function(require, exports, module) {
  35. "use strict";
  36. var EditSession = require("./edit_session").EditSession;
  37. var Editor = require("./editor").Editor;
  38. var MockRenderer = require("./test/mockrenderer").MockRenderer;
  39. var assert = require("./test/assertions");
  40. module.exports = {
  41. createEditSession : function(rows, cols) {
  42. var line = new Array(cols + 1).join("a");
  43. var text = new Array(rows).join(line + "\n") + line;
  44. return new EditSession(text);
  45. },
  46. "test: navigate to end of file should scroll the last line into view" : function() {
  47. var doc = this.createEditSession(200, 10);
  48. var editor = new Editor(new MockRenderer(), doc);
  49. editor.navigateFileEnd();
  50. var cursor = editor.getCursorPosition();
  51. assert.ok(editor.getFirstVisibleRow() <= cursor.row);
  52. assert.ok(editor.getLastVisibleRow() >= cursor.row);
  53. },
  54. "test: navigate to start of file should scroll the first row into view" : function() {
  55. var doc = this.createEditSession(200, 10);
  56. var editor = new Editor(new MockRenderer(), doc);
  57. editor.moveCursorTo(editor.getLastVisibleRow() + 20);
  58. editor.navigateFileStart();
  59. assert.equal(editor.getFirstVisibleRow(), 0);
  60. },
  61. "test: goto hidden line should scroll the line into the middle of the viewport" : function() {
  62. var editor = new Editor(new MockRenderer(), this.createEditSession(200, 5));
  63. editor.navigateTo(0, 0);
  64. editor.gotoLine(101);
  65. assert.position(editor.getCursorPosition(), 100, 0);
  66. assert.equal(editor.getFirstVisibleRow(), 89);
  67. editor.navigateTo(100, 0);
  68. editor.gotoLine(11);
  69. assert.position(editor.getCursorPosition(), 10, 0);
  70. assert.equal(editor.getFirstVisibleRow(), 0);
  71. editor.navigateTo(100, 0);
  72. editor.gotoLine(6);
  73. assert.position(editor.getCursorPosition(), 5, 0);
  74. assert.equal(0, editor.getFirstVisibleRow(), 0);
  75. editor.navigateTo(100, 0);
  76. editor.gotoLine(1);
  77. assert.position(editor.getCursorPosition(), 0, 0);
  78. assert.equal(editor.getFirstVisibleRow(), 0);
  79. editor.navigateTo(0, 0);
  80. editor.gotoLine(191);
  81. assert.position(editor.getCursorPosition(), 190, 0);
  82. assert.equal(editor.getFirstVisibleRow(), 179);
  83. editor.navigateTo(0, 0);
  84. editor.gotoLine(196);
  85. assert.position(editor.getCursorPosition(), 195, 0);
  86. assert.equal(editor.getFirstVisibleRow(), 180);
  87. },
  88. "test: goto visible line should only move the cursor and not scroll": function() {
  89. var editor = new Editor(new MockRenderer(), this.createEditSession(200, 5));
  90. editor.navigateTo(0, 0);
  91. editor.gotoLine(12);
  92. assert.position(editor.getCursorPosition(), 11, 0);
  93. assert.equal(editor.getFirstVisibleRow(), 0);
  94. editor.navigateTo(30, 0);
  95. editor.gotoLine(33);
  96. assert.position(editor.getCursorPosition(), 32, 0);
  97. assert.equal(editor.getFirstVisibleRow(), 30);
  98. },
  99. "test: navigate from the end of a long line down to a short line and back should maintain the curser column": function() {
  100. var editor = new Editor(new MockRenderer(), new EditSession(["123456", "1"]));
  101. editor.navigateTo(0, 6);
  102. assert.position(editor.getCursorPosition(), 0, 6);
  103. editor.navigateDown();
  104. assert.position(editor.getCursorPosition(), 1, 1);
  105. editor.navigateUp();
  106. assert.position(editor.getCursorPosition(), 0, 6);
  107. },
  108. "test: reset desired column on navigate left or right": function() {
  109. var editor = new Editor(new MockRenderer(), new EditSession(["123456", "12"]));
  110. editor.navigateTo(0, 6);
  111. assert.position(editor.getCursorPosition(), 0, 6);
  112. editor.navigateDown();
  113. assert.position(editor.getCursorPosition(), 1, 2);
  114. editor.navigateLeft();
  115. assert.position(editor.getCursorPosition(), 1, 1);
  116. editor.navigateUp();
  117. assert.position(editor.getCursorPosition(), 0, 1);
  118. },
  119. "test: typing text should update the desired column": function() {
  120. var editor = new Editor(new MockRenderer(), new EditSession(["1234", "1234567890"]));
  121. editor.navigateTo(0, 3);
  122. editor.insert("juhu");
  123. editor.navigateDown();
  124. assert.position(editor.getCursorPosition(), 1, 7);
  125. }
  126. };
  127. });
  128. if (typeof module !== "undefined" && module === require.main) {
  129. require("asyncjs").test.testcase(module.exports).exec()
  130. }