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 ipc_ = base.Ipc.getInstance();
16 afterEach: function() {
17 base.Ipc.deleteInstance();
23 'register() should return false if the request type was already registered',
25 var handler1 = function() {};
26 var handler2 = function() {};
27 assert.equal(true, ipc_.register('foo', handler1));
28 assert.equal(false, ipc_.register('foo', handler2));
32 'send() should invoke a registered handler with the correct arguments',
34 var handler = sinon.spy();
35 var argArray = [1, 2, 3];
41 ipc_.register('foo', handler);
42 return base.Ipc.invoke('foo', 1, false, 'string', argArray, argDict).then(
44 sinon.assert.calledWith(handler, 1, false, 'string', argArray, argDict);
49 'send() should not invoke a handler that is unregistered',
51 var handler = sinon.spy();
52 ipc_.register('foo', handler);
53 ipc_.unregister('foo');
54 return base.Ipc.invoke('foo', 'hello', 'world').then(function() {
55 assert.ok(false, 'Invoking an unregistered handler should fail.');
56 }).catch(function(error) {
57 sinon.assert.notCalled(handler);
58 assert.equal(error, base.Ipc.Error.UNSUPPORTED_REQUEST_TYPE);
63 'send() should raise exceptions on unknown request types',
65 var handler = sinon.spy();
66 ipc_.register('foo', handler);
67 return base.Ipc.invoke('bar', 'hello', 'world').then(function() {
68 assert.ok(false, 'Invoking unknown request types should fail.');
69 }).catch(function(error) {
70 assert.equal(error, base.Ipc.Error.UNSUPPORTED_REQUEST_TYPE);
75 'send() should raise exceptions on request from another extension',
77 var handler = sinon.spy();
78 var oldId = chrome.runtime.id;
79 ipc_.register('foo', handler);
80 chrome.runtime.id = 'foreign-extension';
81 var promise = base.Ipc.invoke('foo', 'hello', 'world').then(function() {
82 assert.ok(false, 'Requests from another extension should fail.');
83 }).catch(function(error) {
84 assert.equal(error, base.Ipc.Error.INVALID_REQUEST_ORIGIN);
86 chrome.runtime.id = oldId;
92 'send() should pass exceptions raised by the handler to the caller',
94 var handler = function() {
95 throw new Error('Whatever can go wrong, will go wrong.');
97 ipc_.register('foo', handler);
98 return base.Ipc.invoke('foo').then(function() {
99 assert.ok(false, 'Exceptions expected.');
100 }).catch(function(error) {
101 assert.equal(error, 'Whatever can go wrong, will go wrong.');
106 'send() should pass the return value of the handler to the caller',
109 'boolean': function() { return false; },
110 'number': function() { return 12; },
111 'string': function() { return 'string'; },
112 'array': function() { return [1, 2]; },
113 'dict': function() { return {key1: 'value1', key2: 'value2'}; }
117 for (var ipcName in handlers) {
118 ipc_.register(ipcName, handlers[ipcName]);
119 testCases.push(base.Ipc.invoke(ipcName));
122 return Promise.all(testCases).then(function(results){
123 assert.equal(results[0], false);
124 assert.equal(results[1], 12);
125 assert.equal(results[2], 'string');
126 assert.deepEqual(results[3], [1,2]);
127 assert.deepEqual(results[4], {key1: 'value1', key2: 'value2'});