further reworking geolocation
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/0d7e3e7b Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/0d7e3e7b Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/0d7e3e7b Branch: refs/heads/master Commit: 0d7e3e7b1524b6d003c743b1ebecfd21e05a7317 Parents: 97aa789 Author: Fil Maj <maj....@gmail.com> Authored: Sat Mar 24 12:54:14 2012 -0700 Committer: Fil Maj <maj....@gmail.com> Committed: Mon May 7 13:51:05 2012 -0700 ---------------------------------------------------------------------- lib/common/plugin/geolocation.js | 3 - test/test.geolocation.js | 85 +++++++++++++++----------------- 2 files changed, 40 insertions(+), 48 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0d7e3e7b/lib/common/plugin/geolocation.js ---------------------------------------------------------------------- diff --git a/lib/common/plugin/geolocation.js b/lib/common/plugin/geolocation.js index 2cc7330..0831d0c 100644 --- a/lib/common/plugin/geolocation.js +++ b/lib/common/plugin/geolocation.js @@ -129,9 +129,6 @@ var geolocation = { options = parseParameters(options); var id = utils.createUUID(); - timers[id] = window.setInterval(function() { - geolocation.getCurrentPosition(successCallback, errorCallback, options); - }, options.timeout); return id; }, http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/0d7e3e7b/test/test.geolocation.js ---------------------------------------------------------------------- diff --git a/test/test.geolocation.js b/test/test.geolocation.js index dbd04cf..6daaa4c 100644 --- a/test/test.geolocation.js +++ b/test/test.geolocation.js @@ -51,7 +51,7 @@ describe("geolocation", function () { it("should throw an exception if used with no arguments", function() { expect(function() { geo.getCurrentPosition();}).toThrow("getCurrentPosition must be called with at least one argument."); }); - }) + }); describe("position acquisition", function() { it("should provide a cached position if one exists and has a timestamp value conforming with passed in maximumAge", function() { // Create a date object from 2 seconds ago to store as cached position. @@ -149,60 +149,55 @@ describe("geolocation", function () { }); }); - describe("when watching the position", function () { + describe("watchPosition", function () { var utils = require('cordova/utils'); - beforeEach(function () { - spyOn(window, "setInterval").andReturn("2"); - spyOn(utils, "createUUID").andReturn("leeroy jenkins"); - }); + describe("arguments", function () { + it("uses default PositionOptions if none are specified", function () { + geo.watchPosition(s, e); + expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function), "Geolocation", "getLocation", [false, Infinity, 0]); + }); - it("gets and returns an id from createUUID", function () { - var id = geo.watchPosition(s, e); - expect(utils.createUUID).toHaveBeenCalled(); - expect(id).toBe("leeroy jenkins"); - }); + it("uses the maximumAge option if specified", function () { + geo.watchPosition(s, e, {maximumAge: 10}); + expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function), "Geolocation", "getLocation", [false, Infinity, 10]); + }); - it("sets an interval for the default timeout", function () { - geo.watchPosition(s, e); - expect(window.setInterval).toHaveBeenCalledWith(jasmine.any(Function), Infinity); - }); + it("uses the enableHighAccuracy option if specified", function () { + geo.watchPosition(s, e, {enableHighAccuracy: true, maximumAge: 100}); + expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function), "Geolocation", "getLocation", [true, Infinity, 100]); + }); - it("sets an interval for the provided timeout", function () { - geo.watchPosition(s, e, {timeout: 10}); - expect(window.setInterval).toHaveBeenCalledWith(jasmine.any(Function), 10); - }); + it("uses the timeout option if specified and positive", function () { + geo.watchPosition(s, e, {timeout: 1000}); + expect(exec).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function), "Geolocation", "getLocation", [false, 1000, 0]); + }); - it("calls exec on the given interval", function () { - geo.watchPosition(s, e); - var func = window.setInterval.mostRecentCall.args[0]; - func(); - expect(exec).toHaveBeenCalled(); - }); - }); + it("uses a timeout value of 0 if specified and negative, which should call the error callback immediately (since we have no cached position)", function () { + geo.watchPosition(s, e, {timeout: -1000}); + expect(e).toHaveBeenCalledWith({ + code:PositionError.TIMEOUT, + message:"timeout value in PositionOptions set to 0 and no cached Position object available, or cached Position object's age exceed's provided PositionOptions' maximumAge parameter." + }); + }); - describe("when clearing the watch", function () { - beforeEach(function () { - spyOn(window, "setInterval").andReturn("woohoo"); - spyOn(window, "clearInterval"); - }); + it("can be used with one, two or three arguments", function() { + expect(function() { geo.watchPosition(s); }).not.toThrow(); + expect(function() { geo.watchPosition(s,e); }).not.toThrow(); + expect(function() { geo.watchPosition(s,e,{}); }).not.toThrow(); + }); - it("calls clear interval for the watch", function () { - var timer = geo.watchPosition(s, e); - geo.clearWatch(timer); - expect(window.clearInterval).toHaveBeenCalledWith("woohoo"); + it("should throw an exception if used with no arguments", function() { + expect(function() { geo.watchPosition();}).toThrow("watchPosition must be called with at least one argument."); + }); }); - - it("only calls clear interval once", function () { - var timer = geo.watchPosition(s, e); - geo.clearWatch(timer); - geo.clearWatch(timer); - expect(window.clearInterval.callCount).toBe(1); + describe("position acquisition", function() { + it("should invoke the success callback every time the position changes"); + it("should invoke the error callback if position could not be retrieved"); + }); + }); - it("doesn't call clear interval when no watch", function () { - geo.clearWatch("derp"); - expect(window.clearInterval).not.toHaveBeenCalled(); - }); + describe("clearWatch", function () { }); });