command_manager_test.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 CommandManager = require("./command_manager").CommandManager;
  36. var keys = require("../lib/keys");
  37. var assert = require("../test/assertions");
  38. module.exports = {
  39. setUp: function() {
  40. this.command = {
  41. name: "gotoline",
  42. bindKey: {
  43. mac: "Command-L",
  44. win: "Ctrl-L"
  45. },
  46. called: false,
  47. exec: function(editor) { this.called = true; }
  48. };
  49. this.cm = new CommandManager("mac", [this.command]);
  50. },
  51. "test: register command": function() {
  52. this.cm.exec("gotoline");
  53. assert.ok(this.command.called);
  54. },
  55. "test: mac hotkeys": function() {
  56. var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "l");
  57. assert.equal(command, this.command);
  58. var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "l");
  59. assert.equal(command, undefined);
  60. },
  61. "test: win hotkeys": function() {
  62. var cm = new CommandManager("win", [this.command]);
  63. var command = cm.findKeyCommand(keys.KEY_MODS.command, "l");
  64. assert.equal(command, undefined);
  65. var command = cm.findKeyCommand(keys.KEY_MODS.ctrl, "l");
  66. assert.equal(command, this.command);
  67. },
  68. "test: remove command by object": function() {
  69. this.cm.removeCommand(this.command);
  70. this.cm.exec("gotoline");
  71. assert.ok(!this.command.called);
  72. var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "l");
  73. assert.equal(command, null);
  74. },
  75. "test: remove command by name": function() {
  76. this.cm.removeCommand("gotoline");
  77. this.cm.exec("gotoline");
  78. assert.ok(!this.command.called);
  79. var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "l");
  80. assert.equal(command, null);
  81. },
  82. "test: adding a new command with the same name as an existing one should remove the old one first": function() {
  83. var command = {
  84. name: "gotoline",
  85. bindKey: {
  86. mac: "Command-L",
  87. win: "Ctrl-L"
  88. },
  89. called: false,
  90. exec: function(editor) { this.called = true; }
  91. };
  92. this.cm.addCommand(command);
  93. this.cm.exec("gotoline");
  94. assert.ok(command.called);
  95. assert.ok(!this.command.called);
  96. assert.equal(this.cm.findKeyCommand(keys.KEY_MODS.command, "l"), command);
  97. },
  98. "test: adding commands and recording a macro": function() {
  99. var called = "";
  100. this.cm.addCommands({
  101. togglerecording: function(editor) {
  102. editor.cm.toggleRecording(editor);
  103. },
  104. replay: function(editor) {
  105. editor.cm.replay();
  106. },
  107. cm1: function(editor, arg) {
  108. called += "1" + (arg || "");
  109. },
  110. cm2: function(editor) {
  111. called += "2";
  112. }
  113. });
  114. var statusUpdateEmitted = false;
  115. this._emit = function() {statusUpdateEmitted = true};
  116. this.cm.exec("togglerecording", this);
  117. assert.ok(this.cm.recording);
  118. assert.ok(statusUpdateEmitted);
  119. this.cm.exec("cm1", this, "-");
  120. this.cm.exec("cm2");
  121. this.cm.exec("replay", this);
  122. assert.ok(!this.cm.recording);
  123. assert.equal(called, "1-2");
  124. called = "";
  125. this.cm.exec("replay", this);
  126. assert.equal(called, "1-2");
  127. },
  128. "test: bindkeys": function() {
  129. this.cm.bindKeys({
  130. "Ctrl-L|Command-C": "cm1",
  131. "Ctrl-R": "cm2"
  132. });
  133. var command = this.cm.findKeyCommand(keys.KEY_MODS.command, "c");
  134. assert.equal(command, "cm1");
  135. var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "r");
  136. assert.equal(command, "cm2");
  137. this.cm.bindKeys({
  138. "Ctrl-R": null
  139. });
  140. var command = this.cm.findKeyCommand(keys.KEY_MODS.ctrl, "r");
  141. assert.equal(command, null);
  142. },
  143. "test: binding keys without modifiers": function() {
  144. this.cm.bindKeys({
  145. "R": "cm1",
  146. "Shift-r": "cm2",
  147. "Return": "cm4",
  148. "Enter": "cm3"
  149. });
  150. var command = this.cm.findKeyCommand(-1, "r");
  151. assert.equal(command, "cm1");
  152. var command = this.cm.findKeyCommand(-1, "R");
  153. assert.equal(command, "cm2");
  154. var command = this.cm.findKeyCommand(0, "return");
  155. assert.equal(command + "", ["cm4", "cm3"] + "");
  156. }
  157. };
  158. });
  159. if (typeof module !== "undefined" && module === require.main) {
  160. require("asyncjs").test.testcase(module.exports).exec();
  161. }