VulpesOS/gaia/shared/test/unit/mocks/mock_geolocation.js
OverSu b38dea0597 Publish Vulpes Preview 15 sources for desktop, Android and Tundra
Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
2026-09-30 23:13:03 +02:00

68 lines
1.8 KiB
JavaScript
Executable file

/* exported MockGeolocation */
'use strict';
var MockGeolocation = {
mSetup: function() {
this.fakeCoords = {
latitude: 37.388590,
longitude: -122.061704,
accuracy: 5.0
};
this.fakePosition = {
timestamp: 1404756850457,
coords: this.fakeCoords
};
this.activeWatches = [];
this.realGetCurrentPosition = navigator.geolocation.getCurrentPosition;
navigator.geolocation.getCurrentPosition = (function(onsuccess, onerror) {
if (onsuccess) {
onsuccess(this.createFakePosition());
}
}).bind(this);
this.realWatchPosition = navigator.geolocation.watchPosition;
navigator.geolocation.watchPosition = (function(onsuccess, onerror) {
var watch = setInterval(function() {
if (onsuccess) {
onsuccess(this.createFakePosition());
}
}.bind(this), 4);
this.activeWatches.push(watch);
return watch;
}).bind(this);
this.realClearWatch = navigator.geolocation.clearWatch;
navigator.geolocation.clearWatch = (function(watch) {
var idx = this.activeWatches.indexOf(watch);
if (idx >= 0) {
this.activeWatches.splice(idx, 1);
clearInterval(watch);
}
}).bind(this);
this.createFakePosition = function() {
this.fakePosition = {
timestamp: Date.now(),
coords: this.fakeCoords
};
return this.fakePosition;
};
},
mTeardown: function() {
for (var i = 0; i < this.activeWatches.length; i++) {
clearInterval(this.activeWatches[i]);
}
this.activeWatches = [];
navigator.geolocation.getCurrentPosition = this.realGetCurrentPosition;
navigator.geolocation.watchPosition = this.realWatchPosition;
navigator.geolocation.clearWatch = this.realClearWatch;
}
};