multi_select_test.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. }
  33. define(function(require, exports, module) {
  34. "use strict";
  35. var EditSession = require("./edit_session").EditSession;
  36. var Editor = require("./editor").Editor;
  37. var MockRenderer = require("./test/mockrenderer").MockRenderer;
  38. var Range = require("./range").Range;
  39. var assert = require("./test/assertions");
  40. var MultiSelect = require("./multi_select").MultiSelect;
  41. var editor;
  42. var exec = function(name, times, args) {
  43. do {
  44. editor.commands.exec(name, editor, args);
  45. } while(times --> 1)
  46. };
  47. var testRanges = function(str) {
  48. assert.equal(editor.selection.getAllRanges() + "", str + "");
  49. }
  50. module.exports = {
  51. name: "ACE multi_select.js",
  52. "test: multiselect editing": function() {
  53. var doc = new EditSession([
  54. "w1.w2",
  55. " wtt.w",
  56. " wtt.w"
  57. ]);
  58. editor = new Editor(new MockRenderer(), doc);
  59. MultiSelect(editor);
  60. editor.navigateFileEnd();
  61. exec("selectMoreBefore", 3);
  62. assert.ok(editor.inMultiSelectMode);
  63. assert.equal(editor.selection.getAllRanges().length, 4);
  64. var newLine = editor.session.getDocument().getNewLineCharacter();
  65. var copyText = "wwww".split("").join(newLine);
  66. assert.equal(editor.getCopyText(), copyText);
  67. exec("insertstring", 1, "a");
  68. exec("backspace", 2);
  69. assert.equal(editor.session.getValue(), "w1.w2\ntt\ntt");
  70. assert.equal(editor.selection.getAllRanges().length, 4);
  71. exec("selectall");
  72. assert.ok(!editor.inMultiSelectMode);
  73. //assert.equal(editor.selection.getAllRanges().length, 1);
  74. },
  75. "test: multiselect navigation": function() {
  76. var doc = new EditSession([
  77. "w1.w2",
  78. " wtt.w",
  79. " wtt.we"
  80. ]);
  81. editor = new Editor(new MockRenderer(), doc);
  82. MultiSelect(editor);
  83. editor.selectMoreLines(1);
  84. testRanges("Range: [0/0] -> [0/0],Range: [1/0] -> [1/0]");
  85. assert.ok(editor.inMultiSelectMode);
  86. exec("golinedown");
  87. exec("gotolineend");
  88. testRanges("Range: [1/9] -> [1/9],Range: [2/10] -> [2/10]");
  89. exec("selectwordleft");
  90. testRanges("Range: [1/8] -> [1/9],Range: [2/8] -> [2/10]");
  91. exec("golinedown", 2);
  92. assert.ok(!editor.inMultiSelectMode);
  93. },
  94. "test: multiselect session change": function() {
  95. var doc = new EditSession([
  96. "w1.w2",
  97. " wtt.w",
  98. " wtt.w"
  99. ]);
  100. editor = new Editor(new MockRenderer(), doc);
  101. MultiSelect(editor);
  102. editor.selectMoreLines(1)
  103. testRanges("Range: [0/0] -> [0/0],Range: [1/0] -> [1/0]");
  104. assert.ok(editor.inMultiSelectMode);
  105. var doc2 = new EditSession(["w1"]);
  106. editor.setSession(doc2);
  107. assert.ok(!editor.inMultiSelectMode);
  108. editor.setSession(doc);
  109. assert.ok(editor.inMultiSelectMode);
  110. },
  111. "test: multiselect addRange": function() {
  112. var doc = new EditSession([
  113. "w1.w2",
  114. " wtt.w",
  115. " wtt.w"
  116. ]);
  117. editor = new Editor(new MockRenderer(), doc);
  118. MultiSelect(editor);
  119. var selection = editor.selection;
  120. var range1 = new Range(0, 2, 0, 4);
  121. editor.selection.fromOrientedRange(range1);
  122. var range2 = new Range(0, 3, 0, 4);
  123. selection.addRange(range2);
  124. assert.ok(!editor.inMultiSelectMode);
  125. assert.ok(range2.isEqual(editor.selection.getRange()));
  126. var range3 = new Range(0, 1, 0, 1);
  127. selection.addRange(range3);
  128. assert.ok(editor.inMultiSelectMode);
  129. testRanges([range3, range2]);
  130. var range4 = new Range(0, 0, 4, 0);
  131. selection.addRange(range4);
  132. assert.ok(!editor.inMultiSelectMode);
  133. },
  134. "test: onPaste in command with multiselect": function() {
  135. var doc = new EditSession(["l1", "l2"]);
  136. editor = new Editor(new MockRenderer(), doc);
  137. MultiSelect(editor);
  138. editor.commands.addCommand({
  139. name: 'insertfoo',
  140. exec: function(ed) { ed.onPaste('foo'); },
  141. multiSelectAction: "forEach"
  142. });
  143. var selection = editor.selection;
  144. var range1 = new Range(0,2,0,2);
  145. var range2 = new Range(1,2,1,2);
  146. selection.fromOrientedRange(range1)
  147. selection.addRange(range2);
  148. editor.execCommand('insertfoo');
  149. assert.equal('l1foo\nl2foo', editor.getValue());
  150. },
  151. "test multiselect fromJSON/toJSON": function() {
  152. var doc = new EditSession(["l1", "l2"]);
  153. editor = new Editor(new MockRenderer(), doc);
  154. MultiSelect(editor);
  155. var selection = editor.selection;
  156. var before = selection.toJSON();
  157. var range1 = new Range(0,2,0,2);
  158. var range2 = new Range(1,2,1,2);
  159. selection.fromOrientedRange(range1)
  160. selection.addRange(range2);
  161. var after = selection.toJSON();
  162. selection.fromJSON(before);
  163. assert.ok(!selection.isEqual(after));
  164. assert.ok(selection.isEqual(before));
  165. selection.fromJSON(after);
  166. assert.ok(!selection.isEqual(before));
  167. assert.ok(selection.isEqual(after));
  168. }
  169. };
  170. });
  171. if (typeof module !== "undefined" && module === require.main) {
  172. require("asyncjs").test.testcase(module.exports).exec()
  173. }