multichannel_test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. goog.provide('goog.messaging.MultiChannelTest');
  15. goog.setTestOnly('goog.messaging.MultiChannelTest');
  16. goog.require('goog.messaging.MultiChannel');
  17. goog.require('goog.testing.MockControl');
  18. goog.require('goog.testing.jsunit');
  19. goog.require('goog.testing.messaging.MockMessageChannel');
  20. goog.require('goog.testing.mockmatchers.IgnoreArgument');
  21. var mockControl;
  22. var mockChannel;
  23. var multiChannel;
  24. var channel1;
  25. var channel2;
  26. function setUp() {
  27. mockControl = new goog.testing.MockControl();
  28. mockChannel = new goog.testing.messaging.MockMessageChannel(mockControl);
  29. multiChannel = new goog.messaging.MultiChannel(mockChannel);
  30. channel0 = multiChannel.createVirtualChannel('foo');
  31. channel1 = multiChannel.createVirtualChannel('bar');
  32. }
  33. function expectedFn(name, callback) {
  34. var ignored = new goog.testing.mockmatchers.IgnoreArgument();
  35. var fn = mockControl.createFunctionMock(name);
  36. fn(ignored).$does(function(args) { callback.apply(this, args); });
  37. return function() { fn(arguments); };
  38. }
  39. function notExpectedFn() {
  40. return mockControl.createFunctionMock('notExpectedFn');
  41. }
  42. function assertEqualsFn() {
  43. var expectedArgs = Array.prototype.slice.call(arguments);
  44. return expectedFn('assertEqualsFn', function() {
  45. assertObjectEquals(expectedArgs, Array.prototype.slice.call(arguments));
  46. });
  47. }
  48. function tearDown() {
  49. multiChannel.dispose();
  50. mockControl.$verifyAll();
  51. assertTrue(mockChannel.disposed);
  52. }
  53. function testSend0() {
  54. mockChannel.send('foo:fooBar', {foo: 'bar'});
  55. mockControl.$replayAll();
  56. channel0.send('fooBar', {foo: 'bar'});
  57. }
  58. function testSend1() {
  59. mockChannel.send('bar:fooBar', {foo: 'bar'});
  60. mockControl.$replayAll();
  61. channel1.send('fooBar', {foo: 'bar'});
  62. }
  63. function testReceive0() {
  64. channel0.registerService('fooBar', assertEqualsFn('Baz bang'));
  65. channel1.registerService('fooBar', notExpectedFn());
  66. mockControl.$replayAll();
  67. mockChannel.receive('foo:fooBar', 'Baz bang');
  68. }
  69. function testReceive1() {
  70. channel1.registerService('fooBar', assertEqualsFn('Baz bang'));
  71. channel0.registerService('fooBar', notExpectedFn());
  72. mockControl.$replayAll();
  73. mockChannel.receive('bar:fooBar', 'Baz bang');
  74. }
  75. function testDefaultReceive0() {
  76. channel0.registerDefaultService(assertEqualsFn('fooBar', 'Baz bang'));
  77. channel1.registerDefaultService(notExpectedFn());
  78. mockControl.$replayAll();
  79. mockChannel.receive('foo:fooBar', 'Baz bang');
  80. }
  81. function testDefaultReceive1() {
  82. channel1.registerDefaultService(assertEqualsFn('fooBar', 'Baz bang'));
  83. channel0.registerDefaultService(notExpectedFn());
  84. mockControl.$replayAll();
  85. mockChannel.receive('bar:fooBar', 'Baz bang');
  86. }
  87. function testReceiveAfterDisposed() {
  88. channel0.registerService('fooBar', notExpectedFn());
  89. mockControl.$replayAll();
  90. channel0.dispose();
  91. mockChannel.receive('foo:fooBar', 'Baz bang');
  92. }
  93. function testReceiveAfterParentDisposed() {
  94. channel0.registerService('fooBar', notExpectedFn());
  95. mockControl.$replayAll();
  96. multiChannel.dispose();
  97. mockChannel.receive('foo:fooBar', 'Baz bang');
  98. }