tor-browser

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

browser_download.js (2502B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 var gTestRoot = getRootDirectory(gTestPath).replace(
      5  "chrome://mochitests/content/",
      6  "http://mochi.test:8888/"
      7 );
      8 
      9 function getFile(aFilename) {
     10  if (aFilename.startsWith("file:")) {
     11    var url = NetUtil.newURI(aFilename).QueryInterface(Ci.nsIFileURL);
     12    return url.file.clone();
     13  }
     14 
     15  var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
     16  file.initWithPath(aFilename);
     17  return file;
     18 }
     19 
     20 function windowObserver(win, topic) {
     21  if (topic !== "domwindowopened") {
     22    return;
     23  }
     24 
     25  win.addEventListener(
     26    "load",
     27    function () {
     28      if (
     29        win.document.documentURI ===
     30        "chrome://mozapps/content/downloads/unknownContentType.xhtml"
     31      ) {
     32        executeSoon(function () {
     33          let dialog = win.document.getElementById("unknownContentType");
     34          let button = dialog.getButton("accept");
     35          button.disabled = false;
     36          dialog.acceptDialog();
     37        });
     38      }
     39    },
     40    { once: true }
     41  );
     42 }
     43 
     44 function test() {
     45  waitForExplicitFinish();
     46 
     47  Services.ww.registerNotification(windowObserver);
     48 
     49  SpecialPowers.pushPrefEnv(
     50    {
     51      set: [
     52        ["dom.serviceWorkers.enabled", true],
     53        ["dom.serviceWorkers.exemptFromPerDomainMax", true],
     54        ["dom.serviceWorkers.testing.enabled", true],
     55      ],
     56    },
     57    function () {
     58      var url = gTestRoot + "download/window.html";
     59      var tab = BrowserTestUtils.addTab(gBrowser);
     60      gBrowser.selectedTab = tab;
     61 
     62      Downloads.getList(Downloads.ALL).then(function (downloadList) {
     63        var downloadListener;
     64 
     65        function downloadVerifier(aDownload) {
     66          if (aDownload.succeeded) {
     67            var file = getFile(aDownload.target.path);
     68            ok(file.exists(), "download completed");
     69            is(file.fileSize, 33, "downloaded file has correct size");
     70            file.remove(false);
     71            downloadList.remove(aDownload).catch(console.error);
     72            downloadList.removeView(downloadListener);
     73            gBrowser.removeTab(tab);
     74            Services.ww.unregisterNotification(windowObserver);
     75 
     76            executeSoon(finish);
     77          }
     78        }
     79 
     80        downloadListener = {
     81          onDownloadAdded: downloadVerifier,
     82          onDownloadChanged: downloadVerifier,
     83        };
     84 
     85        downloadList.addView(downloadListener);
     86 
     87        BrowserTestUtils.startLoadingURIString(gBrowser, url);
     88      });
     89    }
     90  );
     91 }