Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / remoting / webapp / base / js / ipc_unittest.js
blob1634d8638fdc6eed3468f8949cc939835eaaefce
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 chromeMocks.activate(['runtime']);
15 ipc_ = base.Ipc.getInstance();
17 afterEach: function() {
18 base.Ipc.deleteInstance();
19 ipc_ = null;
20 chromeMocks.restore();
22 });
24 QUnit.test(
25 'register() should return false if the request type was already registered',
26 function(assert) {
27 var handler1 = function() {};
28 var handler2 = function() {};
29 assert.equal(true, ipc_.register('foo', handler1));
30 assert.equal(false, ipc_.register('foo', handler2));
31 });
33 QUnit.test(
34 'send() should invoke a registered handler with the correct arguments',
35 function(assert) {
36 var handler = sinon.spy();
37 var argArray = [1, 2, 3];
38 var argDict = {
39 key1: 'value1',
40 key2: false
43 ipc_.register('foo', handler);
44 return base.Ipc.invoke('foo', 1, false, 'string', argArray, argDict).then(
45 function() {
46 sinon.assert.calledWith(handler, 1, false, 'string', argArray, argDict);
47 });
48 });
50 QUnit.test(
51 'send() should not invoke a handler that is unregistered',
52 function(assert) {
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);
61 });
62 });
64 QUnit.test(
65 'send() should raise exceptions on unknown request types',
66 function(assert) {
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);
73 });
74 });
76 QUnit.test(
77 'send() should raise exceptions on request from another extension',
78 function(assert) {
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);
87 });
88 chrome.runtime.id = oldId;
89 return promise;
90 });
93 QUnit.test(
94 'send() should pass exceptions raised by the handler to the caller',
95 function(assert) {
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.');
107 QUnit.test(
108 'send() should pass the return value of the handler to the caller',
109 function(assert) {
110 var handlers = {
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'}; }
118 var testCases = [];
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'});
133 })();