| 1 |
// Copyright 2007 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.IdleTimerTest'); |
| 16 |
goog.setTestOnly('goog.ui.IdleTimerTest'); |
| 17 |
|
| 18 |
goog.require('goog.events'); |
| 19 |
goog.require('goog.testing.MockClock'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
goog.require('goog.ui.IdleTimer'); |
| 22 |
goog.require('goog.ui.MockActivityMonitor'); |
| 23 |
|
| 24 |
var clock; |
| 25 |
|
| 26 |
function setUp() { |
| 27 |
clock = new goog.testing.MockClock(true); |
| 28 |
goog.now = goog.bind(clock.getCurrentTime, clock); |
| 29 |
} |
| 30 |
|
| 31 |
function tearDown() { |
| 32 |
clock.dispose(); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* Tests whether an event is fired when the user becomes idle |
| 38 |
*/ |
| 39 |
function testBecomeIdle() { |
| 40 |
var idleThreshold = 1000; |
| 41 |
var mockActivityMonitor = new goog.ui.MockActivityMonitor(); |
| 42 |
var idleTimer = new goog.ui.IdleTimer(idleThreshold, mockActivityMonitor); |
| 43 |
|
| 44 |
mockActivityMonitor.simulateEvent(); |
| 45 |
assertFalse('Precondition: user should be active', idleTimer.isIdle()); |
| 46 |
|
| 47 |
var onBecomeIdleCount = 0; |
| 48 |
var onBecomeIdle = function() { onBecomeIdleCount += 1; }; |
| 49 |
goog.events.listen( |
| 50 |
idleTimer, goog.ui.IdleTimer.Event.BECOME_IDLE, onBecomeIdle); |
| 51 |
|
| 52 |
clock.tick(idleThreshold); |
| 53 |
mockActivityMonitor.simulateEvent(); |
| 54 |
clock.tick(idleThreshold); |
| 55 |
assert('The BECOME_IDLE event fired too early', onBecomeIdleCount == 0); |
| 56 |
assertFalse('The user should still be active', idleTimer.isIdle()); |
| 57 |
|
| 58 |
clock.tick(1); |
| 59 |
assert('The BECOME_IDLE event fired too late', onBecomeIdleCount == 1); |
| 60 |
assert('The user should be idle', idleTimer.isIdle()); |
| 61 |
|
| 62 |
idleTimer.dispose(); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Tests whether an event is fired when the user becomes active |
| 68 |
*/ |
| 69 |
function testBecomeActive() { |
| 70 |
var idleThreshold = 1000; |
| 71 |
var mockActivityMonitor = new goog.ui.MockActivityMonitor(); |
| 72 |
var idleTimer = new goog.ui.IdleTimer(idleThreshold, mockActivityMonitor); |
| 73 |
|
| 74 |
clock.tick(idleThreshold + 1); |
| 75 |
assert('Precondition: user should be idle', idleTimer.isIdle()); |
| 76 |
|
| 77 |
var onBecomeActiveCount = 0; |
| 78 |
var onBecomeActive = function() { onBecomeActiveCount += 1; }; |
| 79 |
goog.events.listen( |
| 80 |
idleTimer, goog.ui.IdleTimer.Event.BECOME_ACTIVE, onBecomeActive); |
| 81 |
|
| 82 |
clock.tick(idleThreshold); |
| 83 |
assert('The BECOME_ACTIVE event fired too early', onBecomeActiveCount == 0); |
| 84 |
assert('The user should still be idle', idleTimer.isIdle()); |
| 85 |
|
| 86 |
mockActivityMonitor.simulateEvent(); |
| 87 |
assert('The BECOME_ACTIVE event fired too late', onBecomeActiveCount == 1); |
| 88 |
assertFalse('The user should be active', idleTimer.isIdle()); |
| 89 |
|
| 90 |
idleTimer.dispose(); |
| 91 |
} |