Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 (function() { | 5 (function() { |
| 6 | 6 |
| 7 'use strict'; | 7 'use strict'; |
| 8 | 8 |
| 9 /** @type {base.Ipc} */ | 9 /** @type {base.Ipc} */ |
| 10 var ipc_; | 10 var ipc_; |
| 11 | 11 |
| 12 function pass() { | 12 QUnit.module('base.Ipc', { |
| 13 ok(true); | 13 beforeEach: function() { |
| 14 QUnit.start(); | |
| 15 } | |
| 16 | |
| 17 function fail() { | |
| 18 ok(false); | |
| 19 QUnit.start(); | |
| 20 } | |
| 21 | |
| 22 module('base.Ipc', { | |
| 23 setup: function() { | |
| 24 chromeMocks.activate(['runtime']); | 14 chromeMocks.activate(['runtime']); |
| 25 ipc_ = base.Ipc.getInstance(); | 15 ipc_ = base.Ipc.getInstance(); |
| 26 }, | 16 }, |
| 27 teardown: function() { | 17 afterEach: function() { |
| 28 base.Ipc.deleteInstance(); | 18 base.Ipc.deleteInstance(); |
| 29 ipc_ = null; | 19 ipc_ = null; |
| 30 chromeMocks.restore(); | 20 chromeMocks.restore(); |
| 31 } | 21 } |
| 32 }); | 22 }); |
| 33 | 23 |
| 34 QUnit.test( | 24 QUnit.test( |
| 35 'register() should return false if the request type was already registered', | 25 'register() should return false if the request type was already registered', |
| 36 function() { | 26 function() { |
| 37 var handler1 = function() {}; | 27 var handler1 = function() {}; |
| 38 var handler2 = function() {}; | 28 var handler2 = function() {}; |
| 39 QUnit.equal(true, ipc_.register('foo', handler1)); | 29 QUnit.equal(true, ipc_.register('foo', handler1)); |
| 40 QUnit.equal(false, ipc_.register('foo', handler2)); | 30 QUnit.equal(false, ipc_.register('foo', handler2)); |
| 41 }); | 31 }); |
| 42 | 32 |
| 43 QUnit.asyncTest( | 33 QUnit.test( |
| 44 'send() should invoke a registered handler with the correct arguments', | 34 'send() should invoke a registered handler with the correct arguments', |
| 45 function() { | 35 function() { |
| 46 var handler = sinon.spy(); | 36 var handler = sinon.spy(); |
| 47 var argArray = [1, 2, 3]; | 37 var argArray = [1, 2, 3]; |
| 48 var argDict = { | 38 var argDict = { |
| 49 key1: 'value1', | 39 key1: 'value1', |
| 50 key2: false | 40 key2: false |
| 51 }; | 41 }; |
| 52 | 42 |
| 53 ipc_.register('foo', handler); | 43 ipc_.register('foo', handler); |
| 54 base.Ipc.invoke('foo', 1, false, 'string', argArray, argDict).then( | 44 return base.Ipc.invoke('foo', 1, false, 'string', argArray, argDict).then( |
| 55 function() { | 45 function() { |
| 56 sinon.assert.calledWith(handler, 1, false, 'string', argArray, argDict); | 46 sinon.assert.calledWith(handler, 1, false, 'string', argArray, argDict); |
| 57 pass(); | 47 }); |
| 58 }, fail); | |
| 59 }); | 48 }); |
| 60 | 49 |
| 61 QUnit.asyncTest( | 50 QUnit.test( |
| 62 'send() should not invoke a handler that is unregistered', | 51 'send() should not invoke a handler that is unregistered', |
| 63 function() { | 52 function() { |
| 64 var handler = sinon.spy(); | 53 var handler = sinon.spy(); |
| 65 ipc_.register('foo', handler); | 54 ipc_.register('foo', handler); |
| 66 ipc_.unregister('foo'); | 55 ipc_.unregister('foo'); |
| 67 base.Ipc.invoke('foo', 'hello', 'world').then(fail, function(error) { | 56 return base.Ipc.invoke('foo', 'hello', 'world').then(function() { |
| 57 QUnit.ok(false); | |
|
Jamie
2015/03/17 17:57:15
ok(false) is not very useful without an associated
kelvinp
2015/03/17 18:13:58
Done.
| |
| 58 }).catch(function(error) { | |
| 68 sinon.assert.notCalled(handler); | 59 sinon.assert.notCalled(handler); |
| 69 QUnit.equal(error, base.Ipc.Error.UNSUPPORTED_REQUEST_TYPE); | 60 QUnit.equal(error, base.Ipc.Error.UNSUPPORTED_REQUEST_TYPE); |
| 70 pass(); | |
| 71 }); | 61 }); |
| 72 }); | 62 }); |
| 73 | 63 |
| 74 QUnit.asyncTest( | 64 QUnit.test( |
| 75 'send() should raise exceptions on unknown request types', | 65 'send() should raise exceptions on unknown request types', |
| 76 function() { | 66 function() { |
| 77 var handler = sinon.spy(); | 67 var handler = sinon.spy(); |
| 78 ipc_.register('foo', handler); | 68 ipc_.register('foo', handler); |
| 79 base.Ipc.invoke('bar', 'hello', 'world').then(fail, function(error) { | 69 return base.Ipc.invoke('bar', 'hello', 'world').then(function() { |
| 70 QUnit.ok(false); | |
| 71 }).catch(function(error) { | |
| 80 QUnit.equal(error, base.Ipc.Error.UNSUPPORTED_REQUEST_TYPE); | 72 QUnit.equal(error, base.Ipc.Error.UNSUPPORTED_REQUEST_TYPE); |
| 81 pass(); | |
| 82 }); | 73 }); |
| 83 }); | 74 }); |
| 84 | 75 |
| 85 QUnit.asyncTest( | 76 QUnit.test( |
| 86 'send() should raise exceptions on request from another extension', | 77 'send() should raise exceptions on request from another extension', |
| 87 function() { | 78 function() { |
| 88 var handler = sinon.spy(); | 79 var handler = sinon.spy(); |
| 89 var oldId = chrome.runtime.id; | 80 var oldId = chrome.runtime.id; |
| 90 ipc_.register('foo', handler); | 81 ipc_.register('foo', handler); |
| 91 chrome.runtime.id = 'foreign-extension'; | 82 chrome.runtime.id = 'foreign-extension'; |
| 92 base.Ipc.invoke('foo', 'hello', 'world').then(fail, function(error) { | 83 var promise = base.Ipc.invoke('foo', 'hello', 'world').then(function() { |
| 84 QUnit.ok(false); | |
| 85 }).catch(function(error) { | |
| 93 QUnit.equal(error, base.Ipc.Error.INVALID_REQUEST_ORIGIN); | 86 QUnit.equal(error, base.Ipc.Error.INVALID_REQUEST_ORIGIN); |
| 94 pass(); | |
| 95 }); | 87 }); |
| 96 chrome.runtime.id = oldId; | 88 chrome.runtime.id = oldId; |
| 89 return promise; | |
| 97 }); | 90 }); |
| 98 | 91 |
| 99 | 92 |
| 100 QUnit.asyncTest( | 93 QUnit.test( |
| 101 'send() should pass exceptions raised by the handler to the caller', | 94 'send() should pass exceptions raised by the handler to the caller', |
| 102 function() { | 95 function() { |
| 103 var handler = function() { | 96 var handler = function() { |
| 104 throw new Error('Whatever can go wrong, will go wrong.'); | 97 throw new Error('Whatever can go wrong, will go wrong.'); |
| 105 }; | 98 }; |
| 106 ipc_.register('foo', handler); | 99 ipc_.register('foo', handler); |
| 107 base.Ipc.invoke('foo').then(fail, function(error) { | 100 return base.Ipc.invoke('foo').then(function() { |
| 101 QUnit.ok(false); | |
| 102 }).catch(function(error) { | |
| 108 QUnit.equal(error, 'Whatever can go wrong, will go wrong.'); | 103 QUnit.equal(error, 'Whatever can go wrong, will go wrong.'); |
| 109 pass(); | |
| 110 }); | 104 }); |
| 111 }); | 105 }); |
| 112 | 106 |
| 113 QUnit.asyncTest( | 107 QUnit.test( |
| 114 'send() should pass the return value of the handler to the caller', | 108 'send() should pass the return value of the handler to the caller', |
| 115 function() { | 109 function() { |
| 116 var handlers = { | 110 var handlers = { |
| 117 'boolean': function() { return false; }, | 111 'boolean': function() { return false; }, |
| 118 'number': function() { return 12; }, | 112 'number': function() { return 12; }, |
| 119 'string': function() { return 'string'; }, | 113 'string': function() { return 'string'; }, |
| 120 'array': function() { return [1, 2]; }, | 114 'array': function() { return [1, 2]; }, |
| 121 'dict': function() { return {key1: 'value1', key2: 'value2'}; } | 115 'dict': function() { return {key1: 'value1', key2: 'value2'}; } |
| 122 }; | 116 }; |
| 123 | 117 |
| 124 var testCases = []; | 118 var testCases = []; |
| 125 for (var ipcName in handlers) { | 119 for (var ipcName in handlers) { |
| 126 ipc_.register(ipcName, handlers[ipcName]); | 120 ipc_.register(ipcName, handlers[ipcName]); |
| 127 testCases.push(base.Ipc.invoke(ipcName)); | 121 testCases.push(base.Ipc.invoke(ipcName)); |
| 128 } | 122 } |
| 129 | 123 |
| 130 Promise.all(testCases).then(function(results){ | 124 return Promise.all(testCases).then(function(results){ |
| 131 QUnit.equal(results[0], false); | 125 QUnit.equal(results[0], false); |
| 132 QUnit.equal(results[1], 12); | 126 QUnit.equal(results[1], 12); |
| 133 QUnit.equal(results[2], 'string'); | 127 QUnit.equal(results[2], 'string'); |
| 134 QUnit.deepEqual(results[3], [1,2]); | 128 QUnit.deepEqual(results[3], [1,2]); |
| 135 QUnit.deepEqual(results[4], {key1: 'value1', key2: 'value2'}); | 129 QUnit.deepEqual(results[4], {key1: 'value1', key2: 'value2'}); |
| 136 pass(); | 130 }); |
| 137 }, fail); | |
| 138 }); | 131 }); |
| 139 | 132 |
| 140 })(); | 133 })(); |
| OLD | NEW |