| 1 |
// Copyright 2006 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 |
goog.provide('goog.ui.ActivityMonitorTest'); |
| 16 |
goog.setTestOnly('goog.ui.ActivityMonitorTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.events'); |
| 20 |
goog.require('goog.events.Event'); |
| 21 |
goog.require('goog.events.EventType'); |
| 22 |
goog.require('goog.testing.MockClock'); |
| 23 |
goog.require('goog.testing.PropertyReplacer'); |
| 24 |
goog.require('goog.testing.events'); |
| 25 |
goog.require('goog.testing.jsunit'); |
| 26 |
goog.require('goog.testing.recordFunction'); |
| 27 |
goog.require('goog.ui.ActivityMonitor'); |
| 28 |
|
| 29 |
var mockClock; |
| 30 |
var stubs = new goog.testing.PropertyReplacer(); |
| 31 |
var mydiv; |
| 32 |
|
| 33 |
function setUp() { |
| 34 |
mydiv = document.getElementById('mydiv'); |
| 35 |
mockClock = new goog.testing.MockClock(true); |
| 36 |
// ActivityMonitor initializes a private to 0 which it compares to now(), |
| 37 |
// so tests fail unless we start the mock clock with something besides 0. |
| 38 |
mockClock.tick(1); |
| 39 |
} |
| 40 |
|
| 41 |
function tearDown() { |
| 42 |
mockClock.dispose(); |
| 43 |
stubs.reset(); |
| 44 |
} |
| 45 |
|
| 46 |
function testIdle() { |
| 47 |
var activityMonitor = new goog.ui.ActivityMonitor(); |
| 48 |
assertEquals( |
| 49 |
'Upon creation, last event time should be creation time', |
| 50 |
mockClock.getCurrentTime(), activityMonitor.getLastEventTime()); |
| 51 |
|
| 52 |
mockClock.tick(1000); |
| 53 |
activityMonitor.resetTimer(); |
| 54 |
var resetTime = mockClock.getCurrentTime(); |
| 55 |
assertEquals( |
| 56 |
'Upon reset, last event time should be reset time', resetTime, |
| 57 |
activityMonitor.getLastEventTime()); |
| 58 |
assertEquals( |
| 59 |
'Upon reset, idle time should be zero', 0, activityMonitor.getIdleTime()); |
| 60 |
|
| 61 |
mockClock.tick(1000); |
| 62 |
assertEquals( |
| 63 |
'1s after reset, last event time should be reset time', resetTime, |
| 64 |
activityMonitor.getLastEventTime()); |
| 65 |
assertEquals( |
| 66 |
'1s after reset, idle time should be 1s', 1000, |
| 67 |
activityMonitor.getIdleTime()); |
| 68 |
} |
| 69 |
|
| 70 |
function testEventFired() { |
| 71 |
var activityMonitor = new goog.ui.ActivityMonitor(); |
| 72 |
var listener = goog.testing.recordFunction(); |
| 73 |
goog.events.listen( |
| 74 |
activityMonitor, goog.ui.ActivityMonitor.Event.ACTIVITY, listener); |
| 75 |
|
| 76 |
mockClock.tick(1000); |
| 77 |
goog.testing.events.fireClickEvent(mydiv); |
| 78 |
assertEquals( |
| 79 |
'Activity event should fire when click happens after creation', 1, |
| 80 |
listener.getCallCount()); |
| 81 |
|
| 82 |
mockClock.tick(3000); |
| 83 |
goog.testing.events.fireClickEvent(mydiv); |
| 84 |
assertEquals( |
| 85 |
'Activity event should not fire when click happens 3s or ' + |
| 86 |
'less since the last activity', |
| 87 |
1, listener.getCallCount()); |
| 88 |
|
| 89 |
mockClock.tick(1); |
| 90 |
goog.testing.events.fireClickEvent(mydiv); |
| 91 |
assertEquals( |
| 92 |
'Activity event should fire when click happens more than ' + |
| 93 |
'3s since the last activity', |
| 94 |
2, listener.getCallCount()); |
| 95 |
} |
| 96 |
|
| 97 |
function testEventFiredWhenPropagationStopped() { |
| 98 |
var activityMonitor = new goog.ui.ActivityMonitor(); |
| 99 |
var listener = goog.testing.recordFunction(); |
| 100 |
goog.events.listen( |
| 101 |
activityMonitor, goog.ui.ActivityMonitor.Event.ACTIVITY, listener); |
| 102 |
|
| 103 |
goog.events.listenOnce( |
| 104 |
mydiv, goog.events.EventType.CLICK, goog.events.Event.stopPropagation); |
| 105 |
goog.testing.events.fireClickEvent(mydiv); |
| 106 |
assertEquals( |
| 107 |
'Activity event should fire despite click propagation ' + |
| 108 |
'stopped because listening on capture', |
| 109 |
1, listener.getCallCount()); |
| 110 |
} |
| 111 |
|
| 112 |
function testEventNotFiredWhenPropagationStopped() { |
| 113 |
var activityMonitor = new goog.ui.ActivityMonitor(undefined, true); |
| 114 |
var listener = goog.testing.recordFunction(); |
| 115 |
goog.events.listen( |
| 116 |
activityMonitor, goog.ui.ActivityMonitor.Event.ACTIVITY, listener); |
| 117 |
|
| 118 |
goog.events.listenOnce( |
| 119 |
mydiv, goog.events.EventType.CLICK, goog.events.Event.stopPropagation); |
| 120 |
goog.testing.events.fireClickEvent(mydiv); |
| 121 |
assertEquals( |
| 122 |
'Activity event should not fire since click propagation ' + |
| 123 |
'stopped and listening on bubble', |
| 124 |
0, listener.getCallCount()); |
| 125 |
} |
| 126 |
|
| 127 |
function testTouchSequenceFired() { |
| 128 |
var activityMonitor = new goog.ui.ActivityMonitor(); |
| 129 |
var listener = goog.testing.recordFunction(); |
| 130 |
goog.events.listen( |
| 131 |
activityMonitor, goog.ui.ActivityMonitor.Event.ACTIVITY, listener); |
| 132 |
|
| 133 |
mockClock.tick(1000); |
| 134 |
goog.testing.events.fireTouchSequence(mydiv); |
| 135 |
assertEquals( |
| 136 |
'Activity event should fire when touch happens after creation', 1, |
| 137 |
listener.getCallCount()); |
| 138 |
|
| 139 |
mockClock.tick(3000); |
| 140 |
goog.testing.events.fireTouchSequence(mydiv); |
| 141 |
assertEquals( |
| 142 |
'Activity event should not fire when touch happens 3s or ' + |
| 143 |
'less since the last activity', |
| 144 |
1, listener.getCallCount()); |
| 145 |
|
| 146 |
mockClock.tick(1); |
| 147 |
goog.testing.events.fireTouchSequence(mydiv); |
| 148 |
assertEquals( |
| 149 |
'Activity event should fire when touch happens more than ' + |
| 150 |
'3s since the last activity', |
| 151 |
2, listener.getCallCount()); |
| 152 |
} |
| 153 |
|
| 154 |
function testAddDocument_duplicate() { |
| 155 |
var defaultDoc = goog.dom.getDomHelper().getDocument(); |
| 156 |
var activityMonitor = new goog.ui.ActivityMonitor(); |
| 157 |
assertEquals(1, activityMonitor.documents_.length); |
| 158 |
assertEquals(defaultDoc, activityMonitor.documents_[0]); |
| 159 |
var listenerCount = activityMonitor.eventHandler_.getListenerCount(); |
| 160 |
|
| 161 |
activityMonitor.addDocument(defaultDoc); |
| 162 |
assertEquals(1, activityMonitor.documents_.length); |
| 163 |
assertEquals(defaultDoc, activityMonitor.documents_[0]); |
| 164 |
assertEquals(listenerCount, activityMonitor.eventHandler_.getListenerCount()); |
| 165 |
} |