forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer-tests.ts
More file actions
156 lines (129 loc) · 4.65 KB
/
timer-tests.ts
File metadata and controls
156 lines (129 loc) · 4.65 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import TKUnit = require("./TKUnit");
import platform = require("platform");
var timer = require("timer/timer");
// <snippet module="timer" title="timer">
// # Timer module
// ### How to require timer module
// ``` JavaScript
// require("globals");
//// OR
// var timer = require("timer");
// ```
// </snippet>
export var test_setTimeout_isDefined = function () {
TKUnit.assert(typeof (timer.setTimeout) !== "undefined", "Method timer.setTimeout() should be defined!");
};
export var test_clearTimeout_isDefined = function () {
TKUnit.assert(typeof (timer.clearTimeout) !== "undefined", "Method timer.clearTimeout() should be defined!");
};
export var test_setInterval_isDefined = function () {
TKUnit.assert(typeof (timer.setInterval) !== "undefined", "Method timer.setInterval() should be defined!");
};
export var test_clearInterval_isDefined = function () {
TKUnit.assert(typeof (timer.clearInterval) !== "undefined", "Method timer.clearInterval() should be defined!");
};
export var test_setTimeout = function () {
var completed: boolean;
var isReady = function () { return completed; }
// <snippet module="timer" title="timer">
// ### Evaluates an expression after 0 milliseconds.
// ``` JavaScript
timer.setTimeout(function () {
// <hide>
completed = true;
// </hide>
});
// ```
// </snippet>
TKUnit.waitUntilReady(isReady, 0.5);
TKUnit.assert(completed, "Callback should be called!");
};
export var test_setTimeout_callbackCalledAfterSpecifiedTime = function () {
var completed: boolean;
var isReady = function () { return completed; }
// <snippet module="timer" title="timer">
// ### Evaluates an expression after a specified number of milliseconds.
// ``` JavaScript
timer.setTimeout(function () {
// <hide>
completed = true;
// </hide>
}, 500);
// ```
// </snippet>
TKUnit.waitUntilReady(isReady, 1);
TKUnit.assert(completed, "Callback should be called after specified time!");
};
export var test_setTimeout_callbackNotCalled = function () {
var completed: boolean;
var isReady = function () { return completed; }
timer.setTimeout(function () {
completed = true;
}, 1000);
TKUnit.waitUntilReady(isReady, 0.5);
TKUnit.assert(!completed, "Callback should be called after specified time!");
};
export var test_setTimeout_shouldReturnNumber = function () {
var id = timer.setTimeout(function () {
//
});
TKUnit.assert(typeof id === "number", "Callback should return number!");
};
export var test_setTimeout_callbackShouldBeCleared = function () {
// This test is very unstable in iOS, because the platform does not guarantee the
// callback will be cleared on time. Better skip it for iOS.
if (platform.device.os === platform.platformNames.ios) {
return;
}
var completed: boolean;
var isReady = function () { return completed; }
// <snippet module="timer" title="timer">
// ### Cancels the evaluation with the clearTimeout method.
// ``` JavaScript
var id = timer.setTimeout(function () {
// <hide>
completed = true;
// </hide>
}, 2000);
//// Clear timeout with specified id.
timer.clearTimeout(id);
// ```
// </snippet>
TKUnit.waitUntilReady(isReady, 3);
TKUnit.assert(!completed, "Callback should be cleared when clearTimeout() is executed for specified id!");
};
export var test_setInterval_callbackCalledDuringPeriod = function () {
var counter = 0;
var expected = 4;
var isReady = function () { return counter >= expected; }
// <snippet module="timer" title="timer">
// ### Evaluates an expression each time a specified number of milliseconds has elapsed.
// ``` JavaScript
timer.setInterval(function () {
// <hide>
counter++;
// </hide>
}, 100);
// ```
// </snippet>
TKUnit.waitUntilReady(isReady, 0.5);
TKUnit.assert(isReady(), "Callback should be raised at least" + expected + "times! Callback raised " + counter + " times.");
};
export var test_setInterval_callbackShouldBeCleared = function () {
var counter = 0;
var expected = 1;
var isReady = function () { return counter === expected; }
// <snippet module="timer" title="timer">
// ### Cancel the interval previously started using the setInterval method.
// ``` JavaScript
var id = timer.setInterval(function () {
// <hide>
counter++;
// </hide>
timer.clearInterval(id);
}, 100);
// ```
// </snippet>
TKUnit.waitUntilReady(isReady, 0.5);
TKUnit.assert(isReady(), "Callback should be raised only once!");
};