aboutsummaryrefslogtreecommitdiffstats
path: root/ui/tests/draggable.js
blob: ce5d97ab448167d7b8747ef5db81e6b68cf3639f (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * draggable tests
 */

var drag = function(el, dx, dy, complete) {
	
	// speed = sync -> Drag syncrhonously.
	// speed = fast|slow -> Drag asyncrhonously - animated.
	
	return $(el).simulate("drag", {
		dx: dx||0, dy: dy||0, speed: 'sync', complete: complete 
	});
};

module("Draggable");

test("create and destroy", function() {
	
	expect(2);
	
	$("#draggable1").draggable();
	ok($("#draggable1").data("draggable"), "Accessing draggable instance after creation");
	
	$("#draggable1").draggable("destroy");
	ok(!$("#draggable1").data("draggable"), "Accessing draggable instance after destroy");
	
});

test("No options, relative", function() {
	
	expect(2);
	
	var dx = 50, dy = 50;
	
	$("#draggable1").draggable();
	var el = $("#draggable1").data("draggable").element;
	
	var before = el.offset();
	drag("#draggable1", dx, dy);
	var after = el.offset();	
	
	equals(after.left, before.left + dx, "Checking new left position");
	equals(after.top, before.top + dy, "Checking new top position");
	
});

test("No options, absolute", function() {
	
	expect(2);
	
	var dx = 50, dy = 50;
	
	$("#draggable2").draggable();
	var el = $("#draggable2").data("draggable").element;
	
	var before = el.offset();
	drag("#draggable2", dx, dy);
	var after = el.offset();	
	
	equals(after.left, before.left + dx, "Checking new left position");
	equals(after.top, before.top + dy, "Checking new top position");
	
});

test("{ helper: 'clone' }", function() {
	
	expect(4);
	
	var dx = 50, dy = 50;
	
	$("#draggable1, #draggable2").draggable({ helper: "clone" });
	var el1 = $("#draggable1").data("draggable").element;
	var el2 = $("#draggable2").data("draggable").element;
	
	var before1 = el1.offset();
	var before2 = el2.offset();
	drag("#draggable1, #draggable2", dx, dy);
	var after1 = el1.offset();
	var after2 = el2.offset();
	
	equals(after1.left, before1.left, "Relative: Original position should be untouched");
	equals(after1.top, before1.top, "Relative: Original position should be untouched");
	
	equals(after2.left, before2.left, "Absolute: Original position should be untouched");
	equals(after2.top, before2.top, "Absolute: Original position should be untouched");
	
});

test("{ grid: [50,50] }", function() {
	
	expect(4);
	
	var gx = 50, gy = 50;
	var dx = gx / 2 + 1, dy = gy / 2 + 1;
	
	$("#draggable1, #draggable2").draggable({ grid: [gx, gy] });
	var el1 = $("#draggable1").data("draggable").element;
	var el2 = $("#draggable2").data("draggable").element;
	
	var before1 = el1.offset();
	var before2 = el2.offset();
	drag("#draggable1, #draggable2", dx, dy);
	var after1 = el1.offset();
	var after2 = el2.offset();
	
	equals(after1.left, before1.left + gx, "Relative: 50px move in grid");
	equals(after1.top, before1.top + gy, "Relative: 50px move in grid");
	
	equals(after2.left, before2.left + gx, "Absolute: 50px move in grid");
	equals(after2.top, before2.top + gy, "Absolute: 50px move in grid");
	
});

test("{ grid: [50,50] }", function() {
	
	expect(4);
	
	var gx = 50, gy = 50;
	var dx = gx / 2 - 1, dy = gy / 2 - 1;
	
	$("#draggable1, #draggable2").draggable({ grid: [gx, gy] });
	var el1 = $("#draggable1").data("draggable").element;
	var el2 = $("#draggable2").data("draggable").element;
	
	var before1 = el1.offset();
	var before2 = el2.offset();
	drag("#draggable1, #draggable2", dx, dy);
	var after1 = el1.offset();
	var after2 = el2.offset();
	
	equals(after1.left, before1.left, "Relative: 50px move in grid");
	equals(after1.top, before1.top, "Relative: 50px move in grid");
	
	equals(after2.left, before2.left, "Absolute: 50px move in grid");
	equals(after2.top, before2.top, "Absolute: 50px move in grid");
	
});

test("{ axis: 'y' }", function() {
	
	expect(4);
	
	var dx = 50, dy = 50;
	
	$("#draggable1, #draggable2").draggable({ axis: "y" });
	var el1 = $("#draggable1").data("draggable").element;
	var el2 = $("#draggable2").data("draggable").element;
	
	var before1 = el1.offset();
	var before2 = el2.offset();
	drag("#draggable1, #draggable2", dx, dy);
	var after1 = el1.offset();
	var after2 = el2.offset();
	
	equals(after1.left, before1.left, "Relative: 0px move");
	equals(after1.top, before1.top + dy, "Relative: 50px move");
	
	equals(after2.left, before2.left, "Absolute: 0px move");
	equals(after2.top, before2.top + dy, "Absolute: 50px move");
	
});

test("{ axis: 'x' }", function() {
	
	expect(4);
	
	var dx = 50, dy = 50;
	
	$("#draggable1, #draggable2").draggable({ axis: "x" });
	var el1 = $("#draggable1").data("draggable").element;
	var el2 = $("#draggable2").data("draggable").element;
	
	var before1 = el1.offset();
	var before2 = el2.offset();
	drag("#draggable1, #draggable2", dx, dy);
	var after1 = el1.offset();
	var after2 = el2.offset();
	
	equals(after1.left, before1.left + dx, "Relative: 0px move");
	equals(after1.top, before1.top, "Relative: 50px move");
	
	equals(after2.left, before2.left + dy, "Absolute: 0px move");
	equals(after2.top, before2.top, "Absolute: 50px move");
	
});

test("{ cancel: 'span' }", function() {
	
	expect(4);
	
	var dx = 50, dy = 50;
	
	$("#draggable2").draggable({ cancel: 'span' });
	var el = $("#draggable2").data("draggable").element;
	
	var before = el.offset();
	drag("#draggable2 span", dx, dy);
	var after = el.offset();	
	
	equals(after.left, before.left, "Trigger on span: 0px move");
	equals(after.top, before.top, "Trigger on span: 0px move");
	
	var before = el.offset();
	drag("#draggable2", dx, dy);
	var after = el.offset();	
	
	equals(after.left, before.left + dx, "Trigger on element: 50px move");
	equals(after.top, before.top + dy, "Trigger on element: 50px move");
	
});

test("{ handle: 'span' }", function() {
	
	expect(4);
	
	var dx = 50, dy = 50;
	
	$("#draggable2").draggable({ handle: 'span' });
	var el = $("#draggable2").data("draggable").element;
	
	var before = el.offset();
	drag("#draggable2 span", dx, dy);
	var after = el.offset();	
	
	equals(after.left, before.left + dx, "Trigger on span: 50px move");
	equals(after.top, before.top + dy, "Trigger on span: 50px move");
	
	var before = el.offset();
	drag("#draggable2", dx, dy);
	var after = el.offset();	
	
	equals(after.left, before.left, "Trigger on element: 0px move");
	equals(after.top, before.top, "Trigger on element: 0px move");
	
});

test("{ containment: 'parent' }", function() {
	
	function border(el, side) { return parseInt(el.css('border-' + side + '-width')); }

	expect(4);

	var dx = -100, dy = -100;
	
	$("#draggable1, #draggable2").draggable({ containment: 'parent' });
	var el1 = $("#draggable1").data("draggable").element;
	var el2 = $("#draggable2").data("draggable").element;
	
	var before1 = el1.offset();
	var before2 = el2.offset();
	drag("#draggable1, #draggable2", dx, dy);
	var after1 = el1.offset();
	var after2 = el2.offset();

	var p1 = $("#draggable1").parent(), po1 = p1.offset();
	var p2 = $("#draggable2").parent(), po2 = p2.offset();
	var b1 = { left: border(p1, 'left'), top: border(p1, 'top') };
	var b2 = { left: border(p2, 'left'), top: border(p2, 'top') };
	
	equals(after1.left, po1.left + b1.left, "Absolute: " + (before1.left - po1.left + b1.left) + "px move (to parent's margin end)");
	equals(after1.top, po1.top + b1.top, "Absolute: " + (before1.top - po1.top + b1.top) + "px move (to parent's margin end)");
	
	equals(after2.left, po2.left + b2.left, "Relative: " + (before2.left - po2.left + b2.left) + "move (to parent's margin end)");
	equals(after2.top, po2.top + b2.top, "Relative: " + (before2.top - po2.top + b2.top) + "px move (to parent's margin end)");
	
});

test("{ cursorAt: { left: -5, top: -5 } }", function() {
	
	expect(4);
	
	var dx = -1, dy = -1;
	var ox = 5, oy = 5;
	var cax = -5, cay = -5;
	
	var actual = null;
	$("#draggable2").draggable({
		cursorAt: { left: cax, top: cay },
		drag: function(e, ui) {
			actual = ui.absolutePosition;
		}
	});
	var el = $("#draggable2").data("draggable").element;
	
	var before = el.offset();
	var pos = { clientX: before.left + ox, clientY: before.top + oy };
	$("#draggable2").simulate("mousedown", pos);
	pos = { clientX: pos.clientX + dx, clientY: pos.clienY + dy };
	$(document).simulate("mousedown", pos);
	$(document).simulate("mousedown", pos);
	$("#draggable2").simulate("mouseup", pos);
	var expected = { left: before.left + ox - cax, top: before.top + oy - cay };
	
	equals(actual.left, expected.left, "Absolute: -1px left");
	equals(actual.top, expected.top, "Absolute: -1px top");
	
	var actual = null;
	$("#draggable1").draggable({
		cursorAt: { left: cax, top: cay },
		drag: function(e, ui) {
			actual = ui.absolutePosition;
		}
	});
	var el = $("#draggable2").data("draggable").element;
	
	var before = el.offset();
	var pos = { clientX: before.left + ox, clientY: before.top + oy };
	$("#draggable2").simulate("mousedown", pos);
	pos = { clientX: pos.clientX + dx, clientY: pos.clienY + dy };
	$(document).simulate("mousedown", pos);
	$(document).simulate("mousedown", pos);
	$("#draggable2").simulate("mouseup", pos);
	var expected = { left: before.left + ox - cax, top: before.top + oy - cay };
	
	equals(actual.left, expected.left, "Relative: -1px left");
	equals(actual.top, expected.top, "Relative: -1px top");
	
});

test("{ cursor: 'move' }", function() {
	
	function getCursor() { return $("body").css("cursor"); }
	
	expect(2);
	
	var expected = "move", actual, before, after;
	
	$("#draggable2").draggable({
		cursor: expected,
		start: function(e, ui) {
			actual = getCursor();
		}
	});
	
	before = getCursor();
	drag("#draggable2", -1, -1);
	after = getCursor();
	
	equals(actual, expected, "start callback: cursor '" + expected + "'");
	equals(after, before, "after drag: cursor restored");
	
});

test("{ distance: 10 }", function() {
	
	expect(3);
	
	var dragged;
	$("#draggable2").draggable({
		distance: 10,
		start: function(e, ui) {
			dragged = true;
		}
	});
	
	dragged = false;
	drag("#draggable2", -9, -9);
	equals(dragged, false, "The draggable should not have moved when moving -9px");
	
	dragged = false;
	drag("#draggable2", -10, -10);
	equals(dragged, true, "The draggable should have moved when moving -10px");
	
	dragged = false;
	drag("#draggable2", 9, 9);
	equals(dragged, false, "The draggable should not have moved when moving 9px");
	
});

test("{ opacity: 0.5 }", function() {
	
	expect(1);
	
	var opacity = null;
	$("#draggable2").draggable({
		opacity: 0.5,
		start: function(e, ui) {
			opacity = $(this).css("opacity");
		}
	});
	
	drag("#draggable2", -1, -1);
	
	equals(opacity, 0.5, "start callback: opacity is");
	
});

test("{ zIndex: 10 }", function() {
	
	expect(1);

	var expected = 10, actual;
	
	var zIndex = null;
	$("#draggable2").draggable({
		zIndex: expected,
		start: function(e, ui) {
			actual = $(this).css("zIndex");
		}
	});
	
	drag("#draggable2", -1, -1);
	
	equals(actual, expected, "start callback: zIndex is");
	
});

test("callbacks occurance count", function() {
	
	expect(3);
	
	var start = 0, stop = 0, dragc = 0;
	$("#draggable2").draggable({
		start: function() { start++; },
		drag: function() { dragc++; },
		stop: function() { stop++; }
	});
	
	drag("#draggable2", 10, 10);
	
	equals(start, 1, "start callback should happen exactly once");
	equals(dragc, 11, "drag callback should happen exactly 1+10 times (first simultaneously with start)");
	equals(stop, 1, "stop callback should happen exactly once");
	
});