1 // Copyright 2015 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.
13 /** @type {remoting.ProtocolExtensionManager} */
16 /** @type {(sinon.Spy|function(string, string))} */
17 var sendClientMessage;
21 * @param {Array<string>} types
22 * @implements {remoting.ProtocolExtension}
24 var DummyExtension = function(types) {
25 /** @private {?function(string, string)} */
26 this.sendMessageToHost_ = null;
31 DummyExtension.prototype.getExtensionTypes = function() {
32 return this.types_.slice(0);
36 * @param {function(string,string)} sendMessageToHost Callback to send a message
39 DummyExtension.prototype.startExtension = function(sendMessageToHost) {
40 this.sendMessageToHost_ = sendMessageToHost;
44 * Called when an extension message of a matching type is received.
46 * @param {string} type The message type.
47 * @param {Object} message The parsed extension message data.
48 * @return {boolean} True if the extension message was handled.
50 DummyExtension.prototype.onExtensionMessage = function(type, message){
51 return this.types_.indexOf(type) !== 1;
55 QUnit.module('ProtocolExtensionManager', {
56 beforeEach: function() {
57 sendClientMessage = /** @type {function(string, string)} */ (sinon.spy());
58 extensionManager = new remoting.ProtocolExtensionManager(sendClientMessage);
60 afterEach: function() {
64 QUnit.test('should route message to extension by type', function(assert) {
65 var extension = new DummyExtension(['type1', 'type2']);
66 var onExtensionMessage = /** @type {(sinon.Spy|function(string, string))} */ (
67 sinon.spy(extension, 'onExtensionMessage'));
68 assert.ok(extensionManager.register(extension));
69 extensionManager.start();
71 extensionManager.onProtocolExtensionMessage('type1', '{"message": "hello"}');
72 assert.ok(onExtensionMessage.called);
73 onExtensionMessage.reset();
75 extensionManager.onProtocolExtensionMessage('type2', '{"message": "hello"}');
76 assert.ok(onExtensionMessage.called);
77 onExtensionMessage.reset();
79 extensionManager.onProtocolExtensionMessage('type3', '{"message": "hello"}');
80 assert.ok(!onExtensionMessage.called);
81 onExtensionMessage.reset();
84 QUnit.test('startExtension() should only be called once', function(assert) {
85 var extension = new DummyExtension(['type1', 'type2']);
86 var startExtension = /** @type {(sinon.Spy|Function)} */ (
87 sinon.spy(extension, 'startExtension'));
89 assert.ok(extensionManager.register(extension));
90 extensionManager.start();
91 assert.ok(startExtension.calledOnce);
95 QUnit.test('should not register extensions of the same type', function(assert) {
96 var extension1 = new DummyExtension(['type1']);
97 var extension2 = new DummyExtension(['type1']);
99 var onExtensionMessage1 = /** @type {(sinon.Spy|function(string, string))} */(
100 sinon.spy(extension1, 'onExtensionMessage'));
101 var onExtensionMessage2 = /** @type {(sinon.Spy|function(string, string))} */(
102 sinon.spy(extension2, 'onExtensionMessage'));
104 assert.ok(extensionManager.register(extension1));
105 assert.ok(!extensionManager.register(extension2));
106 extensionManager.start();
108 extensionManager.onProtocolExtensionMessage('type1', '{"message": "hello"}');
109 assert.ok(onExtensionMessage1.called);
110 assert.ok(!onExtensionMessage2.called);
113 QUnit.test('should handle extensions registration after it is started',
115 var extension = new DummyExtension(['type']);
117 var onExtensionMessage = /** @type {(sinon.Spy|function(string, string))} */(
118 sinon.spy(extension, 'onExtensionMessage'));
120 extensionManager.start();
121 assert.ok(extensionManager.register(extension));
123 extensionManager.onProtocolExtensionMessage('type', '{"message": "hello"}');
124 assert.ok(onExtensionMessage.called);