| 1 |
// Copyright 2013 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 |
/** |
| 17 |
* @fileoverview Tests for goog.ui.MockActivityMonitorTest. |
| 18 |
* @author [email protected] (Nathan Naze) |
| 19 |
*/ |
| 20 |
|
| 21 |
/** @suppress {extraProvide} */ |
| 22 |
goog.provide('goog.ui.MockActivityMonitorTest'); |
| 23 |
|
| 24 |
goog.require('goog.events'); |
| 25 |
goog.require('goog.functions'); |
| 26 |
goog.require('goog.testing.jsunit'); |
| 27 |
goog.require('goog.testing.recordFunction'); |
| 28 |
goog.require('goog.ui.ActivityMonitor'); |
| 29 |
goog.require('goog.ui.MockActivityMonitor'); |
| 30 |
|
| 31 |
goog.setTestOnly('goog.ui.MockActivityMonitorTest'); |
| 32 |
|
| 33 |
var googNow = goog.now; |
| 34 |
var monitor; |
| 35 |
var recordedFunction; |
| 36 |
var replacer; |
| 37 |
|
| 38 |
function setUp() { |
| 39 |
monitor = new goog.ui.MockActivityMonitor(); |
| 40 |
recordedFunction = goog.testing.recordFunction(); |
| 41 |
|
| 42 |
goog.events.listen( |
| 43 |
monitor, goog.ui.ActivityMonitor.Event.ACTIVITY, recordedFunction); |
| 44 |
} |
| 45 |
|
| 46 |
function tearDown() { |
| 47 |
goog.dispose(monitor); |
| 48 |
goog.now = googNow; |
| 49 |
} |
| 50 |
|
| 51 |
function testEventFireSameTime() { |
| 52 |
goog.now = goog.functions.constant(1000); |
| 53 |
|
| 54 |
monitor.simulateEvent(); |
| 55 |
assertEquals(1, recordedFunction.getCallCount()); |
| 56 |
|
| 57 |
monitor.simulateEvent(); |
| 58 |
assertEquals(2, recordedFunction.getCallCount()); |
| 59 |
} |
| 60 |
|
| 61 |
function testEventFireDifferingTime() { |
| 62 |
goog.now = goog.functions.constant(1000); |
| 63 |
monitor.simulateEvent(); |
| 64 |
assertEquals(1, recordedFunction.getCallCount()); |
| 65 |
|
| 66 |
goog.now = goog.functions.constant(1001); |
| 67 |
monitor.simulateEvent(); |
| 68 |
assertEquals(2, recordedFunction.getCallCount()); |
| 69 |
} |
| 70 |
|
| 71 |
function testDispatchEventReturnValue() { |
| 72 |
assertTrue(monitor.dispatchEvent(goog.ui.ActivityMonitor.Event.ACTIVITY)); |
| 73 |
assertEquals(1, recordedFunction.getCallCount()); |
| 74 |
} |
| 75 |
|
| 76 |
function testDispatchEventPreventDefault() { |
| 77 |
// Undo the listen call in setUp. |
| 78 |
goog.events.unlisten( |
| 79 |
monitor, goog.ui.ActivityMonitor.Event.ACTIVITY, recordedFunction); |
| 80 |
|
| 81 |
// Listen with a function that cancels the event. |
| 82 |
goog.events.listen( |
| 83 |
monitor, goog.ui.ActivityMonitor.Event.ACTIVITY, |
| 84 |
function(e) { e.preventDefault(); }); |
| 85 |
|
| 86 |
assertFalse(monitor.dispatchEvent(goog.ui.ActivityMonitor.Event.ACTIVITY)); |
| 87 |
} |