| 1 |
// Copyright 2010 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.messaging.MultiChannelTest'); |
| 16 |
goog.setTestOnly('goog.messaging.MultiChannelTest'); |
| 17 |
|
| 18 |
goog.require('goog.messaging.MultiChannel'); |
| 19 |
goog.require('goog.testing.MockControl'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
goog.require('goog.testing.messaging.MockMessageChannel'); |
| 22 |
goog.require('goog.testing.mockmatchers.IgnoreArgument'); |
| 23 |
|
| 24 |
var mockControl; |
| 25 |
var mockChannel; |
| 26 |
var multiChannel; |
| 27 |
var channel1; |
| 28 |
var channel2; |
| 29 |
|
| 30 |
function setUp() { |
| 31 |
mockControl = new goog.testing.MockControl(); |
| 32 |
mockChannel = new goog.testing.messaging.MockMessageChannel(mockControl); |
| 33 |
multiChannel = new goog.messaging.MultiChannel(mockChannel); |
| 34 |
channel0 = multiChannel.createVirtualChannel('foo'); |
| 35 |
channel1 = multiChannel.createVirtualChannel('bar'); |
| 36 |
} |
| 37 |
|
| 38 |
function expectedFn(name, callback) { |
| 39 |
var ignored = new goog.testing.mockmatchers.IgnoreArgument(); |
| 40 |
var fn = mockControl.createFunctionMock(name); |
| 41 |
fn(ignored).$does(function(args) { callback.apply(this, args); }); |
| 42 |
return function() { fn(arguments); }; |
| 43 |
} |
| 44 |
|
| 45 |
function notExpectedFn() { |
| 46 |
return mockControl.createFunctionMock('notExpectedFn'); |
| 47 |
} |
| 48 |
|
| 49 |
function assertEqualsFn() { |
| 50 |
var expectedArgs = Array.prototype.slice.call(arguments); |
| 51 |
return expectedFn('assertEqualsFn', function() { |
| 52 |
assertObjectEquals(expectedArgs, Array.prototype.slice.call(arguments)); |
| 53 |
}); |
| 54 |
} |
| 55 |
|
| 56 |
function tearDown() { |
| 57 |
multiChannel.dispose(); |
| 58 |
mockControl.$verifyAll(); |
| 59 |
assertTrue(mockChannel.disposed); |
| 60 |
} |
| 61 |
|
| 62 |
function testSend0() { |
| 63 |
mockChannel.send('foo:fooBar', {foo: 'bar'}); |
| 64 |
mockControl.$replayAll(); |
| 65 |
channel0.send('fooBar', {foo: 'bar'}); |
| 66 |
} |
| 67 |
|
| 68 |
function testSend1() { |
| 69 |
mockChannel.send('bar:fooBar', {foo: 'bar'}); |
| 70 |
mockControl.$replayAll(); |
| 71 |
channel1.send('fooBar', {foo: 'bar'}); |
| 72 |
} |
| 73 |
|
| 74 |
function testReceive0() { |
| 75 |
channel0.registerService('fooBar', assertEqualsFn('Baz bang')); |
| 76 |
channel1.registerService('fooBar', notExpectedFn()); |
| 77 |
mockControl.$replayAll(); |
| 78 |
mockChannel.receive('foo:fooBar', 'Baz bang'); |
| 79 |
} |
| 80 |
|
| 81 |
function testReceive1() { |
| 82 |
channel1.registerService('fooBar', assertEqualsFn('Baz bang')); |
| 83 |
channel0.registerService('fooBar', notExpectedFn()); |
| 84 |
mockControl.$replayAll(); |
| 85 |
mockChannel.receive('bar:fooBar', 'Baz bang'); |
| 86 |
} |
| 87 |
|
| 88 |
function testDefaultReceive0() { |
| 89 |
channel0.registerDefaultService(assertEqualsFn('fooBar', 'Baz bang')); |
| 90 |
channel1.registerDefaultService(notExpectedFn()); |
| 91 |
mockControl.$replayAll(); |
| 92 |
mockChannel.receive('foo:fooBar', 'Baz bang'); |
| 93 |
} |
| 94 |
|
| 95 |
function testDefaultReceive1() { |
| 96 |
channel1.registerDefaultService(assertEqualsFn('fooBar', 'Baz bang')); |
| 97 |
channel0.registerDefaultService(notExpectedFn()); |
| 98 |
mockControl.$replayAll(); |
| 99 |
mockChannel.receive('bar:fooBar', 'Baz bang'); |
| 100 |
} |
| 101 |
|
| 102 |
function testReceiveAfterDisposed() { |
| 103 |
channel0.registerService('fooBar', notExpectedFn()); |
| 104 |
mockControl.$replayAll(); |
| 105 |
channel0.dispose(); |
| 106 |
mockChannel.receive('foo:fooBar', 'Baz bang'); |
| 107 |
} |
| 108 |
|
| 109 |
function testReceiveAfterParentDisposed() { |
| 110 |
channel0.registerService('fooBar', notExpectedFn()); |
| 111 |
mockControl.$replayAll(); |
| 112 |
multiChannel.dispose(); |
| 113 |
mockChannel.receive('foo:fooBar', 'Baz bang'); |
| 114 |
} |