mockmessagechannel.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /**
  15. * @fileoverview Mock MessageChannel implementation that can receive fake
  16. * messages and test that the right messages are sent.
  17. *
  18. */
  19. goog.setTestOnly('goog.testing.messaging.MockMessageChannel');
  20. goog.provide('goog.testing.messaging.MockMessageChannel');
  21. goog.require('goog.messaging.AbstractChannel');
  22. goog.require('goog.testing.asserts');
  23. /**
  24. * Class for unit-testing code that communicates over a MessageChannel.
  25. * @param {goog.testing.MockControl} mockControl The mock control used to create
  26. * the method mock for #send.
  27. * @extends {goog.messaging.AbstractChannel}
  28. * @constructor
  29. * @final
  30. */
  31. goog.testing.messaging.MockMessageChannel = function(mockControl) {
  32. goog.testing.messaging.MockMessageChannel.base(this, 'constructor');
  33. /**
  34. * Whether the channel has been disposed.
  35. * @type {boolean}
  36. */
  37. this.disposed = false;
  38. mockControl.createMethodMock(this, 'send');
  39. };
  40. goog.inherits(
  41. goog.testing.messaging.MockMessageChannel, goog.messaging.AbstractChannel);
  42. /**
  43. * A mock send function. Actually an instance of
  44. * {@link goog.testing.FunctionMock}.
  45. * @param {string} serviceName The name of the remote service to run.
  46. * @param {string|!Object} payload The payload to send to the remote page.
  47. * @override
  48. */
  49. goog.testing.messaging.MockMessageChannel.prototype.send = function(
  50. serviceName, payload) {};
  51. /**
  52. * Sets a flag indicating that this is disposed.
  53. * @override
  54. */
  55. goog.testing.messaging.MockMessageChannel.prototype.dispose = function() {
  56. this.disposed = true;
  57. };
  58. /**
  59. * Mocks the receipt of a message. Passes the payload the appropriate service.
  60. * @param {string} serviceName The service to run.
  61. * @param {string|!Object} payload The argument to pass to the service.
  62. */
  63. goog.testing.messaging.MockMessageChannel.prototype.receive = function(
  64. serviceName, payload) {
  65. this.deliver(serviceName, payload);
  66. };