dom/workers/test/test_suspend.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 <!--
     2   Any copyright is dedicated to the Public Domain.
     3   http://creativecommons.org/publicdomain/zero/1.0/
     4 -->
     5 <!DOCTYPE HTML>
     6 <html>
     7 <head>
     8   <meta charset="utf-8">
     9   <title>Test for DOM Worker Threads</title>
    10   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    11   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    12 </head>
    13 <body>
    14 <p id="display"></p>
    15 <div id="content" style="display: none"></div>
    16 <pre id="test">
    17 <iframe id="workerFrame" src="suspend_iframe.html" onload="subframeLoaded();">
    18 </iframe>
    19 <script class="testbody" type="text/javascript">
    21   SimpleTest.waitForExplicitFinish();
    23   var iframe;
    24   var lastCount;
    26   var suspended = false;
    27   var resumed = false;
    28   var finished = false;
    30   var interval;
    31   var oldMessageCount;
    32   var waitCount = 0;
    34   function setCachePref(enabled) {
    35     var prefBranch = SpecialPowers.Cc["@mozilla.org/preferences-service;1"]
    36                                   .getService(SpecialPowers.Ci.nsIPrefBranch);
    37     if (enabled) {
    38       prefBranch.setBoolPref("browser.sessionhistory.cache_subframes", true);
    39     }
    40     else {
    41       try {
    42         prefBranch.clearUserPref("browser.sessionhistory.cache_subframes");
    43       } catch (e) { /* Pref didn't exist, meh */ }
    44     }
    45   }
    47   function finishTest() {
    48     if (finished) {
    49       return;
    50     }
    51     finished = true;
    52     setCachePref(false);
    53     iframe.terminateWorker();
    54     SimpleTest.finish();
    55   }
    57   function waitInterval() {
    58     if (finished) {
    59       return;
    60     }
    61     is(iframe.location, "about:blank", "Wrong url!");
    62     is(suspended, true, "Not suspended?");
    63     is(resumed, false, "Already resumed?!");
    64     is(lastCount, oldMessageCount, "Received a message while suspended!");
    65     if (++waitCount == 5) {
    66       clearInterval(interval);
    67       resumed = true;
    68       iframe.history.back();
    69     }
    70   }
    72   function badOnloadCallback() {
    73     if (finished) {
    74       return;
    75     }
    76     ok(false, "We don't want suspend_iframe.html to fire a new load event, we want it to come out of the bfcache!");
    77     finishTest();
    78   }
    80   function suspendCallback() {
    81     if (finished) {
    82       return;
    83     }
    84     is(iframe.location, "about:blank", "Wrong url!");
    85     is(suspended, false, "Already suspended?");
    86     is(resumed, false, "Already resumed?");
    87     setCachePref(false);
    88     suspended = true;
    89     var iframeElement = document.getElementById("workerFrame");
    90     iframeElement.onload = badOnloadCallback;
    91     oldMessageCount = lastCount;
    92     interval = setInterval(waitInterval, 1000);
    93   }
    95   function messageCallback(data) {
    96     if (finished) {
    97       return;
    98     }
   100     if (!suspended) {
   101       ok(lastCount === undefined || lastCount == data - 1,
   102          "Got good data, lastCount = " + lastCount + ", data = " + data);
   103       lastCount = data;
   104       if (lastCount == 25) {
   105         setCachePref(true);
   106         iframe.location = "about:blank";
   107         // We want suspend_iframe.html to go into bfcache, so we need to flush
   108         // out all pending notifications. Otherwise, if they're flushed too
   109         // late, they could kick us out of the bfcache again.
   110         iframe.document.body.offsetTop;
   111       }
   112       return;
   113     }
   115     var newLocation =
   116       window.location.toString().replace("test_suspend.html",
   117                                          "suspend_iframe.html");
   118     is(newLocation.indexOf(iframe.location.toString()), 0, "Wrong url!");
   119     is(resumed, true, "Got message before resumed!");
   120     is(lastCount, data - 1, "Missed a message, suspend failed!");
   121     finishTest();
   122   }
   124   function errorCallback(data) {
   125     if (finished) {
   126       return;
   127     }
   128     ok(false, "Iframe had an error: '" + data + "'");
   129     finishTest();
   130   }
   132   function subframeLoaded() {
   133     if (finished) {
   134       return;
   135     }
   136     var iframeElement = document.getElementById("workerFrame");
   137     iframeElement.onload = suspendCallback;
   139     iframe = iframeElement.contentWindow;
   140     ok(iframe, "No iframe?!");
   142     iframe.startWorker(messageCallback, errorCallback);
   143   }
   145 </script>
   146 </pre>
   147 </body>
   148 </html>

mercurial