portoperator_test.js 3.5 KB

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