[CB-462] more refactoring work for the accelerometer.
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/59ba352c Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/59ba352c Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/59ba352c Branch: refs/heads/master Commit: 59ba352cd595a6ea98ed89542266b381f6981df2 Parents: f4e00d9 Author: Fil Maj <maj....@gmail.com> Authored: Mon May 14 13:02:38 2012 -0700 Committer: Fil Maj <maj....@gmail.com> Committed: Fri May 18 15:14:37 2012 -0700 ---------------------------------------------------------------------- lib/common/plugin/accelerometer.js | 36 +++++++++++++++++++++++++----- test/test.accelerometer.js | 35 +++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/59ba352c/lib/common/plugin/accelerometer.js ---------------------------------------------------------------------- diff --git a/lib/common/plugin/accelerometer.js b/lib/common/plugin/accelerometer.js index c2528c4..7d49b15 100644 --- a/lib/common/plugin/accelerometer.js +++ b/lib/common/plugin/accelerometer.js @@ -4,11 +4,15 @@ */ var utils = require("cordova/utils"), exec = require("cordova/exec"), + Acceleration = require('cordova/plugin/Acceleration'); // Keeps reference to watchAcceleration calls. var timers = {}; +// Last returned acceleration object from native +var accel = null; + var accelerometer = { /** * Asynchronously aquires the current acceleration. @@ -18,14 +22,18 @@ var accelerometer = { * @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. (OPTIONAL) */ getCurrentAcceleration: function(successCallback, errorCallback, options) { - // successCallback required if (typeof successCallback !== "function") { throw "getCurrentAcceleration must be called with at least a success callback function as first parameter."; } + var win = function(a) { + accel = new Acceleration(a.x, a.y, a.z, a.timestamp); + successCallback(accel); + }; + // Get acceleration - exec(successCallback, errorCallback, "Accelerometer", "getAcceleration", []); + exec(win, errorCallback, "Accelerometer", "getAcceleration", []); }, /** @@ -37,7 +45,6 @@ var accelerometer = { * @return String The watch id that must be passed to #clearWatch to stop watching. */ watchAcceleration: function(successCallback, errorCallback, options) { - // Default interval (10 sec) var frequency = (options && options.frequency && typeof options.frequency == 'number') ? options.frequency : 10000; @@ -46,11 +53,27 @@ var accelerometer = { throw "watchAcceleration must be called with at least a success callback function as first parameter."; } - // Keep reference to watch id + // Keep reference to watch id, and report accel readings as often as defined in frequency var id = utils.createUUID(); - timers[id] = true; + timers[id] = window.setInterval(function() { + if (accel) { + successCallback(accel); + } + }, frequency); + + // Success callback from native just updates the accel object. + var win = function(a) { + console.log('inside watchaccel win, ' + JSON.stringify(a)); + accel = new Acceleration(a.x, a.y, a.z, a.timestamp); + }; + + // Fail callback clears the watch and sends an error back. + var fail = function(err) { + accelerometer.clearWatch(id); + errorCallback(err); + }; - exec(successCallback, errorCallback, "Accelerometer", "addWatch", [id, frequency]); + exec(win, fail, "Accelerometer", "addWatch", [id, frequency]); return id; }, @@ -63,6 +86,7 @@ var accelerometer = { clearWatch: function(id) { // Stop javascript timer & remove from timer list if (id && timers[id]) { + window.clearInterval(timers[id]); delete timers[id]; exec(null, null, "Accelerometer", "clearWatch", [id]); } http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/59ba352c/test/test.accelerometer.js ---------------------------------------------------------------------- diff --git a/test/test.accelerometer.js b/test/test.accelerometer.js index cef4f2b..8e8ad38 100644 --- a/test/test.accelerometer.js +++ b/test/test.accelerometer.js @@ -26,7 +26,7 @@ describe("accelerometer", function () { error = function () {}; accelerometer.getCurrentAcceleration(success, error, "options"); - expect(exec).toHaveBeenCalledWith(success, error, "Accelerometer", "getAcceleration", []); + expect(exec).toHaveBeenCalledWith(jasmine.any(Function), error, "Accelerometer", "getAcceleration", []); }); }); @@ -63,6 +63,39 @@ describe("accelerometer", function () { expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function), "Accelerometer", "addWatch", [jasmine.any(String), 10000]); }); + + it("should set a timer with the provided frequency", function() { + spyOn(window, "setInterval"); + + accelerometer.watchAcceleration(function(){}, function(){}, {frequency:50}); + + expect(window.setInterval).toHaveBeenCalledWith(jasmine.any(Function), 50); + }); + + it("should not fire the timer until the framework sends back an acceleration reading", function() { + var success = jasmine.createSpy(); + + runs(function() { + accelerometer.watchAcceleration(success, function(){}, {frequency:50}); + }); + + waits(51); + + runs(function() { + expect(success).not.toHaveBeenCalled(); + }); + + runs(function() { + // "fake" native returning an accel reading + exec.mostRecentCall.args[0]({x:1, y:2, z:3, timestamp:100}); + }); + + waits(51); + + runs(function() { + expect(success).toHaveBeenCalled(); + }); + }); }); });