Debian Codegraph @

closure-library/closure/goog/async/animationdelay_test.js

raw
1 // Copyright 2012 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.module('goog.async.AnimationDelayTest');
16 goog.setTestOnly('goog.async.AnimationDelayTest');
17
18 var AnimationDelay = goog.require('goog.async.AnimationDelay');
19 var Promise = goog.require('goog.Promise');
20 var PropertyReplacer = goog.require('goog.testing.PropertyReplacer');
21 var Timer = goog.require('goog.Timer');
22 var jsunit = goog.require('goog.testing.jsunit');
23 var testSuite = goog.require('goog.testing.testSuite');
24
25 var TEST_DELAY = 50;
26 var stubs = new PropertyReplacer();
27
28 testSuite({
29 tearDown: function() { stubs.reset(); },
30
31 testStart: function() {
32 var resolver = Promise.withResolver();
33 var start = goog.now();
34 var delay = new AnimationDelay(function(end) {
35 assertNotNull(resolver); // fail if called multiple times
36 resolver.resolve();
37 resolver = null;
38 });
39
40 delay.start();
41
42 return resolver.promise;
43 },
44
45 testStop: function() {
46 var resolver = Promise.withResolver();
47 var start = goog.now();
48 var delay = new AnimationDelay(function(end) { resolver.reject(); });
49
50 delay.start();
51 delay.stop();
52
53 return Timer.promise(TEST_DELAY).then(function() {
54 resolver.resolve();
55 return resolver.promise;
56 });
57 },
58
59 testAlwaysUseGoogNowForHandlerTimestamp: function() {
60 var resolver = Promise.withResolver();
61 var expectedValue = 12345.1;
62 stubs.set(goog, 'now', function() { return expectedValue; });
63
64 var delay = new AnimationDelay(function(timestamp) {
65 assertEquals(expectedValue, timestamp);
66 resolver.resolve();
67 });
68
69 delay.start();
70
71 return resolver.promise;
72 },
73
74 testStartIfActive: function() {
75 var delay = new AnimationDelay(goog.nullFunction);
76 delay.start();
77
78 var startWasCalled = false;
79 stubs.set(AnimationDelay.prototype, 'start', function() {
80 startWasCalled = true;
81 });
82
83 delay.startIfNotActive();
84 assertEquals(startWasCalled, false);
85 delay.stop();
86 delay.startIfNotActive();
87 assertEquals(startWasCalled, true);
88 }
89 });