Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 (function() { | |
| 6 | |
| 7 'use strict'; | |
| 8 | |
| 9 var ipc_; | |
| 10 | |
| 11 function pass() { | |
| 12 ok(true); | |
| 13 QUnit.start(); | |
| 14 } | |
| 15 | |
| 16 function fail() { | |
| 17 ok(false); | |
| 18 QUnit.start(); | |
| 19 } | |
| 20 | |
| 21 module('base.IPC', { | |
| 22 setup: function() { | |
| 23 chromeMocks.activate(['runtime']); | |
| 24 ipc_ = base.IPC.getInstance(); | |
| 25 }, | |
| 26 teardown: function() { | |
| 27 base.IPC.deleteInstance(); | |
| 28 ipc_ = null; | |
| 29 chromeMocks.restore(); | |
| 30 } | |
| 31 }); | |
| 32 | |
| 33 QUnit.test( | |
| 34 'register() should return false if the request type was already registered', | |
| 35 function() { | |
| 36 var handler1 = function() {}; | |
| 37 var handler2 = function() {}; | |
| 38 QUnit.equal(true, ipc_.register('foo', handler1)); | |
| 39 QUnit.equal(false, ipc_.register('foo', handler2)); | |
| 40 }); | |
| 41 | |
| 42 QUnit.asyncTest( | |
| 43 'send() should invoke a registered handler with the correct arguments', | |
| 44 function() { | |
| 45 var handler = sinon.spy(); | |
| 46 var argArray = [1, 2, 3]; | |
| 47 var argJson = { | |
|
Jamie
2015/01/27 22:52:25
s/argJson/argDict/?
kelvinp
2015/01/28 01:31:51
Done.
| |
| 48 key1: 'value1', | |
| 49 key2: false | |
| 50 }; | |
| 51 | |
| 52 ipc_.register('foo', handler); | |
| 53 base.IPC.invoke('foo', 1, false, 'string', argArray, argJson).then( | |
| 54 function() { | |
| 55 sinon.assert.calledWith(handler, 1, false, 'string', argArray, argJson); | |
| 56 pass(); | |
| 57 }, fail); | |
| 58 }); | |
| 59 | |
| 60 QUnit.asyncTest( | |
| 61 'send() should not invoke a handler that is unregistered', | |
| 62 function() { | |
| 63 var handler = sinon.spy(); | |
| 64 ipc_.register('foo', handler); | |
| 65 ipc_.unregister('foo', handler); | |
| 66 base.IPC.invoke('foo', 'hello', 'world').then(fail, function(error) { | |
| 67 sinon.assert.notCalled(handler); | |
| 68 QUnit.equal(error, base.IPC.Error.UNSUPPORTED_REQUEST_TYPE); | |
| 69 pass(); | |
| 70 }); | |
| 71 }); | |
| 72 | |
| 73 QUnit.asyncTest( | |
| 74 'send() should raise exceptions on unknown request types', | |
| 75 function() { | |
| 76 var handler = sinon.spy(); | |
| 77 ipc_.register('foo', handler); | |
| 78 base.IPC.invoke('bar', 'hello', 'world').then(fail, function(error) { | |
| 79 QUnit.equal(error, base.IPC.Error.UNSUPPORTED_REQUEST_TYPE); | |
| 80 pass(); | |
| 81 }); | |
| 82 }); | |
| 83 | |
| 84 QUnit.asyncTest( | |
| 85 'send() should raise exceptions on request from another extension', | |
| 86 function() { | |
| 87 var handler = sinon.spy(); | |
| 88 chrome.runtime.id = 'our-extension'; | |
| 89 ipc_.register('foo', handler); | |
| 90 base.IPC.invoke('foo', 'hello', 'world').then(fail, function(error) { | |
| 91 QUnit.equal(error, base.IPC.Error.INVALID_REQUEST_ORIGIN); | |
| 92 pass(); | |
| 93 }); | |
| 94 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.
| |
| 95 }); | |
| 96 | |
| 97 | |
| 98 QUnit.asyncTest( | |
| 99 'send() should pass exceptions raised by the handler to the caller', | |
| 100 function() { | |
| 101 var handler = function() { | |
| 102 throw new Error('Whatever can go wrong, will go wrong.'); | |
| 103 }; | |
| 104 ipc_.register('foo', handler); | |
| 105 base.IPC.invoke('foo').then(fail, function(error) { | |
| 106 QUnit.equal(error, 'Whatever can go wrong, will go wrong.'); | |
| 107 pass(); | |
| 108 }); | |
| 109 }); | |
| 110 | |
| 111 QUnit.asyncTest( | |
| 112 'send() should pass the return value of the handler to the caller', | |
| 113 function() { | |
| 114 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.
| |
| 115 var handler = function() { | |
| 116 return returnValue; | |
| 117 }; | |
| 118 ipc_.register('foo', handler); | |
| 119 base.IPC.invoke('foo').then(function(response) { | |
| 120 QUnit.equal(response, returnValue); | |
| 121 pass(); | |
| 122 }, fail); | |
| 123 }); | |
| 124 | |
| 125 })(); | |
| OLD | NEW |