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