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

const { sinon } = ChromeUtils.importESModule(
  "resource://testing-common/Sinon.sys.mjs"
);
const sandbox = sinon.createSandbox();

const URL1 = "https://example.com/1/";
const URL2 = "https://example.com/2/";
const BOOKMARKLET_URL = `javascript: (() => {alert('Hello, World!');})();`;
let bookmarks;

registerCleanupFunction(async function () {
  sandbox.restore();
});

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");

  await PlacesUtils.bookmarks.eraseEverything();

  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");
  Assert.ok(placesItems, "PlacesToolbarItems should not be null");

  /**
   * Simulates a drop of a bookmarklet URI onto the bookmarks bar.
   *
   * @param {string} aEffect
   *        The effect to use for the drop operation: move, copy, or link.
   */
  let simulateDragDrop = async function (aEffect) {
    info("Simulates drag/drop of a new javascript:URL to the bookmarks");
    await withBookmarksDialog(
      true,
      function openDialog() {
        EventUtils.synthesizeDrop(
          toolbar,
          placesItems,
          [[{ type: "text/x-moz-url", data: BOOKMARKLET_URL }]],
          aEffect,
          window
        );
      },
      async function testNameField(dialogWin) {
        info("Checks that there is a javascript:URL in ShowBookmarksDialog");

        let location = dialogWin.document.getElementById(
          "editBMPanel_locationField"
        ).value;

        Assert.equal(
          location,
          BOOKMARKLET_URL,
          "Should have opened the ShowBookmarksDialog with the correct bookmarklet url to be bookmarked"
        );
      }
    );

    info("Simulates drag/drop of a new URL to the bookmarks");
    let spy = sandbox
      .stub(PlacesUIUtils, "showBookmarkDialog")
      .returns(Promise.resolve());

    let promise = PlacesTestUtils.waitForNotification(
      "bookmark-added",
      events => events.some(({ url }) => url == URL1)
    );

    EventUtils.synthesizeDrop(
      toolbar,
      placesItems,
      [[{ type: "text/x-moz-url", data: URL1 }]],
      aEffect,
      window
    );

    await promise;
    Assert.ok(spy.notCalled, "ShowBookmarksDialog on drop not called for url");
    sandbox.restore();
  };

  let effects = ["copy", "link"];
  for (let effect of effects) {
    await simulateDragDrop(effect);
  }

  info("Move of existing bookmark / bookmarklet on toolbar");
  // Clean previous bookmarks to ensure right ids count.
  await PlacesUtils.bookmarks.eraseEverything();

  info("Insert list of bookamrks to have bookmarks (ids) for moving");
  // Ensure bookmarks are visible on the toolbar.
  let promiseBookmarksOnToolbar = BrowserTestUtils.waitForMutationCondition(
    placesItems,
    { childList: true },
    () => placesItems.childNodes.length == 3
  );
  bookmarks = await PlacesUtils.bookmarks.insertTree({
    guid: PlacesUtils.bookmarks.toolbarGuid,
    children: [
      {
        title: "bm1",
        url: URL1,
      },
      {
        title: "bm2",
        url: URL2,
      },
      {
        title: "bookmarklet",
        url: BOOKMARKLET_URL,
      },
    ],
  });
  await promiseBookmarksOnToolbar;

  let spy = sandbox
    .stub(PlacesUIUtils, "showBookmarkDialog")
    .returns(Promise.resolve());

  info("Moving existing Bookmark from position [1] to [0] on Toolbar");
  let urlMoveNotification = PlacesTestUtils.waitForNotification(
    "bookmark-moved",
    events =>
      events.some(
        e =>
          e.parentGuid === PlacesUtils.bookmarks.toolbarGuid &&
          e.oldParentGuid === PlacesUtils.bookmarks.toolbarGuid &&
          e.oldIndex == 1 &&
          e.index == 0
      )
  );

  EventUtils.synthesizeDrop(
    placesItems,
    placesItems.childNodes[0],
    [
      [
        {
          type: "text/x-moz-place",
          data: PlacesUtils.wrapNode(
            placesItems.childNodes[1]._placesNode,
            "text/x-moz-place"
          ),
        },
      ],
    ],
    "move",
    window
  );

  await urlMoveNotification;
  Assert.ok(spy.notCalled, "ShowBookmarksDialog not called on move for url");

  info("Moving existing Bookmarklet from position [2] to [1] on Toolbar");
  let bookmarkletMoveNotificatio = PlacesTestUtils.waitForNotification(
    "bookmark-moved",
    events =>
      events.some(
        e =>
          e.parentGuid === PlacesUtils.bookmarks.toolbarGuid &&
          e.oldParentGuid === PlacesUtils.bookmarks.toolbarGuid &&
          e.oldIndex == 2 &&
          e.index == 1
      )
  );

  EventUtils.synthesizeDrop(
    toolbar,
    placesItems.childNodes[1],
    [
      [
        {
          type: "text/x-moz-place",
          data: PlacesUtils.wrapNode(
            placesItems.childNodes[2]._placesNode,
            "text/x-moz-place"
          ),
        },
      ],
    ],
    "move",
    window
  );

  await bookmarkletMoveNotificatio;
  Assert.ok(spy.notCalled, "ShowBookmarksDialog not called on move for url");
  sandbox.restore();
});