mockmessageport.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. * @fileoverview A simple dummy class for representing message ports in tests.
  16. *
  17. */
  18. goog.setTestOnly('goog.testing.messaging.MockMessagePort');
  19. goog.provide('goog.testing.messaging.MockMessagePort');
  20. goog.require('goog.events.EventTarget');
  21. /**
  22. * Class for unit-testing code that uses MessagePorts.
  23. * @param {*} id An opaque identifier, used because message ports otherwise have
  24. * no distinguishing characteristics.
  25. * @param {goog.testing.MockControl} mockControl The mock control used to create
  26. * the method mock for #postMessage.
  27. * @constructor
  28. * @extends {goog.events.EventTarget}
  29. * @final
  30. */
  31. goog.testing.messaging.MockMessagePort = function(id, mockControl) {
  32. goog.testing.messaging.MockMessagePort.base(this, 'constructor');
  33. /**
  34. * An opaque identifier, used because message ports otherwise have no
  35. * distinguishing characteristics.
  36. * @type {*}
  37. */
  38. this.id = id;
  39. /**
  40. * Whether or not the port has been started.
  41. * @type {boolean}
  42. */
  43. this.started = false;
  44. /**
  45. * Whether or not the port has been closed.
  46. * @type {boolean}
  47. */
  48. this.closed = false;
  49. mockControl.createMethodMock(this, 'postMessage');
  50. };
  51. goog.inherits(goog.testing.messaging.MockMessagePort, goog.events.EventTarget);
  52. /**
  53. * A mock postMessage funciton. Actually an instance of
  54. * {@link goog.testing.FunctionMock}.
  55. * @param {*} message The message to send.
  56. * @param {Array<MessagePort>=} opt_ports Ports to send with the message.
  57. */
  58. goog.testing.messaging.MockMessagePort.prototype.postMessage = function(
  59. message, opt_ports) {};
  60. /**
  61. * Starts the port.
  62. */
  63. goog.testing.messaging.MockMessagePort.prototype.start = function() {
  64. this.started = true;
  65. };
  66. /**
  67. * Closes the port.
  68. */
  69. goog.testing.messaging.MockMessagePort.prototype.close = function() {
  70. this.closed = true;
  71. };