views:

682

answers:

3

Are there any easy ways to override the default behaviors of the geolocation api and just hard code your current location?

I think this would be useful for testing and for privacy reasons (providing fake location data)

I thought there was an add on for this but I can't seem to find one. Only option right now seems to be changing the about:config geo.wifi.url to some alternative webservice, which I consider overly complicated.

Any ideas?

Thanks

Ideal Scenario

Somebody implements an add-on where a google map appears and I can pick a new default location.

A: 

Hmmm, you can't mock navigator.geolocation directly - perhaps you can refactor your code that uses it to use another object - say customGeoLocation.

In production you can just set customGeoLocation to navigator.geolocation and in tests use a mock implementation of whatever functionality you use.

EDIT: Turns out you can replace functions on navigator.geolocation object but that is still useless if you are using a mock library (like JsMock) which needs to create a mock object. So you wouldn't be able to replace navigator.geolocation with a mock.

You can do the mocking yourself in this fashion:

var getCurrentPositionCalled = false;
navigator.geolocation.getCurrentPosition = function() { 
  getCurrentPositionCalled = true; 
};

//Your app code here
//...
assert(getCurrentPositionCalled);
Igor Zevaka
"you can't mock navigator.geolocation". Incorrect. Sample forthcoming....
Roatin Marth
Fair enough, see edit.
Igor Zevaka
+6  A: 

The geo.wifi.url does not need to be a webservice. You can also set it to a local uri with file://...

The file should be a json file with content like this:

{"location": {
              "latitude": 48.777025000000002, 
              "longitude": 9.1713909999999998, 
              "accuracy": 10.0}}
Peter Hoffmann
+1  A: 

Geolocater is an experimental add-on that lets you edit your geolocation.

Slothman