Skip to content

Commit

Permalink
Ensure camera methods fire rotatestart and rotateend events
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Feb 16, 2018
1 parent c5dbb57 commit 07e06b8
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 25 deletions.
11 changes: 10 additions & 1 deletion src/ui/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,9 @@ class Camera extends Evented {
}

if (bearingChanged) {
this.fire('rotate', eventData);
this.fire('rotatestart', eventData)
.fire('rotate', eventData)
.fire('rotateend', eventData);
}

if (pitchChanged) {
Expand Down Expand Up @@ -613,6 +615,9 @@ class Camera extends Evented {
if (this.zooming) {
this.fire('zoomstart', eventData);
}
if (this.rotating) {
this.fire('rotatestart', eventData);
}
if (this.pitching) {
this.fire('pitchstart', eventData);
}
Expand All @@ -633,6 +638,7 @@ class Camera extends Evented {

_afterEase(eventData?: Object) {
const wasZooming = this.zooming;
const wasRotating = this.rotating;
const wasPitching = this.pitching;
this.moving = false;
this.zooming = false;
Expand All @@ -642,6 +648,9 @@ class Camera extends Evented {
if (wasZooming) {
this.fire('zoomend', eventData);
}
if (wasRotating) {
this.fire('rotateend', eventData);
}
if (wasPitching) {
this.fire('pitchend', eventData);
}
Expand Down
119 changes: 95 additions & 24 deletions test/unit/ui/camera.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ test('camera', (t) => {
t.end();
});

t.test('emits rotate events, preserving eventData', (t) => {
let started, rotated, ended;
const eventData = { data: 'ok'};

camera
.on('rotatestart', (d) => { started = d.data; })
.on('rotate', (d) => { rotated = d.data; })
.on('rotateend', (d) => { ended = d.data; });

camera.jumpTo({bearing: 90}, eventData);
t.equal(started, 'ok');
t.equal(rotated, 'ok');
t.equal(ended, 'ok');
t.end();
});

t.test('emits pitch events, preserving eventData', (t)=>{
let started, pitched, ended;
const eventData = { data: 'ok'};
Expand Down Expand Up @@ -250,25 +266,32 @@ test('camera', (t) => {
t.end();
});

t.test('emits move events, preserving eventData', (t) => {
let started, moved, ended;
t.test('emits move and rotate events, preserving eventData', (t) => {
let movestarted, moved, moveended, rotatestarted, rotated, rotateended;
const eventData = { data: 'ok' };

camera
.on('movestart', (d) => { started = d.data; })
.on('movestart', (d) => { movestarted = d.data; })
.on('move', (d) => { moved = d.data; })
.on('moveend', (d) => { ended = d.data; });
camera.setBearing(4, eventData);
t.equal(started, 'ok');
.on('moveend', (d) => { moveended = d.data; })
.on('rotatestart', (d) => { rotatestarted = d.data; })
.on('rotate', (d) => { rotated = d.data; })
.on('rotateend', (d) => { rotateended = d.data; });

camera.setBearing(5, eventData);
t.equal(movestarted, 'ok');
t.equal(moved, 'ok');
t.equal(ended, 'ok');
t.equal(moveended, 'ok');
t.equal(rotatestarted, 'ok');
t.equal(rotated, 'ok');
t.equal(rotateended, 'ok');
t.end();
});

t.test('cancels in-progress easing', (t) => {
camera.panTo([3, 4]);
t.ok(camera.isEasing());
camera.setBearing(5);
camera.setBearing(6);
t.ok(!camera.isEasing());
t.end();
});
Expand Down Expand Up @@ -504,19 +527,27 @@ test('camera', (t) => {

t.test('emits move and rotate events, preserving eventData', (t) => {
const camera = createCamera();
let movestarted, moved, rotated;
let movestarted, moved, rotatestarted, rotated;
const eventData = { data: 'ok' };

t.plan(6);

camera
.on('movestart', (d) => { movestarted = d.data; })
.on('move', (d) => { moved = d.data; })
.on('rotate', (d) => { rotated = d.data; })
.on('moveend', (d) => {
t.equal(movestarted, 'ok');
t.equal(moved, 'ok');
t.equal(d.data, 'ok');
});

camera
.on('rotatestart', (d) => { rotatestarted = d.data; })
.on('rotate', (d) => { rotated = d.data; })
.on('rotateend', (d) => {
t.equal(rotatestarted, 'ok');
t.equal(rotated, 'ok');
t.equal(d.data, 'ok');
t.end();
});

camera.rotateTo(90, { duration: 0 }, eventData);
Expand Down Expand Up @@ -661,16 +692,14 @@ test('camera', (t) => {

t.test('emits move, zoom, rotate, and pitch events, preserving eventData', (t) => {
const camera = createCamera();
let movestarted, moved, rotated, pitched, zoomstarted, zoomed;
let movestarted, moved, zoomstarted, zoomed, rotatestarted, rotated, pitchstarted, pitched;
const eventData = { data: 'ok' };

t.plan(12);
t.plan(18);

camera
.on('movestart', (d) => { movestarted = d.data; })
.on('move', (d) => { moved = d.data; })
.on('rotate', (d) => { rotated = d.data; })
.on('pitch', (d) => { pitched = d.data; })
.on('moveend', (d) => {
t.notOk(camera.zooming);
t.notOk(camera.panning);
Expand All @@ -693,6 +722,24 @@ test('camera', (t) => {
t.equal(d.data, 'ok');
});

camera
.on('rotatestart', (d) => { rotatestarted = d.data; })
.on('rotate', (d) => { rotated = d.data; })
.on('rotateend', (d) => {
t.equal(rotatestarted, 'ok');
t.equal(rotated, 'ok');
t.equal(d.data, 'ok');
});

camera
.on('pitchstart', (d) => { pitchstarted = d.data; })
.on('pitch', (d) => { pitched = d.data; })
.on('pitchend', (d) => {
t.equal(pitchstarted, 'ok');
t.equal(pitched, 'ok');
t.equal(d.data, 'ok');
});

camera.easeTo(
{ center: [100, 0], zoom: 3.2, bearing: 90, duration: 0, pitch: 45 },
eventData);
Expand Down Expand Up @@ -938,9 +985,10 @@ test('camera', (t) => {
});

t.test('emits move, zoom, rotate, and pitch events, preserving eventData', (t) => {
t.plan(18);

const camera = createCamera();
let movestarted, moved, rotated, pitched, zoomstarted, zoomed,
count = 0;
let movestarted, moved, zoomstarted, zoomed, rotatestarted, rotated, pitchstarted, pitched;
const eventData = { data: 'ok' };

camera
Expand All @@ -959,7 +1007,6 @@ test('camera', (t) => {
t.equal(rotated, 'ok');
t.equal(pitched, 'ok');
t.equal(d.data, 'ok');
if (++count === 2) t.end();
});

camera
Expand All @@ -969,7 +1016,24 @@ test('camera', (t) => {
t.equal(zoomstarted, 'ok');
t.equal(zoomed, 'ok');
t.equal(d.data, 'ok');
if (++count === 2) t.end();
});

camera
.on('rotatestart', (d) => { rotatestarted = d.data; })
.on('rotate', (d) => { rotated = d.data; })
.on('rotateend', (d) => {
t.equal(rotatestarted, 'ok');
t.equal(rotated, 'ok');
t.equal(d.data, 'ok');
});

camera
.on('pitchstart', (d) => { pitchstarted = d.data; })
.on('pitch', (d) => { pitched = d.data; })
.on('pitchend', (d) => {
t.equal(pitchstarted, 'ok');
t.equal(pitched, 'ok');
t.equal(d.data, 'ok');
});

camera.flyTo(
Expand All @@ -981,19 +1045,24 @@ test('camera', (t) => {
//As I type this, the code path for guiding super-short flights is (and will probably remain) different.
//As such; it deserves a separate test case. This test case flies the map from A to A.
const camera = createCamera({ center: [100, 0] });
let movestarted, moved, rotated, pitched, pitchstarted, pitchended, zoomstarted, zoomed, zoomended;
let movestarted, moved,
zoomstarted, zoomed, zoomended,
rotatestarted, rotated, rotateended,
pitchstarted, pitched, pitchended;
const eventData = { data: 'ok' };

camera
.on('movestart', (d) => { movestarted = d.data; })
.on('move', (d) => { moved = d.data; })
.on('rotate', (d) => { rotated = d.data; })
.on('pitch', (d) => { pitched = d.data; })
.on('pitchstart', (d) => { pitchstarted = d.data; })
.on('pitchend', (d) => { pitchended = d.data; })
.on('zoomstart', (d) => { zoomstarted = d.data; })
.on('zoom', (d) => { zoomed = d.data; })
.on('zoomend', (d) => { zoomended = d.data; })
.on('rotatestart', (d) => { rotatestarted = d.data; })
.on('rotate', (d) => { rotated = d.data; })
.on('rotateend', (d) => { rotateended = d.data; })
.on('pitchstart', (d) => { pitchstarted = d.data; })
.on('pitch', (d) => { pitched = d.data; })
.on('pitchend', (d) => { pitchended = d.data; })
.on('moveend', function(d) {
t.notOk(this.zooming);
t.notOk(this.panning);
Expand All @@ -1004,7 +1073,9 @@ test('camera', (t) => {
t.equal(zoomstarted, undefined);
t.equal(zoomed, undefined);
t.equal(zoomended, undefined);
t.equal(rotatestarted, undefined);
t.equal(rotated, undefined);
t.equal(rotateended, undefined);
t.equal(pitched, undefined);
t.equal(pitchstarted, undefined);
t.equal(pitchended, undefined);
Expand Down

0 comments on commit 07e06b8

Please sign in to comment.