editor_change_document_test.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 Text = require("./mode/text").Mode;
  39. var JavaScriptMode = require("./mode/javascript").Mode;
  40. var CssMode = require("./mode/css").Mode;
  41. var HtmlMode = require("./mode/html").Mode;
  42. var MockRenderer = require("./test/mockrenderer").MockRenderer;
  43. var assert = require("./test/assertions");
  44. module.exports = {
  45. setUp : function(next) {
  46. this.session1 = new EditSession(["abc", "def"]);
  47. this.session2 = new EditSession(["ghi", "jkl"]);
  48. this.editor = new Editor(new MockRenderer());
  49. next();
  50. },
  51. "test: change document" : function() {
  52. this.editor.setSession(this.session1);
  53. assert.equal(this.editor.getSession(), this.session1);
  54. this.editor.setSession(this.session2);
  55. assert.equal(this.editor.getSession(), this.session2);
  56. },
  57. "test: only changes to the new document should have effect" : function() {
  58. var called = false;
  59. this.editor.onDocumentChange = function() {
  60. called = true;
  61. };
  62. this.editor.setSession(this.session1);
  63. this.editor.setSession(this.session2);
  64. this.session1.duplicateLines(0, 0);
  65. assert.notOk(called);
  66. this.session2.duplicateLines(0, 0);
  67. assert.ok(called);
  68. },
  69. "test: should use cursor of new document" : function() {
  70. this.session1.getSelection().moveCursorTo(0, 1);
  71. this.session2.getSelection().moveCursorTo(1, 0);
  72. this.editor.setSession(this.session1);
  73. assert.position(this.editor.getCursorPosition(), 0, 1);
  74. this.editor.setSession(this.session2);
  75. assert.position(this.editor.getCursorPosition(), 1, 0);
  76. },
  77. "test: only changing the cursor of the new doc should not have an effect" : function() {
  78. this.editor.onCursorChange = function() {
  79. called = true;
  80. };
  81. this.editor.setSession(this.session1);
  82. this.editor.setSession(this.session2);
  83. assert.position(this.editor.getCursorPosition(), 0, 0);
  84. var called = false;
  85. this.session1.getSelection().moveCursorTo(0, 1);
  86. assert.position(this.editor.getCursorPosition(), 0, 0);
  87. assert.notOk(called);
  88. this.session2.getSelection().moveCursorTo(1, 1);
  89. assert.position(this.editor.getCursorPosition(), 1, 1);
  90. assert.ok(called);
  91. },
  92. "test: should use selection of new document" : function() {
  93. this.session1.getSelection().selectTo(0, 1);
  94. this.session2.getSelection().selectTo(1, 0);
  95. this.editor.setSession(this.session1);
  96. assert.position(this.editor.getSelection().getSelectionLead(), 0, 1);
  97. this.editor.setSession(this.session2);
  98. assert.position(this.editor.getSelection().getSelectionLead(), 1, 0);
  99. },
  100. "test: only changing the selection of the new doc should not have an effect" : function() {
  101. this.editor.onSelectionChange = function() {
  102. called = true;
  103. };
  104. this.editor.setSession(this.session1);
  105. this.editor.setSession(this.session2);
  106. assert.position(this.editor.getSelection().getSelectionLead(), 0, 0);
  107. var called = false;
  108. this.session1.getSelection().selectTo(0, 1);
  109. assert.position(this.editor.getSelection().getSelectionLead(), 0, 0);
  110. assert.notOk(called);
  111. this.session2.getSelection().selectTo(1, 1);
  112. assert.position(this.editor.getSelection().getSelectionLead(), 1, 1);
  113. assert.ok(called);
  114. },
  115. "test: should use mode of new document" : function() {
  116. this.editor.onChangeMode = function() {
  117. called = true;
  118. };
  119. this.editor.setSession(this.session1);
  120. this.editor.setSession(this.session2);
  121. var called = false;
  122. this.session1.setMode(new Text());
  123. assert.notOk(called);
  124. this.session2.setMode(new JavaScriptMode());
  125. assert.ok(called);
  126. },
  127. "test: should use stop worker of old document" : function(next) {
  128. var self = this;
  129. // 1. Open an editor and set the session to CssMode
  130. self.editor.setSession(self.session1);
  131. self.session1.setMode(new CssMode());
  132. // 2. Add a line or two of valid CSS.
  133. self.session1.setValue("DIV { color: red; }");
  134. // 3. Clear the session value.
  135. self.session1.setValue("");
  136. // 4. Set the session to HtmlMode
  137. self.session1.setMode(new HtmlMode());
  138. // 5. Try to type valid HTML
  139. self.session1.insert({row: 0, column: 0}, "<html></html>");
  140. setTimeout(function() {
  141. assert.equal(Object.keys(self.session1.getAnnotations()).length, 0);
  142. next();
  143. }, 600);
  144. }
  145. };
  146. });
  147. if (typeof module !== "undefined" && module === require.main) {
  148. require("asyncjs").test.testcase(module.exports).exec()
  149. }