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