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