summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_toolbar_drop_text.js
blob: 3e3ff84b39652c57af041de57b84a1e09455cb99 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
// Instead of loading EventUtils.js into the test scope in browser-test.js for all tests,
// we only need EventUtils.js for a few files which is why we are using loadSubScript.
var EventUtils = {};
Services.scriptloader.loadSubScript(
  "chrome://mochikit/content/tests/SimpleTest/EventUtils.js",
  EventUtils
);

add_task(async function test() {
  // Make sure the bookmarks bar is visible and restore its state on cleanup.
  let toolbar = document.getElementById("PersonalToolbar");
  ok(toolbar, "PersonalToolbar should not be null");

  if (toolbar.collapsed) {
    await promiseSetToolbarVisibility(toolbar, true);
    registerCleanupFunction(function () {
      return promiseSetToolbarVisibility(toolbar, false);
    });
  }

  // Setup the node we will use to be dropped. The actual node used does not
  // matter because we will set its data, effect, and mimeType manually.
  let placesItems = document.getElementById("PlacesToolbarItems");
  ok(placesItems, "PlacesToolbarItems should not be null");
  ok(
    placesItems.localName == "scrollbox",
    "PlacesToolbarItems should not be null"
  );

  /**
   * Simulates a drop of a URI onto the bookmarks bar.
   *
   * @param {string} aEffect
   *        The effect to use for the drop operation: move, copy, or link.
   * @param {string} aMimeType
   *        The mime type to use for the drop operation.
   */
  let simulateDragDrop = async function (aEffect, aMimeType) {
    const url = "http://www.mozilla.org/D1995729-A152-4e30-8329-469B01F30AA7";
    let promiseItemAddedNotification = PlacesTestUtils.waitForNotification(
      "bookmark-added",
      events => events.some(({ url: eventUrl }) => eventUrl == url)
    );

    // We use the toolbar as the drag source, as we just need almost any node
    // to simulate the drag. The actual data for the drop is passed via the
    // drag data. Note: The toolbar is used rather than another bookmark node,
    // as we need something that is immovable from a places perspective, as this
    // forces the move into a copy.
    EventUtils.synthesizeDrop(
      toolbar,
      placesItems,
      [[{ type: aMimeType, data: url }]],
      aEffect,
      window
    );

    await promiseItemAddedNotification;

    // Verify that the drop produces exactly one bookmark.
    let bookmark = await PlacesUtils.bookmarks.fetch({ url });
    Assert.ok(bookmark, "There should be exactly one bookmark");

    await PlacesUtils.bookmarks.remove(bookmark.guid);

    // Verify that we removed the bookmark successfully.
    Assert.equal(
      await PlacesUtils.bookmarks.fetch({ url }),
      null,
      "URI should be removed"
    );
  };

  /**
   * Simulates a drop of multiple URIs onto the bookmarks bar.
   *
   * @param {string} aEffect
   *        The effect to use for the drop operation: move, copy, or link.
   * @param {string} aMimeType
   *        The mime type to use for the drop operation.
   */
  let simulateDragDropMultiple = async function (aEffect, aMimeType) {
    const urls = [
      "http://www.mozilla.org/C54263C6-A484-46CF-8E2B-FE131586348A",
      "http://www.mozilla.org/71381257-61E6-4376-AF7C-BF3C5FD8870D",
      "http://www.mozilla.org/091A88BD-5743-4C16-A005-3D2EA3A3B71E",
    ];
    let data;
    if (aMimeType == "text/x-moz-url") {
      data = urls.map(spec => spec + "\n" + spec).join("\n");
    } else {
      data = urls.join("\n");
    }

    let promiseItemAddedNotification = PlacesTestUtils.waitForNotification(
      "bookmark-added",
      events => events.some(({ url }) => url == urls[2])
    );

    // See notes for EventUtils.synthesizeDrop in simulateDragDrop().
    EventUtils.synthesizeDrop(
      toolbar,
      placesItems,
      [[{ type: aMimeType, data }]],
      aEffect,
      window
    );

    await promiseItemAddedNotification;

    // Verify that the drop produces exactly one bookmark per each URL.
    for (let url of urls) {
      let bookmark = await PlacesUtils.bookmarks.fetch({ url });
      Assert.equal(
        typeof bookmark,
        "object",
        "There should be exactly one bookmark"
      );

      await PlacesUtils.bookmarks.remove(bookmark.guid);

      // Verify that we removed the bookmark successfully.
      Assert.equal(
        await PlacesUtils.bookmarks.fetch({ url }),
        null,
        "URI should be removed"
      );
    }
  };

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