summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/contextMenu/browser_contextmenu_save_blocked.js
blob: dbfc57d149b7dc2e375469681cebedfd66af4f5e (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

var MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(window);

function mockPromptService() {
  let { prompt } = Services;
  let promptService = {
    QueryInterface: ChromeUtils.generateQI(["nsIPromptService"]),
    alert: () => {},
  };
  Services.prompt = promptService;
  registerCleanupFunction(() => {
    Services.prompt = prompt;
  });
  return promptService;
}

add_task(async function test_save_link_blocked_by_extension() {
  let ext = ExtensionTestUtils.loadExtension({
    manifest: {
      browser_specific_settings: { gecko: { id: "cancel@test" } },
      name: "Cancel Test",
      permissions: ["webRequest", "webRequestBlocking", "<all_urls>"],
    },

    background() {
      // eslint-disable-next-line no-undef
      browser.webRequest.onBeforeRequest.addListener(
        details => {
          return { cancel: details.url === "http://example.com/" };
        },
        { urls: ["*://*/*"] },
        ["blocking"]
      );
    },
  });
  await ext.startup();

  await BrowserTestUtils.withNewTab(
    `data:text/html;charset=utf-8,<a href="http://example.com">Download</a>`,
    async browser => {
      let menu = document.getElementById("contentAreaContextMenu");
      let popupShown = BrowserTestUtils.waitForEvent(menu, "popupshown");
      BrowserTestUtils.synthesizeMouseAtCenter(
        "a[href]",
        { type: "contextmenu", button: 2 },
        browser
      );
      await popupShown;

      await new Promise(resolve => {
        let promptService = mockPromptService();
        promptService.alert = (window, title, msg) => {
          is(
            msg,
            "The download cannot be saved because it is blocked by Cancel Test.",
            "prompt should be shown"
          );
          setTimeout(resolve, 0);
        };

        MockFilePicker.showCallback = function(fp) {
          ok(false, "filepicker should never been shown");
          setTimeout(resolve, 0);
          return Ci.nsIFilePicker.returnCancel;
        };
        menu.activateItem(menu.querySelector("#context-savelink"));
      });
    }
  );

  await ext.unload();
});