Index: remoting/webapp/unittests/ipc_unittest.js |
diff --git a/remoting/webapp/unittests/ipc_unittest.js b/remoting/webapp/unittests/ipc_unittest.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..cb0d966e5f875a79c14cf9a37d0e295307ce552c |
--- /dev/null |
+++ b/remoting/webapp/unittests/ipc_unittest.js |
@@ -0,0 +1,125 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+(function() { |
+ |
+'use strict'; |
+ |
+var ipc_; |
+ |
+function pass() { |
+ ok(true); |
+ QUnit.start(); |
+} |
+ |
+function fail() { |
+ ok(false); |
+ QUnit.start(); |
+} |
+ |
+module('base.IPC', { |
+ setup: function() { |
+ chromeMocks.activate(['runtime']); |
+ ipc_ = base.IPC.getInstance(); |
+ }, |
+ teardown: function() { |
+ base.IPC.deleteInstance(); |
+ ipc_ = null; |
+ chromeMocks.restore(); |
+ } |
+}); |
+ |
+QUnit.test( |
+ 'register() should return false if the request type was already registered', |
+ function() { |
+ var handler1 = function() {}; |
+ var handler2 = function() {}; |
+ QUnit.equal(true, ipc_.register('foo', handler1)); |
+ QUnit.equal(false, ipc_.register('foo', handler2)); |
+}); |
+ |
+QUnit.asyncTest( |
+ 'send() should invoke a registered handler with the correct arguments', |
+ function() { |
+ var handler = sinon.spy(); |
+ var argArray = [1, 2, 3]; |
+ var argJson = { |
Jamie
2015/01/27 22:52:25
s/argJson/argDict/?
kelvinp
2015/01/28 01:31:51
Done.
|
+ key1: 'value1', |
+ key2: false |
+ }; |
+ |
+ ipc_.register('foo', handler); |
+ base.IPC.invoke('foo', 1, false, 'string', argArray, argJson).then( |
+ function() { |
+ sinon.assert.calledWith(handler, 1, false, 'string', argArray, argJson); |
+ pass(); |
+ }, fail); |
+}); |
+ |
+QUnit.asyncTest( |
+ 'send() should not invoke a handler that is unregistered', |
+ function() { |
+ var handler = sinon.spy(); |
+ ipc_.register('foo', handler); |
+ ipc_.unregister('foo', handler); |
+ base.IPC.invoke('foo', 'hello', 'world').then(fail, function(error) { |
+ sinon.assert.notCalled(handler); |
+ QUnit.equal(error, base.IPC.Error.UNSUPPORTED_REQUEST_TYPE); |
+ pass(); |
+ }); |
+}); |
+ |
+QUnit.asyncTest( |
+ 'send() should raise exceptions on unknown request types', |
+ function() { |
+ var handler = sinon.spy(); |
+ ipc_.register('foo', handler); |
+ base.IPC.invoke('bar', 'hello', 'world').then(fail, function(error) { |
+ QUnit.equal(error, base.IPC.Error.UNSUPPORTED_REQUEST_TYPE); |
+ pass(); |
+ }); |
+}); |
+ |
+QUnit.asyncTest( |
+ 'send() should raise exceptions on request from another extension', |
+ function() { |
+ var handler = sinon.spy(); |
+ chrome.runtime.id = 'our-extension'; |
+ ipc_.register('foo', handler); |
+ base.IPC.invoke('foo', 'hello', 'world').then(fail, function(error) { |
+ QUnit.equal(error, base.IPC.Error.INVALID_REQUEST_ORIGIN); |
+ pass(); |
+ }); |
+ chrome.runtime.id = 'some-other-extension'; |
Jamie
2015/01/27 22:52:25
I don't understand how this manipulation of chrome
kelvinp
2015/01/28 01:31:51
Done.
|
+}); |
+ |
+ |
+QUnit.asyncTest( |
+ 'send() should pass exceptions raised by the handler to the caller', |
+ function() { |
+ var handler = function() { |
+ throw new Error('Whatever can go wrong, will go wrong.'); |
+ }; |
+ ipc_.register('foo', handler); |
+ base.IPC.invoke('foo').then(fail, function(error) { |
+ QUnit.equal(error, 'Whatever can go wrong, will go wrong.'); |
+ pass(); |
+ }); |
+}); |
+ |
+QUnit.asyncTest( |
+ 'send() should pass the return value of the handler to the caller', |
+ function() { |
+ var returnValue = {}; |
Jamie
2015/01/27 22:52:25
You should probably include some values of various
kelvinp
2015/01/28 01:31:51
Done.
|
+ var handler = function() { |
+ return returnValue; |
+ }; |
+ ipc_.register('foo', handler); |
+ base.IPC.invoke('foo').then(function(response) { |
+ QUnit.equal(response, returnValue); |
+ pass(); |
+ }, fail); |
+}); |
+ |
+})(); |