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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 module("event");
     3 test("bind(), with data", function() {
     4 	expect(3);
     5 	var handler = function(event) {
     6 		ok( event.data, "bind() with data, check passed data exists" );
     7 		equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
     8 	};
     9 	$("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
    11 	ok( !jQuery.data($("#firstp")[0], "events"), "Event handler unbound when using data." );
    12 });
    14 test("bind(), with data, trigger with data", function() {
    15 	expect(4);
    16 	var handler = function(event, data) {
    17 		ok( event.data, "check passed data exists" );
    18 		equals( event.data.foo, "bar", "Check value of passed data" );
    19 		ok( data, "Check trigger data" );
    20 		equals( data.bar, "foo", "Check value of trigger data" );
    21 	};
    22 	$("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
    23 });
    25 test("bind(), multiple events at once", function() {
    26 	expect(2);
    27 	var clickCounter = 0,
    28 		mouseoverCounter = 0;
    29 	var handler = function(event) {
    30 		if (event.type == "click")
    31 			clickCounter += 1;
    32 		else if (event.type == "mouseover")
    33 			mouseoverCounter += 1;
    34 	};
    35 	$("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
    36 	equals( clickCounter, 1, "bind() with multiple events at once" );
    37 	equals( mouseoverCounter, 1, "bind() with multiple events at once" );
    38 });
    40 test("bind(), no data", function() {
    41 	expect(1);
    42 	var handler = function(event) {
    43 		ok ( !event.data, "Check that no data is added to the event object" );
    44 	};
    45 	$("#firstp").bind("click", handler).trigger("click");
    46 });
    48 test("bind(), iframes", function() {
    49 	// events don't work with iframes, see #939 - this test fails in IE because of contentDocument
    50 	// var doc = document.getElementById("iframe").contentDocument;
    51 	// 
    52 	// doc.body.innerHTML = "<input type='text'/>";
    53 	//
    54 	// var input = doc.getElementsByTagName("input")[0];
    55 	//
    56 	// $(input).bind("click",function() {
    57 	// 	ok( true, "Binding to element inside iframe" );
    58 	// }).click();
    59 });
    61 test("bind(), trigger change on select", function() {
    62 	expect(3);
    63 	var counter = 0;
    64 	function selectOnChange(event) {
    65 		equals( event.data, counter++, "Event.data is not a global event object" );
    66 	};
    67 	$("#form select").each(function(i){
    68 		$(this).bind('change', i, selectOnChange);
    69 	}).trigger('change');
    70 });
    72 test("bind(), namespaced events, cloned events", function() {
    73 	expect(6);
    75 	$("#firstp").bind("custom.test",function(e){
    76 		ok(true, "Custom event triggered");
    77 	});
    79 	$("#firstp").bind("click",function(e){
    80 		ok(true, "Normal click triggered");
    81 	});
    83 	$("#firstp").bind("click.test",function(e){
    84 		ok(true, "Namespaced click triggered");
    85 	});
    87 	// Trigger both bound fn (2)
    88 	$("#firstp").trigger("click");
    90 	// Trigger one bound fn (1)
    91 	$("#firstp").trigger("click.test");
    93 	// Remove only the one fn
    94 	$("#firstp").unbind("click.test");
    96 	// Trigger the remaining fn (1)
    97 	$("#firstp").trigger("click");
    99 	// Remove the remaining fn
   100 	$("#firstp").unbind(".test");
   102 	// Trigger the remaining fn (0)
   103 	$("#firstp").trigger("custom");
   105 	// using contents will get comments regular, text, and comment nodes
   106 	$("#nonnodes").contents().bind("tester", function () {
   107 		equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
   108 	}).trigger("tester");
   110 	// Make sure events stick with appendTo'd elements (which are cloned) #2027
   111 	$("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
   112 	ok( $("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
   113 });
   115 test("trigger() shortcuts", function() {
   116 	expect(6);
   117 	$('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
   118 		var close = $('spanx', this); // same with $(this).find('span');
   119 		equals( close.length, 0, "Context element does not exist, length must be zero" );
   120 		ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
   121 		return false;
   122 	}).click();
   124 	$("#check1").click(function() {
   125 		ok( true, "click event handler for checkbox gets fired twice, see #815" );
   126 	}).click();
   128 	var counter = 0;
   129 	$('#firstp')[0].onclick = function(event) {
   130 		counter++;
   131 	};
   132 	$('#firstp').click();
   133 	equals( counter, 1, "Check that click, triggers onclick event handler also" );
   135 	var clickCounter = 0;
   136 	$('#simon1')[0].onclick = function(event) {
   137 		clickCounter++;
   138 	};
   139 	$('#simon1').click();
   140 	equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
   142 	$('<img />').load(function(){
   143 		ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
   144 	}).load();
   145 });
   147 test("unbind(event)", function() {
   148 	expect(8);
   149 	var el = $("#firstp");
   150 	el.click(function() {
   151 		ok( true, "Fake normal bind" );
   152 	});
   153 	el.click(function(event) {
   154 		el.unbind(event);
   155 		ok( true, "Fake onebind" );
   156 	});
   157 	el.click().click();
   159 	el.click(function() { return; });
   160 	el.unbind('click');
   161 	ok( !el[0].onclick, "Handler is removed" ); // Bug #964
   163 	el.click(function() { return; });
   164 	el.unbind('change',function(){ return; });
   165 	for (var ret in jQuery.data(el[0], "events")['click']) break;
   166 	ok( ret, "Extra handlers weren't accidentally removed." );
   168 	el.unbind('click');
   169 	ok( !jQuery.data(el[0], "events"), "Removed the events expando after all handlers are unbound." );
   171 	reset();
   172 	var clickCounter = (mouseoverCounter = 0);
   173 	var handler = function(event) {
   174 		if (event.type == "click")
   175 			clickCounter += 1;
   176 		else if (event.type == "mouseover")
   177 			mouseoverCounter += 1;
   178 	};
   179 	$("#firstp").bind("click mouseover", handler).unbind("click mouseover", handler).trigger("click").trigger("mouseover");
   180 	equals( clickCounter, 0, "unbind() with multiple events at once" );
   181 	equals( mouseoverCounter, 0, "unbind() with multiple events at once" );
   182 });
   184 test("trigger(event, [data], [fn])", function() {
   185 	expect(67);
   187 	var handler = function(event, a, b, c) {
   188 		equals( event.type, "click", "check passed data" );
   189 		equals( a, 1, "check passed data" );
   190 		equals( b, "2", "check passed data" );
   191 		equals( c, "abc", "check passed data" );
   192 		return "test";
   193 	};
   195 	var handler2 = function(a, b, c) {
   196 		equals( a, 1, "check passed data" );
   197 		equals( b, "2", "check passed data" );
   198 		equals( c, "abc", "check passed data" );
   199 		return false;
   200 	};
   202 	var handler3 = function(a, b, c, v) {
   203 		equals( a, 1, "check passed data" );
   204 		equals( b, "2", "check passed data" );
   205 		equals( c, "abc", "check passed data" );
   206 		equals( v, "test", "check current value" );
   207 		return "newVal";
   208 	};
   210 	var handler4 = function(a, b, c, v) {
   211 		equals( a, 1, "check passed data" );
   212 		equals( b, "2", "check passed data" );
   213 		equals( c, "abc", "check passed data" );
   214 		equals( v, "test", "check current value" );
   215 	};
   217 	// Simulate a "native" click
   218 	$("#firstp")[0].click = function(){
   219 		ok( true, "Native call was triggered" );
   220 	};
   222 	// Triggers handlrs and native
   223 	// Trigger 5
   224 	$("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]);
   226 	// Triggers handlers, native, and extra fn
   227 	// Triggers 9
   228 	$("#firstp").trigger("click", [1, "2", "abc"], handler4);
   230 	// Simulate a "native" click
   231 	$("#firstp")[0].click = function(){
   232 		ok( false, "Native call was triggered" );
   233 	};
   235 	// Triggers handlers, native, and extra fn
   236 	// Triggers 7
   237 	$("#firstp").trigger("click", [1, "2", "abc"], handler2);
   239 	// Trigger only the handlers (no native)
   240 	// Triggers 5
   241 	equals( $("#firstp").triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
   243 	// Trigger only the handlers (no native) and extra fn
   244 	// Triggers 8
   245 	equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );
   247 	// Build fake click event to pass in
   248 	var eventObj = jQuery.event.fix({ type: "foo", target: document.body });
   250 	// Trigger only the handlers (no native), with external event obj
   251 	// Triggers 5
   252 	equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"]), "test", "Verify handler response" );
   254 	// Trigger only the handlers (no native) and extra fn, with external event obj
   255 	// Triggers 9
   256 	eventObj = jQuery.event.fix({ type: "foo", target: document.body });
   257 	equals( $("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" );
   259 	var pass = true;
   260 	try {
   261 		$('input:first')
   262 			.hide()
   263 			.trigger('focus');
   264 	} catch(e) {
   265 		pass = false;
   266 	}
   267 	ok( pass, "Trigger focus on hidden element" );
   269 	// have the extra handler override the return
   270 	// Triggers 9
   271 	equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );
   273 	// have the extra handler leave the return value alone
   274 	// Triggers 9
   275 	equals( $("#firstp").triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" );
   276 });
   278 test("toggle(Function, Function, ...)", function() {
   279 	expect(11);
   281 	var count = 0,
   282 		fn1 = function(e) { count++; },
   283 		fn2 = function(e) { count--; },
   284 		preventDefault = function(e) { e.preventDefault() },
   285 		link = $('#mark');
   286 	link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
   287 	equals( count, 1, "Check for toggle(fn, fn)" );
   289 	$("#firstp").toggle(function () {
   290 		equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
   291 	}, function() {}).trigger("click", [ 1, 2, 3 ]);
   293 	var first = 0;
   294 	$("#simon1").one("click", function() {
   295 		ok( true, "Execute event only once" );
   296 		$(this).toggle(function() {
   297 			equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
   298 		}, function() {
   299 			equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
   300 		});
   301 		return false;
   302 	}).click().click().click();
   304 	var turn = 0;
   305 	var fns = [
   306 		function(){
   307 			turn = 1;
   308 		},
   309 		function(){
   310 			turn = 2;
   311 		},
   312 		function(){
   313 			turn = 3;
   314 		}
   315 	];
   317 	var $div = $("<div>&nbsp;</div>").toggle( fns[0], fns[1], fns[2] );
   318 	$div.click();
   319 	equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
   320 	$div.click();
   321 	equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
   322 	$div.click();
   323 	equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
   324 	$div.click();
   325 	equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
   326 	$div.click();
   327 	equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
   329 	$div.unbind('click',fns[0]);
   330 	var data = $.data( $div[0], 'events' );
   331 	ok( !data, "Unbinding one function from toggle unbinds them all");
   332 });
   334 test("jQuery(function($) {})", function() {
   335 	stop();
   336 	jQuery(function($) {
   337 		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");
   338 		start();
   339 	});
   340 });
   342 test("event properties", function() {
   343 	stop();
   344 	$("#simon1").click(function(event) {
   345 		ok( event.timeStamp, "assert event.timeStamp is present" );
   346 		start();
   347 	}).click();
   348 });

mercurial