placeholder_test.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. ace_require("amd-loader");
  32. ace_require("./test/mockdom");
  33. }
  34. define(function(ace_require, exports, module) {
  35. "use strict";
  36. var EditSession = ace_require("./edit_session").EditSession;
  37. var Editor = ace_require("./editor").Editor;
  38. var MockRenderer = ace_require("./test/mockrenderer").MockRenderer;
  39. var assert = ace_require("./test/assertions");
  40. var JavaScriptMode = ace_require("./mode/javascript").Mode;
  41. var PlaceHolder = ace_require("./placeholder").PlaceHolder;
  42. var UndoManager = ace_require("./undomanager").UndoManager;
  43. ace_require("./multi_select");
  44. module.exports = {
  45. "test: simple at the end appending of text" : function() {
  46. var session = new EditSession("var a = 10;\nconsole.log(a, a);", new JavaScriptMode());
  47. var editor = new Editor(new MockRenderer(), session);
  48. new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]);
  49. editor.moveCursorTo(0, 5);
  50. editor.insert('b');
  51. assert.equal(session.doc.getValue(), "var ab = 10;\nconsole.log(ab, ab);");
  52. editor.insert('cd');
  53. assert.equal(session.doc.getValue(), "var abcd = 10;\nconsole.log(abcd, abcd);");
  54. editor.remove('left');
  55. editor.remove('left');
  56. editor.remove('left');
  57. assert.equal(session.doc.getValue(), "var a = 10;\nconsole.log(a, a);");
  58. },
  59. "test: inserting text outside placeholder" : function() {
  60. var session = new EditSession("var a = 10;\nconsole.log(a, a);\n", new JavaScriptMode());
  61. var editor = new Editor(new MockRenderer(), session);
  62. new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]);
  63. editor.moveCursorTo(2, 0);
  64. editor.insert('b');
  65. assert.equal(session.doc.getValue(), "var a = 10;\nconsole.log(a, a);\nb");
  66. },
  67. "test: insertion at the beginning" : function(next) {
  68. var session = new EditSession("var a = 10;\nconsole.log(a, a);", new JavaScriptMode());
  69. var editor = new Editor(new MockRenderer(), session);
  70. var p = new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]);
  71. editor.moveCursorTo(0, 4);
  72. editor.insert('$');
  73. assert.equal(session.doc.getValue(), "var $a = 10;\nconsole.log($a, $a);");
  74. editor.moveCursorTo(0, 4);
  75. // Have to put this in a setTimeout because the anchor is only fixed later.
  76. setTimeout(function() {
  77. editor.insert('v');
  78. assert.equal(session.doc.getValue(), "var v$a = 10;\nconsole.log(v$a, v$a);");
  79. next();
  80. }, 20);
  81. },
  82. "test: detaching placeholder" : function() {
  83. var session = new EditSession("var a = 10;\nconsole.log(a, a);", new JavaScriptMode());
  84. var editor = new Editor(new MockRenderer(), session);
  85. var p = new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]);
  86. editor.moveCursorTo(0, 5);
  87. editor.insert('b');
  88. assert.equal(session.doc.getValue(), "var ab = 10;\nconsole.log(ab, ab);");
  89. p.detach();
  90. editor.insert('cd');
  91. assert.equal(session.doc.getValue(), "var abcd = 10;\nconsole.log(ab, ab);");
  92. },
  93. "test: events" : function() {
  94. var session = new EditSession("var a = 10;\nconsole.log(a, a);", new JavaScriptMode());
  95. var editor = new Editor(new MockRenderer(), session);
  96. var p = new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]);
  97. var entered = false;
  98. var left = false;
  99. p.on("cursorEnter", function() {
  100. entered = true;
  101. });
  102. p.on("cursorLeave", function() {
  103. left = true;
  104. });
  105. editor.moveCursorTo(0, 0);
  106. editor.moveCursorTo(0, 4);
  107. p.onCursorChange(); // Have to do this by hand because moveCursorTo doesn't trigger the event
  108. assert.ok(entered);
  109. editor.moveCursorTo(1, 0);
  110. p.onCursorChange(); // Have to do this by hand because moveCursorTo doesn't trigger the event
  111. assert.ok(left);
  112. },
  113. "test: cancel": function(next) {
  114. var session = new EditSession("var a = 10;\nconsole.log(a, a);", new JavaScriptMode());
  115. session.setUndoManager(new UndoManager());
  116. var editor = new Editor(new MockRenderer(), session);
  117. var p = new PlaceHolder(session, 1, {row: 0, column: 4}, [{row: 1, column: 12}, {row: 1, column: 15}]);
  118. editor.moveCursorTo(0, 5);
  119. editor.insert('b');
  120. editor.insert('cd');
  121. editor.remove('left');
  122. assert.equal(session.doc.getValue(), "var abc = 10;\nconsole.log(abc, abc);");
  123. // Wait a little for the changes to enter the undo stack
  124. setTimeout(function() {
  125. p.cancel();
  126. assert.equal(session.doc.getValue(), "var a = 10;\nconsole.log(a, a);");
  127. next();
  128. }, 80);
  129. }
  130. };
  131. });
  132. if (typeof module !== "undefined" && module === ace_require.main) {
  133. ace_require("asyncjs").test.testcase(module.exports).exec();
  134. }