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.

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

mercurial