mockmessageevent.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 A simple mock class for imitating HTML5 MessageEvents.
  16. *
  17. */
  18. goog.setTestOnly('goog.testing.messaging.MockMessageEvent');
  19. goog.provide('goog.testing.messaging.MockMessageEvent');
  20. goog.require('goog.events.BrowserEvent');
  21. goog.require('goog.events.EventType');
  22. goog.require('goog.testing.events.Event');
  23. /**
  24. * Creates a new fake MessageEvent.
  25. *
  26. * @param {*} data The data of the message.
  27. * @param {string=} opt_origin The origin of the message, for server-sent and
  28. * cross-document events.
  29. * @param {string=} opt_lastEventId The last event ID, for server-sent events.
  30. * @param {Window=} opt_source The proxy for the source window, for
  31. * cross-document events.
  32. * @param {Array<MessagePort>=} opt_ports The Array of ports sent with the
  33. * message, for cross-document and channel events.
  34. * @extends {goog.testing.events.Event}
  35. * @constructor
  36. * @final
  37. */
  38. goog.testing.messaging.MockMessageEvent = function(
  39. data, opt_origin, opt_lastEventId, opt_source, opt_ports) {
  40. goog.testing.messaging.MockMessageEvent.base(
  41. this, 'constructor', goog.events.EventType.MESSAGE);
  42. /**
  43. * The data of the message.
  44. * @type {*}
  45. */
  46. this.data = data;
  47. /**
  48. * The origin of the message, for server-sent and cross-document events.
  49. * @type {?string}
  50. */
  51. this.origin = opt_origin || null;
  52. /**
  53. * The last event ID, for server-sent events.
  54. * @type {?string}
  55. */
  56. this.lastEventId = opt_lastEventId || null;
  57. /**
  58. * The proxy for the source window, for cross-document events.
  59. * @type {Window}
  60. */
  61. this.source = opt_source || null;
  62. /**
  63. * The Array of ports sent with the message, for cross-document and channel
  64. * events.
  65. * @type {Array<!MessagePort>}
  66. */
  67. this.ports = opt_ports || null;
  68. };
  69. goog.inherits(
  70. goog.testing.messaging.MockMessageEvent, goog.testing.events.Event);
  71. /**
  72. * Wraps a new fake MessageEvent in a BrowserEvent, like how a real MessageEvent
  73. * would be wrapped.
  74. *
  75. * @param {*} data The data of the message.
  76. * @param {string=} opt_origin The origin of the message, for server-sent and
  77. * cross-document events.
  78. * @param {string=} opt_lastEventId The last event ID, for server-sent events.
  79. * @param {Window=} opt_source The proxy for the source window, for
  80. * cross-document events.
  81. * @param {Array<MessagePort>=} opt_ports The Array of ports sent with the
  82. * message, for cross-document and channel events.
  83. * @return {!goog.events.BrowserEvent} The wrapping event.
  84. */
  85. goog.testing.messaging.MockMessageEvent.wrap = function(
  86. data, opt_origin, opt_lastEventId, opt_source, opt_ports) {
  87. return new goog.events.BrowserEvent(
  88. new goog.testing.messaging.MockMessageEvent(
  89. data, opt_origin, opt_lastEventId, opt_source, opt_ports));
  90. };