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 alert works. michael@0: "use strict"; michael@0: michael@0: SimpleTest.waitForExplicitFinish(); michael@0: browserElementTestHelpers.setEnabledPref(true); michael@0: browserElementTestHelpers.addPermission(); michael@0: michael@0: var numPendingChildTests = 0; michael@0: var iframe; michael@0: var mm; michael@0: michael@0: function runTest() { michael@0: iframe = document.createElement('iframe'); michael@0: SpecialPowers.wrap(iframe).mozbrowser = true; michael@0: document.body.appendChild(iframe); michael@0: michael@0: mm = SpecialPowers.getBrowserFrameMessageManager(iframe); michael@0: mm.addMessageListener('test-success', function(msg) { michael@0: numPendingChildTests--; michael@0: ok(true, SpecialPowers.wrap(msg).json); michael@0: }); michael@0: mm.addMessageListener('test-fail', function(msg) { michael@0: numPendingChildTests--; michael@0: ok(false, SpecialPowers.wrap(msg).json); michael@0: }); michael@0: michael@0: // Wait for the initial load to finish, then navigate the page, then wait michael@0: // for that load to finish, then start test1. michael@0: iframe.addEventListener('mozbrowserloadend', function loadend() { michael@0: iframe.removeEventListener('mozbrowserloadend', loadend); michael@0: iframe.src = browserElementTestHelpers.emptyPage1; michael@0: michael@0: iframe.addEventListener('mozbrowserloadend', function loadend2() { michael@0: iframe.removeEventListener('mozbrowserloadend', loadend2); michael@0: SimpleTest.executeSoon(test1); michael@0: }); michael@0: }); michael@0: michael@0: } michael@0: michael@0: function test1() { michael@0: iframe.addEventListener('mozbrowsershowmodalprompt', test2); michael@0: michael@0: // Do window.alert within the iframe, then modify the global |testState| michael@0: // after the alert. michael@0: var script = 'data:,\ michael@0: testState = 0; \ michael@0: content.alert("Hello, world!"); \ michael@0: testState = 1; \ michael@0: '; michael@0: michael@0: mm.loadFrameScript(script, /* allowDelayedLoad = */ false); michael@0: michael@0: // Triggers a mozbrowsershowmodalprompt event, which sends us down to test2. michael@0: } michael@0: michael@0: // test2 is a mozbrowsershowmodalprompt listener. michael@0: function test2(e) { michael@0: iframe.removeEventListener("mozbrowsershowmodalprompt", test2); michael@0: michael@0: is(e.detail.message, 'Hello, world!'); michael@0: e.preventDefault(); // cause the alert to block. michael@0: michael@0: SimpleTest.executeSoon(function() { test2a(e); }); michael@0: } michael@0: michael@0: function test2a(e) { michael@0: // The iframe should be blocked on the alert call at the moment, so testState michael@0: // should still be 0. michael@0: var script = 'data:,\ michael@0: if (testState === 0) { \ michael@0: sendAsyncMessage("test-success", "1: Correct testState"); \ michael@0: } \ michael@0: else { \ michael@0: sendAsyncMessage("test-fail", "1: Wrong testState: " + testState); \ michael@0: }'; michael@0: michael@0: mm.loadFrameScript(script, /* allowDelayedLoad = */ false); michael@0: numPendingChildTests++; michael@0: michael@0: waitForPendingTests(function() { test3(e); }); michael@0: } michael@0: michael@0: function test3(e) { michael@0: // Now unblock the iframe and check that the script completed. michael@0: e.detail.unblock(); michael@0: michael@0: var script2 = 'data:,\ michael@0: if (testState === 1) { \ michael@0: sendAsyncMessage("test-success", "2: Correct testState"); \ michael@0: } \ michael@0: else { \ michael@0: sendAsyncMessage("test-try-again", "2: Wrong testState (for now): " + testState); \ michael@0: }'; michael@0: michael@0: // Urgh. e.unblock() didn't necessarily unblock us immediately, so we have michael@0: // to spin and wait. michael@0: function onTryAgain() { michael@0: SimpleTest.executeSoon(function() { michael@0: //dump('onTryAgain\n'); michael@0: mm.loadFrameScript(script2, /* allowDelayedLoad = */ false); michael@0: }); michael@0: } michael@0: michael@0: mm.addMessageListener('test-try-again', onTryAgain); michael@0: numPendingChildTests++; michael@0: michael@0: onTryAgain(); michael@0: waitForPendingTests(function() { test4(); }); michael@0: } michael@0: michael@0: function test4() { michael@0: // Navigate the iframe while an alert is pending. This shouldn't screw michael@0: // things up. michael@0: michael@0: iframe.addEventListener("mozbrowsershowmodalprompt", test5); michael@0: michael@0: var script = 'data:,content.alert("test4");'; michael@0: mm.loadFrameScript(script, /* allowDelayedLoad = */ false); michael@0: } michael@0: michael@0: // test4 is a mozbrowsershowmodalprompt listener. michael@0: function test5(e) { michael@0: iframe.removeEventListener('mozbrowsershowmodalprompt', test4); michael@0: michael@0: is(e.detail.message, 'test4'); michael@0: e.preventDefault(); // cause the page to block. michael@0: michael@0: SimpleTest.executeSoon(test5a); michael@0: } michael@0: michael@0: function test5a() { michael@0: iframe.addEventListener('mozbrowserloadend', test5b); michael@0: iframe.src = browserElementTestHelpers.emptyPage2; michael@0: } michael@0: michael@0: function test5b() { michael@0: iframe.removeEventListener('mozbrowserloadend', test5b); michael@0: SimpleTest.finish(); michael@0: } michael@0: michael@0: var prevNumPendingTests = null; michael@0: function waitForPendingTests(next) { michael@0: if (numPendingChildTests !== prevNumPendingTests) { michael@0: dump("Waiting for end; " + numPendingChildTests + " pending tests\n"); michael@0: prevNumPendingTests = numPendingChildTests; michael@0: } michael@0: michael@0: if (numPendingChildTests > 0) { michael@0: SimpleTest.executeSoon(function() { waitForPendingTests(next); }); michael@0: return; michael@0: } michael@0: michael@0: prevNumPendingTests = null; michael@0: next(); michael@0: } michael@0: michael@0: addEventListener('testready', runTest);