From 7f01de9261c5382b1f6b767a85a10705212662e5 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Fri, 11 Nov 2016 06:27:44 -0700 Subject: [PATCH] Reset hint first and notify of change when cancelling animations --- src/ol/view.js | 3 +- test/spec/ol/view.test.js | 94 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/ol/view.js b/src/ol/view.js index e2efd2bd0b..6155efd98c 100644 --- a/src/ol/view.js +++ b/src/ol/view.js @@ -272,6 +272,7 @@ ol.View.prototype.getAnimating = function() { * Cancel any ongoing animations. */ ol.View.prototype.cancelAnimations = function() { + this.setHint(ol.View.Hint.ANIMATING, -this.getHints()[ol.View.Hint.ANIMATING]); for (var i = 0, ii = this.animations_.length; i < ii; ++i) { var series = this.animations_[i]; if (series[0].callback) { @@ -279,7 +280,7 @@ ol.View.prototype.cancelAnimations = function() { } } this.animations_.length = 0; - this.setHint(ol.View.Hint.ANIMATING, -this.getHints()[ol.View.Hint.ANIMATING]); + this.changed(); // notify that the hint changed }; /** diff --git a/test/spec/ol/view.test.js b/test/spec/ol/view.test.js index 6e36870a25..2f97a6547d 100644 --- a/test/spec/ol/view.test.js +++ b/test/spec/ol/view.test.js @@ -519,6 +519,100 @@ describe('ol.View', function() { }); + describe('#cancelAnimations()', function() { + + var originalRequestAnimationFrame = window.requestAnimationFrame; + var originalCancelAnimationFrame = window.cancelAnimationFrame; + + beforeEach(function() { + window.requestAnimationFrame = function(callback) { + return setTimeout(callback, 1); + }; + window.cancelAnimationFrame = function(key) { + return clearTimeout(key); + }; + }); + + afterEach(function() { + window.requestAnimationFrame = originalRequestAnimationFrame; + window.cancelAnimationFrame = originalCancelAnimationFrame; + }); + + it('cancels a currently running animation', function(done) { + var view = new ol.View({ + center: [0, 0], + zoom: 0, + rotation: 0 + }); + + view.animate({ + rotation: 10, + duration: 50 + }); + + setTimeout(function() { + expect(view.getAnimating()).to.be(true); + view.once('change', function() { + expect(view.getAnimating()).to.be(false); + done(); + }); + view.cancelAnimations(); + }, 10); + }); + + it('cancels a multiple animations', function(done) { + var view = new ol.View({ + center: [0, 0], + zoom: 0, + rotation: 0 + }); + + view.animate({ + rotation: 10, + duration: 50 + }, { + zoom: 10, + duration: 50 + }); + + view.animate({ + center: [10, 30], + duration: 100 + }); + + setTimeout(function() { + expect(view.getAnimating()).to.be(true); + view.once('change', function() { + expect(view.getAnimating()).to.be(false); + done(); + }); + view.cancelAnimations(); + }, 10); + }); + + it('calls callbacks with false to indicate animations did not complete', function(done) { + var view = new ol.View({ + center: [0, 0], + zoom: 0 + }); + + view.animate({ + zoom: 10, + duration: 50 + }, function(complete) { + expect(view.getAnimating()).to.be(false); + expect(complete).to.be(false); + done(); + }); + + setTimeout(function() { + expect(view.getAnimating()).to.be(true); + view.cancelAnimations(); + }, 10); + }); + + }); + describe('#getResolutions', function() { var view; var resolutions = [512, 256, 128, 64, 32, 16];