browser/devtools/commandline/test/browser_gcli_canon.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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  */
    17 'use strict';
    18 // <INJECTED SOURCE:START>
    20 // THIS FILE IS GENERATED FROM SOURCE IN THE GCLI PROJECT
    21 // DO NOT EDIT IT DIRECTLY
    23 var exports = {};
    25 var TEST_URI = "data:text/html;charset=utf-8,<p id='gcli-input'>gcli-testCanon.js</p>";
    27 function test() {
    28   return Task.spawn(function() {
    29     let options = yield helpers.openTab(TEST_URI);
    30     yield helpers.openToolbar(options);
    31     gcli.addItems(mockCommands.items);
    33     yield helpers.runTests(options, exports);
    35     gcli.removeItems(mockCommands.items);
    36     yield helpers.closeToolbar(options);
    37     yield helpers.closeTab(options);
    38   }).then(finish, helpers.handleError);
    39 }
    41 // <INJECTED SOURCE:END>
    43 // var assert = require('../testharness/assert');
    44 // var helpers = require('./helpers');
    45 var Canon = require('gcli/commands/commands').Canon;
    47 var startCount;
    48 var events;
    50 var canonChange = function(ev) {
    51   events++;
    52 };
    54 exports.setup = function(options) {
    55   startCount = options.requisition.canon.getCommands().length;
    56   events = 0;
    57 };
    59 exports.shutdown = function(options) {
    60   startCount = undefined;
    61   events = undefined;
    62 };
    64 exports.testAddRemove1 = function(options) {
    65   var canon = options.requisition.canon;
    67   return helpers.audit(options, [
    68     {
    69       name: 'testadd add',
    70       setup: function() {
    71         canon.onCanonChange.add(canonChange);
    73         canon.addCommand({
    74           name: 'testadd',
    75           exec: function() {
    76             return 1;
    77           }
    78         });
    80         assert.is(canon.getCommands().length,
    81                   startCount + 1,
    82                   'add command success');
    83         assert.is(events, 1, 'add event');
    85         return helpers.setInput(options, 'testadd');
    86       },
    87       check: {
    88         input:  'testadd',
    89         hints:         '',
    90         markup: 'VVVVVVV',
    91         cursor: 7,
    92         current: '__command',
    93         status: 'VALID',
    94         predictions: [ ],
    95         unassigned: [ ],
    96         args: { }
    97       },
    98       exec: {
    99         output: /^1$/
   100       }
   101     },
   102     {
   103       name: 'testadd alter',
   104       setup: function() {
   105         canon.addCommand({
   106           name: 'testadd',
   107           exec: function() {
   108             return 2;
   109           }
   110         });
   112         assert.is(canon.getCommands().length,
   113                   startCount + 1,
   114                   'read command success');
   115         assert.is(events, 2, 'read event');
   117         return helpers.setInput(options, 'testadd');
   118       },
   119       check: {
   120         input:  'testadd',
   121         hints:         '',
   122         markup: 'VVVVVVV',
   123       },
   124       exec: {
   125         output: '2'
   126       }
   127     },
   128     {
   129       name: 'testadd remove',
   130       setup: function() {
   131         canon.removeCommand('testadd');
   133         assert.is(canon.getCommands().length,
   134                   startCount,
   135                   'remove command success');
   136         assert.is(events, 3, 'remove event');
   138         return helpers.setInput(options, 'testadd');
   139       },
   140       check: {
   141         typed: 'testadd',
   142         cursor: 7,
   143         current: '__command',
   144         status: 'ERROR',
   145         unassigned: [ ],
   146       }
   147     }
   148   ]);
   149 };
   151 exports.testAddRemove2 = function(options) {
   152   var canon = options.requisition.canon;
   154   canon.addCommand({
   155     name: 'testadd',
   156     exec: function() {
   157       return 3;
   158     }
   159   });
   161   assert.is(canon.getCommands().length,
   162             startCount + 1,
   163             'rereadd command success');
   164   assert.is(events, 4, 'rereadd event');
   166   return helpers.audit(options, [
   167     {
   168       setup: 'testadd',
   169       exec: {
   170         output: /^3$/
   171       },
   172       post: function() {
   173         canon.removeCommand({
   174           name: 'testadd'
   175         });
   177         assert.is(canon.getCommands().length,
   178                   startCount,
   179                   'reremove command success');
   180         assert.is(events, 5, 'reremove event');
   181       }
   182     },
   183     {
   184       setup: 'testadd',
   185       check: {
   186         typed: 'testadd',
   187         status: 'ERROR'
   188       }
   189     }
   190   ]);
   191 };
   193 exports.testAddRemove3 = function(options) {
   194   var canon = options.requisition.canon;
   196   canon.removeCommand({ name: 'nonexistant' });
   197   assert.is(canon.getCommands().length,
   198             startCount,
   199             'nonexistant1 command success');
   200   assert.is(events, 5, 'nonexistant1 event');
   202   canon.removeCommand('nonexistant');
   203   assert.is(canon.getCommands().length,
   204             startCount,
   205             'nonexistant2 command success');
   206   assert.is(events, 5, 'nonexistant2 event');
   208   canon.onCanonChange.remove(canonChange);
   209 };
   211 exports.testAltCanon = function(options) {
   212   var canon = options.requisition.canon;
   213   var altCanon = new Canon();
   215   var tss = {
   216     name: 'tss',
   217     params: [
   218       { name: 'str', type: 'string' },
   219       { name: 'num', type: 'number' },
   220       { name: 'opt', type: { name: 'selection', data: [ '1', '2', '3' ] } },
   221     ],
   222     exec: function(args, context) {
   223       return context.commandName + ':' +
   224               args.str + ':' + args.num + ':' + args.opt;
   225     }
   226   };
   227   altCanon.addCommand(tss);
   229   var commandSpecs = altCanon.getCommandSpecs();
   230   assert.is(JSON.stringify(commandSpecs),
   231             '[{"item":"command","name":"tss","params":[' +
   232               '{"name":"str","type":"string"},' +
   233               '{"name":"num","type":"number"},' +
   234               '{"name":"opt","type":{"name":"selection","data":["1","2","3"]}}' +
   235             '],"isParent":false}]',
   236             'JSON.stringify(commandSpecs)');
   238   var remoter = function(args, context) {
   239     assert.is(context.commandName, 'tss', 'commandName is tss');
   241     var cmd = altCanon.getCommand(context.commandName);
   242     return cmd.exec(args, context);
   243   };
   245   canon.addProxyCommands(commandSpecs, remoter, 'proxy', 'test');
   247   var parent = canon.getCommand('proxy');
   248   assert.is(parent.name, 'proxy', 'Parent command called proxy');
   250   var child = canon.getCommand('proxy tss');
   251   assert.is(child.name, 'proxy tss', 'child command called proxy tss');
   253   return helpers.audit(options, [
   254     {
   255       setup:    'proxy tss foo 6 3',
   256       check: {
   257         input:  'proxy tss foo 6 3',
   258         hints:                    '',
   259         markup: 'VVVVVVVVVVVVVVVVV',
   260         cursor: 17,
   261         status: 'VALID',
   262         args: {
   263           str: { value: 'foo', status: 'VALID' },
   264           num: { value: 6, status: 'VALID' },
   265           opt: { value: '3', status: 'VALID' }
   266         }
   267       },
   268       exec: {
   269         output: 'tss:foo:6:3'
   270       },
   271       post: function() {
   272         canon.removeCommand('proxy');
   273         canon.removeCommand('proxy tss');
   275         assert.is(canon.getCommand('proxy'), undefined, 'remove proxy');
   276         assert.is(canon.getCommand('proxy tss'), undefined, 'remove proxy tss');
   277       }
   278     }
   279   ]);
   280 };

mercurial