deferredchannel_test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.DeferredChannelTest');
  15. goog.setTestOnly('goog.messaging.DeferredChannelTest');
  16. goog.require('goog.async.Deferred');
  17. goog.require('goog.messaging.DeferredChannel');
  18. goog.require('goog.testing.MockControl');
  19. goog.require('goog.testing.async.MockControl');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.testing.messaging.MockMessageChannel');
  22. var mockControl, asyncMockControl;
  23. var mockChannel, deferredChannel;
  24. var cancelled;
  25. var deferred;
  26. function setUp() {
  27. mockControl = new goog.testing.MockControl();
  28. asyncMockControl = new goog.testing.async.MockControl(mockControl);
  29. mockChannel = new goog.testing.messaging.MockMessageChannel(mockControl);
  30. cancelled = false;
  31. deferred = new goog.async.Deferred(function() { cancelled = true; });
  32. deferredChannel = new goog.messaging.DeferredChannel(deferred);
  33. }
  34. function tearDown() {
  35. mockControl.$verifyAll();
  36. }
  37. function testDeferredResolvedBeforeSend() {
  38. mockChannel.send('test', 'val');
  39. mockControl.$replayAll();
  40. deferred.callback(mockChannel);
  41. deferredChannel.send('test', 'val');
  42. }
  43. function testDeferredResolvedBeforeRegister() {
  44. deferred.callback(mockChannel);
  45. deferredChannel.registerService(
  46. 'test', asyncMockControl.asyncAssertEquals('passes on register', 'val'));
  47. mockChannel.receive('test', 'val');
  48. }
  49. function testDeferredResolvedBeforeRegisterObject() {
  50. deferred.callback(mockChannel);
  51. deferredChannel.registerService(
  52. 'test',
  53. asyncMockControl.asyncAssertEquals('passes on register', {'key': 'val'}),
  54. true);
  55. mockChannel.receive('test', {'key': 'val'});
  56. }
  57. function testDeferredResolvedBeforeRegisterDefault() {
  58. deferred.callback(mockChannel);
  59. deferredChannel.registerDefaultService(
  60. asyncMockControl.asyncAssertEquals('passes on register', 'test', 'val'));
  61. mockChannel.receive('test', 'val');
  62. }
  63. function testDeferredResolvedAfterSend() {
  64. mockChannel.send('test', 'val');
  65. mockControl.$replayAll();
  66. deferredChannel.send('test', 'val');
  67. deferred.callback(mockChannel);
  68. }
  69. function testDeferredResolvedAfterRegister() {
  70. deferredChannel.registerService(
  71. 'test', asyncMockControl.asyncAssertEquals('passes on register', 'val'));
  72. deferred.callback(mockChannel);
  73. mockChannel.receive('test', 'val');
  74. }
  75. function testDeferredResolvedAfterRegisterObject() {
  76. deferredChannel.registerService(
  77. 'test',
  78. asyncMockControl.asyncAssertEquals('passes on register', {'key': 'val'}),
  79. true);
  80. deferred.callback(mockChannel);
  81. mockChannel.receive('test', {'key': 'val'});
  82. }
  83. function testDeferredResolvedAfterRegisterDefault() {
  84. deferredChannel.registerDefaultService(
  85. asyncMockControl.asyncAssertEquals('passes on register', 'test', 'val'));
  86. deferred.callback(mockChannel);
  87. mockChannel.receive('test', 'val');
  88. }
  89. function testCancel() {
  90. deferredChannel.cancel();
  91. assertTrue(cancelled);
  92. }