Skip to content
Permalink
Browse files

Dev: Standardized the draggable test suite. Fixed #8748 - Dev: Get dr…

…aggable test suite to pass
  • Loading branch information...
mikesherov authored and scottgonzalez committed Oct 30, 2012
1 parent d693ce5 commit f0daa220d67968f61c42e378b10fcb41687cd215
@@ -13,18 +13,18 @@ var versions = {
"Autocomplete": "autocomplete/autocomplete.html",
"Button": "button/button.html",
"Core": "core/core.html",
//"datepicker/datepicker.html",
//"dialog/dialog.html",
//"draggable/draggable.html",
//"droppable/droppable.html",
//"Datepicker": "datepicker/datepicker.html",
//"Dialog": "dialog/dialog.html",
"Draggable": "draggable/draggable.html",
//"Droppable": "droppable/droppable.html",
"Effects": "effects/effects.html",
"Menu": "menu/menu.html",
"Position": "position/position.html",
"Progressbar": "progressbar/progressbar.html",
//"resizable/resizable.html",
//"selectable/selectable.html",
//"slider/slider.html",
//"sortable/sortable.html",
//"Resizable": "resizable/resizable.html",
//"Selectable": "selectable/selectable.html",
//"Slider": "slider/slider.html",
//"Sortable": "sortable/sortable.html",
"Spinner": "spinner/spinner.html",
"Tabs": "tabs/tabs.html",
"Tooltip": "tooltip/tooltip.html",
@@ -281,13 +281,13 @@ grunt.initConfig({
files: grunt.file.expandFiles( "tests/unit/**/*.html" ).filter(function( file ) {
// disabling everything that doesn't (quite) work with PhantomJS for now
// TODO except for all|index|test, try to include more as we go
return !( /(all|all-active|index|test|draggable|droppable|selectable|resizable|sortable|dialog|slider|datepicker|tabs|tooltip)\.html$/ ).test( file );
return !( /(all|all-active|index|test|droppable|selectable|resizable|sortable|dialog|slider|datepicker|tabs|tooltip)\.html$/ ).test( file );
})
},
lint: {
ui: grunt.file.expandFiles( "ui/*.js" ).filter(function( file ) {
// TODO remove items from this list once rewritten
return !( /(mouse|datepicker|draggable|droppable|resizable|selectable|sortable)\.js$/ ).test( file );
return !( /(mouse|datepicker|droppable|resizable|selectable|sortable)\.js$/ ).test( file );
}),
grunt: [ "grunt.js", "build/**/*.js" ],
tests: "tests/unit/**/*.js"
@@ -22,7 +22,7 @@
"core/core.html",
//"datepicker/datepicker.html",
//"dialog/dialog.html",
//"draggable/draggable.html",
"draggable/draggable.html",
//"droppable/droppable.html",
"effects/effects.html",
"menu/menu.html",
@@ -22,7 +22,7 @@
"core/core.html",
"datepicker/datepicker.html",
"dialog/dialog.html",
//"draggable/draggable.html",
"draggable/draggable.html",
//"droppable/droppable.html",
"effects/effects.html",
"menu/menu.html",
@@ -38,11 +38,14 @@ <h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">

<div id="main"></div>

<div id="draggable1" style="background: green; width: 200px; height: 100px;">Relative</div>
<div id="draggable2" style="background: green; width: 200px; height: 100px; position: absolute; top: 10px; left: 10px;"><span>Absolute</span></div>
<div style='width: 1px; height: 1000px;'></div>
<div style="position: absolute; width: 1px; height: 2000px;"></div>

</div>

</body>
</html>
@@ -6,6 +6,7 @@ TestHelpers.commonWidgetTests( "draggable", {
cancel: "input,textarea,button,select,option",
connectToSortable: false,
containment: false,
create: null,
cursor: "auto",
cursorAt: false,
delay: 0,
@@ -2,32 +2,66 @@
* draggable_core.js
*/

var el, offsetBefore, offsetAfter, dragged;
TestHelpers.draggable = {};

function drag(handle, dx, dy) {
offsetBefore = el.offset();
TestHelpers.draggable.drag = function(handle, dx, dy) {
$(handle).simulate("drag", {
dx: dx || 0,
dy: dy || 0
});
dragged = { dx: dx, dy: dy };
offsetAfter = el.offset();
}
return el.offset();
};

TestHelpers.draggable.testDrag = function(el, handle, dx, dy, expectedDX, expectedDY, msg) {

var offsetBefore = el.offset(),
offsetAfter = TestHelpers.draggable.drag(handle, dx, dy),
actual = { left: offsetAfter.left, top: offsetAfter.top },
expected = { left: offsetBefore.left + expectedDX, top: offsetBefore.top + expectedDY };

function moved(dx, dy, msg) {
msg = msg ? msg + "." : "";
var actual = { left: offsetAfter.left, top: offsetAfter.top },
expected = { left: offsetBefore.left + dx, top: offsetBefore.top + dy };
deepEqual(actual, expected, 'dragged[' + dragged.dx + ', ' + dragged.dy + '] ' + msg);
}
deepEqual(actual, expected, 'dragged[' + dx + ', ' + dy + '] ' + msg);
};

TestHelpers.draggable.shouldMove = function(el, why) {
TestHelpers.draggable.testDrag(el, el, 50, 50, 50, 50, why);
};

TestHelpers.draggable.shouldNotMove = function(el, why) {
TestHelpers.draggable.testDrag(el, el, 50, 50, 0, 0, why);
};

TestHelpers.draggable.testScroll = function(el, position ) {
var oldPosition = $("#main").css('position');
$("#main").css('position', position);
TestHelpers.draggable.shouldMove(el, position+' parent');
$("#main").css('position', oldPosition);
};

TestHelpers.restoreScroll = function( what ) {
TestHelpers.draggable.restoreScroll = function( what ) {
if( what ) {
$(document).scrollTop(0); $(document).scrollLeft(0);
} else {
$("#main")[0].scrollTop = 0; $("#main")[0].scrollLeft = 0;
$("#main").scrollTop(0); $("#main").scrollLeft(0);
}
}
};

TestHelpers.draggable.setScroll = function( what ) {
if(what) {
// todo: currently, the draggable interaction doesn't properly account for scrolled pages,
// uncomment the line below to make the tests fail that should when the page is scrolled
// $(document).scrollTop(100); $(document).scrollLeft(100);
} else {
$("#main").scrollTop(100); $("#main").scrollLeft(100);
}
};

TestHelpers.draggable.border = function(el, side) {
return parseInt(el.css('border-' + side + '-width'), 10);
};
TestHelpers.draggable.margin = function(el, side) {
return parseInt(el.css('margin-' + side), 10);
};

(function($) {

@@ -36,31 +70,35 @@ module("draggable");
test("element types", function() {
var typeNames = ('p,h1,h2,h3,h4,h5,h6,blockquote,ol,ul,dl,div,form' +
',table,fieldset,address,ins,del,em,strong,q,cite,dfn,abbr' +
',acronym,code,samp,kbd,var,img,object,hr' +
',acronym,code,samp,kbd,var,img,hr' +
',input,button,label,select,iframe').split(',');

expect( typeNames.length );

$.each(typeNames, function(i) {
var typeName = typeNames[i];
el = $(document.createElement(typeName)).appendTo('body');
var offsetBefore, offsetAfter, typeName = typeNames[i];
el = $(document.createElement(typeName)).appendTo('#main');
(typeName === 'table' && el.append("<tr><td>content</td></tr>"));
el.draggable({ cancel: '' });
drag(el, 50, 50);
moved(50, 50, "&lt;" + typeName + "&gt;");
offsetBefore = el.offset();
offsetAfter =TestHelpers.draggable.drag(el, 50, 50);
//there are some rounding errors in FF and Chrome, so we can't say equal, we have to settle for close enough
ok(offsetAfter.left - offsetBefore.left - 50 < 1 && offsetAfter.top - offsetBefore.top - 50 < 1, 'dragged[50, 50] ' + "&lt;" + typeName + "&gt;");
el.draggable("destroy");
el.remove();
});
});

test("No options, relative", function() {
expect( 1 );
el = $("#draggable1").draggable();
drag(el, 50, 50);
moved(50, 50);
TestHelpers.draggable.shouldMove(el);
});

test("No options, absolute", function() {
expect( 1 );
el = $("#draggable2").draggable();
drag(el, 50, 50);
moved(50, 50);
TestHelpers.draggable.shouldMove(el);
});

})(jQuery);
@@ -16,7 +16,7 @@ test("callbacks occurrence count", function() {
stop: function() { stop++; }
});

drag(el, 10, 10);
TestHelpers.draggable.drag(el, 10, 10);

equal(start, 1, "start callback should happen exactly once");
equal(dragc, 3, "drag callback should happen exactly once per mousemove");
@@ -35,7 +35,7 @@ test("stopping the start callback", function() {
stop: function() { stop++; }
});

drag(el, 10, 10);
TestHelpers.draggable.drag(el, 10, 10);

equal(start, 1, "start callback should happen exactly once");
equal(dragc, 0, "drag callback should not happen at all");
@@ -54,7 +54,7 @@ test("stopping the drag callback", function() {
stop: function() { stop++; }
});

drag(el, 10, 10);
TestHelpers.draggable.drag(el, 10, 10);

equal(start, 1, "start callback should happen exactly once");
equal(dragc, 1, "drag callback should happen exactly once");
@@ -71,7 +71,7 @@ test("stopping the stop callback", function() {
stop: function() { return false; }
});

drag(el, 10, 10);
TestHelpers.draggable.drag(el, 10, 10);

ok($("#draggable2").data('draggable').helper, "the clone should not be deleted if the stop callback is stopped");

@@ -3,20 +3,10 @@
*/
(function($) {

function shouldmove(why) {
drag(el, 50, 50);
moved(50, 50, why);
}

function shouldnotmove(why) {
drag(el, 50, 50);
moved(0, 0, why);
}

module("draggable: methods");

test("init", function() {
expect(6);
expect(5);

$("<div></div>").appendTo('body').draggable().remove();
ok(true, '.draggable() called on element');
@@ -27,9 +17,6 @@ test("init", function() {
$("<div></div>").draggable();
ok(true, '.draggable() called on disconnected DOMElement');

$("<div></div>").draggable().draggable("foo");
ok(true, 'arbitrary method called after init');

$("<div></div>").draggable().draggable("option", "foo");
ok(true, 'arbitrary option getter after init');

@@ -38,6 +25,7 @@ test("init", function() {
});

test("destroy", function() {
expect(4);
$("<div></div>").appendTo('body').draggable().draggable("destroy").remove();
ok(true, '.draggable("destroy") called on element');

@@ -47,9 +35,6 @@ test("destroy", function() {
$("<div></div>").draggable().draggable("destroy");
ok(true, '.draggable("destroy") called on disconnected DOMElement');

$("<div></div>").draggable().draggable("destroy").draggable("foo");
ok(true, 'arbitrary method called after destroy');

var expected = $('<div></div>').draggable(),
actual = expected.draggable('destroy');
equal(actual, expected, 'destroy is chainable');
@@ -58,17 +43,19 @@ test("destroy", function() {
test("enable", function() {
expect(7);
el = $("#draggable2").draggable({ disabled: true });
shouldnotmove('.draggable({ disabled: true })');
TestHelpers.draggable.shouldNotMove(el, '.draggable({ disabled: true })');

el.draggable("enable");
shouldmove('.draggable("enable")');
TestHelpers.draggable.shouldMove(el, '.draggable("enable")');
equal(el.draggable("option", "disabled"), false, "disabled option getter");

el.draggable("destroy");
el.draggable({ disabled: true });
shouldnotmove('.draggable({ disabled: true })');
TestHelpers.draggable.shouldNotMove(el, '.draggable({ disabled: true })');

el.draggable("option", "disabled", false);
equal(el.draggable("option", "disabled"), false, "disabled option setter");
shouldmove('.draggable("option", "disabled", false)');
TestHelpers.draggable.shouldMove(el, '.draggable("option", "disabled", false)');

var expected = $('<div></div>').draggable(),
actual = expected.draggable('enable');
@@ -78,18 +65,20 @@ test("enable", function() {
test("disable", function() {
expect(7);
el = $("#draggable2").draggable({ disabled: false });
shouldmove('.draggable({ disabled: false })');
TestHelpers.draggable.shouldMove(el, '.draggable({ disabled: false })');

el.draggable("disable");
shouldnotmove('.draggable("disable")');
TestHelpers.draggable.shouldNotMove(el, '.draggable("disable")');
equal(el.draggable("option", "disabled"), true, "disabled option getter");

el.draggable("destroy");

el.draggable({ disabled: false });
shouldmove('.draggable({ disabled: false })');
TestHelpers.draggable.shouldMove(el, '.draggable({ disabled: false })');

el.draggable("option", "disabled", true);
equal(el.draggable("option", "disabled"), true, "disabled option setter");
shouldnotmove('.draggable("option", "disabled", true)');
TestHelpers.draggable.shouldNotMove(el, '.draggable("option", "disabled", true)');

var expected = $('<div></div>').draggable(),
actual = expected.draggable('disable');

0 comments on commit f0daa22

Please sign in to comment.
You can’t perform that action at this time.