You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2012/05/12 01:14:33 UTC
js commit: [CB-466] refactoring accelerometer JS
Updated Branches:
refs/heads/462 [created] e0096fc63
[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/e0096fc6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/e0096fc6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/e0096fc6
Branch: refs/heads/462
Commit: e0096fc63632a5b92240a59d81f807606d6150e2
Parents: 55e46ce
Author: Fil Maj <ma...@gmail.com>
Authored: Fri May 11 16:17:48 2012 -0700
Committer: Fil Maj <ma...@gmail.com>
Committed: Fri May 11 16:17:48 2012 -0700
----------------------------------------------------------------------
lib/bada/plugin/bada/Contacts.js | 18 ++--
lib/common/plugin/Acceleration.js | 10 +-
lib/common/plugin/accelerometer.js | 49 +++--------
test/test.accelerometer.js | 146 ++++++++++---------------------
4 files changed, 72 insertions(+), 151 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/e0096fc6/lib/bada/plugin/bada/Contacts.js
----------------------------------------------------------------------
diff --git a/lib/bada/plugin/bada/Contacts.js b/lib/bada/plugin/bada/Contacts.js
index c98ba80..586cb5d 100644
--- a/lib/bada/plugin/bada/Contacts.js
+++ b/lib/bada/plugin/bada/Contacts.js
@@ -7,7 +7,7 @@ var allowedFilters = ["firstName", "lastName", "phoneticName", "nickname", "phon
function _pgToWac(contact) {
var i, j;
var wacContact = {};
-
+
if(contact.id) {
wacContact.id = contact.id;
}
@@ -38,7 +38,7 @@ function _pgToWac(contact) {
}
}
}
-
+
// emails
if(contact.emails && contact.emails.length > 0) {
wacContact.emails = [];
@@ -88,11 +88,11 @@ function _pgToWac(contact) {
function _wacToPg(contact) {
var i, j;
var pgContact = {};
-
+
if(contact.id) {
pgContact.id = contact.id;
}
-
+
// name
if(contact.firstName || contact.lastName) {
pgContact.name = {};
@@ -100,12 +100,12 @@ function _wacToPg(contact) {
pgContact.name.familyName = contact.lastName;
pgContact.displayName = contact.firstName + ' ' + contact.lastName;
}
-
+
// nicknames
if(contact.nicknames && contact.nicknames.length > 0) {
pgContact.nickname = contact.nicknames[0];
}
-
+
// phoneNumbers
if(contact.phoneNumbers && contact.phoneNumbers.length > 0) {
pgContact.phoneNumbers = [];
@@ -122,7 +122,7 @@ function _wacToPg(contact) {
pgContact.phoneNumbers.push(pgPhoneNumber);
}
}
-
+
// emails
if(contact.emails && contact.emails.length > 0) {
pgContact.emails = [];
@@ -139,7 +139,7 @@ function _wacToPg(contact) {
pgContact.emails.push(pgEmailAddress);
}
}
-
+
// addresses
if(contact.addresses && contact.addresses.length > 0) {
pgContact.addresses = [];
@@ -160,7 +160,7 @@ function _wacToPg(contact) {
pgContact.addresses.push(pgAddress);
}
}
-
+
// photos
// can only store one photo URL
if(contact.photoURL) {
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/e0096fc6/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/e0096fc6/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/e0096fc6/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]);
});
});
});