aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/messaging/multichannel_test.html
blob: 448c584aceb8f8703edcae7674cfe8893614c904 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html>
<!--
Copyright 2010 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<title>
  Closure Unit Tests - goog.messaging.MultiChannel
</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.testing.MockControl');
  goog.require('goog.testing.messaging.MockMessageChannel');
  goog.require('goog.testing.mockmatchers.IgnoreArgument');
  goog.require('goog.testing.jsunit');
  goog.require('goog.messaging.MultiChannel');
</script>
</head>
<body>
<script>

var mockControl;
var mockChannel;
var multiChannel;
var channel1;
var channel2;

function setUp() {
  mockControl = new goog.testing.MockControl();
  mockChannel = new goog.testing.messaging.MockMessageChannel(mockControl);
  multiChannel = new goog.messaging.MultiChannel(mockChannel);
  channel0 = multiChannel.createVirtualChannel('foo');
  channel1 = multiChannel.createVirtualChannel('bar');
}

function expectedFn(name, callback) {
  var ignored = new goog.testing.mockmatchers.IgnoreArgument();
  var fn = mockControl.createFunctionMock(name);
  fn(ignored).$does(function(args) {
    callback.apply(this, args);
  });
  return function() { fn(arguments); };
}

function notExpectedFn() {
  return mockControl.createFunctionMock('notExpectedFn');
}

function assertEqualsFn() {
  var expectedArgs = Array.prototype.slice.call(arguments);
  return expectedFn('assertEqualsFn', function() {
    assertObjectEquals(expectedArgs, Array.prototype.slice.call(arguments));
  });
}

function tearDown() {
  multiChannel.dispose();
  mockControl.$verifyAll();
  assertTrue(mockChannel.disposed);
}

function testSend0() {
  mockChannel.send('foo:fooBar', {foo: 'bar'});
  mockControl.$replayAll();
  channel0.send('fooBar', {foo: 'bar'});
}

function testSend1() {
  mockChannel.send('bar:fooBar', {foo: 'bar'});
  mockControl.$replayAll();
  channel1.send('fooBar', {foo: 'bar'});
}

function testReceive0() {
  channel0.registerService('fooBar', assertEqualsFn('Baz bang'));
  channel1.registerService('fooBar', notExpectedFn());
  mockControl.$replayAll();
  mockChannel.receive('foo:fooBar', 'Baz bang');
}

function testReceive1() {
  channel1.registerService('fooBar', assertEqualsFn('Baz bang'));
  channel0.registerService('fooBar', notExpectedFn());
  mockControl.$replayAll();
  mockChannel.receive('bar:fooBar', 'Baz bang');
}

function testDefaultReceive0() {
  channel0.registerDefaultService(assertEqualsFn('fooBar', 'Baz bang'));
  channel1.registerDefaultService(notExpectedFn());
  mockControl.$replayAll();
  mockChannel.receive('foo:fooBar', 'Baz bang');
}

function testDefaultReceive1() {
  channel1.registerDefaultService(assertEqualsFn('fooBar', 'Baz bang'));
  channel0.registerDefaultService(notExpectedFn());
  mockControl.$replayAll();
  mockChannel.receive('bar:fooBar', 'Baz bang');
}

function testReceiveAfterDisposed() {
  channel0.registerService('fooBar', notExpectedFn());
  mockControl.$replayAll();
  channel0.dispose();
  mockChannel.receive('foo:fooBar', 'Baz bang');
}

function testReceiveAfterParentDisposed() {
  channel0.registerService('fooBar', notExpectedFn());
  mockControl.$replayAll();
  multiChannel.dispose();
  mockChannel.receive('foo:fooBar', 'Baz bang');
}
</script>
</body>
</html>