summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_toolbar_drop_multiple_flavors.js
blob: 1f958989cf73f96cb119c2e6e9f851211b0852b4 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Check flavors priority when dropping a url/title tuple on the toolbar.

function getDataForType(url, title, type) {
  switch (type) {
    case "text/x-moz-url":
      return `${url}\n${title}`;
    case "text/plain":
      return url;
    case "text/html":
      return `<a href="${url}">${title}</a>`;
  }
  throw new Error("Unknown mime type");
}

async function testDragDrop(effect, mimeTypes) {
  const url = "https://www.mozilla.org/drag_drop_test/";
  const title = "Drag & Drop Test";
  let promiseItemAddedNotification = PlacesTestUtils.waitForNotification(
    "bookmark-added",
    events => events.some(e => e.url == url)
  );

  // Ensure there's no bookmark initially
  let bookmark = await PlacesUtils.bookmarks.fetch({ url });
  Assert.ok(!bookmark, "There should not be a bookmark to the given URL");

  // We use the toolbar as the drag source, as we just need almost any node
  // to simulate the drag.
  let toolbar = document.getElementById("PersonalToolbar");
  EventUtils.synthesizeDrop(
    toolbar,
    document.getElementById("PlacesToolbarItems"),
    [mimeTypes.map(type => ({ type, data: getDataForType(url, title, type) }))],
    effect,
    window
  );

  await promiseItemAddedNotification;

  // Verify that the drop produces exactly one bookmark.
  bookmark = await PlacesUtils.bookmarks.fetch({ url });
  Assert.ok(bookmark, "There should be exactly one bookmark");
  Assert.equal(bookmark.url, url, "Check bookmark URL is correct");
  Assert.equal(bookmark.title, title, "Check bookmark title was preserved");
  await PlacesUtils.bookmarks.remove(bookmark);
}

add_task(async function test() {
  registerCleanupFunction(() => PlacesUtils.bookmarks.eraseEverything());

  // Make sure the bookmarks bar is visible and restore its state on cleanup.
  let toolbar = document.getElementById("PersonalToolbar");
  if (toolbar.collapsed) {
    await promiseSetToolbarVisibility(toolbar, true);
    registerCleanupFunction(function () {
      return promiseSetToolbarVisibility(toolbar, false);
    });
  }

  // Test a bookmark drop for all of the mime types and effects.
  let mimeTypes = ["text/plain", "text/html", "text/x-moz-url"];
  let effects = ["move", "copy", "link"];
  for (let effect of effects) {
    await testDragDrop(effect, mimeTypes);
  }
});