summaryrefslogtreecommitdiffstats
path: root/devtools/client/commandline/test/browser_gcli_canon.js
blob: 8072445058e162d807c744bc7ae9965a3229c095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Copyright 2012, Mozilla Foundation and contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

"use strict";

// THIS FILE IS GENERATED FROM SOURCE IN THE GCLI PROJECT
// PLEASE TALK TO SOMEONE IN DEVELOPER TOOLS BEFORE EDITING IT

const exports = {};

function test() {
  helpers.runTestModule(exports, "browser_gcli_canon.js");
}

// var assert = require('../testharness/assert');
// var helpers = require('./helpers');
var Commands = require("gcli/commands/commands").Commands;

var startCount;
var events;

var commandsChange = function (ev) {
  events++;
};

exports.setup = function (options) {
  startCount = options.requisition.system.commands.getAll().length;
  events = 0;
};

exports.shutdown = function (options) {
  startCount = undefined;
  events = undefined;
};

exports.testAddRemove1 = function (options) {
  var commands = options.requisition.system.commands;

  return helpers.audit(options, [
    {
      name: "testadd add",
      setup: function () {
        commands.onCommandsChange.add(commandsChange);

        commands.add({
          name: "testadd",
          exec: function () {
            return 1;
          }
        });

        assert.is(commands.getAll().length,
                  startCount + 1,
                  "add command success");
        assert.is(events, 1, "add event");

        return helpers.setInput(options, "testadd");
      },
      check: {
        input:  "testadd",
        hints:         "",
        markup: "VVVVVVV",
        cursor: 7,
        current: "__command",
        status: "VALID",
        predictions: [ ],
        unassigned: [ ],
        args: { }
      },
      exec: {
        output: /^1$/
      }
    },
    {
      name: "testadd alter",
      setup: function () {
        commands.add({
          name: "testadd",
          exec: function () {
            return 2;
          }
        });

        assert.is(commands.getAll().length,
                  startCount + 1,
                  "read command success");
        assert.is(events, 2, "read event");

        return helpers.setInput(options, "testadd");
      },
      check: {
        input:  "testadd",
        hints:         "",
        markup: "VVVVVVV",
      },
      exec: {
        output: "2"
      }
    },
    {
      name: "testadd remove",
      setup: function () {
        commands.remove("testadd");

        assert.is(commands.getAll().length,
                  startCount,
                  "remove command success");
        assert.is(events, 3, "remove event");

        return helpers.setInput(options, "testadd");
      },
      check: {
        typed: "testadd",
        cursor: 7,
        current: "__command",
        status: "ERROR",
        unassigned: [ ],
      }
    }
  ]);
};

exports.testAddRemove2 = function (options) {
  var commands = options.requisition.system.commands;

  commands.add({
    name: "testadd",
    exec: function () {
      return 3;
    }
  });

  assert.is(commands.getAll().length,
            startCount + 1,
            "rereadd command success");
  assert.is(events, 4, "rereadd event");

  return helpers.audit(options, [
    {
      setup: "testadd",
      exec: {
        output: /^3$/
      },
      post: function () {
        commands.remove({
          name: "testadd"
        });

        assert.is(commands.getAll().length,
                  startCount,
                  "reremove command success");
        assert.is(events, 5, "reremove event");
      }
    },
    {
      setup: "testadd",
      check: {
        typed: "testadd",
        status: "ERROR"
      }
    }
  ]);
};

exports.testAddRemove3 = function (options) {
  var commands = options.requisition.system.commands;

  commands.remove({ name: "nonexistant" });
  assert.is(commands.getAll().length,
            startCount,
            "nonexistant1 command success");
  assert.is(events, 5, "nonexistant1 event");

  commands.remove("nonexistant");
  assert.is(commands.getAll().length,
            startCount,
            "nonexistant2 command success");
  assert.is(events, 5, "nonexistant2 event");

  commands.onCommandsChange.remove(commandsChange);
};

exports.testAltCommands = function (options) {
  var commands = options.requisition.system.commands;
  var altCommands = new Commands(options.requisition.system.types);

  var tss = {
    name: "tss",
    params: [
      { name: "str", type: "string" },
      { name: "num", type: "number" },
      { name: "opt", type: { name: "selection", data: [ "1", "2", "3" ] } },
    ],
    customProp1: "localValue",
    customProp2: true,
    customProp3: 42,
    exec: function (args, context) {
      return context.commandName + ":" +
              args.str + ":" + args.num + ":" + args.opt;
    }
  };
  altCommands.add(tss);

  var commandSpecs = altCommands.getCommandSpecs();
  assert.is(JSON.stringify(commandSpecs),
            '[{"item":"command","name":"tss","params":[' +
              '{"name":"str","type":"string"},' +
              '{"name":"num","type":"number"},' +
              '{"name":"opt","type":{"name":"selection","data":["1","2","3"]}}' +
            '],"isParent":false}]',
            "JSON.stringify(commandSpecs)");

  var customProps = [ "customProp1", "customProp2", "customProp3", ];
  var commandSpecs2 = altCommands.getCommandSpecs(customProps);
  assert.is(JSON.stringify(commandSpecs2),
            "[{" +
              '"item":"command",' +
              '"name":"tss",' +
              '"params":[' +
                '{"name":"str","type":"string"},' +
                '{"name":"num","type":"number"},' +
                '{"name":"opt","type":{"name":"selection","data":["1","2","3"]}}' +
              "]," +
              '"isParent":false,' +
              '"customProp1":"localValue",' +
              '"customProp2":true,' +
              '"customProp3":42' +
            "}]",
            "JSON.stringify(commandSpecs)");

  var remoter = function (args, context) {
    assert.is(context.commandName, "tss", "commandName is tss");

    var cmd = altCommands.get(context.commandName);
    return cmd.exec(args, context);
  };

  commands.addProxyCommands(commandSpecs, remoter, "proxy", "test");

  var parent = commands.get("proxy");
  assert.is(parent.name, "proxy", "Parent command called proxy");

  var child = commands.get("proxy tss");
  assert.is(child.name, "proxy tss", "child command called proxy tss");

  return helpers.audit(options, [
    {
      setup:    "proxy tss foo 6 3",
      check: {
        input:  "proxy tss foo 6 3",
        hints:                    "",
        markup: "VVVVVVVVVVVVVVVVV",
        cursor: 17,
        status: "VALID",
        args: {
          str: { value: "foo", status: "VALID" },
          num: { value: 6, status: "VALID" },
          opt: { value: "3", status: "VALID" }
        }
      },
      exec: {
        output: "tss:foo:6:3"
      },
      post: function () {
        commands.remove("proxy");
        commands.remove("proxy tss");

        assert.is(commands.get("proxy"), undefined, "remove proxy");
        assert.is(commands.get("proxy tss"), undefined, "remove proxy tss");
      }
    }
  ]);
};