1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/commandline/test/browser_gcli_canon.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,280 @@ 1.4 +/* 1.5 + * Copyright 2012, Mozilla Foundation and contributors 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 + 1.20 +'use strict'; 1.21 +// <INJECTED SOURCE:START> 1.22 + 1.23 +// THIS FILE IS GENERATED FROM SOURCE IN THE GCLI PROJECT 1.24 +// DO NOT EDIT IT DIRECTLY 1.25 + 1.26 +var exports = {}; 1.27 + 1.28 +var TEST_URI = "data:text/html;charset=utf-8,<p id='gcli-input'>gcli-testCanon.js</p>"; 1.29 + 1.30 +function test() { 1.31 + return Task.spawn(function() { 1.32 + let options = yield helpers.openTab(TEST_URI); 1.33 + yield helpers.openToolbar(options); 1.34 + gcli.addItems(mockCommands.items); 1.35 + 1.36 + yield helpers.runTests(options, exports); 1.37 + 1.38 + gcli.removeItems(mockCommands.items); 1.39 + yield helpers.closeToolbar(options); 1.40 + yield helpers.closeTab(options); 1.41 + }).then(finish, helpers.handleError); 1.42 +} 1.43 + 1.44 +// <INJECTED SOURCE:END> 1.45 + 1.46 +// var assert = require('../testharness/assert'); 1.47 +// var helpers = require('./helpers'); 1.48 +var Canon = require('gcli/commands/commands').Canon; 1.49 + 1.50 +var startCount; 1.51 +var events; 1.52 + 1.53 +var canonChange = function(ev) { 1.54 + events++; 1.55 +}; 1.56 + 1.57 +exports.setup = function(options) { 1.58 + startCount = options.requisition.canon.getCommands().length; 1.59 + events = 0; 1.60 +}; 1.61 + 1.62 +exports.shutdown = function(options) { 1.63 + startCount = undefined; 1.64 + events = undefined; 1.65 +}; 1.66 + 1.67 +exports.testAddRemove1 = function(options) { 1.68 + var canon = options.requisition.canon; 1.69 + 1.70 + return helpers.audit(options, [ 1.71 + { 1.72 + name: 'testadd add', 1.73 + setup: function() { 1.74 + canon.onCanonChange.add(canonChange); 1.75 + 1.76 + canon.addCommand({ 1.77 + name: 'testadd', 1.78 + exec: function() { 1.79 + return 1; 1.80 + } 1.81 + }); 1.82 + 1.83 + assert.is(canon.getCommands().length, 1.84 + startCount + 1, 1.85 + 'add command success'); 1.86 + assert.is(events, 1, 'add event'); 1.87 + 1.88 + return helpers.setInput(options, 'testadd'); 1.89 + }, 1.90 + check: { 1.91 + input: 'testadd', 1.92 + hints: '', 1.93 + markup: 'VVVVVVV', 1.94 + cursor: 7, 1.95 + current: '__command', 1.96 + status: 'VALID', 1.97 + predictions: [ ], 1.98 + unassigned: [ ], 1.99 + args: { } 1.100 + }, 1.101 + exec: { 1.102 + output: /^1$/ 1.103 + } 1.104 + }, 1.105 + { 1.106 + name: 'testadd alter', 1.107 + setup: function() { 1.108 + canon.addCommand({ 1.109 + name: 'testadd', 1.110 + exec: function() { 1.111 + return 2; 1.112 + } 1.113 + }); 1.114 + 1.115 + assert.is(canon.getCommands().length, 1.116 + startCount + 1, 1.117 + 'read command success'); 1.118 + assert.is(events, 2, 'read event'); 1.119 + 1.120 + return helpers.setInput(options, 'testadd'); 1.121 + }, 1.122 + check: { 1.123 + input: 'testadd', 1.124 + hints: '', 1.125 + markup: 'VVVVVVV', 1.126 + }, 1.127 + exec: { 1.128 + output: '2' 1.129 + } 1.130 + }, 1.131 + { 1.132 + name: 'testadd remove', 1.133 + setup: function() { 1.134 + canon.removeCommand('testadd'); 1.135 + 1.136 + assert.is(canon.getCommands().length, 1.137 + startCount, 1.138 + 'remove command success'); 1.139 + assert.is(events, 3, 'remove event'); 1.140 + 1.141 + return helpers.setInput(options, 'testadd'); 1.142 + }, 1.143 + check: { 1.144 + typed: 'testadd', 1.145 + cursor: 7, 1.146 + current: '__command', 1.147 + status: 'ERROR', 1.148 + unassigned: [ ], 1.149 + } 1.150 + } 1.151 + ]); 1.152 +}; 1.153 + 1.154 +exports.testAddRemove2 = function(options) { 1.155 + var canon = options.requisition.canon; 1.156 + 1.157 + canon.addCommand({ 1.158 + name: 'testadd', 1.159 + exec: function() { 1.160 + return 3; 1.161 + } 1.162 + }); 1.163 + 1.164 + assert.is(canon.getCommands().length, 1.165 + startCount + 1, 1.166 + 'rereadd command success'); 1.167 + assert.is(events, 4, 'rereadd event'); 1.168 + 1.169 + return helpers.audit(options, [ 1.170 + { 1.171 + setup: 'testadd', 1.172 + exec: { 1.173 + output: /^3$/ 1.174 + }, 1.175 + post: function() { 1.176 + canon.removeCommand({ 1.177 + name: 'testadd' 1.178 + }); 1.179 + 1.180 + assert.is(canon.getCommands().length, 1.181 + startCount, 1.182 + 'reremove command success'); 1.183 + assert.is(events, 5, 'reremove event'); 1.184 + } 1.185 + }, 1.186 + { 1.187 + setup: 'testadd', 1.188 + check: { 1.189 + typed: 'testadd', 1.190 + status: 'ERROR' 1.191 + } 1.192 + } 1.193 + ]); 1.194 +}; 1.195 + 1.196 +exports.testAddRemove3 = function(options) { 1.197 + var canon = options.requisition.canon; 1.198 + 1.199 + canon.removeCommand({ name: 'nonexistant' }); 1.200 + assert.is(canon.getCommands().length, 1.201 + startCount, 1.202 + 'nonexistant1 command success'); 1.203 + assert.is(events, 5, 'nonexistant1 event'); 1.204 + 1.205 + canon.removeCommand('nonexistant'); 1.206 + assert.is(canon.getCommands().length, 1.207 + startCount, 1.208 + 'nonexistant2 command success'); 1.209 + assert.is(events, 5, 'nonexistant2 event'); 1.210 + 1.211 + canon.onCanonChange.remove(canonChange); 1.212 +}; 1.213 + 1.214 +exports.testAltCanon = function(options) { 1.215 + var canon = options.requisition.canon; 1.216 + var altCanon = new Canon(); 1.217 + 1.218 + var tss = { 1.219 + name: 'tss', 1.220 + params: [ 1.221 + { name: 'str', type: 'string' }, 1.222 + { name: 'num', type: 'number' }, 1.223 + { name: 'opt', type: { name: 'selection', data: [ '1', '2', '3' ] } }, 1.224 + ], 1.225 + exec: function(args, context) { 1.226 + return context.commandName + ':' + 1.227 + args.str + ':' + args.num + ':' + args.opt; 1.228 + } 1.229 + }; 1.230 + altCanon.addCommand(tss); 1.231 + 1.232 + var commandSpecs = altCanon.getCommandSpecs(); 1.233 + assert.is(JSON.stringify(commandSpecs), 1.234 + '[{"item":"command","name":"tss","params":[' + 1.235 + '{"name":"str","type":"string"},' + 1.236 + '{"name":"num","type":"number"},' + 1.237 + '{"name":"opt","type":{"name":"selection","data":["1","2","3"]}}' + 1.238 + '],"isParent":false}]', 1.239 + 'JSON.stringify(commandSpecs)'); 1.240 + 1.241 + var remoter = function(args, context) { 1.242 + assert.is(context.commandName, 'tss', 'commandName is tss'); 1.243 + 1.244 + var cmd = altCanon.getCommand(context.commandName); 1.245 + return cmd.exec(args, context); 1.246 + }; 1.247 + 1.248 + canon.addProxyCommands(commandSpecs, remoter, 'proxy', 'test'); 1.249 + 1.250 + var parent = canon.getCommand('proxy'); 1.251 + assert.is(parent.name, 'proxy', 'Parent command called proxy'); 1.252 + 1.253 + var child = canon.getCommand('proxy tss'); 1.254 + assert.is(child.name, 'proxy tss', 'child command called proxy tss'); 1.255 + 1.256 + return helpers.audit(options, [ 1.257 + { 1.258 + setup: 'proxy tss foo 6 3', 1.259 + check: { 1.260 + input: 'proxy tss foo 6 3', 1.261 + hints: '', 1.262 + markup: 'VVVVVVVVVVVVVVVVV', 1.263 + cursor: 17, 1.264 + status: 'VALID', 1.265 + args: { 1.266 + str: { value: 'foo', status: 'VALID' }, 1.267 + num: { value: 6, status: 'VALID' }, 1.268 + opt: { value: '3', status: 'VALID' } 1.269 + } 1.270 + }, 1.271 + exec: { 1.272 + output: 'tss:foo:6:3' 1.273 + }, 1.274 + post: function() { 1.275 + canon.removeCommand('proxy'); 1.276 + canon.removeCommand('proxy tss'); 1.277 + 1.278 + assert.is(canon.getCommand('proxy'), undefined, 'remove proxy'); 1.279 + assert.is(canon.getCommand('proxy tss'), undefined, 'remove proxy tss'); 1.280 + } 1.281 + } 1.282 + ]); 1.283 +};