| 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 Event observer. |
| 17 |
* |
| 18 |
* Provides an event observer that holds onto events that it handles. This |
| 19 |
* can be used in unit testing to verify an event target's events -- |
| 20 |
* that the order count, types, etc. are correct. |
| 21 |
* |
| 22 |
* Example usage: |
| 23 |
* <pre> |
| 24 |
* var observer = new goog.testing.events.EventObserver(); |
| 25 |
* var widget = new foo.Widget(); |
| 26 |
* goog.events.listen(widget, ['select', 'submit'], observer); |
| 27 |
* // Simulate user action of 3 select events and 2 submit events. |
| 28 |
* assertEquals(3, observer.getEvents('select').length); |
| 29 |
* assertEquals(2, observer.getEvents('submit').length); |
| 30 |
* </pre> |
| 31 |
* |
| 32 |
* @author [email protected] (Nathan Naze) |
| 33 |
*/ |
| 34 |
|
| 35 |
goog.setTestOnly('goog.testing.events.EventObserver'); |
| 36 |
goog.provide('goog.testing.events.EventObserver'); |
| 37 |
|
| 38 |
goog.require('goog.array'); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Event observer. Implements a handleEvent interface so it may be used as |
| 44 |
* a listener in listening functions and methods. |
| 45 |
* @see goog.events.listen |
| 46 |
* @see goog.events.EventHandler |
| 47 |
* @constructor |
| 48 |
* @final |
| 49 |
*/ |
| 50 |
goog.testing.events.EventObserver = function() { |
| 51 |
|
| 52 |
/** |
| 53 |
* A list of events handled by the observer in order of handling, oldest to |
| 54 |
* newest. |
| 55 |
* @type {!Array<!goog.events.Event>} |
| 56 |
* @private |
| 57 |
*/ |
| 58 |
this.events_ = []; |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* Handles an event and remembers it. Event listening functions and methods |
| 64 |
* will call this method when this observer is used as a listener. |
| 65 |
* @see goog.events.listen |
| 66 |
* @see goog.events.EventHandler |
| 67 |
* @param {!goog.events.Event} e Event to handle. |
| 68 |
*/ |
| 69 |
goog.testing.events.EventObserver.prototype.handleEvent = function(e) { |
| 70 |
this.events_.push(e); |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
/** |
| 75 |
* @param {string=} opt_type If given, only return events of this type. |
| 76 |
* @return {!Array<!goog.events.Event>} The events handled, oldest to newest. |
| 77 |
*/ |
| 78 |
goog.testing.events.EventObserver.prototype.getEvents = function(opt_type) { |
| 79 |
var events = goog.array.clone(this.events_); |
| 80 |
|
| 81 |
if (opt_type) { |
| 82 |
events = goog.array.filter( |
| 83 |
events, function(event) { return event.type == opt_type; }); |
| 84 |
} |
| 85 |
|
| 86 |
return events; |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
/** Clears the list of events seen by this observer. */ |
| 91 |
goog.testing.events.EventObserver.prototype.clear = function() { |
| 92 |
this.events_ = []; |
| 93 |
}; |