Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / url / tests / browser_download_after_revoke.js
blobf145b362813d8cb34e018fd76961edbbfa34ca71
1 async function test() {
2   waitForExplicitFinish();
3   const target = "http://example.com/browser/dom/url/tests/empty.html";
4   info("Loading download page...");
5   let tab = BrowserTestUtils.addTab(gBrowser, target);
6   registerCleanupFunction(function () {
7     gBrowser.removeTab(tab);
8     window.restore();
9   });
10   gBrowser.selectedTab = tab;
11   BrowserTestUtils.browserLoaded(tab.linkedBrowser, false, target).then(
12     async () => {
13       info("Page loaded.");
14       let allDownloads = await Downloads.getList(Downloads.ALL);
15       let started = new Promise(resolve => {
16         if (
17           Services.prefs.getBoolPref(
18             "browser.download.always_ask_before_handling_new_types",
19             false
20           )
21         ) {
22           // If the download modal is enabled, wait for it to open and declare the
23           // download to have begun when we see it.
24           let listener = {
25             onOpenWindow(aXULWindow) {
26               info("Download modal shown...");
27               Services.wm.removeListener(listener);
29               let domwindow = aXULWindow.docShell.domWindow;
30               function onModalLoad() {
31                 domwindow.removeEventListener("load", onModalLoad, true);
33                 is(
34                   domwindow.document.location.href,
35                   "chrome://mozapps/content/downloads/unknownContentType.xhtml",
36                   "Download modal loaded..."
37                 );
39                 domwindow.close();
40                 info("Download modal closed.");
41                 resolve();
42               }
44               domwindow.addEventListener("load", onModalLoad, true);
45             },
46             onCloseWindow() {},
47           };
49           Services.wm.addListener(listener);
50         } else {
51           // With no download modal, the download will begin on its own, so we need
52           // to wait to be notified by the downloads list when that happens.
53           let downloadView = {
54             onDownloadAdded(download) {
55               ok(true, "Download was started.");
56               download.cancel();
57               allDownloads.removeView(this);
58               allDownloads.removeFinished();
59               resolve();
60             },
61           };
62           allDownloads.addView(downloadView);
63         }
64       });
66       let revoked = SpecialPowers.spawn(
67         tab.linkedBrowser,
68         [],
69         () =>
70           new Promise(resolve => {
71             info("Creating BlobURL...");
72             let blob = new content.Blob(["test"], { type: "text/plain" });
73             let url = content.URL.createObjectURL(blob);
75             let link = content.document.createElement("a");
76             link.href = url;
77             link.download = "example.txt";
78             content.document.body.appendChild(link);
79             info("Clicking HTMLAnchorElement...");
80             link.click();
82             content.URL.revokeObjectURL(url);
83             info("BlobURL revoked.");
84             resolve();
85           })
86       );
88       info("Waiting for async activities...");
89       await Promise.all([revoked, started]);
90       ok(true, "Exiting test.");
91       finish();
92     }
93   );