summaryrefslogtreecommitdiffstats
path: root/browser/components/downloads/test/browser/browser_iframe_gone_mid_download.js
blob: a1b82fb9c290d11019b518f624c3c806b0fc0ba9 (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
const SAVE_PER_SITE_PREF = "browser.download.lastDir.savePerSite";

function test_deleted_iframe(perSitePref, windowOptions = {}) {
  return async function () {
    await SpecialPowers.pushPrefEnv({
      set: [[SAVE_PER_SITE_PREF, perSitePref]],
    });
    let { DownloadLastDir } = ChromeUtils.importESModule(
      "resource://gre/modules/DownloadLastDir.sys.mjs"
    );

    let win = await BrowserTestUtils.openNewBrowserWindow(windowOptions);
    let tab = await BrowserTestUtils.openNewForegroundTab(
      win.gBrowser,
      "about:mozilla"
    );

    let doc = tab.linkedBrowser.contentDocument;
    let iframe = doc.createElement("iframe");
    doc.body.appendChild(iframe);

    ok(iframe.contentWindow, "iframe should have a window");
    let gDownloadLastDir = new DownloadLastDir(iframe.contentWindow);
    let cw = iframe.contentWindow;
    let promiseIframeWindowGone = new Promise((resolve, reject) => {
      Services.obs.addObserver(function obs(subject, topic) {
        if (subject == cw) {
          Services.obs.removeObserver(obs, topic);
          resolve();
        }
      }, "dom-window-destroyed");
    });
    iframe.remove();
    await promiseIframeWindowGone;
    cw = null;
    ok(!iframe.contentWindow, "Managed to destroy iframe");

    let someDir = "blah";
    try {
      someDir = await gDownloadLastDir.getFileAsync("http://www.mozilla.org/");
    } catch (ex) {
      ok(
        false,
        "Got an exception trying to get the directory where things should be saved."
      );
      console.error(ex);
    }
    // NB: someDir can legitimately be null here when set, hence the 'blah' workaround:
    isnot(
      someDir,
      "blah",
      "Should get a file even after the window was destroyed."
    );

    try {
      gDownloadLastDir.setFile("http://www.mozilla.org/", null);
    } catch (ex) {
      ok(
        false,
        "Got an exception trying to set the directory where things should be saved."
      );
      console.error(ex);
    }

    await BrowserTestUtils.closeWindow(win);
  };
}

add_task(test_deleted_iframe(false));
add_task(test_deleted_iframe(false));
add_task(test_deleted_iframe(true, { private: true }));
add_task(test_deleted_iframe(true, { private: true }));