aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/draggable/draggable_test_helpers.js
blob: a4a0d31998af053a4fb401ab2f1ed900d28d687c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
TestHelpers.draggable = {
	// TODO: remove the unreliable offset hacks
	unreliableOffset: $.ui.ie && ( !document.documentMode || document.documentMode < 8 ) ? 2 : 0,
	// Support: Opera 12.10, Safari 5.1, jQuery <1.8
	unreliableContains: (function(){
		var element = $( "<div>" );
		return $.contains( element[ 0 ].ownerDocument, element[ 0 ] );
	})(),
	testDrag: function( el, handle, dx, dy, expectedDX, expectedDY, msg ) {
		msg = msg ? msg + "." : "";

		var offsetActual,
			offsetBefore = el.offset(),
			offsetExpected = { left: offsetBefore.left + expectedDX, top: offsetBefore.top + expectedDY };

		$( el ).one( "dragstop", function( /* event, ui */ ) {
			// var expectedPosition = { left: ui.originalPosition.left + expectedDX, top: ui.originalPosition.top + expectedDY };
			// TODO: fix test bugs and actual bugs that cause this not to be true
			// deepEqual( ui.position, expectedPosition, "position dragged[" + dx + ", " + dy + "] " + msg );
			ok( true, "TODO: deepEqual( ui.position, expectedPosition, 'position dragged[" + dx + ", " + dy + "] " + msg + "');");
		} );

		$( handle ).simulate( "drag", {
			dx: dx,
			dy: dy,
			// moves is 1 here because simulate currently fire events synchronously
			// so we can't faithfully test things that rely on a scroll event (which is async)
			moves: 1
		});

		offsetActual = el.offset();

		deepEqual( offsetActual, offsetExpected, "offset dragged[" + dx + ", " + dy + "] " + msg );
	},
	shouldMove: function( el, why, handle ) {
		handle = handle || el;
		TestHelpers.draggable.testDrag( el, handle, 50, 50, 50, 50, why );
	},
	shouldNotMove: function( el, why, handle ) {
		handle = handle || el;
		TestHelpers.draggable.testDrag( el, handle, 50, 50, 0, 0, why );
	},
	shouldNotDrag: function( el, why, handle ) {
		$( el ).bind( "dragstop", function() {
			ok( false, "should not drag " + why );
		} );
		TestHelpers.draggable.shouldNotMove( el, why, handle );
		$( el ).unbind( "dragstop" );
	},
	testScroll: function( el, position ) {
		var oldPosition = $( "#main" ).css( "position" );
		$( "#main" ).css( "position", position);
		TestHelpers.draggable.shouldMove( el, position + " parent" );
		$( "#main" ).css( "position", oldPosition );
	},
	restoreScroll: function( what ) {
		$( what ).scrollTop( 0 ).scrollLeft( 0 );
	},
	setScroll: function( what ) {
		$( what ).scrollTop( 100 ).scrollLeft( 100 );
	},
	border: function( el, side ) {
		return parseInt( el.css( "border-" + side + "-width" ), 10 ) || 0;
	},
	margin: function( el, side ) {
		return parseInt( el.css( "margin-" + side ), 10 ) || 0;
	},
	move: function( el, x, y ) {
		$( el ).simulate( "drag", {
			dx: x,
			dy: y
		});
	},
	trackMouseCss : function( el ) {
		el.bind( "drag", function() {
			el.data( "last_dragged_cursor", $( "body" ).css( "cursor" ) );
		});
	},
	trackAppendedParent : function( el ) {
		// TODO: appendTo is currently ignored if helper is original (see #7044)
		el.draggable( "option", "helper", "clone" );

		// Get what parent is at time of drag
		el.bind( "drag", function(e,ui) {
			el.data( "last_dragged_parent", ui.helper.parent()[ 0 ] );
		});
	}
};