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.
23 chromeMocks.activate(['runtime']);
24 ipc_ = base.Ipc.getInstance();
26 teardown: function() {
27 base.Ipc.deleteInstance();
29 chromeMocks.restore();
34 'register() should return false if the request type was already registered',
36 var handler1 = function() {};
37 var handler2 = function() {};
38 QUnit.equal(true, ipc_.register('foo', handler1));
39 QUnit.equal(false, ipc_.register('foo', handler2));
43 'send() should invoke a registered handler with the correct arguments',
45 var handler = sinon.spy();
46 var argArray = [1, 2, 3];
52 ipc_.register('foo', handler);
53 base.Ipc.invoke('foo', 1, false, 'string', argArray, argDict).then(
55 sinon.assert.calledWith(handler, 1, false, 'string', argArray, argDict);
61 'send() should not invoke a handler that is unregistered',
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);
74 'send() should raise exceptions on unknown request types',
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);
85 'send() should raise exceptions on request from another extension',
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);
95 chrome.runtime.id = oldId;
100 'send() should pass exceptions raised by the handler to the caller',
102 var handler = function() {
103 throw new Error('Whatever can go wrong, will go wrong.');
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.');
113 'send() should pass the return value of the handler to the caller',
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'}; }
124 for (var ipcName in handlers) {
125 ipc_.register(ipcName, handlers[ipcName]);
126 testCases.push(base.Ipc.invoke(ipcName));
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'});