diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-05-16 23:46:04 +0200 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-05-16 23:46:04 +0200 |
commit | 837cc126d35dfa1387d0073fdb7f45fa36543f4b (patch) | |
tree | 3c42bb54ded1908b587722c210ecec0a7ab48a78 /spec/lib/RAFPlugin.js | |
parent | 82b3524c10b10094c7a6129639a34ca7c034dbdc (diff) | |
download | svg.js-837cc126d35dfa1387d0073fdb7f45fa36543f4b.tar.gz svg.js-837cc126d35dfa1387d0073fdb7f45fa36543f4b.zip |
add requestAnimationFrame Mock and tests for the animator
Diffstat (limited to 'spec/lib/RAFPlugin.js')
-rw-r--r-- | spec/lib/RAFPlugin.js | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/spec/lib/RAFPlugin.js b/spec/lib/RAFPlugin.js new file mode 100644 index 0000000..fefdda6 --- /dev/null +++ b/spec/lib/RAFPlugin.js @@ -0,0 +1,82 @@ +/** + * Jasmine RequestAnimationFrame: a set of helpers for testing funcionality + * that uses requestAnimationFrame under the Jasmine BDD framework for JavaScript. + */ +;(function() { + + var index = 0, + callbacks = []; + + function MockRAF(global) { + this.realRAF = global.requestAnimationFrame, + this.realCAF = global.cancelAnimationFrame, + this.realPerf = global.performance, + this.nextTime = 0 + + var _this = this + + /** + * Mock for window.requestAnimationFrame + */ + this.mockRAF = function(fn) { + if (typeof fn !== 'function') { + throw new Error('You should pass a function to requestAnimationFrame'); + } + + callbacks[index++] = fn; + + return index; + }; + + /** + * Mock for window.cancelAnimationFrame + */ + this.mockCAF = function(requestID) { + callbacks.splice(requestID, 1) + }; + + this.mockPerf = { + now: function () { + return _this.nextTime + } + } + + /** + * Install request animation frame mocks. + */ + this.install = function() { + global.requestAnimationFrame = _this.mockRAF; + global.cancelAnimationFrame = _this.mockCAF; + global.performance = _this.mockPerf; + }; + + /** + * Uninstall request animation frame mocks. + */ + this.uninstall = function() { + global.requestAnimationFrame = _this.realRAF; + global.cancelAnimationFrame = _this.realCAF; + global.performance = _this.realPerf; + }; + + /** + * Simulate animation frame readiness. + */ + this.tick = function(dt) { + _this.nextTime += (dt || 1) + + var fns = callbacks, fn, i; + + callbacks = []; + index = 0; + + for (i in fns) { + fn = fns[i]; + fn(_this.nextTime); + } + }; + } + + + jasmine.RequestAnimationFrame = new MockRAF(window); +}()); |