dom/tests/mochitest/ajax/jquery/test/unit/event.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/ajax/jquery/test/unit/event.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,348 @@
     1.4 +module("event");
     1.5 +
     1.6 +test("bind(), with data", function() {
     1.7 +	expect(3);
     1.8 +	var handler = function(event) {
     1.9 +		ok( event.data, "bind() with data, check passed data exists" );
    1.10 +		equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
    1.11 +	};
    1.12 +	$("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
    1.13 +
    1.14 +	ok( !jQuery.data($("#firstp")[0], "events"), "Event handler unbound when using data." );
    1.15 +});
    1.16 +
    1.17 +test("bind(), with data, trigger with data", function() {
    1.18 +	expect(4);
    1.19 +	var handler = function(event, data) {
    1.20 +		ok( event.data, "check passed data exists" );
    1.21 +		equals( event.data.foo, "bar", "Check value of passed data" );
    1.22 +		ok( data, "Check trigger data" );
    1.23 +		equals( data.bar, "foo", "Check value of trigger data" );
    1.24 +	};
    1.25 +	$("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
    1.26 +});
    1.27 +
    1.28 +test("bind(), multiple events at once", function() {
    1.29 +	expect(2);
    1.30 +	var clickCounter = 0,
    1.31 +		mouseoverCounter = 0;
    1.32 +	var handler = function(event) {
    1.33 +		if (event.type == "click")
    1.34 +			clickCounter += 1;
    1.35 +		else if (event.type == "mouseover")
    1.36 +			mouseoverCounter += 1;
    1.37 +	};
    1.38 +	$("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
    1.39 +	equals( clickCounter, 1, "bind() with multiple events at once" );
    1.40 +	equals( mouseoverCounter, 1, "bind() with multiple events at once" );
    1.41 +});
    1.42 +
    1.43 +test("bind(), no data", function() {
    1.44 +	expect(1);
    1.45 +	var handler = function(event) {
    1.46 +		ok ( !event.data, "Check that no data is added to the event object" );
    1.47 +	};
    1.48 +	$("#firstp").bind("click", handler).trigger("click");
    1.49 +});
    1.50 +
    1.51 +test("bind(), iframes", function() {
    1.52 +	// events don't work with iframes, see #939 - this test fails in IE because of contentDocument
    1.53 +	// var doc = document.getElementById("iframe").contentDocument;
    1.54 +	// 
    1.55 +	// doc.body.innerHTML = "<input type='text'/>";
    1.56 +	//
    1.57 +	// var input = doc.getElementsByTagName("input")[0];
    1.58 +	//
    1.59 +	// $(input).bind("click",function() {
    1.60 +	// 	ok( true, "Binding to element inside iframe" );
    1.61 +	// }).click();
    1.62 +});
    1.63 +
    1.64 +test("bind(), trigger change on select", function() {
    1.65 +	expect(3);
    1.66 +	var counter = 0;
    1.67 +	function selectOnChange(event) {
    1.68 +		equals( event.data, counter++, "Event.data is not a global event object" );
    1.69 +	};
    1.70 +	$("#form select").each(function(i){
    1.71 +		$(this).bind('change', i, selectOnChange);
    1.72 +	}).trigger('change');
    1.73 +});
    1.74 +
    1.75 +test("bind(), namespaced events, cloned events", function() {
    1.76 +	expect(6);
    1.77 +
    1.78 +	$("#firstp").bind("custom.test",function(e){
    1.79 +		ok(true, "Custom event triggered");
    1.80 +	});
    1.81 +
    1.82 +	$("#firstp").bind("click",function(e){
    1.83 +		ok(true, "Normal click triggered");
    1.84 +	});
    1.85 +
    1.86 +	$("#firstp").bind("click.test",function(e){
    1.87 +		ok(true, "Namespaced click triggered");
    1.88 +	});
    1.89 +
    1.90 +	// Trigger both bound fn (2)
    1.91 +	$("#firstp").trigger("click");
    1.92 +
    1.93 +	// Trigger one bound fn (1)
    1.94 +	$("#firstp").trigger("click.test");
    1.95 +
    1.96 +	// Remove only the one fn
    1.97 +	$("#firstp").unbind("click.test");
    1.98 +
    1.99 +	// Trigger the remaining fn (1)
   1.100 +	$("#firstp").trigger("click");
   1.101 +
   1.102 +	// Remove the remaining fn
   1.103 +	$("#firstp").unbind(".test");
   1.104 +
   1.105 +	// Trigger the remaining fn (0)
   1.106 +	$("#firstp").trigger("custom");
   1.107 +
   1.108 +	// using contents will get comments regular, text, and comment nodes
   1.109 +	$("#nonnodes").contents().bind("tester", function () {
   1.110 +		equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
   1.111 +	}).trigger("tester");
   1.112 +
   1.113 +	// Make sure events stick with appendTo'd elements (which are cloned) #2027
   1.114 +	$("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
   1.115 +	ok( $("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
   1.116 +});
   1.117 +
   1.118 +test("trigger() shortcuts", function() {
   1.119 +	expect(6);
   1.120 +	$('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
   1.121 +		var close = $('spanx', this); // same with $(this).find('span');
   1.122 +		equals( close.length, 0, "Context element does not exist, length must be zero" );
   1.123 +		ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
   1.124 +		return false;
   1.125 +	}).click();
   1.126 +	
   1.127 +	$("#check1").click(function() {
   1.128 +		ok( true, "click event handler for checkbox gets fired twice, see #815" );
   1.129 +	}).click();
   1.130 +	
   1.131 +	var counter = 0;
   1.132 +	$('#firstp')[0].onclick = function(event) {
   1.133 +		counter++;
   1.134 +	};
   1.135 +	$('#firstp').click();
   1.136 +	equals( counter, 1, "Check that click, triggers onclick event handler also" );
   1.137 +	
   1.138 +	var clickCounter = 0;
   1.139 +	$('#simon1')[0].onclick = function(event) {
   1.140 +		clickCounter++;
   1.141 +	};
   1.142 +	$('#simon1').click();
   1.143 +	equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
   1.144 +	
   1.145 +	$('<img />').load(function(){
   1.146 +		ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
   1.147 +	}).load();
   1.148 +});
   1.149 +
   1.150 +test("unbind(event)", function() {
   1.151 +	expect(8);
   1.152 +	var el = $("#firstp");
   1.153 +	el.click(function() {
   1.154 +		ok( true, "Fake normal bind" );
   1.155 +	});
   1.156 +	el.click(function(event) {
   1.157 +		el.unbind(event);
   1.158 +		ok( true, "Fake onebind" );
   1.159 +	});
   1.160 +	el.click().click();
   1.161 +	
   1.162 +	el.click(function() { return; });
   1.163 +	el.unbind('click');
   1.164 +	ok( !el[0].onclick, "Handler is removed" ); // Bug #964
   1.165 +
   1.166 +	el.click(function() { return; });
   1.167 +	el.unbind('change',function(){ return; });
   1.168 +	for (var ret in jQuery.data(el[0], "events")['click']) break;
   1.169 +	ok( ret, "Extra handlers weren't accidentally removed." );
   1.170 +
   1.171 +	el.unbind('click');
   1.172 +	ok( !jQuery.data(el[0], "events"), "Removed the events expando after all handlers are unbound." );
   1.173 +	
   1.174 +	reset();
   1.175 +	var clickCounter = (mouseoverCounter = 0);
   1.176 +	var handler = function(event) {
   1.177 +		if (event.type == "click")
   1.178 +			clickCounter += 1;
   1.179 +		else if (event.type == "mouseover")
   1.180 +			mouseoverCounter += 1;
   1.181 +	};
   1.182 +	$("#firstp").bind("click mouseover", handler).unbind("click mouseover", handler).trigger("click").trigger("mouseover");
   1.183 +	equals( clickCounter, 0, "unbind() with multiple events at once" );
   1.184 +	equals( mouseoverCounter, 0, "unbind() with multiple events at once" );
   1.185 +});
   1.186 +
   1.187 +test("trigger(event, [data], [fn])", function() {
   1.188 +	expect(67);
   1.189 +
   1.190 +	var handler = function(event, a, b, c) {
   1.191 +		equals( event.type, "click", "check passed data" );
   1.192 +		equals( a, 1, "check passed data" );
   1.193 +		equals( b, "2", "check passed data" );
   1.194 +		equals( c, "abc", "check passed data" );
   1.195 +		return "test";
   1.196 +	};
   1.197 +
   1.198 +	var handler2 = function(a, b, c) {
   1.199 +		equals( a, 1, "check passed data" );
   1.200 +		equals( b, "2", "check passed data" );
   1.201 +		equals( c, "abc", "check passed data" );
   1.202 +		return false;
   1.203 +	};
   1.204 +
   1.205 +	var handler3 = function(a, b, c, v) {
   1.206 +		equals( a, 1, "check passed data" );
   1.207 +		equals( b, "2", "check passed data" );
   1.208 +		equals( c, "abc", "check passed data" );
   1.209 +		equals( v, "test", "check current value" );
   1.210 +		return "newVal";
   1.211 +	};
   1.212 +
   1.213 +	var handler4 = function(a, b, c, v) {
   1.214 +		equals( a, 1, "check passed data" );
   1.215 +		equals( b, "2", "check passed data" );
   1.216 +		equals( c, "abc", "check passed data" );
   1.217 +		equals( v, "test", "check current value" );
   1.218 +	};
   1.219 +
   1.220 +	// Simulate a "native" click
   1.221 +	$("#firstp")[0].click = function(){
   1.222 +		ok( true, "Native call was triggered" );
   1.223 +	};
   1.224 +
   1.225 +	// Triggers handlrs and native
   1.226 +	// Trigger 5
   1.227 +	$("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]);
   1.228 +
   1.229 +	// Triggers handlers, native, and extra fn
   1.230 +	// Triggers 9
   1.231 +	$("#firstp").trigger("click", [1, "2", "abc"], handler4);
   1.232 +
   1.233 +	// Simulate a "native" click
   1.234 +	$("#firstp")[0].click = function(){
   1.235 +		ok( false, "Native call was triggered" );
   1.236 +	};
   1.237 +
   1.238 +	// Triggers handlers, native, and extra fn
   1.239 +	// Triggers 7
   1.240 +	$("#firstp").trigger("click", [1, "2", "abc"], handler2);
   1.241 +
   1.242 +	// Trigger only the handlers (no native)
   1.243 +	// Triggers 5
   1.244 +	equals( $("#firstp").triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
   1.245 +
   1.246 +	// Trigger only the handlers (no native) and extra fn
   1.247 +	// Triggers 8
   1.248 +	equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );
   1.249 +
   1.250 +	// Build fake click event to pass in
   1.251 +	var eventObj = jQuery.event.fix({ type: "foo", target: document.body });
   1.252 +
   1.253 +	// Trigger only the handlers (no native), with external event obj
   1.254 +	// Triggers 5
   1.255 +	equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"]), "test", "Verify handler response" );
   1.256 +
   1.257 +	// Trigger only the handlers (no native) and extra fn, with external event obj
   1.258 +	// Triggers 9
   1.259 +	eventObj = jQuery.event.fix({ type: "foo", target: document.body });
   1.260 +	equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" );
   1.261 +	
   1.262 +	var pass = true;
   1.263 +	try {
   1.264 +		$('input:first')
   1.265 +			.hide()
   1.266 +			.trigger('focus');
   1.267 +	} catch(e) {
   1.268 +		pass = false;
   1.269 +	}
   1.270 +	ok( pass, "Trigger focus on hidden element" );
   1.271 +
   1.272 +	// have the extra handler override the return
   1.273 +	// Triggers 9
   1.274 +	equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );
   1.275 +
   1.276 +	// have the extra handler leave the return value alone
   1.277 +	// Triggers 9
   1.278 +	equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" );
   1.279 +});
   1.280 +
   1.281 +test("toggle(Function, Function, ...)", function() {
   1.282 +	expect(11);
   1.283 +	
   1.284 +	var count = 0,
   1.285 +		fn1 = function(e) { count++; },
   1.286 +		fn2 = function(e) { count--; },
   1.287 +		preventDefault = function(e) { e.preventDefault() },
   1.288 +		link = $('#mark');
   1.289 +	link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
   1.290 +	equals( count, 1, "Check for toggle(fn, fn)" );
   1.291 +
   1.292 +	$("#firstp").toggle(function () {
   1.293 +		equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
   1.294 +	}, function() {}).trigger("click", [ 1, 2, 3 ]);
   1.295 +
   1.296 +	var first = 0;
   1.297 +	$("#simon1").one("click", function() {
   1.298 +		ok( true, "Execute event only once" );
   1.299 +		$(this).toggle(function() {
   1.300 +			equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
   1.301 +		}, function() {
   1.302 +			equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
   1.303 +		});
   1.304 +		return false;
   1.305 +	}).click().click().click();
   1.306 +	
   1.307 +	var turn = 0;
   1.308 +	var fns = [
   1.309 +		function(){
   1.310 +			turn = 1;
   1.311 +		},
   1.312 +		function(){
   1.313 +			turn = 2;
   1.314 +		},
   1.315 +		function(){
   1.316 +			turn = 3;
   1.317 +		}
   1.318 +	];
   1.319 +	
   1.320 +	var $div = $("<div>&nbsp;</div>").toggle( fns[0], fns[1], fns[2] );
   1.321 +	$div.click();
   1.322 +	equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
   1.323 +	$div.click();
   1.324 +	equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
   1.325 +	$div.click();
   1.326 +	equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
   1.327 +	$div.click();
   1.328 +	equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
   1.329 +	$div.click();
   1.330 +	equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
   1.331 +	
   1.332 +	$div.unbind('click',fns[0]);
   1.333 +	var data = $.data( $div[0], 'events' );
   1.334 +	ok( !data, "Unbinding one function from toggle unbinds them all");
   1.335 +});
   1.336 +
   1.337 +test("jQuery(function($) {})", function() {
   1.338 +	stop();
   1.339 +	jQuery(function($) {
   1.340 +		equals(jQuery, $, "ready doesn't provide an event object, instead it provides a reference to the jQuery function, see http://docs.jquery.com/Events/ready#fn");
   1.341 +		start();
   1.342 +	});
   1.343 +});
   1.344 +
   1.345 +test("event properties", function() {
   1.346 +	stop();
   1.347 +	$("#simon1").click(function(event) {
   1.348 +		ok( event.timeStamp, "assert event.timeStamp is present" );
   1.349 +		start();
   1.350 +	}).click();
   1.351 +});

mercurial