1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/browser-element/mochitest/browserElement_Alert.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,161 @@ 1.4 +/* Any copyright is dedicated to the public domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +// Test that alert works. 1.8 +"use strict"; 1.9 + 1.10 +SimpleTest.waitForExplicitFinish(); 1.11 +browserElementTestHelpers.setEnabledPref(true); 1.12 +browserElementTestHelpers.addPermission(); 1.13 + 1.14 +var numPendingChildTests = 0; 1.15 +var iframe; 1.16 +var mm; 1.17 + 1.18 +function runTest() { 1.19 + iframe = document.createElement('iframe'); 1.20 + SpecialPowers.wrap(iframe).mozbrowser = true; 1.21 + document.body.appendChild(iframe); 1.22 + 1.23 + mm = SpecialPowers.getBrowserFrameMessageManager(iframe); 1.24 + mm.addMessageListener('test-success', function(msg) { 1.25 + numPendingChildTests--; 1.26 + ok(true, SpecialPowers.wrap(msg).json); 1.27 + }); 1.28 + mm.addMessageListener('test-fail', function(msg) { 1.29 + numPendingChildTests--; 1.30 + ok(false, SpecialPowers.wrap(msg).json); 1.31 + }); 1.32 + 1.33 + // Wait for the initial load to finish, then navigate the page, then wait 1.34 + // for that load to finish, then start test1. 1.35 + iframe.addEventListener('mozbrowserloadend', function loadend() { 1.36 + iframe.removeEventListener('mozbrowserloadend', loadend); 1.37 + iframe.src = browserElementTestHelpers.emptyPage1; 1.38 + 1.39 + iframe.addEventListener('mozbrowserloadend', function loadend2() { 1.40 + iframe.removeEventListener('mozbrowserloadend', loadend2); 1.41 + SimpleTest.executeSoon(test1); 1.42 + }); 1.43 + }); 1.44 + 1.45 +} 1.46 + 1.47 +function test1() { 1.48 + iframe.addEventListener('mozbrowsershowmodalprompt', test2); 1.49 + 1.50 + // Do window.alert within the iframe, then modify the global |testState| 1.51 + // after the alert. 1.52 + var script = 'data:,\ 1.53 + testState = 0; \ 1.54 + content.alert("Hello, world!"); \ 1.55 + testState = 1; \ 1.56 + '; 1.57 + 1.58 + mm.loadFrameScript(script, /* allowDelayedLoad = */ false); 1.59 + 1.60 + // Triggers a mozbrowsershowmodalprompt event, which sends us down to test2. 1.61 +} 1.62 + 1.63 +// test2 is a mozbrowsershowmodalprompt listener. 1.64 +function test2(e) { 1.65 + iframe.removeEventListener("mozbrowsershowmodalprompt", test2); 1.66 + 1.67 + is(e.detail.message, 'Hello, world!'); 1.68 + e.preventDefault(); // cause the alert to block. 1.69 + 1.70 + SimpleTest.executeSoon(function() { test2a(e); }); 1.71 +} 1.72 + 1.73 +function test2a(e) { 1.74 + // The iframe should be blocked on the alert call at the moment, so testState 1.75 + // should still be 0. 1.76 + var script = 'data:,\ 1.77 + if (testState === 0) { \ 1.78 + sendAsyncMessage("test-success", "1: Correct testState"); \ 1.79 + } \ 1.80 + else { \ 1.81 + sendAsyncMessage("test-fail", "1: Wrong testState: " + testState); \ 1.82 + }'; 1.83 + 1.84 + mm.loadFrameScript(script, /* allowDelayedLoad = */ false); 1.85 + numPendingChildTests++; 1.86 + 1.87 + waitForPendingTests(function() { test3(e); }); 1.88 +} 1.89 + 1.90 +function test3(e) { 1.91 + // Now unblock the iframe and check that the script completed. 1.92 + e.detail.unblock(); 1.93 + 1.94 + var script2 = 'data:,\ 1.95 + if (testState === 1) { \ 1.96 + sendAsyncMessage("test-success", "2: Correct testState"); \ 1.97 + } \ 1.98 + else { \ 1.99 + sendAsyncMessage("test-try-again", "2: Wrong testState (for now): " + testState); \ 1.100 + }'; 1.101 + 1.102 + // Urgh. e.unblock() didn't necessarily unblock us immediately, so we have 1.103 + // to spin and wait. 1.104 + function onTryAgain() { 1.105 + SimpleTest.executeSoon(function() { 1.106 + //dump('onTryAgain\n'); 1.107 + mm.loadFrameScript(script2, /* allowDelayedLoad = */ false); 1.108 + }); 1.109 + } 1.110 + 1.111 + mm.addMessageListener('test-try-again', onTryAgain); 1.112 + numPendingChildTests++; 1.113 + 1.114 + onTryAgain(); 1.115 + waitForPendingTests(function() { test4(); }); 1.116 +} 1.117 + 1.118 +function test4() { 1.119 + // Navigate the iframe while an alert is pending. This shouldn't screw 1.120 + // things up. 1.121 + 1.122 + iframe.addEventListener("mozbrowsershowmodalprompt", test5); 1.123 + 1.124 + var script = 'data:,content.alert("test4");'; 1.125 + mm.loadFrameScript(script, /* allowDelayedLoad = */ false); 1.126 +} 1.127 + 1.128 +// test4 is a mozbrowsershowmodalprompt listener. 1.129 +function test5(e) { 1.130 + iframe.removeEventListener('mozbrowsershowmodalprompt', test4); 1.131 + 1.132 + is(e.detail.message, 'test4'); 1.133 + e.preventDefault(); // cause the page to block. 1.134 + 1.135 + SimpleTest.executeSoon(test5a); 1.136 +} 1.137 + 1.138 +function test5a() { 1.139 + iframe.addEventListener('mozbrowserloadend', test5b); 1.140 + iframe.src = browserElementTestHelpers.emptyPage2; 1.141 +} 1.142 + 1.143 +function test5b() { 1.144 + iframe.removeEventListener('mozbrowserloadend', test5b); 1.145 + SimpleTest.finish(); 1.146 +} 1.147 + 1.148 +var prevNumPendingTests = null; 1.149 +function waitForPendingTests(next) { 1.150 + if (numPendingChildTests !== prevNumPendingTests) { 1.151 + dump("Waiting for end; " + numPendingChildTests + " pending tests\n"); 1.152 + prevNumPendingTests = numPendingChildTests; 1.153 + } 1.154 + 1.155 + if (numPendingChildTests > 0) { 1.156 + SimpleTest.executeSoon(function() { waitForPendingTests(next); }); 1.157 + return; 1.158 + } 1.159 + 1.160 + prevNumPendingTests = null; 1.161 + next(); 1.162 +} 1.163 + 1.164 +addEventListener('testready', runTest);