dom/browser-element/mochitest/browserElement_Alert.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 /* Any copyright is dedicated to the public domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 // Test that alert works.
michael@0 5 "use strict";
michael@0 6
michael@0 7 SimpleTest.waitForExplicitFinish();
michael@0 8 browserElementTestHelpers.setEnabledPref(true);
michael@0 9 browserElementTestHelpers.addPermission();
michael@0 10
michael@0 11 var numPendingChildTests = 0;
michael@0 12 var iframe;
michael@0 13 var mm;
michael@0 14
michael@0 15 function runTest() {
michael@0 16 iframe = document.createElement('iframe');
michael@0 17 SpecialPowers.wrap(iframe).mozbrowser = true;
michael@0 18 document.body.appendChild(iframe);
michael@0 19
michael@0 20 mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
michael@0 21 mm.addMessageListener('test-success', function(msg) {
michael@0 22 numPendingChildTests--;
michael@0 23 ok(true, SpecialPowers.wrap(msg).json);
michael@0 24 });
michael@0 25 mm.addMessageListener('test-fail', function(msg) {
michael@0 26 numPendingChildTests--;
michael@0 27 ok(false, SpecialPowers.wrap(msg).json);
michael@0 28 });
michael@0 29
michael@0 30 // Wait for the initial load to finish, then navigate the page, then wait
michael@0 31 // for that load to finish, then start test1.
michael@0 32 iframe.addEventListener('mozbrowserloadend', function loadend() {
michael@0 33 iframe.removeEventListener('mozbrowserloadend', loadend);
michael@0 34 iframe.src = browserElementTestHelpers.emptyPage1;
michael@0 35
michael@0 36 iframe.addEventListener('mozbrowserloadend', function loadend2() {
michael@0 37 iframe.removeEventListener('mozbrowserloadend', loadend2);
michael@0 38 SimpleTest.executeSoon(test1);
michael@0 39 });
michael@0 40 });
michael@0 41
michael@0 42 }
michael@0 43
michael@0 44 function test1() {
michael@0 45 iframe.addEventListener('mozbrowsershowmodalprompt', test2);
michael@0 46
michael@0 47 // Do window.alert within the iframe, then modify the global |testState|
michael@0 48 // after the alert.
michael@0 49 var script = 'data:,\
michael@0 50 testState = 0; \
michael@0 51 content.alert("Hello, world!"); \
michael@0 52 testState = 1; \
michael@0 53 ';
michael@0 54
michael@0 55 mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
michael@0 56
michael@0 57 // Triggers a mozbrowsershowmodalprompt event, which sends us down to test2.
michael@0 58 }
michael@0 59
michael@0 60 // test2 is a mozbrowsershowmodalprompt listener.
michael@0 61 function test2(e) {
michael@0 62 iframe.removeEventListener("mozbrowsershowmodalprompt", test2);
michael@0 63
michael@0 64 is(e.detail.message, 'Hello, world!');
michael@0 65 e.preventDefault(); // cause the alert to block.
michael@0 66
michael@0 67 SimpleTest.executeSoon(function() { test2a(e); });
michael@0 68 }
michael@0 69
michael@0 70 function test2a(e) {
michael@0 71 // The iframe should be blocked on the alert call at the moment, so testState
michael@0 72 // should still be 0.
michael@0 73 var script = 'data:,\
michael@0 74 if (testState === 0) { \
michael@0 75 sendAsyncMessage("test-success", "1: Correct testState"); \
michael@0 76 } \
michael@0 77 else { \
michael@0 78 sendAsyncMessage("test-fail", "1: Wrong testState: " + testState); \
michael@0 79 }';
michael@0 80
michael@0 81 mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
michael@0 82 numPendingChildTests++;
michael@0 83
michael@0 84 waitForPendingTests(function() { test3(e); });
michael@0 85 }
michael@0 86
michael@0 87 function test3(e) {
michael@0 88 // Now unblock the iframe and check that the script completed.
michael@0 89 e.detail.unblock();
michael@0 90
michael@0 91 var script2 = 'data:,\
michael@0 92 if (testState === 1) { \
michael@0 93 sendAsyncMessage("test-success", "2: Correct testState"); \
michael@0 94 } \
michael@0 95 else { \
michael@0 96 sendAsyncMessage("test-try-again", "2: Wrong testState (for now): " + testState); \
michael@0 97 }';
michael@0 98
michael@0 99 // Urgh. e.unblock() didn't necessarily unblock us immediately, so we have
michael@0 100 // to spin and wait.
michael@0 101 function onTryAgain() {
michael@0 102 SimpleTest.executeSoon(function() {
michael@0 103 //dump('onTryAgain\n');
michael@0 104 mm.loadFrameScript(script2, /* allowDelayedLoad = */ false);
michael@0 105 });
michael@0 106 }
michael@0 107
michael@0 108 mm.addMessageListener('test-try-again', onTryAgain);
michael@0 109 numPendingChildTests++;
michael@0 110
michael@0 111 onTryAgain();
michael@0 112 waitForPendingTests(function() { test4(); });
michael@0 113 }
michael@0 114
michael@0 115 function test4() {
michael@0 116 // Navigate the iframe while an alert is pending. This shouldn't screw
michael@0 117 // things up.
michael@0 118
michael@0 119 iframe.addEventListener("mozbrowsershowmodalprompt", test5);
michael@0 120
michael@0 121 var script = 'data:,content.alert("test4");';
michael@0 122 mm.loadFrameScript(script, /* allowDelayedLoad = */ false);
michael@0 123 }
michael@0 124
michael@0 125 // test4 is a mozbrowsershowmodalprompt listener.
michael@0 126 function test5(e) {
michael@0 127 iframe.removeEventListener('mozbrowsershowmodalprompt', test4);
michael@0 128
michael@0 129 is(e.detail.message, 'test4');
michael@0 130 e.preventDefault(); // cause the page to block.
michael@0 131
michael@0 132 SimpleTest.executeSoon(test5a);
michael@0 133 }
michael@0 134
michael@0 135 function test5a() {
michael@0 136 iframe.addEventListener('mozbrowserloadend', test5b);
michael@0 137 iframe.src = browserElementTestHelpers.emptyPage2;
michael@0 138 }
michael@0 139
michael@0 140 function test5b() {
michael@0 141 iframe.removeEventListener('mozbrowserloadend', test5b);
michael@0 142 SimpleTest.finish();
michael@0 143 }
michael@0 144
michael@0 145 var prevNumPendingTests = null;
michael@0 146 function waitForPendingTests(next) {
michael@0 147 if (numPendingChildTests !== prevNumPendingTests) {
michael@0 148 dump("Waiting for end; " + numPendingChildTests + " pending tests\n");
michael@0 149 prevNumPendingTests = numPendingChildTests;
michael@0 150 }
michael@0 151
michael@0 152 if (numPendingChildTests > 0) {
michael@0 153 SimpleTest.executeSoon(function() { waitForPendingTests(next); });
michael@0 154 return;
michael@0 155 }
michael@0 156
michael@0 157 prevNumPendingTests = null;
michael@0 158 next();
michael@0 159 }
michael@0 160
michael@0 161 addEventListener('testready', runTest);

mercurial