aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/activitymonitor_test.html
blob: 2815036f838d8cb2f2402895e36ac547280f07ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<html>
<!--
Copyright 2006 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.ui.ActivityMonitor</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.events.Event');
  goog.require('goog.events.EventType');
  goog.require('goog.testing.PropertyReplacer');
  goog.require('goog.testing.MockClock');
  goog.require('goog.testing.events');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.mockmatchers');
  goog.require('goog.testing.recordFunction');
  goog.require('goog.ui.ActivityMonitor');
</script>
</head>
<body>
<div id="mydiv">foo</div>
<script>

  var mockClock;
  var stubs = new goog.testing.PropertyReplacer();
  var mydiv = document.getElementById('mydiv');

  function setUp() {
    mockClock = new goog.testing.MockClock(true);
    // ActivityMonitor initializes a private to 0 which it compares to now(),
    // so tests fail unless we start the mock clock with something besides 0.
    mockClock.tick(1);
  }

  function tearDown() {
    mockClock.dispose();
    stubs.reset();
  }

  function testIdle() {
    var activityMonitor = new goog.ui.ActivityMonitor();
    assertEquals('Upon creation, last event time should be creation time',
        mockClock.getCurrentTime(), activityMonitor.getLastEventTime());

    mockClock.tick(1000);
    activityMonitor.resetTimer();
    var resetTime = mockClock.getCurrentTime();
    assertEquals('Upon reset, last event time should be reset time',
        resetTime, activityMonitor.getLastEventTime());
    assertEquals('Upon reset, idle time should be zero',
        0, activityMonitor.getIdleTime());

    mockClock.tick(1000);
    assertEquals('1s after reset, last event time should be reset time',
        resetTime, activityMonitor.getLastEventTime());
    assertEquals('1s after reset, idle time should be 1s',
        1000, activityMonitor.getIdleTime());
  }

  function testEventFired() {
    var activityMonitor = new goog.ui.ActivityMonitor();
    var listener = goog.testing.recordFunction();
    goog.events.listen(activityMonitor, goog.ui.ActivityMonitor.Event.ACTIVITY,
                       listener);

    mockClock.tick(1000);
    goog.testing.events.fireClickEvent(mydiv);
    assertEquals('Activity event should fire when click happens after creation',
                 1, listener.getCallCount());

    mockClock.tick(3000);
    goog.testing.events.fireClickEvent(mydiv);
    assertEquals('Activity event should not fire when click happens 3s or ' +
                 'less since the last activity', 1, listener.getCallCount());

    mockClock.tick(1);
    goog.testing.events.fireClickEvent(mydiv);
    assertEquals('Activity event should fire when click happens more than ' +
                 '3s since the last activity', 2, listener.getCallCount());
  }

  function testEventFiredWhenPropagationStopped() {
    var activityMonitor = new goog.ui.ActivityMonitor();
    var listener = goog.testing.recordFunction();
    goog.events.listen(activityMonitor, goog.ui.ActivityMonitor.Event.ACTIVITY,
                       listener);

    goog.events.listenOnce(mydiv, goog.events.EventType.CLICK,
                           goog.events.Event.stopPropagation);
    goog.testing.events.fireClickEvent(mydiv);
    assertEquals('Activity event should fire despite click propagation ' +
        'stopped because listening on capture', 1, listener.getCallCount());
  }

  function testEventNotFiredWhenPropagationStopped() {
    var activityMonitor = new goog.ui.ActivityMonitor(undefined, true);
    var listener = goog.testing.recordFunction();
    goog.events.listen(activityMonitor, goog.ui.ActivityMonitor.Event.ACTIVITY,
                       listener);

    goog.events.listenOnce(mydiv, goog.events.EventType.CLICK,
                           goog.events.Event.stopPropagation);
    goog.testing.events.fireClickEvent(mydiv);
    assertEquals('Activity event should not fire since click propagation ' +
        'stopped and listening on bubble', 0, listener.getCallCount());
  }

</script>
</body>
</html>