Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 /** | |
| 6 * @fileoverview | |
| 7 */ | |
| 8 | |
| 9 (function() { | |
| 10 | |
| 11 'use strict'; | |
| 12 | |
| 13 /** @type {remoting.ProtocolExtensionManager} */ | |
| 14 var extensionManager; | |
| 15 | |
| 16 /** @type {(sinon.Spy|function(string, string))} */ | |
| 17 var sendClientMessage; | |
| 18 | |
| 19 /** | |
| 20 * @constructor | |
| 21 * @param {Array<string>} types | |
| 22 * @implements {remoting.ProtocolExtension} | |
| 23 */ | |
| 24 var DummyExtension = function(types) { | |
| 25 /** @private {?function(string, string)} */ | |
| 26 this.sendMessageToHost_ = null; | |
| 27 /** @private */ | |
| 28 this.types_ = types; | |
| 29 }; | |
| 30 | |
| 31 DummyExtension.prototype.getExtensionTypes = function() { | |
| 32 return this.types_.slice(0); | |
| 33 } | |
| 34 | |
| 35 /** | |
| 36 * @param {function(string,string)} sendMessageToHost Callback to send a message | |
| 37 * to the host. | |
| 38 */ | |
| 39 DummyExtension.prototype.startExtension = function(sendMessageToHost) { | |
| 40 this.sendMessageToHost_ = sendMessageToHost; | |
| 41 }; | |
| 42 | |
| 43 /** | |
| 44 * Called when an extension message of a matching type is received. | |
| 45 * | |
| 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. | |
| 49 */ | |
| 50 DummyExtension.prototype.onExtensionMessage = function(type, message){ | |
| 51 return this.types_.indexOf(type) !== 1; | |
| 52 }; | |
| 53 | |
| 54 | |
| 55 QUnit.module('ProtocolExtensionManager', { | |
| 56 beforeEach: function() { | |
| 57 sendClientMessage = /** @type {function(string, string)} */ (sinon.spy()); | |
| 58 extensionManager = new remoting.ProtocolExtensionManager(sendClientMessage); | |
| 59 }, | |
| 60 afterEach: function() { | |
| 61 } | |
| 62 }); | |
| 63 | |
| 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 extensionManager.register(extension); | |
| 69 extensionManager.start(); | |
| 70 | |
| 71 extensionManager.onProtocolExtensionMessage('type1', '{"message": "hello"}'); | |
| 72 assert.ok(onExtensionMessage.called); | |
| 73 onExtensionMessage.reset(); | |
| 74 | |
| 75 extensionManager.onProtocolExtensionMessage('type2', '{"message": "hello"}'); | |
| 76 assert.ok(onExtensionMessage.called); | |
| 77 onExtensionMessage.reset(); | |
| 78 | |
| 79 extensionManager.onProtocolExtensionMessage('type3', '{"message": "hello"}'); | |
| 80 assert.ok(!onExtensionMessage.called); | |
| 81 onExtensionMessage.reset(); | |
| 82 }); | |
| 83 | |
| 84 | |
| 85 QUnit.test('should not register extensions of the same type', function(assert) { | |
|
garykac
2015/04/07 22:14:16
I assume that there's no way to detect that an err
kelvinp
2015/04/08 00:26:02
I modified register to return true when an extensi
| |
| 86 var extension1 = new DummyExtension(['type1']); | |
| 87 var extension2 = new DummyExtension(['type1']); | |
| 88 | |
| 89 var onExtensionMessage1 = /** @type {(sinon.Spy|function(string, string))} */( | |
| 90 sinon.spy(extension1, 'onExtensionMessage')); | |
| 91 var onExtensionMessage2 = /** @type {(sinon.Spy|function(string, string))} */( | |
| 92 sinon.spy(extension2, 'onExtensionMessage')); | |
| 93 | |
| 94 extensionManager.register(extension1); | |
| 95 extensionManager.register(extension2); | |
| 96 extensionManager.start(); | |
| 97 | |
| 98 extensionManager.onProtocolExtensionMessage('type1', '{"message": "hello"}'); | |
| 99 assert.ok(onExtensionMessage1.called); | |
| 100 assert.ok(!onExtensionMessage2.called); | |
| 101 }); | |
| 102 | |
| 103 QUnit.test('should handle extensions registration after it is started', | |
| 104 function(assert) { | |
| 105 var extension = new DummyExtension(['type']); | |
| 106 | |
| 107 var onExtensionMessage = /** @type {(sinon.Spy|function(string, string))} */( | |
| 108 sinon.spy(extension, 'onExtensionMessage')); | |
| 109 | |
| 110 extensionManager.start(); | |
| 111 extensionManager.register(extension); | |
| 112 | |
| 113 extensionManager.onProtocolExtensionMessage('type', '{"message": "hello"}'); | |
| 114 assert.ok(onExtensionMessage.called); | |
| 115 }); | |
| 116 | |
| 117 })(); | |
| OLD | NEW |