browser_gcli_canon.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright 2012, Mozilla Foundation and contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. "use strict";
  17. // THIS FILE IS GENERATED FROM SOURCE IN THE GCLI PROJECT
  18. // PLEASE TALK TO SOMEONE IN DEVELOPER TOOLS BEFORE EDITING IT
  19. const exports = {};
  20. function test() {
  21. helpers.runTestModule(exports, "browser_gcli_canon.js");
  22. }
  23. // var assert = require('../testharness/assert');
  24. // var helpers = require('./helpers');
  25. var Commands = require("gcli/commands/commands").Commands;
  26. var startCount;
  27. var events;
  28. var commandsChange = function (ev) {
  29. events++;
  30. };
  31. exports.setup = function (options) {
  32. startCount = options.requisition.system.commands.getAll().length;
  33. events = 0;
  34. };
  35. exports.shutdown = function (options) {
  36. startCount = undefined;
  37. events = undefined;
  38. };
  39. exports.testAddRemove1 = function (options) {
  40. var commands = options.requisition.system.commands;
  41. return helpers.audit(options, [
  42. {
  43. name: "testadd add",
  44. setup: function () {
  45. commands.onCommandsChange.add(commandsChange);
  46. commands.add({
  47. name: "testadd",
  48. exec: function () {
  49. return 1;
  50. }
  51. });
  52. assert.is(commands.getAll().length,
  53. startCount + 1,
  54. "add command success");
  55. assert.is(events, 1, "add event");
  56. return helpers.setInput(options, "testadd");
  57. },
  58. check: {
  59. input: "testadd",
  60. hints: "",
  61. markup: "VVVVVVV",
  62. cursor: 7,
  63. current: "__command",
  64. status: "VALID",
  65. predictions: [ ],
  66. unassigned: [ ],
  67. args: { }
  68. },
  69. exec: {
  70. output: /^1$/
  71. }
  72. },
  73. {
  74. name: "testadd alter",
  75. setup: function () {
  76. commands.add({
  77. name: "testadd",
  78. exec: function () {
  79. return 2;
  80. }
  81. });
  82. assert.is(commands.getAll().length,
  83. startCount + 1,
  84. "read command success");
  85. assert.is(events, 2, "read event");
  86. return helpers.setInput(options, "testadd");
  87. },
  88. check: {
  89. input: "testadd",
  90. hints: "",
  91. markup: "VVVVVVV",
  92. },
  93. exec: {
  94. output: "2"
  95. }
  96. },
  97. {
  98. name: "testadd remove",
  99. setup: function () {
  100. commands.remove("testadd");
  101. assert.is(commands.getAll().length,
  102. startCount,
  103. "remove command success");
  104. assert.is(events, 3, "remove event");
  105. return helpers.setInput(options, "testadd");
  106. },
  107. check: {
  108. typed: "testadd",
  109. cursor: 7,
  110. current: "__command",
  111. status: "ERROR",
  112. unassigned: [ ],
  113. }
  114. }
  115. ]);
  116. };
  117. exports.testAddRemove2 = function (options) {
  118. var commands = options.requisition.system.commands;
  119. commands.add({
  120. name: "testadd",
  121. exec: function () {
  122. return 3;
  123. }
  124. });
  125. assert.is(commands.getAll().length,
  126. startCount + 1,
  127. "rereadd command success");
  128. assert.is(events, 4, "rereadd event");
  129. return helpers.audit(options, [
  130. {
  131. setup: "testadd",
  132. exec: {
  133. output: /^3$/
  134. },
  135. post: function () {
  136. commands.remove({
  137. name: "testadd"
  138. });
  139. assert.is(commands.getAll().length,
  140. startCount,
  141. "reremove command success");
  142. assert.is(events, 5, "reremove event");
  143. }
  144. },
  145. {
  146. setup: "testadd",
  147. check: {
  148. typed: "testadd",
  149. status: "ERROR"
  150. }
  151. }
  152. ]);
  153. };
  154. exports.testAddRemove3 = function (options) {
  155. var commands = options.requisition.system.commands;
  156. commands.remove({ name: "nonexistant" });
  157. assert.is(commands.getAll().length,
  158. startCount,
  159. "nonexistant1 command success");
  160. assert.is(events, 5, "nonexistant1 event");
  161. commands.remove("nonexistant");
  162. assert.is(commands.getAll().length,
  163. startCount,
  164. "nonexistant2 command success");
  165. assert.is(events, 5, "nonexistant2 event");
  166. commands.onCommandsChange.remove(commandsChange);
  167. };
  168. exports.testAltCommands = function (options) {
  169. var commands = options.requisition.system.commands;
  170. var altCommands = new Commands(options.requisition.system.types);
  171. var tss = {
  172. name: "tss",
  173. params: [
  174. { name: "str", type: "string" },
  175. { name: "num", type: "number" },
  176. { name: "opt", type: { name: "selection", data: [ "1", "2", "3" ] } },
  177. ],
  178. customProp1: "localValue",
  179. customProp2: true,
  180. customProp3: 42,
  181. exec: function (args, context) {
  182. return context.commandName + ":" +
  183. args.str + ":" + args.num + ":" + args.opt;
  184. }
  185. };
  186. altCommands.add(tss);
  187. var commandSpecs = altCommands.getCommandSpecs();
  188. assert.is(JSON.stringify(commandSpecs),
  189. '[{"item":"command","name":"tss","params":[' +
  190. '{"name":"str","type":"string"},' +
  191. '{"name":"num","type":"number"},' +
  192. '{"name":"opt","type":{"name":"selection","data":["1","2","3"]}}' +
  193. '],"isParent":false}]',
  194. "JSON.stringify(commandSpecs)");
  195. var customProps = [ "customProp1", "customProp2", "customProp3", ];
  196. var commandSpecs2 = altCommands.getCommandSpecs(customProps);
  197. assert.is(JSON.stringify(commandSpecs2),
  198. "[{" +
  199. '"item":"command",' +
  200. '"name":"tss",' +
  201. '"params":[' +
  202. '{"name":"str","type":"string"},' +
  203. '{"name":"num","type":"number"},' +
  204. '{"name":"opt","type":{"name":"selection","data":["1","2","3"]}}' +
  205. "]," +
  206. '"isParent":false,' +
  207. '"customProp1":"localValue",' +
  208. '"customProp2":true,' +
  209. '"customProp3":42' +
  210. "}]",
  211. "JSON.stringify(commandSpecs)");
  212. var remoter = function (args, context) {
  213. assert.is(context.commandName, "tss", "commandName is tss");
  214. var cmd = altCommands.get(context.commandName);
  215. return cmd.exec(args, context);
  216. };
  217. commands.addProxyCommands(commandSpecs, remoter, "proxy", "test");
  218. var parent = commands.get("proxy");
  219. assert.is(parent.name, "proxy", "Parent command called proxy");
  220. var child = commands.get("proxy tss");
  221. assert.is(child.name, "proxy tss", "child command called proxy tss");
  222. return helpers.audit(options, [
  223. {
  224. setup: "proxy tss foo 6 3",
  225. check: {
  226. input: "proxy tss foo 6 3",
  227. hints: "",
  228. markup: "VVVVVVVVVVVVVVVVV",
  229. cursor: 17,
  230. status: "VALID",
  231. args: {
  232. str: { value: "foo", status: "VALID" },
  233. num: { value: 6, status: "VALID" },
  234. opt: { value: "3", status: "VALID" }
  235. }
  236. },
  237. exec: {
  238. output: "tss:foo:6:3"
  239. },
  240. post: function () {
  241. commands.remove("proxy");
  242. commands.remove("proxy tss");
  243. assert.is(commands.get("proxy"), undefined, "remove proxy");
  244. assert.is(commands.get("proxy tss"), undefined, "remove proxy tss");
  245. }
  246. }
  247. ]);
  248. };