Debian Codegraph @

closure-library/closure/goog/messaging/portoperator_test.js

raw
1 // Copyright 2011 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.PortOperatorTest');
16 goog.setTestOnly('goog.messaging.PortOperatorTest');
17
18 goog.require('goog.messaging.PortNetwork');
19 goog.require('goog.messaging.PortOperator');
20 goog.require('goog.testing.MockControl');
21 goog.require('goog.testing.PropertyReplacer');
22 goog.require('goog.testing.jsunit');
23 goog.require('goog.testing.messaging.MockMessageChannel');
24 goog.require('goog.testing.messaging.MockMessagePort');
25
26 var stubs;
27
28 var mockControl;
29 var mockChannel1;
30 var mockChannel2;
31 var operator;
32
33 function setUpPage() {
34 stubs = new goog.testing.PropertyReplacer();
35 }
36
37 function setUp() {
38 mockControl = new goog.testing.MockControl();
39 var index = 0;
40 stubs.set(goog.global, 'MessageChannel', function() {
41 this.port1 = makeMockPort(index, 1);
42 this.port2 = makeMockPort(index, 2);
43 index += 1;
44 });
45
46 mockChannel1 = new goog.testing.messaging.MockMessageChannel(mockControl);
47 mockChannel2 = new goog.testing.messaging.MockMessageChannel(mockControl);
48 operator = new goog.messaging.PortOperator('operator');
49 operator.addPort('1', mockChannel1);
50 operator.addPort('2', mockChannel2);
51 }
52
53 function tearDown() {
54 goog.dispose(operator);
55 mockControl.$verifyAll();
56 stubs.reset();
57 }
58
59 function makeMockPort(index, port) {
60 return new goog.testing.messaging.MockMessagePort(
61 {index: index, port: port}, mockControl);
62 }
63
64 function testConnectSelfToPortViaRequestConnection() {
65 mockChannel1.send(
66 goog.messaging.PortNetwork.GRANT_CONNECTION_SERVICE,
67 {success: true, name: 'operator', port: makeMockPort(0, 1)});
68 mockControl.$replayAll();
69 mockChannel1.receive(
70 goog.messaging.PortNetwork.REQUEST_CONNECTION_SERVICE, 'operator');
71 var port = operator.dial('1').port_;
72 assertObjectEquals({index: 0, port: 2}, port.id);
73 assertEquals(true, port.started);
74 }
75
76 function testConnectSelfToPortViaGetPort() {
77 mockChannel1.send(
78 goog.messaging.PortNetwork.GRANT_CONNECTION_SERVICE,
79 {success: true, name: 'operator', port: makeMockPort(0, 1)});
80 mockControl.$replayAll();
81 var port = operator.dial('1').port_;
82 assertObjectEquals({index: 0, port: 2}, port.id);
83 assertEquals(true, port.started);
84 }
85
86 function testConnectTwoCallers() {
87 mockChannel1.send(
88 goog.messaging.PortNetwork.GRANT_CONNECTION_SERVICE,
89 {success: true, name: '2', port: makeMockPort(0, 1)});
90 mockChannel2.send(
91 goog.messaging.PortNetwork.GRANT_CONNECTION_SERVICE,
92 {success: true, name: '1', port: makeMockPort(0, 2)});
93 mockControl.$replayAll();
94 mockChannel1.receive(
95 goog.messaging.PortNetwork.REQUEST_CONNECTION_SERVICE, '2');
96 }
97
98 function testConnectCallerToNonexistentCaller() {
99 mockChannel1.send(goog.messaging.PortNetwork.GRANT_CONNECTION_SERVICE, {
100 success: false,
101 message: 'Port "1" requested a connection to port "no", which doesn\'t ' +
102 'exist'
103 });
104 mockControl.$replayAll();
105 mockChannel1.receive(
106 goog.messaging.PortNetwork.REQUEST_CONNECTION_SERVICE, 'no');
107 }