diff --git a/BCL.csproj b/BCL.csproj index 89e8cb413..6d92cd7ed 100644 --- a/BCL.csproj +++ b/BCL.csproj @@ -200,6 +200,7 @@ + diff --git a/Tests/testRunner.ts b/Tests/testRunner.ts index fa360024d..517985b6e 100644 --- a/Tests/testRunner.ts +++ b/Tests/testRunner.ts @@ -6,6 +6,7 @@ allTests["HTTP"] = require("Tests/http-tests"); allTests["LOCATION"] = require("Tests/location-tests"); allTests["LOCAL SETTINGS"] = require("Tests/local-settings-tests"); allTests["IMAGE SOURCE"] = require("Tests/image-tests"); +allTests["TIMER"] = require("Tests/timer-tests"); export var runAll = function (moduleName?: string) { for (var name in allTests) { diff --git a/Tests/timer-tests.ts b/Tests/timer-tests.ts new file mode 100644 index 000000000..03b665e51 --- /dev/null +++ b/Tests/timer-tests.ts @@ -0,0 +1,146 @@ +import TKUnit = require("Tests/TKUnit"); +var timer = require("timer/timer"); + +// +// # Timer module +// ``` JavaScript +// require("globals"); +//// OR +// var timer = require("timer"); +// ``` +// + +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; } + + // + // ### Evaluates an expression after 0 milliseconds. + // ``` JavaScript + timer.setTimeout(function () { + // + completed = true; + // + }); + // ``` + // + + TKUnit.waitUntilReady(isReady, 0.5); + TKUnit.assert(completed, "Callback should be called!"); +}; + +export var test_setTimeout_callbackCalledAfterSpecifedTime = function () { + var completed: boolean; + var isReady = function () { return completed; } + + // + // ### Evaluates an expression after a specified number of milliseconds. + // ``` JavaScript + timer.setTimeout(function () { + // + completed = true; + // + }, 500); + // ``` + // + + 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 () { + var completed: boolean; + var isReady = function () { return completed; } + + // + // ### Cancels the evaluation with the clearTimeout method. + // ``` JavaScript + var id = timer.setTimeout(function () { + // + completed = true; + // + }, 2000); + + //// Clear timeout with specified id. + timer.clearTimeout(id); + + // ``` + // + + 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; } + + // + // ### Evaluates an expression each time a specified number of milliseconds has elapsed. + // ``` JavaScript + timer.setInterval(function () { + // + counter++; + // + }, 100); + // ``` + // + + 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; } + + // + // ### Cancel the interval previously started using the setInterval method. + // ``` JavaScript + var id = timer.setInterval(function () { + // + counter++; + // + timer.clearInterval(id); + }, 100); + // ``` + // + + TKUnit.waitUntilReady(isReady, 0.5); + TKUnit.assert(isReady(), "Callback should be raised only once!"); +}; diff --git a/globals/index.ts b/globals/index.ts index b98961fb8..eb104f54f 100644 --- a/globals/index.ts +++ b/globals/index.ts @@ -5,6 +5,6 @@ import consoleModule = require("console/console"); setTimeout = timer.setTimeout; clearTimeout = timer.clearTimeout; setInterval = timer.setInterval; -clearInterval = timer.clearTimeout; +clearInterval = timer.clearInterval; console = new consoleModule.Console(); \ No newline at end of file diff --git a/timer/timer.android.ts b/timer/timer.android.ts index aaee3b446..954721bce 100644 --- a/timer/timer.android.ts +++ b/timer/timer.android.ts @@ -56,3 +56,5 @@ export function setInterval(callback: Function, milliseconds = 0): number { return id; } + +export var clearInterval = clearTimeout; \ No newline at end of file diff --git a/timer/timer.ios.ts b/timer/timer.ios.ts index 7a25e57ee..9112d2ae6 100644 --- a/timer/timer.ios.ts +++ b/timer/timer.ios.ts @@ -30,3 +30,5 @@ export function clearTimeout(id: number): void { export function setInterval(callback: Function, milliseconds = 0): number { return createTimerAndGetId(callback, milliseconds, true); } + +export var clearInterval = clearTimeout; \ No newline at end of file