Skip to content

Commit

Permalink
add fit to accuracy tests and speed up existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewharvey committed Jan 12, 2017
1 parent 64cfd4a commit a2ef69a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"mock-geolocation": "^1.0.11",
"npm-run-all": "^3.0.0",
"nyc": "^8.3.0",
"object.map": "^0.2.0",
"pixelmatch": "^4.0.2",
"pngjs": "^3.0.0",
"proxyquire": "^1.7.9",
Expand Down
92 changes: 82 additions & 10 deletions test/js/ui/control/geolocate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const test = require('mapbox-gl-js-test').test;
const window = require('../../../../js/util/window');
const Map = require('../../../../js/ui/map');
const GeolocateControl = require('../../../../js/ui/control/geolocate_control');
const map = require('object.map');

// window and navigator globals need to be set for mock-geolocation
global.window = {};
Expand All @@ -27,6 +28,13 @@ function createMap() {
});
}

// convert the coordinates of a LngLat object to a fixed number of digits
function lngLatAsFixed(lngLat, digits) {
return map(lngLat, (val) => {
return val.toFixed(digits);
});
}

test('GeolocateControl with no options', (t) => {
const map = createMap();
const geolocate = new GeolocateControl();
Expand Down Expand Up @@ -84,20 +92,69 @@ test('GeolocateControl geolocate event', (t) => {
});
});

test('GeolocateControl no watching map centered on geolocation', (t) => {
test('GeolocateControl geolocate fitBoundsOptions', (t) => {
const map = createMap();
const geolocate = new GeolocateControl();
const geolocate = new GeolocateControl({
fitBoundsOptions: {
linear: true,
duration: 0,
maxZoom: 10
}
});
map.addControl(geolocate);

const click = new window.Event('click');

geolocate.on('ready', () => {
map.once('moveend', (position) => {
t.equal(map.getZoom(), 10, 'geolocate fitBounds maxZoom');
t.end();
});
geolocate._geolocateButton.dispatchEvent(click);
geolocation.send({latitude: 10, longitude: 20, accuracy: 1});
});
});

test('GeolocateControl no watching map camera on geolocation', (t) => {
const map = createMap();
const geolocate = new GeolocateControl({
fitBoundsOptions: {
maxZoom: 20,
linear: true,
duration: 0
}
});
map.addControl(geolocate);

const click = new window.Event('click');

geolocate.on('ready', () => {
map.on('moveend', () => {
t.deepEqual(map.getCenter(), { lat: 10, lng: 20 }, 'map centered on location');
t.deepEqual(lngLatAsFixed(map.getCenter(), 4), { lat: 10, lng: 20 }, 'map centered on location');

const mapBounds = map.getBounds();

// map bounds should contain or equal accuracy bounds, that is the ensure accuracy bounds doesn't fall outside the map bounds
const accuracyBounds = map.getCenter().toBounds(1000);
t.ok(accuracyBounds.getNorth().toFixed(4) <= mapBounds.getNorth().toFixed(4), 'map contains north of accuracy radius');
t.ok(accuracyBounds.getSouth().toFixed(4) >= mapBounds.getSouth().toFixed(4), 'map contains south of accuracy radius');
t.ok(accuracyBounds.getEast().toFixed(4) <= mapBounds.getEast().toFixed(4), 'map contains east of accuracy radius');
t.ok(accuracyBounds.getWest().toFixed(4) >= mapBounds.getWest().toFixed(4), 'map contains west of accuracy radius');

// map bounds should not be too much bigger on all edges of the accuracy bounds (this test will only work for an orthogonal bearing),
// ensures map bounds does not contain buffered accuracy bounds, as if it does there is too much gap around the accuracy bounds
const bufferedAccuracyBounds = map.getCenter().toBounds(1100);
t.notOk(
(bufferedAccuracyBounds.getNorth().toFixed(4) < mapBounds.getNorth().toFixed(4)) &&
(bufferedAccuracyBounds.getSouth().toFixed(4) > mapBounds.getSouth().toFixed(4)) &&
(bufferedAccuracyBounds.getEast().toFixed(4) < mapBounds.getEast().toFixed(4)) &&
(bufferedAccuracyBounds.getWest().toFixed(4) > mapBounds.getWest().toFixed(4)),
'map bounds is much is larger than the accuracy radius');

t.end();
});
geolocate._geolocateButton.dispatchEvent(click);
geolocation.send({latitude: 10, longitude: 20, accuracy: 30});
geolocation.send({latitude: 10, longitude: 20, accuracy: 1000});
});
});

Expand All @@ -106,6 +163,10 @@ test('GeolocateControl watching map updates recenter on location with marker', (
const geolocate = new GeolocateControl({
watchPosition: true,
showMarker: true,
fitBoundsOptions: {
linear: true,
duration: 0
},
markerPaintProperties: {
'circle-radius': 10,
'circle-color': '#000',
Expand All @@ -122,11 +183,11 @@ test('GeolocateControl watching map updates recenter on location with marker', (

geolocate.on('ready', () => {
map.once('moveend', () => {
t.deepEqual(map.getCenter(), { lat: 10, lng: 20 }, 'map centered on location after 1st update');
t.deepEqual(lngLatAsFixed(map.getCenter(), 4), { lat: 10, lng: 20 }, 'map centered on location after 1st update');
t.ok(map.getLayer('_geolocate-control-marker'), 'has marker layer');
t.equals(map.getPaintProperty('_geolocate-control-marker', 'circle-color'), '#000', 'markerPaintProperty circle-color');
map.once('moveend', () => {
t.deepEqual(map.getCenter(), { lat: 40, lng: 50 }, 'map centered on location after 2nd update');
t.deepEqual(lngLatAsFixed(map.getCenter(), 4), { lat: 40, lng: 50 }, 'map centered on location after 2nd update');
geolocate.once('error', () => {
t.equals(map.getPaintProperty('_geolocate-control-marker', 'circle-color'), '#f00', 'markerStalePaintProperty circle-color');
t.end();
Expand All @@ -143,7 +204,11 @@ test('GeolocateControl watching map updates recenter on location with marker', (
test('GeolocateControl watching map background event', (t) => {
const map = createMap();
const geolocate = new GeolocateControl({
watchPosition: true
watchPosition: true,
fitBoundsOptions: {
linear: true,
duration: 0
}
});
map.addControl(geolocate);

Expand Down Expand Up @@ -171,7 +236,11 @@ test('GeolocateControl watching map background event', (t) => {
test('GeolocateControl watching map background state', (t) => {
const map = createMap();
const geolocate = new GeolocateControl({
watchPosition: true
watchPosition: true,
fitBoundsOptions: {
linear: true,
duration: 0
}
});
map.addControl(geolocate);

Expand Down Expand Up @@ -202,10 +271,13 @@ test('GeolocateControl watching map background state', (t) => {
});

test('GeolocateControl active_lock event', (t) => {
console.log('start final test');
const map = createMap();
const geolocate = new GeolocateControl({
watchPosition: true
watchPosition: true,
fitBoundsOptions: {
linear: true,
duration: 0
}
});
map.addControl(geolocate);

Expand Down

0 comments on commit a2ef69a

Please sign in to comment.