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.
9 /** @type {base.Ipc} */
12 QUnit
.module('base.Ipc', {
13 beforeEach: function() {
14 chromeMocks
.activate(['runtime']);
15 ipc_
= base
.Ipc
.getInstance();
17 afterEach: function() {
18 base
.Ipc
.deleteInstance();
20 chromeMocks
.restore();
25 'register() should return false if the request type was already registered',
27 var handler1 = function() {};
28 var handler2 = function() {};
29 assert
.equal(true, ipc_
.register('foo', handler1
));
30 assert
.equal(false, ipc_
.register('foo', handler2
));
34 'send() should invoke a registered handler with the correct arguments',
36 var handler
= sinon
.spy();
37 var argArray
= [1, 2, 3];
43 ipc_
.register('foo', handler
);
44 return base
.Ipc
.invoke('foo', 1, false, 'string', argArray
, argDict
).then(
46 sinon
.assert
.calledWith(handler
, 1, false, 'string', argArray
, argDict
);
51 'send() should not invoke a handler that is unregistered',
53 var handler
= sinon
.spy();
54 ipc_
.register('foo', handler
);
55 ipc_
.unregister('foo');
56 return base
.Ipc
.invoke('foo', 'hello', 'world').then(function() {
57 assert
.ok(false, 'Invoking an unregistered handler should fail.');
58 }).catch(function(error
) {
59 sinon
.assert
.notCalled(handler
);
60 assert
.equal(error
, base
.Ipc
.Error
.UNSUPPORTED_REQUEST_TYPE
);
65 'send() should raise exceptions on unknown request types',
67 var handler
= sinon
.spy();
68 ipc_
.register('foo', handler
);
69 return base
.Ipc
.invoke('bar', 'hello', 'world').then(function() {
70 assert
.ok(false, 'Invoking unknown request types should fail.');
71 }).catch(function(error
) {
72 assert
.equal(error
, base
.Ipc
.Error
.UNSUPPORTED_REQUEST_TYPE
);
77 'send() should raise exceptions on request from another extension',
79 var handler
= sinon
.spy();
80 var oldId
= chrome
.runtime
.id
;
81 ipc_
.register('foo', handler
);
82 chrome
.runtime
.id
= 'foreign-extension';
83 var promise
= base
.Ipc
.invoke('foo', 'hello', 'world').then(function() {
84 assert
.ok(false, 'Requests from another extension should fail.');
85 }).catch(function(error
) {
86 assert
.equal(error
, base
.Ipc
.Error
.INVALID_REQUEST_ORIGIN
);
88 chrome
.runtime
.id
= oldId
;
94 'send() should pass exceptions raised by the handler to the caller',
96 var handler = function() {
97 throw new Error('Whatever can go wrong, will go wrong.');
99 ipc_
.register('foo', handler
);
100 return base
.Ipc
.invoke('foo').then(function() {
101 assert
.ok(false, 'Exceptions expected.');
102 }).catch(function(error
) {
103 assert
.equal(error
, 'Whatever can go wrong, will go wrong.');
108 'send() should pass the return value of the handler to the caller',
111 'boolean': function() { return false; },
112 'number': function() { return 12; },
113 'string': function() { return 'string'; },
114 'array': function() { return [1, 2]; },
115 'dict': function() { return {key1
: 'value1', key2
: 'value2'}; }
119 for (var ipcName
in handlers
) {
120 ipc_
.register(ipcName
, handlers
[ipcName
]);
121 testCases
.push(base
.Ipc
.invoke(ipcName
));
124 return Promise
.all(testCases
).then(function(results
){
125 assert
.equal(results
[0], false);
126 assert
.equal(results
[1], 12);
127 assert
.equal(results
[2], 'string');
128 assert
.deepEqual(results
[3], [1,2]);
129 assert
.deepEqual(results
[4], {key1
: 'value1', key2
: 'value2'});