Permalink
Browse files

Added drift testing. Also added drift module which encapsulates the d…

…rifting logic
  • Loading branch information...
1 parent 1adc66b commit b7681461074b3dec604103d54c68a3c4142ced81 @timsuchanek timsuchanek committed May 23, 2013
Showing with 112 additions and 0 deletions.
  1. +38 −0 test/drift.js
  2. +74 −0 test/modules/drift-move.js
View
@@ -0,0 +1,38 @@
+var casper = require('casper').create();
+
+// set up
+casper.start();
+casper.options.clientScripts = ['./test/lib/jquery.js'];
+casper.viewport(500, 500);
+
+
+// add helper methods
+casper.getScrollTop = function () {
+ return this.evaluate(function () {
+ return $('#overscroll').scrollTop();
+ });
+};
+
+casper.getScrollLeft = function () {
+ return this.evaluate(function () {
+ return $('#overscroll').scrollLeft();
+ });
+};
+
+casper.thenOpen('http://localhost:9000/test/resources/simple.html', function () {
+
+ casper.test.comment('test scroll up');
+
+ this.test.assertEquals(this.getScrollTop(), 0, 'init - check top');
+ this.test.assertEquals(this.getScrollLeft(), 0, 'init - check left');
+
+ this.mouse.down(0, 400);
+ this.mouse.move(0, 300);
+ this.mouse.up(0, 300);
+
+ this.test.assertEquals(this.getScrollTop(), 100, 'dragged 100 px up - check top');
+ this.test.assertEquals(this.getScrollLeft(), 0, 'dragged 100 px up - check left');
+
+});
+
+casper.run();
View
@@ -0,0 +1,74 @@
+// var options = {
+// startCoordinates: { //optional
+// x: Number,
+// y: Number
+// },
+// degree: Number, // degree of direction to drift.
+// speed: Number, // speed in pixels per second
+// duration: Number, // duration of drift in seconds
+// start: Function, // Callback for start, arguments x,y
+// way: Function, // Callback for the way, arguments x,y
+// finished: Function, // Callback when finished, arguments x,y
+// waitFinished: Number// time to wait between last way() and finished call
+// }
+
+module.exports = drift;
+
+function drift (options) {
+ var MOVE_INTERVAL = 20; //ms
+ var stepX = Math.sin(options.degree);
+ var stepY = Math.cos(options.degree);
+ var moveCoeficent = (1000 / MOVE_INTERVAL) * options.speed;
+ var interval;
+ var x, y;
+ var counter;
+ var limit = Math.floor(duration / MOVE_INTERVAL);
+ var that = this;
+
+ // if no start point was given, start at 0
+ if (options.hasOwnProperty('startCoordinates')) {
+ if (options.startCoordinates.hasOwnProperty('x')
+ && options.startCoordinates.hasOwnProperty('y')
+ && typeof options.startCoordinates.x === 'number'
+ && typeof options.startCoordinates.y === 'number') {
+ x = options.startCoordinates.x;
+ y = options.startCoordinates.y;
+ } else {
+ x = 0;
+ y = 0;
+ }
+ } else {
+ x = 0;
+ y = 0;
+ }
+
+ stepX = stepX * moveCoeficent;
+ stepY = stepY * moveCoeficent;
+
+ //start
+ if (options.hasOwnProperty('start')
+ && typeof options.start === 'function') {
+ //call start callback
+ options.start(x, y);
+ }
+
+ interval = setInterval (function () {
+ if (counter <= limit) {
+ x += stepX;
+ y += stepY;
+ //call the way callback
+ options.way.call(that, x,y);
+ counter += 1;
+ } else {
+ clearInterval(interval);
+ if (options.hasOwnProperty('waitFinished')) {
+ setTimeout(function () {
+ finished.call(that, x, y);
+ }, waitFinished);
+ } else {
+ finished.call(that, x, y);
+ }
+ }
+ }, MOVE_INTERVAL);
+
+}

0 comments on commit b768146

Please sign in to comment.