1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/workers/test/test_suspend.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,148 @@ 1.4 +<!-- 1.5 + Any copyright is dedicated to the Public Domain. 1.6 + http://creativecommons.org/publicdomain/zero/1.0/ 1.7 +--> 1.8 +<!DOCTYPE HTML> 1.9 +<html> 1.10 +<head> 1.11 + <meta charset="utf-8"> 1.12 + <title>Test for DOM Worker Threads</title> 1.13 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.14 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.15 +</head> 1.16 +<body> 1.17 +<p id="display"></p> 1.18 +<div id="content" style="display: none"></div> 1.19 +<pre id="test"> 1.20 +<iframe id="workerFrame" src="suspend_iframe.html" onload="subframeLoaded();"> 1.21 +</iframe> 1.22 +<script class="testbody" type="text/javascript"> 1.23 + 1.24 + SimpleTest.waitForExplicitFinish(); 1.25 + 1.26 + var iframe; 1.27 + var lastCount; 1.28 + 1.29 + var suspended = false; 1.30 + var resumed = false; 1.31 + var finished = false; 1.32 + 1.33 + var interval; 1.34 + var oldMessageCount; 1.35 + var waitCount = 0; 1.36 + 1.37 + function setCachePref(enabled) { 1.38 + var prefBranch = SpecialPowers.Cc["@mozilla.org/preferences-service;1"] 1.39 + .getService(SpecialPowers.Ci.nsIPrefBranch); 1.40 + if (enabled) { 1.41 + prefBranch.setBoolPref("browser.sessionhistory.cache_subframes", true); 1.42 + } 1.43 + else { 1.44 + try { 1.45 + prefBranch.clearUserPref("browser.sessionhistory.cache_subframes"); 1.46 + } catch (e) { /* Pref didn't exist, meh */ } 1.47 + } 1.48 + } 1.49 + 1.50 + function finishTest() { 1.51 + if (finished) { 1.52 + return; 1.53 + } 1.54 + finished = true; 1.55 + setCachePref(false); 1.56 + iframe.terminateWorker(); 1.57 + SimpleTest.finish(); 1.58 + } 1.59 + 1.60 + function waitInterval() { 1.61 + if (finished) { 1.62 + return; 1.63 + } 1.64 + is(iframe.location, "about:blank", "Wrong url!"); 1.65 + is(suspended, true, "Not suspended?"); 1.66 + is(resumed, false, "Already resumed?!"); 1.67 + is(lastCount, oldMessageCount, "Received a message while suspended!"); 1.68 + if (++waitCount == 5) { 1.69 + clearInterval(interval); 1.70 + resumed = true; 1.71 + iframe.history.back(); 1.72 + } 1.73 + } 1.74 + 1.75 + function badOnloadCallback() { 1.76 + if (finished) { 1.77 + return; 1.78 + } 1.79 + ok(false, "We don't want suspend_iframe.html to fire a new load event, we want it to come out of the bfcache!"); 1.80 + finishTest(); 1.81 + } 1.82 + 1.83 + function suspendCallback() { 1.84 + if (finished) { 1.85 + return; 1.86 + } 1.87 + is(iframe.location, "about:blank", "Wrong url!"); 1.88 + is(suspended, false, "Already suspended?"); 1.89 + is(resumed, false, "Already resumed?"); 1.90 + setCachePref(false); 1.91 + suspended = true; 1.92 + var iframeElement = document.getElementById("workerFrame"); 1.93 + iframeElement.onload = badOnloadCallback; 1.94 + oldMessageCount = lastCount; 1.95 + interval = setInterval(waitInterval, 1000); 1.96 + } 1.97 + 1.98 + function messageCallback(data) { 1.99 + if (finished) { 1.100 + return; 1.101 + } 1.102 + 1.103 + if (!suspended) { 1.104 + ok(lastCount === undefined || lastCount == data - 1, 1.105 + "Got good data, lastCount = " + lastCount + ", data = " + data); 1.106 + lastCount = data; 1.107 + if (lastCount == 25) { 1.108 + setCachePref(true); 1.109 + iframe.location = "about:blank"; 1.110 + // We want suspend_iframe.html to go into bfcache, so we need to flush 1.111 + // out all pending notifications. Otherwise, if they're flushed too 1.112 + // late, they could kick us out of the bfcache again. 1.113 + iframe.document.body.offsetTop; 1.114 + } 1.115 + return; 1.116 + } 1.117 + 1.118 + var newLocation = 1.119 + window.location.toString().replace("test_suspend.html", 1.120 + "suspend_iframe.html"); 1.121 + is(newLocation.indexOf(iframe.location.toString()), 0, "Wrong url!"); 1.122 + is(resumed, true, "Got message before resumed!"); 1.123 + is(lastCount, data - 1, "Missed a message, suspend failed!"); 1.124 + finishTest(); 1.125 + } 1.126 + 1.127 + function errorCallback(data) { 1.128 + if (finished) { 1.129 + return; 1.130 + } 1.131 + ok(false, "Iframe had an error: '" + data + "'"); 1.132 + finishTest(); 1.133 + } 1.134 + 1.135 + function subframeLoaded() { 1.136 + if (finished) { 1.137 + return; 1.138 + } 1.139 + var iframeElement = document.getElementById("workerFrame"); 1.140 + iframeElement.onload = suspendCallback; 1.141 + 1.142 + iframe = iframeElement.contentWindow; 1.143 + ok(iframe, "No iframe?!"); 1.144 + 1.145 + iframe.startWorker(messageCallback, errorCallback); 1.146 + } 1.147 + 1.148 +</script> 1.149 +</pre> 1.150 +</body> 1.151 +</html>