incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fil...@apache.org
Subject [4/7] js commit: [CB-466] refactoring accelerometer JS
Date Fri, 18 May 2012 22:11:21 GMT
[CB-466] refactoring accelerometer JS


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/f4e00d94
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/f4e00d94
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/f4e00d94

Branch: refs/heads/master
Commit: f4e00d943c1fe6eecab9d4988992d3d9001fb044
Parents: b1673e3
Author: Fil Maj <maj.fil@gmail.com>
Authored: Fri May 11 16:17:48 2012 -0700
Committer: Fil Maj <maj.fil@gmail.com>
Committed: Fri May 18 15:14:37 2012 -0700

----------------------------------------------------------------------
 lib/common/plugin/Acceleration.js  |   10 +-
 lib/common/plugin/accelerometer.js |   49 +++--------
 test/test.accelerometer.js         |  146 ++++++++++---------------------
 3 files changed, 63 insertions(+), 142 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/f4e00d94/lib/common/plugin/Acceleration.js
----------------------------------------------------------------------
diff --git a/lib/common/plugin/Acceleration.js b/lib/common/plugin/Acceleration.js
index 99a319e..93c7a19 100644
--- a/lib/common/plugin/Acceleration.js
+++ b/lib/common/plugin/Acceleration.js
@@ -1,8 +1,8 @@
 var Acceleration = function(x, y, z, timestamp) {
-  this.x = x;
-  this.y = y;
-  this.z = z;
-  this.timestamp = timestamp || (new Date()).getTime();
+    this.x = x;
+    this.y = y;
+    this.z = z;
+    this.timestamp = timestamp || (new Date()).getTime();
 };
 
-module.exports = Acceleration;
\ No newline at end of file
+module.exports = Acceleration;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/f4e00d94/lib/common/plugin/accelerometer.js
----------------------------------------------------------------------
diff --git a/lib/common/plugin/accelerometer.js b/lib/common/plugin/accelerometer.js
index 29ab721..c2528c4 100644
--- a/lib/common/plugin/accelerometer.js
+++ b/lib/common/plugin/accelerometer.js
@@ -3,9 +3,10 @@
  * @constructor
  */
 var utils = require("cordova/utils"),
-    exec = require("cordova/exec");
+    exec = require("cordova/exec"),
 
-// Local singleton variables.
+
+// Keeps reference to watchAcceleration calls.
 var timers = {};
 
 var accelerometer = {
@@ -20,14 +21,7 @@ var accelerometer = {
 
         // successCallback required
         if (typeof successCallback !== "function") {
-            console.log("Accelerometer Error: successCallback is not a function");
-            return;
-        }
-
-        // errorCallback optional
-        if (errorCallback && (typeof errorCallback !== "function")) {
-            console.log("Accelerometer Error: errorCallback is not a function");
-            return;
+            throw "getCurrentAcceleration must be called with at least a success callback
function as first parameter.";
         }
 
         // Get acceleration
@@ -45,34 +39,18 @@ var accelerometer = {
     watchAcceleration: function(successCallback, errorCallback, options) {
 
         // Default interval (10 sec)
-        var frequency = (options !== undefined && options.frequency !== undefined)?
options.frequency : 10000;
+        var frequency = (options && options.frequency && typeof options.frequency
== 'number') ? options.frequency : 10000;
 
         // successCallback required
         if (typeof successCallback !== "function") {
-            console.log("Accelerometer Error: successCallback is not a function");
-            return;
-        }
-
-        // errorCallback optional
-        if (errorCallback && (typeof errorCallback !== "function")) {
-            console.log("Accelerometer Error: errorCallback is not a function");
-            return;
+            throw "watchAcceleration must be called with at least a success callback function
as first parameter.";
         }
 
-        // Make sure accelerometer timeout > frequency + 10 sec
-        exec(
-            function(timeout) {
-                if (timeout < (frequency + 10000)) {
-                    exec(null, null, "Accelerometer", "setTimeout", [frequency + 10000]);
-                }
-            },
-            function(e) { }, "Accelerometer", "getTimeout", []);
-
-        // Start watch timer
+        // Keep reference to watch id
         var id = utils.createUUID();
-        timers[id] = window.setInterval(function() {
-            exec(successCallback, errorCallback, "Accelerometer", "getAcceleration", []);
-        }, (frequency ? frequency : 1));
+        timers[id] = true;
+
+        exec(successCallback, errorCallback, "Accelerometer", "addWatch", [id, frequency]);
 
         return id;
     },
@@ -83,13 +61,12 @@ var accelerometer = {
      * @param {String} id       The id of the watch returned from #watchAcceleration.
      */
     clearWatch: function(id) {
-
         // Stop javascript timer & remove from timer list
-        if (id && timers[id] !== undefined) {
-            window.clearInterval(timers[id]);
+        if (id && timers[id]) {
             delete timers[id];
+            exec(null, null, "Accelerometer", "clearWatch", [id]);
         }
     }
 };
 
-module.exports = accelerometer;
\ No newline at end of file
+module.exports = accelerometer;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/f4e00d94/test/test.accelerometer.js
----------------------------------------------------------------------
diff --git a/test/test.accelerometer.js b/test/test.accelerometer.js
index ed5f86e..cef4f2b 100644
--- a/test/test.accelerometer.js
+++ b/test/test.accelerometer.js
@@ -2,28 +2,26 @@ describe("accelerometer", function () {
     var accelerometer = require("cordova/plugin/accelerometer"),
         exec = require('cordova/exec');
 
-    describe("when getting the current acceleration", function () {
-        describe("when passing in bad data", function () {
-            it("logs the error message when missing the successCallback", function () {
-                spyOn(console, "log");
-                accelerometer.getCurrentAcceleration();
-                expect(console.log).toHaveBeenCalledWith("Accelerometer Error: successCallback
is not a function");
-            });
-
-            it("logs the error message when the success callback isn't a function", function
() {
-                spyOn(console, "log");
-                accelerometer.getCurrentAcceleration("ponies");
-                expect(console.log).toHaveBeenCalledWith("Accelerometer Error: successCallback
is not a function");
-            });
+    beforeEach(function() {
+        exec.reset();
+    });
 
-            it("logs the error message the error callback isn't a function", function ()
{
-                spyOn(console, "log");
-                accelerometer.getCurrentAcceleration(jasmine.createSpy(), "rainbows");
-                expect(console.log).toHaveBeenCalledWith("Accelerometer Error: errorCallback
is not a function");
+    describe("getCurrentAcceleration", function () {
+        describe("failure", function () {
+            it("should throw exception if bad success callback passed in", function () {
+                expect(function() {
+                    accelerometer.getCurrentAcceleration();
+                }).toThrow();
+                expect(function() {
+                    accelerometer.getCurrentAcceleration(null);
+                }).toThrow();
+                expect(function() {
+                    accelerometer.getCurrentAcceleration({call:function(){}, apply:function(){}});
+                }).toThrow();
             });
         });
 
-        it("calls the exec method", function () {
+        it("should call the exec method", function () {
             var success = function () {},
                 error = function () {};
 
@@ -32,112 +30,58 @@ describe("accelerometer", function () {
         });
     });
 
-    describe("when watching the acceleration", function () {
-        describe("when passing in bad data", function () {
-            it("logs the error message when missing the successCallback", function () {
-                spyOn(console, "log");
-                accelerometer.watchAcceleration();
-                expect(console.log).toHaveBeenCalledWith("Accelerometer Error: successCallback
is not a function");
-            });
-
-            it("logs the error message when the success callback isn't a function", function
() {
-                spyOn(console, "log");
-                accelerometer.watchAcceleration("ponies");
-                expect(console.log).toHaveBeenCalledWith("Accelerometer Error: successCallback
is not a function");
-            });
-
-            it("logs the error message the error callback isn't a function", function ()
{
-                spyOn(console, "log");
-                accelerometer.watchAcceleration(jasmine.createSpy(), "rainbows");
-                expect(console.log).toHaveBeenCalledWith("Accelerometer Error: errorCallback
is not a function");
+    describe("watchAcceleration", function () {
+        describe("failure", function () {
+            it("throws an exception if bad successCallback", function () {
+                expect(function() {
+                    accelerometer.watchAcceleration();
+                }).toThrow();
+                expect(function() {
+                    accelerometer.watchAcceleration(null);
+                }).toThrow();
+                expect(function() {
+                    accelerometer.watchAcceleration({call:function(){}, apply:function(){}});
+                }).toThrow();
             });
         });
 
-        describe("when working with the timeout", function () {
-            it("calls exec to get the timeout", function () {
-                var success = jasmine.createSpy(),
-                    fail = jasmine.createSpy();
-
-                spyOn(window, "setInterval");
-                accelerometer.watchAcceleration(success, fail);
-
-                expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function),
"Accelerometer", "getTimeout", []);
-            });
-
-            it("it sets it to 10 seconds greater than the frequency", function () {
+        describe('success', function() {
+            it("should call exec with a provided frequency", function () {
                 var success = jasmine.createSpy(),
                     fail = jasmine.createSpy();
 
-                spyOn(window, "setInterval");
-                accelerometer.watchAcceleration(success, fail, {frequency: 5000});
-
-                //execute the success callback ;)
-                exec.mostRecentCall.args[0](5000);
+                accelerometer.watchAcceleration(success, fail, {frequency: 11});
 
-                expect(exec).toHaveBeenCalledWith(null, null, "Accelerometer", "setTimeout",
[15000]);
+                expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function),
"Accelerometer", "addWatch", [jasmine.any(String), 11]);
             });
 
-            it("doesn't set it if timeout is less than freq + 10sec", function () {
+            it("should call exec with default frequency if no options provided", function
() {
                 var success = jasmine.createSpy(),
                     fail = jasmine.createSpy();
 
-                spyOn(window, "setInterval");
-                accelerometer.watchAcceleration(success, fail, {frequency: 1000});
-
-                //execute the success callback ;)
-                exec.mostRecentCall.args[0](20000);
+                accelerometer.watchAcceleration(success, fail);
 
-                expect(exec).not.toHaveBeenCalledWith(null, null, "Accelerometer", "setTimeout",
[11000]);
+                expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function),
"Accelerometer", "addWatch", [jasmine.any(String), 10000]);
             });
         });
-
-        it("starts the interval with the provided frequency", function () {
-            var success = jasmine.createSpy(),
-                fail = jasmine.createSpy();
-
-            spyOn(window, "setInterval");
-            accelerometer.watchAcceleration(success, fail, {frequency: 11});
-
-            expect(window.setInterval).toHaveBeenCalledWith(jasmine.any(Function), 11);
-        });
-
-        it("starts the interval with the default frequency", function () {
-            var success = jasmine.createSpy(),
-                fail = jasmine.createSpy();
-
-            spyOn(window, "setInterval");
-            accelerometer.watchAcceleration(success, fail);
-
-            expect(window.setInterval).toHaveBeenCalledWith(jasmine.any(Function), 10000);
-        });
-
-        it("gets the acceleration for the provided interval", function () {
-            var success = jasmine.createSpy(),
-                fail = jasmine.createSpy();
-
-            spyOn(window, "setInterval");
-            accelerometer.watchAcceleration(success, fail, {frequency: 11});
-
-            //exec the interval callback!
-            window.setInterval.mostRecentCall.args[0]();
-
-            expect(exec).toHaveBeenCalledWith(success, fail, "Accelerometer", "getAcceleration",
[]);
-        });
     });
 
     describe("when clearing the watch", function () {
-        beforeEach(function () {
-            spyOn(window, "clearInterval");
-        });
-
         it("doesn't clear anything if the timer doesn't exist", function () {
             accelerometer.clearWatch("Never Gonna Give you Up");
-            expect(window.clearInterval).not.toHaveBeenCalled();
+            expect(exec).not.toHaveBeenCalled();
         });
 
-        it("can be called with no args", function () {
+        it("doesnt invoke exec if no id provided", function () {
             accelerometer.clearWatch();
-            expect(window.clearInterval).not.toHaveBeenCalled();
+            expect(exec).not.toHaveBeenCalled();
+        });
+
+        it("invokes exec if watch exists", function() {
+            var id = accelerometer.watchAcceleration(function(){}, function(){});
+            exec.reset();
+            accelerometer.clearWatch(id);
+            expect(exec).toHaveBeenCalledWith(null, null, "Accelerometer", "clearWatch",
[id]);
         });
     });
 });


Mime
View raw message