summaryrefslogtreecommitdiffstats
path: root/dom/url/tests/browser_download_after_revoke.js
blob: f145b362813d8cb34e018fd76961edbbfa34ca71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
async function test() {
  waitForExplicitFinish();
  const target = "http://example.com/browser/dom/url/tests/empty.html";
  info("Loading download page...");
  let tab = BrowserTestUtils.addTab(gBrowser, target);
  registerCleanupFunction(function () {
    gBrowser.removeTab(tab);
    window.restore();
  });
  gBrowser.selectedTab = tab;
  BrowserTestUtils.browserLoaded(tab.linkedBrowser, false, target).then(
    async () => {
      info("Page loaded.");
      let allDownloads = await Downloads.getList(Downloads.ALL);
      let started = new Promise(resolve => {
        if (
          Services.prefs.getBoolPref(
            "browser.download.always_ask_before_handling_new_types",
            false
          )
        ) {
          // If the download modal is enabled, wait for it to open and declare the
          // download to have begun when we see it.
          let listener = {
            onOpenWindow(aXULWindow) {
              info("Download modal shown...");
              Services.wm.removeListener(listener);

              let domwindow = aXULWindow.docShell.domWindow;
              function onModalLoad() {
                domwindow.removeEventListener("load", onModalLoad, true);

                is(
                  domwindow.document.location.href,
                  "chrome://mozapps/content/downloads/unknownContentType.xhtml",
                  "Download modal loaded..."
                );

                domwindow.close();
                info("Download modal closed.");
                resolve();
              }

              domwindow.addEventListener("load", onModalLoad, true);
            },
            onCloseWindow() {},
          };

          Services.wm.addListener(listener);
        } else {
          // With no download modal, the download will begin on its own, so we need
          // to wait to be notified by the downloads list when that happens.
          let downloadView = {
            onDownloadAdded(download) {
              ok(true, "Download was started.");
              download.cancel();
              allDownloads.removeView(this);
              allDownloads.removeFinished();
              resolve();
            },
          };
          allDownloads.addView(downloadView);
        }
      });

      let revoked = SpecialPowers.spawn(
        tab.linkedBrowser,
        [],
        () =>
          new Promise(resolve => {
            info("Creating BlobURL...");
            let blob = new content.Blob(["test"], { type: "text/plain" });
            let url = content.URL.createObjectURL(blob);

            let link = content.document.createElement("a");
            link.href = url;
            link.download = "example.txt";
            content.document.body.appendChild(link);
            info("Clicking HTMLAnchorElement...");
            link.click();

            content.URL.revokeObjectURL(url);
            info("BlobURL revoked.");
            resolve();
          })
      );

      info("Waiting for async activities...");
      await Promise.all([revoked, started]);
      ok(true, "Exiting test.");
      finish();
    }
  );
}