summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/browser/browser_dragdrop.js
blob: ae8625a18aee12480e7622d457c5418ec2d29d9d (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

const ABOUT_ADDONS_URL = "chrome://mozapps/content/extensions/aboutaddons.html";

const dragService = Cc["@mozilla.org/widget/dragservice;1"].getService(
  Ci.nsIDragService
);

// Test that the drag-drop-addon-installer component installs add-ons and is
// included in about:addons. There is an issue with EventUtils.synthesizeDrop
// where it throws an exception when you give it an subbrowser so we test
// the component directly.

async function checkInstallConfirmation(...names) {
  let notificationCount = 0;
  let observer = {
    observe(aSubject, aTopic, aData) {
      let installInfo = aSubject.wrappedJSObject;
      isnot(
        installInfo.browser,
        null,
        "Notification should have non-null browser"
      );

      is(
        installInfo.installs.length,
        1,
        "Got one AddonInstall instance as expected"
      );

      Assert.deepEqual(
        installInfo.installs[0].installTelemetryInfo,
        { source: "about:addons", method: "drag-and-drop" },
        "Got the expected installTelemetryInfo"
      );

      notificationCount++;
    },
  };
  Services.obs.addObserver(observer, "addon-install-started");

  let results = [];

  let promise = promisePopupNotificationShown("addon-webext-permissions");
  for (let i = 0; i < names.length; i++) {
    let panel = await promise;
    let name = panel.getAttribute("name");
    results.push(name);

    info(`Saw install for ${name}`);
    if (results.length < names.length) {
      info(
        `Waiting for installs for ${names.filter(n => !results.includes(n))}`
      );

      promise = promisePopupNotificationShown("addon-webext-permissions");
    }
    panel.secondaryButton.click();
  }

  Assert.deepEqual(results.sort(), names.sort(), "Got expected installs");

  is(
    notificationCount,
    names.length,
    `Saw ${names.length} addon-install-started notification`
  );
  Services.obs.removeObserver(observer, "addon-install-started");
}

function getDragOverTarget(win) {
  return win.document.querySelector("categories-box");
}

function getDropTarget(win) {
  return win.document.querySelector("drag-drop-addon-installer");
}

function withTestPage(fn) {
  return BrowserTestUtils.withNewTab(
    { url: ABOUT_ADDONS_URL, gBrowser },
    async browser => {
      let win = browser.contentWindow;
      await win.customElements.whenDefined("drag-drop-addon-installer");
      await fn(browser);
    }
  );
}

function initDragSession({ dragData, dropEffect }) {
  let dropAction;
  switch (dropEffect) {
    case null:
    case undefined:
    case "move":
      dropAction = _EU_Ci.nsIDragService.DRAGDROP_ACTION_MOVE;
      break;
    case "copy":
      dropAction = _EU_Ci.nsIDragService.DRAGDROP_ACTION_COPY;
      break;
    case "link":
      dropAction = _EU_Ci.nsIDragService.DRAGDROP_ACTION_LINK;
      break;
    default:
      throw new Error(`${dropEffect} is an invalid drop effect value`);
  }

  const dataTransfer = new DataTransfer();
  dataTransfer.dropEffect = dropEffect;

  for (let i = 0; i < dragData.length; i++) {
    const item = dragData[i];
    for (let j = 0; j < item.length; j++) {
      dataTransfer.mozSetDataAt(item[j].type, item[j].data, i);
    }
  }

  dragService.startDragSessionForTests(dropAction);
  const session = dragService.getCurrentSession();
  session.dataTransfer = dataTransfer;

  return session;
}

async function simulateDragAndDrop(win, dragData) {
  const dropTarget = getDropTarget(win);
  const dragOverTarget = getDragOverTarget(win);
  const dropEffect = "move";

  const session = initDragSession({ dragData, dropEffect });

  info("Simulate drag over and wait for the drop target to be visible");

  EventUtils.synthesizeDragOver(
    dragOverTarget,
    dragOverTarget,
    dragData,
    dropEffect,
    win
  );

  // This make sure that the fake dataTransfer has still
  // the expected drop effect after the synthesizeDragOver call.
  session.dataTransfer.dropEffect = "move";

  await BrowserTestUtils.waitForCondition(
    () => !dropTarget.hidden,
    "Wait for the drop target element to be visible"
  );

  info("Simulate drop dragData on drop target");

  EventUtils.synthesizeDropAfterDragOver(
    null,
    session.dataTransfer,
    dropTarget,
    win,
    { _domDispatchOnly: true }
  );

  dragService.endDragSession(true);
}

// Simulates dropping a URL onto the manager
add_task(async function test_drop_url() {
  for (let fileType of ["xpi", "zip"]) {
    await withTestPage(async browser => {
      const url = TESTROOT + `addons/browser_dragdrop1.${fileType}`;
      const promise = checkInstallConfirmation("Drag Drop test 1");

      await simulateDragAndDrop(browser.contentWindow, [
        [{ type: "text/x-moz-url", data: url }],
      ]);

      await promise;
    });
  }
});

// Simulates dropping a file onto the manager
add_task(async function test_drop_file() {
  for (let fileType of ["xpi", "zip"]) {
    await withTestPage(async browser => {
      let fileurl = get_addon_file_url(`browser_dragdrop1.${fileType}`);
      let promise = checkInstallConfirmation("Drag Drop test 1");

      await simulateDragAndDrop(browser.contentWindow, [
        [{ type: "application/x-moz-file", data: fileurl.file }],
      ]);

      await promise;
    });
  }
});

// Simulates dropping two urls onto the manager
add_task(async function test_drop_multiple_urls() {
  await withTestPage(async browser => {
    let url1 = TESTROOT + "addons/browser_dragdrop1.xpi";
    let url2 = TESTROOT2 + "addons/browser_dragdrop2.zip";
    let promise = checkInstallConfirmation(
      "Drag Drop test 1",
      "Drag Drop test 2"
    );

    await simulateDragAndDrop(browser.contentWindow, [
      [{ type: "text/x-moz-url", data: url1 }],
      [{ type: "text/x-moz-url", data: url2 }],
    ]);

    await promise;
  });
}).skip(); // TODO(rpl): this fails because mozSetDataAt throws IndexSizeError.

// Simulates dropping two files onto the manager
add_task(async function test_drop_multiple_files() {
  await withTestPage(async browser => {
    let fileurl1 = get_addon_file_url("browser_dragdrop1.zip");
    let fileurl2 = get_addon_file_url("browser_dragdrop2.xpi");
    let promise = checkInstallConfirmation(
      "Drag Drop test 1",
      "Drag Drop test 2"
    );

    await simulateDragAndDrop(browser.contentWindow, [
      [{ type: "application/x-moz-file", data: fileurl1.file }],
      [{ type: "application/x-moz-file", data: fileurl2.file }],
    ]);

    await promise;
  });
}).skip(); // TODO(rpl): this fails because mozSetDataAt throws IndexSizeError.

// Simulates dropping a file and a url onto the manager (weird, but should still work)
add_task(async function test_drop_file_and_url() {
  await withTestPage(async browser => {
    let url = TESTROOT + "addons/browser_dragdrop1.xpi";
    let fileurl = get_addon_file_url("browser_dragdrop2.zip");
    let promise = checkInstallConfirmation(
      "Drag Drop test 1",
      "Drag Drop test 2"
    );

    await simulateDragAndDrop(browser.contentWindow, [
      [{ type: "text/x-moz-url", data: url }],
      [{ type: "application/x-moz-file", data: fileurl.file }],
    ]);

    await promise;
  });
}).skip(); // TODO(rpl): this fails because mozSetDataAt throws IndexSizeError.

// Test that drag-and-drop of an incompatible addon generates
// an error.
add_task(async function test_drop_incompat_file() {
  await withTestPage(async browser => {
    let url = `${TESTROOT}/addons/browser_dragdrop_incompat.xpi`;

    let panelPromise = promisePopupNotificationShown("addon-install-failed");
    await simulateDragAndDrop(browser.contentWindow, [
      [{ type: "text/x-moz-url", data: url }],
    ]);

    let panel = await panelPromise;
    ok(panel, "Got addon-install-failed popup");
    panel.button.click();
  });
});