multi_select_test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. require("./multi_select");
  37. var assert = require("./test/assertions");
  38. var Range = require("./range").Range;
  39. var Editor = require("./editor").Editor;
  40. var EditSession = require("./edit_session").EditSession;
  41. var MockRenderer = require("./test/mockrenderer").MockRenderer;
  42. var UndoManager = require("./undomanager").UndoManager;
  43. var editor;
  44. var exec = function(name, times, args) {
  45. do {
  46. editor.commands.exec(name, editor, args);
  47. } while(times --> 1);
  48. };
  49. var testRanges = function(str) {
  50. assert.equal(editor.selection.getAllRanges() + "", str + "");
  51. };
  52. function getSelection(editor) {
  53. var data = editor.multiSelect.toJSON();
  54. if (!data.length) data = [data];
  55. data = data.map(function(x) {
  56. var a, c;
  57. if (x.isBackwards) {
  58. a = x.end;
  59. c = x.start;
  60. } else {
  61. c = x.end;
  62. a = x.start;
  63. }
  64. return Range.comparePoints(a, c)
  65. ? [a.row, a.column, c.row, c.column]
  66. : [a.row, a.column];
  67. });
  68. return data.length > 1 ? data : data[0];
  69. }
  70. function testSelection(editor, data) {
  71. assert.equal(getSelection(editor) + "", data + "");
  72. }
  73. function setSelection(editor, data) {
  74. if (typeof data[0] == "number")
  75. data = [data];
  76. editor.selection.fromJSON(data.map(function(x) {
  77. var start = {row: x[0], column: x[1]};
  78. var end = x.length == 2 ? start : {row: x[2], column: x[3]};
  79. var isBackwards = Range.comparePoints(start, end) > 0;
  80. return isBackwards ? {
  81. start: end,
  82. end: start,
  83. isBackwards: isBackwards
  84. } : {
  85. start: start,
  86. end: end,
  87. isBackwards: isBackwards
  88. };
  89. }));
  90. }
  91. module.exports = {
  92. name: "ACE multi_select.js",
  93. "test: multiselect editing": function() {
  94. var doc = new EditSession([
  95. "w1.w2",
  96. " wtt.w",
  97. " wtt.w"
  98. ]);
  99. editor = new Editor(new MockRenderer(), doc);
  100. editor.navigateFileEnd();
  101. exec("selectMoreBefore", 3);
  102. assert.ok(editor.inMultiSelectMode);
  103. assert.equal(editor.selection.getAllRanges().length, 4);
  104. var newLine = editor.session.getDocument().getNewLineCharacter();
  105. var copyText = "wwww".split("").join(newLine);
  106. assert.equal(editor.getCopyText(), copyText);
  107. exec("insertstring", 1, "a");
  108. exec("backspace", 2);
  109. assert.equal(editor.session.getValue(), "w1.w2\ntt\ntt");
  110. assert.equal(editor.selection.getAllRanges().length, 4);
  111. exec("selectall");
  112. assert.ok(!editor.inMultiSelectMode);
  113. //assert.equal(editor.selection.getAllRanges().length, 1);
  114. },
  115. "test: multiselect navigation": function() {
  116. var doc = new EditSession([
  117. "w1.w2",
  118. " wtt.w",
  119. " wtt.we"
  120. ]);
  121. editor = new Editor(new MockRenderer(), doc);
  122. editor.selectMoreLines(1);
  123. testRanges("Range: [0/0] -> [0/0],Range: [1/0] -> [1/0]");
  124. assert.ok(editor.inMultiSelectMode);
  125. exec("golinedown");
  126. exec("gotolineend");
  127. testRanges("Range: [1/9] -> [1/9],Range: [2/10] -> [2/10]");
  128. exec("selectwordleft");
  129. testRanges("Range: [1/8] -> [1/9],Range: [2/8] -> [2/10]");
  130. exec("golinedown", 2);
  131. assert.ok(!editor.inMultiSelectMode);
  132. },
  133. "test: multiselect session change": function() {
  134. var doc = new EditSession([
  135. "w1.w2",
  136. " wtt.w",
  137. " wtt.w"
  138. ]);
  139. editor = new Editor(new MockRenderer(), doc);
  140. editor.selectMoreLines(1);
  141. testRanges("Range: [0/0] -> [0/0],Range: [1/0] -> [1/0]");
  142. assert.ok(editor.inMultiSelectMode);
  143. var doc2 = new EditSession(["w1"]);
  144. editor.setSession(doc2);
  145. assert.ok(!editor.inMultiSelectMode);
  146. editor.setSession(doc);
  147. assert.ok(editor.inMultiSelectMode);
  148. },
  149. "test: multiselect addRange": function() {
  150. var doc = new EditSession([
  151. "w1.w2",
  152. " wtt.w",
  153. " wtt.w"
  154. ]);
  155. editor = new Editor(new MockRenderer(), doc);
  156. var selection = editor.selection;
  157. var range1 = new Range(0, 2, 0, 4);
  158. editor.selection.fromOrientedRange(range1);
  159. var range2 = new Range(0, 3, 0, 4);
  160. selection.addRange(range2);
  161. assert.ok(!editor.inMultiSelectMode);
  162. assert.ok(range2.isEqual(editor.selection.getRange()));
  163. var range3 = new Range(0, 1, 0, 1);
  164. selection.addRange(range3);
  165. assert.ok(editor.inMultiSelectMode);
  166. testRanges([range3, range2]);
  167. var range4 = new Range(0, 0, 4, 0);
  168. selection.addRange(range4);
  169. assert.ok(!editor.inMultiSelectMode);
  170. },
  171. "test: onPaste in command with multiselect": function() {
  172. var doc = new EditSession(["l1", "l2"]);
  173. editor = new Editor(new MockRenderer(), doc);
  174. editor.commands.addCommand({
  175. name: 'insertfoo',
  176. exec: function(ed) { ed.onPaste('foo'); },
  177. multiSelectAction: "forEach"
  178. });
  179. var selection = editor.selection;
  180. var range1 = new Range(0,2,0,2);
  181. var range2 = new Range(1,2,1,2);
  182. selection.fromOrientedRange(range1);
  183. selection.addRange(range2);
  184. editor.execCommand('insertfoo');
  185. assert.equal('l1foo\nl2foo', editor.getValue());
  186. },
  187. "test multiselect move lines": function() {
  188. editor = new Editor(new MockRenderer());
  189. editor.setValue("l1\nl2\nl3\nl4", -1);
  190. setSelection(editor, [[0,2],[1,2],[2,2],[3,2]]);
  191. exec("copylinesdown");
  192. assert.equal(editor.getValue(),"l1\nl1\nl2\nl2\nl3\nl3\nl4\nl4");
  193. testSelection(editor, [[1,2],[3,2],[5,2],[7,2]]);
  194. exec("copylinesup");
  195. assert.equal(editor.getValue(),"l1\nl1\nl1\nl2\nl2\nl2\nl3\nl3\nl3\nl4\nl4\nl4");
  196. testSelection(editor, [[1,2],[4,2],[7,2],[10,2]]);
  197. exec("removeline");
  198. assert.equal(editor.getValue(),"l1\nl1\nl2\nl2\nl3\nl3\nl4\nl4");
  199. testSelection(editor, [[1,0],[3,0],[5,0],[7,0]]);
  200. setSelection(editor, [[1,2],[1,1,1,0],[3,0,3,1],[5,0,5,1],[7,0,7,1]]);
  201. exec("copylinesdown");
  202. exec("copylinesup");
  203. assert.equal(editor.getValue(),"l1\nl1\nl1\nl1\nl2\nl2\nl2\nl2\nl3\nl3\nl3\nl3\nl4\nl4\nl4\nl4");
  204. testSelection(editor, [[2,2],[2,1,2,0],[6,0,6,1],[10,0,10,1],[14,0,14,1]]);
  205. exec("movelinesdown", 12);
  206. assert.equal(editor.getValue(),"l1\nl1\nl1\nl2\nl2\nl2\nl3\nl3\nl3\nl4\nl4\nl4\nl1\nl2\nl3\nl4");
  207. testSelection(editor, [[12,2],[12,1,12,0],[13,0,13,1],[14,0,14,1],[15,0,15,1]]);
  208. exec("movelinesup", 12);
  209. assert.equal(editor.getValue(),"l1\nl2\nl3\nl4\nl1\nl1\nl1\nl2\nl2\nl2\nl3\nl3\nl3\nl4\nl4\nl4");
  210. testSelection(editor, [[0,2],[0,1,0,0],[1,0,1,1],[2,0,2,1],[3,0,3,1]]);
  211. },
  212. "test multiselect fromJSON/toJSON": function() {
  213. var doc = new EditSession(["l1", "l2"]);
  214. editor = new Editor(new MockRenderer(), doc);
  215. var selection = editor.selection;
  216. var before = selection.toJSON();
  217. var range1 = new Range(0,2,0,2);
  218. var range2 = new Range(1,2,1,2);
  219. selection.fromOrientedRange(range1);
  220. selection.addRange(range2);
  221. var after = selection.toJSON();
  222. selection.fromJSON(before);
  223. assert.ok(!selection.isEqual(after));
  224. assert.ok(selection.isEqual(before));
  225. selection.fromJSON(after);
  226. assert.ok(!selection.isEqual(before));
  227. assert.ok(selection.isEqual(after));
  228. },
  229. "test multiselect align": function() {
  230. var doc = new EditSession(["l1", "l2", "l3"]);
  231. doc.setUndoManager(new UndoManager());
  232. editor = new Editor(new MockRenderer(), doc);
  233. var selection = editor.selection;
  234. selection.addRange(new Range(1,0,1,0))
  235. selection.addRange(new Range(2,2,2,2))
  236. editor.execCommand("alignCursors");
  237. assert.equal(' l1\n l2\nl3', editor.getValue());
  238. doc.markUndoGroup();
  239. editor.execCommand("undo");
  240. assert.equal('l1\nl2\nl3', editor.getValue());
  241. }
  242. };
  243. });
  244. if (typeof module !== "undefined" && module === require.main) {
  245. require("asyncjs").test.testcase(module.exports).exec();
  246. }