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