Permalink
Browse files

Refactored drift-move. Added 3 simple test cases for the drifting

  • Loading branch information...
1 parent 149ec59 commit 96d797f567fe38a59d4b99bf5758b6350f063352 @timsuchanek timsuchanek committed May 24, 2013
Showing with 137 additions and 36 deletions.
  1. +105 −10 test/drift.js
  2. +32 −26 test/modules/drift-move.js
View
@@ -1,8 +1,10 @@
-var casper = require('casper').create();
+var casper = require('casper').create(),
+ drift = require('./test/modules/drift-move');
// set up
casper.start();
casper.options.clientScripts = ['./test/lib/jquery.js'];
+casper.options.waitTimeout = 10000;
casper.viewport(500, 500);
@@ -19,20 +21,113 @@ casper.getScrollLeft = function () {
});
};
-casper.thenOpen('http://localhost:9000/test/resources/simple.html', function () {
+function testDrift (test) {
+ casper.thenOpen('http://localhost:9000/test/resources/simple.html', function () {
- casper.test.comment('test scroll up');
+ casper.test.comment('test drift down');
- this.test.assertEquals(this.getScrollTop(), 0, 'init - check top');
- this.test.assertEquals(this.getScrollLeft(), 0, 'init - check left');
+ 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);
+ var that = this;
+ var start;
- this.test.assertEquals(this.getScrollTop(), 100, 'dragged 100 px up - check top');
- this.test.assertEquals(this.getScrollLeft(), 0, 'dragged 100 px up - check left');
+ var distance = test.distance;
+ var duration = test.duration;
+ var degree = test.degree;
+ var target = test.target;
+
+ casper.waitFor(function () {
+ start = new Date().getMilliseconds();
+ var result = drift({
+ startCoordinates: { //optional
+ x: 10,
+ y: 400
+ },
+ degree: degree, // 90 means move right
+ distance: distance, // speed in pixels per second
+ duration: duration, // duration of drift in seconds
+ start: function (x,y) {
+ casper.test.comment('down ' + x + ' ' + y);
+ that.mouse.down(x, y);
+ }, // Callback for start, arguments x,y
+ way: function (x,y) {
+ casper.test.comment('move ' + x + ' ' + y);
+ that.mouse.move(x, y);
+ }, // Callback for the way, arguments x,y
+ finished: function (x,y) {
+ casper.test.comment('up ' + x + ' ' + y);
+ that.mouse.up(x, y);
+ }
+ }, window);
+
+ return result;
+ }, function () {
+ console.log('time needed', new Date().getMilliseconds() - start);
+ var pre = distance + ' px, ' + degree + 'º, ' + duration + ' ms';
+ that.test.assertEquals(that.getScrollLeft(), target.direct.x, pre + ' direct after drift - x');
+ that.test.assertEquals(that.getScrollTop(), target.direct.y, pre + ' direct after drift - y');
+ casper.wait(1000, function () {
+ that.test.assertEquals(that.getScrollLeft(), target.delay.x, pre + ' 1 sec after drift - x');
+ that.test.assertEquals(that.getScrollTop(), target.delay.y, pre + ' 1 sec after drift - y');
+ });
+ });
+
+ });
+}
+
+var tests = [
+ {
+ target: {
+ direct: {
+ x: 0,
+ y: 77
+ },
+ delay: {
+ x: 0,
+ y: 110
+ }
+ },
+ distance: 50,
+ duration: 0.02,
+ degree: 0
+ },
+ {
+ target: {
+ direct: {
+ x: 0,
+ y: 165
+ },
+ delay: {
+ x: 0,
+ y: 236
+ }
+ },
+ distance: 100,
+ duration: 0.02,
+ degree: 0
+ },
+ {
+ target: {
+ direct: {
+ x: 0,
+ y: 247
+ },
+ delay: {
+ x: 0,
+ y: 363
+ }
+ },
+ distance: 150,
+ duration: 0.02,
+ degree: 0
+ }
+];
+
+tests.forEach(function (test) {
+ testDrift(test);
});
+
casper.run();
@@ -1,31 +1,34 @@
-// 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
-// }
-
-
-
-function drift (options) {
- var MOVE_INTERVAL = 16.6666666; //ms
+/**
+ * A wrapper function to make easy drifts.
+ * Works as a nodejs or browser module.
+ * @author vardump
+ * @param {object} options {
+ startCoordinates: { //optional
+ x: Number,
+ y: Number
+ },
+ degree: Number, // degree of direction to drift.
+ distance: Number, // distance in pixel
+ 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
+ }
+ */
+function drift (options, window) {
+ var MOVE_INTERVAL = 2; //ms
var speed = options.distance / options.duration;
options.degree = ((Math.PI * 2) / 360) * options.degree;
var stepX = Math.sin(options.degree);
var stepY = -Math.cos(options.degree);
- console.log(stepX, stepY);
+
var moveCoeficent = (MOVE_INTERVAL / 1000) * speed;
var interval;
var x, y;
var counter = 0;
options.duration = options.duration * 1000;
+
var limit = Math.floor(options.duration / MOVE_INTERVAL);
var that = this;
@@ -48,7 +51,6 @@ function drift (options) {
stepX = stepX * moveCoeficent;
stepY = stepY * moveCoeficent;
- console.log(stepX, stepY);
//start
if (options.hasOwnProperty('start')
@@ -57,26 +59,30 @@ function drift (options) {
options.start(x, y);
}
+ var returnVal = true;
+
interval = setInterval(function () {
//console.log(stepX, stepY);
if (counter <= limit) {
x += stepX;
y += stepY;
- //call the way callback
+ //call the way callback
options.way(x, y);
counter += 1;
- } else {
- clearInterval(interval);
+ } else {
if (options.hasOwnProperty('waitFinished')) {
setTimeout(function () {
- options.finished.call(that, x, y);
+ options.finished(x, y);
}, options.waitFinished);
} else {
- options.finished.call(that, x, y);
+ console.log('finished');
+ options.finished(x, y);
}
+ window.clearInterval(interval);
+ returnVal = true;
}
}, MOVE_INTERVAL);
-
+ return returnVal;
}
//the frontend doesnt know any module ..

0 comments on commit 96d797f

Please sign in to comment.