tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_link_prefetch.html (3770B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=580313
      5 -->
      6 <head>
      7  <title>Test Prefetch (bug 580313)</title>
      8  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      9  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     10 </head>
     11 <body>
     12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=580313">Mozilla Bug 580313</a>
     13 <p id="display"></p>
     14 <div id="content" style="display: none">
     15 
     16 </div>
     17 <pre id="test">
     18 <script type="application/javascript">
     19 
     20  SimpleTest.waitForExplicitFinish();
     21 
     22  var prefetch = SpecialPowers.Cc["@mozilla.org/prefetch-service;1"].
     23                   getService(SpecialPowers.Ci.nsIPrefetchService);
     24  var ios = SpecialPowers.Cc["@mozilla.org/network/io-service;1"].
     25            getService(SpecialPowers.Ci.nsIIOService);
     26 
     27  is(prefetch.hasMoreElements(), false, "No prefetches at the test start.");
     28 
     29  var linkElem = document.createElement('link');
     30  linkElem.rel = "prefetch";
     31 
     32  // Href is empty.
     33  document.head.appendChild(linkElem);
     34  is(prefetch.hasMoreElements(), false,
     35     "If href is not a valid uri, a prefetch has not been started.");
     36 
     37  // Change uri of an existing link. Now it is a valid uri and
     38  // a prefetch should start.
     39  linkElem.href = "https://example.com/1";
     40  is(prefetch.hasMoreElements(), true,
     41     "Setting the href to a valid uri has started a new prefetch.");
     42 
     43  // Removing a link, removes its prefetch.
     44  document.head.removeChild(linkElem);
     45  is(prefetch.hasMoreElements(), false,
     46     "Removing the link has canceled the prefetch.");
     47 
     48  // Add link again.
     49  document.head.appendChild(linkElem);
     50  is(prefetch.hasMoreElements(), true,
     51     "Adding link again, has started the prefetch again.");
     52 
     53  // Changing the href should cancel the current prefetch.
     54  linkElem.href = "https://example.com/2";
     55  is(prefetch.hasMoreElements(), true,
     56     "Changing href, a new prefetch has been started.");
     57  // To check if "https://example.com/1" prefetch has been canceled, we try to
     58  // cancel it using PrefetService. Since the prefetch for
     59  // "https://example.com/1" does not exist, the cancel will throw.
     60  var cancelError = 0;
     61  try {
     62    var uri = ios.newURI("https://example.com/1");
     63    prefetch.cancelPrefetchPreloadURI(uri, linkElem);
     64  } catch(e) {
     65    cancelError = 1;
     66  }
     67  is(cancelError, 1, "This prefetch has aleady been canceled");
     68 
     69  // Now cancel the right uri.
     70  cancelError = 0;
     71  try {
     72    var uri = ios.newURI("https://example.com/2");
     73    prefetch.cancelPrefetchPreloadURI(uri, linkElem);
     74  } catch(e) {
     75    cancelError = 1;
     76  }
     77  is(cancelError, 0, "This prefetch has been canceled successfully");
     78 
     79  is(prefetch.hasMoreElements(), false, "The prefetch has already been canceled.");
     80 
     81  // Removing the link will do nothing regarding prefetch service.
     82  document.head.removeChild(linkElem);
     83 
     84  // Adding two links to the same uri and removing one will not remove the other.
     85  document.head.appendChild(linkElem);
     86  is(prefetch.hasMoreElements(), true,
     87     "Added one prefetch for 'https://example.com/2'.");
     88 
     89  var linkElem2 = document.createElement('link');
     90  linkElem2.rel = "prefetch";
     91  linkElem2.href = "https://example.com/2";
     92  document.head.appendChild(linkElem2);
     93  is(prefetch.hasMoreElements(), true,
     94     "Added second prefetch for 'https://example.com/2'.");
     95 
     96  // Remove first link element. This should not remove the prefetch.
     97  document.head.removeChild(linkElem);
     98  is(prefetch.hasMoreElements(), true,
     99     "The prefetch for 'https://example.com/2' is still present.");
    100 
    101  // Remove the second link element. This should remove the prefetch.
    102  document.head.removeChild(linkElem2);
    103  is(prefetch.hasMoreElements(), false,
    104     "There is no prefetch.");
    105 
    106  SimpleTest.finish();
    107 </script>
    108 </pre>
    109 </body>
    110 </html>