Skip to content

Commit 599bc7c

Browse files
author
Vladimir Enchev
committed
timer tests and snippets added
1 parent 35b9159 commit 599bc7c

6 files changed

Lines changed: 153 additions & 1 deletion

File tree

BCL.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
<TypeScriptCompile Include="local-settings\local-settings-common.ts" />
201201
<TypeScriptCompile Include="location\location-common.ts" />
202202
<TypeScriptCompile Include="location\location-types.ts" />
203+
<TypeScriptCompile Include="Tests\timer-tests.ts" />
203204
<Content Include="_references.ts" />
204205
</ItemGroup>
205206
<ItemGroup>

Tests/testRunner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ allTests["HTTP"] = require("Tests/http-tests");
66
allTests["LOCATION"] = require("Tests/location-tests");
77
allTests["LOCAL SETTINGS"] = require("Tests/local-settings-tests");
88
allTests["IMAGE SOURCE"] = require("Tests/image-tests");
9+
allTests["TIMER"] = require("Tests/timer-tests");
910

1011
export var runAll = function (moduleName?: string) {
1112
for (var name in allTests) {

Tests/timer-tests.ts

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import TKUnit = require("Tests/TKUnit");
2+
var timer = require("timer/timer");
3+
4+
// <snippet name="timer">
5+
// # Timer module
6+
// ``` JavaScript
7+
// require("globals");
8+
//// OR
9+
// var timer = require("timer");
10+
// ```
11+
// </snippet>
12+
13+
export var test_setTimeout_isDefined = function () {
14+
TKUnit.assert(typeof (timer.setTimeout) !== "undefined", "Method timer.setTimeout() should be defined!");
15+
};
16+
17+
export var test_clearTimeout_isDefined = function () {
18+
TKUnit.assert(typeof (timer.clearTimeout) !== "undefined", "Method timer.clearTimeout() should be defined!");
19+
};
20+
21+
export var test_setInterval_isDefined = function () {
22+
TKUnit.assert(typeof (timer.setInterval) !== "undefined", "Method timer.setInterval() should be defined!");
23+
};
24+
25+
export var test_clearInterval_isDefined = function () {
26+
TKUnit.assert(typeof (timer.clearInterval) !== "undefined", "Method timer.clearInterval() should be defined!");
27+
};
28+
29+
export var test_setTimeout = function () {
30+
var completed: boolean;
31+
var isReady = function () { return completed; }
32+
33+
// <snippet name="timer">
34+
// ### Evaluates an expression after 0 milliseconds.
35+
// ``` JavaScript
36+
timer.setTimeout(function () {
37+
// <hide>
38+
completed = true;
39+
// </hide>
40+
});
41+
// ```
42+
// </snippet>
43+
44+
TKUnit.waitUntilReady(isReady, 0.5);
45+
TKUnit.assert(completed, "Callback should be called!");
46+
};
47+
48+
export var test_setTimeout_callbackCalledAfterSpecifedTime = function () {
49+
var completed: boolean;
50+
var isReady = function () { return completed; }
51+
52+
// <snippet name="timer">
53+
// ### Evaluates an expression after a specified number of milliseconds.
54+
// ``` JavaScript
55+
timer.setTimeout(function () {
56+
// <hide>
57+
completed = true;
58+
// </hide>
59+
}, 500);
60+
// ```
61+
// </snippet>
62+
63+
TKUnit.waitUntilReady(isReady, 1);
64+
TKUnit.assert(completed, "Callback should be called after specified time!");
65+
};
66+
67+
export var test_setTimeout_callbackNotCalled = function () {
68+
var completed: boolean;
69+
var isReady = function () { return completed; }
70+
71+
timer.setTimeout(function () {
72+
completed = true;
73+
}, 1000);
74+
75+
TKUnit.waitUntilReady(isReady, 0.5);
76+
TKUnit.assert(!completed, "Callback should be called after specified time!");
77+
};
78+
79+
export var test_setTimeout_shouldReturnNumber = function () {
80+
var id = timer.setTimeout(function () { });
81+
TKUnit.assert(typeof id === "number", "Callback should return number!");
82+
};
83+
84+
export var test_setTimeout_callbackShouldBeCleared = function () {
85+
var completed: boolean;
86+
var isReady = function () { return completed; }
87+
88+
// <snippet name="timer">
89+
// ### Cancels the evaluation with the clearTimeout method.
90+
// ``` JavaScript
91+
var id = timer.setTimeout(function () {
92+
// <hide>
93+
completed = true;
94+
// </hide>
95+
}, 2000);
96+
97+
//// Clear timeout with specified id.
98+
timer.clearTimeout(id);
99+
100+
// ```
101+
// </snippet>
102+
103+
TKUnit.waitUntilReady(isReady, 3);
104+
TKUnit.assert(!completed, "Callback should be cleared when clearTimeout() is executed for specified id!");
105+
};
106+
107+
export var test_setInterval_callbackCalledDuringPeriod = function () {
108+
var counter = 0;
109+
var expected = 4;
110+
var isReady = function () { return counter >= expected; }
111+
112+
// <snippet name="timer">
113+
// ### Evaluates an expression each time a specified number of milliseconds has elapsed.
114+
// ``` JavaScript
115+
timer.setInterval(function () {
116+
// <hide>
117+
counter++;
118+
// </hide>
119+
}, 100);
120+
// ```
121+
// </snippet>
122+
123+
TKUnit.waitUntilReady(isReady, 0.5);
124+
TKUnit.assert(isReady(), "Callback should be raised at least" + expected + "times! Callback raised " + counter + " times.");
125+
};
126+
127+
export var test_setInterval_callbackShouldBeCleared = function () {
128+
var counter = 0;
129+
var expected = 1;
130+
var isReady = function () { return counter == expected; }
131+
132+
// <snippet name="timer">
133+
// ### Cancel the interval previously started using the setInterval method.
134+
// ``` JavaScript
135+
var id = timer.setInterval(function () {
136+
// <hide>
137+
counter++;
138+
// </hide>
139+
timer.clearInterval(id);
140+
}, 100);
141+
// ```
142+
// </snippet>
143+
144+
TKUnit.waitUntilReady(isReady, 0.5);
145+
TKUnit.assert(isReady(), "Callback should be raised only once!");
146+
};

globals/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import consoleModule = require("console/console");
55
setTimeout = timer.setTimeout;
66
clearTimeout = timer.clearTimeout;
77
setInterval = timer.setInterval;
8-
clearInterval = timer.clearTimeout;
8+
clearInterval = timer.clearInterval;
99

1010
console = new consoleModule.Console();

timer/timer.android.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,5 @@ export function setInterval(callback: Function, milliseconds = 0): number {
5656

5757
return id;
5858
}
59+
60+
export var clearInterval = clearTimeout;

timer/timer.ios.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ export function clearTimeout(id: number): void {
3030
export function setInterval(callback: Function, milliseconds = 0): number {
3131
return createTimerAndGetId(callback, milliseconds, true);
3232
}
33+
34+
export var clearInterval = clearTimeout;

0 commit comments

Comments
 (0)