Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / webapp / base / js / ipc_unittest.js
blobebddb37cd737fb6b96334dc576ea87718fc05e7e
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.
5 (function() {
7 'use strict';
9 /** @type {base.Ipc} */
10 var ipc_;
12 QUnit.module('base.Ipc', {
13 beforeEach: function() {
14 ipc_ = base.Ipc.getInstance();
16 afterEach: function() {
17 base.Ipc.deleteInstance();
18 ipc_ = null;
20 });
22 QUnit.test(
23 'register() should return false if the request type was already registered',
24 function(assert) {
25 var handler1 = function() {};
26 var handler2 = function() {};
27 assert.equal(true, ipc_.register('foo', handler1));
28 assert.equal(false, ipc_.register('foo', handler2));
29 });
31 QUnit.test(
32 'send() should invoke a registered handler with the correct arguments',
33 function(assert) {
34 var handler = sinon.spy();
35 var argArray = [1, 2, 3];
36 var argDict = {
37 key1: 'value1',
38 key2: false
41 ipc_.register('foo', handler);
42 return base.Ipc.invoke('foo', 1, false, 'string', argArray, argDict).then(
43 function() {
44 sinon.assert.calledWith(handler, 1, false, 'string', argArray, argDict);
45 });
46 });
48 QUnit.test(
49 'send() should not invoke a handler that is unregistered',
50 function(assert) {
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);
59 });
60 });
62 QUnit.test(
63 'send() should raise exceptions on unknown request types',
64 function(assert) {
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);
71 });
72 });
74 QUnit.test(
75 'send() should raise exceptions on request from another extension',
76 function(assert) {
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);
85 });
86 chrome.runtime.id = oldId;
87 return promise;
88 });
91 QUnit.test(
92 'send() should pass exceptions raised by the handler to the caller',
93 function(assert) {
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.');
105 QUnit.test(
106 'send() should pass the return value of the handler to the caller',
107 function(assert) {
108 var handlers = {
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'}; }
116 var testCases = [];
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'});
131 })();