summaryrefslogtreecommitdiffstats
path: root/browser/components/enterprisepolicies/tests/browser/browser_policy_allowfileselectiondialogs.js
blob: 94f6bba631a91c4ae7ea6ac091b84b5c588ce162 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

add_setup(async function setup() {
  await setupPolicyEngineWithJson({
    policies: {
      AllowFileSelectionDialogs: false,
    },
  });
});

add_task(async function test_file_commands_disabled() {
  // Since testing will apply the policy after the browser has already started,
  // we will need to open a new window to actually see changes from the policy
  let newWin = await BrowserTestUtils.openNewBrowserWindow();

  let savePageCommand = newWin.document.getElementById("Browser:SavePage");
  let openFileCommand = newWin.document.getElementById("Browser:OpenFile");

  Assert.equal(
    savePageCommand.getAttribute("disabled"),
    "true",
    "Browser:SavePage command is disabled"
  );
  Assert.equal(
    openFileCommand.getAttribute("disabled"),
    "true",
    "Browser:OpenFile command is disabled"
  );
  await BrowserTestUtils.closeWindow(newWin);
});

add_task(async function test_file_buttons_disabled() {
  // Since testing will apply the policy after the browser has already started,
  // we will need to open a new window to actually see changes from the policy
  let newWin = await BrowserTestUtils.openNewBrowserWindow();

  newWin.CustomizableUI.addWidgetToArea("save-page-button", "nav-bar");
  newWin.CustomizableUI.addWidgetToArea("open-file-button", "nav-bar");

  let savePageButton = newWin.document.getElementById("save-page-button");
  let openFileButton = newWin.document.getElementById("open-file-button");

  Assert.equal(
    savePageButton.getAttribute("disabled"),
    "true",
    "save-page-button is disabled"
  );
  Assert.equal(
    openFileButton.getAttribute("disabled"),
    "true",
    "open-file-button is disabled"
  );

  newWin.CustomizableUI.reset();
  await BrowserTestUtils.closeWindow(newWin);
});

add_task(async function test_context_menu_items_disabled() {
  await BrowserTestUtils.withNewTab("https://example.org/", async browser => {
    let contextMenu = document.getElementById("contentAreaContextMenu");
    let promiseContextMenuOpen = BrowserTestUtils.waitForEvent(
      contextMenu,
      "popupshown"
    );

    await BrowserTestUtils.synthesizeMouse(
      "a",
      0,
      0,
      {
        type: "contextmenu",
        button: 2,
        centered: true,
      },
      browser
    );

    await promiseContextMenuOpen;

    for (let item of [
      "context-saveimage",
      "context-savepage",
      "context-savelink",
      "context-savevideo",
      "context-saveaudio",
      "context-video-saveimage",
      "context-saveaudio",
    ]) {
      Assert.equal(
        document.getElementById(item).disabled,
        true,
        `${item} item is disabled`
      );
    }

    contextMenu.hidePopup();
  });
});

add_task(async function test_notification() {
  // Since testing will apply the policy after the browser has already started,
  // we will need to open a new window to actually see changes from the policy
  let newWin = await BrowserTestUtils.openNewBrowserWindow();

  await BrowserTestUtils.withNewTab(
    { gBrowser: newWin.gBrowser, url: "https://example.org/" },
    async browser => {
      await SpecialPowers.spawn(browser, [], async function () {
        let elem = content.document.createElement("input");
        elem.setAttribute("type", "file");

        content.document.notifyUserGestureActivation();
        elem.showPicker();
      });

      let notificationBox = browser.getTabBrowser().getNotificationBox(browser);

      let notification = await TestUtils.waitForCondition(() =>
        notificationBox.getNotificationWithValue("filepicker-blocked")
      );

      Assert.ok(
        notification,
        "filepicker-blocked notification appears when showPicker is called"
      );
    }
  );
  await BrowserTestUtils.closeWindow(newWin);
});

add_task(async function test_cancel_event() {
  await BrowserTestUtils.withNewTab("https://example.org/", async browser => {
    let eventType = await SpecialPowers.spawn(browser, [], async function () {
      let elem = content.document.createElement("input");
      elem.setAttribute("type", "file");
      let cancelPromise = new Promise(resolve =>
        elem.addEventListener("cancel", resolve, { once: true })
      );
      content.document.notifyUserGestureActivation();
      elem.showPicker();
      let event = await cancelPromise;
      return event.type;
    });

    Assert.equal(
      eventType,
      "cancel",
      "cancel event should be dispatched when showPicker is called"
    );
  });
});

add_task(async function test_nsIFilePicker_open() {
  let picker = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);

  picker.init(window, "", Ci.nsIFilePicker.modeSave);

  let result = await new Promise(resolve => picker.open(res => resolve(res)));

  Assert.equal(
    result,
    Ci.nsIFilePicker.returnCancel,
    "nsIFilePicker.open callback should immediately answer returnCancel"
  );
});