michael@0: /* Any copyright is dedicated to the public domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: // Test that an iframe with the |mozbrowser| attribute emits mozbrowserloadX michael@0: // events when this page is in the whitelist. michael@0: michael@0: "use strict"; michael@0: SimpleTest.waitForExplicitFinish(); michael@0: browserElementTestHelpers.setEnabledPref(true); michael@0: browserElementTestHelpers.addPermission(); michael@0: michael@0: function runTest() { michael@0: // Load emptypage1 into the iframe, wait for that to finish loading, then michael@0: // call runTest2. michael@0: // michael@0: // This should trigger loadstart, locationchange, and loadend events. michael@0: michael@0: var seenLoadEnd = false; michael@0: var seenLoadStart = false; michael@0: var seenLocationChange = false; michael@0: michael@0: var iframe = document.createElement('iframe'); michael@0: SpecialPowers.wrap(iframe).mozbrowser = true; michael@0: iframe.id = 'iframe'; michael@0: iframe.src = 'http://example.com/tests/dom/browser-element/mochitest/file_browserElement_LoadEvents.html'; michael@0: michael@0: function loadstart(e) { michael@0: ok(e.isTrusted, 'Event should be trusted.'); michael@0: ok(!seenLoadEnd, 'loadstart before loadend.'); michael@0: ok(!seenLoadStart, 'Just one loadstart event.'); michael@0: ok(!seenLocationChange, 'loadstart before locationchange.'); michael@0: seenLoadStart = true; michael@0: } michael@0: michael@0: function locationchange(e) { michael@0: ok(e.isTrusted, 'Event should be trusted.'); michael@0: ok(!seenLocationChange, 'Just one locationchange event.'); michael@0: seenLocationChange = true; michael@0: ok(seenLoadStart, 'Location change after load start.'); michael@0: ok(!seenLoadEnd, 'Location change before load end.'); michael@0: ok(e.detail, browserElementTestHelpers.emptyPage1, "event's reported location"); michael@0: } michael@0: michael@0: function loadend(e) { michael@0: ok(e.isTrusted, 'Event should be trusted.'); michael@0: ok(seenLoadStart, 'loadend after loadstart.'); michael@0: ok(!seenLoadEnd, 'Just one loadend event.'); michael@0: ok(seenLocationChange, 'loadend after locationchange.'); michael@0: is(e.detail.backgroundColor, 'rgb(0, 128, 0)', 'Expected background color reported') michael@0: seenLoadEnd = true; michael@0: } michael@0: michael@0: iframe.addEventListener('mozbrowserloadstart', loadstart); michael@0: iframe.addEventListener('mozbrowserlocationchange', locationchange); michael@0: iframe.addEventListener('mozbrowserloadend', loadend); michael@0: michael@0: function waitForAllCallbacks() { michael@0: if (!seenLoadStart || !seenLoadEnd) { michael@0: SimpleTest.executeSoon(waitForAllCallbacks); michael@0: return; michael@0: } michael@0: michael@0: iframe.removeEventListener('mozbrowserloadstart', loadstart); michael@0: iframe.removeEventListener('mozbrowserlocationchange', locationchange); michael@0: iframe.removeEventListener('mozbrowserloadend', loadend); michael@0: runTest2(); michael@0: } michael@0: michael@0: document.body.appendChild(iframe); michael@0: waitForAllCallbacks(); michael@0: } michael@0: michael@0: function runTest2() { michael@0: var seenLoadStart = false; michael@0: var seenLoadEnd = false; michael@0: var seenLocationChange = false; michael@0: michael@0: // Add this event listener to the document; the events should bubble. michael@0: document.addEventListener('mozbrowserloadstart', function(e) { michael@0: ok(e.isTrusted, 'Event should be trusted.'); michael@0: ok(!seenLoadStart, 'Just one loadstart event.'); michael@0: seenLoadStart = true; michael@0: ok(!seenLoadEnd, 'Got mozbrowserloadstart before loadend.'); michael@0: ok(!seenLocationChange, 'Got mozbrowserloadstart before locationchange.'); michael@0: }); michael@0: michael@0: var iframe = document.getElementById('iframe'); michael@0: iframe.addEventListener('mozbrowserlocationchange', function(e) { michael@0: ok(e.isTrusted, 'Event should be trusted.'); michael@0: ok(!seenLocationChange, 'Just one locationchange event.'); michael@0: seenLocationChange = true; michael@0: ok(seenLoadStart, 'Location change after load start.'); michael@0: ok(!seenLoadEnd, 'Location change before load end.'); michael@0: ok(e.detail, browserElementTestHelpers.emptyPage2, "event's reported location"); michael@0: }); michael@0: michael@0: iframe.addEventListener('mozbrowserloadend', function(e) { michael@0: ok(e.isTrusted, 'Event should be trusted.'); michael@0: ok(!seenLoadEnd, 'Just one load end event.'); michael@0: seenLoadEnd = true; michael@0: ok(seenLoadStart, 'Load end after load start.'); michael@0: ok(seenLocationChange, 'Load end after location change.'); michael@0: is(e.detail.backgroundColor, 'transparent', 'Expected background color reported') michael@0: }); michael@0: michael@0: iframe.src = browserElementTestHelpers.emptyPage2; michael@0: michael@0: function waitForAllCallbacks() { michael@0: if (!seenLoadStart || !seenLoadEnd || !seenLocationChange) { michael@0: SimpleTest.executeSoon(waitForAllCallbacks); michael@0: return; michael@0: } michael@0: michael@0: SimpleTest.finish(); michael@0: } michael@0: michael@0: waitForAllCallbacks(); michael@0: } michael@0: michael@0: addEventListener('testready', runTest);